2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "c-family/c-pragma.h"
33 #include "diagnostic-core.h"
38 #include "c-family/c-common.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct GTY(()) tree_check
{
51 /* The value associated with the token. */
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check
, gc
)* checks
;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope
;
62 typedef struct GTY (()) cp_token
{
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype
) type
: 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid
) keyword
: 8;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c
: 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p
: 1;
78 /* The location at which this token was found. */
80 /* The value associated with this token, if any. */
81 union cp_token_value
{
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check
* GTY((tag ("1"))) tree_check_value
;
84 /* Use for all other tokens. */
85 tree
GTY((tag ("0"))) value
;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u
;
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token
*cp_token_position
;
91 DEF_VEC_P (cp_token_position
);
92 DEF_VEC_ALLOC_P (cp_token_position
,heap
);
94 static cp_token eof_token
=
96 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, 0, 0, { NULL
}
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
104 typedef struct GTY (()) cp_lexer
{
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token
* GTY ((length ("%h.buffer_length"))) buffer
;
108 /* If the lexer owns the buffer, this is the number of tokens in the
110 size_t buffer_length
;
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position
GTY ((skip
)) last_token
;
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position
GTY ((skip
)) next_token
;
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
124 VEC(cp_token_position
,heap
) *GTY ((skip
)) saved_tokens
;
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer
*next
;
129 /* True if we should output debugging information. */
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
143 typedef struct GTY(()) cp_token_cache
{
144 /* The beginning of the token range. */
145 cp_token
* GTY((skip
)) first
;
147 /* Points immediately after the last token in the range. */
148 cp_token
* GTY ((skip
)) last
;
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant
{
154 /* floating-point literal */
158 /* %<__FUNCTION__%> */
160 /* %<__PRETTY_FUNCTION__%> */
168 /* %<typeid%> operator */
170 /* non-constant compound literals */
172 /* a function call */
178 /* an array reference */
184 /* the address of a label */
198 /* calls to overloaded operators */
202 /* a comma operator */
204 /* a call to a constructor */
206 } non_integral_constant
;
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error
{
214 /* is not a class or namespace */
216 /* is not a class, namespace, or enumeration */
220 /* The various kinds of required token */
221 typedef enum required_token
{
223 RT_SEMICOLON
, /* ';' */
224 RT_OPEN_PAREN
, /* '(' */
225 RT_CLOSE_BRACE
, /* '}' */
226 RT_OPEN_BRACE
, /* '{' */
227 RT_CLOSE_SQUARE
, /* ']' */
228 RT_OPEN_SQUARE
, /* '[' */
232 RT_GREATER
, /* '>' */
234 RT_ELLIPSIS
, /* '...' */
238 RT_COLON_SCOPE
, /* ':' or '::' */
239 RT_CLOSE_PAREN
, /* ')' */
240 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
241 RT_PRAGMA_EOL
, /* end of line */
242 RT_NAME
, /* identifier */
244 /* The type is CPP_KEYWORD */
246 RT_DELETE
, /* delete */
247 RT_RETURN
, /* return */
248 RT_WHILE
, /* while */
249 RT_EXTERN
, /* extern */
250 RT_STATIC_ASSERT
, /* static_assert */
251 RT_DECLTYPE
, /* decltype */
252 RT_OPERATOR
, /* operator */
253 RT_CLASS
, /* class */
254 RT_TEMPLATE
, /* template */
255 RT_NAMESPACE
, /* namespace */
256 RT_USING
, /* using */
259 RT_CATCH
, /* catch */
260 RT_THROW
, /* throw */
261 RT_LABEL
, /* __label__ */
262 RT_AT_TRY
, /* @try */
263 RT_AT_SYNCHRONIZED
, /* @synchronized */
264 RT_AT_THROW
, /* @throw */
266 RT_SELECT
, /* selection-statement */
267 RT_INTERATION
, /* iteration-statement */
268 RT_JUMP
, /* jump-statement */
269 RT_CLASS_KEY
, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE
/* class, typename, or template */
275 static cp_lexer
*cp_lexer_new_main
277 static cp_lexer
*cp_lexer_new_from_tokens
278 (cp_token_cache
*tokens
);
279 static void cp_lexer_destroy
281 static int cp_lexer_saving_tokens
283 static cp_token_position cp_lexer_token_position
285 static cp_token
*cp_lexer_token_at
286 (cp_lexer
*, cp_token_position
);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer
*, cp_token
*);
289 static inline cp_token
*cp_lexer_peek_token
291 static cp_token
*cp_lexer_peek_nth_token
292 (cp_lexer
*, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer
*, enum cpp_ttype
);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer
*, enum cpp_ttype
);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer
*, enum rid
);
299 static cp_token
*cp_lexer_consume_token
301 static void cp_lexer_purge_token
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer
*, cp_token_position
);
305 static void cp_lexer_save_tokens
307 static void cp_lexer_commit_tokens
309 static void cp_lexer_rollback_tokens
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token
*);
314 static inline bool cp_lexer_debugging_p
316 static void cp_lexer_start_debugging
317 (cp_lexer
*) ATTRIBUTE_UNUSED
;
318 static void cp_lexer_stop_debugging
319 (cp_lexer
*) ATTRIBUTE_UNUSED
;
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
330 static cp_token_cache
*cp_token_cache_new
331 (cp_token
*, cp_token
*);
333 static void cp_parser_initial_pragma
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream
;
369 #endif /* ENABLE_CHECKING */
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand
;
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
379 cp_lexer_new_main (void)
381 cp_token first_token
;
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token
);
393 c_common_no_more_pch ();
395 /* Allocate the memory. */
396 lexer
= ggc_alloc_cleared_cp_lexer ();
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer
->debugging_p
= false;
401 #endif /* ENABLE_CHECKING */
402 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
403 CP_SAVED_TOKEN_STACK
);
405 /* Create the buffer. */
406 alloc
= CP_LEXER_BUFFER_SIZE
;
407 buffer
= ggc_alloc_vec_cp_token (alloc
);
409 /* Put the first token in the buffer. */
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos
->type
!= CPP_EOF
)
422 buffer
= GGC_RESIZEVEC (cp_token
, buffer
, alloc
);
423 pos
= buffer
+ space
;
425 cp_lexer_get_preprocessor_token (lexer
, pos
);
427 lexer
->buffer
= buffer
;
428 lexer
->buffer_length
= alloc
- space
;
429 lexer
->last_token
= pos
;
430 lexer
->next_token
= lexer
->buffer_length
? buffer
: &eof_token
;
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
436 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
444 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
446 cp_token
*first
= cache
->first
;
447 cp_token
*last
= cache
->last
;
448 cp_lexer
*lexer
= ggc_alloc_cleared_cp_lexer ();
450 /* We do not own the buffer. */
451 lexer
->buffer
= NULL
;
452 lexer
->buffer_length
= 0;
453 lexer
->next_token
= first
== last
? &eof_token
: first
;
454 lexer
->last_token
= last
;
456 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
457 CP_SAVED_TOKEN_STACK
);
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer
->debugging_p
= false;
464 gcc_assert (lexer
->next_token
->type
!= CPP_PURGED
);
468 /* Frees all resources associated with LEXER. */
471 cp_lexer_destroy (cp_lexer
*lexer
)
474 ggc_free (lexer
->buffer
);
475 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
479 /* Returns nonzero if debugging information should be output. */
481 #ifdef ENABLE_CHECKING
484 cp_lexer_debugging_p (cp_lexer
*lexer
)
486 return lexer
->debugging_p
;
489 #endif /* ENABLE_CHECKING */
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
494 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
496 return lexer
->next_token
- previous_p
;
499 static inline cp_token
*
500 cp_lexer_token_at (cp_lexer
*lexer ATTRIBUTE_UNUSED
, cp_token_position pos
)
505 /* nonzero if we are presently saving tokens. */
508 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
510 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
513 /* Store the next token from the preprocessor in *TOKEN. Return true
514 if we reach EOF. If LEXER is NULL, assume we are handling an
515 initial #pragma pch_preprocess, and thus want the lexer to return
516 processed strings. */
519 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
521 static int is_extern_c
= 0;
523 /* Get a new token from the preprocessor. */
525 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
526 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
527 token
->keyword
= RID_MAX
;
528 token
->pragma_kind
= PRAGMA_NONE
;
530 /* On some systems, some header files are surrounded by an
531 implicit extern "C" block. Set a flag in the token if it
532 comes from such a header. */
533 is_extern_c
+= pending_lang_change
;
534 pending_lang_change
= 0;
535 token
->implicit_extern_c
= is_extern_c
> 0;
537 /* Check to see if this token is a keyword. */
538 if (token
->type
== CPP_NAME
)
540 if (C_IS_RESERVED_WORD (token
->u
.value
))
542 /* Mark this token as a keyword. */
543 token
->type
= CPP_KEYWORD
;
544 /* Record which keyword. */
545 token
->keyword
= C_RID_CODE (token
->u
.value
);
549 if (warn_cxx0x_compat
550 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
551 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
553 /* Warn about the C++0x keyword (but still treat it as
555 warning (OPT_Wc__0x_compat
,
556 "identifier %qE will become a keyword in C++0x",
559 /* Clear out the C_RID_CODE so we don't warn about this
560 particular identifier-turned-keyword again. */
561 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
564 token
->ambiguous_p
= false;
565 token
->keyword
= RID_MAX
;
568 else if (token
->type
== CPP_AT_NAME
)
570 /* This only happens in Objective-C++; it must be a keyword. */
571 token
->type
= CPP_KEYWORD
;
572 switch (C_RID_CODE (token
->u
.value
))
574 /* Replace 'class' with '@class', 'private' with '@private',
575 etc. This prevents confusion with the C++ keyword
576 'class', and makes the tokens consistent with other
577 Objective-C 'AT' keywords. For example '@class' is
578 reported as RID_AT_CLASS which is consistent with
579 '@synchronized', which is reported as
582 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
583 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
584 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
585 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
586 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
587 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
588 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
589 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
592 else if (token
->type
== CPP_PRAGMA
)
594 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
595 token
->pragma_kind
= ((enum pragma_kind
)
596 TREE_INT_CST_LOW (token
->u
.value
));
597 token
->u
.value
= NULL_TREE
;
601 /* Update the globals input_location and the input file stack from TOKEN. */
603 cp_lexer_set_source_position_from_token (cp_token
*token
)
605 if (token
->type
!= CPP_EOF
)
607 input_location
= token
->location
;
611 /* Return a pointer to the next token in the token stream, but do not
614 static inline cp_token
*
615 cp_lexer_peek_token (cp_lexer
*lexer
)
617 if (cp_lexer_debugging_p (lexer
))
619 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
620 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
621 putc ('\n', cp_lexer_debug_stream
);
623 return lexer
->next_token
;
626 /* Return true if the next token has the indicated TYPE. */
629 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
631 return cp_lexer_peek_token (lexer
)->type
== type
;
634 /* Return true if the next token does not have the indicated TYPE. */
637 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
639 return !cp_lexer_next_token_is (lexer
, type
);
642 /* Return true if the next token is the indicated KEYWORD. */
645 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
647 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
650 /* Return true if the next token is not the indicated KEYWORD. */
653 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
655 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
658 /* Return true if the next token is a keyword for a decl-specifier. */
661 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
665 token
= cp_lexer_peek_token (lexer
);
666 switch (token
->keyword
)
668 /* auto specifier: storage-class-specifier in C++,
669 simple-type-specifier in C++0x. */
671 /* Storage classes. */
677 /* Elaborated type specifiers. */
683 /* Simple type specifiers. */
698 /* GNU extensions. */
701 /* C++0x extensions. */
710 /* Return a pointer to the Nth token in the token stream. If N is 1,
711 then this is precisely equivalent to cp_lexer_peek_token (except
712 that it is not inline). One would like to disallow that case, but
713 there is one case (cp_parser_nth_token_starts_template_id) where
714 the caller passes a variable for N and it might be 1. */
717 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
721 /* N is 1-based, not zero-based. */
724 if (cp_lexer_debugging_p (lexer
))
725 fprintf (cp_lexer_debug_stream
,
726 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
729 token
= lexer
->next_token
;
730 gcc_assert (!n
|| token
!= &eof_token
);
734 if (token
== lexer
->last_token
)
740 if (token
->type
!= CPP_PURGED
)
744 if (cp_lexer_debugging_p (lexer
))
746 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
747 putc ('\n', cp_lexer_debug_stream
);
753 /* Return the next token, and advance the lexer's next_token pointer
754 to point to the next non-purged token. */
757 cp_lexer_consume_token (cp_lexer
* lexer
)
759 cp_token
*token
= lexer
->next_token
;
761 gcc_assert (token
!= &eof_token
);
762 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
767 if (lexer
->next_token
== lexer
->last_token
)
769 lexer
->next_token
= &eof_token
;
774 while (lexer
->next_token
->type
== CPP_PURGED
);
776 cp_lexer_set_source_position_from_token (token
);
778 /* Provide debugging output. */
779 if (cp_lexer_debugging_p (lexer
))
781 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
782 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
783 putc ('\n', cp_lexer_debug_stream
);
789 /* Permanently remove the next token from the token stream, and
790 advance the next_token pointer to refer to the next non-purged
794 cp_lexer_purge_token (cp_lexer
*lexer
)
796 cp_token
*tok
= lexer
->next_token
;
798 gcc_assert (tok
!= &eof_token
);
799 tok
->type
= CPP_PURGED
;
800 tok
->location
= UNKNOWN_LOCATION
;
801 tok
->u
.value
= NULL_TREE
;
802 tok
->keyword
= RID_MAX
;
807 if (tok
== lexer
->last_token
)
813 while (tok
->type
== CPP_PURGED
);
814 lexer
->next_token
= tok
;
817 /* Permanently remove all tokens after TOK, up to, but not
818 including, the token that will be returned next by
819 cp_lexer_peek_token. */
822 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
824 cp_token
*peek
= lexer
->next_token
;
826 if (peek
== &eof_token
)
827 peek
= lexer
->last_token
;
829 gcc_assert (tok
< peek
);
831 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
833 tok
->type
= CPP_PURGED
;
834 tok
->location
= UNKNOWN_LOCATION
;
835 tok
->u
.value
= NULL_TREE
;
836 tok
->keyword
= RID_MAX
;
840 /* Begin saving tokens. All tokens consumed after this point will be
844 cp_lexer_save_tokens (cp_lexer
* lexer
)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer
))
848 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
850 VEC_safe_push (cp_token_position
, heap
,
851 lexer
->saved_tokens
, lexer
->next_token
);
854 /* Commit to the portion of the token stream most recently saved. */
857 cp_lexer_commit_tokens (cp_lexer
* lexer
)
859 /* Provide debugging output. */
860 if (cp_lexer_debugging_p (lexer
))
861 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
863 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
866 /* Return all tokens saved since the last call to cp_lexer_save_tokens
867 to the token stream. Stop saving tokens. */
870 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
872 /* Provide debugging output. */
873 if (cp_lexer_debugging_p (lexer
))
874 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
876 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
879 /* Print a representation of the TOKEN on the STREAM. */
881 #ifdef ENABLE_CHECKING
884 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
886 /* We don't use cpp_type2name here because the parser defines
887 a few tokens of its own. */
888 static const char *const token_names
[] = {
889 /* cpplib-defined token types */
895 /* C++ parser token types - see "Manifest constants", above. */
898 "NESTED_NAME_SPECIFIER",
902 /* If we have a name for the token, print it out. Otherwise, we
903 simply give the numeric code. */
904 gcc_assert (token
->type
< ARRAY_SIZE(token_names
));
905 fputs (token_names
[token
->type
], stream
);
907 /* For some tokens, print the associated data. */
911 /* Some keywords have a value that is not an IDENTIFIER_NODE.
912 For example, `struct' is mapped to an INTEGER_CST. */
913 if (TREE_CODE (token
->u
.value
) != IDENTIFIER_NODE
)
915 /* else fall through */
917 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
925 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
933 /* Start emitting debugging information. */
936 cp_lexer_start_debugging (cp_lexer
* lexer
)
938 lexer
->debugging_p
= true;
941 /* Stop emitting debugging information. */
944 cp_lexer_stop_debugging (cp_lexer
* lexer
)
946 lexer
->debugging_p
= false;
949 #endif /* ENABLE_CHECKING */
951 /* Create a new cp_token_cache, representing a range of tokens. */
953 static cp_token_cache
*
954 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
956 cp_token_cache
*cache
= ggc_alloc_cp_token_cache ();
957 cache
->first
= first
;
963 /* Decl-specifiers. */
965 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
968 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
970 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
975 /* Nothing other than the parser should be creating declarators;
976 declarators are a semi-syntactic representation of C++ entities.
977 Other parts of the front end that need to create entities (like
978 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
980 static cp_declarator
*make_call_declarator
981 (cp_declarator
*, tree
, cp_cv_quals
, tree
, tree
);
982 static cp_declarator
*make_array_declarator
983 (cp_declarator
*, tree
);
984 static cp_declarator
*make_pointer_declarator
985 (cp_cv_quals
, cp_declarator
*);
986 static cp_declarator
*make_reference_declarator
987 (cp_cv_quals
, cp_declarator
*, bool);
988 static cp_parameter_declarator
*make_parameter_declarator
989 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
990 static cp_declarator
*make_ptrmem_declarator
991 (cp_cv_quals
, tree
, cp_declarator
*);
993 /* An erroneous declarator. */
994 static cp_declarator
*cp_error_declarator
;
996 /* The obstack on which declarators and related data structures are
998 static struct obstack declarator_obstack
;
1000 /* Alloc BYTES from the declarator memory pool. */
1002 static inline void *
1003 alloc_declarator (size_t bytes
)
1005 return obstack_alloc (&declarator_obstack
, bytes
);
1008 /* Allocate a declarator of the indicated KIND. Clear fields that are
1009 common to all declarators. */
1011 static cp_declarator
*
1012 make_declarator (cp_declarator_kind kind
)
1014 cp_declarator
*declarator
;
1016 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1017 declarator
->kind
= kind
;
1018 declarator
->attributes
= NULL_TREE
;
1019 declarator
->declarator
= NULL
;
1020 declarator
->parameter_pack_p
= false;
1021 declarator
->id_loc
= UNKNOWN_LOCATION
;
1026 /* Make a declarator for a generalized identifier. If
1027 QUALIFYING_SCOPE is non-NULL, the identifier is
1028 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1029 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1032 static cp_declarator
*
1033 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1034 special_function_kind sfk
)
1036 cp_declarator
*declarator
;
1038 /* It is valid to write:
1040 class C { void f(); };
1044 The standard is not clear about whether `typedef const C D' is
1045 legal; as of 2002-09-15 the committee is considering that
1046 question. EDG 3.0 allows that syntax. Therefore, we do as
1048 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1049 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1051 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
1052 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1053 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1055 declarator
= make_declarator (cdk_id
);
1056 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1057 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1058 declarator
->u
.id
.sfk
= sfk
;
1063 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1064 of modifiers such as const or volatile to apply to the pointer
1065 type, represented as identifiers. */
1068 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
1070 cp_declarator
*declarator
;
1072 declarator
= make_declarator (cdk_pointer
);
1073 declarator
->declarator
= target
;
1074 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1075 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1078 declarator
->id_loc
= target
->id_loc
;
1079 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1080 target
->parameter_pack_p
= false;
1083 declarator
->parameter_pack_p
= false;
1088 /* Like make_pointer_declarator -- but for references. */
1091 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1094 cp_declarator
*declarator
;
1096 declarator
= make_declarator (cdk_reference
);
1097 declarator
->declarator
= target
;
1098 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1099 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1102 declarator
->id_loc
= target
->id_loc
;
1103 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1104 target
->parameter_pack_p
= false;
1107 declarator
->parameter_pack_p
= false;
1112 /* Like make_pointer_declarator -- but for a pointer to a non-static
1113 member of CLASS_TYPE. */
1116 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1117 cp_declarator
*pointee
)
1119 cp_declarator
*declarator
;
1121 declarator
= make_declarator (cdk_ptrmem
);
1122 declarator
->declarator
= pointee
;
1123 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1124 declarator
->u
.pointer
.class_type
= class_type
;
1128 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1129 pointee
->parameter_pack_p
= false;
1132 declarator
->parameter_pack_p
= false;
1137 /* Make a declarator for the function given by TARGET, with the
1138 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1139 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1140 indicates what exceptions can be thrown. */
1143 make_call_declarator (cp_declarator
*target
,
1145 cp_cv_quals cv_qualifiers
,
1146 tree exception_specification
,
1147 tree late_return_type
)
1149 cp_declarator
*declarator
;
1151 declarator
= make_declarator (cdk_function
);
1152 declarator
->declarator
= target
;
1153 declarator
->u
.function
.parameters
= parms
;
1154 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1155 declarator
->u
.function
.exception_specification
= exception_specification
;
1156 declarator
->u
.function
.late_return_type
= late_return_type
;
1159 declarator
->id_loc
= target
->id_loc
;
1160 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1161 target
->parameter_pack_p
= false;
1164 declarator
->parameter_pack_p
= false;
1169 /* Make a declarator for an array of BOUNDS elements, each of which is
1170 defined by ELEMENT. */
1173 make_array_declarator (cp_declarator
*element
, tree bounds
)
1175 cp_declarator
*declarator
;
1177 declarator
= make_declarator (cdk_array
);
1178 declarator
->declarator
= element
;
1179 declarator
->u
.array
.bounds
= bounds
;
1182 declarator
->id_loc
= element
->id_loc
;
1183 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1184 element
->parameter_pack_p
= false;
1187 declarator
->parameter_pack_p
= false;
1192 /* Determine whether the declarator we've seen so far can be a
1193 parameter pack, when followed by an ellipsis. */
1195 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1197 /* Search for a declarator name, or any other declarator that goes
1198 after the point where the ellipsis could appear in a parameter
1199 pack. If we find any of these, then this declarator can not be
1200 made into a parameter pack. */
1202 while (declarator
&& !found
)
1204 switch ((int)declarator
->kind
)
1215 declarator
= declarator
->declarator
;
1223 cp_parameter_declarator
*no_parameters
;
1225 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1226 DECLARATOR and DEFAULT_ARGUMENT. */
1228 cp_parameter_declarator
*
1229 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1230 cp_declarator
*declarator
,
1231 tree default_argument
)
1233 cp_parameter_declarator
*parameter
;
1235 parameter
= ((cp_parameter_declarator
*)
1236 alloc_declarator (sizeof (cp_parameter_declarator
)));
1237 parameter
->next
= NULL
;
1238 if (decl_specifiers
)
1239 parameter
->decl_specifiers
= *decl_specifiers
;
1241 clear_decl_specs (¶meter
->decl_specifiers
);
1242 parameter
->declarator
= declarator
;
1243 parameter
->default_argument
= default_argument
;
1244 parameter
->ellipsis_p
= false;
1249 /* Returns true iff DECLARATOR is a declaration for a function. */
1252 function_declarator_p (const cp_declarator
*declarator
)
1256 if (declarator
->kind
== cdk_function
1257 && declarator
->declarator
->kind
== cdk_id
)
1259 if (declarator
->kind
== cdk_id
1260 || declarator
->kind
== cdk_error
)
1262 declarator
= declarator
->declarator
;
1272 A cp_parser parses the token stream as specified by the C++
1273 grammar. Its job is purely parsing, not semantic analysis. For
1274 example, the parser breaks the token stream into declarators,
1275 expressions, statements, and other similar syntactic constructs.
1276 It does not check that the types of the expressions on either side
1277 of an assignment-statement are compatible, or that a function is
1278 not declared with a parameter of type `void'.
1280 The parser invokes routines elsewhere in the compiler to perform
1281 semantic analysis and to build up the abstract syntax tree for the
1284 The parser (and the template instantiation code, which is, in a
1285 way, a close relative of parsing) are the only parts of the
1286 compiler that should be calling push_scope and pop_scope, or
1287 related functions. The parser (and template instantiation code)
1288 keeps track of what scope is presently active; everything else
1289 should simply honor that. (The code that generates static
1290 initializers may also need to set the scope, in order to check
1291 access control correctly when emitting the initializers.)
1296 The parser is of the standard recursive-descent variety. Upcoming
1297 tokens in the token stream are examined in order to determine which
1298 production to use when parsing a non-terminal. Some C++ constructs
1299 require arbitrary look ahead to disambiguate. For example, it is
1300 impossible, in the general case, to tell whether a statement is an
1301 expression or declaration without scanning the entire statement.
1302 Therefore, the parser is capable of "parsing tentatively." When the
1303 parser is not sure what construct comes next, it enters this mode.
1304 Then, while we attempt to parse the construct, the parser queues up
1305 error messages, rather than issuing them immediately, and saves the
1306 tokens it consumes. If the construct is parsed successfully, the
1307 parser "commits", i.e., it issues any queued error messages and
1308 the tokens that were being preserved are permanently discarded.
1309 If, however, the construct is not parsed successfully, the parser
1310 rolls back its state completely so that it can resume parsing using
1311 a different alternative.
1316 The performance of the parser could probably be improved substantially.
1317 We could often eliminate the need to parse tentatively by looking ahead
1318 a little bit. In some places, this approach might not entirely eliminate
1319 the need to parse tentatively, but it might still speed up the average
1322 /* Flags that are passed to some parsing functions. These values can
1323 be bitwise-ored together. */
1328 CP_PARSER_FLAGS_NONE
= 0x0,
1329 /* The construct is optional. If it is not present, then no error
1330 should be issued. */
1331 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1332 /* When parsing a type-specifier, treat user-defined type-names
1333 as non-type identifiers. */
1334 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1335 /* When parsing a type-specifier, do not try to parse a class-specifier
1336 or enum-specifier. */
1337 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4
1340 /* This type is used for parameters and variables which hold
1341 combinations of the above flags. */
1342 typedef int cp_parser_flags
;
1344 /* The different kinds of declarators we want to parse. */
1346 typedef enum cp_parser_declarator_kind
1348 /* We want an abstract declarator. */
1349 CP_PARSER_DECLARATOR_ABSTRACT
,
1350 /* We want a named declarator. */
1351 CP_PARSER_DECLARATOR_NAMED
,
1352 /* We don't mind, but the name must be an unqualified-id. */
1353 CP_PARSER_DECLARATOR_EITHER
1354 } cp_parser_declarator_kind
;
1356 /* The precedence values used to parse binary expressions. The minimum value
1357 of PREC must be 1, because zero is reserved to quickly discriminate
1358 binary operators from other tokens. */
1363 PREC_LOGICAL_OR_EXPRESSION
,
1364 PREC_LOGICAL_AND_EXPRESSION
,
1365 PREC_INCLUSIVE_OR_EXPRESSION
,
1366 PREC_EXCLUSIVE_OR_EXPRESSION
,
1367 PREC_AND_EXPRESSION
,
1368 PREC_EQUALITY_EXPRESSION
,
1369 PREC_RELATIONAL_EXPRESSION
,
1370 PREC_SHIFT_EXPRESSION
,
1371 PREC_ADDITIVE_EXPRESSION
,
1372 PREC_MULTIPLICATIVE_EXPRESSION
,
1374 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1377 /* A mapping from a token type to a corresponding tree node type, with a
1378 precedence value. */
1380 typedef struct cp_parser_binary_operations_map_node
1382 /* The token type. */
1383 enum cpp_ttype token_type
;
1384 /* The corresponding tree code. */
1385 enum tree_code tree_type
;
1386 /* The precedence of this operator. */
1387 enum cp_parser_prec prec
;
1388 } cp_parser_binary_operations_map_node
;
1390 /* The status of a tentative parse. */
1392 typedef enum cp_parser_status_kind
1394 /* No errors have occurred. */
1395 CP_PARSER_STATUS_KIND_NO_ERROR
,
1396 /* An error has occurred. */
1397 CP_PARSER_STATUS_KIND_ERROR
,
1398 /* We are committed to this tentative parse, whether or not an error
1400 CP_PARSER_STATUS_KIND_COMMITTED
1401 } cp_parser_status_kind
;
1403 typedef struct cp_parser_expression_stack_entry
1405 /* Left hand side of the binary operation we are currently
1408 /* Original tree code for left hand side, if it was a binary
1409 expression itself (used for -Wparentheses). */
1410 enum tree_code lhs_type
;
1411 /* Tree code for the binary operation we are parsing. */
1412 enum tree_code tree_type
;
1413 /* Precedence of the binary operation we are parsing. */
1414 enum cp_parser_prec prec
;
1415 } cp_parser_expression_stack_entry
;
1417 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1418 entries because precedence levels on the stack are monotonically
1420 typedef struct cp_parser_expression_stack_entry
1421 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1423 /* Context that is saved and restored when parsing tentatively. */
1424 typedef struct GTY (()) cp_parser_context
{
1425 /* If this is a tentative parsing context, the status of the
1427 enum cp_parser_status_kind status
;
1428 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1429 that are looked up in this context must be looked up both in the
1430 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1431 the context of the containing expression. */
1434 /* The next parsing context in the stack. */
1435 struct cp_parser_context
*next
;
1436 } cp_parser_context
;
1440 /* Constructors and destructors. */
1442 static cp_parser_context
*cp_parser_context_new
1443 (cp_parser_context
*);
1445 /* Class variables. */
1447 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1449 /* The operator-precedence table used by cp_parser_binary_expression.
1450 Transformed into an associative array (binops_by_token) by
1453 static const cp_parser_binary_operations_map_node binops
[] = {
1454 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1455 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1457 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1458 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1459 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1461 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1462 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1464 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1465 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1467 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1468 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1469 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1470 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1472 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1473 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1475 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1477 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1479 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1481 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1483 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1486 /* The same as binops, but initialized by cp_parser_new so that
1487 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1489 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1491 /* Constructors and destructors. */
1493 /* Construct a new context. The context below this one on the stack
1494 is given by NEXT. */
1496 static cp_parser_context
*
1497 cp_parser_context_new (cp_parser_context
* next
)
1499 cp_parser_context
*context
;
1501 /* Allocate the storage. */
1502 if (cp_parser_context_free_list
!= NULL
)
1504 /* Pull the first entry from the free list. */
1505 context
= cp_parser_context_free_list
;
1506 cp_parser_context_free_list
= context
->next
;
1507 memset (context
, 0, sizeof (*context
));
1510 context
= ggc_alloc_cleared_cp_parser_context ();
1512 /* No errors have occurred yet in this context. */
1513 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1514 /* If this is not the bottommost context, copy information that we
1515 need from the previous context. */
1518 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1519 expression, then we are parsing one in this context, too. */
1520 context
->object_type
= next
->object_type
;
1521 /* Thread the stack. */
1522 context
->next
= next
;
1528 /* An entry in a queue of function arguments that require post-processing. */
1530 typedef struct GTY(()) cp_default_arg_entry_d
{
1531 /* The current_class_type when we parsed this arg. */
1534 /* The function decl itself. */
1536 } cp_default_arg_entry
;
1538 DEF_VEC_O(cp_default_arg_entry
);
1539 DEF_VEC_ALLOC_O(cp_default_arg_entry
,gc
);
1541 /* An entry in a stack for member functions of local classes. */
1543 typedef struct GTY(()) cp_unparsed_functions_entry_d
{
1544 /* Functions with default arguments that require post-processing.
1545 Functions appear in this list in declaration order. */
1546 VEC(cp_default_arg_entry
,gc
) *funs_with_default_args
;
1548 /* Functions with defintions that require post-processing. Functions
1549 appear in this list in declaration order. */
1550 VEC(tree
,gc
) *funs_with_definitions
;
1551 } cp_unparsed_functions_entry
;
1553 DEF_VEC_O(cp_unparsed_functions_entry
);
1554 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry
,gc
);
1556 /* The cp_parser structure represents the C++ parser. */
1558 typedef struct GTY(()) cp_parser
{
1559 /* The lexer from which we are obtaining tokens. */
1562 /* The scope in which names should be looked up. If NULL_TREE, then
1563 we look up names in the scope that is currently open in the
1564 source program. If non-NULL, this is either a TYPE or
1565 NAMESPACE_DECL for the scope in which we should look. It can
1566 also be ERROR_MARK, when we've parsed a bogus scope.
1568 This value is not cleared automatically after a name is looked
1569 up, so we must be careful to clear it before starting a new look
1570 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1571 will look up `Z' in the scope of `X', rather than the current
1572 scope.) Unfortunately, it is difficult to tell when name lookup
1573 is complete, because we sometimes peek at a token, look it up,
1574 and then decide not to consume it. */
1577 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1578 last lookup took place. OBJECT_SCOPE is used if an expression
1579 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1580 respectively. QUALIFYING_SCOPE is used for an expression of the
1581 form "X::Y"; it refers to X. */
1583 tree qualifying_scope
;
1585 /* A stack of parsing contexts. All but the bottom entry on the
1586 stack will be tentative contexts.
1588 We parse tentatively in order to determine which construct is in
1589 use in some situations. For example, in order to determine
1590 whether a statement is an expression-statement or a
1591 declaration-statement we parse it tentatively as a
1592 declaration-statement. If that fails, we then reparse the same
1593 token stream as an expression-statement. */
1594 cp_parser_context
*context
;
1596 /* True if we are parsing GNU C++. If this flag is not set, then
1597 GNU extensions are not recognized. */
1598 bool allow_gnu_extensions_p
;
1600 /* TRUE if the `>' token should be interpreted as the greater-than
1601 operator. FALSE if it is the end of a template-id or
1602 template-parameter-list. In C++0x mode, this flag also applies to
1603 `>>' tokens, which are viewed as two consecutive `>' tokens when
1604 this flag is FALSE. */
1605 bool greater_than_is_operator_p
;
1607 /* TRUE if default arguments are allowed within a parameter list
1608 that starts at this point. FALSE if only a gnu extension makes
1609 them permissible. */
1610 bool default_arg_ok_p
;
1612 /* TRUE if we are parsing an integral constant-expression. See
1613 [expr.const] for a precise definition. */
1614 bool integral_constant_expression_p
;
1616 /* TRUE if we are parsing an integral constant-expression -- but a
1617 non-constant expression should be permitted as well. This flag
1618 is used when parsing an array bound so that GNU variable-length
1619 arrays are tolerated. */
1620 bool allow_non_integral_constant_expression_p
;
1622 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1623 been seen that makes the expression non-constant. */
1624 bool non_integral_constant_expression_p
;
1626 /* TRUE if local variable names and `this' are forbidden in the
1628 bool local_variables_forbidden_p
;
1630 /* TRUE if the declaration we are parsing is part of a
1631 linkage-specification of the form `extern string-literal
1633 bool in_unbraced_linkage_specification_p
;
1635 /* TRUE if we are presently parsing a declarator, after the
1636 direct-declarator. */
1637 bool in_declarator_p
;
1639 /* TRUE if we are presently parsing a template-argument-list. */
1640 bool in_template_argument_list_p
;
1642 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1643 to IN_OMP_BLOCK if parsing OpenMP structured block and
1644 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1645 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1646 iteration-statement, OpenMP block or loop within that switch. */
1647 #define IN_SWITCH_STMT 1
1648 #define IN_ITERATION_STMT 2
1649 #define IN_OMP_BLOCK 4
1650 #define IN_OMP_FOR 8
1651 #define IN_IF_STMT 16
1652 unsigned char in_statement
;
1654 /* TRUE if we are presently parsing the body of a switch statement.
1655 Note that this doesn't quite overlap with in_statement above.
1656 The difference relates to giving the right sets of error messages:
1657 "case not in switch" vs "break statement used with OpenMP...". */
1658 bool in_switch_statement_p
;
1660 /* TRUE if we are parsing a type-id in an expression context. In
1661 such a situation, both "type (expr)" and "type (type)" are valid
1663 bool in_type_id_in_expr_p
;
1665 /* TRUE if we are currently in a header file where declarations are
1666 implicitly extern "C". */
1667 bool implicit_extern_c
;
1669 /* TRUE if strings in expressions should be translated to the execution
1671 bool translate_strings_p
;
1673 /* TRUE if we are presently parsing the body of a function, but not
1675 bool in_function_body
;
1677 /* If non-NULL, then we are parsing a construct where new type
1678 definitions are not permitted. The string stored here will be
1679 issued as an error message if a type is defined. */
1680 const char *type_definition_forbidden_message
;
1682 /* A stack used for member functions of local classes. The lists
1683 contained in an individual entry can only be processed once the
1684 outermost class being defined is complete. */
1685 VEC(cp_unparsed_functions_entry
,gc
) *unparsed_queues
;
1687 /* The number of classes whose definitions are currently in
1689 unsigned num_classes_being_defined
;
1691 /* The number of template parameter lists that apply directly to the
1692 current declaration. */
1693 unsigned num_template_parameter_lists
;
1696 /* Managing the unparsed function queues. */
1698 #define unparsed_funs_with_default_args \
1699 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1700 #define unparsed_funs_with_definitions \
1701 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1704 push_unparsed_function_queues (cp_parser
*parser
)
1706 VEC_safe_push (cp_unparsed_functions_entry
, gc
,
1707 parser
->unparsed_queues
, NULL
);
1708 unparsed_funs_with_default_args
= NULL
;
1709 unparsed_funs_with_definitions
= make_tree_vector ();
1713 pop_unparsed_function_queues (cp_parser
*parser
)
1715 release_tree_vector (unparsed_funs_with_definitions
);
1716 VEC_pop (cp_unparsed_functions_entry
, parser
->unparsed_queues
);
1721 /* Constructors and destructors. */
1723 static cp_parser
*cp_parser_new
1726 /* Routines to parse various constructs.
1728 Those that return `tree' will return the error_mark_node (rather
1729 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1730 Sometimes, they will return an ordinary node if error-recovery was
1731 attempted, even though a parse error occurred. So, to check
1732 whether or not a parse error occurred, you should always use
1733 cp_parser_error_occurred. If the construct is optional (indicated
1734 either by an `_opt' in the name of the function that does the
1735 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1736 the construct is not present. */
1738 /* Lexical conventions [gram.lex] */
1740 static tree cp_parser_identifier
1742 static tree cp_parser_string_literal
1743 (cp_parser
*, bool, bool);
1745 /* Basic concepts [gram.basic] */
1747 static bool cp_parser_translation_unit
1750 /* Expressions [gram.expr] */
1752 static tree cp_parser_primary_expression
1753 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1754 static tree cp_parser_id_expression
1755 (cp_parser
*, bool, bool, bool *, bool, bool);
1756 static tree cp_parser_unqualified_id
1757 (cp_parser
*, bool, bool, bool, bool);
1758 static tree cp_parser_nested_name_specifier_opt
1759 (cp_parser
*, bool, bool, bool, bool);
1760 static tree cp_parser_nested_name_specifier
1761 (cp_parser
*, bool, bool, bool, bool);
1762 static tree cp_parser_qualifying_entity
1763 (cp_parser
*, bool, bool, bool, bool, bool);
1764 static tree cp_parser_postfix_expression
1765 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1766 static tree cp_parser_postfix_open_square_expression
1767 (cp_parser
*, tree
, bool);
1768 static tree cp_parser_postfix_dot_deref_expression
1769 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1770 static VEC(tree
,gc
) *cp_parser_parenthesized_expression_list
1771 (cp_parser
*, int, bool, bool, bool *);
1772 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1773 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1774 static void cp_parser_pseudo_destructor_name
1775 (cp_parser
*, tree
*, tree
*);
1776 static tree cp_parser_unary_expression
1777 (cp_parser
*, bool, bool, cp_id_kind
*);
1778 static enum tree_code cp_parser_unary_operator
1780 static tree cp_parser_new_expression
1782 static VEC(tree
,gc
) *cp_parser_new_placement
1784 static tree cp_parser_new_type_id
1785 (cp_parser
*, tree
*);
1786 static cp_declarator
*cp_parser_new_declarator_opt
1788 static cp_declarator
*cp_parser_direct_new_declarator
1790 static VEC(tree
,gc
) *cp_parser_new_initializer
1792 static tree cp_parser_delete_expression
1794 static tree cp_parser_cast_expression
1795 (cp_parser
*, bool, bool, cp_id_kind
*);
1796 static tree cp_parser_binary_expression
1797 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1798 static tree cp_parser_question_colon_clause
1799 (cp_parser
*, tree
);
1800 static tree cp_parser_assignment_expression
1801 (cp_parser
*, bool, cp_id_kind
*);
1802 static enum tree_code cp_parser_assignment_operator_opt
1804 static tree cp_parser_expression
1805 (cp_parser
*, bool, cp_id_kind
*);
1806 static tree cp_parser_constant_expression
1807 (cp_parser
*, bool, bool *);
1808 static tree cp_parser_builtin_offsetof
1810 static tree cp_parser_lambda_expression
1812 static void cp_parser_lambda_introducer
1813 (cp_parser
*, tree
);
1814 static void cp_parser_lambda_declarator_opt
1815 (cp_parser
*, tree
);
1816 static void cp_parser_lambda_body
1817 (cp_parser
*, tree
);
1819 /* Statements [gram.stmt.stmt] */
1821 static void cp_parser_statement
1822 (cp_parser
*, tree
, bool, bool *);
1823 static void cp_parser_label_for_labeled_statement
1825 static tree cp_parser_expression_statement
1826 (cp_parser
*, tree
);
1827 static tree cp_parser_compound_statement
1828 (cp_parser
*, tree
, bool);
1829 static void cp_parser_statement_seq_opt
1830 (cp_parser
*, tree
);
1831 static tree cp_parser_selection_statement
1832 (cp_parser
*, bool *);
1833 static tree cp_parser_condition
1835 static tree cp_parser_iteration_statement
1837 static void cp_parser_for_init_statement
1839 static tree cp_parser_c_for
1841 static tree cp_parser_range_for
1843 static tree cp_parser_jump_statement
1845 static void cp_parser_declaration_statement
1848 static tree cp_parser_implicitly_scoped_statement
1849 (cp_parser
*, bool *);
1850 static void cp_parser_already_scoped_statement
1853 /* Declarations [gram.dcl.dcl] */
1855 static void cp_parser_declaration_seq_opt
1857 static void cp_parser_declaration
1859 static void cp_parser_block_declaration
1860 (cp_parser
*, bool);
1861 static void cp_parser_simple_declaration
1862 (cp_parser
*, bool);
1863 static void cp_parser_decl_specifier_seq
1864 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1865 static tree cp_parser_storage_class_specifier_opt
1867 static tree cp_parser_function_specifier_opt
1868 (cp_parser
*, cp_decl_specifier_seq
*);
1869 static tree cp_parser_type_specifier
1870 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1872 static tree cp_parser_simple_type_specifier
1873 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1874 static tree cp_parser_type_name
1876 static tree cp_parser_nonclass_name
1877 (cp_parser
* parser
);
1878 static tree cp_parser_elaborated_type_specifier
1879 (cp_parser
*, bool, bool);
1880 static tree cp_parser_enum_specifier
1882 static void cp_parser_enumerator_list
1883 (cp_parser
*, tree
);
1884 static void cp_parser_enumerator_definition
1885 (cp_parser
*, tree
);
1886 static tree cp_parser_namespace_name
1888 static void cp_parser_namespace_definition
1890 static void cp_parser_namespace_body
1892 static tree cp_parser_qualified_namespace_specifier
1894 static void cp_parser_namespace_alias_definition
1896 static bool cp_parser_using_declaration
1897 (cp_parser
*, bool);
1898 static void cp_parser_using_directive
1900 static void cp_parser_asm_definition
1902 static void cp_parser_linkage_specification
1904 static void cp_parser_static_assert
1905 (cp_parser
*, bool);
1906 static tree cp_parser_decltype
1909 /* Declarators [gram.dcl.decl] */
1911 static tree cp_parser_init_declarator
1912 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *);
1913 static cp_declarator
*cp_parser_declarator
1914 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1915 static cp_declarator
*cp_parser_direct_declarator
1916 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1917 static enum tree_code cp_parser_ptr_operator
1918 (cp_parser
*, tree
*, cp_cv_quals
*);
1919 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1921 static tree cp_parser_late_return_type_opt
1923 static tree cp_parser_declarator_id
1924 (cp_parser
*, bool);
1925 static tree cp_parser_type_id
1927 static tree cp_parser_template_type_arg
1929 static tree
cp_parser_trailing_type_id (cp_parser
*);
1930 static tree cp_parser_type_id_1
1931 (cp_parser
*, bool, bool);
1932 static void cp_parser_type_specifier_seq
1933 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
1934 static tree cp_parser_parameter_declaration_clause
1936 static tree cp_parser_parameter_declaration_list
1937 (cp_parser
*, bool *);
1938 static cp_parameter_declarator
*cp_parser_parameter_declaration
1939 (cp_parser
*, bool, bool *);
1940 static tree cp_parser_default_argument
1941 (cp_parser
*, bool);
1942 static void cp_parser_function_body
1944 static tree cp_parser_initializer
1945 (cp_parser
*, bool *, bool *);
1946 static tree cp_parser_initializer_clause
1947 (cp_parser
*, bool *);
1948 static tree cp_parser_braced_list
1949 (cp_parser
*, bool*);
1950 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1951 (cp_parser
*, bool *);
1953 static bool cp_parser_ctor_initializer_opt_and_function_body
1956 /* Classes [gram.class] */
1958 static tree cp_parser_class_name
1959 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
1960 static tree cp_parser_class_specifier
1962 static tree cp_parser_class_head
1963 (cp_parser
*, bool *, tree
*, tree
*);
1964 static enum tag_types cp_parser_class_key
1966 static void cp_parser_member_specification_opt
1968 static void cp_parser_member_declaration
1970 static tree cp_parser_pure_specifier
1972 static tree cp_parser_constant_initializer
1975 /* Derived classes [gram.class.derived] */
1977 static tree cp_parser_base_clause
1979 static tree cp_parser_base_specifier
1982 /* Special member functions [gram.special] */
1984 static tree cp_parser_conversion_function_id
1986 static tree cp_parser_conversion_type_id
1988 static cp_declarator
*cp_parser_conversion_declarator_opt
1990 static bool cp_parser_ctor_initializer_opt
1992 static void cp_parser_mem_initializer_list
1994 static tree cp_parser_mem_initializer
1996 static tree cp_parser_mem_initializer_id
1999 /* Overloading [gram.over] */
2001 static tree cp_parser_operator_function_id
2003 static tree cp_parser_operator
2006 /* Templates [gram.temp] */
2008 static void cp_parser_template_declaration
2009 (cp_parser
*, bool);
2010 static tree cp_parser_template_parameter_list
2012 static tree cp_parser_template_parameter
2013 (cp_parser
*, bool *, bool *);
2014 static tree cp_parser_type_parameter
2015 (cp_parser
*, bool *);
2016 static tree cp_parser_template_id
2017 (cp_parser
*, bool, bool, bool);
2018 static tree cp_parser_template_name
2019 (cp_parser
*, bool, bool, bool, bool *);
2020 static tree cp_parser_template_argument_list
2022 static tree cp_parser_template_argument
2024 static void cp_parser_explicit_instantiation
2026 static void cp_parser_explicit_specialization
2029 /* Exception handling [gram.exception] */
2031 static tree cp_parser_try_block
2033 static bool cp_parser_function_try_block
2035 static void cp_parser_handler_seq
2037 static void cp_parser_handler
2039 static tree cp_parser_exception_declaration
2041 static tree cp_parser_throw_expression
2043 static tree cp_parser_exception_specification_opt
2045 static tree cp_parser_type_id_list
2048 /* GNU Extensions */
2050 static tree cp_parser_asm_specification_opt
2052 static tree cp_parser_asm_operand_list
2054 static tree cp_parser_asm_clobber_list
2056 static tree cp_parser_asm_label_list
2058 static tree cp_parser_attributes_opt
2060 static tree cp_parser_attribute_list
2062 static bool cp_parser_extension_opt
2063 (cp_parser
*, int *);
2064 static void cp_parser_label_declaration
2067 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
2068 static bool cp_parser_pragma
2069 (cp_parser
*, enum pragma_context
);
2071 /* Objective-C++ Productions */
2073 static tree cp_parser_objc_message_receiver
2075 static tree cp_parser_objc_message_args
2077 static tree cp_parser_objc_message_expression
2079 static tree cp_parser_objc_encode_expression
2081 static tree cp_parser_objc_defs_expression
2083 static tree cp_parser_objc_protocol_expression
2085 static tree cp_parser_objc_selector_expression
2087 static tree cp_parser_objc_expression
2089 static bool cp_parser_objc_selector_p
2091 static tree cp_parser_objc_selector
2093 static tree cp_parser_objc_protocol_refs_opt
2095 static void cp_parser_objc_declaration
2096 (cp_parser
*, tree
);
2097 static tree cp_parser_objc_statement
2099 static bool cp_parser_objc_valid_prefix_attributes
2100 (cp_parser
*, tree
*);
2101 static void cp_parser_objc_at_property
2103 static void cp_parser_objc_property_decl
2106 /* Utility Routines */
2108 static tree cp_parser_lookup_name
2109 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2110 static tree cp_parser_lookup_name_simple
2111 (cp_parser
*, tree
, location_t
);
2112 static tree cp_parser_maybe_treat_template_as_class
2114 static bool cp_parser_check_declarator_template_parameters
2115 (cp_parser
*, cp_declarator
*, location_t
);
2116 static bool cp_parser_check_template_parameters
2117 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2118 static tree cp_parser_simple_cast_expression
2120 static tree cp_parser_global_scope_opt
2121 (cp_parser
*, bool);
2122 static bool cp_parser_constructor_declarator_p
2123 (cp_parser
*, bool);
2124 static tree cp_parser_function_definition_from_specifiers_and_declarator
2125 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2126 static tree cp_parser_function_definition_after_declarator
2127 (cp_parser
*, bool);
2128 static void cp_parser_template_declaration_after_export
2129 (cp_parser
*, bool);
2130 static void cp_parser_perform_template_parameter_access_checks
2131 (VEC (deferred_access_check
,gc
)*);
2132 static tree cp_parser_single_declaration
2133 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
2134 static tree cp_parser_functional_cast
2135 (cp_parser
*, tree
);
2136 static tree cp_parser_save_member_function_body
2137 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2138 static tree cp_parser_enclosed_template_argument_list
2140 static void cp_parser_save_default_args
2141 (cp_parser
*, tree
);
2142 static void cp_parser_late_parsing_for_member
2143 (cp_parser
*, tree
);
2144 static void cp_parser_late_parsing_default_args
2145 (cp_parser
*, tree
);
2146 static tree cp_parser_sizeof_operand
2147 (cp_parser
*, enum rid
);
2148 static tree cp_parser_trait_expr
2149 (cp_parser
*, enum rid
);
2150 static bool cp_parser_declares_only_class_p
2152 static void cp_parser_set_storage_class
2153 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, location_t
);
2154 static void cp_parser_set_decl_spec_type
2155 (cp_decl_specifier_seq
*, tree
, location_t
, bool);
2156 static bool cp_parser_friend_p
2157 (const cp_decl_specifier_seq
*);
2158 static void cp_parser_required_error
2159 (cp_parser
*, required_token
, bool);
2160 static cp_token
*cp_parser_require
2161 (cp_parser
*, enum cpp_ttype
, required_token
);
2162 static cp_token
*cp_parser_require_keyword
2163 (cp_parser
*, enum rid
, required_token
);
2164 static bool cp_parser_token_starts_function_definition_p
2166 static bool cp_parser_next_token_starts_class_definition_p
2168 static bool cp_parser_next_token_ends_template_argument_p
2170 static bool cp_parser_nth_token_starts_template_argument_list_p
2171 (cp_parser
*, size_t);
2172 static enum tag_types cp_parser_token_is_class_key
2174 static void cp_parser_check_class_key
2175 (enum tag_types
, tree type
);
2176 static void cp_parser_check_access_in_redeclaration
2177 (tree type
, location_t location
);
2178 static bool cp_parser_optional_template_keyword
2180 static void cp_parser_pre_parsed_nested_name_specifier
2182 static bool cp_parser_cache_group
2183 (cp_parser
*, enum cpp_ttype
, unsigned);
2184 static void cp_parser_parse_tentatively
2186 static void cp_parser_commit_to_tentative_parse
2188 static void cp_parser_abort_tentative_parse
2190 static bool cp_parser_parse_definitely
2192 static inline bool cp_parser_parsing_tentatively
2194 static bool cp_parser_uncommitted_to_tentative_parse_p
2196 static void cp_parser_error
2197 (cp_parser
*, const char *);
2198 static void cp_parser_name_lookup_error
2199 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2200 static bool cp_parser_simulate_error
2202 static bool cp_parser_check_type_definition
2204 static void cp_parser_check_for_definition_in_return_type
2205 (cp_declarator
*, tree
, location_t type_location
);
2206 static void cp_parser_check_for_invalid_template_id
2207 (cp_parser
*, tree
, location_t location
);
2208 static bool cp_parser_non_integral_constant_expression
2209 (cp_parser
*, non_integral_constant
);
2210 static void cp_parser_diagnose_invalid_type_name
2211 (cp_parser
*, tree
, tree
, location_t
);
2212 static bool cp_parser_parse_and_diagnose_invalid_type_name
2214 static int cp_parser_skip_to_closing_parenthesis
2215 (cp_parser
*, bool, bool, bool);
2216 static void cp_parser_skip_to_end_of_statement
2218 static void cp_parser_consume_semicolon_at_end_of_statement
2220 static void cp_parser_skip_to_end_of_block_or_statement
2222 static bool cp_parser_skip_to_closing_brace
2224 static void cp_parser_skip_to_end_of_template_parameter_list
2226 static void cp_parser_skip_to_pragma_eol
2227 (cp_parser
*, cp_token
*);
2228 static bool cp_parser_error_occurred
2230 static bool cp_parser_allow_gnu_extensions_p
2232 static bool cp_parser_is_string_literal
2234 static bool cp_parser_is_keyword
2235 (cp_token
*, enum rid
);
2236 static tree cp_parser_make_typename_type
2237 (cp_parser
*, tree
, tree
, location_t location
);
2238 static cp_declarator
* cp_parser_make_indirect_declarator
2239 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2241 /* Returns nonzero if we are parsing tentatively. */
2244 cp_parser_parsing_tentatively (cp_parser
* parser
)
2246 return parser
->context
->next
!= NULL
;
2249 /* Returns nonzero if TOKEN is a string literal. */
2252 cp_parser_is_string_literal (cp_token
* token
)
2254 return (token
->type
== CPP_STRING
||
2255 token
->type
== CPP_STRING16
||
2256 token
->type
== CPP_STRING32
||
2257 token
->type
== CPP_WSTRING
||
2258 token
->type
== CPP_UTF8STRING
);
2261 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2264 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2266 return token
->keyword
== keyword
;
2269 /* If not parsing tentatively, issue a diagnostic of the form
2270 FILE:LINE: MESSAGE before TOKEN
2271 where TOKEN is the next token in the input stream. MESSAGE
2272 (specified by the caller) is usually of the form "expected
2276 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2278 if (!cp_parser_simulate_error (parser
))
2280 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2281 /* This diagnostic makes more sense if it is tagged to the line
2282 of the token we just peeked at. */
2283 cp_lexer_set_source_position_from_token (token
);
2285 if (token
->type
== CPP_PRAGMA
)
2287 error_at (token
->location
,
2288 "%<#pragma%> is not allowed here");
2289 cp_parser_skip_to_pragma_eol (parser
, token
);
2293 c_parse_error (gmsgid
,
2294 /* Because c_parser_error does not understand
2295 CPP_KEYWORD, keywords are treated like
2297 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2298 token
->u
.value
, token
->flags
);
2302 /* Issue an error about name-lookup failing. NAME is the
2303 IDENTIFIER_NODE DECL is the result of
2304 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2305 the thing that we hoped to find. */
2308 cp_parser_name_lookup_error (cp_parser
* parser
,
2311 name_lookup_error desired
,
2312 location_t location
)
2314 /* If name lookup completely failed, tell the user that NAME was not
2316 if (decl
== error_mark_node
)
2318 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2319 error_at (location
, "%<%E::%E%> has not been declared",
2320 parser
->scope
, name
);
2321 else if (parser
->scope
== global_namespace
)
2322 error_at (location
, "%<::%E%> has not been declared", name
);
2323 else if (parser
->object_scope
2324 && !CLASS_TYPE_P (parser
->object_scope
))
2325 error_at (location
, "request for member %qE in non-class type %qT",
2326 name
, parser
->object_scope
);
2327 else if (parser
->object_scope
)
2328 error_at (location
, "%<%T::%E%> has not been declared",
2329 parser
->object_scope
, name
);
2331 error_at (location
, "%qE has not been declared", name
);
2333 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2338 error_at (location
, "%<%E::%E%> is not a type",
2339 parser
->scope
, name
);
2342 error_at (location
, "%<%E::%E%> is not a class or namespace",
2343 parser
->scope
, name
);
2347 "%<%E::%E%> is not a class, namespace, or enumeration",
2348 parser
->scope
, name
);
2355 else if (parser
->scope
== global_namespace
)
2360 error_at (location
, "%<::%E%> is not a type", name
);
2363 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2367 "%<::%E%> is not a class, namespace, or enumeration",
2379 error_at (location
, "%qE is not a type", name
);
2382 error_at (location
, "%qE is not a class or namespace", name
);
2386 "%qE is not a class, namespace, or enumeration", name
);
2394 /* If we are parsing tentatively, remember that an error has occurred
2395 during this tentative parse. Returns true if the error was
2396 simulated; false if a message should be issued by the caller. */
2399 cp_parser_simulate_error (cp_parser
* parser
)
2401 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2403 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2409 /* Check for repeated decl-specifiers. */
2412 cp_parser_check_decl_spec (cp_decl_specifier_seq
*decl_specs
,
2413 location_t location
)
2417 for (ds
= ds_first
; ds
!= ds_last
; ++ds
)
2419 unsigned count
= decl_specs
->specs
[ds
];
2422 /* The "long" specifier is a special case because of "long long". */
2426 error_at (location
, "%<long long long%> is too long for GCC");
2428 pedwarn_cxx98 (location
, OPT_Wlong_long
,
2429 "ISO C++ 1998 does not support %<long long%>");
2433 static const char *const decl_spec_names
[] = {
2450 error_at (location
, "duplicate %qs", decl_spec_names
[ds
]);
2455 /* This function is called when a type is defined. If type
2456 definitions are forbidden at this point, an error message is
2460 cp_parser_check_type_definition (cp_parser
* parser
)
2462 /* If types are forbidden here, issue a message. */
2463 if (parser
->type_definition_forbidden_message
)
2465 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2466 in the message need to be interpreted. */
2467 error (parser
->type_definition_forbidden_message
);
2473 /* This function is called when the DECLARATOR is processed. The TYPE
2474 was a type defined in the decl-specifiers. If it is invalid to
2475 define a type in the decl-specifiers for DECLARATOR, an error is
2476 issued. TYPE_LOCATION is the location of TYPE and is used
2477 for error reporting. */
2480 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2481 tree type
, location_t type_location
)
2483 /* [dcl.fct] forbids type definitions in return types.
2484 Unfortunately, it's not easy to know whether or not we are
2485 processing a return type until after the fact. */
2487 && (declarator
->kind
== cdk_pointer
2488 || declarator
->kind
== cdk_reference
2489 || declarator
->kind
== cdk_ptrmem
))
2490 declarator
= declarator
->declarator
;
2492 && declarator
->kind
== cdk_function
)
2494 error_at (type_location
,
2495 "new types may not be defined in a return type");
2496 inform (type_location
,
2497 "(perhaps a semicolon is missing after the definition of %qT)",
2502 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2503 "<" in any valid C++ program. If the next token is indeed "<",
2504 issue a message warning the user about what appears to be an
2505 invalid attempt to form a template-id. LOCATION is the location
2506 of the type-specifier (TYPE) */
2509 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2510 tree type
, location_t location
)
2512 cp_token_position start
= 0;
2514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2517 error_at (location
, "%qT is not a template", type
);
2518 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2519 error_at (location
, "%qE is not a template", type
);
2521 error_at (location
, "invalid template-id");
2522 /* Remember the location of the invalid "<". */
2523 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2524 start
= cp_lexer_token_position (parser
->lexer
, true);
2525 /* Consume the "<". */
2526 cp_lexer_consume_token (parser
->lexer
);
2527 /* Parse the template arguments. */
2528 cp_parser_enclosed_template_argument_list (parser
);
2529 /* Permanently remove the invalid template arguments so that
2530 this error message is not issued again. */
2532 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2536 /* If parsing an integral constant-expression, issue an error message
2537 about the fact that THING appeared and return true. Otherwise,
2538 return false. In either case, set
2539 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2542 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2543 non_integral_constant thing
)
2545 parser
->non_integral_constant_expression_p
= true;
2546 if (parser
->integral_constant_expression_p
)
2548 if (!parser
->allow_non_integral_constant_expression_p
)
2550 const char *msg
= NULL
;
2554 error ("floating-point literal "
2555 "cannot appear in a constant-expression");
2558 error ("a cast to a type other than an integral or "
2559 "enumeration type cannot appear in a "
2560 "constant-expression");
2563 error ("%<typeid%> operator "
2564 "cannot appear in a constant-expression");
2567 error ("non-constant compound literals "
2568 "cannot appear in a constant-expression");
2571 error ("a function call "
2572 "cannot appear in a constant-expression");
2575 error ("an increment "
2576 "cannot appear in a constant-expression");
2579 error ("an decrement "
2580 "cannot appear in a constant-expression");
2583 error ("an array reference "
2584 "cannot appear in a constant-expression");
2586 case NIC_ADDR_LABEL
:
2587 error ("the address of a label "
2588 "cannot appear in a constant-expression");
2590 case NIC_OVERLOADED
:
2591 error ("calls to overloaded operators "
2592 "cannot appear in a constant-expression");
2594 case NIC_ASSIGNMENT
:
2595 error ("an assignment cannot appear in a constant-expression");
2598 error ("a comma operator "
2599 "cannot appear in a constant-expression");
2601 case NIC_CONSTRUCTOR
:
2602 error ("a call to a constructor "
2603 "cannot appear in a constant-expression");
2609 msg
= "__FUNCTION__";
2611 case NIC_PRETTY_FUNC
:
2612 msg
= "__PRETTY_FUNCTION__";
2632 case NIC_PREINCREMENT
:
2635 case NIC_PREDECREMENT
:
2648 error ("%qs cannot appear in a constant-expression", msg
);
2655 /* Emit a diagnostic for an invalid type name. SCOPE is the
2656 qualifying scope (or NULL, if none) for ID. This function commits
2657 to the current active tentative parse, if any. (Otherwise, the
2658 problematic construct might be encountered again later, resulting
2659 in duplicate error messages.) LOCATION is the location of ID. */
2662 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2663 tree scope
, tree id
,
2664 location_t location
)
2666 tree decl
, old_scope
;
2667 /* Try to lookup the identifier. */
2668 old_scope
= parser
->scope
;
2669 parser
->scope
= scope
;
2670 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2671 parser
->scope
= old_scope
;
2672 /* If the lookup found a template-name, it means that the user forgot
2673 to specify an argument list. Emit a useful error message. */
2674 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2676 "invalid use of template-name %qE without an argument list",
2678 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2679 error_at (location
, "invalid use of destructor %qD as a type", id
);
2680 else if (TREE_CODE (decl
) == TYPE_DECL
)
2681 /* Something like 'unsigned A a;' */
2682 error_at (location
, "invalid combination of multiple type-specifiers");
2683 else if (!parser
->scope
)
2685 /* Issue an error message. */
2686 error_at (location
, "%qE does not name a type", id
);
2687 /* If we're in a template class, it's possible that the user was
2688 referring to a type from a base class. For example:
2690 template <typename T> struct A { typedef T X; };
2691 template <typename T> struct B : public A<T> { X x; };
2693 The user should have said "typename A<T>::X". */
2694 if (processing_template_decl
&& current_class_type
2695 && TYPE_BINFO (current_class_type
))
2699 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2703 tree base_type
= BINFO_TYPE (b
);
2704 if (CLASS_TYPE_P (base_type
)
2705 && dependent_type_p (base_type
))
2708 /* Go from a particular instantiation of the
2709 template (which will have an empty TYPE_FIELDs),
2710 to the main version. */
2711 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2712 for (field
= TYPE_FIELDS (base_type
);
2714 field
= DECL_CHAIN (field
))
2715 if (TREE_CODE (field
) == TYPE_DECL
2716 && DECL_NAME (field
) == id
)
2719 "(perhaps %<typename %T::%E%> was intended)",
2720 BINFO_TYPE (b
), id
);
2729 /* Here we diagnose qualified-ids where the scope is actually correct,
2730 but the identifier does not resolve to a valid type name. */
2731 else if (parser
->scope
!= error_mark_node
)
2733 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2734 error_at (location
, "%qE in namespace %qE does not name a type",
2736 else if (CLASS_TYPE_P (parser
->scope
)
2737 && constructor_name_p (id
, parser
->scope
))
2740 error_at (location
, "%<%T::%E%> names the constructor, not"
2741 " the type", parser
->scope
, id
);
2742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2743 error_at (location
, "and %qT has no template constructors",
2746 else if (TYPE_P (parser
->scope
)
2747 && dependent_scope_p (parser
->scope
))
2748 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2749 "%qT is a dependent scope",
2750 parser
->scope
, id
, parser
->scope
);
2751 else if (TYPE_P (parser
->scope
))
2752 error_at (location
, "%qE in class %qT does not name a type",
2757 cp_parser_commit_to_tentative_parse (parser
);
2760 /* Check for a common situation where a type-name should be present,
2761 but is not, and issue a sensible error message. Returns true if an
2762 invalid type-name was detected.
2764 The situation handled by this function are variable declarations of the
2765 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2766 Usually, `ID' should name a type, but if we got here it means that it
2767 does not. We try to emit the best possible error message depending on
2768 how exactly the id-expression looks like. */
2771 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2774 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2776 /* Avoid duplicate error about ambiguous lookup. */
2777 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2779 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2780 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2784 cp_parser_parse_tentatively (parser
);
2785 id
= cp_parser_id_expression (parser
,
2786 /*template_keyword_p=*/false,
2787 /*check_dependency_p=*/true,
2788 /*template_p=*/NULL
,
2789 /*declarator_p=*/true,
2790 /*optional_p=*/false);
2791 /* If the next token is a (, this is a function with no explicit return
2792 type, i.e. constructor, destructor or conversion op. */
2793 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
2794 || TREE_CODE (id
) == TYPE_DECL
)
2796 cp_parser_abort_tentative_parse (parser
);
2799 if (!cp_parser_parse_definitely (parser
))
2802 /* Emit a diagnostic for the invalid type. */
2803 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2804 id
, token
->location
);
2806 /* If we aren't in the middle of a declarator (i.e. in a
2807 parameter-declaration-clause), skip to the end of the declaration;
2808 there's no point in trying to process it. */
2809 if (!parser
->in_declarator_p
)
2810 cp_parser_skip_to_end_of_block_or_statement (parser
);
2814 /* Consume tokens up to, and including, the next non-nested closing `)'.
2815 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2816 are doing error recovery. Returns -1 if OR_COMMA is true and we
2817 found an unnested comma. */
2820 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2825 unsigned paren_depth
= 0;
2826 unsigned brace_depth
= 0;
2827 unsigned square_depth
= 0;
2829 if (recovering
&& !or_comma
2830 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2835 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2837 switch (token
->type
)
2840 case CPP_PRAGMA_EOL
:
2841 /* If we've run out of tokens, then there is no closing `)'. */
2844 /* This is good for lambda expression capture-lists. */
2845 case CPP_OPEN_SQUARE
:
2848 case CPP_CLOSE_SQUARE
:
2849 if (!square_depth
--)
2854 /* This matches the processing in skip_to_end_of_statement. */
2859 case CPP_OPEN_BRACE
:
2862 case CPP_CLOSE_BRACE
:
2868 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
2873 case CPP_OPEN_PAREN
:
2878 case CPP_CLOSE_PAREN
:
2879 if (!brace_depth
&& !paren_depth
--)
2882 cp_lexer_consume_token (parser
->lexer
);
2891 /* Consume the token. */
2892 cp_lexer_consume_token (parser
->lexer
);
2896 /* Consume tokens until we reach the end of the current statement.
2897 Normally, that will be just before consuming a `;'. However, if a
2898 non-nested `}' comes first, then we stop before consuming that. */
2901 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2903 unsigned nesting_depth
= 0;
2907 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2909 switch (token
->type
)
2912 case CPP_PRAGMA_EOL
:
2913 /* If we've run out of tokens, stop. */
2917 /* If the next token is a `;', we have reached the end of the
2923 case CPP_CLOSE_BRACE
:
2924 /* If this is a non-nested '}', stop before consuming it.
2925 That way, when confronted with something like:
2929 we stop before consuming the closing '}', even though we
2930 have not yet reached a `;'. */
2931 if (nesting_depth
== 0)
2934 /* If it is the closing '}' for a block that we have
2935 scanned, stop -- but only after consuming the token.
2941 we will stop after the body of the erroneously declared
2942 function, but before consuming the following `typedef'
2944 if (--nesting_depth
== 0)
2946 cp_lexer_consume_token (parser
->lexer
);
2950 case CPP_OPEN_BRACE
:
2958 /* Consume the token. */
2959 cp_lexer_consume_token (parser
->lexer
);
2963 /* This function is called at the end of a statement or declaration.
2964 If the next token is a semicolon, it is consumed; otherwise, error
2965 recovery is attempted. */
2968 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
2970 /* Look for the trailing `;'. */
2971 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
2973 /* If there is additional (erroneous) input, skip to the end of
2975 cp_parser_skip_to_end_of_statement (parser
);
2976 /* If the next token is now a `;', consume it. */
2977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
2978 cp_lexer_consume_token (parser
->lexer
);
2982 /* Skip tokens until we have consumed an entire block, or until we
2983 have consumed a non-nested `;'. */
2986 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
2988 int nesting_depth
= 0;
2990 while (nesting_depth
>= 0)
2992 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2994 switch (token
->type
)
2997 case CPP_PRAGMA_EOL
:
2998 /* If we've run out of tokens, stop. */
3002 /* Stop if this is an unnested ';'. */
3007 case CPP_CLOSE_BRACE
:
3008 /* Stop if this is an unnested '}', or closes the outermost
3011 if (nesting_depth
< 0)
3017 case CPP_OPEN_BRACE
:
3026 /* Consume the token. */
3027 cp_lexer_consume_token (parser
->lexer
);
3031 /* Skip tokens until a non-nested closing curly brace is the next
3032 token, or there are no more tokens. Return true in the first case,
3036 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3038 unsigned nesting_depth
= 0;
3042 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3044 switch (token
->type
)
3047 case CPP_PRAGMA_EOL
:
3048 /* If we've run out of tokens, stop. */
3051 case CPP_CLOSE_BRACE
:
3052 /* If the next token is a non-nested `}', then we have reached
3053 the end of the current block. */
3054 if (nesting_depth
-- == 0)
3058 case CPP_OPEN_BRACE
:
3059 /* If it the next token is a `{', then we are entering a new
3060 block. Consume the entire block. */
3068 /* Consume the token. */
3069 cp_lexer_consume_token (parser
->lexer
);
3073 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3074 parameter is the PRAGMA token, allowing us to purge the entire pragma
3078 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3082 parser
->lexer
->in_pragma
= false;
3085 token
= cp_lexer_consume_token (parser
->lexer
);
3086 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3088 /* Ensure that the pragma is not parsed again. */
3089 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3092 /* Require pragma end of line, resyncing with it as necessary. The
3093 arguments are as for cp_parser_skip_to_pragma_eol. */
3096 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3098 parser
->lexer
->in_pragma
= false;
3099 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3100 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3103 /* This is a simple wrapper around make_typename_type. When the id is
3104 an unresolved identifier node, we can provide a superior diagnostic
3105 using cp_parser_diagnose_invalid_type_name. */
3108 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3109 tree id
, location_t id_location
)
3112 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3114 result
= make_typename_type (scope
, id
, typename_type
,
3115 /*complain=*/tf_none
);
3116 if (result
== error_mark_node
)
3117 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3120 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3123 /* This is a wrapper around the
3124 make_{pointer,ptrmem,reference}_declarator functions that decides
3125 which one to call based on the CODE and CLASS_TYPE arguments. The
3126 CODE argument should be one of the values returned by
3127 cp_parser_ptr_operator. */
3128 static cp_declarator
*
3129 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3130 cp_cv_quals cv_qualifiers
,
3131 cp_declarator
*target
)
3133 if (code
== ERROR_MARK
)
3134 return cp_error_declarator
;
3136 if (code
== INDIRECT_REF
)
3137 if (class_type
== NULL_TREE
)
3138 return make_pointer_declarator (cv_qualifiers
, target
);
3140 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
3141 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3142 return make_reference_declarator (cv_qualifiers
, target
, false);
3143 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3144 return make_reference_declarator (cv_qualifiers
, target
, true);
3148 /* Create a new C++ parser. */
3151 cp_parser_new (void)
3157 /* cp_lexer_new_main is called before doing GC allocation because
3158 cp_lexer_new_main might load a PCH file. */
3159 lexer
= cp_lexer_new_main ();
3161 /* Initialize the binops_by_token so that we can get the tree
3162 directly from the token. */
3163 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3164 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3166 parser
= ggc_alloc_cleared_cp_parser ();
3167 parser
->lexer
= lexer
;
3168 parser
->context
= cp_parser_context_new (NULL
);
3170 /* For now, we always accept GNU extensions. */
3171 parser
->allow_gnu_extensions_p
= 1;
3173 /* The `>' token is a greater-than operator, not the end of a
3175 parser
->greater_than_is_operator_p
= true;
3177 parser
->default_arg_ok_p
= true;
3179 /* We are not parsing a constant-expression. */
3180 parser
->integral_constant_expression_p
= false;
3181 parser
->allow_non_integral_constant_expression_p
= false;
3182 parser
->non_integral_constant_expression_p
= false;
3184 /* Local variable names are not forbidden. */
3185 parser
->local_variables_forbidden_p
= false;
3187 /* We are not processing an `extern "C"' declaration. */
3188 parser
->in_unbraced_linkage_specification_p
= false;
3190 /* We are not processing a declarator. */
3191 parser
->in_declarator_p
= false;
3193 /* We are not processing a template-argument-list. */
3194 parser
->in_template_argument_list_p
= false;
3196 /* We are not in an iteration statement. */
3197 parser
->in_statement
= 0;
3199 /* We are not in a switch statement. */
3200 parser
->in_switch_statement_p
= false;
3202 /* We are not parsing a type-id inside an expression. */
3203 parser
->in_type_id_in_expr_p
= false;
3205 /* Declarations aren't implicitly extern "C". */
3206 parser
->implicit_extern_c
= false;
3208 /* String literals should be translated to the execution character set. */
3209 parser
->translate_strings_p
= true;
3211 /* We are not parsing a function body. */
3212 parser
->in_function_body
= false;
3214 /* The unparsed function queue is empty. */
3215 push_unparsed_function_queues (parser
);
3217 /* There are no classes being defined. */
3218 parser
->num_classes_being_defined
= 0;
3220 /* No template parameters apply. */
3221 parser
->num_template_parameter_lists
= 0;
3226 /* Create a cp_lexer structure which will emit the tokens in CACHE
3227 and push it onto the parser's lexer stack. This is used for delayed
3228 parsing of in-class method bodies and default arguments, and should
3229 not be confused with tentative parsing. */
3231 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3233 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3234 lexer
->next
= parser
->lexer
;
3235 parser
->lexer
= lexer
;
3237 /* Move the current source position to that of the first token in the
3239 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3242 /* Pop the top lexer off the parser stack. This is never used for the
3243 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3245 cp_parser_pop_lexer (cp_parser
*parser
)
3247 cp_lexer
*lexer
= parser
->lexer
;
3248 parser
->lexer
= lexer
->next
;
3249 cp_lexer_destroy (lexer
);
3251 /* Put the current source position back where it was before this
3252 lexer was pushed. */
3253 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3256 /* Lexical conventions [gram.lex] */
3258 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3262 cp_parser_identifier (cp_parser
* parser
)
3266 /* Look for the identifier. */
3267 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3268 /* Return the value. */
3269 return token
? token
->u
.value
: error_mark_node
;
3272 /* Parse a sequence of adjacent string constants. Returns a
3273 TREE_STRING representing the combined, nul-terminated string
3274 constant. If TRANSLATE is true, translate the string to the
3275 execution character set. If WIDE_OK is true, a wide string is
3278 C++98 [lex.string] says that if a narrow string literal token is
3279 adjacent to a wide string literal token, the behavior is undefined.
3280 However, C99 6.4.5p4 says that this results in a wide string literal.
3281 We follow C99 here, for consistency with the C front end.
3283 This code is largely lifted from lex_string() in c-lex.c.
3285 FUTURE: ObjC++ will need to handle @-strings here. */
3287 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3291 struct obstack str_ob
;
3292 cpp_string str
, istr
, *strs
;
3294 enum cpp_ttype type
;
3296 tok
= cp_lexer_peek_token (parser
->lexer
);
3297 if (!cp_parser_is_string_literal (tok
))
3299 cp_parser_error (parser
, "expected string-literal");
3300 return error_mark_node
;
3305 /* Try to avoid the overhead of creating and destroying an obstack
3306 for the common case of just one string. */
3307 if (!cp_parser_is_string_literal
3308 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3310 cp_lexer_consume_token (parser
->lexer
);
3312 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
3313 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3320 gcc_obstack_init (&str_ob
);
3325 cp_lexer_consume_token (parser
->lexer
);
3327 str
.text
= (const unsigned char *)TREE_STRING_POINTER (tok
->u
.value
);
3328 str
.len
= TREE_STRING_LENGTH (tok
->u
.value
);
3330 if (type
!= tok
->type
)
3332 if (type
== CPP_STRING
)
3334 else if (tok
->type
!= CPP_STRING
)
3335 error_at (tok
->location
,
3336 "unsupported non-standard concatenation "
3337 "of string literals");
3340 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3342 tok
= cp_lexer_peek_token (parser
->lexer
);
3344 while (cp_parser_is_string_literal (tok
));
3346 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3349 if (type
!= CPP_STRING
&& !wide_ok
)
3351 cp_parser_error (parser
, "a wide string is invalid in this context");
3355 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3356 (parse_in
, strs
, count
, &istr
, type
))
3358 value
= build_string (istr
.len
, (const char *)istr
.text
);
3359 free (CONST_CAST (unsigned char *, istr
.text
));
3365 case CPP_UTF8STRING
:
3366 TREE_TYPE (value
) = char_array_type_node
;
3369 TREE_TYPE (value
) = char16_array_type_node
;
3372 TREE_TYPE (value
) = char32_array_type_node
;
3375 TREE_TYPE (value
) = wchar_array_type_node
;
3379 value
= fix_string_type (value
);
3382 /* cpp_interpret_string has issued an error. */
3383 value
= error_mark_node
;
3386 obstack_free (&str_ob
, 0);
3392 /* Basic concepts [gram.basic] */
3394 /* Parse a translation-unit.
3397 declaration-seq [opt]
3399 Returns TRUE if all went well. */
3402 cp_parser_translation_unit (cp_parser
* parser
)
3404 /* The address of the first non-permanent object on the declarator
3406 static void *declarator_obstack_base
;
3410 /* Create the declarator obstack, if necessary. */
3411 if (!cp_error_declarator
)
3413 gcc_obstack_init (&declarator_obstack
);
3414 /* Create the error declarator. */
3415 cp_error_declarator
= make_declarator (cdk_error
);
3416 /* Create the empty parameter list. */
3417 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3418 /* Remember where the base of the declarator obstack lies. */
3419 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3422 cp_parser_declaration_seq_opt (parser
);
3424 /* If there are no tokens left then all went well. */
3425 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3427 /* Get rid of the token array; we don't need it any more. */
3428 cp_lexer_destroy (parser
->lexer
);
3429 parser
->lexer
= NULL
;
3431 /* This file might have been a context that's implicitly extern
3432 "C". If so, pop the lang context. (Only relevant for PCH.) */
3433 if (parser
->implicit_extern_c
)
3435 pop_lang_context ();
3436 parser
->implicit_extern_c
= false;
3440 finish_translation_unit ();
3446 cp_parser_error (parser
, "expected declaration");
3450 /* Make sure the declarator obstack was fully cleaned up. */
3451 gcc_assert (obstack_next_free (&declarator_obstack
)
3452 == declarator_obstack_base
);
3454 /* All went well. */
3458 /* Expressions [gram.expr] */
3460 /* Parse a primary-expression.
3471 ( compound-statement )
3472 __builtin_va_arg ( assignment-expression , type-id )
3473 __builtin_offsetof ( type-id , offsetof-expression )
3476 __has_nothrow_assign ( type-id )
3477 __has_nothrow_constructor ( type-id )
3478 __has_nothrow_copy ( type-id )
3479 __has_trivial_assign ( type-id )
3480 __has_trivial_constructor ( type-id )
3481 __has_trivial_copy ( type-id )
3482 __has_trivial_destructor ( type-id )
3483 __has_virtual_destructor ( type-id )
3484 __is_abstract ( type-id )
3485 __is_base_of ( type-id , type-id )
3486 __is_class ( type-id )
3487 __is_convertible_to ( type-id , type-id )
3488 __is_empty ( type-id )
3489 __is_enum ( type-id )
3490 __is_pod ( type-id )
3491 __is_polymorphic ( type-id )
3492 __is_union ( type-id )
3494 Objective-C++ Extension:
3502 ADDRESS_P is true iff this expression was immediately preceded by
3503 "&" and therefore might denote a pointer-to-member. CAST_P is true
3504 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3505 true iff this expression is a template argument.
3507 Returns a representation of the expression. Upon return, *IDK
3508 indicates what kind of id-expression (if any) was present. */
3511 cp_parser_primary_expression (cp_parser
*parser
,
3514 bool template_arg_p
,
3517 cp_token
*token
= NULL
;
3519 /* Assume the primary expression is not an id-expression. */
3520 *idk
= CP_ID_KIND_NONE
;
3522 /* Peek at the next token. */
3523 token
= cp_lexer_peek_token (parser
->lexer
);
3524 switch (token
->type
)
3537 token
= cp_lexer_consume_token (parser
->lexer
);
3538 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
3540 error_at (token
->location
,
3541 "fixed-point types not supported in C++");
3542 return error_mark_node
;
3544 /* Floating-point literals are only allowed in an integral
3545 constant expression if they are cast to an integral or
3546 enumeration type. */
3547 if (TREE_CODE (token
->u
.value
) == REAL_CST
3548 && parser
->integral_constant_expression_p
3551 /* CAST_P will be set even in invalid code like "int(2.7 +
3552 ...)". Therefore, we have to check that the next token
3553 is sure to end the cast. */
3556 cp_token
*next_token
;
3558 next_token
= cp_lexer_peek_token (parser
->lexer
);
3559 if (/* The comma at the end of an
3560 enumerator-definition. */
3561 next_token
->type
!= CPP_COMMA
3562 /* The curly brace at the end of an enum-specifier. */
3563 && next_token
->type
!= CPP_CLOSE_BRACE
3564 /* The end of a statement. */
3565 && next_token
->type
!= CPP_SEMICOLON
3566 /* The end of the cast-expression. */
3567 && next_token
->type
!= CPP_CLOSE_PAREN
3568 /* The end of an array bound. */
3569 && next_token
->type
!= CPP_CLOSE_SQUARE
3570 /* The closing ">" in a template-argument-list. */
3571 && (next_token
->type
!= CPP_GREATER
3572 || parser
->greater_than_is_operator_p
)
3573 /* C++0x only: A ">>" treated like two ">" tokens,
3574 in a template-argument-list. */
3575 && (next_token
->type
!= CPP_RSHIFT
3576 || (cxx_dialect
== cxx98
)
3577 || parser
->greater_than_is_operator_p
))
3581 /* If we are within a cast, then the constraint that the
3582 cast is to an integral or enumeration type will be
3583 checked at that point. If we are not within a cast, then
3584 this code is invalid. */
3586 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
3588 return token
->u
.value
;
3594 case CPP_UTF8STRING
:
3595 /* ??? Should wide strings be allowed when parser->translate_strings_p
3596 is false (i.e. in attributes)? If not, we can kill the third
3597 argument to cp_parser_string_literal. */
3598 return cp_parser_string_literal (parser
,
3599 parser
->translate_strings_p
,
3602 case CPP_OPEN_PAREN
:
3605 bool saved_greater_than_is_operator_p
;
3607 /* Consume the `('. */
3608 cp_lexer_consume_token (parser
->lexer
);
3609 /* Within a parenthesized expression, a `>' token is always
3610 the greater-than operator. */
3611 saved_greater_than_is_operator_p
3612 = parser
->greater_than_is_operator_p
;
3613 parser
->greater_than_is_operator_p
= true;
3614 /* If we see `( { ' then we are looking at the beginning of
3615 a GNU statement-expression. */
3616 if (cp_parser_allow_gnu_extensions_p (parser
)
3617 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3619 /* Statement-expressions are not allowed by the standard. */
3620 pedwarn (token
->location
, OPT_pedantic
,
3621 "ISO C++ forbids braced-groups within expressions");
3623 /* And they're not allowed outside of a function-body; you
3624 cannot, for example, write:
3626 int i = ({ int j = 3; j + 1; });
3628 at class or namespace scope. */
3629 if (!parser
->in_function_body
3630 || parser
->in_template_argument_list_p
)
3632 error_at (token
->location
,
3633 "statement-expressions are not allowed outside "
3634 "functions nor in template-argument lists");
3635 cp_parser_skip_to_end_of_block_or_statement (parser
);
3636 expr
= error_mark_node
;
3640 /* Start the statement-expression. */
3641 expr
= begin_stmt_expr ();
3642 /* Parse the compound-statement. */
3643 cp_parser_compound_statement (parser
, expr
, false);
3645 expr
= finish_stmt_expr (expr
, false);
3650 /* Parse the parenthesized expression. */
3651 expr
= cp_parser_expression (parser
, cast_p
, idk
);
3652 /* Let the front end know that this expression was
3653 enclosed in parentheses. This matters in case, for
3654 example, the expression is of the form `A::B', since
3655 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3657 finish_parenthesized_expr (expr
);
3659 /* The `>' token might be the end of a template-id or
3660 template-parameter-list now. */
3661 parser
->greater_than_is_operator_p
3662 = saved_greater_than_is_operator_p
;
3663 /* Consume the `)'. */
3664 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
3665 cp_parser_skip_to_end_of_statement (parser
);
3670 case CPP_OPEN_SQUARE
:
3671 if (c_dialect_objc ())
3672 /* We have an Objective-C++ message. */
3673 return cp_parser_objc_expression (parser
);
3674 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
3675 return cp_parser_lambda_expression (parser
);
3677 case CPP_OBJC_STRING
:
3678 if (c_dialect_objc ())
3679 /* We have an Objective-C++ string literal. */
3680 return cp_parser_objc_expression (parser
);
3681 cp_parser_error (parser
, "expected primary-expression");
3682 return error_mark_node
;
3685 switch (token
->keyword
)
3687 /* These two are the boolean literals. */
3689 cp_lexer_consume_token (parser
->lexer
);
3690 return boolean_true_node
;
3692 cp_lexer_consume_token (parser
->lexer
);
3693 return boolean_false_node
;
3695 /* The `__null' literal. */
3697 cp_lexer_consume_token (parser
->lexer
);
3700 /* The `nullptr' literal. */
3702 cp_lexer_consume_token (parser
->lexer
);
3703 return nullptr_node
;
3705 /* Recognize the `this' keyword. */
3707 cp_lexer_consume_token (parser
->lexer
);
3708 if (parser
->local_variables_forbidden_p
)
3710 error_at (token
->location
,
3711 "%<this%> may not be used in this context");
3712 return error_mark_node
;
3714 /* Pointers cannot appear in constant-expressions. */
3715 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
3716 return error_mark_node
;
3717 return finish_this_expr ();
3719 /* The `operator' keyword can be the beginning of an
3724 case RID_FUNCTION_NAME
:
3725 case RID_PRETTY_FUNCTION_NAME
:
3726 case RID_C99_FUNCTION_NAME
:
3728 non_integral_constant name
;
3730 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3731 __func__ are the names of variables -- but they are
3732 treated specially. Therefore, they are handled here,
3733 rather than relying on the generic id-expression logic
3734 below. Grammatically, these names are id-expressions.
3736 Consume the token. */
3737 token
= cp_lexer_consume_token (parser
->lexer
);
3739 switch (token
->keyword
)
3741 case RID_FUNCTION_NAME
:
3742 name
= NIC_FUNC_NAME
;
3744 case RID_PRETTY_FUNCTION_NAME
:
3745 name
= NIC_PRETTY_FUNC
;
3747 case RID_C99_FUNCTION_NAME
:
3748 name
= NIC_C99_FUNC
;
3754 if (cp_parser_non_integral_constant_expression (parser
, name
))
3755 return error_mark_node
;
3757 /* Look up the name. */
3758 return finish_fname (token
->u
.value
);
3766 /* The `__builtin_va_arg' construct is used to handle
3767 `va_arg'. Consume the `__builtin_va_arg' token. */
3768 cp_lexer_consume_token (parser
->lexer
);
3769 /* Look for the opening `('. */
3770 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
3771 /* Now, parse the assignment-expression. */
3772 expression
= cp_parser_assignment_expression (parser
,
3773 /*cast_p=*/false, NULL
);
3774 /* Look for the `,'. */
3775 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
3776 /* Parse the type-id. */
3777 type
= cp_parser_type_id (parser
);
3778 /* Look for the closing `)'. */
3779 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
3780 /* Using `va_arg' in a constant-expression is not
3782 if (cp_parser_non_integral_constant_expression (parser
,
3784 return error_mark_node
;
3785 return build_x_va_arg (expression
, type
);
3789 return cp_parser_builtin_offsetof (parser
);
3791 case RID_HAS_NOTHROW_ASSIGN
:
3792 case RID_HAS_NOTHROW_CONSTRUCTOR
:
3793 case RID_HAS_NOTHROW_COPY
:
3794 case RID_HAS_TRIVIAL_ASSIGN
:
3795 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
3796 case RID_HAS_TRIVIAL_COPY
:
3797 case RID_HAS_TRIVIAL_DESTRUCTOR
:
3798 case RID_HAS_VIRTUAL_DESTRUCTOR
:
3799 case RID_IS_ABSTRACT
:
3800 case RID_IS_BASE_OF
:
3802 case RID_IS_CONVERTIBLE_TO
:
3806 case RID_IS_POLYMORPHIC
:
3807 case RID_IS_STD_LAYOUT
:
3808 case RID_IS_TRIVIAL
:
3810 return cp_parser_trait_expr (parser
, token
->keyword
);
3812 /* Objective-C++ expressions. */
3814 case RID_AT_PROTOCOL
:
3815 case RID_AT_SELECTOR
:
3816 return cp_parser_objc_expression (parser
);
3819 if (parser
->in_function_body
3820 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
3823 error_at (token
->location
,
3824 "a template declaration cannot appear at block scope");
3825 cp_parser_skip_to_end_of_block_or_statement (parser
);
3826 return error_mark_node
;
3829 cp_parser_error (parser
, "expected primary-expression");
3830 return error_mark_node
;
3833 /* An id-expression can start with either an identifier, a
3834 `::' as the beginning of a qualified-id, or the "operator"
3838 case CPP_TEMPLATE_ID
:
3839 case CPP_NESTED_NAME_SPECIFIER
:
3843 const char *error_msg
;
3846 cp_token
*id_expr_token
;
3849 /* Parse the id-expression. */
3851 = cp_parser_id_expression (parser
,
3852 /*template_keyword_p=*/false,
3853 /*check_dependency_p=*/true,
3855 /*declarator_p=*/false,
3856 /*optional_p=*/false);
3857 if (id_expression
== error_mark_node
)
3858 return error_mark_node
;
3859 id_expr_token
= token
;
3860 token
= cp_lexer_peek_token (parser
->lexer
);
3861 done
= (token
->type
!= CPP_OPEN_SQUARE
3862 && token
->type
!= CPP_OPEN_PAREN
3863 && token
->type
!= CPP_DOT
3864 && token
->type
!= CPP_DEREF
3865 && token
->type
!= CPP_PLUS_PLUS
3866 && token
->type
!= CPP_MINUS_MINUS
);
3867 /* If we have a template-id, then no further lookup is
3868 required. If the template-id was for a template-class, we
3869 will sometimes have a TYPE_DECL at this point. */
3870 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
3871 || TREE_CODE (id_expression
) == TYPE_DECL
)
3872 decl
= id_expression
;
3873 /* Look up the name. */
3876 tree ambiguous_decls
;
3878 /* If we already know that this lookup is ambiguous, then
3879 we've already issued an error message; there's no reason
3881 if (id_expr_token
->type
== CPP_NAME
3882 && id_expr_token
->ambiguous_p
)
3884 cp_parser_simulate_error (parser
);
3885 return error_mark_node
;
3888 decl
= cp_parser_lookup_name (parser
, id_expression
,
3891 /*is_namespace=*/false,
3892 /*check_dependency=*/true,
3894 id_expr_token
->location
);
3895 /* If the lookup was ambiguous, an error will already have
3897 if (ambiguous_decls
)
3898 return error_mark_node
;
3900 /* In Objective-C++, an instance variable (ivar) may be preferred
3901 to whatever cp_parser_lookup_name() found. */
3902 decl
= objc_lookup_ivar (decl
, id_expression
);
3904 /* If name lookup gives us a SCOPE_REF, then the
3905 qualifying scope was dependent. */
3906 if (TREE_CODE (decl
) == SCOPE_REF
)
3908 /* At this point, we do not know if DECL is a valid
3909 integral constant expression. We assume that it is
3910 in fact such an expression, so that code like:
3912 template <int N> struct A {
3916 is accepted. At template-instantiation time, we
3917 will check that B<N>::i is actually a constant. */
3920 /* Check to see if DECL is a local variable in a context
3921 where that is forbidden. */
3922 if (parser
->local_variables_forbidden_p
3923 && local_variable_p (decl
))
3925 /* It might be that we only found DECL because we are
3926 trying to be generous with pre-ISO scoping rules.
3927 For example, consider:
3931 for (int i = 0; i < 10; ++i) {}
3932 extern void f(int j = i);
3935 Here, name look up will originally find the out
3936 of scope `i'. We need to issue a warning message,
3937 but then use the global `i'. */
3938 decl
= check_for_out_of_scope_variable (decl
);
3939 if (local_variable_p (decl
))
3941 error_at (id_expr_token
->location
,
3942 "local variable %qD may not appear in this context",
3944 return error_mark_node
;
3949 decl
= (finish_id_expression
3950 (id_expression
, decl
, parser
->scope
,
3952 parser
->integral_constant_expression_p
,
3953 parser
->allow_non_integral_constant_expression_p
,
3954 &parser
->non_integral_constant_expression_p
,
3955 template_p
, done
, address_p
,
3958 id_expr_token
->location
));
3960 cp_parser_error (parser
, error_msg
);
3964 /* Anything else is an error. */
3966 cp_parser_error (parser
, "expected primary-expression");
3967 return error_mark_node
;
3971 /* Parse an id-expression.
3978 :: [opt] nested-name-specifier template [opt] unqualified-id
3980 :: operator-function-id
3983 Return a representation of the unqualified portion of the
3984 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3985 a `::' or nested-name-specifier.
3987 Often, if the id-expression was a qualified-id, the caller will
3988 want to make a SCOPE_REF to represent the qualified-id. This
3989 function does not do this in order to avoid wastefully creating
3990 SCOPE_REFs when they are not required.
3992 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3995 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3996 uninstantiated templates.
3998 If *TEMPLATE_P is non-NULL, it is set to true iff the
3999 `template' keyword is used to explicitly indicate that the entity
4000 named is a template.
4002 If DECLARATOR_P is true, the id-expression is appearing as part of
4003 a declarator, rather than as part of an expression. */
4006 cp_parser_id_expression (cp_parser
*parser
,
4007 bool template_keyword_p
,
4008 bool check_dependency_p
,
4013 bool global_scope_p
;
4014 bool nested_name_specifier_p
;
4016 /* Assume the `template' keyword was not used. */
4018 *template_p
= template_keyword_p
;
4020 /* Look for the optional `::' operator. */
4022 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4024 /* Look for the optional nested-name-specifier. */
4025 nested_name_specifier_p
4026 = (cp_parser_nested_name_specifier_opt (parser
,
4027 /*typename_keyword_p=*/false,
4032 /* If there is a nested-name-specifier, then we are looking at
4033 the first qualified-id production. */
4034 if (nested_name_specifier_p
)
4037 tree saved_object_scope
;
4038 tree saved_qualifying_scope
;
4039 tree unqualified_id
;
4042 /* See if the next token is the `template' keyword. */
4044 template_p
= &is_template
;
4045 *template_p
= cp_parser_optional_template_keyword (parser
);
4046 /* Name lookup we do during the processing of the
4047 unqualified-id might obliterate SCOPE. */
4048 saved_scope
= parser
->scope
;
4049 saved_object_scope
= parser
->object_scope
;
4050 saved_qualifying_scope
= parser
->qualifying_scope
;
4051 /* Process the final unqualified-id. */
4052 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4055 /*optional_p=*/false);
4056 /* Restore the SAVED_SCOPE for our caller. */
4057 parser
->scope
= saved_scope
;
4058 parser
->object_scope
= saved_object_scope
;
4059 parser
->qualifying_scope
= saved_qualifying_scope
;
4061 return unqualified_id
;
4063 /* Otherwise, if we are in global scope, then we are looking at one
4064 of the other qualified-id productions. */
4065 else if (global_scope_p
)
4070 /* Peek at the next token. */
4071 token
= cp_lexer_peek_token (parser
->lexer
);
4073 /* If it's an identifier, and the next token is not a "<", then
4074 we can avoid the template-id case. This is an optimization
4075 for this common case. */
4076 if (token
->type
== CPP_NAME
4077 && !cp_parser_nth_token_starts_template_argument_list_p
4079 return cp_parser_identifier (parser
);
4081 cp_parser_parse_tentatively (parser
);
4082 /* Try a template-id. */
4083 id
= cp_parser_template_id (parser
,
4084 /*template_keyword_p=*/false,
4085 /*check_dependency_p=*/true,
4087 /* If that worked, we're done. */
4088 if (cp_parser_parse_definitely (parser
))
4091 /* Peek at the next token. (Changes in the token buffer may
4092 have invalidated the pointer obtained above.) */
4093 token
= cp_lexer_peek_token (parser
->lexer
);
4095 switch (token
->type
)
4098 return cp_parser_identifier (parser
);
4101 if (token
->keyword
== RID_OPERATOR
)
4102 return cp_parser_operator_function_id (parser
);
4106 cp_parser_error (parser
, "expected id-expression");
4107 return error_mark_node
;
4111 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4112 /*check_dependency_p=*/true,
4117 /* Parse an unqualified-id.
4121 operator-function-id
4122 conversion-function-id
4126 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4127 keyword, in a construct like `A::template ...'.
4129 Returns a representation of unqualified-id. For the `identifier'
4130 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4131 production a BIT_NOT_EXPR is returned; the operand of the
4132 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4133 other productions, see the documentation accompanying the
4134 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4135 names are looked up in uninstantiated templates. If DECLARATOR_P
4136 is true, the unqualified-id is appearing as part of a declarator,
4137 rather than as part of an expression. */
4140 cp_parser_unqualified_id (cp_parser
* parser
,
4141 bool template_keyword_p
,
4142 bool check_dependency_p
,
4148 /* Peek at the next token. */
4149 token
= cp_lexer_peek_token (parser
->lexer
);
4151 switch (token
->type
)
4157 /* We don't know yet whether or not this will be a
4159 cp_parser_parse_tentatively (parser
);
4160 /* Try a template-id. */
4161 id
= cp_parser_template_id (parser
, template_keyword_p
,
4164 /* If it worked, we're done. */
4165 if (cp_parser_parse_definitely (parser
))
4167 /* Otherwise, it's an ordinary identifier. */
4168 return cp_parser_identifier (parser
);
4171 case CPP_TEMPLATE_ID
:
4172 return cp_parser_template_id (parser
, template_keyword_p
,
4179 tree qualifying_scope
;
4184 /* Consume the `~' token. */
4185 cp_lexer_consume_token (parser
->lexer
);
4186 /* Parse the class-name. The standard, as written, seems to
4189 template <typename T> struct S { ~S (); };
4190 template <typename T> S<T>::~S() {}
4192 is invalid, since `~' must be followed by a class-name, but
4193 `S<T>' is dependent, and so not known to be a class.
4194 That's not right; we need to look in uninstantiated
4195 templates. A further complication arises from:
4197 template <typename T> void f(T t) {
4201 Here, it is not possible to look up `T' in the scope of `T'
4202 itself. We must look in both the current scope, and the
4203 scope of the containing complete expression.
4205 Yet another issue is:
4214 The standard does not seem to say that the `S' in `~S'
4215 should refer to the type `S' and not the data member
4218 /* DR 244 says that we look up the name after the "~" in the
4219 same scope as we looked up the qualifying name. That idea
4220 isn't fully worked out; it's more complicated than that. */
4221 scope
= parser
->scope
;
4222 object_scope
= parser
->object_scope
;
4223 qualifying_scope
= parser
->qualifying_scope
;
4225 /* Check for invalid scopes. */
4226 if (scope
== error_mark_node
)
4228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4229 cp_lexer_consume_token (parser
->lexer
);
4230 return error_mark_node
;
4232 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4234 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4235 error_at (token
->location
,
4236 "scope %qT before %<~%> is not a class-name",
4238 cp_parser_simulate_error (parser
);
4239 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4240 cp_lexer_consume_token (parser
->lexer
);
4241 return error_mark_node
;
4243 gcc_assert (!scope
|| TYPE_P (scope
));
4245 /* If the name is of the form "X::~X" it's OK even if X is a
4247 token
= cp_lexer_peek_token (parser
->lexer
);
4249 && token
->type
== CPP_NAME
4250 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4252 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4253 || constructor_name_p (token
->u
.value
, scope
)))
4255 cp_lexer_consume_token (parser
->lexer
);
4256 return build_nt (BIT_NOT_EXPR
, scope
);
4259 /* If there was an explicit qualification (S::~T), first look
4260 in the scope given by the qualification (i.e., S).
4262 Note: in the calls to cp_parser_class_name below we pass
4263 typename_type so that lookup finds the injected-class-name
4264 rather than the constructor. */
4266 type_decl
= NULL_TREE
;
4269 cp_parser_parse_tentatively (parser
);
4270 type_decl
= cp_parser_class_name (parser
,
4271 /*typename_keyword_p=*/false,
4272 /*template_keyword_p=*/false,
4274 /*check_dependency=*/false,
4275 /*class_head_p=*/false,
4277 if (cp_parser_parse_definitely (parser
))
4280 /* In "N::S::~S", look in "N" as well. */
4281 if (!done
&& scope
&& qualifying_scope
)
4283 cp_parser_parse_tentatively (parser
);
4284 parser
->scope
= qualifying_scope
;
4285 parser
->object_scope
= NULL_TREE
;
4286 parser
->qualifying_scope
= NULL_TREE
;
4288 = cp_parser_class_name (parser
,
4289 /*typename_keyword_p=*/false,
4290 /*template_keyword_p=*/false,
4292 /*check_dependency=*/false,
4293 /*class_head_p=*/false,
4295 if (cp_parser_parse_definitely (parser
))
4298 /* In "p->S::~T", look in the scope given by "*p" as well. */
4299 else if (!done
&& object_scope
)
4301 cp_parser_parse_tentatively (parser
);
4302 parser
->scope
= object_scope
;
4303 parser
->object_scope
= NULL_TREE
;
4304 parser
->qualifying_scope
= NULL_TREE
;
4306 = cp_parser_class_name (parser
,
4307 /*typename_keyword_p=*/false,
4308 /*template_keyword_p=*/false,
4310 /*check_dependency=*/false,
4311 /*class_head_p=*/false,
4313 if (cp_parser_parse_definitely (parser
))
4316 /* Look in the surrounding context. */
4319 parser
->scope
= NULL_TREE
;
4320 parser
->object_scope
= NULL_TREE
;
4321 parser
->qualifying_scope
= NULL_TREE
;
4322 if (processing_template_decl
)
4323 cp_parser_parse_tentatively (parser
);
4325 = cp_parser_class_name (parser
,
4326 /*typename_keyword_p=*/false,
4327 /*template_keyword_p=*/false,
4329 /*check_dependency=*/false,
4330 /*class_head_p=*/false,
4332 if (processing_template_decl
4333 && ! cp_parser_parse_definitely (parser
))
4335 /* We couldn't find a type with this name, so just accept
4336 it and check for a match at instantiation time. */
4337 type_decl
= cp_parser_identifier (parser
);
4338 if (type_decl
!= error_mark_node
)
4339 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
4343 /* If an error occurred, assume that the name of the
4344 destructor is the same as the name of the qualifying
4345 class. That allows us to keep parsing after running
4346 into ill-formed destructor names. */
4347 if (type_decl
== error_mark_node
&& scope
)
4348 return build_nt (BIT_NOT_EXPR
, scope
);
4349 else if (type_decl
== error_mark_node
)
4350 return error_mark_node
;
4352 /* Check that destructor name and scope match. */
4353 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
4355 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4356 error_at (token
->location
,
4357 "declaration of %<~%T%> as member of %qT",
4359 cp_parser_simulate_error (parser
);
4360 return error_mark_node
;
4365 A typedef-name that names a class shall not be used as the
4366 identifier in the declarator for a destructor declaration. */
4368 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
4369 && !DECL_SELF_REFERENCE_P (type_decl
)
4370 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4371 error_at (token
->location
,
4372 "typedef-name %qD used as destructor declarator",
4375 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
4379 if (token
->keyword
== RID_OPERATOR
)
4383 /* This could be a template-id, so we try that first. */
4384 cp_parser_parse_tentatively (parser
);
4385 /* Try a template-id. */
4386 id
= cp_parser_template_id (parser
, template_keyword_p
,
4387 /*check_dependency_p=*/true,
4389 /* If that worked, we're done. */
4390 if (cp_parser_parse_definitely (parser
))
4392 /* We still don't know whether we're looking at an
4393 operator-function-id or a conversion-function-id. */
4394 cp_parser_parse_tentatively (parser
);
4395 /* Try an operator-function-id. */
4396 id
= cp_parser_operator_function_id (parser
);
4397 /* If that didn't work, try a conversion-function-id. */
4398 if (!cp_parser_parse_definitely (parser
))
4399 id
= cp_parser_conversion_function_id (parser
);
4408 cp_parser_error (parser
, "expected unqualified-id");
4409 return error_mark_node
;
4413 /* Parse an (optional) nested-name-specifier.
4415 nested-name-specifier: [C++98]
4416 class-or-namespace-name :: nested-name-specifier [opt]
4417 class-or-namespace-name :: template nested-name-specifier [opt]
4419 nested-name-specifier: [C++0x]
4422 nested-name-specifier identifier ::
4423 nested-name-specifier template [opt] simple-template-id ::
4425 PARSER->SCOPE should be set appropriately before this function is
4426 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4427 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4430 Sets PARSER->SCOPE to the class (TYPE) or namespace
4431 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4432 it unchanged if there is no nested-name-specifier. Returns the new
4433 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4435 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4436 part of a declaration and/or decl-specifier. */
4439 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
4440 bool typename_keyword_p
,
4441 bool check_dependency_p
,
4443 bool is_declaration
)
4445 bool success
= false;
4446 cp_token_position start
= 0;
4449 /* Remember where the nested-name-specifier starts. */
4450 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4452 start
= cp_lexer_token_position (parser
->lexer
, false);
4453 push_deferring_access_checks (dk_deferred
);
4460 tree saved_qualifying_scope
;
4461 bool template_keyword_p
;
4463 /* Spot cases that cannot be the beginning of a
4464 nested-name-specifier. */
4465 token
= cp_lexer_peek_token (parser
->lexer
);
4467 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4468 the already parsed nested-name-specifier. */
4469 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
4471 /* Grab the nested-name-specifier and continue the loop. */
4472 cp_parser_pre_parsed_nested_name_specifier (parser
);
4473 /* If we originally encountered this nested-name-specifier
4474 with IS_DECLARATION set to false, we will not have
4475 resolved TYPENAME_TYPEs, so we must do so here. */
4477 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4479 new_scope
= resolve_typename_type (parser
->scope
,
4480 /*only_current_p=*/false);
4481 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
4482 parser
->scope
= new_scope
;
4488 /* Spot cases that cannot be the beginning of a
4489 nested-name-specifier. On the second and subsequent times
4490 through the loop, we look for the `template' keyword. */
4491 if (success
&& token
->keyword
== RID_TEMPLATE
)
4493 /* A template-id can start a nested-name-specifier. */
4494 else if (token
->type
== CPP_TEMPLATE_ID
)
4498 /* If the next token is not an identifier, then it is
4499 definitely not a type-name or namespace-name. */
4500 if (token
->type
!= CPP_NAME
)
4502 /* If the following token is neither a `<' (to begin a
4503 template-id), nor a `::', then we are not looking at a
4504 nested-name-specifier. */
4505 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
4506 if (token
->type
!= CPP_SCOPE
4507 && !cp_parser_nth_token_starts_template_argument_list_p
4512 /* The nested-name-specifier is optional, so we parse
4514 cp_parser_parse_tentatively (parser
);
4516 /* Look for the optional `template' keyword, if this isn't the
4517 first time through the loop. */
4519 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
4521 template_keyword_p
= false;
4523 /* Save the old scope since the name lookup we are about to do
4524 might destroy it. */
4525 old_scope
= parser
->scope
;
4526 saved_qualifying_scope
= parser
->qualifying_scope
;
4527 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4528 look up names in "X<T>::I" in order to determine that "Y" is
4529 a template. So, if we have a typename at this point, we make
4530 an effort to look through it. */
4532 && !typename_keyword_p
4534 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4535 parser
->scope
= resolve_typename_type (parser
->scope
,
4536 /*only_current_p=*/false);
4537 /* Parse the qualifying entity. */
4539 = cp_parser_qualifying_entity (parser
,
4545 /* Look for the `::' token. */
4546 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
4548 /* If we found what we wanted, we keep going; otherwise, we're
4550 if (!cp_parser_parse_definitely (parser
))
4552 bool error_p
= false;
4554 /* Restore the OLD_SCOPE since it was valid before the
4555 failed attempt at finding the last
4556 class-or-namespace-name. */
4557 parser
->scope
= old_scope
;
4558 parser
->qualifying_scope
= saved_qualifying_scope
;
4559 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4561 /* If the next token is an identifier, and the one after
4562 that is a `::', then any valid interpretation would have
4563 found a class-or-namespace-name. */
4564 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4565 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4567 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
4570 token
= cp_lexer_consume_token (parser
->lexer
);
4573 if (!token
->ambiguous_p
)
4576 tree ambiguous_decls
;
4578 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
4580 /*is_template=*/false,
4581 /*is_namespace=*/false,
4582 /*check_dependency=*/true,
4585 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
4586 error_at (token
->location
,
4587 "%qD used without template parameters",
4589 else if (ambiguous_decls
)
4591 error_at (token
->location
,
4592 "reference to %qD is ambiguous",
4594 print_candidates (ambiguous_decls
);
4595 decl
= error_mark_node
;
4599 if (cxx_dialect
!= cxx98
)
4600 cp_parser_name_lookup_error
4601 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
4604 cp_parser_name_lookup_error
4605 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
4609 parser
->scope
= error_mark_node
;
4611 /* Treat this as a successful nested-name-specifier
4616 If the name found is not a class-name (clause
4617 _class_) or namespace-name (_namespace.def_), the
4618 program is ill-formed. */
4621 cp_lexer_consume_token (parser
->lexer
);
4625 /* We've found one valid nested-name-specifier. */
4627 /* Name lookup always gives us a DECL. */
4628 if (TREE_CODE (new_scope
) == TYPE_DECL
)
4629 new_scope
= TREE_TYPE (new_scope
);
4630 /* Uses of "template" must be followed by actual templates. */
4631 if (template_keyword_p
4632 && !(CLASS_TYPE_P (new_scope
)
4633 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
4634 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
4635 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
4636 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
4637 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
4638 == TEMPLATE_ID_EXPR
)))
4639 permerror (input_location
, TYPE_P (new_scope
)
4640 ? "%qT is not a template"
4641 : "%qD is not a template",
4643 /* If it is a class scope, try to complete it; we are about to
4644 be looking up names inside the class. */
4645 if (TYPE_P (new_scope
)
4646 /* Since checking types for dependency can be expensive,
4647 avoid doing it if the type is already complete. */
4648 && !COMPLETE_TYPE_P (new_scope
)
4649 /* Do not try to complete dependent types. */
4650 && !dependent_type_p (new_scope
))
4652 new_scope
= complete_type (new_scope
);
4653 /* If it is a typedef to current class, use the current
4654 class instead, as the typedef won't have any names inside
4656 if (!COMPLETE_TYPE_P (new_scope
)
4657 && currently_open_class (new_scope
))
4658 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
4660 /* Make sure we look in the right scope the next time through
4662 parser
->scope
= new_scope
;
4665 /* If parsing tentatively, replace the sequence of tokens that makes
4666 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4667 token. That way, should we re-parse the token stream, we will
4668 not have to repeat the effort required to do the parse, nor will
4669 we issue duplicate error messages. */
4670 if (success
&& start
)
4674 token
= cp_lexer_token_at (parser
->lexer
, start
);
4675 /* Reset the contents of the START token. */
4676 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
4677 /* Retrieve any deferred checks. Do not pop this access checks yet
4678 so the memory will not be reclaimed during token replacing below. */
4679 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
4680 token
->u
.tree_check_value
->value
= parser
->scope
;
4681 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
4682 token
->u
.tree_check_value
->qualifying_scope
=
4683 parser
->qualifying_scope
;
4684 token
->keyword
= RID_MAX
;
4686 /* Purge all subsequent tokens. */
4687 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4691 pop_to_parent_deferring_access_checks ();
4693 return success
? parser
->scope
: NULL_TREE
;
4696 /* Parse a nested-name-specifier. See
4697 cp_parser_nested_name_specifier_opt for details. This function
4698 behaves identically, except that it will an issue an error if no
4699 nested-name-specifier is present. */
4702 cp_parser_nested_name_specifier (cp_parser
*parser
,
4703 bool typename_keyword_p
,
4704 bool check_dependency_p
,
4706 bool is_declaration
)
4710 /* Look for the nested-name-specifier. */
4711 scope
= cp_parser_nested_name_specifier_opt (parser
,
4716 /* If it was not present, issue an error message. */
4719 cp_parser_error (parser
, "expected nested-name-specifier");
4720 parser
->scope
= NULL_TREE
;
4726 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4727 this is either a class-name or a namespace-name (which corresponds
4728 to the class-or-namespace-name production in the grammar). For
4729 C++0x, it can also be a type-name that refers to an enumeration
4732 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4733 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4734 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4735 TYPE_P is TRUE iff the next name should be taken as a class-name,
4736 even the same name is declared to be another entity in the same
4739 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4740 specified by the class-or-namespace-name. If neither is found the
4741 ERROR_MARK_NODE is returned. */
4744 cp_parser_qualifying_entity (cp_parser
*parser
,
4745 bool typename_keyword_p
,
4746 bool template_keyword_p
,
4747 bool check_dependency_p
,
4749 bool is_declaration
)
4752 tree saved_qualifying_scope
;
4753 tree saved_object_scope
;
4756 bool successful_parse_p
;
4758 /* Before we try to parse the class-name, we must save away the
4759 current PARSER->SCOPE since cp_parser_class_name will destroy
4761 saved_scope
= parser
->scope
;
4762 saved_qualifying_scope
= parser
->qualifying_scope
;
4763 saved_object_scope
= parser
->object_scope
;
4764 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4765 there is no need to look for a namespace-name. */
4766 only_class_p
= template_keyword_p
4767 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
4769 cp_parser_parse_tentatively (parser
);
4770 scope
= cp_parser_class_name (parser
,
4773 type_p
? class_type
: none_type
,
4775 /*class_head_p=*/false,
4777 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
4778 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4780 && cxx_dialect
!= cxx98
4781 && !successful_parse_p
)
4783 /* Restore the saved scope. */
4784 parser
->scope
= saved_scope
;
4785 parser
->qualifying_scope
= saved_qualifying_scope
;
4786 parser
->object_scope
= saved_object_scope
;
4788 /* Parse tentatively. */
4789 cp_parser_parse_tentatively (parser
);
4791 /* Parse a typedef-name or enum-name. */
4792 scope
= cp_parser_nonclass_name (parser
);
4794 /* "If the name found does not designate a namespace or a class,
4795 enumeration, or dependent type, the program is ill-formed."
4797 We cover classes and dependent types above and namespaces below,
4798 so this code is only looking for enums. */
4799 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
4800 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
4801 cp_parser_simulate_error (parser
);
4803 successful_parse_p
= cp_parser_parse_definitely (parser
);
4805 /* If that didn't work, try for a namespace-name. */
4806 if (!only_class_p
&& !successful_parse_p
)
4808 /* Restore the saved scope. */
4809 parser
->scope
= saved_scope
;
4810 parser
->qualifying_scope
= saved_qualifying_scope
;
4811 parser
->object_scope
= saved_object_scope
;
4812 /* If we are not looking at an identifier followed by the scope
4813 resolution operator, then this is not part of a
4814 nested-name-specifier. (Note that this function is only used
4815 to parse the components of a nested-name-specifier.) */
4816 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
4817 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
4818 return error_mark_node
;
4819 scope
= cp_parser_namespace_name (parser
);
4825 /* Parse a postfix-expression.
4829 postfix-expression [ expression ]
4830 postfix-expression ( expression-list [opt] )
4831 simple-type-specifier ( expression-list [opt] )
4832 typename :: [opt] nested-name-specifier identifier
4833 ( expression-list [opt] )
4834 typename :: [opt] nested-name-specifier template [opt] template-id
4835 ( expression-list [opt] )
4836 postfix-expression . template [opt] id-expression
4837 postfix-expression -> template [opt] id-expression
4838 postfix-expression . pseudo-destructor-name
4839 postfix-expression -> pseudo-destructor-name
4840 postfix-expression ++
4841 postfix-expression --
4842 dynamic_cast < type-id > ( expression )
4843 static_cast < type-id > ( expression )
4844 reinterpret_cast < type-id > ( expression )
4845 const_cast < type-id > ( expression )
4846 typeid ( expression )
4852 ( type-id ) { initializer-list , [opt] }
4854 This extension is a GNU version of the C99 compound-literal
4855 construct. (The C99 grammar uses `type-name' instead of `type-id',
4856 but they are essentially the same concept.)
4858 If ADDRESS_P is true, the postfix expression is the operand of the
4859 `&' operator. CAST_P is true if this expression is the target of a
4862 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4863 class member access expressions [expr.ref].
4865 Returns a representation of the expression. */
4868 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
4869 bool member_access_only_p
,
4870 cp_id_kind
* pidk_return
)
4874 cp_id_kind idk
= CP_ID_KIND_NONE
;
4875 tree postfix_expression
= NULL_TREE
;
4876 bool is_member_access
= false;
4878 /* Peek at the next token. */
4879 token
= cp_lexer_peek_token (parser
->lexer
);
4880 /* Some of the productions are determined by keywords. */
4881 keyword
= token
->keyword
;
4891 const char *saved_message
;
4893 /* All of these can be handled in the same way from the point
4894 of view of parsing. Begin by consuming the token
4895 identifying the cast. */
4896 cp_lexer_consume_token (parser
->lexer
);
4898 /* New types cannot be defined in the cast. */
4899 saved_message
= parser
->type_definition_forbidden_message
;
4900 parser
->type_definition_forbidden_message
4901 = G_("types may not be defined in casts");
4903 /* Look for the opening `<'. */
4904 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
4905 /* Parse the type to which we are casting. */
4906 type
= cp_parser_type_id (parser
);
4907 /* Look for the closing `>'. */
4908 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
4909 /* Restore the old message. */
4910 parser
->type_definition_forbidden_message
= saved_message
;
4912 /* And the expression which is being cast. */
4913 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4914 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
4915 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4917 /* Only type conversions to integral or enumeration types
4918 can be used in constant-expressions. */
4919 if (!cast_valid_in_integral_constant_expression_p (type
)
4920 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
4921 return error_mark_node
;
4927 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
4931 = build_static_cast (type
, expression
, tf_warning_or_error
);
4935 = build_reinterpret_cast (type
, expression
,
4936 tf_warning_or_error
);
4940 = build_const_cast (type
, expression
, tf_warning_or_error
);
4951 const char *saved_message
;
4952 bool saved_in_type_id_in_expr_p
;
4954 /* Consume the `typeid' token. */
4955 cp_lexer_consume_token (parser
->lexer
);
4956 /* Look for the `(' token. */
4957 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4958 /* Types cannot be defined in a `typeid' expression. */
4959 saved_message
= parser
->type_definition_forbidden_message
;
4960 parser
->type_definition_forbidden_message
4961 = G_("types may not be defined in a %<typeid%> expression");
4962 /* We can't be sure yet whether we're looking at a type-id or an
4964 cp_parser_parse_tentatively (parser
);
4965 /* Try a type-id first. */
4966 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
4967 parser
->in_type_id_in_expr_p
= true;
4968 type
= cp_parser_type_id (parser
);
4969 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
4970 /* Look for the `)' token. Otherwise, we can't be sure that
4971 we're not looking at an expression: consider `typeid (int
4972 (3))', for example. */
4973 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4974 /* If all went well, simply lookup the type-id. */
4975 if (cp_parser_parse_definitely (parser
))
4976 postfix_expression
= get_typeid (type
);
4977 /* Otherwise, fall back to the expression variant. */
4982 /* Look for an expression. */
4983 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
4984 /* Compute its typeid. */
4985 postfix_expression
= build_typeid (expression
);
4986 /* Look for the `)' token. */
4987 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4989 /* Restore the saved message. */
4990 parser
->type_definition_forbidden_message
= saved_message
;
4991 /* `typeid' may not appear in an integral constant expression. */
4992 if (cp_parser_non_integral_constant_expression(parser
, NIC_TYPEID
))
4993 return error_mark_node
;
5000 /* The syntax permitted here is the same permitted for an
5001 elaborated-type-specifier. */
5002 type
= cp_parser_elaborated_type_specifier (parser
,
5003 /*is_friend=*/false,
5004 /*is_declaration=*/false);
5005 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5013 /* If the next thing is a simple-type-specifier, we may be
5014 looking at a functional cast. We could also be looking at
5015 an id-expression. So, we try the functional cast, and if
5016 that doesn't work we fall back to the primary-expression. */
5017 cp_parser_parse_tentatively (parser
);
5018 /* Look for the simple-type-specifier. */
5019 type
= cp_parser_simple_type_specifier (parser
,
5020 /*decl_specs=*/NULL
,
5021 CP_PARSER_FLAGS_NONE
);
5022 /* Parse the cast itself. */
5023 if (!cp_parser_error_occurred (parser
))
5025 = cp_parser_functional_cast (parser
, type
);
5026 /* If that worked, we're done. */
5027 if (cp_parser_parse_definitely (parser
))
5030 /* If the functional-cast didn't work out, try a
5031 compound-literal. */
5032 if (cp_parser_allow_gnu_extensions_p (parser
)
5033 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5035 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
5036 bool saved_in_type_id_in_expr_p
;
5038 cp_parser_parse_tentatively (parser
);
5039 /* Consume the `('. */
5040 cp_lexer_consume_token (parser
->lexer
);
5041 /* Parse the type. */
5042 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5043 parser
->in_type_id_in_expr_p
= true;
5044 type
= cp_parser_type_id (parser
);
5045 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5046 /* Look for the `)'. */
5047 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5048 /* Look for the `{'. */
5049 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
5050 /* If things aren't going well, there's no need to
5052 if (!cp_parser_error_occurred (parser
))
5054 bool non_constant_p
;
5055 /* Parse the initializer-list. */
5057 = cp_parser_initializer_list (parser
, &non_constant_p
);
5058 /* Allow a trailing `,'. */
5059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
5060 cp_lexer_consume_token (parser
->lexer
);
5061 /* Look for the final `}'. */
5062 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
5064 /* If that worked, we're definitely looking at a
5065 compound-literal expression. */
5066 if (cp_parser_parse_definitely (parser
))
5068 /* Warn the user that a compound literal is not
5069 allowed in standard C++. */
5070 pedwarn (input_location
, OPT_pedantic
, "ISO C++ forbids compound-literals");
5071 /* For simplicity, we disallow compound literals in
5072 constant-expressions. We could
5073 allow compound literals of integer type, whose
5074 initializer was a constant, in constant
5075 expressions. Permitting that usage, as a further
5076 extension, would not change the meaning of any
5077 currently accepted programs. (Of course, as
5078 compound literals are not part of ISO C++, the
5079 standard has nothing to say.) */
5080 if (cp_parser_non_integral_constant_expression (parser
,
5083 postfix_expression
= error_mark_node
;
5086 /* Form the representation of the compound-literal. */
5088 = (finish_compound_literal
5089 (type
, build_constructor (init_list_type_node
,
5090 initializer_list
)));
5095 /* It must be a primary-expression. */
5097 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5098 /*template_arg_p=*/false,
5104 /* Keep looping until the postfix-expression is complete. */
5107 if (idk
== CP_ID_KIND_UNQUALIFIED
5108 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
5109 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
5110 /* It is not a Koenig lookup function call. */
5112 = unqualified_name_lookup_error (postfix_expression
);
5114 /* Peek at the next token. */
5115 token
= cp_lexer_peek_token (parser
->lexer
);
5117 switch (token
->type
)
5119 case CPP_OPEN_SQUARE
:
5121 = cp_parser_postfix_open_square_expression (parser
,
5124 idk
= CP_ID_KIND_NONE
;
5125 is_member_access
= false;
5128 case CPP_OPEN_PAREN
:
5129 /* postfix-expression ( expression-list [opt] ) */
5132 bool is_builtin_constant_p
;
5133 bool saved_integral_constant_expression_p
= false;
5134 bool saved_non_integral_constant_expression_p
= false;
5137 is_member_access
= false;
5139 is_builtin_constant_p
5140 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
5141 if (is_builtin_constant_p
)
5143 /* The whole point of __builtin_constant_p is to allow
5144 non-constant expressions to appear as arguments. */
5145 saved_integral_constant_expression_p
5146 = parser
->integral_constant_expression_p
;
5147 saved_non_integral_constant_expression_p
5148 = parser
->non_integral_constant_expression_p
;
5149 parser
->integral_constant_expression_p
= false;
5151 args
= (cp_parser_parenthesized_expression_list
5153 /*cast_p=*/false, /*allow_expansion_p=*/true,
5154 /*non_constant_p=*/NULL
));
5155 if (is_builtin_constant_p
)
5157 parser
->integral_constant_expression_p
5158 = saved_integral_constant_expression_p
;
5159 parser
->non_integral_constant_expression_p
5160 = saved_non_integral_constant_expression_p
;
5165 postfix_expression
= error_mark_node
;
5169 /* Function calls are not permitted in
5170 constant-expressions. */
5171 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
5172 && cp_parser_non_integral_constant_expression (parser
,
5175 postfix_expression
= error_mark_node
;
5176 release_tree_vector (args
);
5181 if (idk
== CP_ID_KIND_UNQUALIFIED
5182 || idk
== CP_ID_KIND_TEMPLATE_ID
)
5184 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
5186 if (!VEC_empty (tree
, args
))
5189 if (!any_type_dependent_arguments_p (args
))
5191 = perform_koenig_lookup (postfix_expression
, args
,
5192 /*include_std=*/false);
5196 = unqualified_fn_lookup_error (postfix_expression
);
5198 /* We do not perform argument-dependent lookup if
5199 normal lookup finds a non-function, in accordance
5200 with the expected resolution of DR 218. */
5201 else if (!VEC_empty (tree
, args
)
5202 && is_overloaded_fn (postfix_expression
))
5204 tree fn
= get_first_fn (postfix_expression
);
5205 fn
= STRIP_TEMPLATE (fn
);
5207 /* Do not do argument dependent lookup if regular
5208 lookup finds a member function or a block-scope
5209 function declaration. [basic.lookup.argdep]/3 */
5210 if (!DECL_FUNCTION_MEMBER_P (fn
)
5211 && !DECL_LOCAL_FUNCTION_P (fn
))
5214 if (!any_type_dependent_arguments_p (args
))
5216 = perform_koenig_lookup (postfix_expression
, args
,
5217 /*include_std=*/false);
5222 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
5224 tree instance
= TREE_OPERAND (postfix_expression
, 0);
5225 tree fn
= TREE_OPERAND (postfix_expression
, 1);
5227 if (processing_template_decl
5228 && (type_dependent_expression_p (instance
)
5229 || (!BASELINK_P (fn
)
5230 && TREE_CODE (fn
) != FIELD_DECL
)
5231 || type_dependent_expression_p (fn
)
5232 || any_type_dependent_arguments_p (args
)))
5235 = build_nt_call_vec (postfix_expression
, args
);
5236 release_tree_vector (args
);
5240 if (BASELINK_P (fn
))
5243 = (build_new_method_call
5244 (instance
, fn
, &args
, NULL_TREE
,
5245 (idk
== CP_ID_KIND_QUALIFIED
5246 ? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
),
5248 tf_warning_or_error
));
5252 = finish_call_expr (postfix_expression
, &args
,
5253 /*disallow_virtual=*/false,
5255 tf_warning_or_error
);
5257 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
5258 || TREE_CODE (postfix_expression
) == MEMBER_REF
5259 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
5260 postfix_expression
= (build_offset_ref_call_from_tree
5261 (postfix_expression
, &args
));
5262 else if (idk
== CP_ID_KIND_QUALIFIED
)
5263 /* A call to a static class member, or a namespace-scope
5266 = finish_call_expr (postfix_expression
, &args
,
5267 /*disallow_virtual=*/true,
5269 tf_warning_or_error
);
5271 /* All other function calls. */
5273 = finish_call_expr (postfix_expression
, &args
,
5274 /*disallow_virtual=*/false,
5276 tf_warning_or_error
);
5278 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5279 idk
= CP_ID_KIND_NONE
;
5281 release_tree_vector (args
);
5287 /* postfix-expression . template [opt] id-expression
5288 postfix-expression . pseudo-destructor-name
5289 postfix-expression -> template [opt] id-expression
5290 postfix-expression -> pseudo-destructor-name */
5292 /* Consume the `.' or `->' operator. */
5293 cp_lexer_consume_token (parser
->lexer
);
5296 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
5301 is_member_access
= true;
5305 /* postfix-expression ++ */
5306 /* Consume the `++' token. */
5307 cp_lexer_consume_token (parser
->lexer
);
5308 /* Generate a representation for the complete expression. */
5310 = finish_increment_expr (postfix_expression
,
5311 POSTINCREMENT_EXPR
);
5312 /* Increments may not appear in constant-expressions. */
5313 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
5314 postfix_expression
= error_mark_node
;
5315 idk
= CP_ID_KIND_NONE
;
5316 is_member_access
= false;
5319 case CPP_MINUS_MINUS
:
5320 /* postfix-expression -- */
5321 /* Consume the `--' token. */
5322 cp_lexer_consume_token (parser
->lexer
);
5323 /* Generate a representation for the complete expression. */
5325 = finish_increment_expr (postfix_expression
,
5326 POSTDECREMENT_EXPR
);
5327 /* Decrements may not appear in constant-expressions. */
5328 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
5329 postfix_expression
= error_mark_node
;
5330 idk
= CP_ID_KIND_NONE
;
5331 is_member_access
= false;
5335 if (pidk_return
!= NULL
)
5336 * pidk_return
= idk
;
5337 if (member_access_only_p
)
5338 return is_member_access
? postfix_expression
: error_mark_node
;
5340 return postfix_expression
;
5344 /* We should never get here. */
5346 return error_mark_node
;
5349 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5350 by cp_parser_builtin_offsetof. We're looking for
5352 postfix-expression [ expression ]
5354 FOR_OFFSETOF is set if we're being called in that context, which
5355 changes how we deal with integer constant expressions. */
5358 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
5359 tree postfix_expression
,
5364 /* Consume the `[' token. */
5365 cp_lexer_consume_token (parser
->lexer
);
5367 /* Parse the index expression. */
5368 /* ??? For offsetof, there is a question of what to allow here. If
5369 offsetof is not being used in an integral constant expression context,
5370 then we *could* get the right answer by computing the value at runtime.
5371 If we are in an integral constant expression context, then we might
5372 could accept any constant expression; hard to say without analysis.
5373 Rather than open the barn door too wide right away, allow only integer
5374 constant expressions here. */
5376 index
= cp_parser_constant_expression (parser
, false, NULL
);
5378 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5380 /* Look for the closing `]'. */
5381 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
5383 /* Build the ARRAY_REF. */
5384 postfix_expression
= grok_array_decl (postfix_expression
, index
);
5386 /* When not doing offsetof, array references are not permitted in
5387 constant-expressions. */
5389 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
5390 postfix_expression
= error_mark_node
;
5392 return postfix_expression
;
5395 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5396 by cp_parser_builtin_offsetof. We're looking for
5398 postfix-expression . template [opt] id-expression
5399 postfix-expression . pseudo-destructor-name
5400 postfix-expression -> template [opt] id-expression
5401 postfix-expression -> pseudo-destructor-name
5403 FOR_OFFSETOF is set if we're being called in that context. That sorta
5404 limits what of the above we'll actually accept, but nevermind.
5405 TOKEN_TYPE is the "." or "->" token, which will already have been
5406 removed from the stream. */
5409 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
5410 enum cpp_ttype token_type
,
5411 tree postfix_expression
,
5412 bool for_offsetof
, cp_id_kind
*idk
,
5413 location_t location
)
5417 bool pseudo_destructor_p
;
5418 tree scope
= NULL_TREE
;
5420 /* If this is a `->' operator, dereference the pointer. */
5421 if (token_type
== CPP_DEREF
)
5422 postfix_expression
= build_x_arrow (postfix_expression
);
5423 /* Check to see whether or not the expression is type-dependent. */
5424 dependent_p
= type_dependent_expression_p (postfix_expression
);
5425 /* The identifier following the `->' or `.' is not qualified. */
5426 parser
->scope
= NULL_TREE
;
5427 parser
->qualifying_scope
= NULL_TREE
;
5428 parser
->object_scope
= NULL_TREE
;
5429 *idk
= CP_ID_KIND_NONE
;
5431 /* Enter the scope corresponding to the type of the object
5432 given by the POSTFIX_EXPRESSION. */
5433 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
5435 scope
= TREE_TYPE (postfix_expression
);
5436 /* According to the standard, no expression should ever have
5437 reference type. Unfortunately, we do not currently match
5438 the standard in this respect in that our internal representation
5439 of an expression may have reference type even when the standard
5440 says it does not. Therefore, we have to manually obtain the
5441 underlying type here. */
5442 scope
= non_reference (scope
);
5443 /* The type of the POSTFIX_EXPRESSION must be complete. */
5444 if (scope
== unknown_type_node
)
5446 error_at (location
, "%qE does not have class type",
5447 postfix_expression
);
5451 scope
= complete_type_or_else (scope
, NULL_TREE
);
5452 /* Let the name lookup machinery know that we are processing a
5453 class member access expression. */
5454 parser
->context
->object_type
= scope
;
5455 /* If something went wrong, we want to be able to discern that case,
5456 as opposed to the case where there was no SCOPE due to the type
5457 of expression being dependent. */
5459 scope
= error_mark_node
;
5460 /* If the SCOPE was erroneous, make the various semantic analysis
5461 functions exit quickly -- and without issuing additional error
5463 if (scope
== error_mark_node
)
5464 postfix_expression
= error_mark_node
;
5467 /* Assume this expression is not a pseudo-destructor access. */
5468 pseudo_destructor_p
= false;
5470 /* If the SCOPE is a scalar type, then, if this is a valid program,
5471 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5472 is type dependent, it can be pseudo-destructor-name or something else.
5473 Try to parse it as pseudo-destructor-name first. */
5474 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
5479 cp_parser_parse_tentatively (parser
);
5480 /* Parse the pseudo-destructor-name. */
5482 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
5484 && (cp_parser_error_occurred (parser
)
5485 || TREE_CODE (type
) != TYPE_DECL
5486 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
5487 cp_parser_abort_tentative_parse (parser
);
5488 else if (cp_parser_parse_definitely (parser
))
5490 pseudo_destructor_p
= true;
5492 = finish_pseudo_destructor_expr (postfix_expression
,
5493 s
, TREE_TYPE (type
));
5497 if (!pseudo_destructor_p
)
5499 /* If the SCOPE is not a scalar type, we are looking at an
5500 ordinary class member access expression, rather than a
5501 pseudo-destructor-name. */
5503 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
5504 /* Parse the id-expression. */
5505 name
= (cp_parser_id_expression
5507 cp_parser_optional_template_keyword (parser
),
5508 /*check_dependency_p=*/true,
5510 /*declarator_p=*/false,
5511 /*optional_p=*/false));
5512 /* In general, build a SCOPE_REF if the member name is qualified.
5513 However, if the name was not dependent and has already been
5514 resolved; there is no need to build the SCOPE_REF. For example;
5516 struct X { void f(); };
5517 template <typename T> void f(T* t) { t->X::f(); }
5519 Even though "t" is dependent, "X::f" is not and has been resolved
5520 to a BASELINK; there is no need to include scope information. */
5522 /* But we do need to remember that there was an explicit scope for
5523 virtual function calls. */
5525 *idk
= CP_ID_KIND_QUALIFIED
;
5527 /* If the name is a template-id that names a type, we will get a
5528 TYPE_DECL here. That is invalid code. */
5529 if (TREE_CODE (name
) == TYPE_DECL
)
5531 error_at (token
->location
, "invalid use of %qD", name
);
5532 postfix_expression
= error_mark_node
;
5536 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
5538 name
= build_qualified_name (/*type=*/NULL_TREE
,
5542 parser
->scope
= NULL_TREE
;
5543 parser
->qualifying_scope
= NULL_TREE
;
5544 parser
->object_scope
= NULL_TREE
;
5546 if (scope
&& name
&& BASELINK_P (name
))
5547 adjust_result_of_qualified_name_lookup
5548 (name
, BINFO_TYPE (BASELINK_ACCESS_BINFO (name
)), scope
);
5550 = finish_class_member_access_expr (postfix_expression
, name
,
5552 tf_warning_or_error
);
5556 /* We no longer need to look up names in the scope of the object on
5557 the left-hand side of the `.' or `->' operator. */
5558 parser
->context
->object_type
= NULL_TREE
;
5560 /* Outside of offsetof, these operators may not appear in
5561 constant-expressions. */
5563 && (cp_parser_non_integral_constant_expression
5564 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
5565 postfix_expression
= error_mark_node
;
5567 return postfix_expression
;
5570 /* Parse a parenthesized expression-list.
5573 assignment-expression
5574 expression-list, assignment-expression
5579 identifier, expression-list
5581 CAST_P is true if this expression is the target of a cast.
5583 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5586 Returns a vector of trees. Each element is a representation of an
5587 assignment-expression. NULL is returned if the ( and or ) are
5588 missing. An empty, but allocated, vector is returned on no
5589 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5590 if we are parsing an attribute list for an attribute that wants a
5591 plain identifier argument, normal_attr for an attribute that wants
5592 an expression, or non_attr if we aren't parsing an attribute list. If
5593 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5594 not all of the expressions in the list were constant. */
5596 static VEC(tree
,gc
) *
5597 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
5598 int is_attribute_list
,
5600 bool allow_expansion_p
,
5601 bool *non_constant_p
)
5603 VEC(tree
,gc
) *expression_list
;
5604 bool fold_expr_p
= is_attribute_list
!= non_attr
;
5605 tree identifier
= NULL_TREE
;
5606 bool saved_greater_than_is_operator_p
;
5608 /* Assume all the expressions will be constant. */
5610 *non_constant_p
= false;
5612 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
5615 expression_list
= make_tree_vector ();
5617 /* Within a parenthesized expression, a `>' token is always
5618 the greater-than operator. */
5619 saved_greater_than_is_operator_p
5620 = parser
->greater_than_is_operator_p
;
5621 parser
->greater_than_is_operator_p
= true;
5623 /* Consume expressions until there are no more. */
5624 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
5629 /* At the beginning of attribute lists, check to see if the
5630 next token is an identifier. */
5631 if (is_attribute_list
== id_attr
5632 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
5636 /* Consume the identifier. */
5637 token
= cp_lexer_consume_token (parser
->lexer
);
5638 /* Save the identifier. */
5639 identifier
= token
->u
.value
;
5643 bool expr_non_constant_p
;
5645 /* Parse the next assignment-expression. */
5646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5648 /* A braced-init-list. */
5649 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
5650 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
5651 if (non_constant_p
&& expr_non_constant_p
)
5652 *non_constant_p
= true;
5654 else if (non_constant_p
)
5656 expr
= (cp_parser_constant_expression
5657 (parser
, /*allow_non_constant_p=*/true,
5658 &expr_non_constant_p
));
5659 if (expr_non_constant_p
)
5660 *non_constant_p
= true;
5663 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
5666 expr
= fold_non_dependent_expr (expr
);
5668 /* If we have an ellipsis, then this is an expression
5670 if (allow_expansion_p
5671 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5673 /* Consume the `...'. */
5674 cp_lexer_consume_token (parser
->lexer
);
5676 /* Build the argument pack. */
5677 expr
= make_pack_expansion (expr
);
5680 /* Add it to the list. We add error_mark_node
5681 expressions to the list, so that we can still tell if
5682 the correct form for a parenthesized expression-list
5683 is found. That gives better errors. */
5684 VEC_safe_push (tree
, gc
, expression_list
, expr
);
5686 if (expr
== error_mark_node
)
5690 /* After the first item, attribute lists look the same as
5691 expression lists. */
5692 is_attribute_list
= non_attr
;
5695 /* If the next token isn't a `,', then we are done. */
5696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
5699 /* Otherwise, consume the `,' and keep going. */
5700 cp_lexer_consume_token (parser
->lexer
);
5703 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
5708 /* We try and resync to an unnested comma, as that will give the
5709 user better diagnostics. */
5710 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
5711 /*recovering=*/true,
5713 /*consume_paren=*/true);
5718 parser
->greater_than_is_operator_p
5719 = saved_greater_than_is_operator_p
;
5724 parser
->greater_than_is_operator_p
5725 = saved_greater_than_is_operator_p
;
5728 VEC_safe_insert (tree
, gc
, expression_list
, 0, identifier
);
5730 return expression_list
;
5733 /* Parse a pseudo-destructor-name.
5735 pseudo-destructor-name:
5736 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5737 :: [opt] nested-name-specifier template template-id :: ~ type-name
5738 :: [opt] nested-name-specifier [opt] ~ type-name
5740 If either of the first two productions is used, sets *SCOPE to the
5741 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5742 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5743 or ERROR_MARK_NODE if the parse fails. */
5746 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
5750 bool nested_name_specifier_p
;
5752 /* Assume that things will not work out. */
5753 *type
= error_mark_node
;
5755 /* Look for the optional `::' operator. */
5756 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
5757 /* Look for the optional nested-name-specifier. */
5758 nested_name_specifier_p
5759 = (cp_parser_nested_name_specifier_opt (parser
,
5760 /*typename_keyword_p=*/false,
5761 /*check_dependency_p=*/true,
5763 /*is_declaration=*/false)
5765 /* Now, if we saw a nested-name-specifier, we might be doing the
5766 second production. */
5767 if (nested_name_specifier_p
5768 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
5770 /* Consume the `template' keyword. */
5771 cp_lexer_consume_token (parser
->lexer
);
5772 /* Parse the template-id. */
5773 cp_parser_template_id (parser
,
5774 /*template_keyword_p=*/true,
5775 /*check_dependency_p=*/false,
5776 /*is_declaration=*/true);
5777 /* Look for the `::' token. */
5778 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5780 /* If the next token is not a `~', then there might be some
5781 additional qualification. */
5782 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
5784 /* At this point, we're looking for "type-name :: ~". The type-name
5785 must not be a class-name, since this is a pseudo-destructor. So,
5786 it must be either an enum-name, or a typedef-name -- both of which
5787 are just identifiers. So, we peek ahead to check that the "::"
5788 and "~" tokens are present; if they are not, then we can avoid
5789 calling type_name. */
5790 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
5791 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
5792 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
5794 cp_parser_error (parser
, "non-scalar type");
5798 /* Look for the type-name. */
5799 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
5800 if (*scope
== error_mark_node
)
5803 /* Look for the `::' token. */
5804 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5809 /* Look for the `~'. */
5810 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
5811 /* Look for the type-name again. We are not responsible for
5812 checking that it matches the first type-name. */
5813 *type
= cp_parser_nonclass_name (parser
);
5816 /* Parse a unary-expression.
5822 unary-operator cast-expression
5823 sizeof unary-expression
5831 __extension__ cast-expression
5832 __alignof__ unary-expression
5833 __alignof__ ( type-id )
5834 __real__ cast-expression
5835 __imag__ cast-expression
5838 ADDRESS_P is true iff the unary-expression is appearing as the
5839 operand of the `&' operator. CAST_P is true if this expression is
5840 the target of a cast.
5842 Returns a representation of the expression. */
5845 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5849 enum tree_code unary_operator
;
5851 /* Peek at the next token. */
5852 token
= cp_lexer_peek_token (parser
->lexer
);
5853 /* Some keywords give away the kind of expression. */
5854 if (token
->type
== CPP_KEYWORD
)
5856 enum rid keyword
= token
->keyword
;
5866 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
5867 /* Consume the token. */
5868 cp_lexer_consume_token (parser
->lexer
);
5869 /* Parse the operand. */
5870 operand
= cp_parser_sizeof_operand (parser
, keyword
);
5872 if (TYPE_P (operand
))
5873 return cxx_sizeof_or_alignof_type (operand
, op
, true);
5875 return cxx_sizeof_or_alignof_expr (operand
, op
, true);
5879 return cp_parser_new_expression (parser
);
5882 return cp_parser_delete_expression (parser
);
5886 /* The saved value of the PEDANTIC flag. */
5890 /* Save away the PEDANTIC flag. */
5891 cp_parser_extension_opt (parser
, &saved_pedantic
);
5892 /* Parse the cast-expression. */
5893 expr
= cp_parser_simple_cast_expression (parser
);
5894 /* Restore the PEDANTIC flag. */
5895 pedantic
= saved_pedantic
;
5905 /* Consume the `__real__' or `__imag__' token. */
5906 cp_lexer_consume_token (parser
->lexer
);
5907 /* Parse the cast-expression. */
5908 expression
= cp_parser_simple_cast_expression (parser
);
5909 /* Create the complete representation. */
5910 return build_x_unary_op ((keyword
== RID_REALPART
5911 ? REALPART_EXPR
: IMAGPART_EXPR
),
5913 tf_warning_or_error
);
5920 const char *saved_message
;
5921 bool saved_integral_constant_expression_p
;
5922 bool saved_non_integral_constant_expression_p
;
5923 bool saved_greater_than_is_operator_p
;
5925 cp_lexer_consume_token (parser
->lexer
);
5926 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5928 saved_message
= parser
->type_definition_forbidden_message
;
5929 parser
->type_definition_forbidden_message
5930 = G_("types may not be defined in %<noexcept%> expressions");
5932 saved_integral_constant_expression_p
5933 = parser
->integral_constant_expression_p
;
5934 saved_non_integral_constant_expression_p
5935 = parser
->non_integral_constant_expression_p
;
5936 parser
->integral_constant_expression_p
= false;
5938 saved_greater_than_is_operator_p
5939 = parser
->greater_than_is_operator_p
;
5940 parser
->greater_than_is_operator_p
= true;
5942 ++cp_unevaluated_operand
;
5943 ++c_inhibit_evaluation_warnings
;
5944 expr
= cp_parser_expression (parser
, false, NULL
);
5945 --c_inhibit_evaluation_warnings
;
5946 --cp_unevaluated_operand
;
5948 parser
->greater_than_is_operator_p
5949 = saved_greater_than_is_operator_p
;
5951 parser
->integral_constant_expression_p
5952 = saved_integral_constant_expression_p
;
5953 parser
->non_integral_constant_expression_p
5954 = saved_non_integral_constant_expression_p
;
5956 parser
->type_definition_forbidden_message
= saved_message
;
5958 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5959 return finish_noexcept_expr (expr
, tf_warning_or_error
);
5967 /* Look for the `:: new' and `:: delete', which also signal the
5968 beginning of a new-expression, or delete-expression,
5969 respectively. If the next token is `::', then it might be one of
5971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
5975 /* See if the token after the `::' is one of the keywords in
5976 which we're interested. */
5977 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
5978 /* If it's `new', we have a new-expression. */
5979 if (keyword
== RID_NEW
)
5980 return cp_parser_new_expression (parser
);
5981 /* Similarly, for `delete'. */
5982 else if (keyword
== RID_DELETE
)
5983 return cp_parser_delete_expression (parser
);
5986 /* Look for a unary operator. */
5987 unary_operator
= cp_parser_unary_operator (token
);
5988 /* The `++' and `--' operators can be handled similarly, even though
5989 they are not technically unary-operators in the grammar. */
5990 if (unary_operator
== ERROR_MARK
)
5992 if (token
->type
== CPP_PLUS_PLUS
)
5993 unary_operator
= PREINCREMENT_EXPR
;
5994 else if (token
->type
== CPP_MINUS_MINUS
)
5995 unary_operator
= PREDECREMENT_EXPR
;
5996 /* Handle the GNU address-of-label extension. */
5997 else if (cp_parser_allow_gnu_extensions_p (parser
)
5998 && token
->type
== CPP_AND_AND
)
6002 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6004 /* Consume the '&&' token. */
6005 cp_lexer_consume_token (parser
->lexer
);
6006 /* Look for the identifier. */
6007 identifier
= cp_parser_identifier (parser
);
6008 /* Create an expression representing the address. */
6009 expression
= finish_label_address_expr (identifier
, loc
);
6010 if (cp_parser_non_integral_constant_expression (parser
,
6012 expression
= error_mark_node
;
6016 if (unary_operator
!= ERROR_MARK
)
6018 tree cast_expression
;
6019 tree expression
= error_mark_node
;
6020 non_integral_constant non_constant_p
= NIC_NONE
;
6022 /* Consume the operator token. */
6023 token
= cp_lexer_consume_token (parser
->lexer
);
6024 /* Parse the cast-expression. */
6026 = cp_parser_cast_expression (parser
,
6027 unary_operator
== ADDR_EXPR
,
6028 /*cast_p=*/false, pidk
);
6029 /* Now, build an appropriate representation. */
6030 switch (unary_operator
)
6033 non_constant_p
= NIC_STAR
;
6034 expression
= build_x_indirect_ref (cast_expression
, RO_UNARY_STAR
,
6035 tf_warning_or_error
);
6039 non_constant_p
= NIC_ADDR
;
6042 expression
= build_x_unary_op (unary_operator
, cast_expression
,
6043 tf_warning_or_error
);
6046 case PREINCREMENT_EXPR
:
6047 case PREDECREMENT_EXPR
:
6048 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
6049 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
6051 case UNARY_PLUS_EXPR
:
6053 case TRUTH_NOT_EXPR
:
6054 expression
= finish_unary_op_expr (unary_operator
, cast_expression
);
6061 if (non_constant_p
!= NIC_NONE
6062 && cp_parser_non_integral_constant_expression (parser
,
6064 expression
= error_mark_node
;
6069 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
6070 /*member_access_only_p=*/false,
6074 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6075 unary-operator, the corresponding tree code is returned. */
6077 static enum tree_code
6078 cp_parser_unary_operator (cp_token
* token
)
6080 switch (token
->type
)
6083 return INDIRECT_REF
;
6089 return UNARY_PLUS_EXPR
;
6095 return TRUTH_NOT_EXPR
;
6098 return BIT_NOT_EXPR
;
6105 /* Parse a new-expression.
6108 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6109 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6111 Returns a representation of the expression. */
6114 cp_parser_new_expression (cp_parser
* parser
)
6116 bool global_scope_p
;
6117 VEC(tree
,gc
) *placement
;
6119 VEC(tree
,gc
) *initializer
;
6123 /* Look for the optional `::' operator. */
6125 = (cp_parser_global_scope_opt (parser
,
6126 /*current_scope_valid_p=*/false)
6128 /* Look for the `new' operator. */
6129 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
6130 /* There's no easy way to tell a new-placement from the
6131 `( type-id )' construct. */
6132 cp_parser_parse_tentatively (parser
);
6133 /* Look for a new-placement. */
6134 placement
= cp_parser_new_placement (parser
);
6135 /* If that didn't work out, there's no new-placement. */
6136 if (!cp_parser_parse_definitely (parser
))
6138 if (placement
!= NULL
)
6139 release_tree_vector (placement
);
6143 /* If the next token is a `(', then we have a parenthesized
6145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6148 /* Consume the `('. */
6149 cp_lexer_consume_token (parser
->lexer
);
6150 /* Parse the type-id. */
6151 type
= cp_parser_type_id (parser
);
6152 /* Look for the closing `)'. */
6153 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6154 token
= cp_lexer_peek_token (parser
->lexer
);
6155 /* There should not be a direct-new-declarator in this production,
6156 but GCC used to allowed this, so we check and emit a sensible error
6157 message for this case. */
6158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6160 error_at (token
->location
,
6161 "array bound forbidden after parenthesized type-id");
6162 inform (token
->location
,
6163 "try removing the parentheses around the type-id");
6164 cp_parser_direct_new_declarator (parser
);
6168 /* Otherwise, there must be a new-type-id. */
6170 type
= cp_parser_new_type_id (parser
, &nelts
);
6172 /* If the next token is a `(' or '{', then we have a new-initializer. */
6173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
6174 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6175 initializer
= cp_parser_new_initializer (parser
);
6179 /* A new-expression may not appear in an integral constant
6181 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
6182 ret
= error_mark_node
;
6185 /* Create a representation of the new-expression. */
6186 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
6187 tf_warning_or_error
);
6190 if (placement
!= NULL
)
6191 release_tree_vector (placement
);
6192 if (initializer
!= NULL
)
6193 release_tree_vector (initializer
);
6198 /* Parse a new-placement.
6203 Returns the same representation as for an expression-list. */
6205 static VEC(tree
,gc
) *
6206 cp_parser_new_placement (cp_parser
* parser
)
6208 VEC(tree
,gc
) *expression_list
;
6210 /* Parse the expression-list. */
6211 expression_list
= (cp_parser_parenthesized_expression_list
6212 (parser
, non_attr
, /*cast_p=*/false,
6213 /*allow_expansion_p=*/true,
6214 /*non_constant_p=*/NULL
));
6216 return expression_list
;
6219 /* Parse a new-type-id.
6222 type-specifier-seq new-declarator [opt]
6224 Returns the TYPE allocated. If the new-type-id indicates an array
6225 type, *NELTS is set to the number of elements in the last array
6226 bound; the TYPE will not include the last array bound. */
6229 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
6231 cp_decl_specifier_seq type_specifier_seq
;
6232 cp_declarator
*new_declarator
;
6233 cp_declarator
*declarator
;
6234 cp_declarator
*outer_declarator
;
6235 const char *saved_message
;
6238 /* The type-specifier sequence must not contain type definitions.
6239 (It cannot contain declarations of new types either, but if they
6240 are not definitions we will catch that because they are not
6242 saved_message
= parser
->type_definition_forbidden_message
;
6243 parser
->type_definition_forbidden_message
6244 = G_("types may not be defined in a new-type-id");
6245 /* Parse the type-specifier-seq. */
6246 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
6247 /*is_trailing_return=*/false,
6248 &type_specifier_seq
);
6249 /* Restore the old message. */
6250 parser
->type_definition_forbidden_message
= saved_message
;
6251 /* Parse the new-declarator. */
6252 new_declarator
= cp_parser_new_declarator_opt (parser
);
6254 /* Determine the number of elements in the last array dimension, if
6257 /* Skip down to the last array dimension. */
6258 declarator
= new_declarator
;
6259 outer_declarator
= NULL
;
6260 while (declarator
&& (declarator
->kind
== cdk_pointer
6261 || declarator
->kind
== cdk_ptrmem
))
6263 outer_declarator
= declarator
;
6264 declarator
= declarator
->declarator
;
6267 && declarator
->kind
== cdk_array
6268 && declarator
->declarator
6269 && declarator
->declarator
->kind
== cdk_array
)
6271 outer_declarator
= declarator
;
6272 declarator
= declarator
->declarator
;
6275 if (declarator
&& declarator
->kind
== cdk_array
)
6277 *nelts
= declarator
->u
.array
.bounds
;
6278 if (*nelts
== error_mark_node
)
6279 *nelts
= integer_one_node
;
6281 if (outer_declarator
)
6282 outer_declarator
->declarator
= declarator
->declarator
;
6284 new_declarator
= NULL
;
6287 type
= groktypename (&type_specifier_seq
, new_declarator
, false);
6291 /* Parse an (optional) new-declarator.
6294 ptr-operator new-declarator [opt]
6295 direct-new-declarator
6297 Returns the declarator. */
6299 static cp_declarator
*
6300 cp_parser_new_declarator_opt (cp_parser
* parser
)
6302 enum tree_code code
;
6304 cp_cv_quals cv_quals
;
6306 /* We don't know if there's a ptr-operator next, or not. */
6307 cp_parser_parse_tentatively (parser
);
6308 /* Look for a ptr-operator. */
6309 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
6310 /* If that worked, look for more new-declarators. */
6311 if (cp_parser_parse_definitely (parser
))
6313 cp_declarator
*declarator
;
6315 /* Parse another optional declarator. */
6316 declarator
= cp_parser_new_declarator_opt (parser
);
6318 return cp_parser_make_indirect_declarator
6319 (code
, type
, cv_quals
, declarator
);
6322 /* If the next token is a `[', there is a direct-new-declarator. */
6323 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6324 return cp_parser_direct_new_declarator (parser
);
6329 /* Parse a direct-new-declarator.
6331 direct-new-declarator:
6333 direct-new-declarator [constant-expression]
6337 static cp_declarator
*
6338 cp_parser_direct_new_declarator (cp_parser
* parser
)
6340 cp_declarator
*declarator
= NULL
;
6346 /* Look for the opening `['. */
6347 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
6348 /* The first expression is not required to be constant. */
6351 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6352 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6353 /* The standard requires that the expression have integral
6354 type. DR 74 adds enumeration types. We believe that the
6355 real intent is that these expressions be handled like the
6356 expression in a `switch' condition, which also allows
6357 classes with a single conversion to integral or
6358 enumeration type. */
6359 if (!processing_template_decl
)
6362 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
6367 error_at (token
->location
,
6368 "expression in new-declarator must have integral "
6369 "or enumeration type");
6370 expression
= error_mark_node
;
6374 /* But all the other expressions must be. */
6377 = cp_parser_constant_expression (parser
,
6378 /*allow_non_constant=*/false,
6380 /* Look for the closing `]'. */
6381 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6383 /* Add this bound to the declarator. */
6384 declarator
= make_array_declarator (declarator
, expression
);
6386 /* If the next token is not a `[', then there are no more
6388 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
6395 /* Parse a new-initializer.
6398 ( expression-list [opt] )
6401 Returns a representation of the expression-list. */
6403 static VEC(tree
,gc
) *
6404 cp_parser_new_initializer (cp_parser
* parser
)
6406 VEC(tree
,gc
) *expression_list
;
6408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6411 bool expr_non_constant_p
;
6412 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6413 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6414 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
6415 expression_list
= make_tree_vector_single (t
);
6418 expression_list
= (cp_parser_parenthesized_expression_list
6419 (parser
, non_attr
, /*cast_p=*/false,
6420 /*allow_expansion_p=*/true,
6421 /*non_constant_p=*/NULL
));
6423 return expression_list
;
6426 /* Parse a delete-expression.
6429 :: [opt] delete cast-expression
6430 :: [opt] delete [ ] cast-expression
6432 Returns a representation of the expression. */
6435 cp_parser_delete_expression (cp_parser
* parser
)
6437 bool global_scope_p
;
6441 /* Look for the optional `::' operator. */
6443 = (cp_parser_global_scope_opt (parser
,
6444 /*current_scope_valid_p=*/false)
6446 /* Look for the `delete' keyword. */
6447 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
6448 /* See if the array syntax is in use. */
6449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6451 /* Consume the `[' token. */
6452 cp_lexer_consume_token (parser
->lexer
);
6453 /* Look for the `]' token. */
6454 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6455 /* Remember that this is the `[]' construct. */
6461 /* Parse the cast-expression. */
6462 expression
= cp_parser_simple_cast_expression (parser
);
6464 /* A delete-expression may not appear in an integral constant
6466 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
6467 return error_mark_node
;
6469 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
);
6472 /* Returns true if TOKEN may start a cast-expression and false
6476 cp_parser_token_starts_cast_expression (cp_token
*token
)
6478 switch (token
->type
)
6484 case CPP_CLOSE_SQUARE
:
6485 case CPP_CLOSE_PAREN
:
6486 case CPP_CLOSE_BRACE
:
6490 case CPP_DEREF_STAR
:
6498 case CPP_GREATER_EQ
:
6518 /* '[' may start a primary-expression in obj-c++. */
6519 case CPP_OPEN_SQUARE
:
6520 return c_dialect_objc ();
6527 /* Parse a cast-expression.
6531 ( type-id ) cast-expression
6533 ADDRESS_P is true iff the unary-expression is appearing as the
6534 operand of the `&' operator. CAST_P is true if this expression is
6535 the target of a cast.
6537 Returns a representation of the expression. */
6540 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6543 /* If it's a `(', then we might be looking at a cast. */
6544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6546 tree type
= NULL_TREE
;
6547 tree expr
= NULL_TREE
;
6548 bool compound_literal_p
;
6549 const char *saved_message
;
6551 /* There's no way to know yet whether or not this is a cast.
6552 For example, `(int (3))' is a unary-expression, while `(int)
6553 3' is a cast. So, we resort to parsing tentatively. */
6554 cp_parser_parse_tentatively (parser
);
6555 /* Types may not be defined in a cast. */
6556 saved_message
= parser
->type_definition_forbidden_message
;
6557 parser
->type_definition_forbidden_message
6558 = G_("types may not be defined in casts");
6559 /* Consume the `('. */
6560 cp_lexer_consume_token (parser
->lexer
);
6561 /* A very tricky bit is that `(struct S) { 3 }' is a
6562 compound-literal (which we permit in C++ as an extension).
6563 But, that construct is not a cast-expression -- it is a
6564 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6565 is legal; if the compound-literal were a cast-expression,
6566 you'd need an extra set of parentheses.) But, if we parse
6567 the type-id, and it happens to be a class-specifier, then we
6568 will commit to the parse at that point, because we cannot
6569 undo the action that is done when creating a new class. So,
6570 then we cannot back up and do a postfix-expression.
6572 Therefore, we scan ahead to the closing `)', and check to see
6573 if the token after the `)' is a `{'. If so, we are not
6574 looking at a cast-expression.
6576 Save tokens so that we can put them back. */
6577 cp_lexer_save_tokens (parser
->lexer
);
6578 /* Skip tokens until the next token is a closing parenthesis.
6579 If we find the closing `)', and the next token is a `{', then
6580 we are looking at a compound-literal. */
6582 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6583 /*consume_paren=*/true)
6584 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6585 /* Roll back the tokens we skipped. */
6586 cp_lexer_rollback_tokens (parser
->lexer
);
6587 /* If we were looking at a compound-literal, simulate an error
6588 so that the call to cp_parser_parse_definitely below will
6590 if (compound_literal_p
)
6591 cp_parser_simulate_error (parser
);
6594 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6595 parser
->in_type_id_in_expr_p
= true;
6596 /* Look for the type-id. */
6597 type
= cp_parser_type_id (parser
);
6598 /* Look for the closing `)'. */
6599 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6600 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6603 /* Restore the saved message. */
6604 parser
->type_definition_forbidden_message
= saved_message
;
6606 /* At this point this can only be either a cast or a
6607 parenthesized ctor such as `(T ())' that looks like a cast to
6608 function returning T. */
6609 if (!cp_parser_error_occurred (parser
)
6610 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6613 cp_parser_parse_definitely (parser
);
6614 expr
= cp_parser_cast_expression (parser
,
6615 /*address_p=*/false,
6616 /*cast_p=*/true, pidk
);
6618 /* Warn about old-style casts, if so requested. */
6619 if (warn_old_style_cast
6620 && !in_system_header
6621 && !VOID_TYPE_P (type
)
6622 && current_lang_name
!= lang_name_c
)
6623 warning (OPT_Wold_style_cast
, "use of old-style cast");
6625 /* Only type conversions to integral or enumeration types
6626 can be used in constant-expressions. */
6627 if (!cast_valid_in_integral_constant_expression_p (type
)
6628 && cp_parser_non_integral_constant_expression (parser
,
6630 return error_mark_node
;
6632 /* Perform the cast. */
6633 expr
= build_c_cast (input_location
, type
, expr
);
6637 cp_parser_abort_tentative_parse (parser
);
6640 /* If we get here, then it's not a cast, so it must be a
6641 unary-expression. */
6642 return cp_parser_unary_expression (parser
, address_p
, cast_p
, pidk
);
6645 /* Parse a binary expression of the general form:
6649 pm-expression .* cast-expression
6650 pm-expression ->* cast-expression
6652 multiplicative-expression:
6654 multiplicative-expression * pm-expression
6655 multiplicative-expression / pm-expression
6656 multiplicative-expression % pm-expression
6658 additive-expression:
6659 multiplicative-expression
6660 additive-expression + multiplicative-expression
6661 additive-expression - multiplicative-expression
6665 shift-expression << additive-expression
6666 shift-expression >> additive-expression
6668 relational-expression:
6670 relational-expression < shift-expression
6671 relational-expression > shift-expression
6672 relational-expression <= shift-expression
6673 relational-expression >= shift-expression
6677 relational-expression:
6678 relational-expression <? shift-expression
6679 relational-expression >? shift-expression
6681 equality-expression:
6682 relational-expression
6683 equality-expression == relational-expression
6684 equality-expression != relational-expression
6688 and-expression & equality-expression
6690 exclusive-or-expression:
6692 exclusive-or-expression ^ and-expression
6694 inclusive-or-expression:
6695 exclusive-or-expression
6696 inclusive-or-expression | exclusive-or-expression
6698 logical-and-expression:
6699 inclusive-or-expression
6700 logical-and-expression && inclusive-or-expression
6702 logical-or-expression:
6703 logical-and-expression
6704 logical-or-expression || logical-and-expression
6706 All these are implemented with a single function like:
6709 simple-cast-expression
6710 binary-expression <token> binary-expression
6712 CAST_P is true if this expression is the target of a cast.
6714 The binops_by_token map is used to get the tree codes for each <token> type.
6715 binary-expressions are associated according to a precedence table. */
6717 #define TOKEN_PRECEDENCE(token) \
6718 (((token->type == CPP_GREATER \
6719 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6720 && !parser->greater_than_is_operator_p) \
6721 ? PREC_NOT_OPERATOR \
6722 : binops_by_token[token->type].prec)
6725 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
6726 bool no_toplevel_fold_p
,
6727 enum cp_parser_prec prec
,
6730 cp_parser_expression_stack stack
;
6731 cp_parser_expression_stack_entry
*sp
= &stack
[0];
6734 enum tree_code tree_type
, lhs_type
, rhs_type
;
6735 enum cp_parser_prec new_prec
, lookahead_prec
;
6738 /* Parse the first expression. */
6739 lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false, cast_p
, pidk
);
6740 lhs_type
= ERROR_MARK
;
6744 /* Get an operator token. */
6745 token
= cp_lexer_peek_token (parser
->lexer
);
6747 if (warn_cxx0x_compat
6748 && token
->type
== CPP_RSHIFT
6749 && !parser
->greater_than_is_operator_p
)
6751 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
6752 "%<>>%> operator will be treated as"
6753 " two right angle brackets in C++0x"))
6754 inform (token
->location
,
6755 "suggest parentheses around %<>>%> expression");
6758 new_prec
= TOKEN_PRECEDENCE (token
);
6760 /* Popping an entry off the stack means we completed a subexpression:
6761 - either we found a token which is not an operator (`>' where it is not
6762 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6763 will happen repeatedly;
6764 - or, we found an operator which has lower priority. This is the case
6765 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6767 if (new_prec
<= prec
)
6776 tree_type
= binops_by_token
[token
->type
].tree_type
;
6778 /* We used the operator token. */
6779 cp_lexer_consume_token (parser
->lexer
);
6781 /* For "false && x" or "true || x", x will never be executed;
6782 disable warnings while evaluating it. */
6783 if (tree_type
== TRUTH_ANDIF_EXPR
)
6784 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_false_node
;
6785 else if (tree_type
== TRUTH_ORIF_EXPR
)
6786 c_inhibit_evaluation_warnings
+= lhs
== truthvalue_true_node
;
6788 /* Extract another operand. It may be the RHS of this expression
6789 or the LHS of a new, higher priority expression. */
6790 rhs
= cp_parser_simple_cast_expression (parser
);
6791 rhs_type
= ERROR_MARK
;
6793 /* Get another operator token. Look up its precedence to avoid
6794 building a useless (immediately popped) stack entry for common
6795 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6796 token
= cp_lexer_peek_token (parser
->lexer
);
6797 lookahead_prec
= TOKEN_PRECEDENCE (token
);
6798 if (lookahead_prec
> new_prec
)
6800 /* ... and prepare to parse the RHS of the new, higher priority
6801 expression. Since precedence levels on the stack are
6802 monotonically increasing, we do not have to care about
6805 sp
->tree_type
= tree_type
;
6807 sp
->lhs_type
= lhs_type
;
6810 lhs_type
= rhs_type
;
6812 new_prec
= lookahead_prec
;
6816 lookahead_prec
= new_prec
;
6817 /* If the stack is not empty, we have parsed into LHS the right side
6818 (`4' in the example above) of an expression we had suspended.
6819 We can use the information on the stack to recover the LHS (`3')
6820 from the stack together with the tree code (`MULT_EXPR'), and
6821 the precedence of the higher level subexpression
6822 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6823 which will be used to actually build the additive expression. */
6826 tree_type
= sp
->tree_type
;
6828 rhs_type
= lhs_type
;
6830 lhs_type
= sp
->lhs_type
;
6833 /* Undo the disabling of warnings done above. */
6834 if (tree_type
== TRUTH_ANDIF_EXPR
)
6835 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_false_node
;
6836 else if (tree_type
== TRUTH_ORIF_EXPR
)
6837 c_inhibit_evaluation_warnings
-= lhs
== truthvalue_true_node
;
6839 overloaded_p
= false;
6840 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6841 ERROR_MARK for everything that is not a binary expression.
6842 This makes warn_about_parentheses miss some warnings that
6843 involve unary operators. For unary expressions we should
6844 pass the correct tree_code unless the unary expression was
6845 surrounded by parentheses.
6847 if (no_toplevel_fold_p
6848 && lookahead_prec
<= prec
6850 && TREE_CODE_CLASS (tree_type
) == tcc_comparison
)
6851 lhs
= build2 (tree_type
, boolean_type_node
, lhs
, rhs
);
6853 lhs
= build_x_binary_op (tree_type
, lhs
, lhs_type
, rhs
, rhs_type
,
6854 &overloaded_p
, tf_warning_or_error
);
6855 lhs_type
= tree_type
;
6857 /* If the binary operator required the use of an overloaded operator,
6858 then this expression cannot be an integral constant-expression.
6859 An overloaded operator can be used even if both operands are
6860 otherwise permissible in an integral constant-expression if at
6861 least one of the operands is of enumeration type. */
6864 && cp_parser_non_integral_constant_expression (parser
,
6866 return error_mark_node
;
6873 /* Parse the `? expression : assignment-expression' part of a
6874 conditional-expression. The LOGICAL_OR_EXPR is the
6875 logical-or-expression that started the conditional-expression.
6876 Returns a representation of the entire conditional-expression.
6878 This routine is used by cp_parser_assignment_expression.
6880 ? expression : assignment-expression
6884 ? : assignment-expression */
6887 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
6890 tree assignment_expr
;
6891 struct cp_token
*token
;
6893 /* Consume the `?' token. */
6894 cp_lexer_consume_token (parser
->lexer
);
6895 token
= cp_lexer_peek_token (parser
->lexer
);
6896 if (cp_parser_allow_gnu_extensions_p (parser
)
6897 && token
->type
== CPP_COLON
)
6899 pedwarn (token
->location
, OPT_pedantic
,
6900 "ISO C++ does not allow ?: with omitted middle operand");
6901 /* Implicit true clause. */
6903 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
6904 warn_for_omitted_condop (token
->location
, logical_or_expr
);
6908 /* Parse the expression. */
6909 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
6910 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6911 c_inhibit_evaluation_warnings
+=
6912 ((logical_or_expr
== truthvalue_true_node
)
6913 - (logical_or_expr
== truthvalue_false_node
));
6916 /* The next token should be a `:'. */
6917 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
6918 /* Parse the assignment-expression. */
6919 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
6920 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
6922 /* Build the conditional-expression. */
6923 return build_x_conditional_expr (logical_or_expr
,
6926 tf_warning_or_error
);
6929 /* Parse an assignment-expression.
6931 assignment-expression:
6932 conditional-expression
6933 logical-or-expression assignment-operator assignment_expression
6936 CAST_P is true if this expression is the target of a cast.
6938 Returns a representation for the expression. */
6941 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
6946 /* If the next token is the `throw' keyword, then we're looking at
6947 a throw-expression. */
6948 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
6949 expr
= cp_parser_throw_expression (parser
);
6950 /* Otherwise, it must be that we are looking at a
6951 logical-or-expression. */
6954 /* Parse the binary expressions (logical-or-expression). */
6955 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
6956 PREC_NOT_OPERATOR
, pidk
);
6957 /* If the next token is a `?' then we're actually looking at a
6958 conditional-expression. */
6959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
6960 return cp_parser_question_colon_clause (parser
, expr
);
6963 enum tree_code assignment_operator
;
6965 /* If it's an assignment-operator, we're using the second
6968 = cp_parser_assignment_operator_opt (parser
);
6969 if (assignment_operator
!= ERROR_MARK
)
6971 bool non_constant_p
;
6973 /* Parse the right-hand side of the assignment. */
6974 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
6976 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
6977 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6979 /* An assignment may not appear in a
6980 constant-expression. */
6981 if (cp_parser_non_integral_constant_expression (parser
,
6983 return error_mark_node
;
6984 /* Build the assignment expression. */
6985 expr
= build_x_modify_expr (expr
,
6986 assignment_operator
,
6988 tf_warning_or_error
);
6996 /* Parse an (optional) assignment-operator.
6998 assignment-operator: one of
6999 = *= /= %= += -= >>= <<= &= ^= |=
7003 assignment-operator: one of
7006 If the next token is an assignment operator, the corresponding tree
7007 code is returned, and the token is consumed. For example, for
7008 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7009 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7010 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7011 operator, ERROR_MARK is returned. */
7013 static enum tree_code
7014 cp_parser_assignment_operator_opt (cp_parser
* parser
)
7019 /* Peek at the next token. */
7020 token
= cp_lexer_peek_token (parser
->lexer
);
7022 switch (token
->type
)
7033 op
= TRUNC_DIV_EXPR
;
7037 op
= TRUNC_MOD_EXPR
;
7069 /* Nothing else is an assignment operator. */
7073 /* If it was an assignment operator, consume it. */
7074 if (op
!= ERROR_MARK
)
7075 cp_lexer_consume_token (parser
->lexer
);
7080 /* Parse an expression.
7083 assignment-expression
7084 expression , assignment-expression
7086 CAST_P is true if this expression is the target of a cast.
7088 Returns a representation of the expression. */
7091 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
7093 tree expression
= NULL_TREE
;
7097 tree assignment_expression
;
7099 /* Parse the next assignment-expression. */
7100 assignment_expression
7101 = cp_parser_assignment_expression (parser
, cast_p
, pidk
);
7102 /* If this is the first assignment-expression, we can just
7105 expression
= assignment_expression
;
7107 expression
= build_x_compound_expr (expression
,
7108 assignment_expression
,
7109 tf_warning_or_error
);
7110 /* If the next token is not a comma, then we are done with the
7112 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7114 /* Consume the `,'. */
7115 cp_lexer_consume_token (parser
->lexer
);
7116 /* A comma operator cannot appear in a constant-expression. */
7117 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
7118 expression
= error_mark_node
;
7124 /* Parse a constant-expression.
7126 constant-expression:
7127 conditional-expression
7129 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7130 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7131 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7132 is false, NON_CONSTANT_P should be NULL. */
7135 cp_parser_constant_expression (cp_parser
* parser
,
7136 bool allow_non_constant_p
,
7137 bool *non_constant_p
)
7139 bool saved_integral_constant_expression_p
;
7140 bool saved_allow_non_integral_constant_expression_p
;
7141 bool saved_non_integral_constant_expression_p
;
7144 /* It might seem that we could simply parse the
7145 conditional-expression, and then check to see if it were
7146 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7147 one that the compiler can figure out is constant, possibly after
7148 doing some simplifications or optimizations. The standard has a
7149 precise definition of constant-expression, and we must honor
7150 that, even though it is somewhat more restrictive.
7156 is not a legal declaration, because `(2, 3)' is not a
7157 constant-expression. The `,' operator is forbidden in a
7158 constant-expression. However, GCC's constant-folding machinery
7159 will fold this operation to an INTEGER_CST for `3'. */
7161 /* Save the old settings. */
7162 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
7163 saved_allow_non_integral_constant_expression_p
7164 = parser
->allow_non_integral_constant_expression_p
;
7165 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
7166 /* We are now parsing a constant-expression. */
7167 parser
->integral_constant_expression_p
= true;
7168 parser
->allow_non_integral_constant_expression_p
= allow_non_constant_p
;
7169 parser
->non_integral_constant_expression_p
= false;
7170 /* Although the grammar says "conditional-expression", we parse an
7171 "assignment-expression", which also permits "throw-expression"
7172 and the use of assignment operators. In the case that
7173 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7174 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7175 actually essential that we look for an assignment-expression.
7176 For example, cp_parser_initializer_clauses uses this function to
7177 determine whether a particular assignment-expression is in fact
7179 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7180 /* Restore the old settings. */
7181 parser
->integral_constant_expression_p
7182 = saved_integral_constant_expression_p
;
7183 parser
->allow_non_integral_constant_expression_p
7184 = saved_allow_non_integral_constant_expression_p
;
7185 if (allow_non_constant_p
)
7186 *non_constant_p
= parser
->non_integral_constant_expression_p
;
7187 else if (parser
->non_integral_constant_expression_p
)
7188 expression
= error_mark_node
;
7189 parser
->non_integral_constant_expression_p
7190 = saved_non_integral_constant_expression_p
;
7195 /* Parse __builtin_offsetof.
7197 offsetof-expression:
7198 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7200 offsetof-member-designator:
7202 | offsetof-member-designator "." id-expression
7203 | offsetof-member-designator "[" expression "]"
7204 | offsetof-member-designator "->" id-expression */
7207 cp_parser_builtin_offsetof (cp_parser
*parser
)
7209 int save_ice_p
, save_non_ice_p
;
7214 /* We're about to accept non-integral-constant things, but will
7215 definitely yield an integral constant expression. Save and
7216 restore these values around our local parsing. */
7217 save_ice_p
= parser
->integral_constant_expression_p
;
7218 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
7220 /* Consume the "__builtin_offsetof" token. */
7221 cp_lexer_consume_token (parser
->lexer
);
7222 /* Consume the opening `('. */
7223 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7224 /* Parse the type-id. */
7225 type
= cp_parser_type_id (parser
);
7226 /* Look for the `,'. */
7227 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7228 token
= cp_lexer_peek_token (parser
->lexer
);
7230 /* Build the (type *)null that begins the traditional offsetof macro. */
7231 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
7232 tf_warning_or_error
);
7234 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7235 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
7236 true, &dummy
, token
->location
);
7239 token
= cp_lexer_peek_token (parser
->lexer
);
7240 switch (token
->type
)
7242 case CPP_OPEN_SQUARE
:
7243 /* offsetof-member-designator "[" expression "]" */
7244 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
7248 /* offsetof-member-designator "->" identifier */
7249 expr
= grok_array_decl (expr
, integer_zero_node
);
7253 /* offsetof-member-designator "." identifier */
7254 cp_lexer_consume_token (parser
->lexer
);
7255 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
7260 case CPP_CLOSE_PAREN
:
7261 /* Consume the ")" token. */
7262 cp_lexer_consume_token (parser
->lexer
);
7266 /* Error. We know the following require will fail, but
7267 that gives the proper error message. */
7268 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7269 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
7270 expr
= error_mark_node
;
7276 /* If we're processing a template, we can't finish the semantics yet.
7277 Otherwise we can fold the entire expression now. */
7278 if (processing_template_decl
)
7279 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
7281 expr
= finish_offsetof (expr
);
7284 parser
->integral_constant_expression_p
= save_ice_p
;
7285 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
7290 /* Parse a trait expression. */
7293 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
7296 tree type1
, type2
= NULL_TREE
;
7297 bool binary
= false;
7298 cp_decl_specifier_seq decl_specs
;
7302 case RID_HAS_NOTHROW_ASSIGN
:
7303 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
7305 case RID_HAS_NOTHROW_CONSTRUCTOR
:
7306 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
7308 case RID_HAS_NOTHROW_COPY
:
7309 kind
= CPTK_HAS_NOTHROW_COPY
;
7311 case RID_HAS_TRIVIAL_ASSIGN
:
7312 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
7314 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
7315 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
7317 case RID_HAS_TRIVIAL_COPY
:
7318 kind
= CPTK_HAS_TRIVIAL_COPY
;
7320 case RID_HAS_TRIVIAL_DESTRUCTOR
:
7321 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
7323 case RID_HAS_VIRTUAL_DESTRUCTOR
:
7324 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
7326 case RID_IS_ABSTRACT
:
7327 kind
= CPTK_IS_ABSTRACT
;
7329 case RID_IS_BASE_OF
:
7330 kind
= CPTK_IS_BASE_OF
;
7334 kind
= CPTK_IS_CLASS
;
7336 case RID_IS_CONVERTIBLE_TO
:
7337 kind
= CPTK_IS_CONVERTIBLE_TO
;
7341 kind
= CPTK_IS_EMPTY
;
7344 kind
= CPTK_IS_ENUM
;
7349 case RID_IS_POLYMORPHIC
:
7350 kind
= CPTK_IS_POLYMORPHIC
;
7352 case RID_IS_STD_LAYOUT
:
7353 kind
= CPTK_IS_STD_LAYOUT
;
7355 case RID_IS_TRIVIAL
:
7356 kind
= CPTK_IS_TRIVIAL
;
7359 kind
= CPTK_IS_UNION
;
7365 /* Consume the token. */
7366 cp_lexer_consume_token (parser
->lexer
);
7368 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7370 type1
= cp_parser_type_id (parser
);
7372 if (type1
== error_mark_node
)
7373 return error_mark_node
;
7375 /* Build a trivial decl-specifier-seq. */
7376 clear_decl_specs (&decl_specs
);
7377 decl_specs
.type
= type1
;
7379 /* Call grokdeclarator to figure out what type this is. */
7380 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7381 /*initialized=*/0, /*attrlist=*/NULL
);
7385 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7387 type2
= cp_parser_type_id (parser
);
7389 if (type2
== error_mark_node
)
7390 return error_mark_node
;
7392 /* Build a trivial decl-specifier-seq. */
7393 clear_decl_specs (&decl_specs
);
7394 decl_specs
.type
= type2
;
7396 /* Call grokdeclarator to figure out what type this is. */
7397 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7398 /*initialized=*/0, /*attrlist=*/NULL
);
7401 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7403 /* Complete the trait expression, which may mean either processing
7404 the trait expr now or saving it for template instantiation. */
7405 return finish_trait_expr (kind
, type1
, type2
);
7408 /* Lambdas that appear in variable initializer or default argument scope
7409 get that in their mangling, so we need to record it. We might as well
7410 use the count for function and namespace scopes as well. */
7411 static GTY(()) tree lambda_scope
;
7412 static GTY(()) int lambda_count
;
7413 typedef struct GTY(()) tree_int
7418 DEF_VEC_O(tree_int
);
7419 DEF_VEC_ALLOC_O(tree_int
,gc
);
7420 static GTY(()) VEC(tree_int
,gc
) *lambda_scope_stack
;
7423 start_lambda_scope (tree decl
)
7427 /* Once we're inside a function, we ignore other scopes and just push
7428 the function again so that popping works properly. */
7429 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
7430 decl
= current_function_decl
;
7431 ti
.t
= lambda_scope
;
7432 ti
.i
= lambda_count
;
7433 VEC_safe_push (tree_int
, gc
, lambda_scope_stack
, &ti
);
7434 if (lambda_scope
!= decl
)
7436 /* Don't reset the count if we're still in the same function. */
7437 lambda_scope
= decl
;
7443 record_lambda_scope (tree lambda
)
7445 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
7446 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
7450 finish_lambda_scope (void)
7452 tree_int
*p
= VEC_last (tree_int
, lambda_scope_stack
);
7453 if (lambda_scope
!= p
->t
)
7455 lambda_scope
= p
->t
;
7456 lambda_count
= p
->i
;
7458 VEC_pop (tree_int
, lambda_scope_stack
);
7461 /* Parse a lambda expression.
7464 lambda-introducer lambda-declarator [opt] compound-statement
7466 Returns a representation of the expression. */
7469 cp_parser_lambda_expression (cp_parser
* parser
)
7471 tree lambda_expr
= build_lambda_expr ();
7474 LAMBDA_EXPR_LOCATION (lambda_expr
)
7475 = cp_lexer_peek_token (parser
->lexer
)->location
;
7477 if (cp_unevaluated_operand
)
7478 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
7479 "lambda-expression in unevaluated context");
7481 /* We may be in the middle of deferred access check. Disable
7483 push_deferring_access_checks (dk_no_deferred
);
7485 cp_parser_lambda_introducer (parser
, lambda_expr
);
7487 type
= begin_lambda_type (lambda_expr
);
7489 record_lambda_scope (lambda_expr
);
7491 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7492 determine_visibility (TYPE_NAME (type
));
7494 /* Now that we've started the type, add the capture fields for any
7495 explicit captures. */
7496 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
7499 /* Inside the class, surrounding template-parameter-lists do not apply. */
7500 unsigned int saved_num_template_parameter_lists
7501 = parser
->num_template_parameter_lists
;
7503 parser
->num_template_parameter_lists
= 0;
7505 /* By virtue of defining a local class, a lambda expression has access to
7506 the private variables of enclosing classes. */
7508 cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
7510 cp_parser_lambda_body (parser
, lambda_expr
);
7512 /* The capture list was built up in reverse order; fix that now. */
7514 tree newlist
= NULL_TREE
;
7517 for (elt
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
7520 tree field
= TREE_PURPOSE (elt
);
7523 next
= TREE_CHAIN (elt
);
7524 TREE_CHAIN (elt
) = newlist
;
7527 /* Also add __ to the beginning of the field name so that code
7528 outside the lambda body can't see the captured name. We could
7529 just remove the name entirely, but this is more useful for
7531 if (field
== LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
7532 /* The 'this' capture already starts with __. */
7535 buf
= (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field
)) + 3);
7536 buf
[1] = buf
[0] = '_';
7537 memcpy (buf
+ 2, IDENTIFIER_POINTER (DECL_NAME (field
)),
7538 IDENTIFIER_LENGTH (DECL_NAME (field
)) + 1);
7539 DECL_NAME (field
) = get_identifier (buf
);
7541 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
) = newlist
;
7544 maybe_add_lambda_conv_op (type
);
7546 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
7548 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
7551 pop_deferring_access_checks ();
7553 return build_lambda_object (lambda_expr
);
7556 /* Parse the beginning of a lambda expression.
7559 [ lambda-capture [opt] ]
7561 LAMBDA_EXPR is the current representation of the lambda expression. */
7564 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
7566 /* Need commas after the first capture. */
7569 /* Eat the leading `['. */
7570 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7572 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
7574 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
7575 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
7576 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7577 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
7579 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
7581 cp_lexer_consume_token (parser
->lexer
);
7585 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
7587 cp_token
* capture_token
;
7589 tree capture_init_expr
;
7590 cp_id_kind idk
= CP_ID_KIND_NONE
;
7591 bool explicit_init_p
= false;
7593 enum capture_kind_type
7598 enum capture_kind_type capture_kind
= BY_COPY
;
7600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
7602 error ("expected end of capture-list");
7609 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7611 /* Possibly capture `this'. */
7612 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
7614 cp_lexer_consume_token (parser
->lexer
);
7615 add_capture (lambda_expr
,
7616 /*id=*/get_identifier ("__this"),
7617 /*initializer=*/finish_this_expr(),
7618 /*by_reference_p=*/false,
7623 /* Remember whether we want to capture as a reference or not. */
7624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
7626 capture_kind
= BY_REFERENCE
;
7627 cp_lexer_consume_token (parser
->lexer
);
7630 /* Get the identifier. */
7631 capture_token
= cp_lexer_peek_token (parser
->lexer
);
7632 capture_id
= cp_parser_identifier (parser
);
7634 if (capture_id
== error_mark_node
)
7635 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7636 delimiters, but I modified this to stop on unnested ']' as well. It
7637 was already changed to stop on unnested '}', so the
7638 "closing_parenthesis" name is no more misleading with my change. */
7640 cp_parser_skip_to_closing_parenthesis (parser
,
7641 /*recovering=*/true,
7643 /*consume_paren=*/true);
7647 /* Find the initializer for this capture. */
7648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
7650 /* An explicit expression exists. */
7651 cp_lexer_consume_token (parser
->lexer
);
7652 pedwarn (input_location
, OPT_pedantic
,
7653 "ISO C++ does not allow initializers "
7654 "in lambda expression capture lists");
7655 capture_init_expr
= cp_parser_assignment_expression (parser
,
7658 explicit_init_p
= true;
7662 const char* error_msg
;
7664 /* Turn the identifier into an id-expression. */
7666 = cp_parser_lookup_name
7670 /*is_template=*/false,
7671 /*is_namespace=*/false,
7672 /*check_dependency=*/true,
7673 /*ambiguous_decls=*/NULL
,
7674 capture_token
->location
);
7677 = finish_id_expression
7682 /*integral_constant_expression_p=*/false,
7683 /*allow_non_integral_constant_expression_p=*/false,
7684 /*non_integral_constant_expression_p=*/NULL
,
7685 /*template_p=*/false,
7687 /*address_p=*/false,
7688 /*template_arg_p=*/false,
7690 capture_token
->location
);
7693 if (TREE_CODE (capture_init_expr
) == IDENTIFIER_NODE
)
7695 = unqualified_name_lookup_error (capture_init_expr
);
7697 add_capture (lambda_expr
,
7700 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
7704 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7707 /* Parse the (optional) middle of a lambda expression.
7710 ( parameter-declaration-clause [opt] )
7711 attribute-specifier [opt]
7713 exception-specification [opt]
7714 lambda-return-type-clause [opt]
7716 LAMBDA_EXPR is the current representation of the lambda expression. */
7719 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
7721 /* 5.1.1.4 of the standard says:
7722 If a lambda-expression does not include a lambda-declarator, it is as if
7723 the lambda-declarator were ().
7724 This means an empty parameter list, no attributes, and no exception
7726 tree param_list
= void_list_node
;
7727 tree attributes
= NULL_TREE
;
7728 tree exception_spec
= NULL_TREE
;
7731 /* The lambda-declarator is optional, but must begin with an opening
7732 parenthesis if present. */
7733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7735 cp_lexer_consume_token (parser
->lexer
);
7737 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
7739 /* Parse parameters. */
7740 param_list
= cp_parser_parameter_declaration_clause (parser
);
7742 /* Default arguments shall not be specified in the
7743 parameter-declaration-clause of a lambda-declarator. */
7744 for (t
= param_list
; t
; t
= TREE_CHAIN (t
))
7745 if (TREE_PURPOSE (t
))
7746 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_pedantic
,
7747 "default argument specified for lambda parameter");
7749 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7751 attributes
= cp_parser_attributes_opt (parser
);
7753 /* Parse optional `mutable' keyword. */
7754 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
7756 cp_lexer_consume_token (parser
->lexer
);
7757 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
7760 /* Parse optional exception specification. */
7761 exception_spec
= cp_parser_exception_specification_opt (parser
);
7763 /* Parse optional trailing return type. */
7764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
7766 cp_lexer_consume_token (parser
->lexer
);
7767 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
) = cp_parser_type_id (parser
);
7770 /* The function parameters must be in scope all the way until after the
7771 trailing-return-type in case of decltype. */
7772 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
7773 pop_binding (DECL_NAME (t
), t
);
7778 /* Create the function call operator.
7780 Messing with declarators like this is no uglier than building up the
7781 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7784 cp_decl_specifier_seq return_type_specs
;
7785 cp_declarator
* declarator
;
7790 clear_decl_specs (&return_type_specs
);
7791 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7792 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
7794 /* Maybe we will deduce the return type later, but we can use void
7795 as a placeholder return type anyways. */
7796 return_type_specs
.type
= void_type_node
;
7798 p
= obstack_alloc (&declarator_obstack
, 0);
7800 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
7803 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
7804 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
7805 declarator
= make_call_declarator (declarator
, param_list
, quals
,
7807 /*late_return_type=*/NULL_TREE
);
7808 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
7810 fco
= grokmethod (&return_type_specs
,
7813 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
7814 DECL_ARTIFICIAL (fco
) = 1;
7816 finish_member_declaration (fco
);
7818 obstack_free (&declarator_obstack
, p
);
7822 /* Parse the body of a lambda expression, which is simply
7826 but which requires special handling.
7827 LAMBDA_EXPR is the current representation of the lambda expression. */
7830 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
7832 bool nested
= (current_function_decl
!= NULL_TREE
);
7834 push_function_context ();
7836 /* Finish the function call operator
7838 + late_parsing_for_member
7839 + function_definition_after_declarator
7840 + ctor_initializer_opt_and_function_body */
7842 tree fco
= lambda_function (lambda_expr
);
7846 /* Let the front end know that we are going to be defining this
7848 start_preparsed_function (fco
,
7850 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
7852 start_lambda_scope (fco
);
7853 body
= begin_function_body ();
7855 /* 5.1.1.4 of the standard says:
7856 If a lambda-expression does not include a trailing-return-type, it
7857 is as if the trailing-return-type denotes the following type:
7858 * if the compound-statement is of the form
7859 { return attribute-specifier [opt] expression ; }
7860 the type of the returned expression after lvalue-to-rvalue
7861 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7862 (_conv.array_ 4.2), and function-to-pointer conversion
7864 * otherwise, void. */
7866 /* In a lambda that has neither a lambda-return-type-clause
7867 nor a deducible form, errors should be reported for return statements
7868 in the body. Since we used void as the placeholder return type, parsing
7869 the body as usual will give such desired behavior. */
7870 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
7871 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
7872 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
== RID_RETURN
7873 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_SEMICOLON
)
7876 tree expr
= NULL_TREE
;
7877 cp_id_kind idk
= CP_ID_KIND_NONE
;
7879 /* Parse tentatively in case there's more after the initial return
7881 cp_parser_parse_tentatively (parser
);
7883 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
7884 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
7886 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
7888 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
7889 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
7891 if (cp_parser_parse_definitely (parser
))
7893 apply_lambda_return_type (lambda_expr
, lambda_return_type (expr
));
7895 compound_stmt
= begin_compound_stmt (0);
7896 /* Will get error here if type not deduced yet. */
7897 finish_return_stmt (expr
);
7898 finish_compound_stmt (compound_stmt
);
7906 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
7907 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = true;
7908 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7909 cp_parser_compound_stmt does not pass it. */
7910 cp_parser_function_body (parser
);
7911 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr
) = false;
7914 finish_function_body (body
);
7915 finish_lambda_scope ();
7917 /* Finish the function and generate code for it if necessary. */
7918 expand_or_defer_fn (finish_function (/*inline*/2));
7922 pop_function_context();
7925 /* Statements [gram.stmt.stmt] */
7927 /* Parse a statement.
7931 expression-statement
7936 declaration-statement
7939 IN_COMPOUND is true when the statement is nested inside a
7940 cp_parser_compound_statement; this matters for certain pragmas.
7942 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7943 is a (possibly labeled) if statement which is not enclosed in braces
7944 and has an else clause. This is used to implement -Wparentheses. */
7947 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
7948 bool in_compound
, bool *if_p
)
7952 location_t statement_location
;
7957 /* There is no statement yet. */
7958 statement
= NULL_TREE
;
7959 /* Peek at the next token. */
7960 token
= cp_lexer_peek_token (parser
->lexer
);
7961 /* Remember the location of the first token in the statement. */
7962 statement_location
= token
->location
;
7963 /* If this is a keyword, then that will often determine what kind of
7964 statement we have. */
7965 if (token
->type
== CPP_KEYWORD
)
7967 enum rid keyword
= token
->keyword
;
7973 /* Looks like a labeled-statement with a case label.
7974 Parse the label, and then use tail recursion to parse
7976 cp_parser_label_for_labeled_statement (parser
);
7981 statement
= cp_parser_selection_statement (parser
, if_p
);
7987 statement
= cp_parser_iteration_statement (parser
);
7994 statement
= cp_parser_jump_statement (parser
);
7997 /* Objective-C++ exception-handling constructs. */
8000 case RID_AT_FINALLY
:
8001 case RID_AT_SYNCHRONIZED
:
8003 statement
= cp_parser_objc_statement (parser
);
8007 statement
= cp_parser_try_block (parser
);
8011 /* This must be a namespace alias definition. */
8012 cp_parser_declaration_statement (parser
);
8016 /* It might be a keyword like `int' that can start a
8017 declaration-statement. */
8021 else if (token
->type
== CPP_NAME
)
8023 /* If the next token is a `:', then we are looking at a
8024 labeled-statement. */
8025 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8026 if (token
->type
== CPP_COLON
)
8028 /* Looks like a labeled-statement with an ordinary label.
8029 Parse the label, and then use tail recursion to parse
8031 cp_parser_label_for_labeled_statement (parser
);
8035 /* Anything that starts with a `{' must be a compound-statement. */
8036 else if (token
->type
== CPP_OPEN_BRACE
)
8037 statement
= cp_parser_compound_statement (parser
, NULL
, false);
8038 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8039 a statement all its own. */
8040 else if (token
->type
== CPP_PRAGMA
)
8042 /* Only certain OpenMP pragmas are attached to statements, and thus
8043 are considered statements themselves. All others are not. In
8044 the context of a compound, accept the pragma as a "statement" and
8045 return so that we can check for a close brace. Otherwise we
8046 require a real statement and must go back and read one. */
8048 cp_parser_pragma (parser
, pragma_compound
);
8049 else if (!cp_parser_pragma (parser
, pragma_stmt
))
8053 else if (token
->type
== CPP_EOF
)
8055 cp_parser_error (parser
, "expected statement");
8059 /* Everything else must be a declaration-statement or an
8060 expression-statement. Try for the declaration-statement
8061 first, unless we are looking at a `;', in which case we know that
8062 we have an expression-statement. */
8065 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8067 cp_parser_parse_tentatively (parser
);
8068 /* Try to parse the declaration-statement. */
8069 cp_parser_declaration_statement (parser
);
8070 /* If that worked, we're done. */
8071 if (cp_parser_parse_definitely (parser
))
8074 /* Look for an expression-statement instead. */
8075 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
8078 /* Set the line number for the statement. */
8079 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
8080 SET_EXPR_LOCATION (statement
, statement_location
);
8083 /* Parse the label for a labeled-statement, i.e.
8086 case constant-expression :
8090 case constant-expression ... constant-expression : statement
8092 When a label is parsed without errors, the label is added to the
8093 parse tree by the finish_* functions, so this function doesn't
8094 have to return the label. */
8097 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
8100 tree label
= NULL_TREE
;
8102 /* The next token should be an identifier. */
8103 token
= cp_lexer_peek_token (parser
->lexer
);
8104 if (token
->type
!= CPP_NAME
8105 && token
->type
!= CPP_KEYWORD
)
8107 cp_parser_error (parser
, "expected labeled-statement");
8111 switch (token
->keyword
)
8118 /* Consume the `case' token. */
8119 cp_lexer_consume_token (parser
->lexer
);
8120 /* Parse the constant-expression. */
8121 expr
= cp_parser_constant_expression (parser
,
8122 /*allow_non_constant_p=*/false,
8125 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
8126 if (ellipsis
->type
== CPP_ELLIPSIS
)
8128 /* Consume the `...' token. */
8129 cp_lexer_consume_token (parser
->lexer
);
8131 cp_parser_constant_expression (parser
,
8132 /*allow_non_constant_p=*/false,
8134 /* We don't need to emit warnings here, as the common code
8135 will do this for us. */
8138 expr_hi
= NULL_TREE
;
8140 if (parser
->in_switch_statement_p
)
8141 finish_case_label (token
->location
, expr
, expr_hi
);
8143 error_at (token
->location
,
8144 "case label %qE not within a switch statement",
8150 /* Consume the `default' token. */
8151 cp_lexer_consume_token (parser
->lexer
);
8153 if (parser
->in_switch_statement_p
)
8154 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
8156 error_at (token
->location
, "case label not within a switch statement");
8160 /* Anything else must be an ordinary label. */
8161 label
= finish_label_stmt (cp_parser_identifier (parser
));
8165 /* Require the `:' token. */
8166 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8168 /* An ordinary label may optionally be followed by attributes.
8169 However, this is only permitted if the attributes are then
8170 followed by a semicolon. This is because, for backward
8171 compatibility, when parsing
8172 lab: __attribute__ ((unused)) int i;
8173 we want the attribute to attach to "i", not "lab". */
8174 if (label
!= NULL_TREE
8175 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
8179 cp_parser_parse_tentatively (parser
);
8180 attrs
= cp_parser_attributes_opt (parser
);
8181 if (attrs
== NULL_TREE
8182 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8183 cp_parser_abort_tentative_parse (parser
);
8184 else if (!cp_parser_parse_definitely (parser
))
8187 cplus_decl_attributes (&label
, attrs
, 0);
8191 /* Parse an expression-statement.
8193 expression-statement:
8196 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8197 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8198 indicates whether this expression-statement is part of an
8199 expression statement. */
8202 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
8204 tree statement
= NULL_TREE
;
8205 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8207 /* If the next token is a ';', then there is no expression
8209 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8210 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8212 /* Give a helpful message for "A<T>::type t;" and the like. */
8213 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
8214 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
8216 if (TREE_CODE (statement
) == SCOPE_REF
)
8217 error_at (token
->location
, "need %<typename%> before %qE because "
8218 "%qT is a dependent scope",
8219 statement
, TREE_OPERAND (statement
, 0));
8220 else if (is_overloaded_fn (statement
)
8221 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
8224 tree fn
= get_first_fn (statement
);
8225 error_at (token
->location
,
8226 "%<%T::%D%> names the constructor, not the type",
8227 DECL_CONTEXT (fn
), DECL_NAME (fn
));
8231 /* Consume the final `;'. */
8232 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8234 if (in_statement_expr
8235 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
8236 /* This is the final expression statement of a statement
8238 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
8240 statement
= finish_expr_stmt (statement
);
8247 /* Parse a compound-statement.
8250 { statement-seq [opt] }
8255 { label-declaration-seq [opt] statement-seq [opt] }
8257 label-declaration-seq:
8259 label-declaration-seq label-declaration
8261 Returns a tree representing the statement. */
8264 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
8269 /* Consume the `{'. */
8270 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8271 return error_mark_node
;
8272 /* Begin the compound-statement. */
8273 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
8274 /* If the next keyword is `__label__' we have a label declaration. */
8275 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8276 cp_parser_label_declaration (parser
);
8277 /* Parse an (optional) statement-seq. */
8278 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
8279 /* Finish the compound-statement. */
8280 finish_compound_stmt (compound_stmt
);
8281 /* Consume the `}'. */
8282 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8284 return compound_stmt
;
8287 /* Parse an (optional) statement-seq.
8291 statement-seq [opt] statement */
8294 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
8296 /* Scan statements until there aren't any more. */
8299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8301 /* If we are looking at a `}', then we have run out of
8302 statements; the same is true if we have reached the end
8303 of file, or have stumbled upon a stray '@end'. */
8304 if (token
->type
== CPP_CLOSE_BRACE
8305 || token
->type
== CPP_EOF
8306 || token
->type
== CPP_PRAGMA_EOL
8307 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
8310 /* If we are in a compound statement and find 'else' then
8311 something went wrong. */
8312 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
8314 if (parser
->in_statement
& IN_IF_STMT
)
8318 token
= cp_lexer_consume_token (parser
->lexer
);
8319 error_at (token
->location
, "%<else%> without a previous %<if%>");
8323 /* Parse the statement. */
8324 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
8328 /* Parse a selection-statement.
8330 selection-statement:
8331 if ( condition ) statement
8332 if ( condition ) statement else statement
8333 switch ( condition ) statement
8335 Returns the new IF_STMT or SWITCH_STMT.
8337 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8338 is a (possibly labeled) if statement which is not enclosed in
8339 braces and has an else clause. This is used to implement
8343 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
8351 /* Peek at the next token. */
8352 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
8354 /* See what kind of keyword it is. */
8355 keyword
= token
->keyword
;
8364 /* Look for the `('. */
8365 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
8367 cp_parser_skip_to_end_of_statement (parser
);
8368 return error_mark_node
;
8371 /* Begin the selection-statement. */
8372 if (keyword
== RID_IF
)
8373 statement
= begin_if_stmt ();
8375 statement
= begin_switch_stmt ();
8377 /* Parse the condition. */
8378 condition
= cp_parser_condition (parser
);
8379 /* Look for the `)'. */
8380 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
8381 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
8382 /*consume_paren=*/true);
8384 if (keyword
== RID_IF
)
8387 unsigned char in_statement
;
8389 /* Add the condition. */
8390 finish_if_stmt_cond (condition
, statement
);
8392 /* Parse the then-clause. */
8393 in_statement
= parser
->in_statement
;
8394 parser
->in_statement
|= IN_IF_STMT
;
8395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8397 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8398 add_stmt (build_empty_stmt (loc
));
8399 cp_lexer_consume_token (parser
->lexer
);
8400 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
8401 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
8402 "empty body in an %<if%> statement");
8406 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
8407 parser
->in_statement
= in_statement
;
8409 finish_then_clause (statement
);
8411 /* If the next token is `else', parse the else-clause. */
8412 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
8415 /* Consume the `else' keyword. */
8416 cp_lexer_consume_token (parser
->lexer
);
8417 begin_else_clause (statement
);
8418 /* Parse the else-clause. */
8419 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
8422 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8424 OPT_Wempty_body
, "suggest braces around "
8425 "empty body in an %<else%> statement");
8426 add_stmt (build_empty_stmt (loc
));
8427 cp_lexer_consume_token (parser
->lexer
);
8430 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8432 finish_else_clause (statement
);
8434 /* If we are currently parsing a then-clause, then
8435 IF_P will not be NULL. We set it to true to
8436 indicate that this if statement has an else clause.
8437 This may trigger the Wparentheses warning below
8438 when we get back up to the parent if statement. */
8444 /* This if statement does not have an else clause. If
8445 NESTED_IF is true, then the then-clause is an if
8446 statement which does have an else clause. We warn
8447 about the potential ambiguity. */
8449 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
8450 "suggest explicit braces to avoid ambiguous"
8454 /* Now we're all done with the if-statement. */
8455 finish_if_stmt (statement
);
8459 bool in_switch_statement_p
;
8460 unsigned char in_statement
;
8462 /* Add the condition. */
8463 finish_switch_cond (condition
, statement
);
8465 /* Parse the body of the switch-statement. */
8466 in_switch_statement_p
= parser
->in_switch_statement_p
;
8467 in_statement
= parser
->in_statement
;
8468 parser
->in_switch_statement_p
= true;
8469 parser
->in_statement
|= IN_SWITCH_STMT
;
8470 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8471 parser
->in_switch_statement_p
= in_switch_statement_p
;
8472 parser
->in_statement
= in_statement
;
8474 /* Now we're all done with the switch-statement. */
8475 finish_switch_stmt (statement
);
8483 cp_parser_error (parser
, "expected selection-statement");
8484 return error_mark_node
;
8488 /* Parse a condition.
8492 type-specifier-seq declarator = initializer-clause
8493 type-specifier-seq declarator braced-init-list
8498 type-specifier-seq declarator asm-specification [opt]
8499 attributes [opt] = assignment-expression
8501 Returns the expression that should be tested. */
8504 cp_parser_condition (cp_parser
* parser
)
8506 cp_decl_specifier_seq type_specifiers
;
8507 const char *saved_message
;
8509 /* Try the declaration first. */
8510 cp_parser_parse_tentatively (parser
);
8511 /* New types are not allowed in the type-specifier-seq for a
8513 saved_message
= parser
->type_definition_forbidden_message
;
8514 parser
->type_definition_forbidden_message
8515 = G_("types may not be defined in conditions");
8516 /* Parse the type-specifier-seq. */
8517 cp_parser_type_specifier_seq (parser
, /*is_declaration==*/true,
8518 /*is_trailing_return=*/false,
8520 /* Restore the saved message. */
8521 parser
->type_definition_forbidden_message
= saved_message
;
8522 /* If all is well, we might be looking at a declaration. */
8523 if (!cp_parser_error_occurred (parser
))
8526 tree asm_specification
;
8528 cp_declarator
*declarator
;
8529 tree initializer
= NULL_TREE
;
8531 /* Parse the declarator. */
8532 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8533 /*ctor_dtor_or_conv_p=*/NULL
,
8534 /*parenthesized_p=*/NULL
,
8535 /*member_p=*/false);
8536 /* Parse the attributes. */
8537 attributes
= cp_parser_attributes_opt (parser
);
8538 /* Parse the asm-specification. */
8539 asm_specification
= cp_parser_asm_specification_opt (parser
);
8540 /* If the next token is not an `=' or '{', then we might still be
8541 looking at an expression. For example:
8545 looks like a decl-specifier-seq and a declarator -- but then
8546 there is no `=', so this is an expression. */
8547 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
8548 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
8549 cp_parser_simulate_error (parser
);
8551 /* If we did see an `=' or '{', then we are looking at a declaration
8553 if (cp_parser_parse_definitely (parser
))
8556 bool non_constant_p
;
8557 bool flags
= LOOKUP_ONLYCONVERTING
;
8559 /* Create the declaration. */
8560 decl
= start_decl (declarator
, &type_specifiers
,
8561 /*initialized_p=*/true,
8562 attributes
, /*prefix_attributes=*/NULL_TREE
,
8565 /* Parse the initializer. */
8566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8568 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
8569 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
8574 /* Consume the `='. */
8575 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
8576 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8578 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
8579 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8581 if (!non_constant_p
)
8582 initializer
= fold_non_dependent_expr (initializer
);
8584 /* Process the initializer. */
8585 cp_finish_decl (decl
,
8586 initializer
, !non_constant_p
,
8591 pop_scope (pushed_scope
);
8593 return convert_from_reference (decl
);
8596 /* If we didn't even get past the declarator successfully, we are
8597 definitely not looking at a declaration. */
8599 cp_parser_abort_tentative_parse (parser
);
8601 /* Otherwise, we are looking at an expression. */
8602 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8605 /* Parses a traditional for-statement until the closing ')', not included. */
8608 cp_parser_c_for (cp_parser
*parser
)
8610 /* Normal for loop */
8612 tree condition
= NULL_TREE
;
8613 tree expression
= NULL_TREE
;
8615 /* Begin the for-statement. */
8616 stmt
= begin_for_stmt ();
8618 /* Parse the initialization. */
8619 cp_parser_for_init_statement (parser
);
8620 finish_for_init_stmt (stmt
);
8622 /* If there's a condition, process it. */
8623 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8624 condition
= cp_parser_condition (parser
);
8625 finish_for_cond (condition
, stmt
);
8626 /* Look for the `;'. */
8627 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8629 /* If there's an expression, process it. */
8630 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8631 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8632 finish_for_expr (expression
, stmt
);
8637 /* Tries to parse a range-based for-statement:
8640 type-specifier-seq declarator : expression
8642 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8643 expression. Note that the *DECL is returned unfinished, so
8644 later you should call cp_finish_decl().
8646 Returns TRUE iff a range-based for is parsed. */
8649 cp_parser_range_for (cp_parser
*parser
)
8651 tree stmt
, range_decl
, range_expr
;
8652 cp_decl_specifier_seq type_specifiers
;
8653 cp_declarator
*declarator
;
8654 const char *saved_message
;
8655 tree attributes
, pushed_scope
;
8657 cp_parser_parse_tentatively (parser
);
8658 /* New types are not allowed in the type-specifier-seq for a
8659 range-based for loop. */
8660 saved_message
= parser
->type_definition_forbidden_message
;
8661 parser
->type_definition_forbidden_message
8662 = G_("types may not be defined in range-based for loops");
8663 /* Parse the type-specifier-seq. */
8664 cp_parser_type_specifier_seq (parser
, /*is_declaration==*/true,
8665 /*is_trailing_return=*/false,
8667 /* Restore the saved message. */
8668 parser
->type_definition_forbidden_message
= saved_message
;
8669 /* If all is well, we might be looking at a declaration. */
8670 if (cp_parser_error_occurred (parser
))
8672 cp_parser_abort_tentative_parse (parser
);
8675 /* Parse the declarator. */
8676 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
8677 /*ctor_dtor_or_conv_p=*/NULL
,
8678 /*parenthesized_p=*/NULL
,
8679 /*member_p=*/false);
8680 /* Parse the attributes. */
8681 attributes
= cp_parser_attributes_opt (parser
);
8682 /* The next token should be `:'. */
8683 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
8684 cp_parser_simulate_error (parser
);
8686 /* Check if it is a range-based for */
8687 if (!cp_parser_parse_definitely (parser
))
8690 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8693 bool expr_non_constant_p
;
8694 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8697 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8699 /* If in template, STMT is converted to a normal for-statements
8700 at instantiation. If not, it is done just ahead. */
8701 if (processing_template_decl
)
8702 stmt
= begin_range_for_stmt ();
8704 stmt
= begin_for_stmt ();
8706 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8707 range_decl
= start_decl (declarator
, &type_specifiers
,
8708 /*initialized_p=*/SD_INITIALIZED
,
8709 attributes
, /*prefix_attributes=*/NULL_TREE
,
8711 /* No scope allowed here */
8712 pop_scope (pushed_scope
);
8714 if (TREE_CODE (stmt
) == RANGE_FOR_STMT
)
8715 finish_range_for_decl (stmt
, range_decl
, range_expr
);
8717 /* Convert the range-based for loop into a normal for-statement. */
8718 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
);
8723 /* Converts a range-based for-statement into a normal
8724 for-statement, as per the definition.
8726 for (RANGE_DECL : RANGE_EXPR)
8729 should be equivalent to:
8732 auto &&__range = RANGE_EXPR;
8733 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8737 RANGE_DECL = *__begin;
8742 If RANGE_EXPR is an array:
8743 BEGIN_EXPR = __range
8744 END_EXPR = __range + ARRAY_SIZE(__range)
8746 BEGIN_EXPR = begin(__range)
8747 END_EXPR = end(__range);
8749 When calling begin()/end() we must use argument dependent
8750 lookup, but always considering 'std' as an associated namespace. */
8753 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
)
8755 tree range_type
, range_temp
;
8757 tree iter_type
, begin_expr
, end_expr
;
8758 tree condition
, expression
;
8760 /* Find out the type deduced by the declaration
8761 * `auto &&__range = range_expr' */
8762 range_type
= cp_build_reference_type (make_auto (), true);
8763 range_type
= do_auto_deduction (range_type
, range_expr
,
8764 type_uses_auto (range_type
));
8766 /* Create the __range variable */
8767 range_temp
= build_decl (input_location
, VAR_DECL
,
8768 get_identifier ("__for_range"), range_type
);
8769 TREE_USED (range_temp
) = 1;
8770 DECL_ARTIFICIAL (range_temp
) = 1;
8771 pushdecl (range_temp
);
8772 finish_expr_stmt (cp_build_modify_expr (range_temp
, INIT_EXPR
, range_expr
,
8773 tf_warning_or_error
));
8774 range_temp
= convert_from_reference (range_temp
);
8776 if (TREE_CODE (TREE_TYPE (range_temp
)) == ARRAY_TYPE
)
8778 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8779 iter_type
= build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp
)));
8780 begin_expr
= range_temp
;
8782 = build_binary_op (input_location
, PLUS_EXPR
,
8784 array_type_nelts_top (TREE_TYPE (range_temp
)), 0);
8788 /* If it is not an array, we must call begin(__range)/end__range() */
8791 begin_expr
= get_identifier ("begin");
8792 vec
= make_tree_vector ();
8793 VEC_safe_push (tree
, gc
, vec
, range_temp
);
8794 begin_expr
= perform_koenig_lookup (begin_expr
, vec
,
8795 /*include_std=*/true);
8796 begin_expr
= finish_call_expr (begin_expr
, &vec
, false, true,
8797 tf_warning_or_error
);
8798 release_tree_vector (vec
);
8800 end_expr
= get_identifier ("end");
8801 vec
= make_tree_vector ();
8802 VEC_safe_push (tree
, gc
, vec
, range_temp
);
8803 end_expr
= perform_koenig_lookup (end_expr
, vec
,
8804 /*include_std=*/true);
8805 end_expr
= finish_call_expr (end_expr
, &vec
, false, true,
8806 tf_warning_or_error
);
8807 release_tree_vector (vec
);
8809 /* The unqualified type of the __begin and __end temporaries should
8810 * be the same as required by the multiple auto declaration */
8811 iter_type
= cv_unqualified (TREE_TYPE (begin_expr
));
8812 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (end_expr
))))
8813 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8814 TREE_TYPE (begin_expr
), TREE_TYPE (end_expr
));
8817 /* The new for initialization statement */
8818 begin
= build_decl (input_location
, VAR_DECL
,
8819 get_identifier ("__for_begin"), iter_type
);
8820 TREE_USED (begin
) = 1;
8821 DECL_ARTIFICIAL (begin
) = 1;
8823 finish_expr_stmt (cp_build_modify_expr (begin
, INIT_EXPR
, begin_expr
,
8824 tf_warning_or_error
));
8825 end
= build_decl (input_location
, VAR_DECL
,
8826 get_identifier ("__for_end"), iter_type
);
8827 TREE_USED (end
) = 1;
8828 DECL_ARTIFICIAL (end
) = 1;
8831 finish_expr_stmt (cp_build_modify_expr (end
, INIT_EXPR
, end_expr
,
8832 tf_warning_or_error
));
8834 finish_for_init_stmt (statement
);
8836 /* The new for condition */
8837 condition
= build_x_binary_op (NE_EXPR
,
8840 NULL
, tf_warning_or_error
);
8841 finish_for_cond (condition
, statement
);
8843 /* The new increment expression */
8844 expression
= finish_unary_op_expr (PREINCREMENT_EXPR
, begin
);
8845 finish_for_expr (expression
, statement
);
8847 /* The declaration is initialized with *__begin inside the loop body */
8848 cp_finish_decl (range_decl
,
8849 build_x_indirect_ref (begin
, RO_NULL
, tf_warning_or_error
),
8850 /*is_constant_init*/false, NULL_TREE
,
8851 LOOKUP_ONLYCONVERTING
);
8857 /* Parse an iteration-statement.
8859 iteration-statement:
8860 while ( condition ) statement
8861 do statement while ( expression ) ;
8862 for ( for-init-statement condition [opt] ; expression [opt] )
8865 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8868 cp_parser_iteration_statement (cp_parser
* parser
)
8873 unsigned char in_statement
;
8875 /* Peek at the next token. */
8876 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
8878 return error_mark_node
;
8880 /* Remember whether or not we are already within an iteration
8882 in_statement
= parser
->in_statement
;
8884 /* See what kind of keyword it is. */
8885 keyword
= token
->keyword
;
8892 /* Begin the while-statement. */
8893 statement
= begin_while_stmt ();
8894 /* Look for the `('. */
8895 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8896 /* Parse the condition. */
8897 condition
= cp_parser_condition (parser
);
8898 finish_while_stmt_cond (condition
, statement
);
8899 /* Look for the `)'. */
8900 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8901 /* Parse the dependent statement. */
8902 parser
->in_statement
= IN_ITERATION_STMT
;
8903 cp_parser_already_scoped_statement (parser
);
8904 parser
->in_statement
= in_statement
;
8905 /* We're done with the while-statement. */
8906 finish_while_stmt (statement
);
8914 /* Begin the do-statement. */
8915 statement
= begin_do_stmt ();
8916 /* Parse the body of the do-statement. */
8917 parser
->in_statement
= IN_ITERATION_STMT
;
8918 cp_parser_implicitly_scoped_statement (parser
, NULL
);
8919 parser
->in_statement
= in_statement
;
8920 finish_do_body (statement
);
8921 /* Look for the `while' keyword. */
8922 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
8923 /* Look for the `('. */
8924 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8925 /* Parse the expression. */
8926 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8927 /* We're done with the do-statement. */
8928 finish_do_stmt (expression
, statement
);
8929 /* Look for the `)'. */
8930 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8931 /* Look for the `;'. */
8932 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8938 /* Look for the `('. */
8939 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8941 if (cxx_dialect
== cxx0x
)
8942 statement
= cp_parser_range_for (parser
);
8944 statement
= NULL_TREE
;
8945 if (statement
== NULL_TREE
)
8946 statement
= cp_parser_c_for (parser
);
8948 /* Look for the `)'. */
8949 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8951 /* Parse the body of the for-statement. */
8952 parser
->in_statement
= IN_ITERATION_STMT
;
8953 cp_parser_already_scoped_statement (parser
);
8954 parser
->in_statement
= in_statement
;
8956 /* We're done with the for-statement. */
8957 finish_for_stmt (statement
);
8962 cp_parser_error (parser
, "expected iteration-statement");
8963 statement
= error_mark_node
;
8970 /* Parse a for-init-statement.
8973 expression-statement
8974 simple-declaration */
8977 cp_parser_for_init_statement (cp_parser
* parser
)
8979 /* If the next token is a `;', then we have an empty
8980 expression-statement. Grammatically, this is also a
8981 simple-declaration, but an invalid one, because it does not
8982 declare anything. Therefore, if we did not handle this case
8983 specially, we would issue an error message about an invalid
8985 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8987 /* We're going to speculatively look for a declaration, falling back
8988 to an expression, if necessary. */
8989 cp_parser_parse_tentatively (parser
);
8990 /* Parse the declaration. */
8991 cp_parser_simple_declaration (parser
,
8992 /*function_definition_allowed_p=*/false);
8993 /* If the tentative parse failed, then we shall need to look for an
8994 expression-statement. */
8995 if (cp_parser_parse_definitely (parser
))
8999 cp_parser_expression_statement (parser
, NULL_TREE
);
9002 /* Parse a jump-statement.
9007 return expression [opt] ;
9008 return braced-init-list ;
9016 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9019 cp_parser_jump_statement (cp_parser
* parser
)
9021 tree statement
= error_mark_node
;
9024 unsigned char in_statement
;
9026 /* Peek at the next token. */
9027 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
9029 return error_mark_node
;
9031 /* See what kind of keyword it is. */
9032 keyword
= token
->keyword
;
9036 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
9037 switch (in_statement
)
9040 error_at (token
->location
, "break statement not within loop or switch");
9043 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
9044 || in_statement
== IN_ITERATION_STMT
);
9045 statement
= finish_break_stmt ();
9048 error_at (token
->location
, "invalid exit from OpenMP structured block");
9051 error_at (token
->location
, "break statement used with OpenMP for loop");
9054 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9058 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
9061 error_at (token
->location
, "continue statement not within a loop");
9063 case IN_ITERATION_STMT
:
9065 statement
= finish_continue_stmt ();
9068 error_at (token
->location
, "invalid exit from OpenMP structured block");
9073 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9079 bool expr_non_constant_p
;
9081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9083 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9084 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9086 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9087 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9089 /* If the next token is a `;', then there is no
9092 /* Build the return-statement. */
9093 statement
= finish_return_stmt (expr
);
9094 /* Look for the final `;'. */
9095 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9100 /* Create the goto-statement. */
9101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
9103 /* Issue a warning about this use of a GNU extension. */
9104 pedwarn (token
->location
, OPT_pedantic
, "ISO C++ forbids computed gotos");
9105 /* Consume the '*' token. */
9106 cp_lexer_consume_token (parser
->lexer
);
9107 /* Parse the dependent expression. */
9108 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
9111 finish_goto_stmt (cp_parser_identifier (parser
));
9112 /* Look for the final `;'. */
9113 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9117 cp_parser_error (parser
, "expected jump-statement");
9124 /* Parse a declaration-statement.
9126 declaration-statement:
9127 block-declaration */
9130 cp_parser_declaration_statement (cp_parser
* parser
)
9134 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9135 p
= obstack_alloc (&declarator_obstack
, 0);
9137 /* Parse the block-declaration. */
9138 cp_parser_block_declaration (parser
, /*statement_p=*/true);
9140 /* Free any declarators allocated. */
9141 obstack_free (&declarator_obstack
, p
);
9143 /* Finish off the statement. */
9147 /* Some dependent statements (like `if (cond) statement'), are
9148 implicitly in their own scope. In other words, if the statement is
9149 a single statement (as opposed to a compound-statement), it is
9150 none-the-less treated as if it were enclosed in braces. Any
9151 declarations appearing in the dependent statement are out of scope
9152 after control passes that point. This function parses a statement,
9153 but ensures that is in its own scope, even if it is not a
9156 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9157 is a (possibly labeled) if statement which is not enclosed in
9158 braces and has an else clause. This is used to implement
9161 Returns the new statement. */
9164 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
9171 /* Mark if () ; with a special NOP_EXPR. */
9172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9174 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9175 cp_lexer_consume_token (parser
->lexer
);
9176 statement
= add_stmt (build_empty_stmt (loc
));
9178 /* if a compound is opened, we simply parse the statement directly. */
9179 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9180 statement
= cp_parser_compound_statement (parser
, NULL
, false);
9181 /* If the token is not a `{', then we must take special action. */
9184 /* Create a compound-statement. */
9185 statement
= begin_compound_stmt (0);
9186 /* Parse the dependent-statement. */
9187 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
9188 /* Finish the dummy compound-statement. */
9189 finish_compound_stmt (statement
);
9192 /* Return the statement. */
9196 /* For some dependent statements (like `while (cond) statement'), we
9197 have already created a scope. Therefore, even if the dependent
9198 statement is a compound-statement, we do not want to create another
9202 cp_parser_already_scoped_statement (cp_parser
* parser
)
9204 /* If the token is a `{', then we must take special action. */
9205 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9206 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
9209 /* Avoid calling cp_parser_compound_statement, so that we
9210 don't create a new scope. Do everything else by hand. */
9211 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
9212 /* If the next keyword is `__label__' we have a label declaration. */
9213 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9214 cp_parser_label_declaration (parser
);
9215 /* Parse an (optional) statement-seq. */
9216 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9217 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9221 /* Declarations [gram.dcl.dcl] */
9223 /* Parse an optional declaration-sequence.
9227 declaration-seq declaration */
9230 cp_parser_declaration_seq_opt (cp_parser
* parser
)
9236 token
= cp_lexer_peek_token (parser
->lexer
);
9238 if (token
->type
== CPP_CLOSE_BRACE
9239 || token
->type
== CPP_EOF
9240 || token
->type
== CPP_PRAGMA_EOL
)
9243 if (token
->type
== CPP_SEMICOLON
)
9245 /* A declaration consisting of a single semicolon is
9246 invalid. Allow it unless we're being pedantic. */
9247 cp_lexer_consume_token (parser
->lexer
);
9248 if (!in_system_header
)
9249 pedwarn (input_location
, OPT_pedantic
, "extra %<;%>");
9253 /* If we're entering or exiting a region that's implicitly
9254 extern "C", modify the lang context appropriately. */
9255 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
9257 push_lang_context (lang_name_c
);
9258 parser
->implicit_extern_c
= true;
9260 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
9262 pop_lang_context ();
9263 parser
->implicit_extern_c
= false;
9266 if (token
->type
== CPP_PRAGMA
)
9268 /* A top-level declaration can consist solely of a #pragma.
9269 A nested declaration cannot, so this is done here and not
9270 in cp_parser_declaration. (A #pragma at block scope is
9271 handled in cp_parser_statement.) */
9272 cp_parser_pragma (parser
, pragma_external
);
9276 /* Parse the declaration itself. */
9277 cp_parser_declaration (parser
);
9281 /* Parse a declaration.
9286 template-declaration
9287 explicit-instantiation
9288 explicit-specialization
9289 linkage-specification
9290 namespace-definition
9295 __extension__ declaration */
9298 cp_parser_declaration (cp_parser
* parser
)
9304 tree attributes
= NULL_TREE
;
9306 /* Check for the `__extension__' keyword. */
9307 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
9309 /* Parse the qualified declaration. */
9310 cp_parser_declaration (parser
);
9311 /* Restore the PEDANTIC flag. */
9312 pedantic
= saved_pedantic
;
9317 /* Try to figure out what kind of declaration is present. */
9318 token1
= *cp_lexer_peek_token (parser
->lexer
);
9320 if (token1
.type
!= CPP_EOF
)
9321 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
9324 token2
.type
= CPP_EOF
;
9325 token2
.keyword
= RID_MAX
;
9328 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9329 p
= obstack_alloc (&declarator_obstack
, 0);
9331 /* If the next token is `extern' and the following token is a string
9332 literal, then we have a linkage specification. */
9333 if (token1
.keyword
== RID_EXTERN
9334 && cp_parser_is_string_literal (&token2
))
9335 cp_parser_linkage_specification (parser
);
9336 /* If the next token is `template', then we have either a template
9337 declaration, an explicit instantiation, or an explicit
9339 else if (token1
.keyword
== RID_TEMPLATE
)
9341 /* `template <>' indicates a template specialization. */
9342 if (token2
.type
== CPP_LESS
9343 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
9344 cp_parser_explicit_specialization (parser
);
9345 /* `template <' indicates a template declaration. */
9346 else if (token2
.type
== CPP_LESS
)
9347 cp_parser_template_declaration (parser
, /*member_p=*/false);
9348 /* Anything else must be an explicit instantiation. */
9350 cp_parser_explicit_instantiation (parser
);
9352 /* If the next token is `export', then we have a template
9354 else if (token1
.keyword
== RID_EXPORT
)
9355 cp_parser_template_declaration (parser
, /*member_p=*/false);
9356 /* If the next token is `extern', 'static' or 'inline' and the one
9357 after that is `template', we have a GNU extended explicit
9358 instantiation directive. */
9359 else if (cp_parser_allow_gnu_extensions_p (parser
)
9360 && (token1
.keyword
== RID_EXTERN
9361 || token1
.keyword
== RID_STATIC
9362 || token1
.keyword
== RID_INLINE
)
9363 && token2
.keyword
== RID_TEMPLATE
)
9364 cp_parser_explicit_instantiation (parser
);
9365 /* If the next token is `namespace', check for a named or unnamed
9366 namespace definition. */
9367 else if (token1
.keyword
== RID_NAMESPACE
9368 && (/* A named namespace definition. */
9369 (token2
.type
== CPP_NAME
9370 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
9372 /* An unnamed namespace definition. */
9373 || token2
.type
== CPP_OPEN_BRACE
9374 || token2
.keyword
== RID_ATTRIBUTE
))
9375 cp_parser_namespace_definition (parser
);
9376 /* An inline (associated) namespace definition. */
9377 else if (token1
.keyword
== RID_INLINE
9378 && token2
.keyword
== RID_NAMESPACE
)
9379 cp_parser_namespace_definition (parser
);
9380 /* Objective-C++ declaration/definition. */
9381 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
9382 cp_parser_objc_declaration (parser
, NULL_TREE
);
9383 else if (c_dialect_objc ()
9384 && token1
.keyword
== RID_ATTRIBUTE
9385 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
9386 cp_parser_objc_declaration (parser
, attributes
);
9387 /* We must have either a block declaration or a function
9390 /* Try to parse a block-declaration, or a function-definition. */
9391 cp_parser_block_declaration (parser
, /*statement_p=*/false);
9393 /* Free any declarators allocated. */
9394 obstack_free (&declarator_obstack
, p
);
9397 /* Parse a block-declaration.
9402 namespace-alias-definition
9409 __extension__ block-declaration
9414 static_assert-declaration
9416 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9417 part of a declaration-statement. */
9420 cp_parser_block_declaration (cp_parser
*parser
,
9426 /* Check for the `__extension__' keyword. */
9427 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
9429 /* Parse the qualified declaration. */
9430 cp_parser_block_declaration (parser
, statement_p
);
9431 /* Restore the PEDANTIC flag. */
9432 pedantic
= saved_pedantic
;
9437 /* Peek at the next token to figure out which kind of declaration is
9439 token1
= cp_lexer_peek_token (parser
->lexer
);
9441 /* If the next keyword is `asm', we have an asm-definition. */
9442 if (token1
->keyword
== RID_ASM
)
9445 cp_parser_commit_to_tentative_parse (parser
);
9446 cp_parser_asm_definition (parser
);
9448 /* If the next keyword is `namespace', we have a
9449 namespace-alias-definition. */
9450 else if (token1
->keyword
== RID_NAMESPACE
)
9451 cp_parser_namespace_alias_definition (parser
);
9452 /* If the next keyword is `using', we have either a
9453 using-declaration or a using-directive. */
9454 else if (token1
->keyword
== RID_USING
)
9459 cp_parser_commit_to_tentative_parse (parser
);
9460 /* If the token after `using' is `namespace', then we have a
9462 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9463 if (token2
->keyword
== RID_NAMESPACE
)
9464 cp_parser_using_directive (parser
);
9465 /* Otherwise, it's a using-declaration. */
9467 cp_parser_using_declaration (parser
,
9468 /*access_declaration_p=*/false);
9470 /* If the next keyword is `__label__' we have a misplaced label
9472 else if (token1
->keyword
== RID_LABEL
)
9474 cp_lexer_consume_token (parser
->lexer
);
9475 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
9476 cp_parser_skip_to_end_of_statement (parser
);
9477 /* If the next token is now a `;', consume it. */
9478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9479 cp_lexer_consume_token (parser
->lexer
);
9481 /* If the next token is `static_assert' we have a static assertion. */
9482 else if (token1
->keyword
== RID_STATIC_ASSERT
)
9483 cp_parser_static_assert (parser
, /*member_p=*/false);
9484 /* Anything else must be a simple-declaration. */
9486 cp_parser_simple_declaration (parser
, !statement_p
);
9489 /* Parse a simple-declaration.
9492 decl-specifier-seq [opt] init-declarator-list [opt] ;
9494 init-declarator-list:
9496 init-declarator-list , init-declarator
9498 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9499 function-definition as a simple-declaration. */
9502 cp_parser_simple_declaration (cp_parser
* parser
,
9503 bool function_definition_allowed_p
)
9505 cp_decl_specifier_seq decl_specifiers
;
9506 int declares_class_or_enum
;
9507 bool saw_declarator
;
9509 /* Defer access checks until we know what is being declared; the
9510 checks for names appearing in the decl-specifier-seq should be
9511 done as if we were in the scope of the thing being declared. */
9512 push_deferring_access_checks (dk_deferred
);
9514 /* Parse the decl-specifier-seq. We have to keep track of whether
9515 or not the decl-specifier-seq declares a named class or
9516 enumeration type, since that is the only case in which the
9517 init-declarator-list is allowed to be empty.
9521 In a simple-declaration, the optional init-declarator-list can be
9522 omitted only when declaring a class or enumeration, that is when
9523 the decl-specifier-seq contains either a class-specifier, an
9524 elaborated-type-specifier, or an enum-specifier. */
9525 cp_parser_decl_specifier_seq (parser
,
9526 CP_PARSER_FLAGS_OPTIONAL
,
9528 &declares_class_or_enum
);
9529 /* We no longer need to defer access checks. */
9530 stop_deferring_access_checks ();
9532 /* In a block scope, a valid declaration must always have a
9533 decl-specifier-seq. By not trying to parse declarators, we can
9534 resolve the declaration/expression ambiguity more quickly. */
9535 if (!function_definition_allowed_p
9536 && !decl_specifiers
.any_specifiers_p
)
9538 cp_parser_error (parser
, "expected declaration");
9542 /* If the next two tokens are both identifiers, the code is
9543 erroneous. The usual cause of this situation is code like:
9547 where "T" should name a type -- but does not. */
9548 if (!decl_specifiers
.any_type_specifiers_p
9549 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
9551 /* If parsing tentatively, we should commit; we really are
9552 looking at a declaration. */
9553 cp_parser_commit_to_tentative_parse (parser
);
9558 /* If we have seen at least one decl-specifier, and the next token
9559 is not a parenthesis, then we must be looking at a declaration.
9560 (After "int (" we might be looking at a functional cast.) */
9561 if (decl_specifiers
.any_specifiers_p
9562 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
9563 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
9564 && !cp_parser_error_occurred (parser
))
9565 cp_parser_commit_to_tentative_parse (parser
);
9567 /* Keep going until we hit the `;' at the end of the simple
9569 saw_declarator
= false;
9570 while (cp_lexer_next_token_is_not (parser
->lexer
,
9574 bool function_definition_p
;
9579 /* If we are processing next declarator, coma is expected */
9580 token
= cp_lexer_peek_token (parser
->lexer
);
9581 gcc_assert (token
->type
== CPP_COMMA
);
9582 cp_lexer_consume_token (parser
->lexer
);
9585 saw_declarator
= true;
9587 /* Parse the init-declarator. */
9588 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
9590 function_definition_allowed_p
,
9592 declares_class_or_enum
,
9593 &function_definition_p
);
9594 /* If an error occurred while parsing tentatively, exit quickly.
9595 (That usually happens when in the body of a function; each
9596 statement is treated as a declaration-statement until proven
9598 if (cp_parser_error_occurred (parser
))
9600 /* Handle function definitions specially. */
9601 if (function_definition_p
)
9603 /* If the next token is a `,', then we are probably
9604 processing something like:
9608 which is erroneous. */
9609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9611 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9612 error_at (token
->location
,
9614 " declarations and function-definitions is forbidden");
9616 /* Otherwise, we're done with the list of declarators. */
9619 pop_deferring_access_checks ();
9623 /* The next token should be either a `,' or a `;'. */
9624 token
= cp_lexer_peek_token (parser
->lexer
);
9625 /* If it's a `,', there are more declarators to come. */
9626 if (token
->type
== CPP_COMMA
)
9627 /* will be consumed next time around */;
9628 /* If it's a `;', we are done. */
9629 else if (token
->type
== CPP_SEMICOLON
)
9631 /* Anything else is an error. */
9634 /* If we have already issued an error message we don't need
9635 to issue another one. */
9636 if (decl
!= error_mark_node
9637 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
9638 cp_parser_error (parser
, "expected %<,%> or %<;%>");
9639 /* Skip tokens until we reach the end of the statement. */
9640 cp_parser_skip_to_end_of_statement (parser
);
9641 /* If the next token is now a `;', consume it. */
9642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9643 cp_lexer_consume_token (parser
->lexer
);
9646 /* After the first time around, a function-definition is not
9647 allowed -- even if it was OK at first. For example:
9652 function_definition_allowed_p
= false;
9655 /* Issue an error message if no declarators are present, and the
9656 decl-specifier-seq does not itself declare a class or
9658 if (!saw_declarator
)
9660 if (cp_parser_declares_only_class_p (parser
))
9661 shadow_tag (&decl_specifiers
);
9662 /* Perform any deferred access checks. */
9663 perform_deferred_access_checks ();
9666 /* Consume the `;'. */
9667 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9670 pop_deferring_access_checks ();
9673 /* Parse a decl-specifier-seq.
9676 decl-specifier-seq [opt] decl-specifier
9679 storage-class-specifier
9690 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9692 The parser flags FLAGS is used to control type-specifier parsing.
9694 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9697 1: one of the decl-specifiers is an elaborated-type-specifier
9698 (i.e., a type declaration)
9699 2: one of the decl-specifiers is an enum-specifier or a
9700 class-specifier (i.e., a type definition)
9705 cp_parser_decl_specifier_seq (cp_parser
* parser
,
9706 cp_parser_flags flags
,
9707 cp_decl_specifier_seq
*decl_specs
,
9708 int* declares_class_or_enum
)
9710 bool constructor_possible_p
= !parser
->in_declarator_p
;
9711 cp_token
*start_token
= NULL
;
9713 /* Clear DECL_SPECS. */
9714 clear_decl_specs (decl_specs
);
9716 /* Assume no class or enumeration type is declared. */
9717 *declares_class_or_enum
= 0;
9719 /* Keep reading specifiers until there are no more to read. */
9723 bool found_decl_spec
;
9726 /* Peek at the next token. */
9727 token
= cp_lexer_peek_token (parser
->lexer
);
9729 /* Save the first token of the decl spec list for error
9732 start_token
= token
;
9733 /* Handle attributes. */
9734 if (token
->keyword
== RID_ATTRIBUTE
)
9736 /* Parse the attributes. */
9737 decl_specs
->attributes
9738 = chainon (decl_specs
->attributes
,
9739 cp_parser_attributes_opt (parser
));
9742 /* Assume we will find a decl-specifier keyword. */
9743 found_decl_spec
= true;
9744 /* If the next token is an appropriate keyword, we can simply
9745 add it to the list. */
9746 switch (token
->keyword
)
9752 if (!at_class_scope_p ())
9754 error_at (token
->location
, "%<friend%> used outside of class");
9755 cp_lexer_purge_token (parser
->lexer
);
9759 ++decl_specs
->specs
[(int) ds_friend
];
9760 /* Consume the token. */
9761 cp_lexer_consume_token (parser
->lexer
);
9766 ++decl_specs
->specs
[(int) ds_constexpr
];
9767 cp_lexer_consume_token (parser
->lexer
);
9770 /* function-specifier:
9777 cp_parser_function_specifier_opt (parser
, decl_specs
);
9783 ++decl_specs
->specs
[(int) ds_typedef
];
9784 /* Consume the token. */
9785 cp_lexer_consume_token (parser
->lexer
);
9786 /* A constructor declarator cannot appear in a typedef. */
9787 constructor_possible_p
= false;
9788 /* The "typedef" keyword can only occur in a declaration; we
9789 may as well commit at this point. */
9790 cp_parser_commit_to_tentative_parse (parser
);
9792 if (decl_specs
->storage_class
!= sc_none
)
9793 decl_specs
->conflicting_specifiers_p
= true;
9796 /* storage-class-specifier:
9806 if (cxx_dialect
== cxx98
)
9808 /* Consume the token. */
9809 cp_lexer_consume_token (parser
->lexer
);
9811 /* Complain about `auto' as a storage specifier, if
9812 we're complaining about C++0x compatibility. */
9813 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
9814 " will change meaning in C++0x; please remove it");
9816 /* Set the storage class anyway. */
9817 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
9821 /* C++0x auto type-specifier. */
9822 found_decl_spec
= false;
9829 /* Consume the token. */
9830 cp_lexer_consume_token (parser
->lexer
);
9831 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
9835 /* Consume the token. */
9836 cp_lexer_consume_token (parser
->lexer
);
9837 ++decl_specs
->specs
[(int) ds_thread
];
9841 /* We did not yet find a decl-specifier yet. */
9842 found_decl_spec
= false;
9846 /* Constructors are a special case. The `S' in `S()' is not a
9847 decl-specifier; it is the beginning of the declarator. */
9850 && constructor_possible_p
9851 && (cp_parser_constructor_declarator_p
9852 (parser
, decl_specs
->specs
[(int) ds_friend
] != 0)));
9854 /* If we don't have a DECL_SPEC yet, then we must be looking at
9855 a type-specifier. */
9856 if (!found_decl_spec
&& !constructor_p
)
9858 int decl_spec_declares_class_or_enum
;
9859 bool is_cv_qualifier
;
9863 = cp_parser_type_specifier (parser
, flags
,
9865 /*is_declaration=*/true,
9866 &decl_spec_declares_class_or_enum
,
9868 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
9870 /* If this type-specifier referenced a user-defined type
9871 (a typedef, class-name, etc.), then we can't allow any
9872 more such type-specifiers henceforth.
9876 The longest sequence of decl-specifiers that could
9877 possibly be a type name is taken as the
9878 decl-specifier-seq of a declaration. The sequence shall
9879 be self-consistent as described below.
9883 As a general rule, at most one type-specifier is allowed
9884 in the complete decl-specifier-seq of a declaration. The
9885 only exceptions are the following:
9887 -- const or volatile can be combined with any other
9890 -- signed or unsigned can be combined with char, long,
9898 void g (const int Pc);
9900 Here, Pc is *not* part of the decl-specifier seq; it's
9901 the declarator. Therefore, once we see a type-specifier
9902 (other than a cv-qualifier), we forbid any additional
9903 user-defined types. We *do* still allow things like `int
9904 int' to be considered a decl-specifier-seq, and issue the
9905 error message later. */
9906 if (type_spec
&& !is_cv_qualifier
)
9907 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
9908 /* A constructor declarator cannot follow a type-specifier. */
9911 constructor_possible_p
= false;
9912 found_decl_spec
= true;
9913 if (!is_cv_qualifier
)
9914 decl_specs
->any_type_specifiers_p
= true;
9918 /* If we still do not have a DECL_SPEC, then there are no more
9920 if (!found_decl_spec
)
9923 decl_specs
->any_specifiers_p
= true;
9924 /* After we see one decl-specifier, further decl-specifiers are
9926 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
9929 cp_parser_check_decl_spec (decl_specs
, start_token
->location
);
9931 /* Don't allow a friend specifier with a class definition. */
9932 if (decl_specs
->specs
[(int) ds_friend
] != 0
9933 && (*declares_class_or_enum
& 2))
9934 error_at (start_token
->location
,
9935 "class definition may not be declared a friend");
9938 /* Parse an (optional) storage-class-specifier.
9940 storage-class-specifier:
9949 storage-class-specifier:
9952 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9955 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
9957 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
9960 if (cxx_dialect
!= cxx98
)
9962 /* Fall through for C++98. */
9969 /* Consume the token. */
9970 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
9977 /* Parse an (optional) function-specifier.
9984 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9985 Updates DECL_SPECS, if it is non-NULL. */
9988 cp_parser_function_specifier_opt (cp_parser
* parser
,
9989 cp_decl_specifier_seq
*decl_specs
)
9991 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9992 switch (token
->keyword
)
9996 ++decl_specs
->specs
[(int) ds_inline
];
10000 /* 14.5.2.3 [temp.mem]
10002 A member function template shall not be virtual. */
10003 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10004 error_at (token
->location
, "templates may not be %<virtual%>");
10005 else if (decl_specs
)
10006 ++decl_specs
->specs
[(int) ds_virtual
];
10011 ++decl_specs
->specs
[(int) ds_explicit
];
10018 /* Consume the token. */
10019 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10022 /* Parse a linkage-specification.
10024 linkage-specification:
10025 extern string-literal { declaration-seq [opt] }
10026 extern string-literal declaration */
10029 cp_parser_linkage_specification (cp_parser
* parser
)
10033 /* Look for the `extern' keyword. */
10034 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
10036 /* Look for the string-literal. */
10037 linkage
= cp_parser_string_literal (parser
, false, false);
10039 /* Transform the literal into an identifier. If the literal is a
10040 wide-character string, or contains embedded NULs, then we can't
10041 handle it as the user wants. */
10042 if (strlen (TREE_STRING_POINTER (linkage
))
10043 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
10045 cp_parser_error (parser
, "invalid linkage-specification");
10046 /* Assume C++ linkage. */
10047 linkage
= lang_name_cplusplus
;
10050 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
10052 /* We're now using the new linkage. */
10053 push_lang_context (linkage
);
10055 /* If the next token is a `{', then we're using the first
10057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10059 /* Consume the `{' token. */
10060 cp_lexer_consume_token (parser
->lexer
);
10061 /* Parse the declarations. */
10062 cp_parser_declaration_seq_opt (parser
);
10063 /* Look for the closing `}'. */
10064 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10066 /* Otherwise, there's just one declaration. */
10069 bool saved_in_unbraced_linkage_specification_p
;
10071 saved_in_unbraced_linkage_specification_p
10072 = parser
->in_unbraced_linkage_specification_p
;
10073 parser
->in_unbraced_linkage_specification_p
= true;
10074 cp_parser_declaration (parser
);
10075 parser
->in_unbraced_linkage_specification_p
10076 = saved_in_unbraced_linkage_specification_p
;
10079 /* We're done with the linkage-specification. */
10080 pop_lang_context ();
10083 /* Parse a static_assert-declaration.
10085 static_assert-declaration:
10086 static_assert ( constant-expression , string-literal ) ;
10088 If MEMBER_P, this static_assert is a class member. */
10091 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
10096 location_t saved_loc
;
10098 /* Peek at the `static_assert' token so we can keep track of exactly
10099 where the static assertion started. */
10100 token
= cp_lexer_peek_token (parser
->lexer
);
10101 saved_loc
= token
->location
;
10103 /* Look for the `static_assert' keyword. */
10104 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
10108 /* We know we are in a static assertion; commit to any tentative
10110 if (cp_parser_parsing_tentatively (parser
))
10111 cp_parser_commit_to_tentative_parse (parser
);
10113 /* Parse the `(' starting the static assertion condition. */
10114 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10116 /* Parse the constant-expression. */
10118 cp_parser_constant_expression (parser
,
10119 /*allow_non_constant_p=*/false,
10120 /*non_constant_p=*/NULL
);
10122 /* Parse the separating `,'. */
10123 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10125 /* Parse the string-literal message. */
10126 message
= cp_parser_string_literal (parser
,
10127 /*translate=*/false,
10130 /* A `)' completes the static assertion. */
10131 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10132 cp_parser_skip_to_closing_parenthesis (parser
,
10133 /*recovering=*/true,
10134 /*or_comma=*/false,
10135 /*consume_paren=*/true);
10137 /* A semicolon terminates the declaration. */
10138 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10140 /* Complete the static assertion, which may mean either processing
10141 the static assert now or saving it for template instantiation. */
10142 finish_static_assert (condition
, message
, saved_loc
, member_p
);
10145 /* Parse a `decltype' type. Returns the type.
10147 simple-type-specifier:
10148 decltype ( expression ) */
10151 cp_parser_decltype (cp_parser
*parser
)
10154 bool id_expression_or_member_access_p
= false;
10155 const char *saved_message
;
10156 bool saved_integral_constant_expression_p
;
10157 bool saved_non_integral_constant_expression_p
;
10158 cp_token
*id_expr_start_token
;
10160 /* Look for the `decltype' token. */
10161 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
10162 return error_mark_node
;
10164 /* Types cannot be defined in a `decltype' expression. Save away the
10166 saved_message
= parser
->type_definition_forbidden_message
;
10168 /* And create the new one. */
10169 parser
->type_definition_forbidden_message
10170 = G_("types may not be defined in %<decltype%> expressions");
10172 /* The restrictions on constant-expressions do not apply inside
10173 decltype expressions. */
10174 saved_integral_constant_expression_p
10175 = parser
->integral_constant_expression_p
;
10176 saved_non_integral_constant_expression_p
10177 = parser
->non_integral_constant_expression_p
;
10178 parser
->integral_constant_expression_p
= false;
10180 /* Do not actually evaluate the expression. */
10181 ++cp_unevaluated_operand
;
10183 /* Do not warn about problems with the expression. */
10184 ++c_inhibit_evaluation_warnings
;
10186 /* Parse the opening `('. */
10187 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10188 return error_mark_node
;
10190 /* First, try parsing an id-expression. */
10191 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
10192 cp_parser_parse_tentatively (parser
);
10193 expr
= cp_parser_id_expression (parser
,
10194 /*template_keyword_p=*/false,
10195 /*check_dependency_p=*/true,
10196 /*template_p=*/NULL
,
10197 /*declarator_p=*/false,
10198 /*optional_p=*/false);
10200 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
10202 bool non_integral_constant_expression_p
= false;
10203 tree id_expression
= expr
;
10205 const char *error_msg
;
10207 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
10208 /* Lookup the name we got back from the id-expression. */
10209 expr
= cp_parser_lookup_name (parser
, expr
,
10211 /*is_template=*/false,
10212 /*is_namespace=*/false,
10213 /*check_dependency=*/true,
10214 /*ambiguous_decls=*/NULL
,
10215 id_expr_start_token
->location
);
10218 && expr
!= error_mark_node
10219 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
10220 && TREE_CODE (expr
) != TYPE_DECL
10221 && (TREE_CODE (expr
) != BIT_NOT_EXPR
10222 || !TYPE_P (TREE_OPERAND (expr
, 0)))
10223 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10225 /* Complete lookup of the id-expression. */
10226 expr
= (finish_id_expression
10227 (id_expression
, expr
, parser
->scope
, &idk
,
10228 /*integral_constant_expression_p=*/false,
10229 /*allow_non_integral_constant_expression_p=*/true,
10230 &non_integral_constant_expression_p
,
10231 /*template_p=*/false,
10233 /*address_p=*/false,
10234 /*template_arg_p=*/false,
10236 id_expr_start_token
->location
));
10238 if (expr
== error_mark_node
)
10239 /* We found an id-expression, but it was something that we
10240 should not have found. This is an error, not something
10241 we can recover from, so note that we found an
10242 id-expression and we'll recover as gracefully as
10244 id_expression_or_member_access_p
= true;
10248 && expr
!= error_mark_node
10249 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10250 /* We have an id-expression. */
10251 id_expression_or_member_access_p
= true;
10254 if (!id_expression_or_member_access_p
)
10256 /* Abort the id-expression parse. */
10257 cp_parser_abort_tentative_parse (parser
);
10259 /* Parsing tentatively, again. */
10260 cp_parser_parse_tentatively (parser
);
10262 /* Parse a class member access. */
10263 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
10265 /*member_access_only_p=*/true, NULL
);
10268 && expr
!= error_mark_node
10269 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
10270 /* We have an id-expression. */
10271 id_expression_or_member_access_p
= true;
10274 if (id_expression_or_member_access_p
)
10275 /* We have parsed the complete id-expression or member access. */
10276 cp_parser_parse_definitely (parser
);
10279 bool saved_greater_than_is_operator_p
;
10281 /* Abort our attempt to parse an id-expression or member access
10283 cp_parser_abort_tentative_parse (parser
);
10285 /* Within a parenthesized expression, a `>' token is always
10286 the greater-than operator. */
10287 saved_greater_than_is_operator_p
10288 = parser
->greater_than_is_operator_p
;
10289 parser
->greater_than_is_operator_p
= true;
10291 /* Parse a full expression. */
10292 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10294 /* The `>' token might be the end of a template-id or
10295 template-parameter-list now. */
10296 parser
->greater_than_is_operator_p
10297 = saved_greater_than_is_operator_p
;
10300 /* Go back to evaluating expressions. */
10301 --cp_unevaluated_operand
;
10302 --c_inhibit_evaluation_warnings
;
10304 /* Restore the old message and the integral constant expression
10306 parser
->type_definition_forbidden_message
= saved_message
;
10307 parser
->integral_constant_expression_p
10308 = saved_integral_constant_expression_p
;
10309 parser
->non_integral_constant_expression_p
10310 = saved_non_integral_constant_expression_p
;
10312 if (expr
== error_mark_node
)
10314 /* Skip everything up to the closing `)'. */
10315 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10316 /*consume_paren=*/true);
10317 return error_mark_node
;
10320 /* Parse to the closing `)'. */
10321 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10323 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10324 /*consume_paren=*/true);
10325 return error_mark_node
;
10328 return finish_decltype_type (expr
, id_expression_or_member_access_p
);
10331 /* Special member functions [gram.special] */
10333 /* Parse a conversion-function-id.
10335 conversion-function-id:
10336 operator conversion-type-id
10338 Returns an IDENTIFIER_NODE representing the operator. */
10341 cp_parser_conversion_function_id (cp_parser
* parser
)
10345 tree saved_qualifying_scope
;
10346 tree saved_object_scope
;
10347 tree pushed_scope
= NULL_TREE
;
10349 /* Look for the `operator' token. */
10350 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
10351 return error_mark_node
;
10352 /* When we parse the conversion-type-id, the current scope will be
10353 reset. However, we need that information in able to look up the
10354 conversion function later, so we save it here. */
10355 saved_scope
= parser
->scope
;
10356 saved_qualifying_scope
= parser
->qualifying_scope
;
10357 saved_object_scope
= parser
->object_scope
;
10358 /* We must enter the scope of the class so that the names of
10359 entities declared within the class are available in the
10360 conversion-type-id. For example, consider:
10367 S::operator I() { ... }
10369 In order to see that `I' is a type-name in the definition, we
10370 must be in the scope of `S'. */
10372 pushed_scope
= push_scope (saved_scope
);
10373 /* Parse the conversion-type-id. */
10374 type
= cp_parser_conversion_type_id (parser
);
10375 /* Leave the scope of the class, if any. */
10377 pop_scope (pushed_scope
);
10378 /* Restore the saved scope. */
10379 parser
->scope
= saved_scope
;
10380 parser
->qualifying_scope
= saved_qualifying_scope
;
10381 parser
->object_scope
= saved_object_scope
;
10382 /* If the TYPE is invalid, indicate failure. */
10383 if (type
== error_mark_node
)
10384 return error_mark_node
;
10385 return mangle_conv_op_name_for_type (type
);
10388 /* Parse a conversion-type-id:
10390 conversion-type-id:
10391 type-specifier-seq conversion-declarator [opt]
10393 Returns the TYPE specified. */
10396 cp_parser_conversion_type_id (cp_parser
* parser
)
10399 cp_decl_specifier_seq type_specifiers
;
10400 cp_declarator
*declarator
;
10401 tree type_specified
;
10403 /* Parse the attributes. */
10404 attributes
= cp_parser_attributes_opt (parser
);
10405 /* Parse the type-specifiers. */
10406 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
10407 /*is_trailing_return=*/false,
10409 /* If that didn't work, stop. */
10410 if (type_specifiers
.type
== error_mark_node
)
10411 return error_mark_node
;
10412 /* Parse the conversion-declarator. */
10413 declarator
= cp_parser_conversion_declarator_opt (parser
);
10415 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
10416 /*initialized=*/0, &attributes
);
10418 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
10420 /* Don't give this error when parsing tentatively. This happens to
10421 work because we always parse this definitively once. */
10422 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
10423 && type_uses_auto (type_specified
))
10425 error ("invalid use of %<auto%> in conversion operator");
10426 return error_mark_node
;
10429 return type_specified
;
10432 /* Parse an (optional) conversion-declarator.
10434 conversion-declarator:
10435 ptr-operator conversion-declarator [opt]
10439 static cp_declarator
*
10440 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
10442 enum tree_code code
;
10444 cp_cv_quals cv_quals
;
10446 /* We don't know if there's a ptr-operator next, or not. */
10447 cp_parser_parse_tentatively (parser
);
10448 /* Try the ptr-operator. */
10449 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
10450 /* If it worked, look for more conversion-declarators. */
10451 if (cp_parser_parse_definitely (parser
))
10453 cp_declarator
*declarator
;
10455 /* Parse another optional declarator. */
10456 declarator
= cp_parser_conversion_declarator_opt (parser
);
10458 return cp_parser_make_indirect_declarator
10459 (code
, class_type
, cv_quals
, declarator
);
10465 /* Parse an (optional) ctor-initializer.
10468 : mem-initializer-list
10470 Returns TRUE iff the ctor-initializer was actually present. */
10473 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
10475 /* If the next token is not a `:', then there is no
10476 ctor-initializer. */
10477 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
10479 /* Do default initialization of any bases and members. */
10480 if (DECL_CONSTRUCTOR_P (current_function_decl
))
10481 finish_mem_initializers (NULL_TREE
);
10486 /* Consume the `:' token. */
10487 cp_lexer_consume_token (parser
->lexer
);
10488 /* And the mem-initializer-list. */
10489 cp_parser_mem_initializer_list (parser
);
10494 /* Parse a mem-initializer-list.
10496 mem-initializer-list:
10497 mem-initializer ... [opt]
10498 mem-initializer ... [opt] , mem-initializer-list */
10501 cp_parser_mem_initializer_list (cp_parser
* parser
)
10503 tree mem_initializer_list
= NULL_TREE
;
10504 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10506 /* Let the semantic analysis code know that we are starting the
10507 mem-initializer-list. */
10508 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
10509 error_at (token
->location
,
10510 "only constructors take member initializers");
10512 /* Loop through the list. */
10515 tree mem_initializer
;
10517 token
= cp_lexer_peek_token (parser
->lexer
);
10518 /* Parse the mem-initializer. */
10519 mem_initializer
= cp_parser_mem_initializer (parser
);
10520 /* If the next token is a `...', we're expanding member initializers. */
10521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10523 /* Consume the `...'. */
10524 cp_lexer_consume_token (parser
->lexer
);
10526 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10527 can be expanded but members cannot. */
10528 if (mem_initializer
!= error_mark_node
10529 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
10531 error_at (token
->location
,
10532 "cannot expand initializer for member %<%D%>",
10533 TREE_PURPOSE (mem_initializer
));
10534 mem_initializer
= error_mark_node
;
10537 /* Construct the pack expansion type. */
10538 if (mem_initializer
!= error_mark_node
)
10539 mem_initializer
= make_pack_expansion (mem_initializer
);
10541 /* Add it to the list, unless it was erroneous. */
10542 if (mem_initializer
!= error_mark_node
)
10544 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
10545 mem_initializer_list
= mem_initializer
;
10547 /* If the next token is not a `,', we're done. */
10548 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
10550 /* Consume the `,' token. */
10551 cp_lexer_consume_token (parser
->lexer
);
10554 /* Perform semantic analysis. */
10555 if (DECL_CONSTRUCTOR_P (current_function_decl
))
10556 finish_mem_initializers (mem_initializer_list
);
10559 /* Parse a mem-initializer.
10562 mem-initializer-id ( expression-list [opt] )
10563 mem-initializer-id braced-init-list
10568 ( expression-list [opt] )
10570 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10571 class) or FIELD_DECL (for a non-static data member) to initialize;
10572 the TREE_VALUE is the expression-list. An empty initialization
10573 list is represented by void_list_node. */
10576 cp_parser_mem_initializer (cp_parser
* parser
)
10578 tree mem_initializer_id
;
10579 tree expression_list
;
10581 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10583 /* Find out what is being initialized. */
10584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10586 permerror (token
->location
,
10587 "anachronistic old-style base class initializer");
10588 mem_initializer_id
= NULL_TREE
;
10592 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
10593 if (mem_initializer_id
== error_mark_node
)
10594 return mem_initializer_id
;
10596 member
= expand_member_init (mem_initializer_id
);
10597 if (member
&& !DECL_P (member
))
10598 in_base_initializer
= 1;
10600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10602 bool expr_non_constant_p
;
10603 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10604 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10605 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
10606 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
10611 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
10613 /*allow_expansion_p=*/true,
10614 /*non_constant_p=*/NULL
);
10616 return error_mark_node
;
10617 expression_list
= build_tree_list_vec (vec
);
10618 release_tree_vector (vec
);
10621 if (expression_list
== error_mark_node
)
10622 return error_mark_node
;
10623 if (!expression_list
)
10624 expression_list
= void_type_node
;
10626 in_base_initializer
= 0;
10628 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
10631 /* Parse a mem-initializer-id.
10633 mem-initializer-id:
10634 :: [opt] nested-name-specifier [opt] class-name
10637 Returns a TYPE indicating the class to be initializer for the first
10638 production. Returns an IDENTIFIER_NODE indicating the data member
10639 to be initialized for the second production. */
10642 cp_parser_mem_initializer_id (cp_parser
* parser
)
10644 bool global_scope_p
;
10645 bool nested_name_specifier_p
;
10646 bool template_p
= false;
10649 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10651 /* `typename' is not allowed in this context ([temp.res]). */
10652 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
10654 error_at (token
->location
,
10655 "keyword %<typename%> not allowed in this context (a qualified "
10656 "member initializer is implicitly a type)");
10657 cp_lexer_consume_token (parser
->lexer
);
10659 /* Look for the optional `::' operator. */
10661 = (cp_parser_global_scope_opt (parser
,
10662 /*current_scope_valid_p=*/false)
10664 /* Look for the optional nested-name-specifier. The simplest way to
10669 The keyword `typename' is not permitted in a base-specifier or
10670 mem-initializer; in these contexts a qualified name that
10671 depends on a template-parameter is implicitly assumed to be a
10674 is to assume that we have seen the `typename' keyword at this
10676 nested_name_specifier_p
10677 = (cp_parser_nested_name_specifier_opt (parser
,
10678 /*typename_keyword_p=*/true,
10679 /*check_dependency_p=*/true,
10681 /*is_declaration=*/true)
10683 if (nested_name_specifier_p
)
10684 template_p
= cp_parser_optional_template_keyword (parser
);
10685 /* If there is a `::' operator or a nested-name-specifier, then we
10686 are definitely looking for a class-name. */
10687 if (global_scope_p
|| nested_name_specifier_p
)
10688 return cp_parser_class_name (parser
,
10689 /*typename_keyword_p=*/true,
10690 /*template_keyword_p=*/template_p
,
10692 /*check_dependency_p=*/true,
10693 /*class_head_p=*/false,
10694 /*is_declaration=*/true);
10695 /* Otherwise, we could also be looking for an ordinary identifier. */
10696 cp_parser_parse_tentatively (parser
);
10697 /* Try a class-name. */
10698 id
= cp_parser_class_name (parser
,
10699 /*typename_keyword_p=*/true,
10700 /*template_keyword_p=*/false,
10702 /*check_dependency_p=*/true,
10703 /*class_head_p=*/false,
10704 /*is_declaration=*/true);
10705 /* If we found one, we're done. */
10706 if (cp_parser_parse_definitely (parser
))
10708 /* Otherwise, look for an ordinary identifier. */
10709 return cp_parser_identifier (parser
);
10712 /* Overloading [gram.over] */
10714 /* Parse an operator-function-id.
10716 operator-function-id:
10719 Returns an IDENTIFIER_NODE for the operator which is a
10720 human-readable spelling of the identifier, e.g., `operator +'. */
10723 cp_parser_operator_function_id (cp_parser
* parser
)
10725 /* Look for the `operator' keyword. */
10726 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
10727 return error_mark_node
;
10728 /* And then the name of the operator itself. */
10729 return cp_parser_operator (parser
);
10732 /* Parse an operator.
10735 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10736 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10737 || ++ -- , ->* -> () []
10744 Returns an IDENTIFIER_NODE for the operator which is a
10745 human-readable spelling of the identifier, e.g., `operator +'. */
10748 cp_parser_operator (cp_parser
* parser
)
10750 tree id
= NULL_TREE
;
10753 /* Peek at the next token. */
10754 token
= cp_lexer_peek_token (parser
->lexer
);
10755 /* Figure out which operator we have. */
10756 switch (token
->type
)
10762 /* The keyword should be either `new' or `delete'. */
10763 if (token
->keyword
== RID_NEW
)
10765 else if (token
->keyword
== RID_DELETE
)
10770 /* Consume the `new' or `delete' token. */
10771 cp_lexer_consume_token (parser
->lexer
);
10773 /* Peek at the next token. */
10774 token
= cp_lexer_peek_token (parser
->lexer
);
10775 /* If it's a `[' token then this is the array variant of the
10777 if (token
->type
== CPP_OPEN_SQUARE
)
10779 /* Consume the `[' token. */
10780 cp_lexer_consume_token (parser
->lexer
);
10781 /* Look for the `]' token. */
10782 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10783 id
= ansi_opname (op
== NEW_EXPR
10784 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
10786 /* Otherwise, we have the non-array variant. */
10788 id
= ansi_opname (op
);
10794 id
= ansi_opname (PLUS_EXPR
);
10798 id
= ansi_opname (MINUS_EXPR
);
10802 id
= ansi_opname (MULT_EXPR
);
10806 id
= ansi_opname (TRUNC_DIV_EXPR
);
10810 id
= ansi_opname (TRUNC_MOD_EXPR
);
10814 id
= ansi_opname (BIT_XOR_EXPR
);
10818 id
= ansi_opname (BIT_AND_EXPR
);
10822 id
= ansi_opname (BIT_IOR_EXPR
);
10826 id
= ansi_opname (BIT_NOT_EXPR
);
10830 id
= ansi_opname (TRUTH_NOT_EXPR
);
10834 id
= ansi_assopname (NOP_EXPR
);
10838 id
= ansi_opname (LT_EXPR
);
10842 id
= ansi_opname (GT_EXPR
);
10846 id
= ansi_assopname (PLUS_EXPR
);
10850 id
= ansi_assopname (MINUS_EXPR
);
10854 id
= ansi_assopname (MULT_EXPR
);
10858 id
= ansi_assopname (TRUNC_DIV_EXPR
);
10862 id
= ansi_assopname (TRUNC_MOD_EXPR
);
10866 id
= ansi_assopname (BIT_XOR_EXPR
);
10870 id
= ansi_assopname (BIT_AND_EXPR
);
10874 id
= ansi_assopname (BIT_IOR_EXPR
);
10878 id
= ansi_opname (LSHIFT_EXPR
);
10882 id
= ansi_opname (RSHIFT_EXPR
);
10885 case CPP_LSHIFT_EQ
:
10886 id
= ansi_assopname (LSHIFT_EXPR
);
10889 case CPP_RSHIFT_EQ
:
10890 id
= ansi_assopname (RSHIFT_EXPR
);
10894 id
= ansi_opname (EQ_EXPR
);
10898 id
= ansi_opname (NE_EXPR
);
10902 id
= ansi_opname (LE_EXPR
);
10905 case CPP_GREATER_EQ
:
10906 id
= ansi_opname (GE_EXPR
);
10910 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
10914 id
= ansi_opname (TRUTH_ORIF_EXPR
);
10917 case CPP_PLUS_PLUS
:
10918 id
= ansi_opname (POSTINCREMENT_EXPR
);
10921 case CPP_MINUS_MINUS
:
10922 id
= ansi_opname (PREDECREMENT_EXPR
);
10926 id
= ansi_opname (COMPOUND_EXPR
);
10929 case CPP_DEREF_STAR
:
10930 id
= ansi_opname (MEMBER_REF
);
10934 id
= ansi_opname (COMPONENT_REF
);
10937 case CPP_OPEN_PAREN
:
10938 /* Consume the `('. */
10939 cp_lexer_consume_token (parser
->lexer
);
10940 /* Look for the matching `)'. */
10941 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10942 return ansi_opname (CALL_EXPR
);
10944 case CPP_OPEN_SQUARE
:
10945 /* Consume the `['. */
10946 cp_lexer_consume_token (parser
->lexer
);
10947 /* Look for the matching `]'. */
10948 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10949 return ansi_opname (ARRAY_REF
);
10952 /* Anything else is an error. */
10956 /* If we have selected an identifier, we need to consume the
10959 cp_lexer_consume_token (parser
->lexer
);
10960 /* Otherwise, no valid operator name was present. */
10963 cp_parser_error (parser
, "expected operator");
10964 id
= error_mark_node
;
10970 /* Parse a template-declaration.
10972 template-declaration:
10973 export [opt] template < template-parameter-list > declaration
10975 If MEMBER_P is TRUE, this template-declaration occurs within a
10978 The grammar rule given by the standard isn't correct. What
10979 is really meant is:
10981 template-declaration:
10982 export [opt] template-parameter-list-seq
10983 decl-specifier-seq [opt] init-declarator [opt] ;
10984 export [opt] template-parameter-list-seq
10985 function-definition
10987 template-parameter-list-seq:
10988 template-parameter-list-seq [opt]
10989 template < template-parameter-list > */
10992 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
10994 /* Check for `export'. */
10995 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
10997 /* Consume the `export' token. */
10998 cp_lexer_consume_token (parser
->lexer
);
10999 /* Warn that we do not support `export'. */
11000 warning (0, "keyword %<export%> not implemented, and will be ignored");
11003 cp_parser_template_declaration_after_export (parser
, member_p
);
11006 /* Parse a template-parameter-list.
11008 template-parameter-list:
11010 template-parameter-list , template-parameter
11012 Returns a TREE_LIST. Each node represents a template parameter.
11013 The nodes are connected via their TREE_CHAINs. */
11016 cp_parser_template_parameter_list (cp_parser
* parser
)
11018 tree parameter_list
= NULL_TREE
;
11020 begin_template_parm_list ();
11025 bool is_parameter_pack
;
11026 location_t parm_loc
;
11028 /* Parse the template-parameter. */
11029 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11030 parameter
= cp_parser_template_parameter (parser
,
11032 &is_parameter_pack
);
11033 /* Add it to the list. */
11034 if (parameter
!= error_mark_node
)
11035 parameter_list
= process_template_parm (parameter_list
,
11039 is_parameter_pack
);
11042 tree err_parm
= build_tree_list (parameter
, parameter
);
11043 TREE_VALUE (err_parm
) = error_mark_node
;
11044 parameter_list
= chainon (parameter_list
, err_parm
);
11047 /* If the next token is not a `,', we're done. */
11048 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11050 /* Otherwise, consume the `,' token. */
11051 cp_lexer_consume_token (parser
->lexer
);
11054 return end_template_parm_list (parameter_list
);
11057 /* Parse a template-parameter.
11059 template-parameter:
11061 parameter-declaration
11063 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11064 the parameter. The TREE_PURPOSE is the default value, if any.
11065 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11066 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11067 set to true iff this parameter is a parameter pack. */
11070 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
11071 bool *is_parameter_pack
)
11074 cp_parameter_declarator
*parameter_declarator
;
11075 cp_declarator
*id_declarator
;
11078 /* Assume it is a type parameter or a template parameter. */
11079 *is_non_type
= false;
11080 /* Assume it not a parameter pack. */
11081 *is_parameter_pack
= false;
11082 /* Peek at the next token. */
11083 token
= cp_lexer_peek_token (parser
->lexer
);
11084 /* If it is `class' or `template', we have a type-parameter. */
11085 if (token
->keyword
== RID_TEMPLATE
)
11086 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11087 /* If it is `class' or `typename' we do not know yet whether it is a
11088 type parameter or a non-type parameter. Consider:
11090 template <typename T, typename T::X X> ...
11094 template <class C, class D*> ...
11096 Here, the first parameter is a type parameter, and the second is
11097 a non-type parameter. We can tell by looking at the token after
11098 the identifier -- if it is a `,', `=', or `>' then we have a type
11100 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
11102 /* Peek at the token after `class' or `typename'. */
11103 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11104 /* If it's an ellipsis, we have a template type parameter
11106 if (token
->type
== CPP_ELLIPSIS
)
11107 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11108 /* If it's an identifier, skip it. */
11109 if (token
->type
== CPP_NAME
)
11110 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
11111 /* Now, see if the token looks like the end of a template
11113 if (token
->type
== CPP_COMMA
11114 || token
->type
== CPP_EQ
11115 || token
->type
== CPP_GREATER
)
11116 return cp_parser_type_parameter (parser
, is_parameter_pack
);
11119 /* Otherwise, it is a non-type parameter.
11123 When parsing a default template-argument for a non-type
11124 template-parameter, the first non-nested `>' is taken as the end
11125 of the template parameter-list rather than a greater-than
11127 *is_non_type
= true;
11128 parameter_declarator
11129 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
11130 /*parenthesized_p=*/NULL
);
11132 /* If the parameter declaration is marked as a parameter pack, set
11133 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11134 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11136 if (parameter_declarator
11137 && parameter_declarator
->declarator
11138 && parameter_declarator
->declarator
->parameter_pack_p
)
11140 *is_parameter_pack
= true;
11141 parameter_declarator
->declarator
->parameter_pack_p
= false;
11144 /* If the next token is an ellipsis, and we don't already have it
11145 marked as a parameter pack, then we have a parameter pack (that
11146 has no declarator). */
11147 if (!*is_parameter_pack
11148 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
11149 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
11151 /* Consume the `...'. */
11152 cp_lexer_consume_token (parser
->lexer
);
11153 maybe_warn_variadic_templates ();
11155 *is_parameter_pack
= true;
11157 /* We might end up with a pack expansion as the type of the non-type
11158 template parameter, in which case this is a non-type template
11160 else if (parameter_declarator
11161 && parameter_declarator
->decl_specifiers
.type
11162 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
11164 *is_parameter_pack
= true;
11165 parameter_declarator
->decl_specifiers
.type
=
11166 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
11169 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11171 /* Parameter packs cannot have default arguments. However, a
11172 user may try to do so, so we'll parse them and give an
11173 appropriate diagnostic here. */
11175 /* Consume the `='. */
11176 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11177 cp_lexer_consume_token (parser
->lexer
);
11179 /* Find the name of the parameter pack. */
11180 id_declarator
= parameter_declarator
->declarator
;
11181 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
11182 id_declarator
= id_declarator
->declarator
;
11184 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
11185 error_at (start_token
->location
,
11186 "template parameter pack %qD cannot have a default argument",
11187 id_declarator
->u
.id
.unqualified_name
);
11189 error_at (start_token
->location
,
11190 "template parameter pack cannot have a default argument");
11192 /* Parse the default argument, but throw away the result. */
11193 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
11196 parm
= grokdeclarator (parameter_declarator
->declarator
,
11197 ¶meter_declarator
->decl_specifiers
,
11198 TPARM
, /*initialized=*/0,
11199 /*attrlist=*/NULL
);
11200 if (parm
== error_mark_node
)
11201 return error_mark_node
;
11203 return build_tree_list (parameter_declarator
->default_argument
, parm
);
11206 /* Parse a type-parameter.
11209 class identifier [opt]
11210 class identifier [opt] = type-id
11211 typename identifier [opt]
11212 typename identifier [opt] = type-id
11213 template < template-parameter-list > class identifier [opt]
11214 template < template-parameter-list > class identifier [opt]
11217 GNU Extension (variadic templates):
11220 class ... identifier [opt]
11221 typename ... identifier [opt]
11223 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11224 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11225 the declaration of the parameter.
11227 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11230 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
11235 /* Look for a keyword to tell us what kind of parameter this is. */
11236 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
11238 return error_mark_node
;
11240 switch (token
->keyword
)
11246 tree default_argument
;
11248 /* If the next token is an ellipsis, we have a template
11250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11252 /* Consume the `...' token. */
11253 cp_lexer_consume_token (parser
->lexer
);
11254 maybe_warn_variadic_templates ();
11256 *is_parameter_pack
= true;
11259 /* If the next token is an identifier, then it names the
11261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
11262 identifier
= cp_parser_identifier (parser
);
11264 identifier
= NULL_TREE
;
11266 /* Create the parameter. */
11267 parameter
= finish_template_type_parm (class_type_node
, identifier
);
11269 /* If the next token is an `=', we have a default argument. */
11270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11272 /* Consume the `=' token. */
11273 cp_lexer_consume_token (parser
->lexer
);
11274 /* Parse the default-argument. */
11275 push_deferring_access_checks (dk_no_deferred
);
11276 default_argument
= cp_parser_type_id (parser
);
11278 /* Template parameter packs cannot have default
11280 if (*is_parameter_pack
)
11283 error_at (token
->location
,
11284 "template parameter pack %qD cannot have a "
11285 "default argument", identifier
);
11287 error_at (token
->location
,
11288 "template parameter packs cannot have "
11289 "default arguments");
11290 default_argument
= NULL_TREE
;
11292 pop_deferring_access_checks ();
11295 default_argument
= NULL_TREE
;
11297 /* Create the combined representation of the parameter and the
11298 default argument. */
11299 parameter
= build_tree_list (default_argument
, parameter
);
11306 tree default_argument
;
11308 /* Look for the `<'. */
11309 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
11310 /* Parse the template-parameter-list. */
11311 cp_parser_template_parameter_list (parser
);
11312 /* Look for the `>'. */
11313 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
11314 /* Look for the `class' keyword. */
11315 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
11316 /* If the next token is an ellipsis, we have a template
11318 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11320 /* Consume the `...' token. */
11321 cp_lexer_consume_token (parser
->lexer
);
11322 maybe_warn_variadic_templates ();
11324 *is_parameter_pack
= true;
11326 /* If the next token is an `=', then there is a
11327 default-argument. If the next token is a `>', we are at
11328 the end of the parameter-list. If the next token is a `,',
11329 then we are at the end of this parameter. */
11330 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11331 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
11332 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11334 identifier
= cp_parser_identifier (parser
);
11335 /* Treat invalid names as if the parameter were nameless. */
11336 if (identifier
== error_mark_node
)
11337 identifier
= NULL_TREE
;
11340 identifier
= NULL_TREE
;
11342 /* Create the template parameter. */
11343 parameter
= finish_template_template_parm (class_type_node
,
11346 /* If the next token is an `=', then there is a
11347 default-argument. */
11348 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11352 /* Consume the `='. */
11353 cp_lexer_consume_token (parser
->lexer
);
11354 /* Parse the id-expression. */
11355 push_deferring_access_checks (dk_no_deferred
);
11356 /* save token before parsing the id-expression, for error
11358 token
= cp_lexer_peek_token (parser
->lexer
);
11360 = cp_parser_id_expression (parser
,
11361 /*template_keyword_p=*/false,
11362 /*check_dependency_p=*/true,
11363 /*template_p=*/&is_template
,
11364 /*declarator_p=*/false,
11365 /*optional_p=*/false);
11366 if (TREE_CODE (default_argument
) == TYPE_DECL
)
11367 /* If the id-expression was a template-id that refers to
11368 a template-class, we already have the declaration here,
11369 so no further lookup is needed. */
11372 /* Look up the name. */
11374 = cp_parser_lookup_name (parser
, default_argument
,
11376 /*is_template=*/is_template
,
11377 /*is_namespace=*/false,
11378 /*check_dependency=*/true,
11379 /*ambiguous_decls=*/NULL
,
11381 /* See if the default argument is valid. */
11383 = check_template_template_default_arg (default_argument
);
11385 /* Template parameter packs cannot have default
11387 if (*is_parameter_pack
)
11390 error_at (token
->location
,
11391 "template parameter pack %qD cannot "
11392 "have a default argument",
11395 error_at (token
->location
, "template parameter packs cannot "
11396 "have default arguments");
11397 default_argument
= NULL_TREE
;
11399 pop_deferring_access_checks ();
11402 default_argument
= NULL_TREE
;
11404 /* Create the combined representation of the parameter and the
11405 default argument. */
11406 parameter
= build_tree_list (default_argument
, parameter
);
11411 gcc_unreachable ();
11418 /* Parse a template-id.
11421 template-name < template-argument-list [opt] >
11423 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11424 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11425 returned. Otherwise, if the template-name names a function, or set
11426 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11427 names a class, returns a TYPE_DECL for the specialization.
11429 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11430 uninstantiated templates. */
11433 cp_parser_template_id (cp_parser
*parser
,
11434 bool template_keyword_p
,
11435 bool check_dependency_p
,
11436 bool is_declaration
)
11442 cp_token_position start_of_id
= 0;
11443 deferred_access_check
*chk
;
11444 VEC (deferred_access_check
,gc
) *access_check
;
11445 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
11446 bool is_identifier
;
11448 /* If the next token corresponds to a template-id, there is no need
11450 next_token
= cp_lexer_peek_token (parser
->lexer
);
11451 if (next_token
->type
== CPP_TEMPLATE_ID
)
11453 struct tree_check
*check_value
;
11455 /* Get the stored value. */
11456 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
11457 /* Perform any access checks that were deferred. */
11458 access_check
= check_value
->checks
;
11461 FOR_EACH_VEC_ELT (deferred_access_check
, access_check
, i
, chk
)
11462 perform_or_defer_access_check (chk
->binfo
,
11466 /* Return the stored value. */
11467 return check_value
->value
;
11470 /* Avoid performing name lookup if there is no possibility of
11471 finding a template-id. */
11472 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
11473 || (next_token
->type
== CPP_NAME
11474 && !cp_parser_nth_token_starts_template_argument_list_p
11477 cp_parser_error (parser
, "expected template-id");
11478 return error_mark_node
;
11481 /* Remember where the template-id starts. */
11482 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
11483 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
11485 push_deferring_access_checks (dk_deferred
);
11487 /* Parse the template-name. */
11488 is_identifier
= false;
11489 templ
= cp_parser_template_name (parser
, template_keyword_p
,
11490 check_dependency_p
,
11493 if (templ
== error_mark_node
|| is_identifier
)
11495 pop_deferring_access_checks ();
11499 /* If we find the sequence `[:' after a template-name, it's probably
11500 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11501 parse correctly the argument list. */
11502 next_token
= cp_lexer_peek_token (parser
->lexer
);
11503 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11504 if (next_token
->type
== CPP_OPEN_SQUARE
11505 && next_token
->flags
& DIGRAPH
11506 && next_token_2
->type
== CPP_COLON
11507 && !(next_token_2
->flags
& PREV_WHITE
))
11509 cp_parser_parse_tentatively (parser
);
11510 /* Change `:' into `::'. */
11511 next_token_2
->type
= CPP_SCOPE
;
11512 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11514 cp_lexer_consume_token (parser
->lexer
);
11516 /* Parse the arguments. */
11517 arguments
= cp_parser_enclosed_template_argument_list (parser
);
11518 if (!cp_parser_parse_definitely (parser
))
11520 /* If we couldn't parse an argument list, then we revert our changes
11521 and return simply an error. Maybe this is not a template-id
11523 next_token_2
->type
= CPP_COLON
;
11524 cp_parser_error (parser
, "expected %<<%>");
11525 pop_deferring_access_checks ();
11526 return error_mark_node
;
11528 /* Otherwise, emit an error about the invalid digraph, but continue
11529 parsing because we got our argument list. */
11530 if (permerror (next_token
->location
,
11531 "%<<::%> cannot begin a template-argument list"))
11533 static bool hint
= false;
11534 inform (next_token
->location
,
11535 "%<<:%> is an alternate spelling for %<[%>."
11536 " Insert whitespace between %<<%> and %<::%>");
11537 if (!hint
&& !flag_permissive
)
11539 inform (next_token
->location
, "(if you use %<-fpermissive%>"
11540 " G++ will accept your code)");
11547 /* Look for the `<' that starts the template-argument-list. */
11548 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
11550 pop_deferring_access_checks ();
11551 return error_mark_node
;
11553 /* Parse the arguments. */
11554 arguments
= cp_parser_enclosed_template_argument_list (parser
);
11557 /* Build a representation of the specialization. */
11558 if (TREE_CODE (templ
) == IDENTIFIER_NODE
)
11559 template_id
= build_min_nt (TEMPLATE_ID_EXPR
, templ
, arguments
);
11560 else if (DECL_CLASS_TEMPLATE_P (templ
)
11561 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
11563 bool entering_scope
;
11564 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11565 template (rather than some instantiation thereof) only if
11566 is not nested within some other construct. For example, in
11567 "template <typename T> void f(T) { A<T>::", A<T> is just an
11568 instantiation of A. */
11569 entering_scope
= (template_parm_scope_p ()
11570 && cp_lexer_next_token_is (parser
->lexer
,
11573 = finish_template_type (templ
, arguments
, entering_scope
);
11577 /* If it's not a class-template or a template-template, it should be
11578 a function-template. */
11579 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
11580 || TREE_CODE (templ
) == OVERLOAD
11581 || BASELINK_P (templ
)));
11583 template_id
= lookup_template_function (templ
, arguments
);
11586 /* If parsing tentatively, replace the sequence of tokens that makes
11587 up the template-id with a CPP_TEMPLATE_ID token. That way,
11588 should we re-parse the token stream, we will not have to repeat
11589 the effort required to do the parse, nor will we issue duplicate
11590 error messages about problems during instantiation of the
11594 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
11596 /* Reset the contents of the START_OF_ID token. */
11597 token
->type
= CPP_TEMPLATE_ID
;
11598 /* Retrieve any deferred checks. Do not pop this access checks yet
11599 so the memory will not be reclaimed during token replacing below. */
11600 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
11601 token
->u
.tree_check_value
->value
= template_id
;
11602 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
11603 token
->keyword
= RID_MAX
;
11605 /* Purge all subsequent tokens. */
11606 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
11608 /* ??? Can we actually assume that, if template_id ==
11609 error_mark_node, we will have issued a diagnostic to the
11610 user, as opposed to simply marking the tentative parse as
11612 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
11613 error_at (token
->location
, "parse error in template argument list");
11616 pop_deferring_access_checks ();
11617 return template_id
;
11620 /* Parse a template-name.
11625 The standard should actually say:
11629 operator-function-id
11631 A defect report has been filed about this issue.
11633 A conversion-function-id cannot be a template name because they cannot
11634 be part of a template-id. In fact, looking at this code:
11636 a.operator K<int>()
11638 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11639 It is impossible to call a templated conversion-function-id with an
11640 explicit argument list, since the only allowed template parameter is
11641 the type to which it is converting.
11643 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11644 `template' keyword, in a construction like:
11648 In that case `f' is taken to be a template-name, even though there
11649 is no way of knowing for sure.
11651 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11652 name refers to a set of overloaded functions, at least one of which
11653 is a template, or an IDENTIFIER_NODE with the name of the template,
11654 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11655 names are looked up inside uninstantiated templates. */
11658 cp_parser_template_name (cp_parser
* parser
,
11659 bool template_keyword_p
,
11660 bool check_dependency_p
,
11661 bool is_declaration
,
11662 bool *is_identifier
)
11667 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11669 /* If the next token is `operator', then we have either an
11670 operator-function-id or a conversion-function-id. */
11671 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
11673 /* We don't know whether we're looking at an
11674 operator-function-id or a conversion-function-id. */
11675 cp_parser_parse_tentatively (parser
);
11676 /* Try an operator-function-id. */
11677 identifier
= cp_parser_operator_function_id (parser
);
11678 /* If that didn't work, try a conversion-function-id. */
11679 if (!cp_parser_parse_definitely (parser
))
11681 cp_parser_error (parser
, "expected template-name");
11682 return error_mark_node
;
11685 /* Look for the identifier. */
11687 identifier
= cp_parser_identifier (parser
);
11689 /* If we didn't find an identifier, we don't have a template-id. */
11690 if (identifier
== error_mark_node
)
11691 return error_mark_node
;
11693 /* If the name immediately followed the `template' keyword, then it
11694 is a template-name. However, if the next token is not `<', then
11695 we do not treat it as a template-name, since it is not being used
11696 as part of a template-id. This enables us to handle constructs
11699 template <typename T> struct S { S(); };
11700 template <typename T> S<T>::S();
11702 correctly. We would treat `S' as a template -- if it were `S<T>'
11703 -- but we do not if there is no `<'. */
11705 if (processing_template_decl
11706 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
11708 /* In a declaration, in a dependent context, we pretend that the
11709 "template" keyword was present in order to improve error
11710 recovery. For example, given:
11712 template <typename T> void f(T::X<int>);
11714 we want to treat "X<int>" as a template-id. */
11716 && !template_keyword_p
11717 && parser
->scope
&& TYPE_P (parser
->scope
)
11718 && check_dependency_p
11719 && dependent_scope_p (parser
->scope
)
11720 /* Do not do this for dtors (or ctors), since they never
11721 need the template keyword before their name. */
11722 && !constructor_name_p (identifier
, parser
->scope
))
11724 cp_token_position start
= 0;
11726 /* Explain what went wrong. */
11727 error_at (token
->location
, "non-template %qD used as template",
11729 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
11730 parser
->scope
, identifier
);
11731 /* If parsing tentatively, find the location of the "<" token. */
11732 if (cp_parser_simulate_error (parser
))
11733 start
= cp_lexer_token_position (parser
->lexer
, true);
11734 /* Parse the template arguments so that we can issue error
11735 messages about them. */
11736 cp_lexer_consume_token (parser
->lexer
);
11737 cp_parser_enclosed_template_argument_list (parser
);
11738 /* Skip tokens until we find a good place from which to
11739 continue parsing. */
11740 cp_parser_skip_to_closing_parenthesis (parser
,
11741 /*recovering=*/true,
11743 /*consume_paren=*/false);
11744 /* If parsing tentatively, permanently remove the
11745 template argument list. That will prevent duplicate
11746 error messages from being issued about the missing
11747 "template" keyword. */
11749 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
11751 *is_identifier
= true;
11755 /* If the "template" keyword is present, then there is generally
11756 no point in doing name-lookup, so we just return IDENTIFIER.
11757 But, if the qualifying scope is non-dependent then we can
11758 (and must) do name-lookup normally. */
11759 if (template_keyword_p
11761 || (TYPE_P (parser
->scope
)
11762 && dependent_type_p (parser
->scope
))))
11766 /* Look up the name. */
11767 decl
= cp_parser_lookup_name (parser
, identifier
,
11769 /*is_template=*/true,
11770 /*is_namespace=*/false,
11771 check_dependency_p
,
11772 /*ambiguous_decls=*/NULL
,
11775 /* If DECL is a template, then the name was a template-name. */
11776 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
11780 tree fn
= NULL_TREE
;
11782 /* The standard does not explicitly indicate whether a name that
11783 names a set of overloaded declarations, some of which are
11784 templates, is a template-name. However, such a name should
11785 be a template-name; otherwise, there is no way to form a
11786 template-id for the overloaded templates. */
11787 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
11788 if (TREE_CODE (fns
) == OVERLOAD
)
11789 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
11790 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
11795 /* The name does not name a template. */
11796 cp_parser_error (parser
, "expected template-name");
11797 return error_mark_node
;
11801 /* If DECL is dependent, and refers to a function, then just return
11802 its name; we will look it up again during template instantiation. */
11803 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
11805 tree scope
= CP_DECL_CONTEXT (get_first_fn (decl
));
11806 if (TYPE_P (scope
) && dependent_type_p (scope
))
11813 /* Parse a template-argument-list.
11815 template-argument-list:
11816 template-argument ... [opt]
11817 template-argument-list , template-argument ... [opt]
11819 Returns a TREE_VEC containing the arguments. */
11822 cp_parser_template_argument_list (cp_parser
* parser
)
11824 tree fixed_args
[10];
11825 unsigned n_args
= 0;
11826 unsigned alloced
= 10;
11827 tree
*arg_ary
= fixed_args
;
11829 bool saved_in_template_argument_list_p
;
11831 bool saved_non_ice_p
;
11833 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
11834 parser
->in_template_argument_list_p
= true;
11835 /* Even if the template-id appears in an integral
11836 constant-expression, the contents of the argument list do
11838 saved_ice_p
= parser
->integral_constant_expression_p
;
11839 parser
->integral_constant_expression_p
= false;
11840 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
11841 parser
->non_integral_constant_expression_p
= false;
11842 /* Parse the arguments. */
11848 /* Consume the comma. */
11849 cp_lexer_consume_token (parser
->lexer
);
11851 /* Parse the template-argument. */
11852 argument
= cp_parser_template_argument (parser
);
11854 /* If the next token is an ellipsis, we're expanding a template
11856 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11858 if (argument
== error_mark_node
)
11860 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11861 error_at (token
->location
,
11862 "expected parameter pack before %<...%>");
11864 /* Consume the `...' token. */
11865 cp_lexer_consume_token (parser
->lexer
);
11867 /* Make the argument into a TYPE_PACK_EXPANSION or
11868 EXPR_PACK_EXPANSION. */
11869 argument
= make_pack_expansion (argument
);
11872 if (n_args
== alloced
)
11876 if (arg_ary
== fixed_args
)
11878 arg_ary
= XNEWVEC (tree
, alloced
);
11879 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
11882 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
11884 arg_ary
[n_args
++] = argument
;
11886 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
11888 vec
= make_tree_vec (n_args
);
11891 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
11893 if (arg_ary
!= fixed_args
)
11895 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
11896 parser
->integral_constant_expression_p
= saved_ice_p
;
11897 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
11898 #ifdef ENABLE_CHECKING
11899 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
11904 /* Parse a template-argument.
11907 assignment-expression
11911 The representation is that of an assignment-expression, type-id, or
11912 id-expression -- except that the qualified id-expression is
11913 evaluated, so that the value returned is either a DECL or an
11916 Although the standard says "assignment-expression", it forbids
11917 throw-expressions or assignments in the template argument.
11918 Therefore, we use "conditional-expression" instead. */
11921 cp_parser_template_argument (cp_parser
* parser
)
11926 bool maybe_type_id
= false;
11927 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
11930 /* There's really no way to know what we're looking at, so we just
11931 try each alternative in order.
11935 In a template-argument, an ambiguity between a type-id and an
11936 expression is resolved to a type-id, regardless of the form of
11937 the corresponding template-parameter.
11939 Therefore, we try a type-id first. */
11940 cp_parser_parse_tentatively (parser
);
11941 argument
= cp_parser_template_type_arg (parser
);
11942 /* If there was no error parsing the type-id but the next token is a
11943 '>>', our behavior depends on which dialect of C++ we're
11944 parsing. In C++98, we probably found a typo for '> >'. But there
11945 are type-id which are also valid expressions. For instance:
11947 struct X { int operator >> (int); };
11948 template <int V> struct Foo {};
11951 Here 'X()' is a valid type-id of a function type, but the user just
11952 wanted to write the expression "X() >> 5". Thus, we remember that we
11953 found a valid type-id, but we still try to parse the argument as an
11954 expression to see what happens.
11956 In C++0x, the '>>' will be considered two separate '>'
11958 if (!cp_parser_error_occurred (parser
)
11959 && cxx_dialect
== cxx98
11960 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
11962 maybe_type_id
= true;
11963 cp_parser_abort_tentative_parse (parser
);
11967 /* If the next token isn't a `,' or a `>', then this argument wasn't
11968 really finished. This means that the argument is not a valid
11970 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11971 cp_parser_error (parser
, "expected template-argument");
11972 /* If that worked, we're done. */
11973 if (cp_parser_parse_definitely (parser
))
11976 /* We're still not sure what the argument will be. */
11977 cp_parser_parse_tentatively (parser
);
11978 /* Try a template. */
11979 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
11980 argument
= cp_parser_id_expression (parser
,
11981 /*template_keyword_p=*/false,
11982 /*check_dependency_p=*/true,
11984 /*declarator_p=*/false,
11985 /*optional_p=*/false);
11986 /* If the next token isn't a `,' or a `>', then this argument wasn't
11987 really finished. */
11988 if (!cp_parser_next_token_ends_template_argument_p (parser
))
11989 cp_parser_error (parser
, "expected template-argument");
11990 if (!cp_parser_error_occurred (parser
))
11992 /* Figure out what is being referred to. If the id-expression
11993 was for a class template specialization, then we will have a
11994 TYPE_DECL at this point. There is no need to do name lookup
11995 at this point in that case. */
11996 if (TREE_CODE (argument
) != TYPE_DECL
)
11997 argument
= cp_parser_lookup_name (parser
, argument
,
11999 /*is_template=*/template_p
,
12000 /*is_namespace=*/false,
12001 /*check_dependency=*/true,
12002 /*ambiguous_decls=*/NULL
,
12003 argument_start_token
->location
);
12004 if (TREE_CODE (argument
) != TEMPLATE_DECL
12005 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
12006 cp_parser_error (parser
, "expected template-name");
12008 if (cp_parser_parse_definitely (parser
))
12010 /* It must be a non-type argument. There permitted cases are given
12011 in [temp.arg.nontype]:
12013 -- an integral constant-expression of integral or enumeration
12016 -- the name of a non-type template-parameter; or
12018 -- the name of an object or function with external linkage...
12020 -- the address of an object or function with external linkage...
12022 -- a pointer to member... */
12023 /* Look for a non-type template parameter. */
12024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12026 cp_parser_parse_tentatively (parser
);
12027 argument
= cp_parser_primary_expression (parser
,
12028 /*address_p=*/false,
12030 /*template_arg_p=*/true,
12032 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
12033 || !cp_parser_next_token_ends_template_argument_p (parser
))
12034 cp_parser_simulate_error (parser
);
12035 if (cp_parser_parse_definitely (parser
))
12039 /* If the next token is "&", the argument must be the address of an
12040 object or function with external linkage. */
12041 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
12043 cp_lexer_consume_token (parser
->lexer
);
12044 /* See if we might have an id-expression. */
12045 token
= cp_lexer_peek_token (parser
->lexer
);
12046 if (token
->type
== CPP_NAME
12047 || token
->keyword
== RID_OPERATOR
12048 || token
->type
== CPP_SCOPE
12049 || token
->type
== CPP_TEMPLATE_ID
12050 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
12052 cp_parser_parse_tentatively (parser
);
12053 argument
= cp_parser_primary_expression (parser
,
12056 /*template_arg_p=*/true,
12058 if (cp_parser_error_occurred (parser
)
12059 || !cp_parser_next_token_ends_template_argument_p (parser
))
12060 cp_parser_abort_tentative_parse (parser
);
12065 if (TREE_CODE (argument
) == INDIRECT_REF
)
12067 gcc_assert (REFERENCE_REF_P (argument
));
12068 argument
= TREE_OPERAND (argument
, 0);
12071 /* If we're in a template, we represent a qualified-id referring
12072 to a static data member as a SCOPE_REF even if the scope isn't
12073 dependent so that we can check access control later. */
12075 if (TREE_CODE (probe
) == SCOPE_REF
)
12076 probe
= TREE_OPERAND (probe
, 1);
12077 if (TREE_CODE (probe
) == VAR_DECL
)
12079 /* A variable without external linkage might still be a
12080 valid constant-expression, so no error is issued here
12081 if the external-linkage check fails. */
12082 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
12083 cp_parser_simulate_error (parser
);
12085 else if (is_overloaded_fn (argument
))
12086 /* All overloaded functions are allowed; if the external
12087 linkage test does not pass, an error will be issued
12091 && (TREE_CODE (argument
) == OFFSET_REF
12092 || TREE_CODE (argument
) == SCOPE_REF
))
12093 /* A pointer-to-member. */
12095 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
12098 cp_parser_simulate_error (parser
);
12100 if (cp_parser_parse_definitely (parser
))
12103 argument
= build_x_unary_op (ADDR_EXPR
, argument
,
12104 tf_warning_or_error
);
12109 /* If the argument started with "&", there are no other valid
12110 alternatives at this point. */
12113 cp_parser_error (parser
, "invalid non-type template argument");
12114 return error_mark_node
;
12117 /* If the argument wasn't successfully parsed as a type-id followed
12118 by '>>', the argument can only be a constant expression now.
12119 Otherwise, we try parsing the constant-expression tentatively,
12120 because the argument could really be a type-id. */
12122 cp_parser_parse_tentatively (parser
);
12123 argument
= cp_parser_constant_expression (parser
,
12124 /*allow_non_constant_p=*/false,
12125 /*non_constant_p=*/NULL
);
12126 argument
= fold_non_dependent_expr (argument
);
12127 if (!maybe_type_id
)
12129 if (!cp_parser_next_token_ends_template_argument_p (parser
))
12130 cp_parser_error (parser
, "expected template-argument");
12131 if (cp_parser_parse_definitely (parser
))
12133 /* We did our best to parse the argument as a non type-id, but that
12134 was the only alternative that matched (albeit with a '>' after
12135 it). We can assume it's just a typo from the user, and a
12136 diagnostic will then be issued. */
12137 return cp_parser_template_type_arg (parser
);
12140 /* Parse an explicit-instantiation.
12142 explicit-instantiation:
12143 template declaration
12145 Although the standard says `declaration', what it really means is:
12147 explicit-instantiation:
12148 template decl-specifier-seq [opt] declarator [opt] ;
12150 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12151 supposed to be allowed. A defect report has been filed about this
12156 explicit-instantiation:
12157 storage-class-specifier template
12158 decl-specifier-seq [opt] declarator [opt] ;
12159 function-specifier template
12160 decl-specifier-seq [opt] declarator [opt] ; */
12163 cp_parser_explicit_instantiation (cp_parser
* parser
)
12165 int declares_class_or_enum
;
12166 cp_decl_specifier_seq decl_specifiers
;
12167 tree extension_specifier
= NULL_TREE
;
12169 /* Look for an (optional) storage-class-specifier or
12170 function-specifier. */
12171 if (cp_parser_allow_gnu_extensions_p (parser
))
12173 extension_specifier
12174 = cp_parser_storage_class_specifier_opt (parser
);
12175 if (!extension_specifier
)
12176 extension_specifier
12177 = cp_parser_function_specifier_opt (parser
,
12178 /*decl_specs=*/NULL
);
12181 /* Look for the `template' keyword. */
12182 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
12183 /* Let the front end know that we are processing an explicit
12185 begin_explicit_instantiation ();
12186 /* [temp.explicit] says that we are supposed to ignore access
12187 control while processing explicit instantiation directives. */
12188 push_deferring_access_checks (dk_no_check
);
12189 /* Parse a decl-specifier-seq. */
12190 cp_parser_decl_specifier_seq (parser
,
12191 CP_PARSER_FLAGS_OPTIONAL
,
12193 &declares_class_or_enum
);
12194 /* If there was exactly one decl-specifier, and it declared a class,
12195 and there's no declarator, then we have an explicit type
12197 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
12201 type
= check_tag_decl (&decl_specifiers
);
12202 /* Turn access control back on for names used during
12203 template instantiation. */
12204 pop_deferring_access_checks ();
12206 do_type_instantiation (type
, extension_specifier
,
12207 /*complain=*/tf_error
);
12211 cp_declarator
*declarator
;
12214 /* Parse the declarator. */
12216 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12217 /*ctor_dtor_or_conv_p=*/NULL
,
12218 /*parenthesized_p=*/NULL
,
12219 /*member_p=*/false);
12220 if (declares_class_or_enum
& 2)
12221 cp_parser_check_for_definition_in_return_type (declarator
,
12222 decl_specifiers
.type
,
12223 decl_specifiers
.type_location
);
12224 if (declarator
!= cp_error_declarator
)
12226 decl
= grokdeclarator (declarator
, &decl_specifiers
,
12227 NORMAL
, 0, &decl_specifiers
.attributes
);
12228 /* Turn access control back on for names used during
12229 template instantiation. */
12230 pop_deferring_access_checks ();
12231 /* Do the explicit instantiation. */
12232 do_decl_instantiation (decl
, extension_specifier
);
12236 pop_deferring_access_checks ();
12237 /* Skip the body of the explicit instantiation. */
12238 cp_parser_skip_to_end_of_statement (parser
);
12241 /* We're done with the instantiation. */
12242 end_explicit_instantiation ();
12244 cp_parser_consume_semicolon_at_end_of_statement (parser
);
12247 /* Parse an explicit-specialization.
12249 explicit-specialization:
12250 template < > declaration
12252 Although the standard says `declaration', what it really means is:
12254 explicit-specialization:
12255 template <> decl-specifier [opt] init-declarator [opt] ;
12256 template <> function-definition
12257 template <> explicit-specialization
12258 template <> template-declaration */
12261 cp_parser_explicit_specialization (cp_parser
* parser
)
12263 bool need_lang_pop
;
12264 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12266 /* Look for the `template' keyword. */
12267 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
12268 /* Look for the `<'. */
12269 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
12270 /* Look for the `>'. */
12271 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
12272 /* We have processed another parameter list. */
12273 ++parser
->num_template_parameter_lists
;
12276 A template ... explicit specialization ... shall not have C
12278 if (current_lang_name
== lang_name_c
)
12280 error_at (token
->location
, "template specialization with C linkage");
12281 /* Give it C++ linkage to avoid confusing other parts of the
12283 push_lang_context (lang_name_cplusplus
);
12284 need_lang_pop
= true;
12287 need_lang_pop
= false;
12288 /* Let the front end know that we are beginning a specialization. */
12289 if (!begin_specialization ())
12291 end_specialization ();
12295 /* If the next keyword is `template', we need to figure out whether
12296 or not we're looking a template-declaration. */
12297 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
12299 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
12300 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
12301 cp_parser_template_declaration_after_export (parser
,
12302 /*member_p=*/false);
12304 cp_parser_explicit_specialization (parser
);
12307 /* Parse the dependent declaration. */
12308 cp_parser_single_declaration (parser
,
12310 /*member_p=*/false,
12311 /*explicit_specialization_p=*/true,
12312 /*friend_p=*/NULL
);
12313 /* We're done with the specialization. */
12314 end_specialization ();
12315 /* For the erroneous case of a template with C linkage, we pushed an
12316 implicit C++ linkage scope; exit that scope now. */
12318 pop_lang_context ();
12319 /* We're done with this parameter list. */
12320 --parser
->num_template_parameter_lists
;
12323 /* Parse a type-specifier.
12326 simple-type-specifier
12329 elaborated-type-specifier
12337 Returns a representation of the type-specifier. For a
12338 class-specifier, enum-specifier, or elaborated-type-specifier, a
12339 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12341 The parser flags FLAGS is used to control type-specifier parsing.
12343 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12344 in a decl-specifier-seq.
12346 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12347 class-specifier, enum-specifier, or elaborated-type-specifier, then
12348 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12349 if a type is declared; 2 if it is defined. Otherwise, it is set to
12352 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12353 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12354 is set to FALSE. */
12357 cp_parser_type_specifier (cp_parser
* parser
,
12358 cp_parser_flags flags
,
12359 cp_decl_specifier_seq
*decl_specs
,
12360 bool is_declaration
,
12361 int* declares_class_or_enum
,
12362 bool* is_cv_qualifier
)
12364 tree type_spec
= NULL_TREE
;
12367 cp_decl_spec ds
= ds_last
;
12369 /* Assume this type-specifier does not declare a new type. */
12370 if (declares_class_or_enum
)
12371 *declares_class_or_enum
= 0;
12372 /* And that it does not specify a cv-qualifier. */
12373 if (is_cv_qualifier
)
12374 *is_cv_qualifier
= false;
12375 /* Peek at the next token. */
12376 token
= cp_lexer_peek_token (parser
->lexer
);
12378 /* If we're looking at a keyword, we can use that to guide the
12379 production we choose. */
12380 keyword
= token
->keyword
;
12384 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
12385 goto elaborated_type_specifier
;
12387 /* Look for the enum-specifier. */
12388 type_spec
= cp_parser_enum_specifier (parser
);
12389 /* If that worked, we're done. */
12392 if (declares_class_or_enum
)
12393 *declares_class_or_enum
= 2;
12395 cp_parser_set_decl_spec_type (decl_specs
,
12398 /*user_defined_p=*/true);
12402 goto elaborated_type_specifier
;
12404 /* Any of these indicate either a class-specifier, or an
12405 elaborated-type-specifier. */
12409 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
12410 goto elaborated_type_specifier
;
12412 /* Parse tentatively so that we can back up if we don't find a
12413 class-specifier. */
12414 cp_parser_parse_tentatively (parser
);
12415 /* Look for the class-specifier. */
12416 type_spec
= cp_parser_class_specifier (parser
);
12417 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
12418 /* If that worked, we're done. */
12419 if (cp_parser_parse_definitely (parser
))
12421 if (declares_class_or_enum
)
12422 *declares_class_or_enum
= 2;
12424 cp_parser_set_decl_spec_type (decl_specs
,
12427 /*user_defined_p=*/true);
12431 /* Fall through. */
12432 elaborated_type_specifier
:
12433 /* We're declaring (not defining) a class or enum. */
12434 if (declares_class_or_enum
)
12435 *declares_class_or_enum
= 1;
12437 /* Fall through. */
12439 /* Look for an elaborated-type-specifier. */
12441 = (cp_parser_elaborated_type_specifier
12443 decl_specs
&& decl_specs
->specs
[(int) ds_friend
],
12446 cp_parser_set_decl_spec_type (decl_specs
,
12449 /*user_defined_p=*/true);
12454 if (is_cv_qualifier
)
12455 *is_cv_qualifier
= true;
12460 if (is_cv_qualifier
)
12461 *is_cv_qualifier
= true;
12466 if (is_cv_qualifier
)
12467 *is_cv_qualifier
= true;
12471 /* The `__complex__' keyword is a GNU extension. */
12479 /* Handle simple keywords. */
12484 ++decl_specs
->specs
[(int)ds
];
12485 decl_specs
->any_specifiers_p
= true;
12487 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12490 /* If we do not already have a type-specifier, assume we are looking
12491 at a simple-type-specifier. */
12492 type_spec
= cp_parser_simple_type_specifier (parser
,
12496 /* If we didn't find a type-specifier, and a type-specifier was not
12497 optional in this context, issue an error message. */
12498 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12500 cp_parser_error (parser
, "expected type specifier");
12501 return error_mark_node
;
12507 /* Parse a simple-type-specifier.
12509 simple-type-specifier:
12510 :: [opt] nested-name-specifier [opt] type-name
12511 :: [opt] nested-name-specifier template template-id
12526 simple-type-specifier:
12528 decltype ( expression )
12534 simple-type-specifier:
12536 __typeof__ unary-expression
12537 __typeof__ ( type-id )
12539 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12540 appropriately updated. */
12543 cp_parser_simple_type_specifier (cp_parser
* parser
,
12544 cp_decl_specifier_seq
*decl_specs
,
12545 cp_parser_flags flags
)
12547 tree type
= NULL_TREE
;
12550 /* Peek at the next token. */
12551 token
= cp_lexer_peek_token (parser
->lexer
);
12553 /* If we're looking at a keyword, things are easy. */
12554 switch (token
->keyword
)
12558 decl_specs
->explicit_char_p
= true;
12559 type
= char_type_node
;
12562 type
= char16_type_node
;
12565 type
= char32_type_node
;
12568 type
= wchar_type_node
;
12571 type
= boolean_type_node
;
12575 ++decl_specs
->specs
[(int) ds_short
];
12576 type
= short_integer_type_node
;
12580 decl_specs
->explicit_int_p
= true;
12581 type
= integer_type_node
;
12584 if (!int128_integer_type_node
)
12587 decl_specs
->explicit_int128_p
= true;
12588 type
= int128_integer_type_node
;
12592 ++decl_specs
->specs
[(int) ds_long
];
12593 type
= long_integer_type_node
;
12597 ++decl_specs
->specs
[(int) ds_signed
];
12598 type
= integer_type_node
;
12602 ++decl_specs
->specs
[(int) ds_unsigned
];
12603 type
= unsigned_type_node
;
12606 type
= float_type_node
;
12609 type
= double_type_node
;
12612 type
= void_type_node
;
12616 maybe_warn_cpp0x (CPP0X_AUTO
);
12617 type
= make_auto ();
12621 /* Parse the `decltype' type. */
12622 type
= cp_parser_decltype (parser
);
12625 cp_parser_set_decl_spec_type (decl_specs
, type
,
12627 /*user_defined_p=*/true);
12632 /* Consume the `typeof' token. */
12633 cp_lexer_consume_token (parser
->lexer
);
12634 /* Parse the operand to `typeof'. */
12635 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
12636 /* If it is not already a TYPE, take its type. */
12637 if (!TYPE_P (type
))
12638 type
= finish_typeof (type
);
12641 cp_parser_set_decl_spec_type (decl_specs
, type
,
12643 /*user_defined_p=*/true);
12651 /* If the type-specifier was for a built-in type, we're done. */
12654 /* Record the type. */
12656 && (token
->keyword
!= RID_SIGNED
12657 && token
->keyword
!= RID_UNSIGNED
12658 && token
->keyword
!= RID_SHORT
12659 && token
->keyword
!= RID_LONG
))
12660 cp_parser_set_decl_spec_type (decl_specs
,
12663 /*user_defined=*/false);
12665 decl_specs
->any_specifiers_p
= true;
12667 /* Consume the token. */
12668 cp_lexer_consume_token (parser
->lexer
);
12670 /* There is no valid C++ program where a non-template type is
12671 followed by a "<". That usually indicates that the user thought
12672 that the type was a template. */
12673 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
12675 return TYPE_NAME (type
);
12678 /* The type-specifier must be a user-defined type. */
12679 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
12684 /* Don't gobble tokens or issue error messages if this is an
12685 optional type-specifier. */
12686 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
12687 cp_parser_parse_tentatively (parser
);
12689 /* Look for the optional `::' operator. */
12691 = (cp_parser_global_scope_opt (parser
,
12692 /*current_scope_valid_p=*/false)
12694 /* Look for the nested-name specifier. */
12696 = (cp_parser_nested_name_specifier_opt (parser
,
12697 /*typename_keyword_p=*/false,
12698 /*check_dependency_p=*/true,
12700 /*is_declaration=*/false)
12702 token
= cp_lexer_peek_token (parser
->lexer
);
12703 /* If we have seen a nested-name-specifier, and the next token
12704 is `template', then we are using the template-id production. */
12706 && cp_parser_optional_template_keyword (parser
))
12708 /* Look for the template-id. */
12709 type
= cp_parser_template_id (parser
,
12710 /*template_keyword_p=*/true,
12711 /*check_dependency_p=*/true,
12712 /*is_declaration=*/false);
12713 /* If the template-id did not name a type, we are out of
12715 if (TREE_CODE (type
) != TYPE_DECL
)
12717 cp_parser_error (parser
, "expected template-id for type");
12721 /* Otherwise, look for a type-name. */
12723 type
= cp_parser_type_name (parser
);
12724 /* Keep track of all name-lookups performed in class scopes. */
12728 && TREE_CODE (type
) == TYPE_DECL
12729 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
12730 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
12731 /* If it didn't work out, we don't have a TYPE. */
12732 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
12733 && !cp_parser_parse_definitely (parser
))
12735 if (type
&& decl_specs
)
12736 cp_parser_set_decl_spec_type (decl_specs
, type
,
12738 /*user_defined=*/true);
12741 /* If we didn't get a type-name, issue an error message. */
12742 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
12744 cp_parser_error (parser
, "expected type-name");
12745 return error_mark_node
;
12748 /* There is no valid C++ program where a non-template type is
12749 followed by a "<". That usually indicates that the user thought
12750 that the type was a template. */
12751 if (type
&& type
!= error_mark_node
)
12753 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12754 If it is, then the '<'...'>' enclose protocol names rather than
12755 template arguments, and so everything is fine. */
12756 if (c_dialect_objc () && !parser
->scope
12757 && (objc_is_id (type
) || objc_is_class_name (type
)))
12759 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12760 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
12762 /* Clobber the "unqualified" type previously entered into
12763 DECL_SPECS with the new, improved protocol-qualified version. */
12765 decl_specs
->type
= qual_type
;
12770 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
12777 /* Parse a type-name.
12790 Returns a TYPE_DECL for the type. */
12793 cp_parser_type_name (cp_parser
* parser
)
12797 /* We can't know yet whether it is a class-name or not. */
12798 cp_parser_parse_tentatively (parser
);
12799 /* Try a class-name. */
12800 type_decl
= cp_parser_class_name (parser
,
12801 /*typename_keyword_p=*/false,
12802 /*template_keyword_p=*/false,
12804 /*check_dependency_p=*/true,
12805 /*class_head_p=*/false,
12806 /*is_declaration=*/false);
12807 /* If it's not a class-name, keep looking. */
12808 if (!cp_parser_parse_definitely (parser
))
12810 /* It must be a typedef-name or an enum-name. */
12811 return cp_parser_nonclass_name (parser
);
12817 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12825 Returns a TYPE_DECL for the type. */
12828 cp_parser_nonclass_name (cp_parser
* parser
)
12833 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12834 identifier
= cp_parser_identifier (parser
);
12835 if (identifier
== error_mark_node
)
12836 return error_mark_node
;
12838 /* Look up the type-name. */
12839 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
12841 if (TREE_CODE (type_decl
) != TYPE_DECL
12842 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
12844 /* See if this is an Objective-C type. */
12845 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
12846 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
12848 type_decl
= TYPE_NAME (type
);
12851 /* Issue an error if we did not find a type-name. */
12852 if (TREE_CODE (type_decl
) != TYPE_DECL
)
12854 if (!cp_parser_simulate_error (parser
))
12855 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
12856 NLE_TYPE
, token
->location
);
12857 return error_mark_node
;
12859 /* Remember that the name was used in the definition of the
12860 current class so that we can check later to see if the
12861 meaning would have been different after the class was
12862 entirely defined. */
12863 else if (type_decl
!= error_mark_node
12865 maybe_note_name_used_in_class (identifier
, type_decl
);
12870 /* Parse an elaborated-type-specifier. Note that the grammar given
12871 here incorporates the resolution to DR68.
12873 elaborated-type-specifier:
12874 class-key :: [opt] nested-name-specifier [opt] identifier
12875 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12876 enum-key :: [opt] nested-name-specifier [opt] identifier
12877 typename :: [opt] nested-name-specifier identifier
12878 typename :: [opt] nested-name-specifier template [opt]
12883 elaborated-type-specifier:
12884 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12885 class-key attributes :: [opt] nested-name-specifier [opt]
12886 template [opt] template-id
12887 enum attributes :: [opt] nested-name-specifier [opt] identifier
12889 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12890 declared `friend'. If IS_DECLARATION is TRUE, then this
12891 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12892 something is being declared.
12894 Returns the TYPE specified. */
12897 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
12899 bool is_declaration
)
12901 enum tag_types tag_type
;
12903 tree type
= NULL_TREE
;
12904 tree attributes
= NULL_TREE
;
12906 cp_token
*token
= NULL
;
12908 /* See if we're looking at the `enum' keyword. */
12909 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
12911 /* Consume the `enum' token. */
12912 cp_lexer_consume_token (parser
->lexer
);
12913 /* Remember that it's an enumeration type. */
12914 tag_type
= enum_type
;
12915 /* Parse the optional `struct' or `class' key (for C++0x scoped
12917 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
12918 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
12920 if (cxx_dialect
== cxx98
)
12921 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
12923 /* Consume the `struct' or `class'. */
12924 cp_lexer_consume_token (parser
->lexer
);
12926 /* Parse the attributes. */
12927 attributes
= cp_parser_attributes_opt (parser
);
12929 /* Or, it might be `typename'. */
12930 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12933 /* Consume the `typename' token. */
12934 cp_lexer_consume_token (parser
->lexer
);
12935 /* Remember that it's a `typename' type. */
12936 tag_type
= typename_type
;
12938 /* Otherwise it must be a class-key. */
12941 tag_type
= cp_parser_class_key (parser
);
12942 if (tag_type
== none_type
)
12943 return error_mark_node
;
12944 /* Parse the attributes. */
12945 attributes
= cp_parser_attributes_opt (parser
);
12948 /* Look for the `::' operator. */
12949 globalscope
= cp_parser_global_scope_opt (parser
,
12950 /*current_scope_valid_p=*/false);
12951 /* Look for the nested-name-specifier. */
12952 if (tag_type
== typename_type
&& !globalscope
)
12954 if (!cp_parser_nested_name_specifier (parser
,
12955 /*typename_keyword_p=*/true,
12956 /*check_dependency_p=*/true,
12959 return error_mark_node
;
12962 /* Even though `typename' is not present, the proposed resolution
12963 to Core Issue 180 says that in `class A<T>::B', `B' should be
12964 considered a type-name, even if `A<T>' is dependent. */
12965 cp_parser_nested_name_specifier_opt (parser
,
12966 /*typename_keyword_p=*/true,
12967 /*check_dependency_p=*/true,
12970 /* For everything but enumeration types, consider a template-id.
12971 For an enumeration type, consider only a plain identifier. */
12972 if (tag_type
!= enum_type
)
12974 bool template_p
= false;
12977 /* Allow the `template' keyword. */
12978 template_p
= cp_parser_optional_template_keyword (parser
);
12979 /* If we didn't see `template', we don't know if there's a
12980 template-id or not. */
12982 cp_parser_parse_tentatively (parser
);
12983 /* Parse the template-id. */
12984 token
= cp_lexer_peek_token (parser
->lexer
);
12985 decl
= cp_parser_template_id (parser
, template_p
,
12986 /*check_dependency_p=*/true,
12988 /* If we didn't find a template-id, look for an ordinary
12990 if (!template_p
&& !cp_parser_parse_definitely (parser
))
12992 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12993 in effect, then we must assume that, upon instantiation, the
12994 template will correspond to a class. */
12995 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
12996 && tag_type
== typename_type
)
12997 type
= make_typename_type (parser
->scope
, decl
,
12999 /*complain=*/tf_error
);
13000 /* If the `typename' keyword is in effect and DECL is not a type
13001 decl. Then type is non existant. */
13002 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
13005 type
= TREE_TYPE (decl
);
13010 token
= cp_lexer_peek_token (parser
->lexer
);
13011 identifier
= cp_parser_identifier (parser
);
13013 if (identifier
== error_mark_node
)
13015 parser
->scope
= NULL_TREE
;
13016 return error_mark_node
;
13019 /* For a `typename', we needn't call xref_tag. */
13020 if (tag_type
== typename_type
13021 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
13022 return cp_parser_make_typename_type (parser
, parser
->scope
,
13025 /* Look up a qualified name in the usual way. */
13029 tree ambiguous_decls
;
13031 decl
= cp_parser_lookup_name (parser
, identifier
,
13033 /*is_template=*/false,
13034 /*is_namespace=*/false,
13035 /*check_dependency=*/true,
13039 /* If the lookup was ambiguous, an error will already have been
13041 if (ambiguous_decls
)
13042 return error_mark_node
;
13044 /* If we are parsing friend declaration, DECL may be a
13045 TEMPLATE_DECL tree node here. However, we need to check
13046 whether this TEMPLATE_DECL results in valid code. Consider
13047 the following example:
13050 template <class T> class C {};
13053 template <class T> friend class N::C; // #1, valid code
13055 template <class T> class Y {
13056 friend class N::C; // #2, invalid code
13059 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13060 name lookup of `N::C'. We see that friend declaration must
13061 be template for the code to be valid. Note that
13062 processing_template_decl does not work here since it is
13063 always 1 for the above two cases. */
13065 decl
= (cp_parser_maybe_treat_template_as_class
13066 (decl
, /*tag_name_p=*/is_friend
13067 && parser
->num_template_parameter_lists
));
13069 if (TREE_CODE (decl
) != TYPE_DECL
)
13071 cp_parser_diagnose_invalid_type_name (parser
,
13075 return error_mark_node
;
13078 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
13080 bool allow_template
= (parser
->num_template_parameter_lists
13081 || DECL_SELF_REFERENCE_P (decl
));
13082 type
= check_elaborated_type_specifier (tag_type
, decl
,
13085 if (type
== error_mark_node
)
13086 return error_mark_node
;
13089 /* Forward declarations of nested types, such as
13094 are invalid unless all components preceding the final '::'
13095 are complete. If all enclosing types are complete, these
13096 declarations become merely pointless.
13098 Invalid forward declarations of nested types are errors
13099 caught elsewhere in parsing. Those that are pointless arrive
13102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
13103 && !is_friend
&& !processing_explicit_instantiation
)
13104 warning (0, "declaration %qD does not declare anything", decl
);
13106 type
= TREE_TYPE (decl
);
13110 /* An elaborated-type-specifier sometimes introduces a new type and
13111 sometimes names an existing type. Normally, the rule is that it
13112 introduces a new type only if there is not an existing type of
13113 the same name already in scope. For example, given:
13116 void f() { struct S s; }
13118 the `struct S' in the body of `f' is the same `struct S' as in
13119 the global scope; the existing definition is used. However, if
13120 there were no global declaration, this would introduce a new
13121 local class named `S'.
13123 An exception to this rule applies to the following code:
13125 namespace N { struct S; }
13127 Here, the elaborated-type-specifier names a new type
13128 unconditionally; even if there is already an `S' in the
13129 containing scope this declaration names a new type.
13130 This exception only applies if the elaborated-type-specifier
13131 forms the complete declaration:
13135 A declaration consisting solely of `class-key identifier ;' is
13136 either a redeclaration of the name in the current scope or a
13137 forward declaration of the identifier as a class name. It
13138 introduces the name into the current scope.
13140 We are in this situation precisely when the next token is a `;'.
13142 An exception to the exception is that a `friend' declaration does
13143 *not* name a new type; i.e., given:
13145 struct S { friend struct T; };
13147 `T' is not a new type in the scope of `S'.
13149 Also, `new struct S' or `sizeof (struct S)' never results in the
13150 definition of a new type; a new type can only be declared in a
13151 declaration context. */
13157 /* Friends have special name lookup rules. */
13158 ts
= ts_within_enclosing_non_class
;
13159 else if (is_declaration
13160 && cp_lexer_next_token_is (parser
->lexer
,
13162 /* This is a `class-key identifier ;' */
13168 (parser
->num_template_parameter_lists
13169 && (cp_parser_next_token_starts_class_definition_p (parser
)
13170 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
13171 /* An unqualified name was used to reference this type, so
13172 there were no qualifying templates. */
13173 if (!cp_parser_check_template_parameters (parser
,
13174 /*num_templates=*/0,
13176 /*declarator=*/NULL
))
13177 return error_mark_node
;
13178 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
13182 if (type
== error_mark_node
)
13183 return error_mark_node
;
13185 /* Allow attributes on forward declarations of classes. */
13188 if (TREE_CODE (type
) == TYPENAME_TYPE
)
13189 warning (OPT_Wattributes
,
13190 "attributes ignored on uninstantiated type");
13191 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
13192 && ! processing_explicit_instantiation
)
13193 warning (OPT_Wattributes
,
13194 "attributes ignored on template instantiation");
13195 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
13196 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
13198 warning (OPT_Wattributes
,
13199 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13202 if (tag_type
!= enum_type
)
13203 cp_parser_check_class_key (tag_type
, type
);
13205 /* A "<" cannot follow an elaborated type specifier. If that
13206 happens, the user was probably trying to form a template-id. */
13207 cp_parser_check_for_invalid_template_id (parser
, type
, token
->location
);
13212 /* Parse an enum-specifier.
13215 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
13220 enum struct [C++0x]
13223 : type-specifier-seq
13226 enum-key attributes[opt] identifier [opt] enum-base [opt]
13227 { enumerator-list [opt] }attributes[opt]
13229 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13230 if the token stream isn't an enum-specifier after all. */
13233 cp_parser_enum_specifier (cp_parser
* parser
)
13238 bool scoped_enum_p
= false;
13239 bool has_underlying_type
= false;
13240 tree underlying_type
= NULL_TREE
;
13242 /* Parse tentatively so that we can back up if we don't find a
13244 cp_parser_parse_tentatively (parser
);
13246 /* Caller guarantees that the current token is 'enum', an identifier
13247 possibly follows, and the token after that is an opening brace.
13248 If we don't have an identifier, fabricate an anonymous name for
13249 the enumeration being defined. */
13250 cp_lexer_consume_token (parser
->lexer
);
13252 /* Parse the "class" or "struct", which indicates a scoped
13253 enumeration type in C++0x. */
13254 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
13255 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
13257 if (cxx_dialect
== cxx98
)
13258 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
13260 /* Consume the `struct' or `class' token. */
13261 cp_lexer_consume_token (parser
->lexer
);
13263 scoped_enum_p
= true;
13266 attributes
= cp_parser_attributes_opt (parser
);
13268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13269 identifier
= cp_parser_identifier (parser
);
13271 identifier
= make_anon_name ();
13273 /* Check for the `:' that denotes a specified underlying type in C++0x.
13274 Note that a ':' could also indicate a bitfield width, however. */
13275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13277 cp_decl_specifier_seq type_specifiers
;
13279 /* Consume the `:'. */
13280 cp_lexer_consume_token (parser
->lexer
);
13282 /* Parse the type-specifier-seq. */
13283 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
13284 /*is_trailing_return=*/false,
13287 /* At this point this is surely not elaborated type specifier. */
13288 if (!cp_parser_parse_definitely (parser
))
13291 if (cxx_dialect
== cxx98
)
13292 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
13294 has_underlying_type
= true;
13296 /* If that didn't work, stop. */
13297 if (type_specifiers
.type
!= error_mark_node
)
13299 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
13300 /*initialized=*/0, NULL
);
13301 if (underlying_type
== error_mark_node
)
13302 underlying_type
= NULL_TREE
;
13306 /* Look for the `{' but don't consume it yet. */
13307 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13309 cp_parser_error (parser
, "expected %<{%>");
13310 if (has_underlying_type
)
13314 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
13317 /* Issue an error message if type-definitions are forbidden here. */
13318 if (!cp_parser_check_type_definition (parser
))
13319 type
= error_mark_node
;
13321 /* Create the new type. We do this before consuming the opening
13322 brace so the enum will be recorded as being on the line of its
13323 tag (or the 'enum' keyword, if there is no tag). */
13324 type
= start_enum (identifier
, underlying_type
, scoped_enum_p
);
13326 /* Consume the opening brace. */
13327 cp_lexer_consume_token (parser
->lexer
);
13329 if (type
== error_mark_node
)
13331 cp_parser_skip_to_end_of_block_or_statement (parser
);
13332 return error_mark_node
;
13335 /* If the next token is not '}', then there are some enumerators. */
13336 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
13337 cp_parser_enumerator_list (parser
, type
);
13339 /* Consume the final '}'. */
13340 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13342 /* Look for trailing attributes to apply to this enumeration, and
13343 apply them if appropriate. */
13344 if (cp_parser_allow_gnu_extensions_p (parser
))
13346 tree trailing_attr
= cp_parser_attributes_opt (parser
);
13347 trailing_attr
= chainon (trailing_attr
, attributes
);
13348 cplus_decl_attributes (&type
,
13350 (int) ATTR_FLAG_TYPE_IN_PLACE
);
13353 /* Finish up the enumeration. */
13354 finish_enum (type
);
13359 /* Parse an enumerator-list. The enumerators all have the indicated
13363 enumerator-definition
13364 enumerator-list , enumerator-definition */
13367 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
13371 /* Parse an enumerator-definition. */
13372 cp_parser_enumerator_definition (parser
, type
);
13374 /* If the next token is not a ',', we've reached the end of
13376 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13378 /* Otherwise, consume the `,' and keep going. */
13379 cp_lexer_consume_token (parser
->lexer
);
13380 /* If the next token is a `}', there is a trailing comma. */
13381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
13383 if (!in_system_header
)
13384 pedwarn (input_location
, OPT_pedantic
, "comma at end of enumerator list");
13390 /* Parse an enumerator-definition. The enumerator has the indicated
13393 enumerator-definition:
13395 enumerator = constant-expression
13401 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
13407 /* Save the input location because we are interested in the location
13408 of the identifier and not the location of the explicit value. */
13409 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13411 /* Look for the identifier. */
13412 identifier
= cp_parser_identifier (parser
);
13413 if (identifier
== error_mark_node
)
13416 /* If the next token is an '=', then there is an explicit value. */
13417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13419 /* Consume the `=' token. */
13420 cp_lexer_consume_token (parser
->lexer
);
13421 /* Parse the value. */
13422 value
= cp_parser_constant_expression (parser
,
13423 /*allow_non_constant_p=*/false,
13429 /* If we are processing a template, make sure the initializer of the
13430 enumerator doesn't contain any bare template parameter pack. */
13431 if (check_for_bare_parameter_packs (value
))
13432 value
= error_mark_node
;
13434 /* Create the enumerator. */
13435 build_enumerator (identifier
, value
, type
, loc
);
13438 /* Parse a namespace-name.
13441 original-namespace-name
13444 Returns the NAMESPACE_DECL for the namespace. */
13447 cp_parser_namespace_name (cp_parser
* parser
)
13450 tree namespace_decl
;
13452 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13454 /* Get the name of the namespace. */
13455 identifier
= cp_parser_identifier (parser
);
13456 if (identifier
== error_mark_node
)
13457 return error_mark_node
;
13459 /* Look up the identifier in the currently active scope. Look only
13460 for namespaces, due to:
13462 [basic.lookup.udir]
13464 When looking up a namespace-name in a using-directive or alias
13465 definition, only namespace names are considered.
13469 [basic.lookup.qual]
13471 During the lookup of a name preceding the :: scope resolution
13472 operator, object, function, and enumerator names are ignored.
13474 (Note that cp_parser_qualifying_entity only calls this
13475 function if the token after the name is the scope resolution
13477 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
13479 /*is_template=*/false,
13480 /*is_namespace=*/true,
13481 /*check_dependency=*/true,
13482 /*ambiguous_decls=*/NULL
,
13484 /* If it's not a namespace, issue an error. */
13485 if (namespace_decl
== error_mark_node
13486 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
13488 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
13489 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
13490 cp_parser_error (parser
, "expected namespace-name");
13491 namespace_decl
= error_mark_node
;
13494 return namespace_decl
;
13497 /* Parse a namespace-definition.
13499 namespace-definition:
13500 named-namespace-definition
13501 unnamed-namespace-definition
13503 named-namespace-definition:
13504 original-namespace-definition
13505 extension-namespace-definition
13507 original-namespace-definition:
13508 namespace identifier { namespace-body }
13510 extension-namespace-definition:
13511 namespace original-namespace-name { namespace-body }
13513 unnamed-namespace-definition:
13514 namespace { namespace-body } */
13517 cp_parser_namespace_definition (cp_parser
* parser
)
13519 tree identifier
, attribs
;
13520 bool has_visibility
;
13523 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
13525 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
13527 cp_lexer_consume_token (parser
->lexer
);
13532 /* Look for the `namespace' keyword. */
13533 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13535 /* Get the name of the namespace. We do not attempt to distinguish
13536 between an original-namespace-definition and an
13537 extension-namespace-definition at this point. The semantic
13538 analysis routines are responsible for that. */
13539 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13540 identifier
= cp_parser_identifier (parser
);
13542 identifier
= NULL_TREE
;
13544 /* Parse any specified attributes. */
13545 attribs
= cp_parser_attributes_opt (parser
);
13547 /* Look for the `{' to start the namespace. */
13548 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
13549 /* Start the namespace. */
13550 push_namespace (identifier
);
13552 /* "inline namespace" is equivalent to a stub namespace definition
13553 followed by a strong using directive. */
13556 tree name_space
= current_namespace
;
13557 /* Set up namespace association. */
13558 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
13559 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
13560 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
13561 /* Import the contents of the inline namespace. */
13563 do_using_directive (name_space
);
13564 push_namespace (identifier
);
13567 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
13569 /* Parse the body of the namespace. */
13570 cp_parser_namespace_body (parser
);
13572 #ifdef HANDLE_PRAGMA_VISIBILITY
13573 if (has_visibility
)
13574 pop_visibility (1);
13577 /* Finish the namespace. */
13579 /* Look for the final `}'. */
13580 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13583 /* Parse a namespace-body.
13586 declaration-seq [opt] */
13589 cp_parser_namespace_body (cp_parser
* parser
)
13591 cp_parser_declaration_seq_opt (parser
);
13594 /* Parse a namespace-alias-definition.
13596 namespace-alias-definition:
13597 namespace identifier = qualified-namespace-specifier ; */
13600 cp_parser_namespace_alias_definition (cp_parser
* parser
)
13603 tree namespace_specifier
;
13605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13607 /* Look for the `namespace' keyword. */
13608 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13609 /* Look for the identifier. */
13610 identifier
= cp_parser_identifier (parser
);
13611 if (identifier
== error_mark_node
)
13613 /* Look for the `=' token. */
13614 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
13615 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13617 error_at (token
->location
, "%<namespace%> definition is not allowed here");
13618 /* Skip the definition. */
13619 cp_lexer_consume_token (parser
->lexer
);
13620 if (cp_parser_skip_to_closing_brace (parser
))
13621 cp_lexer_consume_token (parser
->lexer
);
13624 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
13625 /* Look for the qualified-namespace-specifier. */
13626 namespace_specifier
13627 = cp_parser_qualified_namespace_specifier (parser
);
13628 /* Look for the `;' token. */
13629 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13631 /* Register the alias in the symbol table. */
13632 do_namespace_alias (identifier
, namespace_specifier
);
13635 /* Parse a qualified-namespace-specifier.
13637 qualified-namespace-specifier:
13638 :: [opt] nested-name-specifier [opt] namespace-name
13640 Returns a NAMESPACE_DECL corresponding to the specified
13644 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
13646 /* Look for the optional `::'. */
13647 cp_parser_global_scope_opt (parser
,
13648 /*current_scope_valid_p=*/false);
13650 /* Look for the optional nested-name-specifier. */
13651 cp_parser_nested_name_specifier_opt (parser
,
13652 /*typename_keyword_p=*/false,
13653 /*check_dependency_p=*/true,
13655 /*is_declaration=*/true);
13657 return cp_parser_namespace_name (parser
);
13660 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13661 access declaration.
13664 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13665 using :: unqualified-id ;
13667 access-declaration:
13673 cp_parser_using_declaration (cp_parser
* parser
,
13674 bool access_declaration_p
)
13677 bool typename_p
= false;
13678 bool global_scope_p
;
13683 if (access_declaration_p
)
13684 cp_parser_parse_tentatively (parser
);
13687 /* Look for the `using' keyword. */
13688 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
13690 /* Peek at the next token. */
13691 token
= cp_lexer_peek_token (parser
->lexer
);
13692 /* See if it's `typename'. */
13693 if (token
->keyword
== RID_TYPENAME
)
13695 /* Remember that we've seen it. */
13697 /* Consume the `typename' token. */
13698 cp_lexer_consume_token (parser
->lexer
);
13702 /* Look for the optional global scope qualification. */
13704 = (cp_parser_global_scope_opt (parser
,
13705 /*current_scope_valid_p=*/false)
13708 /* If we saw `typename', or didn't see `::', then there must be a
13709 nested-name-specifier present. */
13710 if (typename_p
|| !global_scope_p
)
13711 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
13712 /*check_dependency_p=*/true,
13714 /*is_declaration=*/true);
13715 /* Otherwise, we could be in either of the two productions. In that
13716 case, treat the nested-name-specifier as optional. */
13718 qscope
= cp_parser_nested_name_specifier_opt (parser
,
13719 /*typename_keyword_p=*/false,
13720 /*check_dependency_p=*/true,
13722 /*is_declaration=*/true);
13724 qscope
= global_namespace
;
13726 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
13727 /* Something has already gone wrong; there's no need to parse
13728 further. Since an error has occurred, the return value of
13729 cp_parser_parse_definitely will be false, as required. */
13730 return cp_parser_parse_definitely (parser
);
13732 token
= cp_lexer_peek_token (parser
->lexer
);
13733 /* Parse the unqualified-id. */
13734 identifier
= cp_parser_unqualified_id (parser
,
13735 /*template_keyword_p=*/false,
13736 /*check_dependency_p=*/true,
13737 /*declarator_p=*/true,
13738 /*optional_p=*/false);
13740 if (access_declaration_p
)
13742 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13743 cp_parser_simulate_error (parser
);
13744 if (!cp_parser_parse_definitely (parser
))
13748 /* The function we call to handle a using-declaration is different
13749 depending on what scope we are in. */
13750 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
13752 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
13753 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
13754 /* [namespace.udecl]
13756 A using declaration shall not name a template-id. */
13757 error_at (token
->location
,
13758 "a template-id may not appear in a using-declaration");
13761 if (at_class_scope_p ())
13763 /* Create the USING_DECL. */
13764 decl
= do_class_using_decl (parser
->scope
, identifier
);
13766 if (check_for_bare_parameter_packs (decl
))
13769 /* Add it to the list of members in this class. */
13770 finish_member_declaration (decl
);
13774 decl
= cp_parser_lookup_name_simple (parser
,
13777 if (decl
== error_mark_node
)
13778 cp_parser_name_lookup_error (parser
, identifier
,
13781 else if (check_for_bare_parameter_packs (decl
))
13783 else if (!at_namespace_scope_p ())
13784 do_local_using_decl (decl
, qscope
, identifier
);
13786 do_toplevel_using_decl (decl
, qscope
, identifier
);
13790 /* Look for the final `;'. */
13791 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13796 /* Parse a using-directive.
13799 using namespace :: [opt] nested-name-specifier [opt]
13800 namespace-name ; */
13803 cp_parser_using_directive (cp_parser
* parser
)
13805 tree namespace_decl
;
13808 /* Look for the `using' keyword. */
13809 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
13810 /* And the `namespace' keyword. */
13811 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
13812 /* Look for the optional `::' operator. */
13813 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
13814 /* And the optional nested-name-specifier. */
13815 cp_parser_nested_name_specifier_opt (parser
,
13816 /*typename_keyword_p=*/false,
13817 /*check_dependency_p=*/true,
13819 /*is_declaration=*/true);
13820 /* Get the namespace being used. */
13821 namespace_decl
= cp_parser_namespace_name (parser
);
13822 /* And any specified attributes. */
13823 attribs
= cp_parser_attributes_opt (parser
);
13824 /* Update the symbol table. */
13825 parse_using_directive (namespace_decl
, attribs
);
13826 /* Look for the final `;'. */
13827 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13830 /* Parse an asm-definition.
13833 asm ( string-literal ) ;
13838 asm volatile [opt] ( string-literal ) ;
13839 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13840 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13841 : asm-operand-list [opt] ) ;
13842 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13843 : asm-operand-list [opt]
13844 : asm-clobber-list [opt] ) ;
13845 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13846 : asm-clobber-list [opt]
13847 : asm-goto-list ) ; */
13850 cp_parser_asm_definition (cp_parser
* parser
)
13853 tree outputs
= NULL_TREE
;
13854 tree inputs
= NULL_TREE
;
13855 tree clobbers
= NULL_TREE
;
13856 tree labels
= NULL_TREE
;
13858 bool volatile_p
= false;
13859 bool extended_p
= false;
13860 bool invalid_inputs_p
= false;
13861 bool invalid_outputs_p
= false;
13862 bool goto_p
= false;
13863 required_token missing
= RT_NONE
;
13865 /* Look for the `asm' keyword. */
13866 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
13867 /* See if the next token is `volatile'. */
13868 if (cp_parser_allow_gnu_extensions_p (parser
)
13869 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
13871 /* Remember that we saw the `volatile' keyword. */
13873 /* Consume the token. */
13874 cp_lexer_consume_token (parser
->lexer
);
13876 if (cp_parser_allow_gnu_extensions_p (parser
)
13877 && parser
->in_function_body
13878 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
13880 /* Remember that we saw the `goto' keyword. */
13882 /* Consume the token. */
13883 cp_lexer_consume_token (parser
->lexer
);
13885 /* Look for the opening `('. */
13886 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
13888 /* Look for the string. */
13889 string
= cp_parser_string_literal (parser
, false, false);
13890 if (string
== error_mark_node
)
13892 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13893 /*consume_paren=*/true);
13897 /* If we're allowing GNU extensions, check for the extended assembly
13898 syntax. Unfortunately, the `:' tokens need not be separated by
13899 a space in C, and so, for compatibility, we tolerate that here
13900 too. Doing that means that we have to treat the `::' operator as
13902 if (cp_parser_allow_gnu_extensions_p (parser
)
13903 && parser
->in_function_body
13904 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
13905 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
13907 bool inputs_p
= false;
13908 bool clobbers_p
= false;
13909 bool labels_p
= false;
13911 /* The extended syntax was used. */
13914 /* Look for outputs. */
13915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13917 /* Consume the `:'. */
13918 cp_lexer_consume_token (parser
->lexer
);
13919 /* Parse the output-operands. */
13920 if (cp_lexer_next_token_is_not (parser
->lexer
,
13922 && cp_lexer_next_token_is_not (parser
->lexer
,
13924 && cp_lexer_next_token_is_not (parser
->lexer
,
13927 outputs
= cp_parser_asm_operand_list (parser
);
13929 if (outputs
== error_mark_node
)
13930 invalid_outputs_p
= true;
13932 /* If the next token is `::', there are no outputs, and the
13933 next token is the beginning of the inputs. */
13934 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13935 /* The inputs are coming next. */
13938 /* Look for inputs. */
13940 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13942 /* Consume the `:' or `::'. */
13943 cp_lexer_consume_token (parser
->lexer
);
13944 /* Parse the output-operands. */
13945 if (cp_lexer_next_token_is_not (parser
->lexer
,
13947 && cp_lexer_next_token_is_not (parser
->lexer
,
13949 && cp_lexer_next_token_is_not (parser
->lexer
,
13951 inputs
= cp_parser_asm_operand_list (parser
);
13953 if (inputs
== error_mark_node
)
13954 invalid_inputs_p
= true;
13956 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13957 /* The clobbers are coming next. */
13960 /* Look for clobbers. */
13962 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13965 /* Consume the `:' or `::'. */
13966 cp_lexer_consume_token (parser
->lexer
);
13967 /* Parse the clobbers. */
13968 if (cp_lexer_next_token_is_not (parser
->lexer
,
13970 && cp_lexer_next_token_is_not (parser
->lexer
,
13972 clobbers
= cp_parser_asm_clobber_list (parser
);
13975 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
13976 /* The labels are coming next. */
13979 /* Look for labels. */
13981 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
13984 /* Consume the `:' or `::'. */
13985 cp_lexer_consume_token (parser
->lexer
);
13986 /* Parse the labels. */
13987 labels
= cp_parser_asm_label_list (parser
);
13990 if (goto_p
&& !labels_p
)
13991 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
13994 missing
= RT_COLON_SCOPE
;
13996 /* Look for the closing `)'. */
13997 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
13998 missing
? missing
: RT_CLOSE_PAREN
))
13999 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14000 /*consume_paren=*/true);
14001 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14003 if (!invalid_inputs_p
&& !invalid_outputs_p
)
14005 /* Create the ASM_EXPR. */
14006 if (parser
->in_function_body
)
14008 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
14009 inputs
, clobbers
, labels
);
14010 /* If the extended syntax was not used, mark the ASM_EXPR. */
14013 tree temp
= asm_stmt
;
14014 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
14015 temp
= TREE_OPERAND (temp
, 0);
14017 ASM_INPUT_P (temp
) = 1;
14021 cgraph_add_asm_node (string
);
14025 /* Declarators [gram.dcl.decl] */
14027 /* Parse an init-declarator.
14030 declarator initializer [opt]
14035 declarator asm-specification [opt] attributes [opt] initializer [opt]
14037 function-definition:
14038 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14040 decl-specifier-seq [opt] declarator function-try-block
14044 function-definition:
14045 __extension__ function-definition
14047 The DECL_SPECIFIERS apply to this declarator. Returns a
14048 representation of the entity declared. If MEMBER_P is TRUE, then
14049 this declarator appears in a class scope. The new DECL created by
14050 this declarator is returned.
14052 The CHECKS are access checks that should be performed once we know
14053 what entity is being declared (and, therefore, what classes have
14056 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14057 for a function-definition here as well. If the declarator is a
14058 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14059 be TRUE upon return. By that point, the function-definition will
14060 have been completely parsed.
14062 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14066 cp_parser_init_declarator (cp_parser
* parser
,
14067 cp_decl_specifier_seq
*decl_specifiers
,
14068 VEC (deferred_access_check
,gc
)* checks
,
14069 bool function_definition_allowed_p
,
14071 int declares_class_or_enum
,
14072 bool* function_definition_p
)
14074 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
14075 *attributes_start_token
= NULL
;
14076 cp_declarator
*declarator
;
14077 tree prefix_attributes
;
14079 tree asm_specification
;
14081 tree decl
= NULL_TREE
;
14083 int is_initialized
;
14084 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14085 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14087 enum cpp_ttype initialization_kind
;
14088 bool is_direct_init
= false;
14089 bool is_non_constant_init
;
14090 int ctor_dtor_or_conv_p
;
14092 tree pushed_scope
= NULL
;
14094 /* Gather the attributes that were provided with the
14095 decl-specifiers. */
14096 prefix_attributes
= decl_specifiers
->attributes
;
14098 /* Assume that this is not the declarator for a function
14100 if (function_definition_p
)
14101 *function_definition_p
= false;
14103 /* Defer access checks while parsing the declarator; we cannot know
14104 what names are accessible until we know what is being
14106 resume_deferring_access_checks ();
14108 /* Parse the declarator. */
14109 token
= cp_lexer_peek_token (parser
->lexer
);
14111 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14112 &ctor_dtor_or_conv_p
,
14113 /*parenthesized_p=*/NULL
,
14114 /*member_p=*/false);
14115 /* Gather up the deferred checks. */
14116 stop_deferring_access_checks ();
14118 /* If the DECLARATOR was erroneous, there's no need to go
14120 if (declarator
== cp_error_declarator
)
14121 return error_mark_node
;
14123 /* Check that the number of template-parameter-lists is OK. */
14124 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
14126 return error_mark_node
;
14128 if (declares_class_or_enum
& 2)
14129 cp_parser_check_for_definition_in_return_type (declarator
,
14130 decl_specifiers
->type
,
14131 decl_specifiers
->type_location
);
14133 /* Figure out what scope the entity declared by the DECLARATOR is
14134 located in. `grokdeclarator' sometimes changes the scope, so
14135 we compute it now. */
14136 scope
= get_scope_of_declarator (declarator
);
14138 /* Perform any lookups in the declared type which were thought to be
14139 dependent, but are not in the scope of the declarator. */
14140 decl_specifiers
->type
14141 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
14143 /* If we're allowing GNU extensions, look for an asm-specification
14145 if (cp_parser_allow_gnu_extensions_p (parser
))
14147 /* Look for an asm-specification. */
14148 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
14149 asm_specification
= cp_parser_asm_specification_opt (parser
);
14150 /* And attributes. */
14151 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
14152 attributes
= cp_parser_attributes_opt (parser
);
14156 asm_specification
= NULL_TREE
;
14157 attributes
= NULL_TREE
;
14160 /* Peek at the next token. */
14161 token
= cp_lexer_peek_token (parser
->lexer
);
14162 /* Check to see if the token indicates the start of a
14163 function-definition. */
14164 if (function_declarator_p (declarator
)
14165 && cp_parser_token_starts_function_definition_p (token
))
14167 if (!function_definition_allowed_p
)
14169 /* If a function-definition should not appear here, issue an
14171 cp_parser_error (parser
,
14172 "a function-definition is not allowed here");
14173 return error_mark_node
;
14177 location_t func_brace_location
14178 = cp_lexer_peek_token (parser
->lexer
)->location
;
14180 /* Neither attributes nor an asm-specification are allowed
14181 on a function-definition. */
14182 if (asm_specification
)
14183 error_at (asm_spec_start_token
->location
,
14184 "an asm-specification is not allowed "
14185 "on a function-definition");
14187 error_at (attributes_start_token
->location
,
14188 "attributes are not allowed on a function-definition");
14189 /* This is a function-definition. */
14190 *function_definition_p
= true;
14192 /* Parse the function definition. */
14194 decl
= cp_parser_save_member_function_body (parser
,
14197 prefix_attributes
);
14200 = (cp_parser_function_definition_from_specifiers_and_declarator
14201 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
14203 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
14205 /* This is where the prologue starts... */
14206 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
14207 = func_brace_location
;
14216 Only in function declarations for constructors, destructors, and
14217 type conversions can the decl-specifier-seq be omitted.
14219 We explicitly postpone this check past the point where we handle
14220 function-definitions because we tolerate function-definitions
14221 that are missing their return types in some modes. */
14222 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
14224 cp_parser_error (parser
,
14225 "expected constructor, destructor, or type conversion");
14226 return error_mark_node
;
14229 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14230 if (token
->type
== CPP_EQ
14231 || token
->type
== CPP_OPEN_PAREN
14232 || token
->type
== CPP_OPEN_BRACE
)
14234 is_initialized
= SD_INITIALIZED
;
14235 initialization_kind
= token
->type
;
14237 if (token
->type
== CPP_EQ
14238 && function_declarator_p (declarator
))
14240 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14241 if (t2
->keyword
== RID_DEFAULT
)
14242 is_initialized
= SD_DEFAULTED
;
14243 else if (t2
->keyword
== RID_DELETE
)
14244 is_initialized
= SD_DELETED
;
14249 /* If the init-declarator isn't initialized and isn't followed by a
14250 `,' or `;', it's not a valid init-declarator. */
14251 if (token
->type
!= CPP_COMMA
14252 && token
->type
!= CPP_SEMICOLON
)
14254 cp_parser_error (parser
, "expected initializer");
14255 return error_mark_node
;
14257 is_initialized
= SD_UNINITIALIZED
;
14258 initialization_kind
= CPP_EOF
;
14261 /* Because start_decl has side-effects, we should only call it if we
14262 know we're going ahead. By this point, we know that we cannot
14263 possibly be looking at any other construct. */
14264 cp_parser_commit_to_tentative_parse (parser
);
14266 /* If the decl specifiers were bad, issue an error now that we're
14267 sure this was intended to be a declarator. Then continue
14268 declaring the variable(s), as int, to try to cut down on further
14270 if (decl_specifiers
->any_specifiers_p
14271 && decl_specifiers
->type
== error_mark_node
)
14273 cp_parser_error (parser
, "invalid type in declaration");
14274 decl_specifiers
->type
= integer_type_node
;
14277 /* Check to see whether or not this declaration is a friend. */
14278 friend_p
= cp_parser_friend_p (decl_specifiers
);
14280 /* Enter the newly declared entry in the symbol table. If we're
14281 processing a declaration in a class-specifier, we wait until
14282 after processing the initializer. */
14285 if (parser
->in_unbraced_linkage_specification_p
)
14286 decl_specifiers
->storage_class
= sc_extern
;
14287 decl
= start_decl (declarator
, decl_specifiers
,
14288 is_initialized
, attributes
, prefix_attributes
,
14290 /* Adjust location of decl if declarator->id_loc is more appropriate:
14291 set, and decl wasn't merged with another decl, in which case its
14292 location would be different from input_location, and more accurate. */
14294 && declarator
->id_loc
!= UNKNOWN_LOCATION
14295 && DECL_SOURCE_LOCATION (decl
) == input_location
)
14296 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
14299 /* Enter the SCOPE. That way unqualified names appearing in the
14300 initializer will be looked up in SCOPE. */
14301 pushed_scope
= push_scope (scope
);
14303 /* Perform deferred access control checks, now that we know in which
14304 SCOPE the declared entity resides. */
14305 if (!member_p
&& decl
)
14307 tree saved_current_function_decl
= NULL_TREE
;
14309 /* If the entity being declared is a function, pretend that we
14310 are in its scope. If it is a `friend', it may have access to
14311 things that would not otherwise be accessible. */
14312 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14314 saved_current_function_decl
= current_function_decl
;
14315 current_function_decl
= decl
;
14318 /* Perform access checks for template parameters. */
14319 cp_parser_perform_template_parameter_access_checks (checks
);
14321 /* Perform the access control checks for the declarator and the
14322 decl-specifiers. */
14323 perform_deferred_access_checks ();
14325 /* Restore the saved value. */
14326 if (TREE_CODE (decl
) == FUNCTION_DECL
)
14327 current_function_decl
= saved_current_function_decl
;
14330 /* Parse the initializer. */
14331 initializer
= NULL_TREE
;
14332 is_direct_init
= false;
14333 is_non_constant_init
= true;
14334 if (is_initialized
)
14336 if (function_declarator_p (declarator
))
14338 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
14339 if (initialization_kind
== CPP_EQ
)
14340 initializer
= cp_parser_pure_specifier (parser
);
14343 /* If the declaration was erroneous, we don't really
14344 know what the user intended, so just silently
14345 consume the initializer. */
14346 if (decl
!= error_mark_node
)
14347 error_at (initializer_start_token
->location
,
14348 "initializer provided for function");
14349 cp_parser_skip_to_closing_parenthesis (parser
,
14350 /*recovering=*/true,
14351 /*or_comma=*/false,
14352 /*consume_paren=*/true);
14357 /* We want to record the extra mangling scope for in-class
14358 initializers of class members and initializers of static data
14359 member templates. The former is a C++0x feature which isn't
14360 implemented yet, and I expect it will involve deferring
14361 parsing of the initializer until end of class as with default
14362 arguments. So right here we only handle the latter. */
14363 if (!member_p
&& processing_template_decl
)
14364 start_lambda_scope (decl
);
14365 initializer
= cp_parser_initializer (parser
,
14367 &is_non_constant_init
);
14368 if (!member_p
&& processing_template_decl
)
14369 finish_lambda_scope ();
14373 /* The old parser allows attributes to appear after a parenthesized
14374 initializer. Mark Mitchell proposed removing this functionality
14375 on the GCC mailing lists on 2002-08-13. This parser accepts the
14376 attributes -- but ignores them. */
14377 if (cp_parser_allow_gnu_extensions_p (parser
)
14378 && initialization_kind
== CPP_OPEN_PAREN
)
14379 if (cp_parser_attributes_opt (parser
))
14380 warning (OPT_Wattributes
,
14381 "attributes after parenthesized initializer ignored");
14383 /* For an in-class declaration, use `grokfield' to create the
14389 pop_scope (pushed_scope
);
14390 pushed_scope
= false;
14392 decl
= grokfield (declarator
, decl_specifiers
,
14393 initializer
, !is_non_constant_init
,
14394 /*asmspec=*/NULL_TREE
,
14395 prefix_attributes
);
14396 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
14397 cp_parser_save_default_args (parser
, decl
);
14400 /* Finish processing the declaration. But, skip friend
14402 if (!friend_p
&& decl
&& decl
!= error_mark_node
)
14404 cp_finish_decl (decl
,
14405 initializer
, !is_non_constant_init
,
14407 /* If the initializer is in parentheses, then this is
14408 a direct-initialization, which means that an
14409 `explicit' constructor is OK. Otherwise, an
14410 `explicit' constructor cannot be used. */
14411 ((is_direct_init
|| !is_initialized
)
14412 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
14414 else if ((cxx_dialect
!= cxx98
) && friend_p
14415 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
14416 /* Core issue #226 (C++0x only): A default template-argument
14417 shall not be specified in a friend class template
14419 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/1,
14420 /*is_partial=*/0, /*is_friend_decl=*/1);
14422 if (!friend_p
&& pushed_scope
)
14423 pop_scope (pushed_scope
);
14428 /* Parse a declarator.
14432 ptr-operator declarator
14434 abstract-declarator:
14435 ptr-operator abstract-declarator [opt]
14436 direct-abstract-declarator
14441 attributes [opt] direct-declarator
14442 attributes [opt] ptr-operator declarator
14444 abstract-declarator:
14445 attributes [opt] ptr-operator abstract-declarator [opt]
14446 attributes [opt] direct-abstract-declarator
14448 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14449 detect constructor, destructor or conversion operators. It is set
14450 to -1 if the declarator is a name, and +1 if it is a
14451 function. Otherwise it is set to zero. Usually you just want to
14452 test for >0, but internally the negative value is used.
14454 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14455 a decl-specifier-seq unless it declares a constructor, destructor,
14456 or conversion. It might seem that we could check this condition in
14457 semantic analysis, rather than parsing, but that makes it difficult
14458 to handle something like `f()'. We want to notice that there are
14459 no decl-specifiers, and therefore realize that this is an
14460 expression, not a declaration.)
14462 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14463 the declarator is a direct-declarator of the form "(...)".
14465 MEMBER_P is true iff this declarator is a member-declarator. */
14467 static cp_declarator
*
14468 cp_parser_declarator (cp_parser
* parser
,
14469 cp_parser_declarator_kind dcl_kind
,
14470 int* ctor_dtor_or_conv_p
,
14471 bool* parenthesized_p
,
14474 cp_declarator
*declarator
;
14475 enum tree_code code
;
14476 cp_cv_quals cv_quals
;
14478 tree attributes
= NULL_TREE
;
14480 /* Assume this is not a constructor, destructor, or type-conversion
14482 if (ctor_dtor_or_conv_p
)
14483 *ctor_dtor_or_conv_p
= 0;
14485 if (cp_parser_allow_gnu_extensions_p (parser
))
14486 attributes
= cp_parser_attributes_opt (parser
);
14488 /* Check for the ptr-operator production. */
14489 cp_parser_parse_tentatively (parser
);
14490 /* Parse the ptr-operator. */
14491 code
= cp_parser_ptr_operator (parser
,
14494 /* If that worked, then we have a ptr-operator. */
14495 if (cp_parser_parse_definitely (parser
))
14497 /* If a ptr-operator was found, then this declarator was not
14499 if (parenthesized_p
)
14500 *parenthesized_p
= true;
14501 /* The dependent declarator is optional if we are parsing an
14502 abstract-declarator. */
14503 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14504 cp_parser_parse_tentatively (parser
);
14506 /* Parse the dependent declarator. */
14507 declarator
= cp_parser_declarator (parser
, dcl_kind
,
14508 /*ctor_dtor_or_conv_p=*/NULL
,
14509 /*parenthesized_p=*/NULL
,
14510 /*member_p=*/false);
14512 /* If we are parsing an abstract-declarator, we must handle the
14513 case where the dependent declarator is absent. */
14514 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
14515 && !cp_parser_parse_definitely (parser
))
14518 declarator
= cp_parser_make_indirect_declarator
14519 (code
, class_type
, cv_quals
, declarator
);
14521 /* Everything else is a direct-declarator. */
14524 if (parenthesized_p
)
14525 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
14527 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
14528 ctor_dtor_or_conv_p
,
14532 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
14533 declarator
->attributes
= attributes
;
14538 /* Parse a direct-declarator or direct-abstract-declarator.
14542 direct-declarator ( parameter-declaration-clause )
14543 cv-qualifier-seq [opt]
14544 exception-specification [opt]
14545 direct-declarator [ constant-expression [opt] ]
14548 direct-abstract-declarator:
14549 direct-abstract-declarator [opt]
14550 ( parameter-declaration-clause )
14551 cv-qualifier-seq [opt]
14552 exception-specification [opt]
14553 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14554 ( abstract-declarator )
14556 Returns a representation of the declarator. DCL_KIND is
14557 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14558 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14559 we are parsing a direct-declarator. It is
14560 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14561 of ambiguity we prefer an abstract declarator, as per
14562 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14563 cp_parser_declarator. */
14565 static cp_declarator
*
14566 cp_parser_direct_declarator (cp_parser
* parser
,
14567 cp_parser_declarator_kind dcl_kind
,
14568 int* ctor_dtor_or_conv_p
,
14572 cp_declarator
*declarator
= NULL
;
14573 tree scope
= NULL_TREE
;
14574 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
14575 bool saved_in_declarator_p
= parser
->in_declarator_p
;
14577 tree pushed_scope
= NULL_TREE
;
14581 /* Peek at the next token. */
14582 token
= cp_lexer_peek_token (parser
->lexer
);
14583 if (token
->type
== CPP_OPEN_PAREN
)
14585 /* This is either a parameter-declaration-clause, or a
14586 parenthesized declarator. When we know we are parsing a
14587 named declarator, it must be a parenthesized declarator
14588 if FIRST is true. For instance, `(int)' is a
14589 parameter-declaration-clause, with an omitted
14590 direct-abstract-declarator. But `((*))', is a
14591 parenthesized abstract declarator. Finally, when T is a
14592 template parameter `(T)' is a
14593 parameter-declaration-clause, and not a parenthesized
14596 We first try and parse a parameter-declaration-clause,
14597 and then try a nested declarator (if FIRST is true).
14599 It is not an error for it not to be a
14600 parameter-declaration-clause, even when FIRST is
14606 The first is the declaration of a function while the
14607 second is the definition of a variable, including its
14610 Having seen only the parenthesis, we cannot know which of
14611 these two alternatives should be selected. Even more
14612 complex are examples like:
14617 The former is a function-declaration; the latter is a
14618 variable initialization.
14620 Thus again, we try a parameter-declaration-clause, and if
14621 that fails, we back out and return. */
14623 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14626 unsigned saved_num_template_parameter_lists
;
14627 bool is_declarator
= false;
14630 /* In a member-declarator, the only valid interpretation
14631 of a parenthesis is the start of a
14632 parameter-declaration-clause. (It is invalid to
14633 initialize a static data member with a parenthesized
14634 initializer; only the "=" form of initialization is
14637 cp_parser_parse_tentatively (parser
);
14639 /* Consume the `('. */
14640 cp_lexer_consume_token (parser
->lexer
);
14643 /* If this is going to be an abstract declarator, we're
14644 in a declarator and we can't have default args. */
14645 parser
->default_arg_ok_p
= false;
14646 parser
->in_declarator_p
= true;
14649 /* Inside the function parameter list, surrounding
14650 template-parameter-lists do not apply. */
14651 saved_num_template_parameter_lists
14652 = parser
->num_template_parameter_lists
;
14653 parser
->num_template_parameter_lists
= 0;
14655 begin_scope (sk_function_parms
, NULL_TREE
);
14657 /* Parse the parameter-declaration-clause. */
14658 params
= cp_parser_parameter_declaration_clause (parser
);
14660 parser
->num_template_parameter_lists
14661 = saved_num_template_parameter_lists
;
14663 /* If all went well, parse the cv-qualifier-seq and the
14664 exception-specification. */
14665 if (member_p
|| cp_parser_parse_definitely (parser
))
14667 cp_cv_quals cv_quals
;
14668 tree exception_specification
;
14671 is_declarator
= true;
14673 if (ctor_dtor_or_conv_p
)
14674 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
14676 /* Consume the `)'. */
14677 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
14679 /* Parse the cv-qualifier-seq. */
14680 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
14681 /* And the exception-specification. */
14682 exception_specification
14683 = cp_parser_exception_specification_opt (parser
);
14686 = cp_parser_late_return_type_opt (parser
);
14688 /* Create the function-declarator. */
14689 declarator
= make_call_declarator (declarator
,
14692 exception_specification
,
14694 /* Any subsequent parameter lists are to do with
14695 return type, so are not those of the declared
14697 parser
->default_arg_ok_p
= false;
14700 /* Remove the function parms from scope. */
14701 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
14702 pop_binding (DECL_NAME (t
), t
);
14706 /* Repeat the main loop. */
14710 /* If this is the first, we can try a parenthesized
14714 bool saved_in_type_id_in_expr_p
;
14716 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
14717 parser
->in_declarator_p
= saved_in_declarator_p
;
14719 /* Consume the `('. */
14720 cp_lexer_consume_token (parser
->lexer
);
14721 /* Parse the nested declarator. */
14722 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
14723 parser
->in_type_id_in_expr_p
= true;
14725 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
14726 /*parenthesized_p=*/NULL
,
14728 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
14730 /* Expect a `)'. */
14731 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
14732 declarator
= cp_error_declarator
;
14733 if (declarator
== cp_error_declarator
)
14736 goto handle_declarator
;
14738 /* Otherwise, we must be done. */
14742 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
14743 && token
->type
== CPP_OPEN_SQUARE
)
14745 /* Parse an array-declarator. */
14748 if (ctor_dtor_or_conv_p
)
14749 *ctor_dtor_or_conv_p
= 0;
14752 parser
->default_arg_ok_p
= false;
14753 parser
->in_declarator_p
= true;
14754 /* Consume the `['. */
14755 cp_lexer_consume_token (parser
->lexer
);
14756 /* Peek at the next token. */
14757 token
= cp_lexer_peek_token (parser
->lexer
);
14758 /* If the next token is `]', then there is no
14759 constant-expression. */
14760 if (token
->type
!= CPP_CLOSE_SQUARE
)
14762 bool non_constant_p
;
14765 = cp_parser_constant_expression (parser
,
14766 /*allow_non_constant=*/true,
14768 if (!non_constant_p
)
14769 bounds
= fold_non_dependent_expr (bounds
);
14770 /* Normally, the array bound must be an integral constant
14771 expression. However, as an extension, we allow VLAs
14772 in function scopes as long as they aren't part of a
14773 parameter declaration. */
14774 else if (!parser
->in_function_body
14775 || current_binding_level
->kind
== sk_function_parms
)
14777 cp_parser_error (parser
,
14778 "array bound is not an integer constant");
14779 bounds
= error_mark_node
;
14781 else if (processing_template_decl
&& !error_operand_p (bounds
))
14783 /* Remember this wasn't a constant-expression. */
14784 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
14785 TREE_SIDE_EFFECTS (bounds
) = 1;
14789 bounds
= NULL_TREE
;
14790 /* Look for the closing `]'. */
14791 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14793 declarator
= cp_error_declarator
;
14797 declarator
= make_array_declarator (declarator
, bounds
);
14799 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
14802 tree qualifying_scope
;
14803 tree unqualified_name
;
14804 special_function_kind sfk
;
14806 bool pack_expansion_p
= false;
14807 cp_token
*declarator_id_start_token
;
14809 /* Parse a declarator-id */
14810 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
14813 cp_parser_parse_tentatively (parser
);
14815 /* If we see an ellipsis, we should be looking at a
14817 if (token
->type
== CPP_ELLIPSIS
)
14819 /* Consume the `...' */
14820 cp_lexer_consume_token (parser
->lexer
);
14822 pack_expansion_p
= true;
14826 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
14828 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
14829 qualifying_scope
= parser
->scope
;
14834 if (!unqualified_name
&& pack_expansion_p
)
14836 /* Check whether an error occurred. */
14837 okay
= !cp_parser_error_occurred (parser
);
14839 /* We already consumed the ellipsis to mark a
14840 parameter pack, but we have no way to report it,
14841 so abort the tentative parse. We will be exiting
14842 immediately anyway. */
14843 cp_parser_abort_tentative_parse (parser
);
14846 okay
= cp_parser_parse_definitely (parser
);
14849 unqualified_name
= error_mark_node
;
14850 else if (unqualified_name
14851 && (qualifying_scope
14852 || (TREE_CODE (unqualified_name
)
14853 != IDENTIFIER_NODE
)))
14855 cp_parser_error (parser
, "expected unqualified-id");
14856 unqualified_name
= error_mark_node
;
14860 if (!unqualified_name
)
14862 if (unqualified_name
== error_mark_node
)
14864 declarator
= cp_error_declarator
;
14865 pack_expansion_p
= false;
14866 declarator
->parameter_pack_p
= false;
14870 if (qualifying_scope
&& at_namespace_scope_p ()
14871 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
14873 /* In the declaration of a member of a template class
14874 outside of the class itself, the SCOPE will sometimes
14875 be a TYPENAME_TYPE. For example, given:
14877 template <typename T>
14878 int S<T>::R::i = 3;
14880 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14881 this context, we must resolve S<T>::R to an ordinary
14882 type, rather than a typename type.
14884 The reason we normally avoid resolving TYPENAME_TYPEs
14885 is that a specialization of `S' might render
14886 `S<T>::R' not a type. However, if `S' is
14887 specialized, then this `i' will not be used, so there
14888 is no harm in resolving the types here. */
14891 /* Resolve the TYPENAME_TYPE. */
14892 type
= resolve_typename_type (qualifying_scope
,
14893 /*only_current_p=*/false);
14894 /* If that failed, the declarator is invalid. */
14895 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14897 if (typedef_variant_p (type
))
14898 error_at (declarator_id_start_token
->location
,
14899 "cannot define member of dependent typedef "
14902 error_at (declarator_id_start_token
->location
,
14903 "%<%T::%E%> is not a type",
14904 TYPE_CONTEXT (qualifying_scope
),
14905 TYPE_IDENTIFIER (qualifying_scope
));
14907 qualifying_scope
= type
;
14912 if (unqualified_name
)
14916 if (qualifying_scope
14917 && CLASS_TYPE_P (qualifying_scope
))
14918 class_type
= qualifying_scope
;
14920 class_type
= current_class_type
;
14922 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
14924 tree name_type
= TREE_TYPE (unqualified_name
);
14925 if (class_type
&& same_type_p (name_type
, class_type
))
14927 if (qualifying_scope
14928 && CLASSTYPE_USE_TEMPLATE (name_type
))
14930 error_at (declarator_id_start_token
->location
,
14931 "invalid use of constructor as a template");
14932 inform (declarator_id_start_token
->location
,
14933 "use %<%T::%D%> instead of %<%T::%D%> to "
14934 "name the constructor in a qualified name",
14936 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
14937 class_type
, name_type
);
14938 declarator
= cp_error_declarator
;
14942 unqualified_name
= constructor_name (class_type
);
14946 /* We do not attempt to print the declarator
14947 here because we do not have enough
14948 information about its original syntactic
14950 cp_parser_error (parser
, "invalid declarator");
14951 declarator
= cp_error_declarator
;
14958 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
14959 sfk
= sfk_destructor
;
14960 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
14961 sfk
= sfk_conversion
;
14962 else if (/* There's no way to declare a constructor
14963 for an anonymous type, even if the type
14964 got a name for linkage purposes. */
14965 !TYPE_WAS_ANONYMOUS (class_type
)
14966 && constructor_name_p (unqualified_name
,
14969 unqualified_name
= constructor_name (class_type
);
14970 sfk
= sfk_constructor
;
14972 else if (is_overloaded_fn (unqualified_name
)
14973 && DECL_CONSTRUCTOR_P (get_first_fn
14974 (unqualified_name
)))
14975 sfk
= sfk_constructor
;
14977 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
14978 *ctor_dtor_or_conv_p
= -1;
14981 declarator
= make_id_declarator (qualifying_scope
,
14984 declarator
->id_loc
= token
->location
;
14985 declarator
->parameter_pack_p
= pack_expansion_p
;
14987 if (pack_expansion_p
)
14988 maybe_warn_variadic_templates ();
14991 handle_declarator
:;
14992 scope
= get_scope_of_declarator (declarator
);
14994 /* Any names that appear after the declarator-id for a
14995 member are looked up in the containing scope. */
14996 pushed_scope
= push_scope (scope
);
14997 parser
->in_declarator_p
= true;
14998 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
14999 || (declarator
&& declarator
->kind
== cdk_id
))
15000 /* Default args are only allowed on function
15002 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15004 parser
->default_arg_ok_p
= false;
15013 /* For an abstract declarator, we might wind up with nothing at this
15014 point. That's an error; the declarator is not optional. */
15016 cp_parser_error (parser
, "expected declarator");
15018 /* If we entered a scope, we must exit it now. */
15020 pop_scope (pushed_scope
);
15022 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15023 parser
->in_declarator_p
= saved_in_declarator_p
;
15028 /* Parse a ptr-operator.
15031 * cv-qualifier-seq [opt]
15033 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15038 & cv-qualifier-seq [opt]
15040 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15041 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15042 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15043 filled in with the TYPE containing the member. *CV_QUALS is
15044 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15045 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15046 Note that the tree codes returned by this function have nothing
15047 to do with the types of trees that will be eventually be created
15048 to represent the pointer or reference type being parsed. They are
15049 just constants with suggestive names. */
15050 static enum tree_code
15051 cp_parser_ptr_operator (cp_parser
* parser
,
15053 cp_cv_quals
*cv_quals
)
15055 enum tree_code code
= ERROR_MARK
;
15058 /* Assume that it's not a pointer-to-member. */
15060 /* And that there are no cv-qualifiers. */
15061 *cv_quals
= TYPE_UNQUALIFIED
;
15063 /* Peek at the next token. */
15064 token
= cp_lexer_peek_token (parser
->lexer
);
15066 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15067 if (token
->type
== CPP_MULT
)
15068 code
= INDIRECT_REF
;
15069 else if (token
->type
== CPP_AND
)
15071 else if ((cxx_dialect
!= cxx98
) &&
15072 token
->type
== CPP_AND_AND
) /* C++0x only */
15073 code
= NON_LVALUE_EXPR
;
15075 if (code
!= ERROR_MARK
)
15077 /* Consume the `*', `&' or `&&'. */
15078 cp_lexer_consume_token (parser
->lexer
);
15080 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15081 `&', if we are allowing GNU extensions. (The only qualifier
15082 that can legally appear after `&' is `restrict', but that is
15083 enforced during semantic analysis. */
15084 if (code
== INDIRECT_REF
15085 || cp_parser_allow_gnu_extensions_p (parser
))
15086 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
15090 /* Try the pointer-to-member case. */
15091 cp_parser_parse_tentatively (parser
);
15092 /* Look for the optional `::' operator. */
15093 cp_parser_global_scope_opt (parser
,
15094 /*current_scope_valid_p=*/false);
15095 /* Look for the nested-name specifier. */
15096 token
= cp_lexer_peek_token (parser
->lexer
);
15097 cp_parser_nested_name_specifier (parser
,
15098 /*typename_keyword_p=*/false,
15099 /*check_dependency_p=*/true,
15101 /*is_declaration=*/false);
15102 /* If we found it, and the next token is a `*', then we are
15103 indeed looking at a pointer-to-member operator. */
15104 if (!cp_parser_error_occurred (parser
)
15105 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
15107 /* Indicate that the `*' operator was used. */
15108 code
= INDIRECT_REF
;
15110 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
15111 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
15114 /* The type of which the member is a member is given by the
15116 *type
= parser
->scope
;
15117 /* The next name will not be qualified. */
15118 parser
->scope
= NULL_TREE
;
15119 parser
->qualifying_scope
= NULL_TREE
;
15120 parser
->object_scope
= NULL_TREE
;
15121 /* Look for the optional cv-qualifier-seq. */
15122 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
15125 /* If that didn't work we don't have a ptr-operator. */
15126 if (!cp_parser_parse_definitely (parser
))
15127 cp_parser_error (parser
, "expected ptr-operator");
15133 /* Parse an (optional) cv-qualifier-seq.
15136 cv-qualifier cv-qualifier-seq [opt]
15147 Returns a bitmask representing the cv-qualifiers. */
15150 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
15152 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
15157 cp_cv_quals cv_qualifier
;
15159 /* Peek at the next token. */
15160 token
= cp_lexer_peek_token (parser
->lexer
);
15161 /* See if it's a cv-qualifier. */
15162 switch (token
->keyword
)
15165 cv_qualifier
= TYPE_QUAL_CONST
;
15169 cv_qualifier
= TYPE_QUAL_VOLATILE
;
15173 cv_qualifier
= TYPE_QUAL_RESTRICT
;
15177 cv_qualifier
= TYPE_UNQUALIFIED
;
15184 if (cv_quals
& cv_qualifier
)
15186 error_at (token
->location
, "duplicate cv-qualifier");
15187 cp_lexer_purge_token (parser
->lexer
);
15191 cp_lexer_consume_token (parser
->lexer
);
15192 cv_quals
|= cv_qualifier
;
15199 /* Parse a late-specified return type, if any. This is not a separate
15200 non-terminal, but part of a function declarator, which looks like
15202 -> trailing-type-specifier-seq abstract-declarator(opt)
15204 Returns the type indicated by the type-id. */
15207 cp_parser_late_return_type_opt (cp_parser
* parser
)
15211 /* Peek at the next token. */
15212 token
= cp_lexer_peek_token (parser
->lexer
);
15213 /* A late-specified return type is indicated by an initial '->'. */
15214 if (token
->type
!= CPP_DEREF
)
15217 /* Consume the ->. */
15218 cp_lexer_consume_token (parser
->lexer
);
15220 return cp_parser_trailing_type_id (parser
);
15223 /* Parse a declarator-id.
15227 :: [opt] nested-name-specifier [opt] type-name
15229 In the `id-expression' case, the value returned is as for
15230 cp_parser_id_expression if the id-expression was an unqualified-id.
15231 If the id-expression was a qualified-id, then a SCOPE_REF is
15232 returned. The first operand is the scope (either a NAMESPACE_DECL
15233 or TREE_TYPE), but the second is still just a representation of an
15237 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
15240 /* The expression must be an id-expression. Assume that qualified
15241 names are the names of types so that:
15244 int S<T>::R::i = 3;
15246 will work; we must treat `S<T>::R' as the name of a type.
15247 Similarly, assume that qualified names are templates, where
15251 int S<T>::R<T>::i = 3;
15254 id
= cp_parser_id_expression (parser
,
15255 /*template_keyword_p=*/false,
15256 /*check_dependency_p=*/false,
15257 /*template_p=*/NULL
,
15258 /*declarator_p=*/true,
15260 if (id
&& BASELINK_P (id
))
15261 id
= BASELINK_FUNCTIONS (id
);
15265 /* Parse a type-id.
15268 type-specifier-seq abstract-declarator [opt]
15270 Returns the TYPE specified. */
15273 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
15274 bool is_trailing_return
)
15276 cp_decl_specifier_seq type_specifier_seq
;
15277 cp_declarator
*abstract_declarator
;
15279 /* Parse the type-specifier-seq. */
15280 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15281 is_trailing_return
,
15282 &type_specifier_seq
);
15283 if (type_specifier_seq
.type
== error_mark_node
)
15284 return error_mark_node
;
15286 /* There might or might not be an abstract declarator. */
15287 cp_parser_parse_tentatively (parser
);
15288 /* Look for the declarator. */
15289 abstract_declarator
15290 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
15291 /*parenthesized_p=*/NULL
,
15292 /*member_p=*/false);
15293 /* Check to see if there really was a declarator. */
15294 if (!cp_parser_parse_definitely (parser
))
15295 abstract_declarator
= NULL
;
15297 if (type_specifier_seq
.type
15298 && type_uses_auto (type_specifier_seq
.type
))
15300 /* A type-id with type 'auto' is only ok if the abstract declarator
15301 is a function declarator with a late-specified return type. */
15302 if (abstract_declarator
15303 && abstract_declarator
->kind
== cdk_function
15304 && abstract_declarator
->u
.function
.late_return_type
)
15308 error ("invalid use of %<auto%>");
15309 return error_mark_node
;
15313 return groktypename (&type_specifier_seq
, abstract_declarator
,
15317 static tree
cp_parser_type_id (cp_parser
*parser
)
15319 return cp_parser_type_id_1 (parser
, false, false);
15322 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
15324 return cp_parser_type_id_1 (parser
, true, false);
15327 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
15329 return cp_parser_type_id_1 (parser
, false, true);
15332 /* Parse a type-specifier-seq.
15334 type-specifier-seq:
15335 type-specifier type-specifier-seq [opt]
15339 type-specifier-seq:
15340 attributes type-specifier-seq [opt]
15342 If IS_DECLARATION is true, we are at the start of a "condition" or
15343 exception-declaration, so we might be followed by a declarator-id.
15345 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15346 i.e. we've just seen "->".
15348 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15351 cp_parser_type_specifier_seq (cp_parser
* parser
,
15352 bool is_declaration
,
15353 bool is_trailing_return
,
15354 cp_decl_specifier_seq
*type_specifier_seq
)
15356 bool seen_type_specifier
= false;
15357 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
15358 cp_token
*start_token
= NULL
;
15360 /* Clear the TYPE_SPECIFIER_SEQ. */
15361 clear_decl_specs (type_specifier_seq
);
15363 /* In the context of a trailing return type, enum E { } is an
15364 elaborated-type-specifier followed by a function-body, not an
15366 if (is_trailing_return
)
15367 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
15369 /* Parse the type-specifiers and attributes. */
15372 tree type_specifier
;
15373 bool is_cv_qualifier
;
15375 /* Check for attributes first. */
15376 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
15378 type_specifier_seq
->attributes
=
15379 chainon (type_specifier_seq
->attributes
,
15380 cp_parser_attributes_opt (parser
));
15384 /* record the token of the beginning of the type specifier seq,
15385 for error reporting purposes*/
15387 start_token
= cp_lexer_peek_token (parser
->lexer
);
15389 /* Look for the type-specifier. */
15390 type_specifier
= cp_parser_type_specifier (parser
,
15392 type_specifier_seq
,
15393 /*is_declaration=*/false,
15396 if (!type_specifier
)
15398 /* If the first type-specifier could not be found, this is not a
15399 type-specifier-seq at all. */
15400 if (!seen_type_specifier
)
15402 cp_parser_error (parser
, "expected type-specifier");
15403 type_specifier_seq
->type
= error_mark_node
;
15406 /* If subsequent type-specifiers could not be found, the
15407 type-specifier-seq is complete. */
15411 seen_type_specifier
= true;
15412 /* The standard says that a condition can be:
15414 type-specifier-seq declarator = assignment-expression
15421 we should treat the "S" as a declarator, not as a
15422 type-specifier. The standard doesn't say that explicitly for
15423 type-specifier-seq, but it does say that for
15424 decl-specifier-seq in an ordinary declaration. Perhaps it
15425 would be clearer just to allow a decl-specifier-seq here, and
15426 then add a semantic restriction that if any decl-specifiers
15427 that are not type-specifiers appear, the program is invalid. */
15428 if (is_declaration
&& !is_cv_qualifier
)
15429 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
15432 cp_parser_check_decl_spec (type_specifier_seq
, start_token
->location
);
15435 /* Parse a parameter-declaration-clause.
15437 parameter-declaration-clause:
15438 parameter-declaration-list [opt] ... [opt]
15439 parameter-declaration-list , ...
15441 Returns a representation for the parameter declarations. A return
15442 value of NULL indicates a parameter-declaration-clause consisting
15443 only of an ellipsis. */
15446 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
15453 /* Peek at the next token. */
15454 token
= cp_lexer_peek_token (parser
->lexer
);
15455 /* Check for trivial parameter-declaration-clauses. */
15456 if (token
->type
== CPP_ELLIPSIS
)
15458 /* Consume the `...' token. */
15459 cp_lexer_consume_token (parser
->lexer
);
15462 else if (token
->type
== CPP_CLOSE_PAREN
)
15463 /* There are no parameters. */
15465 #ifndef NO_IMPLICIT_EXTERN_C
15466 if (in_system_header
&& current_class_type
== NULL
15467 && current_lang_name
== lang_name_c
)
15471 return void_list_node
;
15473 /* Check for `(void)', too, which is a special case. */
15474 else if (token
->keyword
== RID_VOID
15475 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
15476 == CPP_CLOSE_PAREN
))
15478 /* Consume the `void' token. */
15479 cp_lexer_consume_token (parser
->lexer
);
15480 /* There are no parameters. */
15481 return void_list_node
;
15484 /* Parse the parameter-declaration-list. */
15485 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
15486 /* If a parse error occurred while parsing the
15487 parameter-declaration-list, then the entire
15488 parameter-declaration-clause is erroneous. */
15492 /* Peek at the next token. */
15493 token
= cp_lexer_peek_token (parser
->lexer
);
15494 /* If it's a `,', the clause should terminate with an ellipsis. */
15495 if (token
->type
== CPP_COMMA
)
15497 /* Consume the `,'. */
15498 cp_lexer_consume_token (parser
->lexer
);
15499 /* Expect an ellipsis. */
15501 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
15503 /* It might also be `...' if the optional trailing `,' was
15505 else if (token
->type
== CPP_ELLIPSIS
)
15507 /* Consume the `...' token. */
15508 cp_lexer_consume_token (parser
->lexer
);
15509 /* And remember that we saw it. */
15513 ellipsis_p
= false;
15515 /* Finish the parameter list. */
15517 parameters
= chainon (parameters
, void_list_node
);
15522 /* Parse a parameter-declaration-list.
15524 parameter-declaration-list:
15525 parameter-declaration
15526 parameter-declaration-list , parameter-declaration
15528 Returns a representation of the parameter-declaration-list, as for
15529 cp_parser_parameter_declaration_clause. However, the
15530 `void_list_node' is never appended to the list. Upon return,
15531 *IS_ERROR will be true iff an error occurred. */
15534 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
15536 tree parameters
= NULL_TREE
;
15537 tree
*tail
= ¶meters
;
15538 bool saved_in_unbraced_linkage_specification_p
;
15541 /* Assume all will go well. */
15543 /* The special considerations that apply to a function within an
15544 unbraced linkage specifications do not apply to the parameters
15545 to the function. */
15546 saved_in_unbraced_linkage_specification_p
15547 = parser
->in_unbraced_linkage_specification_p
;
15548 parser
->in_unbraced_linkage_specification_p
= false;
15550 /* Look for more parameters. */
15553 cp_parameter_declarator
*parameter
;
15554 tree decl
= error_mark_node
;
15555 bool parenthesized_p
;
15556 /* Parse the parameter. */
15558 = cp_parser_parameter_declaration (parser
,
15559 /*template_parm_p=*/false,
15562 /* We don't know yet if the enclosing context is deprecated, so wait
15563 and warn in grokparms if appropriate. */
15564 deprecated_state
= DEPRECATED_SUPPRESS
;
15567 decl
= grokdeclarator (parameter
->declarator
,
15568 ¶meter
->decl_specifiers
,
15570 parameter
->default_argument
!= NULL_TREE
,
15571 ¶meter
->decl_specifiers
.attributes
);
15573 deprecated_state
= DEPRECATED_NORMAL
;
15575 /* If a parse error occurred parsing the parameter declaration,
15576 then the entire parameter-declaration-list is erroneous. */
15577 if (decl
== error_mark_node
)
15580 parameters
= error_mark_node
;
15584 if (parameter
->decl_specifiers
.attributes
)
15585 cplus_decl_attributes (&decl
,
15586 parameter
->decl_specifiers
.attributes
,
15588 if (DECL_NAME (decl
))
15589 decl
= pushdecl (decl
);
15591 if (decl
!= error_mark_node
)
15593 retrofit_lang_decl (decl
);
15594 DECL_PARM_INDEX (decl
) = ++index
;
15597 /* Add the new parameter to the list. */
15598 *tail
= build_tree_list (parameter
->default_argument
, decl
);
15599 tail
= &TREE_CHAIN (*tail
);
15601 /* Peek at the next token. */
15602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
15603 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
15604 /* These are for Objective-C++ */
15605 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15606 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15607 /* The parameter-declaration-list is complete. */
15609 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
15613 /* Peek at the next token. */
15614 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15615 /* If it's an ellipsis, then the list is complete. */
15616 if (token
->type
== CPP_ELLIPSIS
)
15618 /* Otherwise, there must be more parameters. Consume the
15620 cp_lexer_consume_token (parser
->lexer
);
15621 /* When parsing something like:
15623 int i(float f, double d)
15625 we can tell after seeing the declaration for "f" that we
15626 are not looking at an initialization of a variable "i",
15627 but rather at the declaration of a function "i".
15629 Due to the fact that the parsing of template arguments
15630 (as specified to a template-id) requires backtracking we
15631 cannot use this technique when inside a template argument
15633 if (!parser
->in_template_argument_list_p
15634 && !parser
->in_type_id_in_expr_p
15635 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15636 /* However, a parameter-declaration of the form
15637 "foat(f)" (which is a valid declaration of a
15638 parameter "f") can also be interpreted as an
15639 expression (the conversion of "f" to "float"). */
15640 && !parenthesized_p
)
15641 cp_parser_commit_to_tentative_parse (parser
);
15645 cp_parser_error (parser
, "expected %<,%> or %<...%>");
15646 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
15647 cp_parser_skip_to_closing_parenthesis (parser
,
15648 /*recovering=*/true,
15649 /*or_comma=*/false,
15650 /*consume_paren=*/false);
15655 parser
->in_unbraced_linkage_specification_p
15656 = saved_in_unbraced_linkage_specification_p
;
15661 /* Parse a parameter declaration.
15663 parameter-declaration:
15664 decl-specifier-seq ... [opt] declarator
15665 decl-specifier-seq declarator = assignment-expression
15666 decl-specifier-seq ... [opt] abstract-declarator [opt]
15667 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15669 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15670 declares a template parameter. (In that case, a non-nested `>'
15671 token encountered during the parsing of the assignment-expression
15672 is not interpreted as a greater-than operator.)
15674 Returns a representation of the parameter, or NULL if an error
15675 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15676 true iff the declarator is of the form "(p)". */
15678 static cp_parameter_declarator
*
15679 cp_parser_parameter_declaration (cp_parser
*parser
,
15680 bool template_parm_p
,
15681 bool *parenthesized_p
)
15683 int declares_class_or_enum
;
15684 cp_decl_specifier_seq decl_specifiers
;
15685 cp_declarator
*declarator
;
15686 tree default_argument
;
15687 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
15688 const char *saved_message
;
15690 /* In a template parameter, `>' is not an operator.
15694 When parsing a default template-argument for a non-type
15695 template-parameter, the first non-nested `>' is taken as the end
15696 of the template parameter-list rather than a greater-than
15699 /* Type definitions may not appear in parameter types. */
15700 saved_message
= parser
->type_definition_forbidden_message
;
15701 parser
->type_definition_forbidden_message
15702 = G_("types may not be defined in parameter types");
15704 /* Parse the declaration-specifiers. */
15705 cp_parser_decl_specifier_seq (parser
,
15706 CP_PARSER_FLAGS_NONE
,
15708 &declares_class_or_enum
);
15710 /* Complain about missing 'typename' or other invalid type names. */
15711 if (!decl_specifiers
.any_type_specifiers_p
)
15712 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
15714 /* If an error occurred, there's no reason to attempt to parse the
15715 rest of the declaration. */
15716 if (cp_parser_error_occurred (parser
))
15718 parser
->type_definition_forbidden_message
= saved_message
;
15722 /* Peek at the next token. */
15723 token
= cp_lexer_peek_token (parser
->lexer
);
15725 /* If the next token is a `)', `,', `=', `>', or `...', then there
15726 is no declarator. However, when variadic templates are enabled,
15727 there may be a declarator following `...'. */
15728 if (token
->type
== CPP_CLOSE_PAREN
15729 || token
->type
== CPP_COMMA
15730 || token
->type
== CPP_EQ
15731 || token
->type
== CPP_GREATER
)
15734 if (parenthesized_p
)
15735 *parenthesized_p
= false;
15737 /* Otherwise, there should be a declarator. */
15740 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
15741 parser
->default_arg_ok_p
= false;
15743 /* After seeing a decl-specifier-seq, if the next token is not a
15744 "(", there is no possibility that the code is a valid
15745 expression. Therefore, if parsing tentatively, we commit at
15747 if (!parser
->in_template_argument_list_p
15748 /* In an expression context, having seen:
15752 we cannot be sure whether we are looking at a
15753 function-type (taking a "char" as a parameter) or a cast
15754 of some object of type "char" to "int". */
15755 && !parser
->in_type_id_in_expr_p
15756 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
15757 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
15758 cp_parser_commit_to_tentative_parse (parser
);
15759 /* Parse the declarator. */
15760 declarator_token_start
= token
;
15761 declarator
= cp_parser_declarator (parser
,
15762 CP_PARSER_DECLARATOR_EITHER
,
15763 /*ctor_dtor_or_conv_p=*/NULL
,
15765 /*member_p=*/false);
15766 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
15767 /* After the declarator, allow more attributes. */
15768 decl_specifiers
.attributes
15769 = chainon (decl_specifiers
.attributes
,
15770 cp_parser_attributes_opt (parser
));
15773 /* If the next token is an ellipsis, and we have not seen a
15774 declarator name, and the type of the declarator contains parameter
15775 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15776 a parameter pack expansion expression. Otherwise, leave the
15777 ellipsis for a C-style variadic function. */
15778 token
= cp_lexer_peek_token (parser
->lexer
);
15779 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15781 tree type
= decl_specifiers
.type
;
15783 if (type
&& DECL_P (type
))
15784 type
= TREE_TYPE (type
);
15787 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
15788 && declarator_can_be_parameter_pack (declarator
)
15789 && (!declarator
|| !declarator
->parameter_pack_p
)
15790 && uses_parameter_packs (type
))
15792 /* Consume the `...'. */
15793 cp_lexer_consume_token (parser
->lexer
);
15794 maybe_warn_variadic_templates ();
15796 /* Build a pack expansion type */
15798 declarator
->parameter_pack_p
= true;
15800 decl_specifiers
.type
= make_pack_expansion (type
);
15804 /* The restriction on defining new types applies only to the type
15805 of the parameter, not to the default argument. */
15806 parser
->type_definition_forbidden_message
= saved_message
;
15808 /* If the next token is `=', then process a default argument. */
15809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15811 /* Consume the `='. */
15812 cp_lexer_consume_token (parser
->lexer
);
15814 /* If we are defining a class, then the tokens that make up the
15815 default argument must be saved and processed later. */
15816 if (!template_parm_p
&& at_class_scope_p ()
15817 && TYPE_BEING_DEFINED (current_class_type
)
15818 && !LAMBDA_TYPE_P (current_class_type
))
15820 unsigned depth
= 0;
15821 int maybe_template_id
= 0;
15822 cp_token
*first_token
;
15825 /* Add tokens until we have processed the entire default
15826 argument. We add the range [first_token, token). */
15827 first_token
= cp_lexer_peek_token (parser
->lexer
);
15832 /* Peek at the next token. */
15833 token
= cp_lexer_peek_token (parser
->lexer
);
15834 /* What we do depends on what token we have. */
15835 switch (token
->type
)
15837 /* In valid code, a default argument must be
15838 immediately followed by a `,' `)', or `...'. */
15840 if (depth
== 0 && maybe_template_id
)
15842 /* If we've seen a '<', we might be in a
15843 template-argument-list. Until Core issue 325 is
15844 resolved, we don't know how this situation ought
15845 to be handled, so try to DTRT. We check whether
15846 what comes after the comma is a valid parameter
15847 declaration list. If it is, then the comma ends
15848 the default argument; otherwise the default
15849 argument continues. */
15850 bool error
= false;
15853 /* Set ITALP so cp_parser_parameter_declaration_list
15854 doesn't decide to commit to this parse. */
15855 bool saved_italp
= parser
->in_template_argument_list_p
;
15856 parser
->in_template_argument_list_p
= true;
15858 cp_parser_parse_tentatively (parser
);
15859 cp_lexer_consume_token (parser
->lexer
);
15860 begin_scope (sk_function_parms
, NULL_TREE
);
15861 cp_parser_parameter_declaration_list (parser
, &error
);
15862 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
15863 pop_binding (DECL_NAME (t
), t
);
15865 if (!cp_parser_error_occurred (parser
) && !error
)
15867 cp_parser_abort_tentative_parse (parser
);
15869 parser
->in_template_argument_list_p
= saved_italp
;
15872 case CPP_CLOSE_PAREN
:
15874 /* If we run into a non-nested `;', `}', or `]',
15875 then the code is invalid -- but the default
15876 argument is certainly over. */
15877 case CPP_SEMICOLON
:
15878 case CPP_CLOSE_BRACE
:
15879 case CPP_CLOSE_SQUARE
:
15882 /* Update DEPTH, if necessary. */
15883 else if (token
->type
== CPP_CLOSE_PAREN
15884 || token
->type
== CPP_CLOSE_BRACE
15885 || token
->type
== CPP_CLOSE_SQUARE
)
15889 case CPP_OPEN_PAREN
:
15890 case CPP_OPEN_SQUARE
:
15891 case CPP_OPEN_BRACE
:
15897 /* This might be the comparison operator, or it might
15898 start a template argument list. */
15899 ++maybe_template_id
;
15903 if (cxx_dialect
== cxx98
)
15905 /* Fall through for C++0x, which treats the `>>'
15906 operator like two `>' tokens in certain
15912 /* This might be an operator, or it might close a
15913 template argument list. But if a previous '<'
15914 started a template argument list, this will have
15915 closed it, so we can't be in one anymore. */
15916 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
15917 if (maybe_template_id
< 0)
15918 maybe_template_id
= 0;
15922 /* If we run out of tokens, issue an error message. */
15924 case CPP_PRAGMA_EOL
:
15925 error_at (token
->location
, "file ends in default argument");
15931 /* In these cases, we should look for template-ids.
15932 For example, if the default argument is
15933 `X<int, double>()', we need to do name lookup to
15934 figure out whether or not `X' is a template; if
15935 so, the `,' does not end the default argument.
15937 That is not yet done. */
15944 /* If we've reached the end, stop. */
15948 /* Add the token to the token block. */
15949 token
= cp_lexer_consume_token (parser
->lexer
);
15952 /* Create a DEFAULT_ARG to represent the unparsed default
15954 default_argument
= make_node (DEFAULT_ARG
);
15955 DEFARG_TOKENS (default_argument
)
15956 = cp_token_cache_new (first_token
, token
);
15957 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
15959 /* Outside of a class definition, we can just parse the
15960 assignment-expression. */
15963 token
= cp_lexer_peek_token (parser
->lexer
);
15965 = cp_parser_default_argument (parser
, template_parm_p
);
15968 if (!parser
->default_arg_ok_p
)
15970 if (flag_permissive
)
15971 warning (0, "deprecated use of default argument for parameter of non-function");
15974 error_at (token
->location
,
15975 "default arguments are only "
15976 "permitted for function parameters");
15977 default_argument
= NULL_TREE
;
15980 else if ((declarator
&& declarator
->parameter_pack_p
)
15981 || (decl_specifiers
.type
15982 && PACK_EXPANSION_P (decl_specifiers
.type
)))
15984 /* Find the name of the parameter pack. */
15985 cp_declarator
*id_declarator
= declarator
;
15986 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
15987 id_declarator
= id_declarator
->declarator
;
15989 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
15990 error_at (declarator_token_start
->location
,
15992 ? "template parameter pack %qD"
15993 " cannot have a default argument"
15994 : "parameter pack %qD cannot have a default argument",
15995 id_declarator
->u
.id
.unqualified_name
);
15997 error_at (declarator_token_start
->location
,
15999 ? "template parameter pack cannot have a default argument"
16000 : "parameter pack cannot have a default argument");
16002 default_argument
= NULL_TREE
;
16006 default_argument
= NULL_TREE
;
16008 return make_parameter_declarator (&decl_specifiers
,
16013 /* Parse a default argument and return it.
16015 TEMPLATE_PARM_P is true if this is a default argument for a
16016 non-type template parameter. */
16018 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
16020 tree default_argument
= NULL_TREE
;
16021 bool saved_greater_than_is_operator_p
;
16022 bool saved_local_variables_forbidden_p
;
16024 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16026 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
16027 parser
->greater_than_is_operator_p
= !template_parm_p
;
16028 /* Local variable names (and the `this' keyword) may not
16029 appear in a default argument. */
16030 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
16031 parser
->local_variables_forbidden_p
= true;
16032 /* Parse the assignment-expression. */
16033 if (template_parm_p
)
16034 push_deferring_access_checks (dk_no_deferred
);
16036 = cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
16037 if (template_parm_p
)
16038 pop_deferring_access_checks ();
16039 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
16040 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
16042 return default_argument
;
16045 /* Parse a function-body.
16048 compound_statement */
16051 cp_parser_function_body (cp_parser
*parser
)
16053 cp_parser_compound_statement (parser
, NULL
, false);
16056 /* Parse a ctor-initializer-opt followed by a function-body. Return
16057 true if a ctor-initializer was present. */
16060 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
)
16063 bool ctor_initializer_p
;
16065 /* Begin the function body. */
16066 body
= begin_function_body ();
16067 /* Parse the optional ctor-initializer. */
16068 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
16069 /* Parse the function-body. */
16070 cp_parser_function_body (parser
);
16071 /* Finish the function body. */
16072 finish_function_body (body
);
16074 return ctor_initializer_p
;
16077 /* Parse an initializer.
16080 = initializer-clause
16081 ( expression-list )
16083 Returns an expression representing the initializer. If no
16084 initializer is present, NULL_TREE is returned.
16086 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16087 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16088 set to TRUE if there is no initializer present. If there is an
16089 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16090 is set to true; otherwise it is set to false. */
16093 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
16094 bool* non_constant_p
)
16099 /* Peek at the next token. */
16100 token
= cp_lexer_peek_token (parser
->lexer
);
16102 /* Let our caller know whether or not this initializer was
16104 *is_direct_init
= (token
->type
!= CPP_EQ
);
16105 /* Assume that the initializer is constant. */
16106 *non_constant_p
= false;
16108 if (token
->type
== CPP_EQ
)
16110 /* Consume the `='. */
16111 cp_lexer_consume_token (parser
->lexer
);
16112 /* Parse the initializer-clause. */
16113 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
16115 else if (token
->type
== CPP_OPEN_PAREN
)
16118 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
16120 /*allow_expansion_p=*/true,
16123 return error_mark_node
;
16124 init
= build_tree_list_vec (vec
);
16125 release_tree_vector (vec
);
16127 else if (token
->type
== CPP_OPEN_BRACE
)
16129 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
16130 init
= cp_parser_braced_list (parser
, non_constant_p
);
16131 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
16135 /* Anything else is an error. */
16136 cp_parser_error (parser
, "expected initializer");
16137 init
= error_mark_node
;
16143 /* Parse an initializer-clause.
16145 initializer-clause:
16146 assignment-expression
16149 Returns an expression representing the initializer.
16151 If the `assignment-expression' production is used the value
16152 returned is simply a representation for the expression.
16154 Otherwise, calls cp_parser_braced_list. */
16157 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
16161 /* Assume the expression is constant. */
16162 *non_constant_p
= false;
16164 /* If it is not a `{', then we are looking at an
16165 assignment-expression. */
16166 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
16169 = cp_parser_constant_expression (parser
,
16170 /*allow_non_constant_p=*/true,
16172 if (!*non_constant_p
)
16173 initializer
= fold_non_dependent_expr (initializer
);
16176 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
16178 return initializer
;
16181 /* Parse a brace-enclosed initializer list.
16184 { initializer-list , [opt] }
16187 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16188 the elements of the initializer-list (or NULL, if the last
16189 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16190 NULL_TREE. There is no way to detect whether or not the optional
16191 trailing `,' was provided. NON_CONSTANT_P is as for
16192 cp_parser_initializer. */
16195 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
16199 /* Consume the `{' token. */
16200 cp_lexer_consume_token (parser
->lexer
);
16201 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16202 initializer
= make_node (CONSTRUCTOR
);
16203 /* If it's not a `}', then there is a non-trivial initializer. */
16204 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
16206 /* Parse the initializer list. */
16207 CONSTRUCTOR_ELTS (initializer
)
16208 = cp_parser_initializer_list (parser
, non_constant_p
);
16209 /* A trailing `,' token is allowed. */
16210 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
16211 cp_lexer_consume_token (parser
->lexer
);
16213 /* Now, there should be a trailing `}'. */
16214 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16215 TREE_TYPE (initializer
) = init_list_type_node
;
16216 return initializer
;
16219 /* Parse an initializer-list.
16222 initializer-clause ... [opt]
16223 initializer-list , initializer-clause ... [opt]
16228 identifier : initializer-clause
16229 initializer-list, identifier : initializer-clause
16231 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16232 for the initializer. If the INDEX of the elt is non-NULL, it is the
16233 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16234 as for cp_parser_initializer. */
16236 static VEC(constructor_elt
,gc
) *
16237 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
16239 VEC(constructor_elt
,gc
) *v
= NULL
;
16241 /* Assume all of the expressions are constant. */
16242 *non_constant_p
= false;
16244 /* Parse the rest of the list. */
16250 bool clause_non_constant_p
;
16252 /* If the next token is an identifier and the following one is a
16253 colon, we are looking at the GNU designated-initializer
16255 if (cp_parser_allow_gnu_extensions_p (parser
)
16256 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
16257 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
16259 /* Warn the user that they are using an extension. */
16260 pedwarn (input_location
, OPT_pedantic
,
16261 "ISO C++ does not allow designated initializers");
16262 /* Consume the identifier. */
16263 identifier
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16264 /* Consume the `:'. */
16265 cp_lexer_consume_token (parser
->lexer
);
16268 identifier
= NULL_TREE
;
16270 /* Parse the initializer. */
16271 initializer
= cp_parser_initializer_clause (parser
,
16272 &clause_non_constant_p
);
16273 /* If any clause is non-constant, so is the entire initializer. */
16274 if (clause_non_constant_p
)
16275 *non_constant_p
= true;
16277 /* If we have an ellipsis, this is an initializer pack
16279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16281 /* Consume the `...'. */
16282 cp_lexer_consume_token (parser
->lexer
);
16284 /* Turn the initializer into an initializer expansion. */
16285 initializer
= make_pack_expansion (initializer
);
16288 /* Add it to the vector. */
16289 CONSTRUCTOR_APPEND_ELT(v
, identifier
, initializer
);
16291 /* If the next token is not a comma, we have reached the end of
16293 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16296 /* Peek at the next token. */
16297 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16298 /* If the next token is a `}', then we're still done. An
16299 initializer-clause can have a trailing `,' after the
16300 initializer-list and before the closing `}'. */
16301 if (token
->type
== CPP_CLOSE_BRACE
)
16304 /* Consume the `,' token. */
16305 cp_lexer_consume_token (parser
->lexer
);
16311 /* Classes [gram.class] */
16313 /* Parse a class-name.
16319 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16320 to indicate that names looked up in dependent types should be
16321 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16322 keyword has been used to indicate that the name that appears next
16323 is a template. TAG_TYPE indicates the explicit tag given before
16324 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16325 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16326 is the class being defined in a class-head.
16328 Returns the TYPE_DECL representing the class. */
16331 cp_parser_class_name (cp_parser
*parser
,
16332 bool typename_keyword_p
,
16333 bool template_keyword_p
,
16334 enum tag_types tag_type
,
16335 bool check_dependency_p
,
16337 bool is_declaration
)
16343 tree identifier
= NULL_TREE
;
16345 /* All class-names start with an identifier. */
16346 token
= cp_lexer_peek_token (parser
->lexer
);
16347 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
16349 cp_parser_error (parser
, "expected class-name");
16350 return error_mark_node
;
16353 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16354 to a template-id, so we save it here. */
16355 scope
= parser
->scope
;
16356 if (scope
== error_mark_node
)
16357 return error_mark_node
;
16359 /* Any name names a type if we're following the `typename' keyword
16360 in a qualified name where the enclosing scope is type-dependent. */
16361 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
16362 && dependent_type_p (scope
));
16363 /* Handle the common case (an identifier, but not a template-id)
16365 if (token
->type
== CPP_NAME
16366 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
16368 cp_token
*identifier_token
;
16371 /* Look for the identifier. */
16372 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
16373 ambiguous_p
= identifier_token
->ambiguous_p
;
16374 identifier
= cp_parser_identifier (parser
);
16375 /* If the next token isn't an identifier, we are certainly not
16376 looking at a class-name. */
16377 if (identifier
== error_mark_node
)
16378 decl
= error_mark_node
;
16379 /* If we know this is a type-name, there's no need to look it
16381 else if (typename_p
)
16385 tree ambiguous_decls
;
16386 /* If we already know that this lookup is ambiguous, then
16387 we've already issued an error message; there's no reason
16391 cp_parser_simulate_error (parser
);
16392 return error_mark_node
;
16394 /* If the next token is a `::', then the name must be a type
16397 [basic.lookup.qual]
16399 During the lookup for a name preceding the :: scope
16400 resolution operator, object, function, and enumerator
16401 names are ignored. */
16402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16403 tag_type
= typename_type
;
16404 /* Look up the name. */
16405 decl
= cp_parser_lookup_name (parser
, identifier
,
16407 /*is_template=*/false,
16408 /*is_namespace=*/false,
16409 check_dependency_p
,
16411 identifier_token
->location
);
16412 if (ambiguous_decls
)
16414 if (cp_parser_parsing_tentatively (parser
))
16415 cp_parser_simulate_error (parser
);
16416 return error_mark_node
;
16422 /* Try a template-id. */
16423 decl
= cp_parser_template_id (parser
, template_keyword_p
,
16424 check_dependency_p
,
16426 if (decl
== error_mark_node
)
16427 return error_mark_node
;
16430 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
16432 /* If this is a typename, create a TYPENAME_TYPE. */
16433 if (typename_p
&& decl
!= error_mark_node
)
16435 decl
= make_typename_type (scope
, decl
, typename_type
,
16436 /*complain=*/tf_error
);
16437 if (decl
!= error_mark_node
)
16438 decl
= TYPE_NAME (decl
);
16441 /* Check to see that it is really the name of a class. */
16442 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
16443 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
16444 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16445 /* Situations like this:
16447 template <typename T> struct A {
16448 typename T::template X<int>::I i;
16451 are problematic. Is `T::template X<int>' a class-name? The
16452 standard does not seem to be definitive, but there is no other
16453 valid interpretation of the following `::'. Therefore, those
16454 names are considered class-names. */
16456 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
16457 if (decl
!= error_mark_node
)
16458 decl
= TYPE_NAME (decl
);
16460 else if (TREE_CODE (decl
) != TYPE_DECL
16461 || TREE_TYPE (decl
) == error_mark_node
16462 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
)))
16463 decl
= error_mark_node
;
16465 if (decl
== error_mark_node
)
16466 cp_parser_error (parser
, "expected class-name");
16467 else if (identifier
&& !parser
->scope
)
16468 maybe_note_name_used_in_class (identifier
, decl
);
16473 /* Parse a class-specifier.
16476 class-head { member-specification [opt] }
16478 Returns the TREE_TYPE representing the class. */
16481 cp_parser_class_specifier (cp_parser
* parser
)
16484 tree attributes
= NULL_TREE
;
16485 bool nested_name_specifier_p
;
16486 unsigned saved_num_template_parameter_lists
;
16487 bool saved_in_function_body
;
16488 bool saved_in_unbraced_linkage_specification_p
;
16489 tree old_scope
= NULL_TREE
;
16490 tree scope
= NULL_TREE
;
16493 push_deferring_access_checks (dk_no_deferred
);
16495 /* Parse the class-head. */
16496 type
= cp_parser_class_head (parser
,
16497 &nested_name_specifier_p
,
16500 /* If the class-head was a semantic disaster, skip the entire body
16504 cp_parser_skip_to_end_of_block_or_statement (parser
);
16505 pop_deferring_access_checks ();
16506 return error_mark_node
;
16509 /* Look for the `{'. */
16510 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
16512 pop_deferring_access_checks ();
16513 return error_mark_node
;
16516 /* Process the base classes. If they're invalid, skip the
16517 entire class body. */
16518 if (!xref_basetypes (type
, bases
))
16520 /* Consuming the closing brace yields better error messages
16522 if (cp_parser_skip_to_closing_brace (parser
))
16523 cp_lexer_consume_token (parser
->lexer
);
16524 pop_deferring_access_checks ();
16525 return error_mark_node
;
16528 /* Issue an error message if type-definitions are forbidden here. */
16529 cp_parser_check_type_definition (parser
);
16530 /* Remember that we are defining one more class. */
16531 ++parser
->num_classes_being_defined
;
16532 /* Inside the class, surrounding template-parameter-lists do not
16534 saved_num_template_parameter_lists
16535 = parser
->num_template_parameter_lists
;
16536 parser
->num_template_parameter_lists
= 0;
16537 /* We are not in a function body. */
16538 saved_in_function_body
= parser
->in_function_body
;
16539 parser
->in_function_body
= false;
16540 /* We are not immediately inside an extern "lang" block. */
16541 saved_in_unbraced_linkage_specification_p
16542 = parser
->in_unbraced_linkage_specification_p
;
16543 parser
->in_unbraced_linkage_specification_p
= false;
16545 /* Start the class. */
16546 if (nested_name_specifier_p
)
16548 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
16549 old_scope
= push_inner_scope (scope
);
16551 type
= begin_class_definition (type
, attributes
);
16553 if (type
== error_mark_node
)
16554 /* If the type is erroneous, skip the entire body of the class. */
16555 cp_parser_skip_to_closing_brace (parser
);
16557 /* Parse the member-specification. */
16558 cp_parser_member_specification_opt (parser
);
16560 /* Look for the trailing `}'. */
16561 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16562 /* Look for trailing attributes to apply to this class. */
16563 if (cp_parser_allow_gnu_extensions_p (parser
))
16564 attributes
= cp_parser_attributes_opt (parser
);
16565 if (type
!= error_mark_node
)
16566 type
= finish_struct (type
, attributes
);
16567 if (nested_name_specifier_p
)
16568 pop_inner_scope (old_scope
, scope
);
16569 /* If this class is not itself within the scope of another class,
16570 then we need to parse the bodies of all of the queued function
16571 definitions. Note that the queued functions defined in a class
16572 are not always processed immediately following the
16573 class-specifier for that class. Consider:
16576 struct B { void f() { sizeof (A); } };
16579 If `f' were processed before the processing of `A' were
16580 completed, there would be no way to compute the size of `A'.
16581 Note that the nesting we are interested in here is lexical --
16582 not the semantic nesting given by TYPE_CONTEXT. In particular,
16585 struct A { struct B; };
16586 struct A::B { void f() { } };
16588 there is no need to delay the parsing of `A::B::f'. */
16589 if (--parser
->num_classes_being_defined
== 0)
16592 tree class_type
= NULL_TREE
;
16593 tree pushed_scope
= NULL_TREE
;
16595 cp_default_arg_entry
*e
;
16597 /* In a first pass, parse default arguments to the functions.
16598 Then, in a second pass, parse the bodies of the functions.
16599 This two-phased approach handles cases like:
16607 FOR_EACH_VEC_ELT (cp_default_arg_entry
, unparsed_funs_with_default_args
,
16611 /* If there are default arguments that have not yet been processed,
16612 take care of them now. */
16613 if (class_type
!= e
->class_type
)
16616 pop_scope (pushed_scope
);
16617 class_type
= e
->class_type
;
16618 pushed_scope
= push_scope (class_type
);
16620 /* Make sure that any template parameters are in scope. */
16621 maybe_begin_member_template_processing (fn
);
16622 /* Parse the default argument expressions. */
16623 cp_parser_late_parsing_default_args (parser
, fn
);
16624 /* Remove any template parameters from the symbol table. */
16625 maybe_end_member_template_processing ();
16628 pop_scope (pushed_scope
);
16629 VEC_truncate (cp_default_arg_entry
, unparsed_funs_with_default_args
, 0);
16630 /* Now parse the body of the functions. */
16631 FOR_EACH_VEC_ELT (tree
, unparsed_funs_with_definitions
, ix
, fn
)
16632 cp_parser_late_parsing_for_member (parser
, fn
);
16633 VEC_truncate (tree
, unparsed_funs_with_definitions
, 0);
16636 /* Put back any saved access checks. */
16637 pop_deferring_access_checks ();
16639 /* Restore saved state. */
16640 parser
->in_function_body
= saved_in_function_body
;
16641 parser
->num_template_parameter_lists
16642 = saved_num_template_parameter_lists
;
16643 parser
->in_unbraced_linkage_specification_p
16644 = saved_in_unbraced_linkage_specification_p
;
16649 /* Parse a class-head.
16652 class-key identifier [opt] base-clause [opt]
16653 class-key nested-name-specifier identifier base-clause [opt]
16654 class-key nested-name-specifier [opt] template-id
16658 class-key attributes identifier [opt] base-clause [opt]
16659 class-key attributes nested-name-specifier identifier base-clause [opt]
16660 class-key attributes nested-name-specifier [opt] template-id
16663 Upon return BASES is initialized to the list of base classes (or
16664 NULL, if there are none) in the same form returned by
16665 cp_parser_base_clause.
16667 Returns the TYPE of the indicated class. Sets
16668 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16669 involving a nested-name-specifier was used, and FALSE otherwise.
16671 Returns error_mark_node if this is not a class-head.
16673 Returns NULL_TREE if the class-head is syntactically valid, but
16674 semantically invalid in a way that means we should skip the entire
16675 body of the class. */
16678 cp_parser_class_head (cp_parser
* parser
,
16679 bool* nested_name_specifier_p
,
16680 tree
*attributes_p
,
16683 tree nested_name_specifier
;
16684 enum tag_types class_key
;
16685 tree id
= NULL_TREE
;
16686 tree type
= NULL_TREE
;
16688 bool template_id_p
= false;
16689 bool qualified_p
= false;
16690 bool invalid_nested_name_p
= false;
16691 bool invalid_explicit_specialization_p
= false;
16692 tree pushed_scope
= NULL_TREE
;
16693 unsigned num_templates
;
16694 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
16695 /* Assume no nested-name-specifier will be present. */
16696 *nested_name_specifier_p
= false;
16697 /* Assume no template parameter lists will be used in defining the
16701 *bases
= NULL_TREE
;
16703 /* Look for the class-key. */
16704 class_key
= cp_parser_class_key (parser
);
16705 if (class_key
== none_type
)
16706 return error_mark_node
;
16708 /* Parse the attributes. */
16709 attributes
= cp_parser_attributes_opt (parser
);
16711 /* If the next token is `::', that is invalid -- but sometimes
16712 people do try to write:
16716 Handle this gracefully by accepting the extra qualifier, and then
16717 issuing an error about it later if this really is a
16718 class-head. If it turns out just to be an elaborated type
16719 specifier, remain silent. */
16720 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
16721 qualified_p
= true;
16723 push_deferring_access_checks (dk_no_check
);
16725 /* Determine the name of the class. Begin by looking for an
16726 optional nested-name-specifier. */
16727 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
16728 nested_name_specifier
16729 = cp_parser_nested_name_specifier_opt (parser
,
16730 /*typename_keyword_p=*/false,
16731 /*check_dependency_p=*/false,
16733 /*is_declaration=*/false);
16734 /* If there was a nested-name-specifier, then there *must* be an
16736 if (nested_name_specifier
)
16738 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16739 /* Although the grammar says `identifier', it really means
16740 `class-name' or `template-name'. You are only allowed to
16741 define a class that has already been declared with this
16744 The proposed resolution for Core Issue 180 says that wherever
16745 you see `class T::X' you should treat `X' as a type-name.
16747 It is OK to define an inaccessible class; for example:
16749 class A { class B; };
16752 We do not know if we will see a class-name, or a
16753 template-name. We look for a class-name first, in case the
16754 class-name is a template-id; if we looked for the
16755 template-name first we would stop after the template-name. */
16756 cp_parser_parse_tentatively (parser
);
16757 type
= cp_parser_class_name (parser
,
16758 /*typename_keyword_p=*/false,
16759 /*template_keyword_p=*/false,
16761 /*check_dependency_p=*/false,
16762 /*class_head_p=*/true,
16763 /*is_declaration=*/false);
16764 /* If that didn't work, ignore the nested-name-specifier. */
16765 if (!cp_parser_parse_definitely (parser
))
16767 invalid_nested_name_p
= true;
16768 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16769 id
= cp_parser_identifier (parser
);
16770 if (id
== error_mark_node
)
16773 /* If we could not find a corresponding TYPE, treat this
16774 declaration like an unqualified declaration. */
16775 if (type
== error_mark_node
)
16776 nested_name_specifier
= NULL_TREE
;
16777 /* Otherwise, count the number of templates used in TYPE and its
16778 containing scopes. */
16783 for (scope
= TREE_TYPE (type
);
16784 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
16785 scope
= (TYPE_P (scope
)
16786 ? TYPE_CONTEXT (scope
)
16787 : DECL_CONTEXT (scope
)))
16789 && CLASS_TYPE_P (scope
)
16790 && CLASSTYPE_TEMPLATE_INFO (scope
)
16791 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
16792 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
16796 /* Otherwise, the identifier is optional. */
16799 /* We don't know whether what comes next is a template-id,
16800 an identifier, or nothing at all. */
16801 cp_parser_parse_tentatively (parser
);
16802 /* Check for a template-id. */
16803 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16804 id
= cp_parser_template_id (parser
,
16805 /*template_keyword_p=*/false,
16806 /*check_dependency_p=*/true,
16807 /*is_declaration=*/true);
16808 /* If that didn't work, it could still be an identifier. */
16809 if (!cp_parser_parse_definitely (parser
))
16811 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16813 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16814 id
= cp_parser_identifier (parser
);
16821 template_id_p
= true;
16826 pop_deferring_access_checks ();
16829 cp_parser_check_for_invalid_template_id (parser
, id
,
16830 type_start_token
->location
);
16832 /* If it's not a `:' or a `{' then we can't really be looking at a
16833 class-head, since a class-head only appears as part of a
16834 class-specifier. We have to detect this situation before calling
16835 xref_tag, since that has irreversible side-effects. */
16836 if (!cp_parser_next_token_starts_class_definition_p (parser
))
16838 cp_parser_error (parser
, "expected %<{%> or %<:%>");
16839 return error_mark_node
;
16842 /* At this point, we're going ahead with the class-specifier, even
16843 if some other problem occurs. */
16844 cp_parser_commit_to_tentative_parse (parser
);
16845 /* Issue the error about the overly-qualified name now. */
16848 cp_parser_error (parser
,
16849 "global qualification of class name is invalid");
16850 return error_mark_node
;
16852 else if (invalid_nested_name_p
)
16854 cp_parser_error (parser
,
16855 "qualified name does not name a class");
16856 return error_mark_node
;
16858 else if (nested_name_specifier
)
16862 /* Reject typedef-names in class heads. */
16863 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
16865 error_at (type_start_token
->location
,
16866 "invalid class name in declaration of %qD",
16872 /* Figure out in what scope the declaration is being placed. */
16873 scope
= current_scope ();
16874 /* If that scope does not contain the scope in which the
16875 class was originally declared, the program is invalid. */
16876 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
16878 if (at_namespace_scope_p ())
16879 error_at (type_start_token
->location
,
16880 "declaration of %qD in namespace %qD which does not "
16882 type
, scope
, nested_name_specifier
);
16884 error_at (type_start_token
->location
,
16885 "declaration of %qD in %qD which does not enclose %qD",
16886 type
, scope
, nested_name_specifier
);
16892 A declarator-id shall not be qualified except for the
16893 definition of a ... nested class outside of its class
16894 ... [or] the definition or explicit instantiation of a
16895 class member of a namespace outside of its namespace. */
16896 if (scope
== nested_name_specifier
)
16898 permerror (nested_name_specifier_token_start
->location
,
16899 "extra qualification not allowed");
16900 nested_name_specifier
= NULL_TREE
;
16904 /* An explicit-specialization must be preceded by "template <>". If
16905 it is not, try to recover gracefully. */
16906 if (at_namespace_scope_p ()
16907 && parser
->num_template_parameter_lists
== 0
16910 error_at (type_start_token
->location
,
16911 "an explicit specialization must be preceded by %<template <>%>");
16912 invalid_explicit_specialization_p
= true;
16913 /* Take the same action that would have been taken by
16914 cp_parser_explicit_specialization. */
16915 ++parser
->num_template_parameter_lists
;
16916 begin_specialization ();
16918 /* There must be no "return" statements between this point and the
16919 end of this function; set "type "to the correct return value and
16920 use "goto done;" to return. */
16921 /* Make sure that the right number of template parameters were
16923 if (!cp_parser_check_template_parameters (parser
, num_templates
,
16924 type_start_token
->location
,
16925 /*declarator=*/NULL
))
16927 /* If something went wrong, there is no point in even trying to
16928 process the class-definition. */
16933 /* Look up the type. */
16936 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
16937 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
16938 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
16940 error_at (type_start_token
->location
,
16941 "function template %qD redeclared as a class template", id
);
16942 type
= error_mark_node
;
16946 type
= TREE_TYPE (id
);
16947 type
= maybe_process_partial_specialization (type
);
16949 if (nested_name_specifier
)
16950 pushed_scope
= push_scope (nested_name_specifier
);
16952 else if (nested_name_specifier
)
16958 template <typename T> struct S { struct T };
16959 template <typename T> struct S<T>::T { };
16961 we will get a TYPENAME_TYPE when processing the definition of
16962 `S::T'. We need to resolve it to the actual type before we
16963 try to define it. */
16964 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
16966 class_type
= resolve_typename_type (TREE_TYPE (type
),
16967 /*only_current_p=*/false);
16968 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
16969 type
= TYPE_NAME (class_type
);
16972 cp_parser_error (parser
, "could not resolve typename type");
16973 type
= error_mark_node
;
16977 if (maybe_process_partial_specialization (TREE_TYPE (type
))
16978 == error_mark_node
)
16984 class_type
= current_class_type
;
16985 /* Enter the scope indicated by the nested-name-specifier. */
16986 pushed_scope
= push_scope (nested_name_specifier
);
16987 /* Get the canonical version of this type. */
16988 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
16989 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16990 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
16992 type
= push_template_decl (type
);
16993 if (type
== error_mark_node
)
17000 type
= TREE_TYPE (type
);
17001 *nested_name_specifier_p
= true;
17003 else /* The name is not a nested name. */
17005 /* If the class was unnamed, create a dummy name. */
17007 id
= make_anon_name ();
17008 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
17009 parser
->num_template_parameter_lists
);
17012 /* Indicate whether this class was declared as a `class' or as a
17014 if (TREE_CODE (type
) == RECORD_TYPE
)
17015 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
17016 cp_parser_check_class_key (class_key
, type
);
17018 /* If this type was already complete, and we see another definition,
17019 that's an error. */
17020 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
17022 error_at (type_start_token
->location
, "redefinition of %q#T",
17024 error_at (type_start_token
->location
, "previous definition of %q+#T",
17029 else if (type
== error_mark_node
)
17032 /* We will have entered the scope containing the class; the names of
17033 base classes should be looked up in that context. For example:
17035 struct A { struct B {}; struct C; };
17036 struct A::C : B {};
17040 /* Get the list of base-classes, if there is one. */
17041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17042 *bases
= cp_parser_base_clause (parser
);
17045 /* Leave the scope given by the nested-name-specifier. We will
17046 enter the class scope itself while processing the members. */
17048 pop_scope (pushed_scope
);
17050 if (invalid_explicit_specialization_p
)
17052 end_specialization ();
17053 --parser
->num_template_parameter_lists
;
17057 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
17058 *attributes_p
= attributes
;
17062 /* Parse a class-key.
17069 Returns the kind of class-key specified, or none_type to indicate
17072 static enum tag_types
17073 cp_parser_class_key (cp_parser
* parser
)
17076 enum tag_types tag_type
;
17078 /* Look for the class-key. */
17079 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
17083 /* Check to see if the TOKEN is a class-key. */
17084 tag_type
= cp_parser_token_is_class_key (token
);
17086 cp_parser_error (parser
, "expected class-key");
17090 /* Parse an (optional) member-specification.
17092 member-specification:
17093 member-declaration member-specification [opt]
17094 access-specifier : member-specification [opt] */
17097 cp_parser_member_specification_opt (cp_parser
* parser
)
17104 /* Peek at the next token. */
17105 token
= cp_lexer_peek_token (parser
->lexer
);
17106 /* If it's a `}', or EOF then we've seen all the members. */
17107 if (token
->type
== CPP_CLOSE_BRACE
17108 || token
->type
== CPP_EOF
17109 || token
->type
== CPP_PRAGMA_EOL
)
17112 /* See if this token is a keyword. */
17113 keyword
= token
->keyword
;
17117 case RID_PROTECTED
:
17119 /* Consume the access-specifier. */
17120 cp_lexer_consume_token (parser
->lexer
);
17121 /* Remember which access-specifier is active. */
17122 current_access_specifier
= token
->u
.value
;
17123 /* Look for the `:'. */
17124 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17128 /* Accept #pragmas at class scope. */
17129 if (token
->type
== CPP_PRAGMA
)
17131 cp_parser_pragma (parser
, pragma_external
);
17135 /* Otherwise, the next construction must be a
17136 member-declaration. */
17137 cp_parser_member_declaration (parser
);
17142 /* Parse a member-declaration.
17144 member-declaration:
17145 decl-specifier-seq [opt] member-declarator-list [opt] ;
17146 function-definition ; [opt]
17147 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17149 template-declaration
17151 member-declarator-list:
17153 member-declarator-list , member-declarator
17156 declarator pure-specifier [opt]
17157 declarator constant-initializer [opt]
17158 identifier [opt] : constant-expression
17162 member-declaration:
17163 __extension__ member-declaration
17166 declarator attributes [opt] pure-specifier [opt]
17167 declarator attributes [opt] constant-initializer [opt]
17168 identifier [opt] attributes [opt] : constant-expression
17172 member-declaration:
17173 static_assert-declaration */
17176 cp_parser_member_declaration (cp_parser
* parser
)
17178 cp_decl_specifier_seq decl_specifiers
;
17179 tree prefix_attributes
;
17181 int declares_class_or_enum
;
17183 cp_token
*token
= NULL
;
17184 cp_token
*decl_spec_token_start
= NULL
;
17185 cp_token
*initializer_token_start
= NULL
;
17186 int saved_pedantic
;
17188 /* Check for the `__extension__' keyword. */
17189 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
17192 cp_parser_member_declaration (parser
);
17193 /* Restore the old value of the PEDANTIC flag. */
17194 pedantic
= saved_pedantic
;
17199 /* Check for a template-declaration. */
17200 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17202 /* An explicit specialization here is an error condition, and we
17203 expect the specialization handler to detect and report this. */
17204 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17205 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
17206 cp_parser_explicit_specialization (parser
);
17208 cp_parser_template_declaration (parser
, /*member_p=*/true);
17213 /* Check for a using-declaration. */
17214 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
17216 /* Parse the using-declaration. */
17217 cp_parser_using_declaration (parser
,
17218 /*access_declaration_p=*/false);
17222 /* Check for @defs. */
17223 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
17226 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
17227 ivar
= ivar_chains
;
17231 ivar
= TREE_CHAIN (member
);
17232 TREE_CHAIN (member
) = NULL_TREE
;
17233 finish_member_declaration (member
);
17238 /* If the next token is `static_assert' we have a static assertion. */
17239 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
17241 cp_parser_static_assert (parser
, /*member_p=*/true);
17245 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
17248 /* Parse the decl-specifier-seq. */
17249 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
17250 cp_parser_decl_specifier_seq (parser
,
17251 CP_PARSER_FLAGS_OPTIONAL
,
17253 &declares_class_or_enum
);
17254 prefix_attributes
= decl_specifiers
.attributes
;
17255 decl_specifiers
.attributes
= NULL_TREE
;
17256 /* Check for an invalid type-name. */
17257 if (!decl_specifiers
.any_type_specifiers_p
17258 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
17260 /* If there is no declarator, then the decl-specifier-seq should
17262 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17264 /* If there was no decl-specifier-seq, and the next token is a
17265 `;', then we have something like:
17271 Each member-declaration shall declare at least one member
17272 name of the class. */
17273 if (!decl_specifiers
.any_specifiers_p
)
17275 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17276 if (!in_system_header_at (token
->location
))
17277 pedwarn (token
->location
, OPT_pedantic
, "extra %<;%>");
17283 /* See if this declaration is a friend. */
17284 friend_p
= cp_parser_friend_p (&decl_specifiers
);
17285 /* If there were decl-specifiers, check to see if there was
17286 a class-declaration. */
17287 type
= check_tag_decl (&decl_specifiers
);
17288 /* Nested classes have already been added to the class, but
17289 a `friend' needs to be explicitly registered. */
17292 /* If the `friend' keyword was present, the friend must
17293 be introduced with a class-key. */
17294 if (!declares_class_or_enum
)
17295 error_at (decl_spec_token_start
->location
,
17296 "a class-key must be used when declaring a friend");
17299 template <typename T> struct A {
17300 friend struct A<T>::B;
17303 A<T>::B will be represented by a TYPENAME_TYPE, and
17304 therefore not recognized by check_tag_decl. */
17306 && decl_specifiers
.type
17307 && TYPE_P (decl_specifiers
.type
))
17308 type
= decl_specifiers
.type
;
17309 if (!type
|| !TYPE_P (type
))
17310 error_at (decl_spec_token_start
->location
,
17311 "friend declaration does not name a class or "
17314 make_friend_class (current_class_type
, type
,
17315 /*complain=*/true);
17317 /* If there is no TYPE, an error message will already have
17319 else if (!type
|| type
== error_mark_node
)
17321 /* An anonymous aggregate has to be handled specially; such
17322 a declaration really declares a data member (with a
17323 particular type), as opposed to a nested class. */
17324 else if (ANON_AGGR_TYPE_P (type
))
17326 /* Remove constructors and such from TYPE, now that we
17327 know it is an anonymous aggregate. */
17328 fixup_anonymous_aggr (type
);
17329 /* And make the corresponding data member. */
17330 decl
= build_decl (decl_spec_token_start
->location
,
17331 FIELD_DECL
, NULL_TREE
, type
);
17332 /* Add it to the class. */
17333 finish_member_declaration (decl
);
17336 cp_parser_check_access_in_redeclaration
17338 decl_spec_token_start
->location
);
17343 /* See if these declarations will be friends. */
17344 friend_p
= cp_parser_friend_p (&decl_specifiers
);
17346 /* Keep going until we hit the `;' at the end of the
17348 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17350 tree attributes
= NULL_TREE
;
17351 tree first_attribute
;
17353 /* Peek at the next token. */
17354 token
= cp_lexer_peek_token (parser
->lexer
);
17356 /* Check for a bitfield declaration. */
17357 if (token
->type
== CPP_COLON
17358 || (token
->type
== CPP_NAME
17359 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17365 /* Get the name of the bitfield. Note that we cannot just
17366 check TOKEN here because it may have been invalidated by
17367 the call to cp_lexer_peek_nth_token above. */
17368 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
17369 identifier
= cp_parser_identifier (parser
);
17371 identifier
= NULL_TREE
;
17373 /* Consume the `:' token. */
17374 cp_lexer_consume_token (parser
->lexer
);
17375 /* Get the width of the bitfield. */
17377 = cp_parser_constant_expression (parser
,
17378 /*allow_non_constant=*/false,
17381 /* Look for attributes that apply to the bitfield. */
17382 attributes
= cp_parser_attributes_opt (parser
);
17383 /* Remember which attributes are prefix attributes and
17385 first_attribute
= attributes
;
17386 /* Combine the attributes. */
17387 attributes
= chainon (prefix_attributes
, attributes
);
17389 /* Create the bitfield declaration. */
17390 decl
= grokbitfield (identifier
17391 ? make_id_declarator (NULL_TREE
,
17401 cp_declarator
*declarator
;
17403 tree asm_specification
;
17404 int ctor_dtor_or_conv_p
;
17406 /* Parse the declarator. */
17408 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17409 &ctor_dtor_or_conv_p
,
17410 /*parenthesized_p=*/NULL
,
17411 /*member_p=*/true);
17413 /* If something went wrong parsing the declarator, make sure
17414 that we at least consume some tokens. */
17415 if (declarator
== cp_error_declarator
)
17417 /* Skip to the end of the statement. */
17418 cp_parser_skip_to_end_of_statement (parser
);
17419 /* If the next token is not a semicolon, that is
17420 probably because we just skipped over the body of
17421 a function. So, we consume a semicolon if
17422 present, but do not issue an error message if it
17424 if (cp_lexer_next_token_is (parser
->lexer
,
17426 cp_lexer_consume_token (parser
->lexer
);
17430 if (declares_class_or_enum
& 2)
17431 cp_parser_check_for_definition_in_return_type
17432 (declarator
, decl_specifiers
.type
,
17433 decl_specifiers
.type_location
);
17435 /* Look for an asm-specification. */
17436 asm_specification
= cp_parser_asm_specification_opt (parser
);
17437 /* Look for attributes that apply to the declaration. */
17438 attributes
= cp_parser_attributes_opt (parser
);
17439 /* Remember which attributes are prefix attributes and
17441 first_attribute
= attributes
;
17442 /* Combine the attributes. */
17443 attributes
= chainon (prefix_attributes
, attributes
);
17445 /* If it's an `=', then we have a constant-initializer or a
17446 pure-specifier. It is not correct to parse the
17447 initializer before registering the member declaration
17448 since the member declaration should be in scope while
17449 its initializer is processed. However, the rest of the
17450 front end does not yet provide an interface that allows
17451 us to handle this correctly. */
17452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17456 A pure-specifier shall be used only in the declaration of
17457 a virtual function.
17459 A member-declarator can contain a constant-initializer
17460 only if it declares a static member of integral or
17463 Therefore, if the DECLARATOR is for a function, we look
17464 for a pure-specifier; otherwise, we look for a
17465 constant-initializer. When we call `grokfield', it will
17466 perform more stringent semantics checks. */
17467 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
17468 if (function_declarator_p (declarator
))
17469 initializer
= cp_parser_pure_specifier (parser
);
17471 /* Parse the initializer. */
17472 initializer
= cp_parser_constant_initializer (parser
);
17474 /* Otherwise, there is no initializer. */
17476 initializer
= NULL_TREE
;
17478 /* See if we are probably looking at a function
17479 definition. We are certainly not looking at a
17480 member-declarator. Calling `grokfield' has
17481 side-effects, so we must not do it unless we are sure
17482 that we are looking at a member-declarator. */
17483 if (cp_parser_token_starts_function_definition_p
17484 (cp_lexer_peek_token (parser
->lexer
)))
17486 /* The grammar does not allow a pure-specifier to be
17487 used when a member function is defined. (It is
17488 possible that this fact is an oversight in the
17489 standard, since a pure function may be defined
17490 outside of the class-specifier. */
17492 error_at (initializer_token_start
->location
,
17493 "pure-specifier on function-definition");
17494 decl
= cp_parser_save_member_function_body (parser
,
17498 /* If the member was not a friend, declare it here. */
17500 finish_member_declaration (decl
);
17501 /* Peek at the next token. */
17502 token
= cp_lexer_peek_token (parser
->lexer
);
17503 /* If the next token is a semicolon, consume it. */
17504 if (token
->type
== CPP_SEMICOLON
)
17505 cp_lexer_consume_token (parser
->lexer
);
17509 if (declarator
->kind
== cdk_function
)
17510 declarator
->id_loc
= token
->location
;
17511 /* Create the declaration. */
17512 decl
= grokfield (declarator
, &decl_specifiers
,
17513 initializer
, /*init_const_expr_p=*/true,
17518 /* Reset PREFIX_ATTRIBUTES. */
17519 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
17520 attributes
= TREE_CHAIN (attributes
);
17522 TREE_CHAIN (attributes
) = NULL_TREE
;
17524 /* If there is any qualification still in effect, clear it
17525 now; we will be starting fresh with the next declarator. */
17526 parser
->scope
= NULL_TREE
;
17527 parser
->qualifying_scope
= NULL_TREE
;
17528 parser
->object_scope
= NULL_TREE
;
17529 /* If it's a `,', then there are more declarators. */
17530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17531 cp_lexer_consume_token (parser
->lexer
);
17532 /* If the next token isn't a `;', then we have a parse error. */
17533 else if (cp_lexer_next_token_is_not (parser
->lexer
,
17536 cp_parser_error (parser
, "expected %<;%>");
17537 /* Skip tokens until we find a `;'. */
17538 cp_parser_skip_to_end_of_statement (parser
);
17545 /* Add DECL to the list of members. */
17547 finish_member_declaration (decl
);
17549 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17550 cp_parser_save_default_args (parser
, decl
);
17555 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17558 /* Parse a pure-specifier.
17563 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17564 Otherwise, ERROR_MARK_NODE is returned. */
17567 cp_parser_pure_specifier (cp_parser
* parser
)
17571 /* Look for the `=' token. */
17572 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
17573 return error_mark_node
;
17574 /* Look for the `0' token. */
17575 token
= cp_lexer_peek_token (parser
->lexer
);
17577 if (token
->type
== CPP_EOF
17578 || token
->type
== CPP_PRAGMA_EOL
)
17579 return error_mark_node
;
17581 cp_lexer_consume_token (parser
->lexer
);
17583 /* Accept = default or = delete in c++0x mode. */
17584 if (token
->keyword
== RID_DEFAULT
17585 || token
->keyword
== RID_DELETE
)
17587 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
17588 return token
->u
.value
;
17591 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17592 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
17594 cp_parser_error (parser
,
17595 "invalid pure specifier (only %<= 0%> is allowed)");
17596 cp_parser_skip_to_end_of_statement (parser
);
17597 return error_mark_node
;
17599 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17601 error_at (token
->location
, "templates may not be %<virtual%>");
17602 return error_mark_node
;
17605 return integer_zero_node
;
17608 /* Parse a constant-initializer.
17610 constant-initializer:
17611 = constant-expression
17613 Returns a representation of the constant-expression. */
17616 cp_parser_constant_initializer (cp_parser
* parser
)
17618 /* Look for the `=' token. */
17619 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
17620 return error_mark_node
;
17622 /* It is invalid to write:
17624 struct S { static const int i = { 7 }; };
17627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17629 cp_parser_error (parser
,
17630 "a brace-enclosed initializer is not allowed here");
17631 /* Consume the opening brace. */
17632 cp_lexer_consume_token (parser
->lexer
);
17633 /* Skip the initializer. */
17634 cp_parser_skip_to_closing_brace (parser
);
17635 /* Look for the trailing `}'. */
17636 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17638 return error_mark_node
;
17641 return cp_parser_constant_expression (parser
,
17642 /*allow_non_constant=*/false,
17646 /* Derived classes [gram.class.derived] */
17648 /* Parse a base-clause.
17651 : base-specifier-list
17653 base-specifier-list:
17654 base-specifier ... [opt]
17655 base-specifier-list , base-specifier ... [opt]
17657 Returns a TREE_LIST representing the base-classes, in the order in
17658 which they were declared. The representation of each node is as
17659 described by cp_parser_base_specifier.
17661 In the case that no bases are specified, this function will return
17662 NULL_TREE, not ERROR_MARK_NODE. */
17665 cp_parser_base_clause (cp_parser
* parser
)
17667 tree bases
= NULL_TREE
;
17669 /* Look for the `:' that begins the list. */
17670 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
17672 /* Scan the base-specifier-list. */
17677 bool pack_expansion_p
= false;
17679 /* Look for the base-specifier. */
17680 base
= cp_parser_base_specifier (parser
);
17681 /* Look for the (optional) ellipsis. */
17682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17684 /* Consume the `...'. */
17685 cp_lexer_consume_token (parser
->lexer
);
17687 pack_expansion_p
= true;
17690 /* Add BASE to the front of the list. */
17691 if (base
!= error_mark_node
)
17693 if (pack_expansion_p
)
17694 /* Make this a pack expansion type. */
17695 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
17698 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
17700 TREE_CHAIN (base
) = bases
;
17704 /* Peek at the next token. */
17705 token
= cp_lexer_peek_token (parser
->lexer
);
17706 /* If it's not a comma, then the list is complete. */
17707 if (token
->type
!= CPP_COMMA
)
17709 /* Consume the `,'. */
17710 cp_lexer_consume_token (parser
->lexer
);
17713 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17714 base class had a qualified name. However, the next name that
17715 appears is certainly not qualified. */
17716 parser
->scope
= NULL_TREE
;
17717 parser
->qualifying_scope
= NULL_TREE
;
17718 parser
->object_scope
= NULL_TREE
;
17720 return nreverse (bases
);
17723 /* Parse a base-specifier.
17726 :: [opt] nested-name-specifier [opt] class-name
17727 virtual access-specifier [opt] :: [opt] nested-name-specifier
17729 access-specifier virtual [opt] :: [opt] nested-name-specifier
17732 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17733 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17734 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17735 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17738 cp_parser_base_specifier (cp_parser
* parser
)
17742 bool virtual_p
= false;
17743 bool duplicate_virtual_error_issued_p
= false;
17744 bool duplicate_access_error_issued_p
= false;
17745 bool class_scope_p
, template_p
;
17746 tree access
= access_default_node
;
17749 /* Process the optional `virtual' and `access-specifier'. */
17752 /* Peek at the next token. */
17753 token
= cp_lexer_peek_token (parser
->lexer
);
17754 /* Process `virtual'. */
17755 switch (token
->keyword
)
17758 /* If `virtual' appears more than once, issue an error. */
17759 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
17761 cp_parser_error (parser
,
17762 "%<virtual%> specified more than once in base-specified");
17763 duplicate_virtual_error_issued_p
= true;
17768 /* Consume the `virtual' token. */
17769 cp_lexer_consume_token (parser
->lexer
);
17774 case RID_PROTECTED
:
17776 /* If more than one access specifier appears, issue an
17778 if (access
!= access_default_node
17779 && !duplicate_access_error_issued_p
)
17781 cp_parser_error (parser
,
17782 "more than one access specifier in base-specified");
17783 duplicate_access_error_issued_p
= true;
17786 access
= ridpointers
[(int) token
->keyword
];
17788 /* Consume the access-specifier. */
17789 cp_lexer_consume_token (parser
->lexer
);
17798 /* It is not uncommon to see programs mechanically, erroneously, use
17799 the 'typename' keyword to denote (dependent) qualified types
17800 as base classes. */
17801 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
17803 token
= cp_lexer_peek_token (parser
->lexer
);
17804 if (!processing_template_decl
)
17805 error_at (token
->location
,
17806 "keyword %<typename%> not allowed outside of templates");
17808 error_at (token
->location
,
17809 "keyword %<typename%> not allowed in this context "
17810 "(the base class is implicitly a type)");
17811 cp_lexer_consume_token (parser
->lexer
);
17814 /* Look for the optional `::' operator. */
17815 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
17816 /* Look for the nested-name-specifier. The simplest way to
17821 The keyword `typename' is not permitted in a base-specifier or
17822 mem-initializer; in these contexts a qualified name that
17823 depends on a template-parameter is implicitly assumed to be a
17826 is to pretend that we have seen the `typename' keyword at this
17828 cp_parser_nested_name_specifier_opt (parser
,
17829 /*typename_keyword_p=*/true,
17830 /*check_dependency_p=*/true,
17832 /*is_declaration=*/true);
17833 /* If the base class is given by a qualified name, assume that names
17834 we see are type names or templates, as appropriate. */
17835 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
17836 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
17838 /* Finally, look for the class-name. */
17839 type
= cp_parser_class_name (parser
,
17843 /*check_dependency_p=*/true,
17844 /*class_head_p=*/false,
17845 /*is_declaration=*/true);
17847 if (type
== error_mark_node
)
17848 return error_mark_node
;
17850 return finish_base_specifier (TREE_TYPE (type
), access
, virtual_p
);
17853 /* Exception handling [gram.exception] */
17855 /* Parse an (optional) exception-specification.
17857 exception-specification:
17858 throw ( type-id-list [opt] )
17860 Returns a TREE_LIST representing the exception-specification. The
17861 TREE_VALUE of each node is a type. */
17864 cp_parser_exception_specification_opt (cp_parser
* parser
)
17868 const char *saved_message
;
17870 /* Peek at the next token. */
17871 token
= cp_lexer_peek_token (parser
->lexer
);
17873 /* Is it a noexcept-specification? */
17874 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
17877 cp_lexer_consume_token (parser
->lexer
);
17879 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
17881 cp_lexer_consume_token (parser
->lexer
);
17883 /* Types may not be defined in an exception-specification. */
17884 saved_message
= parser
->type_definition_forbidden_message
;
17885 parser
->type_definition_forbidden_message
17886 = G_("types may not be defined in an exception-specification");
17888 expr
= cp_parser_constant_expression (parser
, false, NULL
);
17890 /* Restore the saved message. */
17891 parser
->type_definition_forbidden_message
= saved_message
;
17893 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17896 expr
= boolean_true_node
;
17898 return build_noexcept_spec (expr
, tf_warning_or_error
);
17901 /* If it's not `throw', then there's no exception-specification. */
17902 if (!cp_parser_is_keyword (token
, RID_THROW
))
17906 /* Enable this once a lot of code has transitioned to noexcept? */
17907 if (cxx_dialect
== cxx0x
&& !in_system_header
)
17908 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
17909 "deprecated in C++0x; use %<noexcept%> instead.");
17912 /* Consume the `throw'. */
17913 cp_lexer_consume_token (parser
->lexer
);
17915 /* Look for the `('. */
17916 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
17918 /* Peek at the next token. */
17919 token
= cp_lexer_peek_token (parser
->lexer
);
17920 /* If it's not a `)', then there is a type-id-list. */
17921 if (token
->type
!= CPP_CLOSE_PAREN
)
17923 /* Types may not be defined in an exception-specification. */
17924 saved_message
= parser
->type_definition_forbidden_message
;
17925 parser
->type_definition_forbidden_message
17926 = G_("types may not be defined in an exception-specification");
17927 /* Parse the type-id-list. */
17928 type_id_list
= cp_parser_type_id_list (parser
);
17929 /* Restore the saved message. */
17930 parser
->type_definition_forbidden_message
= saved_message
;
17933 type_id_list
= empty_except_spec
;
17935 /* Look for the `)'. */
17936 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17938 return type_id_list
;
17941 /* Parse an (optional) type-id-list.
17945 type-id-list , type-id ... [opt]
17947 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17948 in the order that the types were presented. */
17951 cp_parser_type_id_list (cp_parser
* parser
)
17953 tree types
= NULL_TREE
;
17960 /* Get the next type-id. */
17961 type
= cp_parser_type_id (parser
);
17962 /* Parse the optional ellipsis. */
17963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17965 /* Consume the `...'. */
17966 cp_lexer_consume_token (parser
->lexer
);
17968 /* Turn the type into a pack expansion expression. */
17969 type
= make_pack_expansion (type
);
17971 /* Add it to the list. */
17972 types
= add_exception_specifier (types
, type
, /*complain=*/1);
17973 /* Peek at the next token. */
17974 token
= cp_lexer_peek_token (parser
->lexer
);
17975 /* If it is not a `,', we are done. */
17976 if (token
->type
!= CPP_COMMA
)
17978 /* Consume the `,'. */
17979 cp_lexer_consume_token (parser
->lexer
);
17982 return nreverse (types
);
17985 /* Parse a try-block.
17988 try compound-statement handler-seq */
17991 cp_parser_try_block (cp_parser
* parser
)
17995 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
17996 try_block
= begin_try_block ();
17997 cp_parser_compound_statement (parser
, NULL
, true);
17998 finish_try_block (try_block
);
17999 cp_parser_handler_seq (parser
);
18000 finish_handler_sequence (try_block
);
18005 /* Parse a function-try-block.
18007 function-try-block:
18008 try ctor-initializer [opt] function-body handler-seq */
18011 cp_parser_function_try_block (cp_parser
* parser
)
18013 tree compound_stmt
;
18015 bool ctor_initializer_p
;
18017 /* Look for the `try' keyword. */
18018 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
18020 /* Let the rest of the front end know where we are. */
18021 try_block
= begin_function_try_block (&compound_stmt
);
18022 /* Parse the function-body. */
18024 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
18025 /* We're done with the `try' part. */
18026 finish_function_try_block (try_block
);
18027 /* Parse the handlers. */
18028 cp_parser_handler_seq (parser
);
18029 /* We're done with the handlers. */
18030 finish_function_handler_sequence (try_block
, compound_stmt
);
18032 return ctor_initializer_p
;
18035 /* Parse a handler-seq.
18038 handler handler-seq [opt] */
18041 cp_parser_handler_seq (cp_parser
* parser
)
18047 /* Parse the handler. */
18048 cp_parser_handler (parser
);
18049 /* Peek at the next token. */
18050 token
= cp_lexer_peek_token (parser
->lexer
);
18051 /* If it's not `catch' then there are no more handlers. */
18052 if (!cp_parser_is_keyword (token
, RID_CATCH
))
18057 /* Parse a handler.
18060 catch ( exception-declaration ) compound-statement */
18063 cp_parser_handler (cp_parser
* parser
)
18068 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
18069 handler
= begin_handler ();
18070 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18071 declaration
= cp_parser_exception_declaration (parser
);
18072 finish_handler_parms (declaration
, handler
);
18073 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18074 cp_parser_compound_statement (parser
, NULL
, false);
18075 finish_handler (handler
);
18078 /* Parse an exception-declaration.
18080 exception-declaration:
18081 type-specifier-seq declarator
18082 type-specifier-seq abstract-declarator
18086 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18087 ellipsis variant is used. */
18090 cp_parser_exception_declaration (cp_parser
* parser
)
18092 cp_decl_specifier_seq type_specifiers
;
18093 cp_declarator
*declarator
;
18094 const char *saved_message
;
18096 /* If it's an ellipsis, it's easy to handle. */
18097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18099 /* Consume the `...' token. */
18100 cp_lexer_consume_token (parser
->lexer
);
18104 /* Types may not be defined in exception-declarations. */
18105 saved_message
= parser
->type_definition_forbidden_message
;
18106 parser
->type_definition_forbidden_message
18107 = G_("types may not be defined in exception-declarations");
18109 /* Parse the type-specifier-seq. */
18110 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
18111 /*is_trailing_return=*/false,
18113 /* If it's a `)', then there is no declarator. */
18114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
18117 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
18118 /*ctor_dtor_or_conv_p=*/NULL
,
18119 /*parenthesized_p=*/NULL
,
18120 /*member_p=*/false);
18122 /* Restore the saved message. */
18123 parser
->type_definition_forbidden_message
= saved_message
;
18125 if (!type_specifiers
.any_specifiers_p
)
18126 return error_mark_node
;
18128 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
18131 /* Parse a throw-expression.
18134 throw assignment-expression [opt]
18136 Returns a THROW_EXPR representing the throw-expression. */
18139 cp_parser_throw_expression (cp_parser
* parser
)
18144 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
18145 token
= cp_lexer_peek_token (parser
->lexer
);
18146 /* Figure out whether or not there is an assignment-expression
18147 following the "throw" keyword. */
18148 if (token
->type
== CPP_COMMA
18149 || token
->type
== CPP_SEMICOLON
18150 || token
->type
== CPP_CLOSE_PAREN
18151 || token
->type
== CPP_CLOSE_SQUARE
18152 || token
->type
== CPP_CLOSE_BRACE
18153 || token
->type
== CPP_COLON
)
18154 expression
= NULL_TREE
;
18156 expression
= cp_parser_assignment_expression (parser
,
18157 /*cast_p=*/false, NULL
);
18159 return build_throw (expression
);
18162 /* GNU Extensions */
18164 /* Parse an (optional) asm-specification.
18167 asm ( string-literal )
18169 If the asm-specification is present, returns a STRING_CST
18170 corresponding to the string-literal. Otherwise, returns
18174 cp_parser_asm_specification_opt (cp_parser
* parser
)
18177 tree asm_specification
;
18179 /* Peek at the next token. */
18180 token
= cp_lexer_peek_token (parser
->lexer
);
18181 /* If the next token isn't the `asm' keyword, then there's no
18182 asm-specification. */
18183 if (!cp_parser_is_keyword (token
, RID_ASM
))
18186 /* Consume the `asm' token. */
18187 cp_lexer_consume_token (parser
->lexer
);
18188 /* Look for the `('. */
18189 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18191 /* Look for the string-literal. */
18192 asm_specification
= cp_parser_string_literal (parser
, false, false);
18194 /* Look for the `)'. */
18195 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18197 return asm_specification
;
18200 /* Parse an asm-operand-list.
18204 asm-operand-list , asm-operand
18207 string-literal ( expression )
18208 [ string-literal ] string-literal ( expression )
18210 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18211 each node is the expression. The TREE_PURPOSE is itself a
18212 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18213 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18214 is a STRING_CST for the string literal before the parenthesis. Returns
18215 ERROR_MARK_NODE if any of the operands are invalid. */
18218 cp_parser_asm_operand_list (cp_parser
* parser
)
18220 tree asm_operands
= NULL_TREE
;
18221 bool invalid_operands
= false;
18225 tree string_literal
;
18229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
18231 /* Consume the `[' token. */
18232 cp_lexer_consume_token (parser
->lexer
);
18233 /* Read the operand name. */
18234 name
= cp_parser_identifier (parser
);
18235 if (name
!= error_mark_node
)
18236 name
= build_string (IDENTIFIER_LENGTH (name
),
18237 IDENTIFIER_POINTER (name
));
18238 /* Look for the closing `]'. */
18239 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
18243 /* Look for the string-literal. */
18244 string_literal
= cp_parser_string_literal (parser
, false, false);
18246 /* Look for the `('. */
18247 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18248 /* Parse the expression. */
18249 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
18250 /* Look for the `)'. */
18251 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18253 if (name
== error_mark_node
18254 || string_literal
== error_mark_node
18255 || expression
== error_mark_node
)
18256 invalid_operands
= true;
18258 /* Add this operand to the list. */
18259 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
18262 /* If the next token is not a `,', there are no more
18264 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18266 /* Consume the `,'. */
18267 cp_lexer_consume_token (parser
->lexer
);
18270 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
18273 /* Parse an asm-clobber-list.
18277 asm-clobber-list , string-literal
18279 Returns a TREE_LIST, indicating the clobbers in the order that they
18280 appeared. The TREE_VALUE of each node is a STRING_CST. */
18283 cp_parser_asm_clobber_list (cp_parser
* parser
)
18285 tree clobbers
= NULL_TREE
;
18289 tree string_literal
;
18291 /* Look for the string literal. */
18292 string_literal
= cp_parser_string_literal (parser
, false, false);
18293 /* Add it to the list. */
18294 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
18295 /* If the next token is not a `,', then the list is
18297 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18299 /* Consume the `,' token. */
18300 cp_lexer_consume_token (parser
->lexer
);
18306 /* Parse an asm-label-list.
18310 asm-label-list , identifier
18312 Returns a TREE_LIST, indicating the labels in the order that they
18313 appeared. The TREE_VALUE of each node is a label. */
18316 cp_parser_asm_label_list (cp_parser
* parser
)
18318 tree labels
= NULL_TREE
;
18322 tree identifier
, label
, name
;
18324 /* Look for the identifier. */
18325 identifier
= cp_parser_identifier (parser
);
18326 if (!error_operand_p (identifier
))
18328 label
= lookup_label (identifier
);
18329 if (TREE_CODE (label
) == LABEL_DECL
)
18331 TREE_USED (label
) = 1;
18332 check_goto (label
);
18333 name
= build_string (IDENTIFIER_LENGTH (identifier
),
18334 IDENTIFIER_POINTER (identifier
));
18335 labels
= tree_cons (name
, label
, labels
);
18338 /* If the next token is not a `,', then the list is
18340 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18342 /* Consume the `,' token. */
18343 cp_lexer_consume_token (parser
->lexer
);
18346 return nreverse (labels
);
18349 /* Parse an (optional) series of attributes.
18352 attributes attribute
18355 __attribute__ (( attribute-list [opt] ))
18357 The return value is as for cp_parser_attribute_list. */
18360 cp_parser_attributes_opt (cp_parser
* parser
)
18362 tree attributes
= NULL_TREE
;
18367 tree attribute_list
;
18369 /* Peek at the next token. */
18370 token
= cp_lexer_peek_token (parser
->lexer
);
18371 /* If it's not `__attribute__', then we're done. */
18372 if (token
->keyword
!= RID_ATTRIBUTE
)
18375 /* Consume the `__attribute__' keyword. */
18376 cp_lexer_consume_token (parser
->lexer
);
18377 /* Look for the two `(' tokens. */
18378 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18379 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
18381 /* Peek at the next token. */
18382 token
= cp_lexer_peek_token (parser
->lexer
);
18383 if (token
->type
!= CPP_CLOSE_PAREN
)
18384 /* Parse the attribute-list. */
18385 attribute_list
= cp_parser_attribute_list (parser
);
18387 /* If the next token is a `)', then there is no attribute
18389 attribute_list
= NULL
;
18391 /* Look for the two `)' tokens. */
18392 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18393 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18395 /* Add these new attributes to the list. */
18396 attributes
= chainon (attributes
, attribute_list
);
18402 /* Parse an attribute-list.
18406 attribute-list , attribute
18410 identifier ( identifier )
18411 identifier ( identifier , expression-list )
18412 identifier ( expression-list )
18414 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18415 to an attribute. The TREE_PURPOSE of each node is the identifier
18416 indicating which attribute is in use. The TREE_VALUE represents
18417 the arguments, if any. */
18420 cp_parser_attribute_list (cp_parser
* parser
)
18422 tree attribute_list
= NULL_TREE
;
18423 bool save_translate_strings_p
= parser
->translate_strings_p
;
18425 parser
->translate_strings_p
= false;
18432 /* Look for the identifier. We also allow keywords here; for
18433 example `__attribute__ ((const))' is legal. */
18434 token
= cp_lexer_peek_token (parser
->lexer
);
18435 if (token
->type
== CPP_NAME
18436 || token
->type
== CPP_KEYWORD
)
18438 tree arguments
= NULL_TREE
;
18440 /* Consume the token. */
18441 token
= cp_lexer_consume_token (parser
->lexer
);
18443 /* Save away the identifier that indicates which attribute
18445 identifier
= (token
->type
== CPP_KEYWORD
)
18446 /* For keywords, use the canonical spelling, not the
18447 parsed identifier. */
18448 ? ridpointers
[(int) token
->keyword
]
18451 attribute
= build_tree_list (identifier
, NULL_TREE
);
18453 /* Peek at the next token. */
18454 token
= cp_lexer_peek_token (parser
->lexer
);
18455 /* If it's an `(', then parse the attribute arguments. */
18456 if (token
->type
== CPP_OPEN_PAREN
)
18459 int attr_flag
= (attribute_takes_identifier_p (identifier
)
18460 ? id_attr
: normal_attr
);
18461 vec
= cp_parser_parenthesized_expression_list
18462 (parser
, attr_flag
, /*cast_p=*/false,
18463 /*allow_expansion_p=*/false,
18464 /*non_constant_p=*/NULL
);
18466 arguments
= error_mark_node
;
18469 arguments
= build_tree_list_vec (vec
);
18470 release_tree_vector (vec
);
18472 /* Save the arguments away. */
18473 TREE_VALUE (attribute
) = arguments
;
18476 if (arguments
!= error_mark_node
)
18478 /* Add this attribute to the list. */
18479 TREE_CHAIN (attribute
) = attribute_list
;
18480 attribute_list
= attribute
;
18483 token
= cp_lexer_peek_token (parser
->lexer
);
18485 /* Now, look for more attributes. If the next token isn't a
18486 `,', we're done. */
18487 if (token
->type
!= CPP_COMMA
)
18490 /* Consume the comma and keep going. */
18491 cp_lexer_consume_token (parser
->lexer
);
18493 parser
->translate_strings_p
= save_translate_strings_p
;
18495 /* We built up the list in reverse order. */
18496 return nreverse (attribute_list
);
18499 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18500 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18501 current value of the PEDANTIC flag, regardless of whether or not
18502 the `__extension__' keyword is present. The caller is responsible
18503 for restoring the value of the PEDANTIC flag. */
18506 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
18508 /* Save the old value of the PEDANTIC flag. */
18509 *saved_pedantic
= pedantic
;
18511 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
18513 /* Consume the `__extension__' token. */
18514 cp_lexer_consume_token (parser
->lexer
);
18515 /* We're not being pedantic while the `__extension__' keyword is
18525 /* Parse a label declaration.
18528 __label__ label-declarator-seq ;
18530 label-declarator-seq:
18531 identifier , label-declarator-seq
18535 cp_parser_label_declaration (cp_parser
* parser
)
18537 /* Look for the `__label__' keyword. */
18538 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
18544 /* Look for an identifier. */
18545 identifier
= cp_parser_identifier (parser
);
18546 /* If we failed, stop. */
18547 if (identifier
== error_mark_node
)
18549 /* Declare it as a label. */
18550 finish_label_decl (identifier
);
18551 /* If the next token is a `;', stop. */
18552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18554 /* Look for the `,' separating the label declarations. */
18555 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
18558 /* Look for the final `;'. */
18559 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18562 /* Support Functions */
18564 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18565 NAME should have one of the representations used for an
18566 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18567 is returned. If PARSER->SCOPE is a dependent type, then a
18568 SCOPE_REF is returned.
18570 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18571 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18572 was formed. Abstractly, such entities should not be passed to this
18573 function, because they do not need to be looked up, but it is
18574 simpler to check for this special case here, rather than at the
18577 In cases not explicitly covered above, this function returns a
18578 DECL, OVERLOAD, or baselink representing the result of the lookup.
18579 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18582 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18583 (e.g., "struct") that was used. In that case bindings that do not
18584 refer to types are ignored.
18586 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18589 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18592 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18595 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18596 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18597 NULL_TREE otherwise. */
18600 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
18601 enum tag_types tag_type
,
18604 bool check_dependency
,
18605 tree
*ambiguous_decls
,
18606 location_t name_location
)
18610 tree object_type
= parser
->context
->object_type
;
18612 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18613 flags
|= LOOKUP_COMPLAIN
;
18615 /* Assume that the lookup will be unambiguous. */
18616 if (ambiguous_decls
)
18617 *ambiguous_decls
= NULL_TREE
;
18619 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18620 no longer valid. Note that if we are parsing tentatively, and
18621 the parse fails, OBJECT_TYPE will be automatically restored. */
18622 parser
->context
->object_type
= NULL_TREE
;
18624 if (name
== error_mark_node
)
18625 return error_mark_node
;
18627 /* A template-id has already been resolved; there is no lookup to
18629 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
18631 if (BASELINK_P (name
))
18633 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
18634 == TEMPLATE_ID_EXPR
);
18638 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18639 it should already have been checked to make sure that the name
18640 used matches the type being destroyed. */
18641 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
18645 /* Figure out to which type this destructor applies. */
18647 type
= parser
->scope
;
18648 else if (object_type
)
18649 type
= object_type
;
18651 type
= current_class_type
;
18652 /* If that's not a class type, there is no destructor. */
18653 if (!type
|| !CLASS_TYPE_P (type
))
18654 return error_mark_node
;
18655 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
18656 lazily_declare_fn (sfk_destructor
, type
);
18657 if (!CLASSTYPE_DESTRUCTORS (type
))
18658 return error_mark_node
;
18659 /* If it was a class type, return the destructor. */
18660 return CLASSTYPE_DESTRUCTORS (type
);
18663 /* By this point, the NAME should be an ordinary identifier. If
18664 the id-expression was a qualified name, the qualifying scope is
18665 stored in PARSER->SCOPE at this point. */
18666 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
18668 /* Perform the lookup. */
18673 if (parser
->scope
== error_mark_node
)
18674 return error_mark_node
;
18676 /* If the SCOPE is dependent, the lookup must be deferred until
18677 the template is instantiated -- unless we are explicitly
18678 looking up names in uninstantiated templates. Even then, we
18679 cannot look up the name if the scope is not a class type; it
18680 might, for example, be a template type parameter. */
18681 dependent_p
= (TYPE_P (parser
->scope
)
18682 && dependent_scope_p (parser
->scope
));
18683 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
18685 /* Defer lookup. */
18686 decl
= error_mark_node
;
18689 tree pushed_scope
= NULL_TREE
;
18691 /* If PARSER->SCOPE is a dependent type, then it must be a
18692 class type, and we must not be checking dependencies;
18693 otherwise, we would have processed this lookup above. So
18694 that PARSER->SCOPE is not considered a dependent base by
18695 lookup_member, we must enter the scope here. */
18697 pushed_scope
= push_scope (parser
->scope
);
18699 /* If the PARSER->SCOPE is a template specialization, it
18700 may be instantiated during name lookup. In that case,
18701 errors may be issued. Even if we rollback the current
18702 tentative parse, those errors are valid. */
18703 decl
= lookup_qualified_name (parser
->scope
, name
,
18704 tag_type
!= none_type
,
18705 /*complain=*/true);
18707 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18708 lookup result and the nested-name-specifier nominates a class C:
18709 * if the name specified after the nested-name-specifier, when
18710 looked up in C, is the injected-class-name of C (Clause 9), or
18711 * if the name specified after the nested-name-specifier is the
18712 same as the identifier or the simple-template-id's template-
18713 name in the last component of the nested-name-specifier,
18714 the name is instead considered to name the constructor of
18715 class C. [ Note: for example, the constructor is not an
18716 acceptable lookup result in an elaborated-type-specifier so
18717 the constructor would not be used in place of the
18718 injected-class-name. --end note ] Such a constructor name
18719 shall be used only in the declarator-id of a declaration that
18720 names a constructor or in a using-declaration. */
18721 if (tag_type
== none_type
18722 && DECL_SELF_REFERENCE_P (decl
)
18723 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
18724 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
18725 tag_type
!= none_type
,
18726 /*complain=*/true);
18728 /* If we have a single function from a using decl, pull it out. */
18729 if (TREE_CODE (decl
) == OVERLOAD
18730 && !really_overloaded_fn (decl
))
18731 decl
= OVL_FUNCTION (decl
);
18734 pop_scope (pushed_scope
);
18737 /* If the scope is a dependent type and either we deferred lookup or
18738 we did lookup but didn't find the name, rememeber the name. */
18739 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
18740 && dependent_type_p (parser
->scope
))
18746 /* The resolution to Core Issue 180 says that `struct
18747 A::B' should be considered a type-name, even if `A'
18749 type
= make_typename_type (parser
->scope
, name
, tag_type
,
18750 /*complain=*/tf_error
);
18751 decl
= TYPE_NAME (type
);
18753 else if (is_template
18754 && (cp_parser_next_token_ends_template_argument_p (parser
)
18755 || cp_lexer_next_token_is (parser
->lexer
,
18757 decl
= make_unbound_class_template (parser
->scope
,
18759 /*complain=*/tf_error
);
18761 decl
= build_qualified_name (/*type=*/NULL_TREE
,
18762 parser
->scope
, name
,
18765 parser
->qualifying_scope
= parser
->scope
;
18766 parser
->object_scope
= NULL_TREE
;
18768 else if (object_type
)
18770 tree object_decl
= NULL_TREE
;
18771 /* Look up the name in the scope of the OBJECT_TYPE, unless the
18772 OBJECT_TYPE is not a class. */
18773 if (CLASS_TYPE_P (object_type
))
18774 /* If the OBJECT_TYPE is a template specialization, it may
18775 be instantiated during name lookup. In that case, errors
18776 may be issued. Even if we rollback the current tentative
18777 parse, those errors are valid. */
18778 object_decl
= lookup_member (object_type
,
18781 tag_type
!= none_type
);
18782 /* Look it up in the enclosing context, too. */
18783 decl
= lookup_name_real (name
, tag_type
!= none_type
,
18785 /*block_p=*/true, is_namespace
, flags
);
18786 parser
->object_scope
= object_type
;
18787 parser
->qualifying_scope
= NULL_TREE
;
18789 decl
= object_decl
;
18793 decl
= lookup_name_real (name
, tag_type
!= none_type
,
18795 /*block_p=*/true, is_namespace
, flags
);
18796 parser
->qualifying_scope
= NULL_TREE
;
18797 parser
->object_scope
= NULL_TREE
;
18800 /* If the lookup failed, let our caller know. */
18801 if (!decl
|| decl
== error_mark_node
)
18802 return error_mark_node
;
18804 /* Pull out the template from an injected-class-name (or multiple). */
18806 decl
= maybe_get_template_decl_from_type_decl (decl
);
18808 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18809 if (TREE_CODE (decl
) == TREE_LIST
)
18811 if (ambiguous_decls
)
18812 *ambiguous_decls
= decl
;
18813 /* The error message we have to print is too complicated for
18814 cp_parser_error, so we incorporate its actions directly. */
18815 if (!cp_parser_simulate_error (parser
))
18817 error_at (name_location
, "reference to %qD is ambiguous",
18819 print_candidates (decl
);
18821 return error_mark_node
;
18824 gcc_assert (DECL_P (decl
)
18825 || TREE_CODE (decl
) == OVERLOAD
18826 || TREE_CODE (decl
) == SCOPE_REF
18827 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
18828 || BASELINK_P (decl
));
18830 /* If we have resolved the name of a member declaration, check to
18831 see if the declaration is accessible. When the name resolves to
18832 set of overloaded functions, accessibility is checked when
18833 overload resolution is done.
18835 During an explicit instantiation, access is not checked at all,
18836 as per [temp.explicit]. */
18838 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
18843 /* Like cp_parser_lookup_name, but for use in the typical case where
18844 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18845 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18848 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
18850 return cp_parser_lookup_name (parser
, name
,
18852 /*is_template=*/false,
18853 /*is_namespace=*/false,
18854 /*check_dependency=*/true,
18855 /*ambiguous_decls=*/NULL
,
18859 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18860 the current context, return the TYPE_DECL. If TAG_NAME_P is
18861 true, the DECL indicates the class being defined in a class-head,
18862 or declared in an elaborated-type-specifier.
18864 Otherwise, return DECL. */
18867 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
18869 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18870 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18873 template <typename T> struct B;
18876 template <typename T> struct A::B {};
18878 Similarly, in an elaborated-type-specifier:
18880 namespace N { struct X{}; }
18883 template <typename T> friend struct N::X;
18886 However, if the DECL refers to a class type, and we are in
18887 the scope of the class, then the name lookup automatically
18888 finds the TYPE_DECL created by build_self_reference rather
18889 than a TEMPLATE_DECL. For example, in:
18891 template <class T> struct S {
18895 there is no need to handle such case. */
18897 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
18898 return DECL_TEMPLATE_RESULT (decl
);
18903 /* If too many, or too few, template-parameter lists apply to the
18904 declarator, issue an error message. Returns TRUE if all went well,
18905 and FALSE otherwise. */
18908 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
18909 cp_declarator
*declarator
,
18910 location_t declarator_location
)
18912 unsigned num_templates
;
18914 /* We haven't seen any classes that involve template parameters yet. */
18917 switch (declarator
->kind
)
18920 if (declarator
->u
.id
.qualifying_scope
)
18924 scope
= declarator
->u
.id
.qualifying_scope
;
18926 while (scope
&& CLASS_TYPE_P (scope
))
18928 /* You're supposed to have one `template <...>'
18929 for every template class, but you don't need one
18930 for a full specialization. For example:
18932 template <class T> struct S{};
18933 template <> struct S<int> { void f(); };
18934 void S<int>::f () {}
18936 is correct; there shouldn't be a `template <>' for
18937 the definition of `S<int>::f'. */
18938 if (!CLASSTYPE_TEMPLATE_INFO (scope
))
18939 /* If SCOPE does not have template information of any
18940 kind, then it is not a template, nor is it nested
18941 within a template. */
18943 if (explicit_class_specialization_p (scope
))
18945 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
)))
18948 scope
= TYPE_CONTEXT (scope
);
18951 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
18952 == TEMPLATE_ID_EXPR
)
18953 /* If the DECLARATOR has the form `X<y>' then it uses one
18954 additional level of template parameters. */
18957 return cp_parser_check_template_parameters
18958 (parser
, num_templates
, declarator_location
, declarator
);
18964 case cdk_reference
:
18966 return (cp_parser_check_declarator_template_parameters
18967 (parser
, declarator
->declarator
, declarator_location
));
18973 gcc_unreachable ();
18978 /* NUM_TEMPLATES were used in the current declaration. If that is
18979 invalid, return FALSE and issue an error messages. Otherwise,
18980 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18981 declarator and we can print more accurate diagnostics. */
18984 cp_parser_check_template_parameters (cp_parser
* parser
,
18985 unsigned num_templates
,
18986 location_t location
,
18987 cp_declarator
*declarator
)
18989 /* If there are the same number of template classes and parameter
18990 lists, that's OK. */
18991 if (parser
->num_template_parameter_lists
== num_templates
)
18993 /* If there are more, but only one more, then we are referring to a
18994 member template. That's OK too. */
18995 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
18997 /* If there are more template classes than parameter lists, we have
19000 template <class T> void S<T>::R<T>::f (); */
19001 if (parser
->num_template_parameter_lists
< num_templates
)
19003 if (declarator
&& !current_function_decl
)
19004 error_at (location
, "specializing member %<%T::%E%> "
19005 "requires %<template<>%> syntax",
19006 declarator
->u
.id
.qualifying_scope
,
19007 declarator
->u
.id
.unqualified_name
);
19008 else if (declarator
)
19009 error_at (location
, "invalid declaration of %<%T::%E%>",
19010 declarator
->u
.id
.qualifying_scope
,
19011 declarator
->u
.id
.unqualified_name
);
19013 error_at (location
, "too few template-parameter-lists");
19016 /* Otherwise, there are too many template parameter lists. We have
19019 template <class T> template <class U> void S::f(); */
19020 error_at (location
, "too many template-parameter-lists");
19024 /* Parse an optional `::' token indicating that the following name is
19025 from the global namespace. If so, PARSER->SCOPE is set to the
19026 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19027 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19028 Returns the new value of PARSER->SCOPE, if the `::' token is
19029 present, and NULL_TREE otherwise. */
19032 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
19036 /* Peek at the next token. */
19037 token
= cp_lexer_peek_token (parser
->lexer
);
19038 /* If we're looking at a `::' token then we're starting from the
19039 global namespace, not our current location. */
19040 if (token
->type
== CPP_SCOPE
)
19042 /* Consume the `::' token. */
19043 cp_lexer_consume_token (parser
->lexer
);
19044 /* Set the SCOPE so that we know where to start the lookup. */
19045 parser
->scope
= global_namespace
;
19046 parser
->qualifying_scope
= global_namespace
;
19047 parser
->object_scope
= NULL_TREE
;
19049 return parser
->scope
;
19051 else if (!current_scope_valid_p
)
19053 parser
->scope
= NULL_TREE
;
19054 parser
->qualifying_scope
= NULL_TREE
;
19055 parser
->object_scope
= NULL_TREE
;
19061 /* Returns TRUE if the upcoming token sequence is the start of a
19062 constructor declarator. If FRIEND_P is true, the declarator is
19063 preceded by the `friend' specifier. */
19066 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
19068 bool constructor_p
;
19069 tree nested_name_specifier
;
19070 cp_token
*next_token
;
19072 /* The common case is that this is not a constructor declarator, so
19073 try to avoid doing lots of work if at all possible. It's not
19074 valid declare a constructor at function scope. */
19075 if (parser
->in_function_body
)
19077 /* And only certain tokens can begin a constructor declarator. */
19078 next_token
= cp_lexer_peek_token (parser
->lexer
);
19079 if (next_token
->type
!= CPP_NAME
19080 && next_token
->type
!= CPP_SCOPE
19081 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
19082 && next_token
->type
!= CPP_TEMPLATE_ID
)
19085 /* Parse tentatively; we are going to roll back all of the tokens
19087 cp_parser_parse_tentatively (parser
);
19088 /* Assume that we are looking at a constructor declarator. */
19089 constructor_p
= true;
19091 /* Look for the optional `::' operator. */
19092 cp_parser_global_scope_opt (parser
,
19093 /*current_scope_valid_p=*/false);
19094 /* Look for the nested-name-specifier. */
19095 nested_name_specifier
19096 = (cp_parser_nested_name_specifier_opt (parser
,
19097 /*typename_keyword_p=*/false,
19098 /*check_dependency_p=*/false,
19100 /*is_declaration=*/false));
19101 /* Outside of a class-specifier, there must be a
19102 nested-name-specifier. */
19103 if (!nested_name_specifier
&&
19104 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
19106 constructor_p
= false;
19107 else if (nested_name_specifier
== error_mark_node
)
19108 constructor_p
= false;
19110 /* If we have a class scope, this is easy; DR 147 says that S::S always
19111 names the constructor, and no other qualified name could. */
19112 if (constructor_p
&& nested_name_specifier
19113 && TYPE_P (nested_name_specifier
))
19115 tree id
= cp_parser_unqualified_id (parser
,
19116 /*template_keyword_p=*/false,
19117 /*check_dependency_p=*/false,
19118 /*declarator_p=*/true,
19119 /*optional_p=*/false);
19120 if (is_overloaded_fn (id
))
19121 id
= DECL_NAME (get_first_fn (id
));
19122 if (!constructor_name_p (id
, nested_name_specifier
))
19123 constructor_p
= false;
19125 /* If we still think that this might be a constructor-declarator,
19126 look for a class-name. */
19127 else if (constructor_p
)
19131 template <typename T> struct S {
19135 we must recognize that the nested `S' names a class. */
19137 type_decl
= cp_parser_class_name (parser
,
19138 /*typename_keyword_p=*/false,
19139 /*template_keyword_p=*/false,
19141 /*check_dependency_p=*/false,
19142 /*class_head_p=*/false,
19143 /*is_declaration=*/false);
19144 /* If there was no class-name, then this is not a constructor. */
19145 constructor_p
= !cp_parser_error_occurred (parser
);
19147 /* If we're still considering a constructor, we have to see a `(',
19148 to begin the parameter-declaration-clause, followed by either a
19149 `)', an `...', or a decl-specifier. We need to check for a
19150 type-specifier to avoid being fooled into thinking that:
19154 is a constructor. (It is actually a function named `f' that
19155 takes one parameter (of type `int') and returns a value of type
19158 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19159 constructor_p
= false;
19162 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
19163 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
19164 /* A parameter declaration begins with a decl-specifier,
19165 which is either the "attribute" keyword, a storage class
19166 specifier, or (usually) a type-specifier. */
19167 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
19170 tree pushed_scope
= NULL_TREE
;
19171 unsigned saved_num_template_parameter_lists
;
19173 /* Names appearing in the type-specifier should be looked up
19174 in the scope of the class. */
19175 if (current_class_type
)
19179 type
= TREE_TYPE (type_decl
);
19180 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19182 type
= resolve_typename_type (type
,
19183 /*only_current_p=*/false);
19184 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19186 cp_parser_abort_tentative_parse (parser
);
19190 pushed_scope
= push_scope (type
);
19193 /* Inside the constructor parameter list, surrounding
19194 template-parameter-lists do not apply. */
19195 saved_num_template_parameter_lists
19196 = parser
->num_template_parameter_lists
;
19197 parser
->num_template_parameter_lists
= 0;
19199 /* Look for the type-specifier. */
19200 cp_parser_type_specifier (parser
,
19201 CP_PARSER_FLAGS_NONE
,
19202 /*decl_specs=*/NULL
,
19203 /*is_declarator=*/true,
19204 /*declares_class_or_enum=*/NULL
,
19205 /*is_cv_qualifier=*/NULL
);
19207 parser
->num_template_parameter_lists
19208 = saved_num_template_parameter_lists
;
19210 /* Leave the scope of the class. */
19212 pop_scope (pushed_scope
);
19214 constructor_p
= !cp_parser_error_occurred (parser
);
19218 /* We did not really want to consume any tokens. */
19219 cp_parser_abort_tentative_parse (parser
);
19221 return constructor_p
;
19224 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19225 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19226 they must be performed once we are in the scope of the function.
19228 Returns the function defined. */
19231 cp_parser_function_definition_from_specifiers_and_declarator
19232 (cp_parser
* parser
,
19233 cp_decl_specifier_seq
*decl_specifiers
,
19235 const cp_declarator
*declarator
)
19240 /* Begin the function-definition. */
19241 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
19243 /* The things we're about to see are not directly qualified by any
19244 template headers we've seen thus far. */
19245 reset_specialization ();
19247 /* If there were names looked up in the decl-specifier-seq that we
19248 did not check, check them now. We must wait until we are in the
19249 scope of the function to perform the checks, since the function
19250 might be a friend. */
19251 perform_deferred_access_checks ();
19255 /* Skip the entire function. */
19256 cp_parser_skip_to_end_of_block_or_statement (parser
);
19257 fn
= error_mark_node
;
19259 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
19261 /* Seen already, skip it. An error message has already been output. */
19262 cp_parser_skip_to_end_of_block_or_statement (parser
);
19263 fn
= current_function_decl
;
19264 current_function_decl
= NULL_TREE
;
19265 /* If this is a function from a class, pop the nested class. */
19266 if (current_class_name
)
19267 pop_nested_class ();
19270 fn
= cp_parser_function_definition_after_declarator (parser
,
19271 /*inline_p=*/false);
19276 /* Parse the part of a function-definition that follows the
19277 declarator. INLINE_P is TRUE iff this function is an inline
19278 function defined within a class-specifier.
19280 Returns the function defined. */
19283 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
19287 bool ctor_initializer_p
= false;
19288 bool saved_in_unbraced_linkage_specification_p
;
19289 bool saved_in_function_body
;
19290 unsigned saved_num_template_parameter_lists
;
19293 saved_in_function_body
= parser
->in_function_body
;
19294 parser
->in_function_body
= true;
19295 /* If the next token is `return', then the code may be trying to
19296 make use of the "named return value" extension that G++ used to
19298 token
= cp_lexer_peek_token (parser
->lexer
);
19299 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
19301 /* Consume the `return' keyword. */
19302 cp_lexer_consume_token (parser
->lexer
);
19303 /* Look for the identifier that indicates what value is to be
19305 cp_parser_identifier (parser
);
19306 /* Issue an error message. */
19307 error_at (token
->location
,
19308 "named return values are no longer supported");
19309 /* Skip tokens until we reach the start of the function body. */
19312 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19313 if (token
->type
== CPP_OPEN_BRACE
19314 || token
->type
== CPP_EOF
19315 || token
->type
== CPP_PRAGMA_EOL
)
19317 cp_lexer_consume_token (parser
->lexer
);
19320 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19321 anything declared inside `f'. */
19322 saved_in_unbraced_linkage_specification_p
19323 = parser
->in_unbraced_linkage_specification_p
;
19324 parser
->in_unbraced_linkage_specification_p
= false;
19325 /* Inside the function, surrounding template-parameter-lists do not
19327 saved_num_template_parameter_lists
19328 = parser
->num_template_parameter_lists
;
19329 parser
->num_template_parameter_lists
= 0;
19331 start_lambda_scope (current_function_decl
);
19333 /* If the next token is `try', then we are looking at a
19334 function-try-block. */
19335 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
19336 ctor_initializer_p
= cp_parser_function_try_block (parser
);
19337 /* A function-try-block includes the function-body, so we only do
19338 this next part if we're not processing a function-try-block. */
19341 = cp_parser_ctor_initializer_opt_and_function_body (parser
);
19343 finish_lambda_scope ();
19345 /* Finish the function. */
19346 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
19347 (inline_p
? 2 : 0));
19348 /* Generate code for it, if necessary. */
19349 expand_or_defer_fn (fn
);
19350 /* Restore the saved values. */
19351 parser
->in_unbraced_linkage_specification_p
19352 = saved_in_unbraced_linkage_specification_p
;
19353 parser
->num_template_parameter_lists
19354 = saved_num_template_parameter_lists
;
19355 parser
->in_function_body
= saved_in_function_body
;
19360 /* Parse a template-declaration, assuming that the `export' (and
19361 `extern') keywords, if present, has already been scanned. MEMBER_P
19362 is as for cp_parser_template_declaration. */
19365 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
19367 tree decl
= NULL_TREE
;
19368 VEC (deferred_access_check
,gc
) *checks
;
19369 tree parameter_list
;
19370 bool friend_p
= false;
19371 bool need_lang_pop
;
19374 /* Look for the `template' keyword. */
19375 token
= cp_lexer_peek_token (parser
->lexer
);
19376 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
19380 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
19382 if (at_class_scope_p () && current_function_decl
)
19384 /* 14.5.2.2 [temp.mem]
19386 A local class shall not have member templates. */
19387 error_at (token
->location
,
19388 "invalid declaration of member template in local class");
19389 cp_parser_skip_to_end_of_block_or_statement (parser
);
19394 A template ... shall not have C linkage. */
19395 if (current_lang_name
== lang_name_c
)
19397 error_at (token
->location
, "template with C linkage");
19398 /* Give it C++ linkage to avoid confusing other parts of the
19400 push_lang_context (lang_name_cplusplus
);
19401 need_lang_pop
= true;
19404 need_lang_pop
= false;
19406 /* We cannot perform access checks on the template parameter
19407 declarations until we know what is being declared, just as we
19408 cannot check the decl-specifier list. */
19409 push_deferring_access_checks (dk_deferred
);
19411 /* If the next token is `>', then we have an invalid
19412 specialization. Rather than complain about an invalid template
19413 parameter, issue an error message here. */
19414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
19416 cp_parser_error (parser
, "invalid explicit specialization");
19417 begin_specialization ();
19418 parameter_list
= NULL_TREE
;
19421 /* Parse the template parameters. */
19422 parameter_list
= cp_parser_template_parameter_list (parser
);
19424 /* Get the deferred access checks from the parameter list. These
19425 will be checked once we know what is being declared, as for a
19426 member template the checks must be performed in the scope of the
19427 class containing the member. */
19428 checks
= get_deferred_access_checks ();
19430 /* Look for the `>'. */
19431 cp_parser_skip_to_end_of_template_parameter_list (parser
);
19432 /* We just processed one more parameter list. */
19433 ++parser
->num_template_parameter_lists
;
19434 /* If the next token is `template', there are more template
19436 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
19438 cp_parser_template_declaration_after_export (parser
, member_p
);
19441 /* There are no access checks when parsing a template, as we do not
19442 know if a specialization will be a friend. */
19443 push_deferring_access_checks (dk_no_check
);
19444 token
= cp_lexer_peek_token (parser
->lexer
);
19445 decl
= cp_parser_single_declaration (parser
,
19448 /*explicit_specialization_p=*/false,
19450 pop_deferring_access_checks ();
19452 /* If this is a member template declaration, let the front
19454 if (member_p
&& !friend_p
&& decl
)
19456 if (TREE_CODE (decl
) == TYPE_DECL
)
19457 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
19459 decl
= finish_member_template_decl (decl
);
19461 else if (friend_p
&& decl
&& TREE_CODE (decl
) == TYPE_DECL
)
19462 make_friend_class (current_class_type
, TREE_TYPE (decl
),
19463 /*complain=*/true);
19465 /* We are done with the current parameter list. */
19466 --parser
->num_template_parameter_lists
;
19468 pop_deferring_access_checks ();
19471 finish_template_decl (parameter_list
);
19473 /* Register member declarations. */
19474 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
19475 finish_member_declaration (decl
);
19476 /* For the erroneous case of a template with C linkage, we pushed an
19477 implicit C++ linkage scope; exit that scope now. */
19479 pop_lang_context ();
19480 /* If DECL is a function template, we must return to parse it later.
19481 (Even though there is no definition, there might be default
19482 arguments that need handling.) */
19483 if (member_p
&& decl
19484 && (TREE_CODE (decl
) == FUNCTION_DECL
19485 || DECL_FUNCTION_TEMPLATE_P (decl
)))
19486 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, decl
);
19489 /* Perform the deferred access checks from a template-parameter-list.
19490 CHECKS is a TREE_LIST of access checks, as returned by
19491 get_deferred_access_checks. */
19494 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
19496 ++processing_template_parmlist
;
19497 perform_access_checks (checks
);
19498 --processing_template_parmlist
;
19501 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19502 `function-definition' sequence. MEMBER_P is true, this declaration
19503 appears in a class scope.
19505 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19506 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19509 cp_parser_single_declaration (cp_parser
* parser
,
19510 VEC (deferred_access_check
,gc
)* checks
,
19512 bool explicit_specialization_p
,
19515 int declares_class_or_enum
;
19516 tree decl
= NULL_TREE
;
19517 cp_decl_specifier_seq decl_specifiers
;
19518 bool function_definition_p
= false;
19519 cp_token
*decl_spec_token_start
;
19521 /* This function is only used when processing a template
19523 gcc_assert (innermost_scope_kind () == sk_template_parms
19524 || innermost_scope_kind () == sk_template_spec
);
19526 /* Defer access checks until we know what is being declared. */
19527 push_deferring_access_checks (dk_deferred
);
19529 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19531 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
19532 cp_parser_decl_specifier_seq (parser
,
19533 CP_PARSER_FLAGS_OPTIONAL
,
19535 &declares_class_or_enum
);
19537 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
19539 /* There are no template typedefs. */
19540 if (decl_specifiers
.specs
[(int) ds_typedef
])
19542 error_at (decl_spec_token_start
->location
,
19543 "template declaration of %<typedef%>");
19544 decl
= error_mark_node
;
19547 /* Gather up the access checks that occurred the
19548 decl-specifier-seq. */
19549 stop_deferring_access_checks ();
19551 /* Check for the declaration of a template class. */
19552 if (declares_class_or_enum
)
19554 if (cp_parser_declares_only_class_p (parser
))
19556 decl
= shadow_tag (&decl_specifiers
);
19561 friend template <typename T> struct A<T>::B;
19564 A<T>::B will be represented by a TYPENAME_TYPE, and
19565 therefore not recognized by shadow_tag. */
19566 if (friend_p
&& *friend_p
19568 && decl_specifiers
.type
19569 && TYPE_P (decl_specifiers
.type
))
19570 decl
= decl_specifiers
.type
;
19572 if (decl
&& decl
!= error_mark_node
)
19573 decl
= TYPE_NAME (decl
);
19575 decl
= error_mark_node
;
19577 /* Perform access checks for template parameters. */
19578 cp_parser_perform_template_parameter_access_checks (checks
);
19582 /* Complain about missing 'typename' or other invalid type names. */
19583 if (!decl_specifiers
.any_type_specifiers_p
)
19584 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
19586 /* If it's not a template class, try for a template function. If
19587 the next token is a `;', then this declaration does not declare
19588 anything. But, if there were errors in the decl-specifiers, then
19589 the error might well have come from an attempted class-specifier.
19590 In that case, there's no need to warn about a missing declarator. */
19592 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
19593 || decl_specifiers
.type
!= error_mark_node
))
19595 decl
= cp_parser_init_declarator (parser
,
19598 /*function_definition_allowed_p=*/true,
19600 declares_class_or_enum
,
19601 &function_definition_p
);
19603 /* 7.1.1-1 [dcl.stc]
19605 A storage-class-specifier shall not be specified in an explicit
19606 specialization... */
19608 && explicit_specialization_p
19609 && decl_specifiers
.storage_class
!= sc_none
)
19611 error_at (decl_spec_token_start
->location
,
19612 "explicit template specialization cannot have a storage class");
19613 decl
= error_mark_node
;
19617 pop_deferring_access_checks ();
19619 /* Clear any current qualification; whatever comes next is the start
19620 of something new. */
19621 parser
->scope
= NULL_TREE
;
19622 parser
->qualifying_scope
= NULL_TREE
;
19623 parser
->object_scope
= NULL_TREE
;
19624 /* Look for a trailing `;' after the declaration. */
19625 if (!function_definition_p
19626 && (decl
== error_mark_node
19627 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
19628 cp_parser_skip_to_end_of_block_or_statement (parser
);
19633 /* Parse a cast-expression that is not the operand of a unary "&". */
19636 cp_parser_simple_cast_expression (cp_parser
*parser
)
19638 return cp_parser_cast_expression (parser
, /*address_p=*/false,
19639 /*cast_p=*/false, NULL
);
19642 /* Parse a functional cast to TYPE. Returns an expression
19643 representing the cast. */
19646 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
19649 tree expression_list
;
19653 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19655 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19656 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
19657 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
19658 if (TREE_CODE (type
) == TYPE_DECL
)
19659 type
= TREE_TYPE (type
);
19660 return finish_compound_literal (type
, expression_list
);
19664 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19666 /*allow_expansion_p=*/true,
19667 /*non_constant_p=*/NULL
);
19669 expression_list
= error_mark_node
;
19672 expression_list
= build_tree_list_vec (vec
);
19673 release_tree_vector (vec
);
19676 cast
= build_functional_cast (type
, expression_list
,
19677 tf_warning_or_error
);
19678 /* [expr.const]/1: In an integral constant expression "only type
19679 conversions to integral or enumeration type can be used". */
19680 if (TREE_CODE (type
) == TYPE_DECL
)
19681 type
= TREE_TYPE (type
);
19682 if (cast
!= error_mark_node
19683 && !cast_valid_in_integral_constant_expression_p (type
)
19684 && cp_parser_non_integral_constant_expression (parser
,
19686 return error_mark_node
;
19690 /* Save the tokens that make up the body of a member function defined
19691 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19692 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19693 specifiers applied to the declaration. Returns the FUNCTION_DECL
19694 for the member function. */
19697 cp_parser_save_member_function_body (cp_parser
* parser
,
19698 cp_decl_specifier_seq
*decl_specifiers
,
19699 cp_declarator
*declarator
,
19706 /* Create the FUNCTION_DECL. */
19707 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
19708 /* If something went badly wrong, bail out now. */
19709 if (fn
== error_mark_node
)
19711 /* If there's a function-body, skip it. */
19712 if (cp_parser_token_starts_function_definition_p
19713 (cp_lexer_peek_token (parser
->lexer
)))
19714 cp_parser_skip_to_end_of_block_or_statement (parser
);
19715 return error_mark_node
;
19718 /* Remember it, if there default args to post process. */
19719 cp_parser_save_default_args (parser
, fn
);
19721 /* Save away the tokens that make up the body of the
19723 first
= parser
->lexer
->next_token
;
19724 /* We can have braced-init-list mem-initializers before the fn body. */
19725 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19727 cp_lexer_consume_token (parser
->lexer
);
19728 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
19729 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
19731 /* cache_group will stop after an un-nested { } pair, too. */
19732 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
19735 /* variadic mem-inits have ... after the ')'. */
19736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19737 cp_lexer_consume_token (parser
->lexer
);
19740 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
19741 /* Handle function try blocks. */
19742 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
19743 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
19744 last
= parser
->lexer
->next_token
;
19746 /* Save away the inline definition; we will process it when the
19747 class is complete. */
19748 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
19749 DECL_PENDING_INLINE_P (fn
) = 1;
19751 /* We need to know that this was defined in the class, so that
19752 friend templates are handled correctly. */
19753 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
19755 /* Add FN to the queue of functions to be parsed later. */
19756 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, fn
);
19761 /* Parse a template-argument-list, as well as the trailing ">" (but
19762 not the opening ">"). See cp_parser_template_argument_list for the
19766 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
19770 tree saved_qualifying_scope
;
19771 tree saved_object_scope
;
19772 bool saved_greater_than_is_operator_p
;
19773 int saved_unevaluated_operand
;
19774 int saved_inhibit_evaluation_warnings
;
19778 When parsing a template-id, the first non-nested `>' is taken as
19779 the end of the template-argument-list rather than a greater-than
19781 saved_greater_than_is_operator_p
19782 = parser
->greater_than_is_operator_p
;
19783 parser
->greater_than_is_operator_p
= false;
19784 /* Parsing the argument list may modify SCOPE, so we save it
19786 saved_scope
= parser
->scope
;
19787 saved_qualifying_scope
= parser
->qualifying_scope
;
19788 saved_object_scope
= parser
->object_scope
;
19789 /* We need to evaluate the template arguments, even though this
19790 template-id may be nested within a "sizeof". */
19791 saved_unevaluated_operand
= cp_unevaluated_operand
;
19792 cp_unevaluated_operand
= 0;
19793 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
19794 c_inhibit_evaluation_warnings
= 0;
19795 /* Parse the template-argument-list itself. */
19796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
19797 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
19798 arguments
= NULL_TREE
;
19800 arguments
= cp_parser_template_argument_list (parser
);
19801 /* Look for the `>' that ends the template-argument-list. If we find
19802 a '>>' instead, it's probably just a typo. */
19803 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
19805 if (cxx_dialect
!= cxx98
)
19807 /* In C++0x, a `>>' in a template argument list or cast
19808 expression is considered to be two separate `>'
19809 tokens. So, change the current token to a `>', but don't
19810 consume it: it will be consumed later when the outer
19811 template argument list (or cast expression) is parsed.
19812 Note that this replacement of `>' for `>>' is necessary
19813 even if we are parsing tentatively: in the tentative
19814 case, after calling
19815 cp_parser_enclosed_template_argument_list we will always
19816 throw away all of the template arguments and the first
19817 closing `>', either because the template argument list
19818 was erroneous or because we are replacing those tokens
19819 with a CPP_TEMPLATE_ID token. The second `>' (which will
19820 not have been thrown away) is needed either to close an
19821 outer template argument list or to complete a new-style
19823 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19824 token
->type
= CPP_GREATER
;
19826 else if (!saved_greater_than_is_operator_p
)
19828 /* If we're in a nested template argument list, the '>>' has
19829 to be a typo for '> >'. We emit the error message, but we
19830 continue parsing and we push a '>' as next token, so that
19831 the argument list will be parsed correctly. Note that the
19832 global source location is still on the token before the
19833 '>>', so we need to say explicitly where we want it. */
19834 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19835 error_at (token
->location
, "%<>>%> should be %<> >%> "
19836 "within a nested template argument list");
19838 token
->type
= CPP_GREATER
;
19842 /* If this is not a nested template argument list, the '>>'
19843 is a typo for '>'. Emit an error message and continue.
19844 Same deal about the token location, but here we can get it
19845 right by consuming the '>>' before issuing the diagnostic. */
19846 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
19847 error_at (token
->location
,
19848 "spurious %<>>%>, use %<>%> to terminate "
19849 "a template argument list");
19853 cp_parser_skip_to_end_of_template_parameter_list (parser
);
19854 /* The `>' token might be a greater-than operator again now. */
19855 parser
->greater_than_is_operator_p
19856 = saved_greater_than_is_operator_p
;
19857 /* Restore the SAVED_SCOPE. */
19858 parser
->scope
= saved_scope
;
19859 parser
->qualifying_scope
= saved_qualifying_scope
;
19860 parser
->object_scope
= saved_object_scope
;
19861 cp_unevaluated_operand
= saved_unevaluated_operand
;
19862 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
19867 /* MEMBER_FUNCTION is a member function, or a friend. If default
19868 arguments, or the body of the function have not yet been parsed,
19872 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
19874 /* If this member is a template, get the underlying
19876 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
19877 member_function
= DECL_TEMPLATE_RESULT (member_function
);
19879 /* There should not be any class definitions in progress at this
19880 point; the bodies of members are only parsed outside of all class
19882 gcc_assert (parser
->num_classes_being_defined
== 0);
19883 /* While we're parsing the member functions we might encounter more
19884 classes. We want to handle them right away, but we don't want
19885 them getting mixed up with functions that are currently in the
19887 push_unparsed_function_queues (parser
);
19889 /* Make sure that any template parameters are in scope. */
19890 maybe_begin_member_template_processing (member_function
);
19892 /* If the body of the function has not yet been parsed, parse it
19894 if (DECL_PENDING_INLINE_P (member_function
))
19896 tree function_scope
;
19897 cp_token_cache
*tokens
;
19899 /* The function is no longer pending; we are processing it. */
19900 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
19901 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
19902 DECL_PENDING_INLINE_P (member_function
) = 0;
19904 /* If this is a local class, enter the scope of the containing
19906 function_scope
= current_function_decl
;
19907 if (function_scope
)
19908 push_function_context ();
19910 /* Push the body of the function onto the lexer stack. */
19911 cp_parser_push_lexer_for_tokens (parser
, tokens
);
19913 /* Let the front end know that we going to be defining this
19915 start_preparsed_function (member_function
, NULL_TREE
,
19916 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
19918 /* Don't do access checking if it is a templated function. */
19919 if (processing_template_decl
)
19920 push_deferring_access_checks (dk_no_check
);
19922 /* Now, parse the body of the function. */
19923 cp_parser_function_definition_after_declarator (parser
,
19924 /*inline_p=*/true);
19926 if (processing_template_decl
)
19927 pop_deferring_access_checks ();
19929 /* Leave the scope of the containing function. */
19930 if (function_scope
)
19931 pop_function_context ();
19932 cp_parser_pop_lexer (parser
);
19935 /* Remove any template parameters from the symbol table. */
19936 maybe_end_member_template_processing ();
19938 /* Restore the queue. */
19939 pop_unparsed_function_queues (parser
);
19942 /* If DECL contains any default args, remember it on the unparsed
19943 functions queue. */
19946 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
19950 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
19952 probe
= TREE_CHAIN (probe
))
19953 if (TREE_PURPOSE (probe
))
19955 cp_default_arg_entry
*entry
19956 = VEC_safe_push (cp_default_arg_entry
, gc
,
19957 unparsed_funs_with_default_args
, NULL
);
19958 entry
->class_type
= current_class_type
;
19959 entry
->decl
= decl
;
19964 /* FN is a FUNCTION_DECL which may contains a parameter with an
19965 unparsed DEFAULT_ARG. Parse the default args now. This function
19966 assumes that the current scope is the scope in which the default
19967 argument should be processed. */
19970 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
19972 bool saved_local_variables_forbidden_p
;
19973 tree parm
, parmdecl
;
19975 /* While we're parsing the default args, we might (due to the
19976 statement expression extension) encounter more classes. We want
19977 to handle them right away, but we don't want them getting mixed
19978 up with default args that are currently in the queue. */
19979 push_unparsed_function_queues (parser
);
19981 /* Local variable names (and the `this' keyword) may not appear
19982 in a default argument. */
19983 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19984 parser
->local_variables_forbidden_p
= true;
19986 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
19987 parmdecl
= DECL_ARGUMENTS (fn
);
19988 parm
&& parm
!= void_list_node
;
19989 parm
= TREE_CHAIN (parm
),
19990 parmdecl
= DECL_CHAIN (parmdecl
))
19992 cp_token_cache
*tokens
;
19993 tree default_arg
= TREE_PURPOSE (parm
);
19995 VEC(tree
,gc
) *insts
;
20002 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
20003 /* This can happen for a friend declaration for a function
20004 already declared with default arguments. */
20007 /* Push the saved tokens for the default argument onto the parser's
20009 tokens
= DEFARG_TOKENS (default_arg
);
20010 cp_parser_push_lexer_for_tokens (parser
, tokens
);
20012 start_lambda_scope (parmdecl
);
20014 /* Parse the assignment-expression. */
20015 parsed_arg
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
20016 if (parsed_arg
== error_mark_node
)
20018 cp_parser_pop_lexer (parser
);
20022 if (!processing_template_decl
)
20023 parsed_arg
= check_default_argument (TREE_VALUE (parm
), parsed_arg
);
20025 TREE_PURPOSE (parm
) = parsed_arg
;
20027 /* Update any instantiations we've already created. */
20028 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
20029 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
20030 TREE_PURPOSE (copy
) = parsed_arg
;
20032 finish_lambda_scope ();
20034 /* If the token stream has not been completely used up, then
20035 there was extra junk after the end of the default
20037 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
20038 cp_parser_error (parser
, "expected %<,%>");
20040 /* Revert to the main lexer. */
20041 cp_parser_pop_lexer (parser
);
20044 /* Make sure no default arg is missing. */
20045 check_default_args (fn
);
20047 /* Restore the state of local_variables_forbidden_p. */
20048 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
20050 /* Restore the queue. */
20051 pop_unparsed_function_queues (parser
);
20054 /* Parse the operand of `sizeof' (or a similar operator). Returns
20055 either a TYPE or an expression, depending on the form of the
20056 input. The KEYWORD indicates which kind of expression we have
20060 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
20062 tree expr
= NULL_TREE
;
20063 const char *saved_message
;
20065 bool saved_integral_constant_expression_p
;
20066 bool saved_non_integral_constant_expression_p
;
20067 bool pack_expansion_p
= false;
20069 /* Types cannot be defined in a `sizeof' expression. Save away the
20071 saved_message
= parser
->type_definition_forbidden_message
;
20072 /* And create the new one. */
20073 tmp
= concat ("types may not be defined in %<",
20074 IDENTIFIER_POINTER (ridpointers
[keyword
]),
20075 "%> expressions", NULL
);
20076 parser
->type_definition_forbidden_message
= tmp
;
20078 /* The restrictions on constant-expressions do not apply inside
20079 sizeof expressions. */
20080 saved_integral_constant_expression_p
20081 = parser
->integral_constant_expression_p
;
20082 saved_non_integral_constant_expression_p
20083 = parser
->non_integral_constant_expression_p
;
20084 parser
->integral_constant_expression_p
= false;
20086 /* If it's a `...', then we are computing the length of a parameter
20088 if (keyword
== RID_SIZEOF
20089 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20091 /* Consume the `...'. */
20092 cp_lexer_consume_token (parser
->lexer
);
20093 maybe_warn_variadic_templates ();
20095 /* Note that this is an expansion. */
20096 pack_expansion_p
= true;
20099 /* Do not actually evaluate the expression. */
20100 ++cp_unevaluated_operand
;
20101 ++c_inhibit_evaluation_warnings
;
20102 /* If it's a `(', then we might be looking at the type-id
20104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
20107 bool saved_in_type_id_in_expr_p
;
20109 /* We can't be sure yet whether we're looking at a type-id or an
20111 cp_parser_parse_tentatively (parser
);
20112 /* Consume the `('. */
20113 cp_lexer_consume_token (parser
->lexer
);
20114 /* Parse the type-id. */
20115 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20116 parser
->in_type_id_in_expr_p
= true;
20117 type
= cp_parser_type_id (parser
);
20118 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20119 /* Now, look for the trailing `)'. */
20120 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20121 /* If all went well, then we're done. */
20122 if (cp_parser_parse_definitely (parser
))
20124 cp_decl_specifier_seq decl_specs
;
20126 /* Build a trivial decl-specifier-seq. */
20127 clear_decl_specs (&decl_specs
);
20128 decl_specs
.type
= type
;
20130 /* Call grokdeclarator to figure out what type this is. */
20131 expr
= grokdeclarator (NULL
,
20135 /*attrlist=*/NULL
);
20139 /* If the type-id production did not work out, then we must be
20140 looking at the unary-expression production. */
20142 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
20143 /*cast_p=*/false, NULL
);
20145 if (pack_expansion_p
)
20146 /* Build a pack expansion. */
20147 expr
= make_pack_expansion (expr
);
20149 /* Go back to evaluating expressions. */
20150 --cp_unevaluated_operand
;
20151 --c_inhibit_evaluation_warnings
;
20153 /* Free the message we created. */
20155 /* And restore the old one. */
20156 parser
->type_definition_forbidden_message
= saved_message
;
20157 parser
->integral_constant_expression_p
20158 = saved_integral_constant_expression_p
;
20159 parser
->non_integral_constant_expression_p
20160 = saved_non_integral_constant_expression_p
;
20165 /* If the current declaration has no declarator, return true. */
20168 cp_parser_declares_only_class_p (cp_parser
*parser
)
20170 /* If the next token is a `;' or a `,' then there is no
20172 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
20173 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
20176 /* Update the DECL_SPECS to reflect the storage class indicated by
20180 cp_parser_set_storage_class (cp_parser
*parser
,
20181 cp_decl_specifier_seq
*decl_specs
,
20183 location_t location
)
20185 cp_storage_class storage_class
;
20187 if (parser
->in_unbraced_linkage_specification_p
)
20189 error_at (location
, "invalid use of %qD in linkage specification",
20190 ridpointers
[keyword
]);
20193 else if (decl_specs
->storage_class
!= sc_none
)
20195 decl_specs
->conflicting_specifiers_p
= true;
20199 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
20200 && decl_specs
->specs
[(int) ds_thread
])
20202 error_at (location
, "%<__thread%> before %qD", ridpointers
[keyword
]);
20203 decl_specs
->specs
[(int) ds_thread
] = 0;
20209 storage_class
= sc_auto
;
20212 storage_class
= sc_register
;
20215 storage_class
= sc_static
;
20218 storage_class
= sc_extern
;
20221 storage_class
= sc_mutable
;
20224 gcc_unreachable ();
20226 decl_specs
->storage_class
= storage_class
;
20228 /* A storage class specifier cannot be applied alongside a typedef
20229 specifier. If there is a typedef specifier present then set
20230 conflicting_specifiers_p which will trigger an error later
20231 on in grokdeclarator. */
20232 if (decl_specs
->specs
[(int)ds_typedef
])
20233 decl_specs
->conflicting_specifiers_p
= true;
20236 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20237 is true, the type is a user-defined type; otherwise it is a
20238 built-in type specified by a keyword. */
20241 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
20243 location_t location
,
20244 bool user_defined_p
)
20246 decl_specs
->any_specifiers_p
= true;
20248 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20249 (with, for example, in "typedef int wchar_t;") we remember that
20250 this is what happened. In system headers, we ignore these
20251 declarations so that G++ can work with system headers that are not
20253 if (decl_specs
->specs
[(int) ds_typedef
]
20255 && (type_spec
== boolean_type_node
20256 || type_spec
== char16_type_node
20257 || type_spec
== char32_type_node
20258 || type_spec
== wchar_type_node
)
20259 && (decl_specs
->type
20260 || decl_specs
->specs
[(int) ds_long
]
20261 || decl_specs
->specs
[(int) ds_short
]
20262 || decl_specs
->specs
[(int) ds_unsigned
]
20263 || decl_specs
->specs
[(int) ds_signed
]))
20265 decl_specs
->redefined_builtin_type
= type_spec
;
20266 if (!decl_specs
->type
)
20268 decl_specs
->type
= type_spec
;
20269 decl_specs
->user_defined_type_p
= false;
20270 decl_specs
->type_location
= location
;
20273 else if (decl_specs
->type
)
20274 decl_specs
->multiple_types_p
= true;
20277 decl_specs
->type
= type_spec
;
20278 decl_specs
->user_defined_type_p
= user_defined_p
;
20279 decl_specs
->redefined_builtin_type
= NULL_TREE
;
20280 decl_specs
->type_location
= location
;
20284 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20285 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20288 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
20290 return decl_specifiers
->specs
[(int) ds_friend
] != 0;
20293 /* Issue an error message indicating that TOKEN_DESC was expected.
20294 If KEYWORD is true, it indicated this function is called by
20295 cp_parser_require_keword and the required token can only be
20296 a indicated keyword. */
20299 cp_parser_required_error (cp_parser
*parser
,
20300 required_token token_desc
,
20303 switch (token_desc
)
20306 cp_parser_error (parser
, "expected %<new%>");
20309 cp_parser_error (parser
, "expected %<delete%>");
20312 cp_parser_error (parser
, "expected %<return%>");
20315 cp_parser_error (parser
, "expected %<while%>");
20318 cp_parser_error (parser
, "expected %<extern%>");
20320 case RT_STATIC_ASSERT
:
20321 cp_parser_error (parser
, "expected %<static_assert%>");
20324 cp_parser_error (parser
, "expected %<decltype%>");
20327 cp_parser_error (parser
, "expected %<operator%>");
20330 cp_parser_error (parser
, "expected %<class%>");
20333 cp_parser_error (parser
, "expected %<template%>");
20336 cp_parser_error (parser
, "expected %<namespace%>");
20339 cp_parser_error (parser
, "expected %<using%>");
20342 cp_parser_error (parser
, "expected %<asm%>");
20345 cp_parser_error (parser
, "expected %<try%>");
20348 cp_parser_error (parser
, "expected %<catch%>");
20351 cp_parser_error (parser
, "expected %<throw%>");
20354 cp_parser_error (parser
, "expected %<__label__%>");
20357 cp_parser_error (parser
, "expected %<@try%>");
20359 case RT_AT_SYNCHRONIZED
:
20360 cp_parser_error (parser
, "expected %<@synchronized%>");
20363 cp_parser_error (parser
, "expected %<@throw%>");
20370 switch (token_desc
)
20373 cp_parser_error (parser
, "expected %<;%>");
20375 case RT_OPEN_PAREN
:
20376 cp_parser_error (parser
, "expected %<(%>");
20378 case RT_CLOSE_BRACE
:
20379 cp_parser_error (parser
, "expected %<}%>");
20381 case RT_OPEN_BRACE
:
20382 cp_parser_error (parser
, "expected %<{%>");
20384 case RT_CLOSE_SQUARE
:
20385 cp_parser_error (parser
, "expected %<]%>");
20387 case RT_OPEN_SQUARE
:
20388 cp_parser_error (parser
, "expected %<[%>");
20391 cp_parser_error (parser
, "expected %<,%>");
20394 cp_parser_error (parser
, "expected %<::%>");
20397 cp_parser_error (parser
, "expected %<<%>");
20400 cp_parser_error (parser
, "expected %<>%>");
20403 cp_parser_error (parser
, "expected %<=%>");
20406 cp_parser_error (parser
, "expected %<...%>");
20409 cp_parser_error (parser
, "expected %<*%>");
20412 cp_parser_error (parser
, "expected %<~%>");
20415 cp_parser_error (parser
, "expected %<:%>");
20417 case RT_COLON_SCOPE
:
20418 cp_parser_error (parser
, "expected %<:%> or %<::%>");
20420 case RT_CLOSE_PAREN
:
20421 cp_parser_error (parser
, "expected %<)%>");
20423 case RT_COMMA_CLOSE_PAREN
:
20424 cp_parser_error (parser
, "expected %<,%> or %<)%>");
20426 case RT_PRAGMA_EOL
:
20427 cp_parser_error (parser
, "expected end of line");
20430 cp_parser_error (parser
, "expected identifier");
20433 cp_parser_error (parser
, "expected selection-statement");
20435 case RT_INTERATION
:
20436 cp_parser_error (parser
, "expected iteration-statement");
20439 cp_parser_error (parser
, "expected jump-statement");
20442 cp_parser_error (parser
, "expected class-key");
20444 case RT_CLASS_TYPENAME_TEMPLATE
:
20445 cp_parser_error (parser
,
20446 "expected %<class%>, %<typename%>, or %<template%>");
20449 gcc_unreachable ();
20453 gcc_unreachable ();
20458 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20459 issue an error message indicating that TOKEN_DESC was expected.
20461 Returns the token consumed, if the token had the appropriate type.
20462 Otherwise, returns NULL. */
20465 cp_parser_require (cp_parser
* parser
,
20466 enum cpp_ttype type
,
20467 required_token token_desc
)
20469 if (cp_lexer_next_token_is (parser
->lexer
, type
))
20470 return cp_lexer_consume_token (parser
->lexer
);
20473 /* Output the MESSAGE -- unless we're parsing tentatively. */
20474 if (!cp_parser_simulate_error (parser
))
20475 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
20480 /* An error message is produced if the next token is not '>'.
20481 All further tokens are skipped until the desired token is
20482 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20485 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
20487 /* Current level of '< ... >'. */
20488 unsigned level
= 0;
20489 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20490 unsigned nesting_depth
= 0;
20492 /* Are we ready, yet? If not, issue error message. */
20493 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
20496 /* Skip tokens until the desired token is found. */
20499 /* Peek at the next token. */
20500 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
20503 if (!nesting_depth
)
20508 if (cxx_dialect
== cxx98
)
20509 /* C++0x views the `>>' operator as two `>' tokens, but
20512 else if (!nesting_depth
&& level
-- == 0)
20514 /* We've hit a `>>' where the first `>' closes the
20515 template argument list, and the second `>' is
20516 spurious. Just consume the `>>' and stop; we've
20517 already produced at least one error. */
20518 cp_lexer_consume_token (parser
->lexer
);
20521 /* Fall through for C++0x, so we handle the second `>' in
20525 if (!nesting_depth
&& level
-- == 0)
20527 /* We've reached the token we want, consume it and stop. */
20528 cp_lexer_consume_token (parser
->lexer
);
20533 case CPP_OPEN_PAREN
:
20534 case CPP_OPEN_SQUARE
:
20538 case CPP_CLOSE_PAREN
:
20539 case CPP_CLOSE_SQUARE
:
20540 if (nesting_depth
-- == 0)
20545 case CPP_PRAGMA_EOL
:
20546 case CPP_SEMICOLON
:
20547 case CPP_OPEN_BRACE
:
20548 case CPP_CLOSE_BRACE
:
20549 /* The '>' was probably forgotten, don't look further. */
20556 /* Consume this token. */
20557 cp_lexer_consume_token (parser
->lexer
);
20561 /* If the next token is the indicated keyword, consume it. Otherwise,
20562 issue an error message indicating that TOKEN_DESC was expected.
20564 Returns the token consumed, if the token had the appropriate type.
20565 Otherwise, returns NULL. */
20568 cp_parser_require_keyword (cp_parser
* parser
,
20570 required_token token_desc
)
20572 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
20574 if (token
&& token
->keyword
!= keyword
)
20576 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
20583 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20584 function-definition. */
20587 cp_parser_token_starts_function_definition_p (cp_token
* token
)
20589 return (/* An ordinary function-body begins with an `{'. */
20590 token
->type
== CPP_OPEN_BRACE
20591 /* A ctor-initializer begins with a `:'. */
20592 || token
->type
== CPP_COLON
20593 /* A function-try-block begins with `try'. */
20594 || token
->keyword
== RID_TRY
20595 /* The named return value extension begins with `return'. */
20596 || token
->keyword
== RID_RETURN
);
20599 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20603 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
20607 token
= cp_lexer_peek_token (parser
->lexer
);
20608 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
20611 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20612 C++0x) ending a template-argument. */
20615 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
20619 token
= cp_lexer_peek_token (parser
->lexer
);
20620 return (token
->type
== CPP_COMMA
20621 || token
->type
== CPP_GREATER
20622 || token
->type
== CPP_ELLIPSIS
20623 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
20626 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20627 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20630 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
20635 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
20636 if (token
->type
== CPP_LESS
)
20638 /* Check for the sequence `<::' in the original code. It would be lexed as
20639 `[:', where `[' is a digraph, and there is no whitespace before
20641 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
20644 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
20645 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
20651 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20652 or none_type otherwise. */
20654 static enum tag_types
20655 cp_parser_token_is_class_key (cp_token
* token
)
20657 switch (token
->keyword
)
20662 return record_type
;
20671 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20674 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
20676 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
20677 permerror (input_location
, "%qs tag used in naming %q#T",
20678 class_key
== union_type
? "union"
20679 : class_key
== record_type
? "struct" : "class",
20683 /* Issue an error message if DECL is redeclared with different
20684 access than its original declaration [class.access.spec/3].
20685 This applies to nested classes and nested class templates.
20689 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
20691 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
20694 if ((TREE_PRIVATE (decl
)
20695 != (current_access_specifier
== access_private_node
))
20696 || (TREE_PROTECTED (decl
)
20697 != (current_access_specifier
== access_protected_node
)))
20698 error_at (location
, "%qD redeclared with different access", decl
);
20701 /* Look for the `template' keyword, as a syntactic disambiguator.
20702 Return TRUE iff it is present, in which case it will be
20706 cp_parser_optional_template_keyword (cp_parser
*parser
)
20708 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20710 /* The `template' keyword can only be used within templates;
20711 outside templates the parser can always figure out what is a
20712 template and what is not. */
20713 if (!processing_template_decl
)
20715 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20716 error_at (token
->location
,
20717 "%<template%> (as a disambiguator) is only allowed "
20718 "within templates");
20719 /* If this part of the token stream is rescanned, the same
20720 error message would be generated. So, we purge the token
20721 from the stream. */
20722 cp_lexer_purge_token (parser
->lexer
);
20727 /* Consume the `template' keyword. */
20728 cp_lexer_consume_token (parser
->lexer
);
20736 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
20737 set PARSER->SCOPE, and perform other related actions. */
20740 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
20743 struct tree_check
*check_value
;
20744 deferred_access_check
*chk
;
20745 VEC (deferred_access_check
,gc
) *checks
;
20747 /* Get the stored value. */
20748 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
20749 /* Perform any access checks that were deferred. */
20750 checks
= check_value
->checks
;
20753 FOR_EACH_VEC_ELT (deferred_access_check
, checks
, i
, chk
)
20754 perform_or_defer_access_check (chk
->binfo
,
20758 /* Set the scope from the stored value. */
20759 parser
->scope
= check_value
->value
;
20760 parser
->qualifying_scope
= check_value
->qualifying_scope
;
20761 parser
->object_scope
= NULL_TREE
;
20764 /* Consume tokens up through a non-nested END token. Returns TRUE if we
20765 encounter the end of a block before what we were looking for. */
20768 cp_parser_cache_group (cp_parser
*parser
,
20769 enum cpp_ttype end
,
20774 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20776 /* Abort a parenthesized expression if we encounter a semicolon. */
20777 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
20778 && token
->type
== CPP_SEMICOLON
)
20780 /* If we've reached the end of the file, stop. */
20781 if (token
->type
== CPP_EOF
20782 || (end
!= CPP_PRAGMA_EOL
20783 && token
->type
== CPP_PRAGMA_EOL
))
20785 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
20786 /* We've hit the end of an enclosing block, so there's been some
20787 kind of syntax error. */
20790 /* Consume the token. */
20791 cp_lexer_consume_token (parser
->lexer
);
20792 /* See if it starts a new group. */
20793 if (token
->type
== CPP_OPEN_BRACE
)
20795 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
20796 /* In theory this should probably check end == '}', but
20797 cp_parser_save_member_function_body needs it to exit
20798 after either '}' or ')' when called with ')'. */
20802 else if (token
->type
== CPP_OPEN_PAREN
)
20804 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
20805 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
20808 else if (token
->type
== CPP_PRAGMA
)
20809 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
20810 else if (token
->type
== end
)
20815 /* Begin parsing tentatively. We always save tokens while parsing
20816 tentatively so that if the tentative parsing fails we can restore the
20820 cp_parser_parse_tentatively (cp_parser
* parser
)
20822 /* Enter a new parsing context. */
20823 parser
->context
= cp_parser_context_new (parser
->context
);
20824 /* Begin saving tokens. */
20825 cp_lexer_save_tokens (parser
->lexer
);
20826 /* In order to avoid repetitive access control error messages,
20827 access checks are queued up until we are no longer parsing
20829 push_deferring_access_checks (dk_deferred
);
20832 /* Commit to the currently active tentative parse. */
20835 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
20837 cp_parser_context
*context
;
20840 /* Mark all of the levels as committed. */
20841 lexer
= parser
->lexer
;
20842 for (context
= parser
->context
; context
->next
; context
= context
->next
)
20844 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
20846 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
20847 while (!cp_lexer_saving_tokens (lexer
))
20848 lexer
= lexer
->next
;
20849 cp_lexer_commit_tokens (lexer
);
20853 /* Abort the currently active tentative parse. All consumed tokens
20854 will be rolled back, and no diagnostics will be issued. */
20857 cp_parser_abort_tentative_parse (cp_parser
* parser
)
20859 cp_parser_simulate_error (parser
);
20860 /* Now, pretend that we want to see if the construct was
20861 successfully parsed. */
20862 cp_parser_parse_definitely (parser
);
20865 /* Stop parsing tentatively. If a parse error has occurred, restore the
20866 token stream. Otherwise, commit to the tokens we have consumed.
20867 Returns true if no error occurred; false otherwise. */
20870 cp_parser_parse_definitely (cp_parser
* parser
)
20872 bool error_occurred
;
20873 cp_parser_context
*context
;
20875 /* Remember whether or not an error occurred, since we are about to
20876 destroy that information. */
20877 error_occurred
= cp_parser_error_occurred (parser
);
20878 /* Remove the topmost context from the stack. */
20879 context
= parser
->context
;
20880 parser
->context
= context
->next
;
20881 /* If no parse errors occurred, commit to the tentative parse. */
20882 if (!error_occurred
)
20884 /* Commit to the tokens read tentatively, unless that was
20886 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
20887 cp_lexer_commit_tokens (parser
->lexer
);
20889 pop_to_parent_deferring_access_checks ();
20891 /* Otherwise, if errors occurred, roll back our state so that things
20892 are just as they were before we began the tentative parse. */
20895 cp_lexer_rollback_tokens (parser
->lexer
);
20896 pop_deferring_access_checks ();
20898 /* Add the context to the front of the free list. */
20899 context
->next
= cp_parser_context_free_list
;
20900 cp_parser_context_free_list
= context
;
20902 return !error_occurred
;
20905 /* Returns true if we are parsing tentatively and are not committed to
20906 this tentative parse. */
20909 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
20911 return (cp_parser_parsing_tentatively (parser
)
20912 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
20915 /* Returns nonzero iff an error has occurred during the most recent
20916 tentative parse. */
20919 cp_parser_error_occurred (cp_parser
* parser
)
20921 return (cp_parser_parsing_tentatively (parser
)
20922 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
20925 /* Returns nonzero if GNU extensions are allowed. */
20928 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
20930 return parser
->allow_gnu_extensions_p
;
20933 /* Objective-C++ Productions */
20936 /* Parse an Objective-C expression, which feeds into a primary-expression
20940 objc-message-expression
20941 objc-string-literal
20942 objc-encode-expression
20943 objc-protocol-expression
20944 objc-selector-expression
20946 Returns a tree representation of the expression. */
20949 cp_parser_objc_expression (cp_parser
* parser
)
20951 /* Try to figure out what kind of declaration is present. */
20952 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
20956 case CPP_OPEN_SQUARE
:
20957 return cp_parser_objc_message_expression (parser
);
20959 case CPP_OBJC_STRING
:
20960 kwd
= cp_lexer_consume_token (parser
->lexer
);
20961 return objc_build_string_object (kwd
->u
.value
);
20964 switch (kwd
->keyword
)
20966 case RID_AT_ENCODE
:
20967 return cp_parser_objc_encode_expression (parser
);
20969 case RID_AT_PROTOCOL
:
20970 return cp_parser_objc_protocol_expression (parser
);
20972 case RID_AT_SELECTOR
:
20973 return cp_parser_objc_selector_expression (parser
);
20979 error_at (kwd
->location
,
20980 "misplaced %<@%D%> Objective-C++ construct",
20982 cp_parser_skip_to_end_of_block_or_statement (parser
);
20985 return error_mark_node
;
20988 /* Parse an Objective-C message expression.
20990 objc-message-expression:
20991 [ objc-message-receiver objc-message-args ]
20993 Returns a representation of an Objective-C message. */
20996 cp_parser_objc_message_expression (cp_parser
* parser
)
20998 tree receiver
, messageargs
;
21000 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
21001 receiver
= cp_parser_objc_message_receiver (parser
);
21002 messageargs
= cp_parser_objc_message_args (parser
);
21003 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21005 return objc_build_message_expr (build_tree_list (receiver
, messageargs
));
21008 /* Parse an objc-message-receiver.
21010 objc-message-receiver:
21012 simple-type-specifier
21014 Returns a representation of the type or expression. */
21017 cp_parser_objc_message_receiver (cp_parser
* parser
)
21021 /* An Objective-C message receiver may be either (1) a type
21022 or (2) an expression. */
21023 cp_parser_parse_tentatively (parser
);
21024 rcv
= cp_parser_expression (parser
, false, NULL
);
21026 if (cp_parser_parse_definitely (parser
))
21029 rcv
= cp_parser_simple_type_specifier (parser
,
21030 /*decl_specs=*/NULL
,
21031 CP_PARSER_FLAGS_NONE
);
21033 return objc_get_class_reference (rcv
);
21036 /* Parse the arguments and selectors comprising an Objective-C message.
21041 objc-selector-args , objc-comma-args
21043 objc-selector-args:
21044 objc-selector [opt] : assignment-expression
21045 objc-selector-args objc-selector [opt] : assignment-expression
21048 assignment-expression
21049 objc-comma-args , assignment-expression
21051 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21052 selector arguments and TREE_VALUE containing a list of comma
21056 cp_parser_objc_message_args (cp_parser
* parser
)
21058 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
21059 bool maybe_unary_selector_p
= true;
21060 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21062 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
21064 tree selector
= NULL_TREE
, arg
;
21066 if (token
->type
!= CPP_COLON
)
21067 selector
= cp_parser_objc_selector (parser
);
21069 /* Detect if we have a unary selector. */
21070 if (maybe_unary_selector_p
21071 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
21072 return build_tree_list (selector
, NULL_TREE
);
21074 maybe_unary_selector_p
= false;
21075 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21076 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
21079 = chainon (sel_args
,
21080 build_tree_list (selector
, arg
));
21082 token
= cp_lexer_peek_token (parser
->lexer
);
21085 /* Handle non-selector arguments, if any. */
21086 while (token
->type
== CPP_COMMA
)
21090 cp_lexer_consume_token (parser
->lexer
);
21091 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
21094 = chainon (addl_args
,
21095 build_tree_list (NULL_TREE
, arg
));
21097 token
= cp_lexer_peek_token (parser
->lexer
);
21100 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
21102 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
21103 return build_tree_list (error_mark_node
, error_mark_node
);
21106 return build_tree_list (sel_args
, addl_args
);
21109 /* Parse an Objective-C encode expression.
21111 objc-encode-expression:
21112 @encode objc-typename
21114 Returns an encoded representation of the type argument. */
21117 cp_parser_objc_encode_expression (cp_parser
* parser
)
21122 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
21123 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21124 token
= cp_lexer_peek_token (parser
->lexer
);
21125 type
= complete_type (cp_parser_type_id (parser
));
21126 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21130 error_at (token
->location
,
21131 "%<@encode%> must specify a type as an argument");
21132 return error_mark_node
;
21135 /* This happens if we find @encode(T) (where T is a template
21136 typename or something dependent on a template typename) when
21137 parsing a template. In that case, we can't compile it
21138 immediately, but we rather create an AT_ENCODE_EXPR which will
21139 need to be instantiated when the template is used.
21141 if (dependent_type_p (type
))
21143 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
21144 TREE_READONLY (value
) = 1;
21148 return objc_build_encode_expr (type
);
21151 /* Parse an Objective-C @defs expression. */
21154 cp_parser_objc_defs_expression (cp_parser
*parser
)
21158 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
21159 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21160 name
= cp_parser_identifier (parser
);
21161 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21163 return objc_get_class_ivars (name
);
21166 /* Parse an Objective-C protocol expression.
21168 objc-protocol-expression:
21169 @protocol ( identifier )
21171 Returns a representation of the protocol expression. */
21174 cp_parser_objc_protocol_expression (cp_parser
* parser
)
21178 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
21179 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21180 proto
= cp_parser_identifier (parser
);
21181 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21183 return objc_build_protocol_expr (proto
);
21186 /* Parse an Objective-C selector expression.
21188 objc-selector-expression:
21189 @selector ( objc-method-signature )
21191 objc-method-signature:
21197 objc-selector-seq objc-selector :
21199 Returns a representation of the method selector. */
21202 cp_parser_objc_selector_expression (cp_parser
* parser
)
21204 tree sel_seq
= NULL_TREE
;
21205 bool maybe_unary_selector_p
= true;
21207 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21209 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
21210 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21211 token
= cp_lexer_peek_token (parser
->lexer
);
21213 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
21214 || token
->type
== CPP_SCOPE
)
21216 tree selector
= NULL_TREE
;
21218 if (token
->type
!= CPP_COLON
21219 || token
->type
== CPP_SCOPE
)
21220 selector
= cp_parser_objc_selector (parser
);
21222 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
21223 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
21225 /* Detect if we have a unary selector. */
21226 if (maybe_unary_selector_p
)
21228 sel_seq
= selector
;
21229 goto finish_selector
;
21233 cp_parser_error (parser
, "expected %<:%>");
21236 maybe_unary_selector_p
= false;
21237 token
= cp_lexer_consume_token (parser
->lexer
);
21239 if (token
->type
== CPP_SCOPE
)
21242 = chainon (sel_seq
,
21243 build_tree_list (selector
, NULL_TREE
));
21245 = chainon (sel_seq
,
21246 build_tree_list (NULL_TREE
, NULL_TREE
));
21250 = chainon (sel_seq
,
21251 build_tree_list (selector
, NULL_TREE
));
21253 token
= cp_lexer_peek_token (parser
->lexer
);
21257 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21259 return objc_build_selector_expr (loc
, sel_seq
);
21262 /* Parse a list of identifiers.
21264 objc-identifier-list:
21266 objc-identifier-list , identifier
21268 Returns a TREE_LIST of identifier nodes. */
21271 cp_parser_objc_identifier_list (cp_parser
* parser
)
21273 tree list
= build_tree_list (NULL_TREE
, cp_parser_identifier (parser
));
21274 cp_token
*sep
= cp_lexer_peek_token (parser
->lexer
);
21276 while (sep
->type
== CPP_COMMA
)
21278 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21279 list
= chainon (list
,
21280 build_tree_list (NULL_TREE
,
21281 cp_parser_identifier (parser
)));
21282 sep
= cp_lexer_peek_token (parser
->lexer
);
21288 /* Parse an Objective-C alias declaration.
21290 objc-alias-declaration:
21291 @compatibility_alias identifier identifier ;
21293 This function registers the alias mapping with the Objective-C front end.
21294 It returns nothing. */
21297 cp_parser_objc_alias_declaration (cp_parser
* parser
)
21301 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
21302 alias
= cp_parser_identifier (parser
);
21303 orig
= cp_parser_identifier (parser
);
21304 objc_declare_alias (alias
, orig
);
21305 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21308 /* Parse an Objective-C class forward-declaration.
21310 objc-class-declaration:
21311 @class objc-identifier-list ;
21313 The function registers the forward declarations with the Objective-C
21314 front end. It returns nothing. */
21317 cp_parser_objc_class_declaration (cp_parser
* parser
)
21319 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
21320 objc_declare_class (cp_parser_objc_identifier_list (parser
));
21321 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21324 /* Parse a list of Objective-C protocol references.
21326 objc-protocol-refs-opt:
21327 objc-protocol-refs [opt]
21329 objc-protocol-refs:
21330 < objc-identifier-list >
21332 Returns a TREE_LIST of identifiers, if any. */
21335 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
21337 tree protorefs
= NULL_TREE
;
21339 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
21341 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
21342 protorefs
= cp_parser_objc_identifier_list (parser
);
21343 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
21349 /* Parse a Objective-C visibility specification. */
21352 cp_parser_objc_visibility_spec (cp_parser
* parser
)
21354 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
21356 switch (vis
->keyword
)
21358 case RID_AT_PRIVATE
:
21359 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
21361 case RID_AT_PROTECTED
:
21362 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
21364 case RID_AT_PUBLIC
:
21365 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
21367 case RID_AT_PACKAGE
:
21368 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
21374 /* Eat '@private'/'@protected'/'@public'. */
21375 cp_lexer_consume_token (parser
->lexer
);
21378 /* Parse an Objective-C method type. */
21381 cp_parser_objc_method_type (cp_parser
* parser
)
21383 objc_set_method_type
21384 (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
21389 /* Parse an Objective-C protocol qualifier. */
21392 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
21394 tree quals
= NULL_TREE
, node
;
21395 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21397 node
= token
->u
.value
;
21399 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
21400 && (node
== ridpointers
[(int) RID_IN
]
21401 || node
== ridpointers
[(int) RID_OUT
]
21402 || node
== ridpointers
[(int) RID_INOUT
]
21403 || node
== ridpointers
[(int) RID_BYCOPY
]
21404 || node
== ridpointers
[(int) RID_BYREF
]
21405 || node
== ridpointers
[(int) RID_ONEWAY
]))
21407 quals
= tree_cons (NULL_TREE
, node
, quals
);
21408 cp_lexer_consume_token (parser
->lexer
);
21409 token
= cp_lexer_peek_token (parser
->lexer
);
21410 node
= token
->u
.value
;
21416 /* Parse an Objective-C typename. */
21419 cp_parser_objc_typename (cp_parser
* parser
)
21421 tree type_name
= NULL_TREE
;
21423 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21425 tree proto_quals
, cp_type
= NULL_TREE
;
21427 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
21428 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
21430 /* An ObjC type name may consist of just protocol qualifiers, in which
21431 case the type shall default to 'id'. */
21432 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
21433 cp_type
= cp_parser_type_id (parser
);
21435 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21436 type_name
= build_tree_list (proto_quals
, cp_type
);
21442 /* Check to see if TYPE refers to an Objective-C selector name. */
21445 cp_parser_objc_selector_p (enum cpp_ttype type
)
21447 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
21448 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
21449 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
21450 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
21451 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
21454 /* Parse an Objective-C selector. */
21457 cp_parser_objc_selector (cp_parser
* parser
)
21459 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
21461 if (!cp_parser_objc_selector_p (token
->type
))
21463 error_at (token
->location
, "invalid Objective-C++ selector name");
21464 return error_mark_node
;
21467 /* C++ operator names are allowed to appear in ObjC selectors. */
21468 switch (token
->type
)
21470 case CPP_AND_AND
: return get_identifier ("and");
21471 case CPP_AND_EQ
: return get_identifier ("and_eq");
21472 case CPP_AND
: return get_identifier ("bitand");
21473 case CPP_OR
: return get_identifier ("bitor");
21474 case CPP_COMPL
: return get_identifier ("compl");
21475 case CPP_NOT
: return get_identifier ("not");
21476 case CPP_NOT_EQ
: return get_identifier ("not_eq");
21477 case CPP_OR_OR
: return get_identifier ("or");
21478 case CPP_OR_EQ
: return get_identifier ("or_eq");
21479 case CPP_XOR
: return get_identifier ("xor");
21480 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
21481 default: return token
->u
.value
;
21485 /* Parse an Objective-C params list. */
21488 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
21490 tree params
= NULL_TREE
;
21491 bool maybe_unary_selector_p
= true;
21492 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21494 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
21496 tree selector
= NULL_TREE
, type_name
, identifier
;
21497 tree parm_attr
= NULL_TREE
;
21499 if (token
->keyword
== RID_ATTRIBUTE
)
21502 if (token
->type
!= CPP_COLON
)
21503 selector
= cp_parser_objc_selector (parser
);
21505 /* Detect if we have a unary selector. */
21506 if (maybe_unary_selector_p
21507 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
21509 params
= selector
; /* Might be followed by attributes. */
21513 maybe_unary_selector_p
= false;
21514 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
21516 /* Something went quite wrong. There should be a colon
21517 here, but there is not. Stop parsing parameters. */
21520 type_name
= cp_parser_objc_typename (parser
);
21521 /* New ObjC allows attributes on parameters too. */
21522 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
21523 parm_attr
= cp_parser_attributes_opt (parser
);
21524 identifier
= cp_parser_identifier (parser
);
21528 objc_build_keyword_decl (selector
,
21533 token
= cp_lexer_peek_token (parser
->lexer
);
21536 if (params
== NULL_TREE
)
21538 cp_parser_error (parser
, "objective-c++ method declaration is expected");
21539 return error_mark_node
;
21542 /* We allow tail attributes for the method. */
21543 if (token
->keyword
== RID_ATTRIBUTE
)
21545 *attributes
= cp_parser_attributes_opt (parser
);
21546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21547 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21549 cp_parser_error (parser
,
21550 "method attributes must be specified at the end");
21551 return error_mark_node
;
21554 if (params
== NULL_TREE
)
21556 cp_parser_error (parser
, "objective-c++ method declaration is expected");
21557 return error_mark_node
;
21562 /* Parse the non-keyword Objective-C params. */
21565 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
21568 tree params
= make_node (TREE_LIST
);
21569 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21570 *ellipsisp
= false; /* Initially, assume no ellipsis. */
21572 while (token
->type
== CPP_COMMA
)
21574 cp_parameter_declarator
*parmdecl
;
21577 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21578 token
= cp_lexer_peek_token (parser
->lexer
);
21580 if (token
->type
== CPP_ELLIPSIS
)
21582 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
21584 token
= cp_lexer_peek_token (parser
->lexer
);
21588 /* TODO: parse attributes for tail parameters. */
21589 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
21590 parm
= grokdeclarator (parmdecl
->declarator
,
21591 &parmdecl
->decl_specifiers
,
21592 PARM
, /*initialized=*/0,
21593 /*attrlist=*/NULL
);
21595 chainon (params
, build_tree_list (NULL_TREE
, parm
));
21596 token
= cp_lexer_peek_token (parser
->lexer
);
21599 /* We allow tail attributes for the method. */
21600 if (token
->keyword
== RID_ATTRIBUTE
)
21602 if (*attributes
== NULL_TREE
)
21604 *attributes
= cp_parser_attributes_opt (parser
);
21605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21606 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21610 /* We have an error, but parse the attributes, so that we can
21612 *attributes
= cp_parser_attributes_opt (parser
);
21614 cp_parser_error (parser
,
21615 "method attributes must be specified at the end");
21616 return error_mark_node
;
21622 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21625 cp_parser_objc_interstitial_code (cp_parser
* parser
)
21627 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21629 /* If the next token is `extern' and the following token is a string
21630 literal, then we have a linkage specification. */
21631 if (token
->keyword
== RID_EXTERN
21632 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
21633 cp_parser_linkage_specification (parser
);
21634 /* Handle #pragma, if any. */
21635 else if (token
->type
== CPP_PRAGMA
)
21636 cp_parser_pragma (parser
, pragma_external
);
21637 /* Allow stray semicolons. */
21638 else if (token
->type
== CPP_SEMICOLON
)
21639 cp_lexer_consume_token (parser
->lexer
);
21640 /* Mark methods as optional or required, when building protocols. */
21641 else if (token
->keyword
== RID_AT_OPTIONAL
)
21643 cp_lexer_consume_token (parser
->lexer
);
21644 objc_set_method_opt (true);
21646 else if (token
->keyword
== RID_AT_REQUIRED
)
21648 cp_lexer_consume_token (parser
->lexer
);
21649 objc_set_method_opt (false);
21651 else if (token
->keyword
== RID_NAMESPACE
)
21652 cp_parser_namespace_definition (parser
);
21653 /* Other stray characters must generate errors. */
21654 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
21656 cp_lexer_consume_token (parser
->lexer
);
21657 error ("stray `%s' between Objective-C++ methods",
21658 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
21660 /* Finally, try to parse a block-declaration, or a function-definition. */
21662 cp_parser_block_declaration (parser
, /*statement_p=*/false);
21665 /* Parse a method signature. */
21668 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
21670 tree rettype
, kwdparms
, optparms
;
21671 bool ellipsis
= false;
21673 cp_parser_objc_method_type (parser
);
21674 rettype
= cp_parser_objc_typename (parser
);
21675 *attributes
= NULL_TREE
;
21676 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
21677 if (kwdparms
== error_mark_node
)
21678 return error_mark_node
;
21679 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
21680 if (optparms
== error_mark_node
)
21681 return error_mark_node
;
21683 return objc_build_method_signature (rettype
, kwdparms
, optparms
, ellipsis
);
21687 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
21690 cp_lexer_save_tokens (parser
->lexer
);
21691 tattr
= cp_parser_attributes_opt (parser
);
21692 gcc_assert (tattr
) ;
21694 /* If the attributes are followed by a method introducer, this is not allowed.
21695 Dump the attributes and flag the situation. */
21696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
21697 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
21700 /* Otherwise, the attributes introduce some interstitial code, possibly so
21701 rewind to allow that check. */
21702 cp_lexer_rollback_tokens (parser
->lexer
);
21706 /* Parse an Objective-C method prototype list. */
21709 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
21711 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21713 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21715 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
21717 tree attributes
, sig
;
21718 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
21719 if (sig
== error_mark_node
)
21721 cp_parser_skip_to_end_of_block_or_statement (parser
);
21722 token
= cp_lexer_peek_token (parser
->lexer
);
21725 objc_add_method_declaration (sig
, attributes
);
21726 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21728 else if (token
->keyword
== RID_AT_PROPERTY
)
21729 cp_parser_objc_at_property (parser
);
21730 else if (token
->keyword
== RID_ATTRIBUTE
21731 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
21732 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
21734 "prefix attributes are ignored for methods");
21736 /* Allow for interspersed non-ObjC++ code. */
21737 cp_parser_objc_interstitial_code (parser
);
21739 token
= cp_lexer_peek_token (parser
->lexer
);
21742 if (token
->type
!= CPP_EOF
)
21743 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
21745 cp_parser_error (parser
, "expected %<@end%>");
21747 objc_finish_interface ();
21750 /* Parse an Objective-C method definition list. */
21753 cp_parser_objc_method_definition_list (cp_parser
* parser
)
21755 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21757 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21761 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
21764 tree sig
, attribute
;
21765 push_deferring_access_checks (dk_deferred
);
21766 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
21767 if (sig
== error_mark_node
)
21769 cp_parser_skip_to_end_of_block_or_statement (parser
);
21770 token
= cp_lexer_peek_token (parser
->lexer
);
21773 objc_start_method_definition (sig
, attribute
);
21775 /* For historical reasons, we accept an optional semicolon. */
21776 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21777 cp_lexer_consume_token (parser
->lexer
);
21779 ptk
= cp_lexer_peek_token (parser
->lexer
);
21780 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
21781 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
21783 perform_deferred_access_checks ();
21784 stop_deferring_access_checks ();
21785 meth
= cp_parser_function_definition_after_declarator (parser
,
21787 pop_deferring_access_checks ();
21788 objc_finish_method_definition (meth
);
21791 else if (token
->keyword
== RID_AT_PROPERTY
)
21792 cp_parser_objc_at_property (parser
);
21793 else if (token
->keyword
== RID_ATTRIBUTE
21794 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
21795 warning_at (token
->location
, OPT_Wattributes
,
21796 "prefix attributes are ignored for methods");
21798 /* Allow for interspersed non-ObjC++ code. */
21799 cp_parser_objc_interstitial_code (parser
);
21801 token
= cp_lexer_peek_token (parser
->lexer
);
21804 if (token
->type
!= CPP_EOF
)
21805 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
21807 cp_parser_error (parser
, "expected %<@end%>");
21809 objc_finish_implementation ();
21812 /* Parse Objective-C ivars. */
21815 cp_parser_objc_class_ivars (cp_parser
* parser
)
21817 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21819 if (token
->type
!= CPP_OPEN_BRACE
)
21820 return; /* No ivars specified. */
21822 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
21823 token
= cp_lexer_peek_token (parser
->lexer
);
21825 while (token
->type
!= CPP_CLOSE_BRACE
21826 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21828 cp_decl_specifier_seq declspecs
;
21829 int decl_class_or_enum_p
;
21830 tree prefix_attributes
;
21832 cp_parser_objc_visibility_spec (parser
);
21834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
21837 cp_parser_decl_specifier_seq (parser
,
21838 CP_PARSER_FLAGS_OPTIONAL
,
21840 &decl_class_or_enum_p
);
21841 prefix_attributes
= declspecs
.attributes
;
21842 declspecs
.attributes
= NULL_TREE
;
21844 /* Keep going until we hit the `;' at the end of the
21846 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21848 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
21849 cp_declarator
*declarator
= NULL
;
21850 int ctor_dtor_or_conv_p
;
21852 /* Check for a (possibly unnamed) bitfield declaration. */
21853 token
= cp_lexer_peek_token (parser
->lexer
);
21854 if (token
->type
== CPP_COLON
)
21857 if (token
->type
== CPP_NAME
21858 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21861 /* Get the name of the bitfield. */
21862 declarator
= make_id_declarator (NULL_TREE
,
21863 cp_parser_identifier (parser
),
21867 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
21868 /* Get the width of the bitfield. */
21870 = cp_parser_constant_expression (parser
,
21871 /*allow_non_constant=*/false,
21876 /* Parse the declarator. */
21878 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
21879 &ctor_dtor_or_conv_p
,
21880 /*parenthesized_p=*/NULL
,
21881 /*member_p=*/false);
21884 /* Look for attributes that apply to the ivar. */
21885 attributes
= cp_parser_attributes_opt (parser
);
21886 /* Remember which attributes are prefix attributes and
21888 first_attribute
= attributes
;
21889 /* Combine the attributes. */
21890 attributes
= chainon (prefix_attributes
, attributes
);
21893 /* Create the bitfield declaration. */
21894 decl
= grokbitfield (declarator
, &declspecs
,
21898 decl
= grokfield (declarator
, &declspecs
,
21899 NULL_TREE
, /*init_const_expr_p=*/false,
21900 NULL_TREE
, attributes
);
21902 /* Add the instance variable. */
21903 objc_add_instance_variable (decl
);
21905 /* Reset PREFIX_ATTRIBUTES. */
21906 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
21907 attributes
= TREE_CHAIN (attributes
);
21909 TREE_CHAIN (attributes
) = NULL_TREE
;
21911 token
= cp_lexer_peek_token (parser
->lexer
);
21913 if (token
->type
== CPP_COMMA
)
21915 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
21921 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21922 token
= cp_lexer_peek_token (parser
->lexer
);
21925 if (token
->keyword
== RID_AT_END
)
21926 cp_parser_error (parser
, "expected %<}%>");
21928 /* Do not consume the RID_AT_END, so it will be read again as terminating
21929 the @interface of @implementation. */
21930 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
21931 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
21933 /* For historical reasons, we accept an optional semicolon. */
21934 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21935 cp_lexer_consume_token (parser
->lexer
);
21938 /* Parse an Objective-C protocol declaration. */
21941 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
21943 tree proto
, protorefs
;
21946 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
21947 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
21949 tok
= cp_lexer_peek_token (parser
->lexer
);
21950 error_at (tok
->location
, "identifier expected after %<@protocol%>");
21954 /* See if we have a forward declaration or a definition. */
21955 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21957 /* Try a forward declaration first. */
21958 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
21960 objc_declare_protocols (cp_parser_objc_identifier_list (parser
));
21962 cp_parser_consume_semicolon_at_end_of_statement (parser
);
21965 /* Ok, we got a full-fledged definition (or at least should). */
21968 proto
= cp_parser_identifier (parser
);
21969 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
21970 objc_start_protocol (proto
, protorefs
, attributes
);
21971 cp_parser_objc_method_prototype_list (parser
);
21975 /* Parse an Objective-C superclass or category. */
21978 cp_parser_objc_superclass_or_category (cp_parser
*parser
, tree
*super
,
21981 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
21983 *super
= *categ
= NULL_TREE
;
21984 if (next
->type
== CPP_COLON
)
21986 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
21987 *super
= cp_parser_identifier (parser
);
21989 else if (next
->type
== CPP_OPEN_PAREN
)
21991 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
21992 *categ
= cp_parser_identifier (parser
);
21993 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21997 /* Parse an Objective-C class interface. */
22000 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
22002 tree name
, super
, categ
, protos
;
22004 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
22005 name
= cp_parser_identifier (parser
);
22006 if (name
== error_mark_node
)
22008 /* It's hard to recover because even if valid @interface stuff
22009 is to follow, we can't compile it (or validate it) if we
22010 don't even know which class it refers to. Let's assume this
22011 was a stray '@interface' token in the stream and skip it.
22015 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
22016 protos
= cp_parser_objc_protocol_refs_opt (parser
);
22018 /* We have either a class or a category on our hands. */
22020 objc_start_category_interface (name
, categ
, protos
, attributes
);
22023 objc_start_class_interface (name
, super
, protos
, attributes
);
22024 /* Handle instance variable declarations, if any. */
22025 cp_parser_objc_class_ivars (parser
);
22026 objc_continue_interface ();
22029 cp_parser_objc_method_prototype_list (parser
);
22032 /* Parse an Objective-C class implementation. */
22035 cp_parser_objc_class_implementation (cp_parser
* parser
)
22037 tree name
, super
, categ
;
22039 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
22040 name
= cp_parser_identifier (parser
);
22041 if (name
== error_mark_node
)
22043 /* It's hard to recover because even if valid @implementation
22044 stuff is to follow, we can't compile it (or validate it) if
22045 we don't even know which class it refers to. Let's assume
22046 this was a stray '@implementation' token in the stream and
22051 cp_parser_objc_superclass_or_category (parser
, &super
, &categ
);
22053 /* We have either a class or a category on our hands. */
22055 objc_start_category_implementation (name
, categ
);
22058 objc_start_class_implementation (name
, super
);
22059 /* Handle instance variable declarations, if any. */
22060 cp_parser_objc_class_ivars (parser
);
22061 objc_continue_implementation ();
22064 cp_parser_objc_method_definition_list (parser
);
22067 /* Consume the @end token and finish off the implementation. */
22070 cp_parser_objc_end_implementation (cp_parser
* parser
)
22072 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
22073 objc_finish_implementation ();
22076 /* Parse an Objective-C declaration. */
22079 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
22081 /* Try to figure out what kind of declaration is present. */
22082 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
22085 switch (kwd
->keyword
)
22090 error_at (kwd
->location
, "attributes may not be specified before"
22091 " the %<@%D%> Objective-C++ keyword",
22095 case RID_AT_IMPLEMENTATION
:
22096 warning_at (kwd
->location
, OPT_Wattributes
,
22097 "prefix attributes are ignored before %<@%D%>",
22104 switch (kwd
->keyword
)
22107 cp_parser_objc_alias_declaration (parser
);
22110 cp_parser_objc_class_declaration (parser
);
22112 case RID_AT_PROTOCOL
:
22113 cp_parser_objc_protocol_declaration (parser
, attributes
);
22115 case RID_AT_INTERFACE
:
22116 cp_parser_objc_class_interface (parser
, attributes
);
22118 case RID_AT_IMPLEMENTATION
:
22119 cp_parser_objc_class_implementation (parser
);
22122 cp_parser_objc_end_implementation (parser
);
22125 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
22127 cp_parser_skip_to_end_of_block_or_statement (parser
);
22131 /* Parse an Objective-C try-catch-finally statement.
22133 objc-try-catch-finally-stmt:
22134 @try compound-statement objc-catch-clause-seq [opt]
22135 objc-finally-clause [opt]
22137 objc-catch-clause-seq:
22138 objc-catch-clause objc-catch-clause-seq [opt]
22141 @catch ( exception-declaration ) compound-statement
22143 objc-finally-clause
22144 @finally compound-statement
22146 Returns NULL_TREE. */
22149 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
) {
22150 location_t location
;
22153 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
22154 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22155 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22156 node, lest it get absorbed into the surrounding block. */
22157 stmt
= push_stmt_list ();
22158 cp_parser_compound_statement (parser
, NULL
, false);
22159 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
22161 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
22163 cp_parameter_declarator
*parmdecl
;
22166 cp_lexer_consume_token (parser
->lexer
);
22167 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22168 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
22169 parm
= grokdeclarator (parmdecl
->declarator
,
22170 &parmdecl
->decl_specifiers
,
22171 PARM
, /*initialized=*/0,
22172 /*attrlist=*/NULL
);
22173 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22174 objc_begin_catch_clause (parm
);
22175 cp_parser_compound_statement (parser
, NULL
, false);
22176 objc_finish_catch_clause ();
22179 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
22181 cp_lexer_consume_token (parser
->lexer
);
22182 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22183 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22184 node, lest it get absorbed into the surrounding block. */
22185 stmt
= push_stmt_list ();
22186 cp_parser_compound_statement (parser
, NULL
, false);
22187 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
22190 return objc_finish_try_stmt ();
22193 /* Parse an Objective-C synchronized statement.
22195 objc-synchronized-stmt:
22196 @synchronized ( expression ) compound-statement
22198 Returns NULL_TREE. */
22201 cp_parser_objc_synchronized_statement (cp_parser
*parser
) {
22202 location_t location
;
22205 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
22207 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
22208 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22209 lock
= cp_parser_expression (parser
, false, NULL
);
22210 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22212 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22213 node, lest it get absorbed into the surrounding block. */
22214 stmt
= push_stmt_list ();
22215 cp_parser_compound_statement (parser
, NULL
, false);
22217 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
22220 /* Parse an Objective-C throw statement.
22223 @throw assignment-expression [opt] ;
22225 Returns a constructed '@throw' statement. */
22228 cp_parser_objc_throw_statement (cp_parser
*parser
) {
22229 tree expr
= NULL_TREE
;
22230 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22232 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
22234 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22235 expr
= cp_parser_assignment_expression (parser
, false, NULL
);
22237 cp_parser_consume_semicolon_at_end_of_statement (parser
);
22239 return objc_build_throw_stmt (loc
, expr
);
22242 /* Parse an Objective-C statement. */
22245 cp_parser_objc_statement (cp_parser
* parser
) {
22246 /* Try to figure out what kind of declaration is present. */
22247 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
22249 switch (kwd
->keyword
)
22252 return cp_parser_objc_try_catch_finally_statement (parser
);
22253 case RID_AT_SYNCHRONIZED
:
22254 return cp_parser_objc_synchronized_statement (parser
);
22256 return cp_parser_objc_throw_statement (parser
);
22258 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
22260 cp_parser_skip_to_end_of_block_or_statement (parser
);
22263 return error_mark_node
;
22266 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22267 look ahead to see if an objc keyword follows the attributes. This
22268 is to detect the use of prefix attributes on ObjC @interface and
22272 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
22274 cp_lexer_save_tokens (parser
->lexer
);
22275 *attrib
= cp_parser_attributes_opt (parser
);
22276 gcc_assert (*attrib
);
22277 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
22279 cp_lexer_commit_tokens (parser
->lexer
);
22282 cp_lexer_rollback_tokens (parser
->lexer
);
22286 /* This routine parses the propery declarations. */
22289 cp_parser_objc_property_decl (cp_parser
*parser
)
22291 int declares_class_or_enum
;
22292 cp_decl_specifier_seq declspecs
;
22294 cp_parser_decl_specifier_seq (parser
,
22295 CP_PARSER_FLAGS_NONE
,
22297 &declares_class_or_enum
);
22298 /* Keep going until we hit the `;' at the end of the declaration. */
22299 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22303 cp_declarator
*declarator
22304 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22305 NULL
, NULL
, false);
22306 property
= grokdeclarator (declarator
, &declspecs
, NORMAL
,0, NULL
);
22307 /* Recover from any kind of error in property declaration. */
22308 if (property
== error_mark_node
|| property
== NULL_TREE
)
22311 /* Add to property list. */
22312 objc_add_property_variable (copy_node (property
));
22313 token
= cp_lexer_peek_token (parser
->lexer
);
22314 if (token
->type
== CPP_COMMA
)
22316 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
22319 else if (token
->type
== CPP_EOF
)
22322 /* Eat ';' if present, or issue an error. */
22323 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22326 /* ObjC @property. */
22327 /* Parse a comma-separated list of property attributes.
22328 The lexer does not recognize */
22331 cp_parser_objc_property_attrlist (cp_parser
*parser
)
22334 /* Initialize to an empty list. */
22335 objc_set_property_attr (cp_lexer_peek_token (parser
->lexer
)->location
,
22336 OBJC_PATTR_INIT
, NULL_TREE
);
22338 /* The list is optional. */
22339 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22343 cp_lexer_consume_token (parser
->lexer
);
22345 token
= cp_lexer_peek_token (parser
->lexer
);
22346 while (token
->type
!= CPP_CLOSE_PAREN
&& token
->type
!= CPP_EOF
)
22348 location_t loc
= token
->location
;
22349 tree node
= cp_parser_identifier (parser
);
22350 if (node
== ridpointers
[(int) RID_READONLY
])
22351 objc_set_property_attr (loc
, OBJC_PATTR_READONLY
, NULL_TREE
);
22352 else if (node
== ridpointers
[(int) RID_GETTER
]
22353 || node
== ridpointers
[(int) RID_SETTER
]
22354 || node
== ridpointers
[(int) RID_IVAR
])
22356 /* Do the getter/setter/ivar attribute. */
22357 token
= cp_lexer_consume_token (parser
->lexer
);
22358 if (token
->type
== CPP_EQ
)
22360 tree attr_ident
= cp_parser_identifier (parser
);
22361 objc_property_attribute_kind pkind
;
22362 if (node
== ridpointers
[(int) RID_GETTER
])
22363 pkind
= OBJC_PATTR_GETTER
;
22364 else if (node
== ridpointers
[(int) RID_SETTER
])
22366 pkind
= OBJC_PATTR_SETTER
;
22367 /* Consume the ':' which must always follow the setter name. */
22368 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22369 cp_lexer_consume_token (parser
->lexer
);
22372 error_at (token
->location
,
22373 "setter name must be followed by %<:%>");
22378 pkind
= OBJC_PATTR_IVAR
;
22379 objc_set_property_attr (loc
, pkind
, attr_ident
);
22383 error_at (token
->location
,
22384 "getter/setter/ivar attribute must be followed by %<=%>");
22388 else if (node
== ridpointers
[(int) RID_COPIES
])
22389 objc_set_property_attr (loc
, OBJC_PATTR_COPIES
, NULL_TREE
);
22392 error_at (token
->location
,"unknown property attribute");
22395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22396 cp_lexer_consume_token (parser
->lexer
);
22397 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
22398 warning_at (token
->location
, 0,
22399 "property attributes should be separated by a %<,%>");
22400 token
= cp_lexer_peek_token (parser
->lexer
);
22403 if (token
->type
!= CPP_CLOSE_PAREN
)
22404 error_at (token
->location
,
22405 "syntax error in @property's attribute declaration");
22408 cp_lexer_consume_token (parser
->lexer
);
22411 /* This function parses a @property declaration inside an objective class
22412 or its implementation. */
22415 cp_parser_objc_at_property (cp_parser
*parser
)
22417 /* Consume @property */
22418 cp_lexer_consume_token (parser
->lexer
);
22420 /* Parse optional attributes list... */
22421 cp_parser_objc_property_attrlist (parser
);
22422 /* ... and the property declaration(s). */
22423 cp_parser_objc_property_decl (parser
);
22426 /* OpenMP 2.5 parsing routines. */
22428 /* Returns name of the next clause.
22429 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
22430 the token is not consumed. Otherwise appropriate pragma_omp_clause is
22431 returned and the token is consumed. */
22433 static pragma_omp_clause
22434 cp_parser_omp_clause_name (cp_parser
*parser
)
22436 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
22438 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
22439 result
= PRAGMA_OMP_CLAUSE_IF
;
22440 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
22441 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
22442 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
22443 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
22444 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22446 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22447 const char *p
= IDENTIFIER_POINTER (id
);
22452 if (!strcmp ("collapse", p
))
22453 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
22454 else if (!strcmp ("copyin", p
))
22455 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
22456 else if (!strcmp ("copyprivate", p
))
22457 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
22460 if (!strcmp ("firstprivate", p
))
22461 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
22464 if (!strcmp ("lastprivate", p
))
22465 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
22468 if (!strcmp ("nowait", p
))
22469 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
22470 else if (!strcmp ("num_threads", p
))
22471 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
22474 if (!strcmp ("ordered", p
))
22475 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
22478 if (!strcmp ("reduction", p
))
22479 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
22482 if (!strcmp ("schedule", p
))
22483 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
22484 else if (!strcmp ("shared", p
))
22485 result
= PRAGMA_OMP_CLAUSE_SHARED
;
22488 if (!strcmp ("untied", p
))
22489 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
22494 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
22495 cp_lexer_consume_token (parser
->lexer
);
22500 /* Validate that a clause of the given type does not already exist. */
22503 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
22504 const char *name
, location_t location
)
22508 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
22509 if (OMP_CLAUSE_CODE (c
) == code
)
22511 error_at (location
, "too many %qs clauses", name
);
22519 variable-list , identifier
22521 In addition, we match a closing parenthesis. An opening parenthesis
22522 will have been consumed by the caller.
22524 If KIND is nonzero, create the appropriate node and install the decl
22525 in OMP_CLAUSE_DECL and add the node to the head of the list.
22527 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
22528 return the list created. */
22531 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
22539 token
= cp_lexer_peek_token (parser
->lexer
);
22540 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
22541 /*check_dependency_p=*/true,
22542 /*template_p=*/NULL
,
22543 /*declarator_p=*/false,
22544 /*optional_p=*/false);
22545 if (name
== error_mark_node
)
22548 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
22549 if (decl
== error_mark_node
)
22550 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
22552 else if (kind
!= 0)
22554 tree u
= build_omp_clause (token
->location
, kind
);
22555 OMP_CLAUSE_DECL (u
) = decl
;
22556 OMP_CLAUSE_CHAIN (u
) = list
;
22560 list
= tree_cons (decl
, NULL_TREE
, list
);
22563 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22565 cp_lexer_consume_token (parser
->lexer
);
22568 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22572 /* Try to resync to an unnested comma. Copied from
22573 cp_parser_parenthesized_expression_list. */
22575 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
22576 /*recovering=*/true,
22578 /*consume_paren=*/true);
22586 /* Similarly, but expect leading and trailing parenthesis. This is a very
22587 common case for omp clauses. */
22590 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
22592 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22593 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
22598 collapse ( constant-expression ) */
22601 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
22607 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22608 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22611 num
= cp_parser_constant_expression (parser
, false, NULL
);
22613 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22614 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22615 /*or_comma=*/false,
22616 /*consume_paren=*/true);
22618 if (num
== error_mark_node
)
22620 num
= fold_non_dependent_expr (num
);
22621 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
22622 || !host_integerp (num
, 0)
22623 || (n
= tree_low_cst (num
, 0)) <= 0
22626 error_at (loc
, "collapse argument needs positive constant integer expression");
22630 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
22631 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
22632 OMP_CLAUSE_CHAIN (c
) = list
;
22633 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
22639 default ( shared | none ) */
22642 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
22644 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
22647 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22651 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22652 const char *p
= IDENTIFIER_POINTER (id
);
22657 if (strcmp ("none", p
) != 0)
22659 kind
= OMP_CLAUSE_DEFAULT_NONE
;
22663 if (strcmp ("shared", p
) != 0)
22665 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
22672 cp_lexer_consume_token (parser
->lexer
);
22677 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
22680 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22681 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22682 /*or_comma=*/false,
22683 /*consume_paren=*/true);
22685 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
22688 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
22689 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
22690 OMP_CLAUSE_CHAIN (c
) = list
;
22691 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
22697 if ( expression ) */
22700 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
22704 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22707 t
= cp_parser_condition (parser
);
22709 if (t
== error_mark_node
22710 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22711 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22712 /*or_comma=*/false,
22713 /*consume_paren=*/true);
22715 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
22717 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
22718 OMP_CLAUSE_IF_EXPR (c
) = t
;
22719 OMP_CLAUSE_CHAIN (c
) = list
;
22728 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
,
22729 tree list
, location_t location
)
22733 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
22735 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
22736 OMP_CLAUSE_CHAIN (c
) = list
;
22741 num_threads ( expression ) */
22744 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
22745 location_t location
)
22749 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22752 t
= cp_parser_expression (parser
, false, NULL
);
22754 if (t
== error_mark_node
22755 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22756 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22757 /*or_comma=*/false,
22758 /*consume_paren=*/true);
22760 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
22761 "num_threads", location
);
22763 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
22764 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
22765 OMP_CLAUSE_CHAIN (c
) = list
;
22774 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
,
22775 tree list
, location_t location
)
22779 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
22780 "ordered", location
);
22782 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
22783 OMP_CLAUSE_CHAIN (c
) = list
;
22788 reduction ( reduction-operator : variable-list )
22790 reduction-operator:
22791 One of: + * - & ^ | && || */
22794 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
22796 enum tree_code code
;
22799 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22802 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
22814 code
= BIT_AND_EXPR
;
22817 code
= BIT_XOR_EXPR
;
22820 code
= BIT_IOR_EXPR
;
22823 code
= TRUTH_ANDIF_EXPR
;
22826 code
= TRUTH_ORIF_EXPR
;
22829 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
22830 "%<|%>, %<&&%>, or %<||%>");
22832 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22833 /*or_comma=*/false,
22834 /*consume_paren=*/true);
22837 cp_lexer_consume_token (parser
->lexer
);
22839 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
22842 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
22843 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
22844 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
22850 schedule ( schedule-kind )
22851 schedule ( schedule-kind , expression )
22854 static | dynamic | guided | runtime | auto */
22857 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
22861 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
22864 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
22866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22868 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
22869 const char *p
= IDENTIFIER_POINTER (id
);
22874 if (strcmp ("dynamic", p
) != 0)
22876 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
22880 if (strcmp ("guided", p
) != 0)
22882 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
22886 if (strcmp ("runtime", p
) != 0)
22888 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
22895 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
22896 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
22897 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
22898 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
22901 cp_lexer_consume_token (parser
->lexer
);
22903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22906 cp_lexer_consume_token (parser
->lexer
);
22908 token
= cp_lexer_peek_token (parser
->lexer
);
22909 t
= cp_parser_assignment_expression (parser
, false, NULL
);
22911 if (t
== error_mark_node
)
22913 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
22914 error_at (token
->location
, "schedule %<runtime%> does not take "
22915 "a %<chunk_size%> parameter");
22916 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
22917 error_at (token
->location
, "schedule %<auto%> does not take "
22918 "a %<chunk_size%> parameter");
22920 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
22922 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22925 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
22928 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
22929 OMP_CLAUSE_CHAIN (c
) = list
;
22933 cp_parser_error (parser
, "invalid schedule kind");
22935 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
22936 /*or_comma=*/false,
22937 /*consume_paren=*/true);
22945 cp_parser_omp_clause_untied (cp_parser
*parser ATTRIBUTE_UNUSED
,
22946 tree list
, location_t location
)
22950 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
22952 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
22953 OMP_CLAUSE_CHAIN (c
) = list
;
22957 /* Parse all OpenMP clauses. The set clauses allowed by the directive
22958 is a bitmask in MASK. Return the list of clauses found; the result
22959 of clause default goes in *pdefault. */
22962 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
22963 const char *where
, cp_token
*pragma_tok
)
22965 tree clauses
= NULL
;
22967 cp_token
*token
= NULL
;
22969 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
22971 pragma_omp_clause c_kind
;
22972 const char *c_name
;
22973 tree prev
= clauses
;
22975 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22976 cp_lexer_consume_token (parser
->lexer
);
22978 token
= cp_lexer_peek_token (parser
->lexer
);
22979 c_kind
= cp_parser_omp_clause_name (parser
);
22984 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
22985 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
22987 c_name
= "collapse";
22989 case PRAGMA_OMP_CLAUSE_COPYIN
:
22990 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
22993 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
22994 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
22996 c_name
= "copyprivate";
22998 case PRAGMA_OMP_CLAUSE_DEFAULT
:
22999 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
23001 c_name
= "default";
23003 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
23004 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
23006 c_name
= "firstprivate";
23008 case PRAGMA_OMP_CLAUSE_IF
:
23009 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
23012 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
23013 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
23015 c_name
= "lastprivate";
23017 case PRAGMA_OMP_CLAUSE_NOWAIT
:
23018 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
23021 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
23022 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
23024 c_name
= "num_threads";
23026 case PRAGMA_OMP_CLAUSE_ORDERED
:
23027 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
23029 c_name
= "ordered";
23031 case PRAGMA_OMP_CLAUSE_PRIVATE
:
23032 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
23034 c_name
= "private";
23036 case PRAGMA_OMP_CLAUSE_REDUCTION
:
23037 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
23038 c_name
= "reduction";
23040 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
23041 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
23043 c_name
= "schedule";
23045 case PRAGMA_OMP_CLAUSE_SHARED
:
23046 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
23050 case PRAGMA_OMP_CLAUSE_UNTIED
:
23051 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
23056 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
23060 if (((mask
>> c_kind
) & 1) == 0)
23062 /* Remove the invalid clause(s) from the list to avoid
23063 confusing the rest of the compiler. */
23065 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
23069 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
23070 return finish_omp_clauses (clauses
);
23077 In practice, we're also interested in adding the statement to an
23078 outer node. So it is convenient if we work around the fact that
23079 cp_parser_statement calls add_stmt. */
23082 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
23084 unsigned save
= parser
->in_statement
;
23086 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23087 This preserves the "not within loop or switch" style error messages
23088 for nonsense cases like
23094 if (parser
->in_statement
)
23095 parser
->in_statement
= IN_OMP_BLOCK
;
23101 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
23103 parser
->in_statement
= save
;
23107 cp_parser_omp_structured_block (cp_parser
*parser
)
23109 tree stmt
= begin_omp_structured_block ();
23110 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
23112 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23114 cp_parser_end_omp_structured_block (parser
, save
);
23115 return finish_omp_structured_block (stmt
);
23119 # pragma omp atomic new-line
23123 x binop= expr | x++ | ++x | x-- | --x
23125 +, *, -, /, &, ^, |, <<, >>
23127 where x is an lvalue expression with scalar type. */
23130 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
23133 enum tree_code code
;
23135 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23137 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
23138 /*cast_p=*/false, NULL
);
23139 switch (TREE_CODE (lhs
))
23144 case PREINCREMENT_EXPR
:
23145 case POSTINCREMENT_EXPR
:
23146 lhs
= TREE_OPERAND (lhs
, 0);
23148 rhs
= integer_one_node
;
23151 case PREDECREMENT_EXPR
:
23152 case POSTDECREMENT_EXPR
:
23153 lhs
= TREE_OPERAND (lhs
, 0);
23155 rhs
= integer_one_node
;
23158 case COMPOUND_EXPR
:
23159 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
23160 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
23161 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
23162 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
23163 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23164 (TREE_OPERAND (lhs
, 1), 0), 0)))
23166 /* Undo effects of boolean_increment for post {in,de}crement. */
23167 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
23170 if (TREE_CODE (lhs
) == MODIFY_EXPR
23171 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
23173 /* Undo effects of boolean_increment. */
23174 if (integer_onep (TREE_OPERAND (lhs
, 1)))
23176 /* This is pre or post increment. */
23177 rhs
= TREE_OPERAND (lhs
, 1);
23178 lhs
= TREE_OPERAND (lhs
, 0);
23185 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
23191 code
= TRUNC_DIV_EXPR
;
23199 case CPP_LSHIFT_EQ
:
23200 code
= LSHIFT_EXPR
;
23202 case CPP_RSHIFT_EQ
:
23203 code
= RSHIFT_EXPR
;
23206 code
= BIT_AND_EXPR
;
23209 code
= BIT_IOR_EXPR
;
23212 code
= BIT_XOR_EXPR
;
23215 cp_parser_error (parser
,
23216 "invalid operator for %<#pragma omp atomic%>");
23219 cp_lexer_consume_token (parser
->lexer
);
23221 rhs
= cp_parser_expression (parser
, false, NULL
);
23222 if (rhs
== error_mark_node
)
23226 finish_omp_atomic (code
, lhs
, rhs
);
23227 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23231 cp_parser_skip_to_end_of_block_or_statement (parser
);
23236 # pragma omp barrier new-line */
23239 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
23241 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23242 finish_omp_barrier ();
23246 # pragma omp critical [(name)] new-line
23247 structured-block */
23250 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
23252 tree stmt
, name
= NULL
;
23254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23256 cp_lexer_consume_token (parser
->lexer
);
23258 name
= cp_parser_identifier (parser
);
23260 if (name
== error_mark_node
23261 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23262 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23263 /*or_comma=*/false,
23264 /*consume_paren=*/true);
23265 if (name
== error_mark_node
)
23268 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23270 stmt
= cp_parser_omp_structured_block (parser
);
23271 return c_finish_omp_critical (input_location
, stmt
, name
);
23275 # pragma omp flush flush-vars[opt] new-line
23278 ( variable-list ) */
23281 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
23283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23284 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
23285 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23287 finish_omp_flush ();
23290 /* Helper function, to parse omp for increment expression. */
23293 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
23295 tree cond
= cp_parser_binary_expression (parser
, false, true,
23296 PREC_NOT_OPERATOR
, NULL
);
23299 if (cond
== error_mark_node
23300 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23302 cp_parser_skip_to_end_of_statement (parser
);
23303 return error_mark_node
;
23306 switch (TREE_CODE (cond
))
23314 return error_mark_node
;
23317 /* If decl is an iterator, preserve LHS and RHS of the relational
23318 expr until finish_omp_for. */
23320 && (type_dependent_expression_p (decl
)
23321 || CLASS_TYPE_P (TREE_TYPE (decl
))))
23324 return build_x_binary_op (TREE_CODE (cond
),
23325 TREE_OPERAND (cond
, 0), ERROR_MARK
,
23326 TREE_OPERAND (cond
, 1), ERROR_MARK
,
23327 &overloaded_p
, tf_warning_or_error
);
23330 /* Helper function, to parse omp for increment expression. */
23333 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
23335 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23341 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
23343 op
= (token
->type
== CPP_PLUS_PLUS
23344 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
23345 cp_lexer_consume_token (parser
->lexer
);
23346 lhs
= cp_parser_cast_expression (parser
, false, false, NULL
);
23348 return error_mark_node
;
23349 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
23352 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
23354 return error_mark_node
;
23356 token
= cp_lexer_peek_token (parser
->lexer
);
23357 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
23359 op
= (token
->type
== CPP_PLUS_PLUS
23360 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
23361 cp_lexer_consume_token (parser
->lexer
);
23362 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
23365 op
= cp_parser_assignment_operator_opt (parser
);
23366 if (op
== ERROR_MARK
)
23367 return error_mark_node
;
23369 if (op
!= NOP_EXPR
)
23371 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
23372 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
23373 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
23376 lhs
= cp_parser_binary_expression (parser
, false, false,
23377 PREC_ADDITIVE_EXPRESSION
, NULL
);
23378 token
= cp_lexer_peek_token (parser
->lexer
);
23379 decl_first
= lhs
== decl
;
23382 if (token
->type
!= CPP_PLUS
23383 && token
->type
!= CPP_MINUS
)
23384 return error_mark_node
;
23388 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
23389 cp_lexer_consume_token (parser
->lexer
);
23390 rhs
= cp_parser_binary_expression (parser
, false, false,
23391 PREC_ADDITIVE_EXPRESSION
, NULL
);
23392 token
= cp_lexer_peek_token (parser
->lexer
);
23393 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
23395 if (lhs
== NULL_TREE
)
23397 if (op
== PLUS_EXPR
)
23400 lhs
= build_x_unary_op (NEGATE_EXPR
, rhs
, tf_warning_or_error
);
23403 lhs
= build_x_binary_op (op
, lhs
, ERROR_MARK
, rhs
, ERROR_MARK
,
23404 NULL
, tf_warning_or_error
);
23407 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
23411 if (rhs
!= decl
|| op
== MINUS_EXPR
)
23412 return error_mark_node
;
23413 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
23416 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
23418 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
23421 /* Parse the restricted form of the for statement allowed by OpenMP. */
23424 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
23426 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
23427 tree real_decl
, initv
, condv
, incrv
, declv
;
23428 tree this_pre_body
, cl
;
23429 location_t loc_first
;
23430 bool collapse_err
= false;
23431 int i
, collapse
= 1, nbraces
= 0;
23432 VEC(tree
,gc
) *for_block
= make_tree_vector ();
23434 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
23435 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
23436 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
23438 gcc_assert (collapse
>= 1);
23440 declv
= make_tree_vec (collapse
);
23441 initv
= make_tree_vec (collapse
);
23442 condv
= make_tree_vec (collapse
);
23443 incrv
= make_tree_vec (collapse
);
23445 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
23447 for (i
= 0; i
< collapse
; i
++)
23449 int bracecount
= 0;
23450 bool add_private_clause
= false;
23453 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
23455 cp_parser_error (parser
, "for statement expected");
23458 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
23460 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23463 init
= decl
= real_decl
= NULL
;
23464 this_pre_body
= push_stmt_list ();
23465 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23467 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
23471 integer-type var = lb
23472 random-access-iterator-type var = lb
23473 pointer-type var = lb
23475 cp_decl_specifier_seq type_specifiers
;
23477 /* First, try to parse as an initialized declaration. See
23478 cp_parser_condition, from whence the bulk of this is copied. */
23480 cp_parser_parse_tentatively (parser
);
23481 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
23482 /*is_trailing_return=*/false,
23484 if (cp_parser_parse_definitely (parser
))
23486 /* If parsing a type specifier seq succeeded, then this
23487 MUST be a initialized declaration. */
23488 tree asm_specification
, attributes
;
23489 cp_declarator
*declarator
;
23491 declarator
= cp_parser_declarator (parser
,
23492 CP_PARSER_DECLARATOR_NAMED
,
23493 /*ctor_dtor_or_conv_p=*/NULL
,
23494 /*parenthesized_p=*/NULL
,
23495 /*member_p=*/false);
23496 attributes
= cp_parser_attributes_opt (parser
);
23497 asm_specification
= cp_parser_asm_specification_opt (parser
);
23499 if (declarator
== cp_error_declarator
)
23500 cp_parser_skip_to_end_of_statement (parser
);
23504 tree pushed_scope
, auto_node
;
23506 decl
= start_decl (declarator
, &type_specifiers
,
23507 SD_INITIALIZED
, attributes
,
23508 /*prefix_attributes=*/NULL_TREE
,
23511 auto_node
= type_uses_auto (TREE_TYPE (decl
));
23512 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
23514 if (cp_lexer_next_token_is (parser
->lexer
,
23516 error ("parenthesized initialization is not allowed in "
23517 "OpenMP %<for%> loop");
23519 /* Trigger an error. */
23520 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23522 init
= error_mark_node
;
23523 cp_parser_skip_to_end_of_statement (parser
);
23525 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
23526 || type_dependent_expression_p (decl
)
23529 bool is_direct_init
, is_non_constant_init
;
23531 init
= cp_parser_initializer (parser
,
23533 &is_non_constant_init
);
23535 if (auto_node
&& describable_type (init
))
23538 = do_auto_deduction (TREE_TYPE (decl
), init
,
23541 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
23542 && !type_dependent_expression_p (decl
))
23546 cp_finish_decl (decl
, init
, !is_non_constant_init
,
23548 LOOKUP_ONLYCONVERTING
);
23549 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
23551 VEC_safe_push (tree
, gc
, for_block
, this_pre_body
);
23555 init
= pop_stmt_list (this_pre_body
);
23556 this_pre_body
= NULL_TREE
;
23561 cp_lexer_consume_token (parser
->lexer
);
23562 init
= cp_parser_assignment_expression (parser
, false, NULL
);
23565 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
23566 init
= error_mark_node
;
23568 cp_finish_decl (decl
, NULL_TREE
,
23569 /*init_const_expr_p=*/false,
23571 LOOKUP_ONLYCONVERTING
);
23575 pop_scope (pushed_scope
);
23581 /* If parsing a type specifier sequence failed, then
23582 this MUST be a simple expression. */
23583 cp_parser_parse_tentatively (parser
);
23584 decl
= cp_parser_primary_expression (parser
, false, false,
23586 if (!cp_parser_error_occurred (parser
)
23589 && CLASS_TYPE_P (TREE_TYPE (decl
)))
23593 cp_parser_parse_definitely (parser
);
23594 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23595 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
23596 finish_expr_stmt (build_x_modify_expr (decl
, NOP_EXPR
,
23598 tf_warning_or_error
));
23599 add_private_clause
= true;
23604 cp_parser_abort_tentative_parse (parser
);
23605 init
= cp_parser_expression (parser
, false, NULL
);
23608 if (TREE_CODE (init
) == MODIFY_EXPR
23609 || TREE_CODE (init
) == MODOP_EXPR
)
23610 real_decl
= TREE_OPERAND (init
, 0);
23615 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23618 this_pre_body
= pop_stmt_list (this_pre_body
);
23622 pre_body
= push_stmt_list ();
23624 add_stmt (this_pre_body
);
23625 pre_body
= pop_stmt_list (pre_body
);
23628 pre_body
= this_pre_body
;
23633 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
23636 for (c
= par_clauses
; *c
; )
23637 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
23638 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23640 error_at (loc
, "iteration variable %qD"
23641 " should not be firstprivate", real_decl
);
23642 *c
= OMP_CLAUSE_CHAIN (*c
);
23644 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
23645 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23647 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
23648 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
23649 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
23650 OMP_CLAUSE_DECL (l
) = real_decl
;
23651 OMP_CLAUSE_CHAIN (l
) = clauses
;
23652 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
23654 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
23655 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
23656 add_private_clause
= false;
23660 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
23661 && OMP_CLAUSE_DECL (*c
) == real_decl
)
23662 add_private_clause
= false;
23663 c
= &OMP_CLAUSE_CHAIN (*c
);
23667 if (add_private_clause
)
23670 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
23672 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
23673 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
23674 && OMP_CLAUSE_DECL (c
) == decl
)
23676 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
23677 && OMP_CLAUSE_DECL (c
) == decl
)
23678 error_at (loc
, "iteration variable %qD "
23679 "should not be firstprivate",
23681 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
23682 && OMP_CLAUSE_DECL (c
) == decl
)
23683 error_at (loc
, "iteration variable %qD should not be reduction",
23688 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
23689 OMP_CLAUSE_DECL (c
) = decl
;
23690 c
= finish_omp_clauses (c
);
23693 OMP_CLAUSE_CHAIN (c
) = clauses
;
23700 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23701 cond
= cp_parser_omp_for_cond (parser
, decl
);
23702 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23705 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
23707 /* If decl is an iterator, preserve the operator on decl
23708 until finish_omp_for. */
23710 && (type_dependent_expression_p (decl
)
23711 || CLASS_TYPE_P (TREE_TYPE (decl
))))
23712 incr
= cp_parser_omp_for_incr (parser
, decl
);
23714 incr
= cp_parser_expression (parser
, false, NULL
);
23717 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23718 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
23719 /*or_comma=*/false,
23720 /*consume_paren=*/true);
23722 TREE_VEC_ELT (declv
, i
) = decl
;
23723 TREE_VEC_ELT (initv
, i
) = init
;
23724 TREE_VEC_ELT (condv
, i
) = cond
;
23725 TREE_VEC_ELT (incrv
, i
) = incr
;
23727 if (i
== collapse
- 1)
23730 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
23731 in between the collapsed for loops to be still considered perfectly
23732 nested. Hopefully the final version clarifies this.
23733 For now handle (multiple) {'s and empty statements. */
23734 cp_parser_parse_tentatively (parser
);
23737 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
23739 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23741 cp_lexer_consume_token (parser
->lexer
);
23744 else if (bracecount
23745 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23746 cp_lexer_consume_token (parser
->lexer
);
23749 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23750 error_at (loc
, "not enough collapsed for loops");
23751 collapse_err
= true;
23752 cp_parser_abort_tentative_parse (parser
);
23761 cp_parser_parse_definitely (parser
);
23762 nbraces
+= bracecount
;
23766 /* Note that we saved the original contents of this flag when we entered
23767 the structured block, and so we don't need to re-save it here. */
23768 parser
->in_statement
= IN_OMP_FOR
;
23770 /* Note that the grammar doesn't call for a structured block here,
23771 though the loop as a whole is a structured block. */
23772 body
= push_stmt_list ();
23773 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23774 body
= pop_stmt_list (body
);
23776 if (declv
== NULL_TREE
)
23779 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
23780 pre_body
, clauses
);
23784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
23786 cp_lexer_consume_token (parser
->lexer
);
23789 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23790 cp_lexer_consume_token (parser
->lexer
);
23795 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
23796 "collapsed loops not perfectly nested");
23798 collapse_err
= true;
23799 cp_parser_statement_seq_opt (parser
, NULL
);
23800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
23805 while (!VEC_empty (tree
, for_block
))
23806 add_stmt (pop_stmt_list (VEC_pop (tree
, for_block
)));
23807 release_tree_vector (for_block
);
23813 #pragma omp for for-clause[optseq] new-line
23816 #define OMP_FOR_CLAUSE_MASK \
23817 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23818 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23819 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23820 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23821 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
23822 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
23823 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
23824 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
23827 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
23829 tree clauses
, sb
, ret
;
23832 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
23833 "#pragma omp for", pragma_tok
);
23835 sb
= begin_omp_structured_block ();
23836 save
= cp_parser_begin_omp_structured_block (parser
);
23838 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
23840 cp_parser_end_omp_structured_block (parser
, save
);
23841 add_stmt (finish_omp_structured_block (sb
));
23847 # pragma omp master new-line
23848 structured-block */
23851 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
23853 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23854 return c_finish_omp_master (input_location
,
23855 cp_parser_omp_structured_block (parser
));
23859 # pragma omp ordered new-line
23860 structured-block */
23863 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
23865 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23866 cp_parser_require_pragma_eol (parser
, pragma_tok
);
23867 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
23873 { section-sequence }
23876 section-directive[opt] structured-block
23877 section-sequence section-directive structured-block */
23880 cp_parser_omp_sections_scope (cp_parser
*parser
)
23882 tree stmt
, substmt
;
23883 bool error_suppress
= false;
23886 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
23889 stmt
= push_stmt_list ();
23891 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
23895 substmt
= begin_omp_structured_block ();
23896 save
= cp_parser_begin_omp_structured_block (parser
);
23900 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
23902 tok
= cp_lexer_peek_token (parser
->lexer
);
23903 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
23905 if (tok
->type
== CPP_CLOSE_BRACE
)
23907 if (tok
->type
== CPP_EOF
)
23911 cp_parser_end_omp_structured_block (parser
, save
);
23912 substmt
= finish_omp_structured_block (substmt
);
23913 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
23914 add_stmt (substmt
);
23919 tok
= cp_lexer_peek_token (parser
->lexer
);
23920 if (tok
->type
== CPP_CLOSE_BRACE
)
23922 if (tok
->type
== CPP_EOF
)
23925 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
23927 cp_lexer_consume_token (parser
->lexer
);
23928 cp_parser_require_pragma_eol (parser
, tok
);
23929 error_suppress
= false;
23931 else if (!error_suppress
)
23933 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
23934 error_suppress
= true;
23937 substmt
= cp_parser_omp_structured_block (parser
);
23938 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
23939 add_stmt (substmt
);
23941 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
23943 substmt
= pop_stmt_list (stmt
);
23945 stmt
= make_node (OMP_SECTIONS
);
23946 TREE_TYPE (stmt
) = void_type_node
;
23947 OMP_SECTIONS_BODY (stmt
) = substmt
;
23954 # pragma omp sections sections-clause[optseq] newline
23957 #define OMP_SECTIONS_CLAUSE_MASK \
23958 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23959 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23960 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
23961 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23962 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
23965 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
23969 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
23970 "#pragma omp sections", pragma_tok
);
23972 ret
= cp_parser_omp_sections_scope (parser
);
23974 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
23980 # pragma parallel parallel-clause new-line
23981 # pragma parallel for parallel-for-clause new-line
23982 # pragma parallel sections parallel-sections-clause new-line */
23984 #define OMP_PARALLEL_CLAUSE_MASK \
23985 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
23986 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
23987 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
23988 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
23989 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
23990 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
23991 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
23992 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
23995 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
23997 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
23998 const char *p_name
= "#pragma omp parallel";
23999 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
24000 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
24002 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24004 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
24006 cp_lexer_consume_token (parser
->lexer
);
24007 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
24008 p_name
= "#pragma omp parallel for";
24009 mask
|= OMP_FOR_CLAUSE_MASK
;
24010 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
24012 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
24014 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
24015 const char *p
= IDENTIFIER_POINTER (id
);
24016 if (strcmp (p
, "sections") == 0)
24018 cp_lexer_consume_token (parser
->lexer
);
24019 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
24020 p_name
= "#pragma omp parallel sections";
24021 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
24022 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
24026 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
24027 block
= begin_omp_parallel ();
24028 save
= cp_parser_begin_omp_structured_block (parser
);
24032 case PRAGMA_OMP_PARALLEL
:
24033 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24034 par_clause
= clauses
;
24037 case PRAGMA_OMP_PARALLEL_FOR
:
24038 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
24039 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
24042 case PRAGMA_OMP_PARALLEL_SECTIONS
:
24043 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
24044 stmt
= cp_parser_omp_sections_scope (parser
);
24046 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
24050 gcc_unreachable ();
24053 cp_parser_end_omp_structured_block (parser
, save
);
24054 stmt
= finish_omp_parallel (par_clause
, block
);
24055 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
24056 OMP_PARALLEL_COMBINED (stmt
) = 1;
24061 # pragma omp single single-clause[optseq] new-line
24062 structured-block */
24064 #define OMP_SINGLE_CLAUSE_MASK \
24065 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24066 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24067 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24068 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24071 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
24073 tree stmt
= make_node (OMP_SINGLE
);
24074 TREE_TYPE (stmt
) = void_type_node
;
24076 OMP_SINGLE_CLAUSES (stmt
)
24077 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
24078 "#pragma omp single", pragma_tok
);
24079 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
24081 return add_stmt (stmt
);
24085 # pragma omp task task-clause[optseq] new-line
24086 structured-block */
24088 #define OMP_TASK_CLAUSE_MASK \
24089 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24090 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24091 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24092 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24093 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24094 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24097 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
24099 tree clauses
, block
;
24102 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
24103 "#pragma omp task", pragma_tok
);
24104 block
= begin_omp_task ();
24105 save
= cp_parser_begin_omp_structured_block (parser
);
24106 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
24107 cp_parser_end_omp_structured_block (parser
, save
);
24108 return finish_omp_task (clauses
, block
);
24112 # pragma omp taskwait new-line */
24115 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
24117 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24118 finish_omp_taskwait ();
24122 # pragma omp threadprivate (variable-list) */
24125 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
24129 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
24130 cp_parser_require_pragma_eol (parser
, pragma_tok
);
24132 finish_omp_threadprivate (vars
);
24135 /* Main entry point to OpenMP statement pragmas. */
24138 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
24142 switch (pragma_tok
->pragma_kind
)
24144 case PRAGMA_OMP_ATOMIC
:
24145 cp_parser_omp_atomic (parser
, pragma_tok
);
24147 case PRAGMA_OMP_CRITICAL
:
24148 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
24150 case PRAGMA_OMP_FOR
:
24151 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
24153 case PRAGMA_OMP_MASTER
:
24154 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
24156 case PRAGMA_OMP_ORDERED
:
24157 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
24159 case PRAGMA_OMP_PARALLEL
:
24160 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
24162 case PRAGMA_OMP_SECTIONS
:
24163 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
24165 case PRAGMA_OMP_SINGLE
:
24166 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
24168 case PRAGMA_OMP_TASK
:
24169 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
24172 gcc_unreachable ();
24176 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
24181 static GTY (()) cp_parser
*the_parser
;
24184 /* Special handling for the first token or line in the file. The first
24185 thing in the file might be #pragma GCC pch_preprocess, which loads a
24186 PCH file, which is a GC collection point. So we need to handle this
24187 first pragma without benefit of an existing lexer structure.
24189 Always returns one token to the caller in *FIRST_TOKEN. This is
24190 either the true first token of the file, or the first token after
24191 the initial pragma. */
24194 cp_parser_initial_pragma (cp_token
*first_token
)
24198 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24199 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
24202 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24203 if (first_token
->type
== CPP_STRING
)
24205 name
= first_token
->u
.value
;
24207 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24208 if (first_token
->type
!= CPP_PRAGMA_EOL
)
24209 error_at (first_token
->location
,
24210 "junk at end of %<#pragma GCC pch_preprocess%>");
24213 error_at (first_token
->location
, "expected string literal");
24215 /* Skip to the end of the pragma. */
24216 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
24217 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24219 /* Now actually load the PCH file. */
24221 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
24223 /* Read one more token to return to our caller. We have to do this
24224 after reading the PCH file in, since its pointers have to be
24226 cp_lexer_get_preprocessor_token (NULL
, first_token
);
24229 /* Normal parsing of a pragma token. Here we can (and must) use the
24233 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
24235 cp_token
*pragma_tok
;
24238 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
24239 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
24240 parser
->lexer
->in_pragma
= true;
24242 id
= pragma_tok
->pragma_kind
;
24245 case PRAGMA_GCC_PCH_PREPROCESS
:
24246 error_at (pragma_tok
->location
,
24247 "%<#pragma GCC pch_preprocess%> must be first");
24250 case PRAGMA_OMP_BARRIER
:
24253 case pragma_compound
:
24254 cp_parser_omp_barrier (parser
, pragma_tok
);
24257 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
24258 "used in compound statements");
24265 case PRAGMA_OMP_FLUSH
:
24268 case pragma_compound
:
24269 cp_parser_omp_flush (parser
, pragma_tok
);
24272 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
24273 "used in compound statements");
24280 case PRAGMA_OMP_TASKWAIT
:
24283 case pragma_compound
:
24284 cp_parser_omp_taskwait (parser
, pragma_tok
);
24287 error_at (pragma_tok
->location
,
24288 "%<#pragma omp taskwait%> may only be "
24289 "used in compound statements");
24296 case PRAGMA_OMP_THREADPRIVATE
:
24297 cp_parser_omp_threadprivate (parser
, pragma_tok
);
24300 case PRAGMA_OMP_ATOMIC
:
24301 case PRAGMA_OMP_CRITICAL
:
24302 case PRAGMA_OMP_FOR
:
24303 case PRAGMA_OMP_MASTER
:
24304 case PRAGMA_OMP_ORDERED
:
24305 case PRAGMA_OMP_PARALLEL
:
24306 case PRAGMA_OMP_SECTIONS
:
24307 case PRAGMA_OMP_SINGLE
:
24308 case PRAGMA_OMP_TASK
:
24309 if (context
== pragma_external
)
24311 cp_parser_omp_construct (parser
, pragma_tok
);
24314 case PRAGMA_OMP_SECTION
:
24315 error_at (pragma_tok
->location
,
24316 "%<#pragma omp section%> may only be used in "
24317 "%<#pragma omp sections%> construct");
24321 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
24322 c_invoke_pragma_handler (id
);
24326 cp_parser_error (parser
, "expected declaration specifiers");
24330 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
24334 /* The interface the pragma parsers have to the lexer. */
24337 pragma_lex (tree
*value
)
24340 enum cpp_ttype ret
;
24342 tok
= cp_lexer_peek_token (the_parser
->lexer
);
24345 *value
= tok
->u
.value
;
24347 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
24349 else if (ret
== CPP_STRING
)
24350 *value
= cp_parser_string_literal (the_parser
, false, false);
24353 cp_lexer_consume_token (the_parser
->lexer
);
24354 if (ret
== CPP_KEYWORD
)
24362 /* External interface. */
24364 /* Parse one entire translation unit. */
24367 c_parse_file (void)
24369 static bool already_called
= false;
24371 if (already_called
)
24373 sorry ("inter-module optimizations not implemented for C++");
24376 already_called
= true;
24378 the_parser
= cp_parser_new ();
24379 push_deferring_access_checks (flag_access_control
24380 ? dk_no_deferred
: dk_no_check
);
24381 cp_parser_translation_unit (the_parser
);
24385 #include "gt-cp-parser.h"