2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011, 2012 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"
31 #include "c-family/c-pragma.h"
34 #include "diagnostic-core.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
40 #include "tree-pretty-print.h"
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token
=
51 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant
{
57 /* floating-point literal */
61 /* %<__FUNCTION__%> */
63 /* %<__PRETTY_FUNCTION__%> */
71 /* %<typeid%> operator */
73 /* non-constant compound literals */
81 /* an array reference */
87 /* the address of a label */
101 /* calls to overloaded operators */
105 /* a comma operator */
107 /* a call to a constructor */
109 /* a transaction expression */
111 } non_integral_constant
;
113 /* The various kinds of errors about name-lookup failing. */
114 typedef enum name_lookup_error
{
119 /* is not a class or namespace */
121 /* is not a class, namespace, or enumeration */
125 /* The various kinds of required token */
126 typedef enum required_token
{
128 RT_SEMICOLON
, /* ';' */
129 RT_OPEN_PAREN
, /* '(' */
130 RT_CLOSE_BRACE
, /* '}' */
131 RT_OPEN_BRACE
, /* '{' */
132 RT_CLOSE_SQUARE
, /* ']' */
133 RT_OPEN_SQUARE
, /* '[' */
137 RT_GREATER
, /* '>' */
139 RT_ELLIPSIS
, /* '...' */
143 RT_COLON_SCOPE
, /* ':' or '::' */
144 RT_CLOSE_PAREN
, /* ')' */
145 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
146 RT_PRAGMA_EOL
, /* end of line */
147 RT_NAME
, /* identifier */
149 /* The type is CPP_KEYWORD */
151 RT_DELETE
, /* delete */
152 RT_RETURN
, /* return */
153 RT_WHILE
, /* while */
154 RT_EXTERN
, /* extern */
155 RT_STATIC_ASSERT
, /* static_assert */
156 RT_DECLTYPE
, /* decltype */
157 RT_OPERATOR
, /* operator */
158 RT_CLASS
, /* class */
159 RT_TEMPLATE
, /* template */
160 RT_NAMESPACE
, /* namespace */
161 RT_USING
, /* using */
164 RT_CATCH
, /* catch */
165 RT_THROW
, /* throw */
166 RT_LABEL
, /* __label__ */
167 RT_AT_TRY
, /* @try */
168 RT_AT_SYNCHRONIZED
, /* @synchronized */
169 RT_AT_THROW
, /* @throw */
171 RT_SELECT
, /* selection-statement */
172 RT_INTERATION
, /* iteration-statement */
173 RT_JUMP
, /* jump-statement */
174 RT_CLASS_KEY
, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
183 static cp_lexer
*cp_lexer_new_main
185 static cp_lexer
*cp_lexer_new_from_tokens
186 (cp_token_cache
*tokens
);
187 static void cp_lexer_destroy
189 static int cp_lexer_saving_tokens
191 static cp_token
*cp_lexer_token_at
192 (cp_lexer
*, cp_token_position
);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer
*, cp_token
*);
195 static inline cp_token
*cp_lexer_peek_token
197 static cp_token
*cp_lexer_peek_nth_token
198 (cp_lexer
*, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer
*, enum cpp_ttype
);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer
*, enum cpp_ttype
);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer
*, enum rid
);
205 static cp_token
*cp_lexer_consume_token
207 static void cp_lexer_purge_token
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer
*, cp_token_position
);
211 static void cp_lexer_save_tokens
213 static void cp_lexer_commit_tokens
215 static void cp_lexer_rollback_tokens
217 static void cp_lexer_print_token
218 (FILE *, cp_token
*);
219 static inline bool cp_lexer_debugging_p
221 static void cp_lexer_start_debugging
222 (cp_lexer
*) ATTRIBUTE_UNUSED
;
223 static void cp_lexer_stop_debugging
224 (cp_lexer
*) ATTRIBUTE_UNUSED
;
226 static cp_token_cache
*cp_token_cache_new
227 (cp_token
*, cp_token
*);
229 static void cp_parser_initial_pragma
232 static tree cp_literal_operator_id
235 /* Manifest constants. */
236 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
237 #define CP_SAVED_TOKEN_STACK 5
241 /* The stream to which debugging output should be written. */
242 static FILE *cp_lexer_debug_stream
;
244 /* Nonzero if we are parsing an unevaluated operand: an operand to
245 sizeof, typeof, or alignof. */
246 int cp_unevaluated_operand
;
248 /* Dump up to NUM tokens in BUFFER to FILE starting with token
249 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
250 first token in BUFFER. If NUM is 0, dump all the tokens. If
251 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
252 highlighted by surrounding it in [[ ]]. */
255 cp_lexer_dump_tokens (FILE *file
, VEC(cp_token
,gc
) *buffer
,
256 cp_token
*start_token
, unsigned num
,
257 cp_token
*curr_token
)
259 unsigned i
, nprinted
;
263 fprintf (file
, "%u tokens\n", VEC_length (cp_token
, buffer
));
269 num
= VEC_length (cp_token
, buffer
);
271 if (start_token
== NULL
)
272 start_token
= VEC_address (cp_token
, buffer
);
274 if (start_token
> VEC_address (cp_token
, buffer
))
276 cp_lexer_print_token (file
, &VEC_index (cp_token
, buffer
, 0));
277 fprintf (file
, " ... ");
282 for (i
= 0; VEC_iterate (cp_token
, buffer
, i
, token
) && nprinted
< num
; i
++)
284 if (token
== start_token
)
291 if (token
== curr_token
)
292 fprintf (file
, "[[");
294 cp_lexer_print_token (file
, token
);
296 if (token
== curr_token
)
297 fprintf (file
, "]]");
303 case CPP_CLOSE_BRACE
:
313 if (i
== num
&& i
< VEC_length (cp_token
, buffer
))
315 fprintf (file
, " ... ");
316 cp_lexer_print_token (file
, &VEC_last (cp_token
, buffer
));
319 fprintf (file
, "\n");
323 /* Dump all tokens in BUFFER to stderr. */
326 cp_lexer_debug_tokens (VEC(cp_token
,gc
) *buffer
)
328 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
332 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
333 description for T. */
336 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
340 fprintf (file
, "%s: ", desc
);
341 print_node_brief (file
, "", t
, 0);
346 /* Dump parser context C to FILE. */
349 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
351 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
352 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
353 print_node_brief (file
, "", c
->object_type
, 0);
354 fprintf (file
, "}\n");
358 /* Print the stack of parsing contexts to FILE starting with FIRST. */
361 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
364 cp_parser_context
*c
;
366 fprintf (file
, "Parsing context stack:\n");
367 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
369 fprintf (file
, "\t#%u: ", i
);
370 cp_debug_print_context (file
, c
);
375 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
378 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
381 fprintf (file
, "%s: true\n", desc
);
385 /* Print an unparsed function entry UF to FILE. */
388 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
391 cp_default_arg_entry
*default_arg_fn
;
394 fprintf (file
, "\tFunctions with default args:\n");
396 VEC_iterate (cp_default_arg_entry
, uf
->funs_with_default_args
, i
,
400 fprintf (file
, "\t\tClass type: ");
401 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
402 fprintf (file
, "\t\tDeclaration: ");
403 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
404 fprintf (file
, "\n");
407 fprintf (file
, "\n\tFunctions with definitions that require "
408 "post-processing\n\t\t");
409 for (i
= 0; VEC_iterate (tree
, uf
->funs_with_definitions
, i
, fn
); i
++)
411 print_node_brief (file
, "", fn
, 0);
414 fprintf (file
, "\n");
416 fprintf (file
, "\n\tNon-static data members with initializers that require "
417 "post-processing\n\t\t");
418 for (i
= 0; VEC_iterate (tree
, uf
->nsdmis
, i
, fn
); i
++)
420 print_node_brief (file
, "", fn
, 0);
423 fprintf (file
, "\n");
427 /* Print the stack of unparsed member functions S to FILE. */
430 cp_debug_print_unparsed_queues (FILE *file
,
431 VEC(cp_unparsed_functions_entry
, gc
) *s
)
434 cp_unparsed_functions_entry
*uf
;
436 fprintf (file
, "Unparsed functions\n");
437 for (i
= 0; VEC_iterate (cp_unparsed_functions_entry
, s
, i
, uf
); i
++)
439 fprintf (file
, "#%u:\n", i
);
440 cp_debug_print_unparsed_function (file
, uf
);
445 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
446 the given PARSER. If FILE is NULL, the output is printed on stderr. */
449 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
451 cp_token
*next_token
, *first_token
, *start_token
;
456 next_token
= parser
->lexer
->next_token
;
457 first_token
= VEC_address (cp_token
, parser
->lexer
->buffer
);
458 start_token
= (next_token
> first_token
+ window_size
/ 2)
459 ? next_token
- window_size
/ 2
461 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
466 /* Dump debugging information for the given PARSER. If FILE is NULL,
467 the output is printed on stderr. */
470 cp_debug_parser (FILE *file
, cp_parser
*parser
)
472 const size_t window_size
= 20;
474 expanded_location eloc
;
479 fprintf (file
, "Parser state\n\n");
480 fprintf (file
, "Number of tokens: %u\n",
481 VEC_length (cp_token
, parser
->lexer
->buffer
));
482 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
483 cp_debug_print_tree_if_set (file
, "Object scope",
484 parser
->object_scope
);
485 cp_debug_print_tree_if_set (file
, "Qualifying scope",
486 parser
->qualifying_scope
);
487 cp_debug_print_context_stack (file
, parser
->context
);
488 cp_debug_print_flag (file
, "Allow GNU extensions",
489 parser
->allow_gnu_extensions_p
);
490 cp_debug_print_flag (file
, "'>' token is greater-than",
491 parser
->greater_than_is_operator_p
);
492 cp_debug_print_flag (file
, "Default args allowed in current "
493 "parameter list", parser
->default_arg_ok_p
);
494 cp_debug_print_flag (file
, "Parsing integral constant-expression",
495 parser
->integral_constant_expression_p
);
496 cp_debug_print_flag (file
, "Allow non-constant expression in current "
497 "constant-expression",
498 parser
->allow_non_integral_constant_expression_p
);
499 cp_debug_print_flag (file
, "Seen non-constant expression",
500 parser
->non_integral_constant_expression_p
);
501 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
503 parser
->local_variables_forbidden_p
);
504 cp_debug_print_flag (file
, "In unbraced linkage specification",
505 parser
->in_unbraced_linkage_specification_p
);
506 cp_debug_print_flag (file
, "Parsing a declarator",
507 parser
->in_declarator_p
);
508 cp_debug_print_flag (file
, "In template argument list",
509 parser
->in_template_argument_list_p
);
510 cp_debug_print_flag (file
, "Parsing an iteration statement",
511 parser
->in_statement
& IN_ITERATION_STMT
);
512 cp_debug_print_flag (file
, "Parsing a switch statement",
513 parser
->in_statement
& IN_SWITCH_STMT
);
514 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
515 parser
->in_statement
& IN_OMP_BLOCK
);
516 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
517 parser
->in_statement
& IN_OMP_FOR
);
518 cp_debug_print_flag (file
, "Parsing an if statement",
519 parser
->in_statement
& IN_IF_STMT
);
520 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
521 "context", parser
->in_type_id_in_expr_p
);
522 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
523 parser
->implicit_extern_c
);
524 cp_debug_print_flag (file
, "String expressions should be translated "
525 "to execution character set",
526 parser
->translate_strings_p
);
527 cp_debug_print_flag (file
, "Parsing function body outside of a "
528 "local class", parser
->in_function_body
);
529 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
530 parser
->colon_corrects_to_scope_p
);
531 if (parser
->type_definition_forbidden_message
)
532 fprintf (file
, "Error message for forbidden type definitions: %s\n",
533 parser
->type_definition_forbidden_message
);
534 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
535 fprintf (file
, "Number of class definitions in progress: %u\n",
536 parser
->num_classes_being_defined
);
537 fprintf (file
, "Number of template parameter lists for the current "
538 "declaration: %u\n", parser
->num_template_parameter_lists
);
539 cp_debug_parser_tokens (file
, parser
, window_size
);
540 token
= parser
->lexer
->next_token
;
541 fprintf (file
, "Next token to parse:\n");
542 fprintf (file
, "\tToken: ");
543 cp_lexer_print_token (file
, token
);
544 eloc
= expand_location (token
->location
);
545 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
546 fprintf (file
, "\tLine: %d\n", eloc
.line
);
547 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
551 /* Allocate memory for a new lexer object and return it. */
554 cp_lexer_alloc (void)
558 c_common_no_more_pch ();
560 /* Allocate the memory. */
561 lexer
= ggc_alloc_cleared_cp_lexer ();
563 /* Initially we are not debugging. */
564 lexer
->debugging_p
= false;
566 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
567 CP_SAVED_TOKEN_STACK
);
569 /* Create the buffer. */
570 lexer
->buffer
= VEC_alloc (cp_token
, gc
, CP_LEXER_BUFFER_SIZE
);
576 /* Create a new main C++ lexer, the lexer that gets tokens from the
580 cp_lexer_new_main (void)
585 /* It's possible that parsing the first pragma will load a PCH file,
586 which is a GC collection point. So we have to do that before
587 allocating any memory. */
588 cp_parser_initial_pragma (&token
);
590 lexer
= cp_lexer_alloc ();
592 /* Put the first token in the buffer. */
593 VEC_quick_push (cp_token
, lexer
->buffer
, token
);
595 /* Get the remaining tokens from the preprocessor. */
596 while (token
.type
!= CPP_EOF
)
598 cp_lexer_get_preprocessor_token (lexer
, &token
);
599 VEC_safe_push (cp_token
, gc
, lexer
->buffer
, token
);
602 lexer
->last_token
= VEC_address (cp_token
, lexer
->buffer
)
603 + VEC_length (cp_token
, lexer
->buffer
)
605 lexer
->next_token
= VEC_length (cp_token
, lexer
->buffer
)
606 ? VEC_address (cp_token
, lexer
->buffer
)
609 /* Subsequent preprocessor diagnostics should use compiler
610 diagnostic functions to get the compiler source location. */
613 gcc_assert (!lexer
->next_token
->purged_p
);
617 /* Create a new lexer whose token stream is primed with the tokens in
618 CACHE. When these tokens are exhausted, no new tokens will be read. */
621 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
623 cp_token
*first
= cache
->first
;
624 cp_token
*last
= cache
->last
;
625 cp_lexer
*lexer
= ggc_alloc_cleared_cp_lexer ();
627 /* We do not own the buffer. */
628 lexer
->buffer
= NULL
;
629 lexer
->next_token
= first
== last
? &eof_token
: first
;
630 lexer
->last_token
= last
;
632 lexer
->saved_tokens
= VEC_alloc (cp_token_position
, heap
,
633 CP_SAVED_TOKEN_STACK
);
635 /* Initially we are not debugging. */
636 lexer
->debugging_p
= false;
638 gcc_assert (!lexer
->next_token
->purged_p
);
642 /* Frees all resources associated with LEXER. */
645 cp_lexer_destroy (cp_lexer
*lexer
)
647 VEC_free (cp_token
, gc
, lexer
->buffer
);
648 VEC_free (cp_token_position
, heap
, lexer
->saved_tokens
);
652 /* Returns nonzero if debugging information should be output. */
655 cp_lexer_debugging_p (cp_lexer
*lexer
)
657 return lexer
->debugging_p
;
661 static inline cp_token_position
662 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
664 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
666 return lexer
->next_token
- previous_p
;
669 static inline cp_token
*
670 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
676 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
678 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
681 static inline cp_token_position
682 cp_lexer_previous_token_position (cp_lexer
*lexer
)
684 if (lexer
->next_token
== &eof_token
)
685 return lexer
->last_token
- 1;
687 return cp_lexer_token_position (lexer
, true);
690 static inline cp_token
*
691 cp_lexer_previous_token (cp_lexer
*lexer
)
693 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
695 return cp_lexer_token_at (lexer
, tp
);
698 /* nonzero if we are presently saving tokens. */
701 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
703 return VEC_length (cp_token_position
, lexer
->saved_tokens
) != 0;
706 /* Store the next token from the preprocessor in *TOKEN. Return true
707 if we reach EOF. If LEXER is NULL, assume we are handling an
708 initial #pragma pch_preprocess, and thus want the lexer to return
709 processed strings. */
712 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
714 static int is_extern_c
= 0;
716 /* Get a new token from the preprocessor. */
718 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
719 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
720 token
->keyword
= RID_MAX
;
721 token
->pragma_kind
= PRAGMA_NONE
;
722 token
->purged_p
= false;
724 /* On some systems, some header files are surrounded by an
725 implicit extern "C" block. Set a flag in the token if it
726 comes from such a header. */
727 is_extern_c
+= pending_lang_change
;
728 pending_lang_change
= 0;
729 token
->implicit_extern_c
= is_extern_c
> 0;
731 /* Check to see if this token is a keyword. */
732 if (token
->type
== CPP_NAME
)
734 if (C_IS_RESERVED_WORD (token
->u
.value
))
736 /* Mark this token as a keyword. */
737 token
->type
= CPP_KEYWORD
;
738 /* Record which keyword. */
739 token
->keyword
= C_RID_CODE (token
->u
.value
);
743 if (warn_cxx0x_compat
744 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
745 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
747 /* Warn about the C++0x keyword (but still treat it as
749 warning (OPT_Wc__0x_compat
,
750 "identifier %qE is a keyword in C++11",
753 /* Clear out the C_RID_CODE so we don't warn about this
754 particular identifier-turned-keyword again. */
755 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
758 token
->ambiguous_p
= false;
759 token
->keyword
= RID_MAX
;
762 else if (token
->type
== CPP_AT_NAME
)
764 /* This only happens in Objective-C++; it must be a keyword. */
765 token
->type
= CPP_KEYWORD
;
766 switch (C_RID_CODE (token
->u
.value
))
768 /* Replace 'class' with '@class', 'private' with '@private',
769 etc. This prevents confusion with the C++ keyword
770 'class', and makes the tokens consistent with other
771 Objective-C 'AT' keywords. For example '@class' is
772 reported as RID_AT_CLASS which is consistent with
773 '@synchronized', which is reported as
776 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
777 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
778 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
779 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
780 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
781 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
782 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
783 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
786 else if (token
->type
== CPP_PRAGMA
)
788 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
789 token
->pragma_kind
= ((enum pragma_kind
)
790 TREE_INT_CST_LOW (token
->u
.value
));
791 token
->u
.value
= NULL_TREE
;
795 /* Update the globals input_location and the input file stack from TOKEN. */
797 cp_lexer_set_source_position_from_token (cp_token
*token
)
799 if (token
->type
!= CPP_EOF
)
801 input_location
= token
->location
;
805 /* Return a pointer to the next token in the token stream, but do not
808 static inline cp_token
*
809 cp_lexer_peek_token (cp_lexer
*lexer
)
811 if (cp_lexer_debugging_p (lexer
))
813 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
814 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
815 putc ('\n', cp_lexer_debug_stream
);
817 return lexer
->next_token
;
820 /* Return true if the next token has the indicated TYPE. */
823 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
825 return cp_lexer_peek_token (lexer
)->type
== type
;
828 /* Return true if the next token does not have the indicated TYPE. */
831 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
833 return !cp_lexer_next_token_is (lexer
, type
);
836 /* Return true if the next token is the indicated KEYWORD. */
839 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
841 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
844 /* Return true if the next token is not the indicated KEYWORD. */
847 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
849 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
852 /* Return true if the next token is a keyword for a decl-specifier. */
855 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
859 token
= cp_lexer_peek_token (lexer
);
860 switch (token
->keyword
)
862 /* auto specifier: storage-class-specifier in C++,
863 simple-type-specifier in C++0x. */
865 /* Storage classes. */
871 /* Elaborated type specifiers. */
877 /* Simple type specifiers. */
892 /* GNU extensions. */
895 /* C++0x extensions. */
897 case RID_UNDERLYING_TYPE
:
905 /* Returns TRUE iff the token T begins a decltype type. */
908 token_is_decltype (cp_token
*t
)
910 return (t
->keyword
== RID_DECLTYPE
911 || t
->type
== CPP_DECLTYPE
);
914 /* Returns TRUE iff the next token begins a decltype type. */
917 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
919 cp_token
*t
= cp_lexer_peek_token (lexer
);
920 return token_is_decltype (t
);
923 /* Return a pointer to the Nth token in the token stream. If N is 1,
924 then this is precisely equivalent to cp_lexer_peek_token (except
925 that it is not inline). One would like to disallow that case, but
926 there is one case (cp_parser_nth_token_starts_template_id) where
927 the caller passes a variable for N and it might be 1. */
930 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
934 /* N is 1-based, not zero-based. */
937 if (cp_lexer_debugging_p (lexer
))
938 fprintf (cp_lexer_debug_stream
,
939 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
942 token
= lexer
->next_token
;
943 gcc_assert (!n
|| token
!= &eof_token
);
947 if (token
== lexer
->last_token
)
953 if (!token
->purged_p
)
957 if (cp_lexer_debugging_p (lexer
))
959 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
960 putc ('\n', cp_lexer_debug_stream
);
966 /* Return the next token, and advance the lexer's next_token pointer
967 to point to the next non-purged token. */
970 cp_lexer_consume_token (cp_lexer
* lexer
)
972 cp_token
*token
= lexer
->next_token
;
974 gcc_assert (token
!= &eof_token
);
975 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
980 if (lexer
->next_token
== lexer
->last_token
)
982 lexer
->next_token
= &eof_token
;
987 while (lexer
->next_token
->purged_p
);
989 cp_lexer_set_source_position_from_token (token
);
991 /* Provide debugging output. */
992 if (cp_lexer_debugging_p (lexer
))
994 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
995 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
996 putc ('\n', cp_lexer_debug_stream
);
1002 /* Permanently remove the next token from the token stream, and
1003 advance the next_token pointer to refer to the next non-purged
1007 cp_lexer_purge_token (cp_lexer
*lexer
)
1009 cp_token
*tok
= lexer
->next_token
;
1011 gcc_assert (tok
!= &eof_token
);
1012 tok
->purged_p
= true;
1013 tok
->location
= UNKNOWN_LOCATION
;
1014 tok
->u
.value
= NULL_TREE
;
1015 tok
->keyword
= RID_MAX
;
1020 if (tok
== lexer
->last_token
)
1026 while (tok
->purged_p
);
1027 lexer
->next_token
= tok
;
1030 /* Permanently remove all tokens after TOK, up to, but not
1031 including, the token that will be returned next by
1032 cp_lexer_peek_token. */
1035 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1037 cp_token
*peek
= lexer
->next_token
;
1039 if (peek
== &eof_token
)
1040 peek
= lexer
->last_token
;
1042 gcc_assert (tok
< peek
);
1044 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1046 tok
->purged_p
= true;
1047 tok
->location
= UNKNOWN_LOCATION
;
1048 tok
->u
.value
= NULL_TREE
;
1049 tok
->keyword
= RID_MAX
;
1053 /* Begin saving tokens. All tokens consumed after this point will be
1057 cp_lexer_save_tokens (cp_lexer
* lexer
)
1059 /* Provide debugging output. */
1060 if (cp_lexer_debugging_p (lexer
))
1061 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1063 VEC_safe_push (cp_token_position
, heap
,
1064 lexer
->saved_tokens
, lexer
->next_token
);
1067 /* Commit to the portion of the token stream most recently saved. */
1070 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1072 /* Provide debugging output. */
1073 if (cp_lexer_debugging_p (lexer
))
1074 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1076 VEC_pop (cp_token_position
, lexer
->saved_tokens
);
1079 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1080 to the token stream. Stop saving tokens. */
1083 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1085 /* Provide debugging output. */
1086 if (cp_lexer_debugging_p (lexer
))
1087 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1089 lexer
->next_token
= VEC_pop (cp_token_position
, lexer
->saved_tokens
);
1092 /* Print a representation of the TOKEN on the STREAM. */
1095 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1097 /* We don't use cpp_type2name here because the parser defines
1098 a few tokens of its own. */
1099 static const char *const token_names
[] = {
1100 /* cpplib-defined token types */
1101 #define OP(e, s) #e,
1102 #define TK(e, s) #e,
1106 /* C++ parser token types - see "Manifest constants", above. */
1109 "NESTED_NAME_SPECIFIER",
1112 /* For some tokens, print the associated data. */
1113 switch (token
->type
)
1116 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1117 For example, `struct' is mapped to an INTEGER_CST. */
1118 if (TREE_CODE (token
->u
.value
) != IDENTIFIER_NODE
)
1120 /* else fall through */
1122 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1129 case CPP_UTF8STRING
:
1130 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1134 print_generic_expr (stream
, token
->u
.value
, 0);
1138 /* If we have a name for the token, print it out. Otherwise, we
1139 simply give the numeric code. */
1140 if (token
->type
< ARRAY_SIZE(token_names
))
1141 fputs (token_names
[token
->type
], stream
);
1143 fprintf (stream
, "[%d]", token
->type
);
1148 /* Start emitting debugging information. */
1151 cp_lexer_start_debugging (cp_lexer
* lexer
)
1153 lexer
->debugging_p
= true;
1154 cp_lexer_debug_stream
= stderr
;
1157 /* Stop emitting debugging information. */
1160 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1162 lexer
->debugging_p
= false;
1163 cp_lexer_debug_stream
= NULL
;
1166 /* Create a new cp_token_cache, representing a range of tokens. */
1168 static cp_token_cache
*
1169 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1171 cp_token_cache
*cache
= ggc_alloc_cp_token_cache ();
1172 cache
->first
= first
;
1178 /* Decl-specifiers. */
1180 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1183 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1185 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1190 /* Nothing other than the parser should be creating declarators;
1191 declarators are a semi-syntactic representation of C++ entities.
1192 Other parts of the front end that need to create entities (like
1193 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1195 static cp_declarator
*make_call_declarator
1196 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, tree
, tree
);
1197 static cp_declarator
*make_array_declarator
1198 (cp_declarator
*, tree
);
1199 static cp_declarator
*make_pointer_declarator
1200 (cp_cv_quals
, cp_declarator
*, tree
);
1201 static cp_declarator
*make_reference_declarator
1202 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1203 static cp_parameter_declarator
*make_parameter_declarator
1204 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1205 static cp_declarator
*make_ptrmem_declarator
1206 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1208 /* An erroneous declarator. */
1209 static cp_declarator
*cp_error_declarator
;
1211 /* The obstack on which declarators and related data structures are
1213 static struct obstack declarator_obstack
;
1215 /* Alloc BYTES from the declarator memory pool. */
1217 static inline void *
1218 alloc_declarator (size_t bytes
)
1220 return obstack_alloc (&declarator_obstack
, bytes
);
1223 /* Allocate a declarator of the indicated KIND. Clear fields that are
1224 common to all declarators. */
1226 static cp_declarator
*
1227 make_declarator (cp_declarator_kind kind
)
1229 cp_declarator
*declarator
;
1231 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1232 declarator
->kind
= kind
;
1233 declarator
->attributes
= NULL_TREE
;
1234 declarator
->std_attributes
= NULL_TREE
;
1235 declarator
->declarator
= NULL
;
1236 declarator
->parameter_pack_p
= false;
1237 declarator
->id_loc
= UNKNOWN_LOCATION
;
1242 /* Make a declarator for a generalized identifier. If
1243 QUALIFYING_SCOPE is non-NULL, the identifier is
1244 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1245 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1248 static cp_declarator
*
1249 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1250 special_function_kind sfk
)
1252 cp_declarator
*declarator
;
1254 /* It is valid to write:
1256 class C { void f(); };
1260 The standard is not clear about whether `typedef const C D' is
1261 legal; as of 2002-09-15 the committee is considering that
1262 question. EDG 3.0 allows that syntax. Therefore, we do as
1264 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1265 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1267 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
1268 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1269 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1271 declarator
= make_declarator (cdk_id
);
1272 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1273 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1274 declarator
->u
.id
.sfk
= sfk
;
1279 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1280 of modifiers such as const or volatile to apply to the pointer
1281 type, represented as identifiers. ATTRIBUTES represent the attributes that
1282 appertain to the pointer or reference. */
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1288 cp_declarator
*declarator
;
1290 declarator
= make_declarator (cdk_pointer
);
1291 declarator
->declarator
= target
;
1292 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1293 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1296 declarator
->id_loc
= target
->id_loc
;
1297 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1298 target
->parameter_pack_p
= false;
1301 declarator
->parameter_pack_p
= false;
1303 declarator
->std_attributes
= attributes
;
1308 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1309 represent the attributes that appertain to the pointer or
1313 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1314 bool rvalue_ref
, tree attributes
)
1316 cp_declarator
*declarator
;
1318 declarator
= make_declarator (cdk_reference
);
1319 declarator
->declarator
= target
;
1320 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1321 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1324 declarator
->id_loc
= target
->id_loc
;
1325 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1326 target
->parameter_pack_p
= false;
1329 declarator
->parameter_pack_p
= false;
1331 declarator
->std_attributes
= attributes
;
1336 /* Like make_pointer_declarator -- but for a pointer to a non-static
1337 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1338 appertain to the pointer or reference. */
1341 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1342 cp_declarator
*pointee
,
1345 cp_declarator
*declarator
;
1347 declarator
= make_declarator (cdk_ptrmem
);
1348 declarator
->declarator
= pointee
;
1349 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1350 declarator
->u
.pointer
.class_type
= class_type
;
1354 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1355 pointee
->parameter_pack_p
= false;
1358 declarator
->parameter_pack_p
= false;
1360 declarator
->std_attributes
= attributes
;
1365 /* Make a declarator for the function given by TARGET, with the
1366 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1367 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1368 indicates what exceptions can be thrown. */
1371 make_call_declarator (cp_declarator
*target
,
1373 cp_cv_quals cv_qualifiers
,
1374 cp_virt_specifiers virt_specifiers
,
1375 tree exception_specification
,
1376 tree late_return_type
)
1378 cp_declarator
*declarator
;
1380 declarator
= make_declarator (cdk_function
);
1381 declarator
->declarator
= target
;
1382 declarator
->u
.function
.parameters
= parms
;
1383 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1384 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1385 declarator
->u
.function
.exception_specification
= exception_specification
;
1386 declarator
->u
.function
.late_return_type
= late_return_type
;
1389 declarator
->id_loc
= target
->id_loc
;
1390 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1391 target
->parameter_pack_p
= false;
1394 declarator
->parameter_pack_p
= false;
1399 /* Make a declarator for an array of BOUNDS elements, each of which is
1400 defined by ELEMENT. */
1403 make_array_declarator (cp_declarator
*element
, tree bounds
)
1405 cp_declarator
*declarator
;
1407 declarator
= make_declarator (cdk_array
);
1408 declarator
->declarator
= element
;
1409 declarator
->u
.array
.bounds
= bounds
;
1412 declarator
->id_loc
= element
->id_loc
;
1413 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1414 element
->parameter_pack_p
= false;
1417 declarator
->parameter_pack_p
= false;
1422 /* Determine whether the declarator we've seen so far can be a
1423 parameter pack, when followed by an ellipsis. */
1425 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1427 /* Search for a declarator name, or any other declarator that goes
1428 after the point where the ellipsis could appear in a parameter
1429 pack. If we find any of these, then this declarator can not be
1430 made into a parameter pack. */
1432 while (declarator
&& !found
)
1434 switch ((int)declarator
->kind
)
1445 declarator
= declarator
->declarator
;
1453 cp_parameter_declarator
*no_parameters
;
1455 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1456 DECLARATOR and DEFAULT_ARGUMENT. */
1458 cp_parameter_declarator
*
1459 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1460 cp_declarator
*declarator
,
1461 tree default_argument
)
1463 cp_parameter_declarator
*parameter
;
1465 parameter
= ((cp_parameter_declarator
*)
1466 alloc_declarator (sizeof (cp_parameter_declarator
)));
1467 parameter
->next
= NULL
;
1468 if (decl_specifiers
)
1469 parameter
->decl_specifiers
= *decl_specifiers
;
1471 clear_decl_specs (¶meter
->decl_specifiers
);
1472 parameter
->declarator
= declarator
;
1473 parameter
->default_argument
= default_argument
;
1474 parameter
->ellipsis_p
= false;
1479 /* Returns true iff DECLARATOR is a declaration for a function. */
1482 function_declarator_p (const cp_declarator
*declarator
)
1486 if (declarator
->kind
== cdk_function
1487 && declarator
->declarator
->kind
== cdk_id
)
1489 if (declarator
->kind
== cdk_id
1490 || declarator
->kind
== cdk_error
)
1492 declarator
= declarator
->declarator
;
1502 A cp_parser parses the token stream as specified by the C++
1503 grammar. Its job is purely parsing, not semantic analysis. For
1504 example, the parser breaks the token stream into declarators,
1505 expressions, statements, and other similar syntactic constructs.
1506 It does not check that the types of the expressions on either side
1507 of an assignment-statement are compatible, or that a function is
1508 not declared with a parameter of type `void'.
1510 The parser invokes routines elsewhere in the compiler to perform
1511 semantic analysis and to build up the abstract syntax tree for the
1514 The parser (and the template instantiation code, which is, in a
1515 way, a close relative of parsing) are the only parts of the
1516 compiler that should be calling push_scope and pop_scope, or
1517 related functions. The parser (and template instantiation code)
1518 keeps track of what scope is presently active; everything else
1519 should simply honor that. (The code that generates static
1520 initializers may also need to set the scope, in order to check
1521 access control correctly when emitting the initializers.)
1526 The parser is of the standard recursive-descent variety. Upcoming
1527 tokens in the token stream are examined in order to determine which
1528 production to use when parsing a non-terminal. Some C++ constructs
1529 require arbitrary look ahead to disambiguate. For example, it is
1530 impossible, in the general case, to tell whether a statement is an
1531 expression or declaration without scanning the entire statement.
1532 Therefore, the parser is capable of "parsing tentatively." When the
1533 parser is not sure what construct comes next, it enters this mode.
1534 Then, while we attempt to parse the construct, the parser queues up
1535 error messages, rather than issuing them immediately, and saves the
1536 tokens it consumes. If the construct is parsed successfully, the
1537 parser "commits", i.e., it issues any queued error messages and
1538 the tokens that were being preserved are permanently discarded.
1539 If, however, the construct is not parsed successfully, the parser
1540 rolls back its state completely so that it can resume parsing using
1541 a different alternative.
1546 The performance of the parser could probably be improved substantially.
1547 We could often eliminate the need to parse tentatively by looking ahead
1548 a little bit. In some places, this approach might not entirely eliminate
1549 the need to parse tentatively, but it might still speed up the average
1552 /* Flags that are passed to some parsing functions. These values can
1553 be bitwise-ored together. */
1558 CP_PARSER_FLAGS_NONE
= 0x0,
1559 /* The construct is optional. If it is not present, then no error
1560 should be issued. */
1561 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1562 /* When parsing a type-specifier, treat user-defined type-names
1563 as non-type identifiers. */
1564 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1565 /* When parsing a type-specifier, do not try to parse a class-specifier
1566 or enum-specifier. */
1567 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1568 /* When parsing a decl-specifier-seq, only allow type-specifier or
1570 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1573 /* This type is used for parameters and variables which hold
1574 combinations of the above flags. */
1575 typedef int cp_parser_flags
;
1577 /* The different kinds of declarators we want to parse. */
1579 typedef enum cp_parser_declarator_kind
1581 /* We want an abstract declarator. */
1582 CP_PARSER_DECLARATOR_ABSTRACT
,
1583 /* We want a named declarator. */
1584 CP_PARSER_DECLARATOR_NAMED
,
1585 /* We don't mind, but the name must be an unqualified-id. */
1586 CP_PARSER_DECLARATOR_EITHER
1587 } cp_parser_declarator_kind
;
1589 /* The precedence values used to parse binary expressions. The minimum value
1590 of PREC must be 1, because zero is reserved to quickly discriminate
1591 binary operators from other tokens. */
1596 PREC_LOGICAL_OR_EXPRESSION
,
1597 PREC_LOGICAL_AND_EXPRESSION
,
1598 PREC_INCLUSIVE_OR_EXPRESSION
,
1599 PREC_EXCLUSIVE_OR_EXPRESSION
,
1600 PREC_AND_EXPRESSION
,
1601 PREC_EQUALITY_EXPRESSION
,
1602 PREC_RELATIONAL_EXPRESSION
,
1603 PREC_SHIFT_EXPRESSION
,
1604 PREC_ADDITIVE_EXPRESSION
,
1605 PREC_MULTIPLICATIVE_EXPRESSION
,
1607 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1610 /* A mapping from a token type to a corresponding tree node type, with a
1611 precedence value. */
1613 typedef struct cp_parser_binary_operations_map_node
1615 /* The token type. */
1616 enum cpp_ttype token_type
;
1617 /* The corresponding tree code. */
1618 enum tree_code tree_type
;
1619 /* The precedence of this operator. */
1620 enum cp_parser_prec prec
;
1621 } cp_parser_binary_operations_map_node
;
1623 typedef struct cp_parser_expression_stack_entry
1625 /* Left hand side of the binary operation we are currently
1628 /* Original tree code for left hand side, if it was a binary
1629 expression itself (used for -Wparentheses). */
1630 enum tree_code lhs_type
;
1631 /* Tree code for the binary operation we are parsing. */
1632 enum tree_code tree_type
;
1633 /* Precedence of the binary operation we are parsing. */
1634 enum cp_parser_prec prec
;
1635 /* Location of the binary operation we are parsing. */
1637 } cp_parser_expression_stack_entry
;
1639 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1640 entries because precedence levels on the stack are monotonically
1642 typedef struct cp_parser_expression_stack_entry
1643 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1647 /* Constructors and destructors. */
1649 static cp_parser_context
*cp_parser_context_new
1650 (cp_parser_context
*);
1652 /* Class variables. */
1654 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1656 /* The operator-precedence table used by cp_parser_binary_expression.
1657 Transformed into an associative array (binops_by_token) by
1660 static const cp_parser_binary_operations_map_node binops
[] = {
1661 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1662 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1664 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1665 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1666 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1668 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1669 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1671 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1672 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1674 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1675 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1676 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1677 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1679 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1680 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1682 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1684 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1686 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1688 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1690 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1693 /* The same as binops, but initialized by cp_parser_new so that
1694 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1696 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1698 /* Constructors and destructors. */
1700 /* Construct a new context. The context below this one on the stack
1701 is given by NEXT. */
1703 static cp_parser_context
*
1704 cp_parser_context_new (cp_parser_context
* next
)
1706 cp_parser_context
*context
;
1708 /* Allocate the storage. */
1709 if (cp_parser_context_free_list
!= NULL
)
1711 /* Pull the first entry from the free list. */
1712 context
= cp_parser_context_free_list
;
1713 cp_parser_context_free_list
= context
->next
;
1714 memset (context
, 0, sizeof (*context
));
1717 context
= ggc_alloc_cleared_cp_parser_context ();
1719 /* No errors have occurred yet in this context. */
1720 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1721 /* If this is not the bottommost context, copy information that we
1722 need from the previous context. */
1725 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1726 expression, then we are parsing one in this context, too. */
1727 context
->object_type
= next
->object_type
;
1728 /* Thread the stack. */
1729 context
->next
= next
;
1735 /* Managing the unparsed function queues. */
1737 #define unparsed_funs_with_default_args \
1738 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_default_args
1739 #define unparsed_funs_with_definitions \
1740 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_definitions
1741 #define unparsed_nsdmis \
1742 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).nsdmis
1745 push_unparsed_function_queues (cp_parser
*parser
)
1747 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
};
1748 VEC_safe_push (cp_unparsed_functions_entry
, gc
, parser
->unparsed_queues
, e
);
1752 pop_unparsed_function_queues (cp_parser
*parser
)
1754 release_tree_vector (unparsed_funs_with_definitions
);
1755 VEC_pop (cp_unparsed_functions_entry
, parser
->unparsed_queues
);
1760 /* Constructors and destructors. */
1762 static cp_parser
*cp_parser_new
1765 /* Routines to parse various constructs.
1767 Those that return `tree' will return the error_mark_node (rather
1768 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1769 Sometimes, they will return an ordinary node if error-recovery was
1770 attempted, even though a parse error occurred. So, to check
1771 whether or not a parse error occurred, you should always use
1772 cp_parser_error_occurred. If the construct is optional (indicated
1773 either by an `_opt' in the name of the function that does the
1774 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1775 the construct is not present. */
1777 /* Lexical conventions [gram.lex] */
1779 static tree cp_parser_identifier
1781 static tree cp_parser_string_literal
1782 (cp_parser
*, bool, bool);
1783 static tree cp_parser_userdef_char_literal
1785 static tree cp_parser_userdef_string_literal
1787 static tree cp_parser_userdef_numeric_literal
1790 /* Basic concepts [gram.basic] */
1792 static bool cp_parser_translation_unit
1795 /* Expressions [gram.expr] */
1797 static tree cp_parser_primary_expression
1798 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1799 static tree cp_parser_id_expression
1800 (cp_parser
*, bool, bool, bool *, bool, bool);
1801 static tree cp_parser_unqualified_id
1802 (cp_parser
*, bool, bool, bool, bool);
1803 static tree cp_parser_nested_name_specifier_opt
1804 (cp_parser
*, bool, bool, bool, bool);
1805 static tree cp_parser_nested_name_specifier
1806 (cp_parser
*, bool, bool, bool, bool);
1807 static tree cp_parser_qualifying_entity
1808 (cp_parser
*, bool, bool, bool, bool, bool);
1809 static tree cp_parser_postfix_expression
1810 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1811 static tree cp_parser_postfix_open_square_expression
1812 (cp_parser
*, tree
, bool);
1813 static tree cp_parser_postfix_dot_deref_expression
1814 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1815 static VEC(tree
,gc
) *cp_parser_parenthesized_expression_list
1816 (cp_parser
*, int, bool, bool, bool *);
1817 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1818 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1819 static void cp_parser_pseudo_destructor_name
1820 (cp_parser
*, tree
*, tree
*);
1821 static tree cp_parser_unary_expression
1822 (cp_parser
*, bool, bool, cp_id_kind
*);
1823 static enum tree_code cp_parser_unary_operator
1825 static tree cp_parser_new_expression
1827 static VEC(tree
,gc
) *cp_parser_new_placement
1829 static tree cp_parser_new_type_id
1830 (cp_parser
*, tree
*);
1831 static cp_declarator
*cp_parser_new_declarator_opt
1833 static cp_declarator
*cp_parser_direct_new_declarator
1835 static VEC(tree
,gc
) *cp_parser_new_initializer
1837 static tree cp_parser_delete_expression
1839 static tree cp_parser_cast_expression
1840 (cp_parser
*, bool, bool, cp_id_kind
*);
1841 static tree cp_parser_binary_expression
1842 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1843 static tree cp_parser_question_colon_clause
1844 (cp_parser
*, tree
);
1845 static tree cp_parser_assignment_expression
1846 (cp_parser
*, bool, cp_id_kind
*);
1847 static enum tree_code cp_parser_assignment_operator_opt
1849 static tree cp_parser_expression
1850 (cp_parser
*, bool, cp_id_kind
*);
1851 static tree cp_parser_constant_expression
1852 (cp_parser
*, bool, bool *);
1853 static tree cp_parser_builtin_offsetof
1855 static tree cp_parser_lambda_expression
1857 static void cp_parser_lambda_introducer
1858 (cp_parser
*, tree
);
1859 static bool cp_parser_lambda_declarator_opt
1860 (cp_parser
*, tree
);
1861 static void cp_parser_lambda_body
1862 (cp_parser
*, tree
);
1864 /* Statements [gram.stmt.stmt] */
1866 static void cp_parser_statement
1867 (cp_parser
*, tree
, bool, bool *);
1868 static void cp_parser_label_for_labeled_statement
1869 (cp_parser
*, tree
);
1870 static tree cp_parser_expression_statement
1871 (cp_parser
*, tree
);
1872 static tree cp_parser_compound_statement
1873 (cp_parser
*, tree
, bool, bool);
1874 static void cp_parser_statement_seq_opt
1875 (cp_parser
*, tree
);
1876 static tree cp_parser_selection_statement
1877 (cp_parser
*, bool *);
1878 static tree cp_parser_condition
1880 static tree cp_parser_iteration_statement
1882 static bool cp_parser_for_init_statement
1883 (cp_parser
*, tree
*decl
);
1884 static tree cp_parser_for
1886 static tree cp_parser_c_for
1887 (cp_parser
*, tree
, tree
);
1888 static tree cp_parser_range_for
1889 (cp_parser
*, tree
, tree
, tree
);
1890 static void do_range_for_auto_deduction
1892 static tree cp_parser_perform_range_for_lookup
1893 (tree
, tree
*, tree
*);
1894 static tree cp_parser_range_for_member_function
1896 static tree cp_parser_jump_statement
1898 static void cp_parser_declaration_statement
1901 static tree cp_parser_implicitly_scoped_statement
1902 (cp_parser
*, bool *);
1903 static void cp_parser_already_scoped_statement
1906 /* Declarations [gram.dcl.dcl] */
1908 static void cp_parser_declaration_seq_opt
1910 static void cp_parser_declaration
1912 static void cp_parser_block_declaration
1913 (cp_parser
*, bool);
1914 static void cp_parser_simple_declaration
1915 (cp_parser
*, bool, tree
*);
1916 static void cp_parser_decl_specifier_seq
1917 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1918 static tree cp_parser_storage_class_specifier_opt
1920 static tree cp_parser_function_specifier_opt
1921 (cp_parser
*, cp_decl_specifier_seq
*);
1922 static tree cp_parser_type_specifier
1923 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1925 static tree cp_parser_simple_type_specifier
1926 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1927 static tree cp_parser_type_name
1929 static tree cp_parser_nonclass_name
1930 (cp_parser
* parser
);
1931 static tree cp_parser_elaborated_type_specifier
1932 (cp_parser
*, bool, bool);
1933 static tree cp_parser_enum_specifier
1935 static void cp_parser_enumerator_list
1936 (cp_parser
*, tree
);
1937 static void cp_parser_enumerator_definition
1938 (cp_parser
*, tree
);
1939 static tree cp_parser_namespace_name
1941 static void cp_parser_namespace_definition
1943 static void cp_parser_namespace_body
1945 static tree cp_parser_qualified_namespace_specifier
1947 static void cp_parser_namespace_alias_definition
1949 static bool cp_parser_using_declaration
1950 (cp_parser
*, bool);
1951 static void cp_parser_using_directive
1953 static tree cp_parser_alias_declaration
1955 static void cp_parser_asm_definition
1957 static void cp_parser_linkage_specification
1959 static void cp_parser_static_assert
1960 (cp_parser
*, bool);
1961 static tree cp_parser_decltype
1964 /* Declarators [gram.dcl.decl] */
1966 static tree cp_parser_init_declarator
1967 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *, tree
*);
1968 static cp_declarator
*cp_parser_declarator
1969 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1970 static cp_declarator
*cp_parser_direct_declarator
1971 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1972 static enum tree_code cp_parser_ptr_operator
1973 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
1974 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1976 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1978 static tree cp_parser_late_return_type_opt
1979 (cp_parser
*, cp_cv_quals
);
1980 static tree cp_parser_declarator_id
1981 (cp_parser
*, bool);
1982 static tree cp_parser_type_id
1984 static tree cp_parser_template_type_arg
1986 static tree
cp_parser_trailing_type_id (cp_parser
*);
1987 static tree cp_parser_type_id_1
1988 (cp_parser
*, bool, bool);
1989 static void cp_parser_type_specifier_seq
1990 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
1991 static tree cp_parser_parameter_declaration_clause
1993 static tree cp_parser_parameter_declaration_list
1994 (cp_parser
*, bool *);
1995 static cp_parameter_declarator
*cp_parser_parameter_declaration
1996 (cp_parser
*, bool, bool *);
1997 static tree cp_parser_default_argument
1998 (cp_parser
*, bool);
1999 static void cp_parser_function_body
2000 (cp_parser
*, bool);
2001 static tree cp_parser_initializer
2002 (cp_parser
*, bool *, bool *);
2003 static tree cp_parser_initializer_clause
2004 (cp_parser
*, bool *);
2005 static tree cp_parser_braced_list
2006 (cp_parser
*, bool*);
2007 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
2008 (cp_parser
*, bool *);
2010 static bool cp_parser_ctor_initializer_opt_and_function_body
2011 (cp_parser
*, bool);
2013 /* Classes [gram.class] */
2015 static tree cp_parser_class_name
2016 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2017 static tree cp_parser_class_specifier
2019 static tree cp_parser_class_head
2020 (cp_parser
*, bool *);
2021 static enum tag_types cp_parser_class_key
2023 static void cp_parser_member_specification_opt
2025 static void cp_parser_member_declaration
2027 static tree cp_parser_pure_specifier
2029 static tree cp_parser_constant_initializer
2032 /* Derived classes [gram.class.derived] */
2034 static tree cp_parser_base_clause
2036 static tree cp_parser_base_specifier
2039 /* Special member functions [gram.special] */
2041 static tree cp_parser_conversion_function_id
2043 static tree cp_parser_conversion_type_id
2045 static cp_declarator
*cp_parser_conversion_declarator_opt
2047 static bool cp_parser_ctor_initializer_opt
2049 static void cp_parser_mem_initializer_list
2051 static tree cp_parser_mem_initializer
2053 static tree cp_parser_mem_initializer_id
2056 /* Overloading [gram.over] */
2058 static tree cp_parser_operator_function_id
2060 static tree cp_parser_operator
2063 /* Templates [gram.temp] */
2065 static void cp_parser_template_declaration
2066 (cp_parser
*, bool);
2067 static tree cp_parser_template_parameter_list
2069 static tree cp_parser_template_parameter
2070 (cp_parser
*, bool *, bool *);
2071 static tree cp_parser_type_parameter
2072 (cp_parser
*, bool *);
2073 static tree cp_parser_template_id
2074 (cp_parser
*, bool, bool, enum tag_types
, bool);
2075 static tree cp_parser_template_name
2076 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2077 static tree cp_parser_template_argument_list
2079 static tree cp_parser_template_argument
2081 static void cp_parser_explicit_instantiation
2083 static void cp_parser_explicit_specialization
2086 /* Exception handling [gram.exception] */
2088 static tree cp_parser_try_block
2090 static bool cp_parser_function_try_block
2092 static void cp_parser_handler_seq
2094 static void cp_parser_handler
2096 static tree cp_parser_exception_declaration
2098 static tree cp_parser_throw_expression
2100 static tree cp_parser_exception_specification_opt
2102 static tree cp_parser_type_id_list
2105 /* GNU Extensions */
2107 static tree cp_parser_asm_specification_opt
2109 static tree cp_parser_asm_operand_list
2111 static tree cp_parser_asm_clobber_list
2113 static tree cp_parser_asm_label_list
2115 static bool cp_next_tokens_can_be_attribute_p
2117 static bool cp_next_tokens_can_be_gnu_attribute_p
2119 static bool cp_next_tokens_can_be_std_attribute_p
2121 static bool cp_nth_tokens_can_be_std_attribute_p
2122 (cp_parser
*, size_t);
2123 static bool cp_nth_tokens_can_be_gnu_attribute_p
2124 (cp_parser
*, size_t);
2125 static bool cp_nth_tokens_can_be_attribute_p
2126 (cp_parser
*, size_t);
2127 static tree cp_parser_attributes_opt
2129 static tree cp_parser_gnu_attributes_opt
2131 static tree cp_parser_gnu_attribute_list
2133 static tree cp_parser_std_attribute
2135 static tree cp_parser_std_attribute_spec
2137 static tree cp_parser_std_attribute_spec_seq
2139 static bool cp_parser_extension_opt
2140 (cp_parser
*, int *);
2141 static void cp_parser_label_declaration
2144 /* Transactional Memory Extensions */
2146 static tree cp_parser_transaction
2147 (cp_parser
*, enum rid
);
2148 static tree cp_parser_transaction_expression
2149 (cp_parser
*, enum rid
);
2150 static bool cp_parser_function_transaction
2151 (cp_parser
*, enum rid
);
2152 static tree cp_parser_transaction_cancel
2155 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
2156 static bool cp_parser_pragma
2157 (cp_parser
*, enum pragma_context
);
2159 /* Objective-C++ Productions */
2161 static tree cp_parser_objc_message_receiver
2163 static tree cp_parser_objc_message_args
2165 static tree cp_parser_objc_message_expression
2167 static tree cp_parser_objc_encode_expression
2169 static tree cp_parser_objc_defs_expression
2171 static tree cp_parser_objc_protocol_expression
2173 static tree cp_parser_objc_selector_expression
2175 static tree cp_parser_objc_expression
2177 static bool cp_parser_objc_selector_p
2179 static tree cp_parser_objc_selector
2181 static tree cp_parser_objc_protocol_refs_opt
2183 static void cp_parser_objc_declaration
2184 (cp_parser
*, tree
);
2185 static tree cp_parser_objc_statement
2187 static bool cp_parser_objc_valid_prefix_attributes
2188 (cp_parser
*, tree
*);
2189 static void cp_parser_objc_at_property_declaration
2191 static void cp_parser_objc_at_synthesize_declaration
2193 static void cp_parser_objc_at_dynamic_declaration
2195 static tree cp_parser_objc_struct_declaration
2198 /* Utility Routines */
2200 static tree cp_parser_lookup_name
2201 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2202 static tree cp_parser_lookup_name_simple
2203 (cp_parser
*, tree
, location_t
);
2204 static tree cp_parser_maybe_treat_template_as_class
2206 static bool cp_parser_check_declarator_template_parameters
2207 (cp_parser
*, cp_declarator
*, location_t
);
2208 static bool cp_parser_check_template_parameters
2209 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2210 static tree cp_parser_simple_cast_expression
2212 static tree cp_parser_global_scope_opt
2213 (cp_parser
*, bool);
2214 static bool cp_parser_constructor_declarator_p
2215 (cp_parser
*, bool);
2216 static tree cp_parser_function_definition_from_specifiers_and_declarator
2217 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2218 static tree cp_parser_function_definition_after_declarator
2219 (cp_parser
*, bool);
2220 static void cp_parser_template_declaration_after_export
2221 (cp_parser
*, bool);
2222 static void cp_parser_perform_template_parameter_access_checks
2223 (VEC (deferred_access_check
,gc
)*);
2224 static tree cp_parser_single_declaration
2225 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
2226 static tree cp_parser_functional_cast
2227 (cp_parser
*, tree
);
2228 static tree cp_parser_save_member_function_body
2229 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2230 static tree cp_parser_save_nsdmi
2232 static tree cp_parser_enclosed_template_argument_list
2234 static void cp_parser_save_default_args
2235 (cp_parser
*, tree
);
2236 static void cp_parser_late_parsing_for_member
2237 (cp_parser
*, tree
);
2238 static tree cp_parser_late_parse_one_default_arg
2239 (cp_parser
*, tree
, tree
, tree
);
2240 static void cp_parser_late_parsing_nsdmi
2241 (cp_parser
*, tree
);
2242 static void cp_parser_late_parsing_default_args
2243 (cp_parser
*, tree
);
2244 static tree cp_parser_sizeof_operand
2245 (cp_parser
*, enum rid
);
2246 static tree cp_parser_trait_expr
2247 (cp_parser
*, enum rid
);
2248 static bool cp_parser_declares_only_class_p
2250 static void cp_parser_set_storage_class
2251 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2252 static void cp_parser_set_decl_spec_type
2253 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2254 static void set_and_check_decl_spec_loc
2255 (cp_decl_specifier_seq
*decl_specs
,
2256 cp_decl_spec ds
, cp_token
*);
2257 static bool cp_parser_friend_p
2258 (const cp_decl_specifier_seq
*);
2259 static void cp_parser_required_error
2260 (cp_parser
*, required_token
, bool);
2261 static cp_token
*cp_parser_require
2262 (cp_parser
*, enum cpp_ttype
, required_token
);
2263 static cp_token
*cp_parser_require_keyword
2264 (cp_parser
*, enum rid
, required_token
);
2265 static bool cp_parser_token_starts_function_definition_p
2267 static bool cp_parser_next_token_starts_class_definition_p
2269 static bool cp_parser_next_token_ends_template_argument_p
2271 static bool cp_parser_nth_token_starts_template_argument_list_p
2272 (cp_parser
*, size_t);
2273 static enum tag_types cp_parser_token_is_class_key
2275 static void cp_parser_check_class_key
2276 (enum tag_types
, tree type
);
2277 static void cp_parser_check_access_in_redeclaration
2278 (tree type
, location_t location
);
2279 static bool cp_parser_optional_template_keyword
2281 static void cp_parser_pre_parsed_nested_name_specifier
2283 static bool cp_parser_cache_group
2284 (cp_parser
*, enum cpp_ttype
, unsigned);
2285 static tree cp_parser_cache_defarg
2286 (cp_parser
*parser
, bool nsdmi
);
2287 static void cp_parser_parse_tentatively
2289 static void cp_parser_commit_to_tentative_parse
2291 static void cp_parser_abort_tentative_parse
2293 static bool cp_parser_parse_definitely
2295 static inline bool cp_parser_parsing_tentatively
2297 static bool cp_parser_uncommitted_to_tentative_parse_p
2299 static void cp_parser_error
2300 (cp_parser
*, const char *);
2301 static void cp_parser_name_lookup_error
2302 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2303 static bool cp_parser_simulate_error
2305 static bool cp_parser_check_type_definition
2307 static void cp_parser_check_for_definition_in_return_type
2308 (cp_declarator
*, tree
, location_t type_location
);
2309 static void cp_parser_check_for_invalid_template_id
2310 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2311 static bool cp_parser_non_integral_constant_expression
2312 (cp_parser
*, non_integral_constant
);
2313 static void cp_parser_diagnose_invalid_type_name
2314 (cp_parser
*, tree
, tree
, location_t
);
2315 static bool cp_parser_parse_and_diagnose_invalid_type_name
2317 static int cp_parser_skip_to_closing_parenthesis
2318 (cp_parser
*, bool, bool, bool);
2319 static void cp_parser_skip_to_end_of_statement
2321 static void cp_parser_consume_semicolon_at_end_of_statement
2323 static void cp_parser_skip_to_end_of_block_or_statement
2325 static bool cp_parser_skip_to_closing_brace
2327 static void cp_parser_skip_to_end_of_template_parameter_list
2329 static void cp_parser_skip_to_pragma_eol
2330 (cp_parser
*, cp_token
*);
2331 static bool cp_parser_error_occurred
2333 static bool cp_parser_allow_gnu_extensions_p
2335 static bool cp_parser_is_pure_string_literal
2337 static bool cp_parser_is_string_literal
2339 static bool cp_parser_is_keyword
2340 (cp_token
*, enum rid
);
2341 static tree cp_parser_make_typename_type
2342 (cp_parser
*, tree
, tree
, location_t location
);
2343 static cp_declarator
* cp_parser_make_indirect_declarator
2344 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2346 /* Returns nonzero if we are parsing tentatively. */
2349 cp_parser_parsing_tentatively (cp_parser
* parser
)
2351 return parser
->context
->next
!= NULL
;
2354 /* Returns nonzero if TOKEN is a string literal. */
2357 cp_parser_is_pure_string_literal (cp_token
* token
)
2359 return (token
->type
== CPP_STRING
||
2360 token
->type
== CPP_STRING16
||
2361 token
->type
== CPP_STRING32
||
2362 token
->type
== CPP_WSTRING
||
2363 token
->type
== CPP_UTF8STRING
);
2366 /* Returns nonzero if TOKEN is a string literal
2367 of a user-defined string literal. */
2370 cp_parser_is_string_literal (cp_token
* token
)
2372 return (cp_parser_is_pure_string_literal (token
) ||
2373 token
->type
== CPP_STRING_USERDEF
||
2374 token
->type
== CPP_STRING16_USERDEF
||
2375 token
->type
== CPP_STRING32_USERDEF
||
2376 token
->type
== CPP_WSTRING_USERDEF
||
2377 token
->type
== CPP_UTF8STRING_USERDEF
);
2380 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2383 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2385 return token
->keyword
== keyword
;
2388 /* If not parsing tentatively, issue a diagnostic of the form
2389 FILE:LINE: MESSAGE before TOKEN
2390 where TOKEN is the next token in the input stream. MESSAGE
2391 (specified by the caller) is usually of the form "expected
2395 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2397 if (!cp_parser_simulate_error (parser
))
2399 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2400 /* This diagnostic makes more sense if it is tagged to the line
2401 of the token we just peeked at. */
2402 cp_lexer_set_source_position_from_token (token
);
2404 if (token
->type
== CPP_PRAGMA
)
2406 error_at (token
->location
,
2407 "%<#pragma%> is not allowed here");
2408 cp_parser_skip_to_pragma_eol (parser
, token
);
2412 c_parse_error (gmsgid
,
2413 /* Because c_parser_error does not understand
2414 CPP_KEYWORD, keywords are treated like
2416 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2417 token
->u
.value
, token
->flags
);
2421 /* Issue an error about name-lookup failing. NAME is the
2422 IDENTIFIER_NODE DECL is the result of
2423 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2424 the thing that we hoped to find. */
2427 cp_parser_name_lookup_error (cp_parser
* parser
,
2430 name_lookup_error desired
,
2431 location_t location
)
2433 /* If name lookup completely failed, tell the user that NAME was not
2435 if (decl
== error_mark_node
)
2437 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2438 error_at (location
, "%<%E::%E%> has not been declared",
2439 parser
->scope
, name
);
2440 else if (parser
->scope
== global_namespace
)
2441 error_at (location
, "%<::%E%> has not been declared", name
);
2442 else if (parser
->object_scope
2443 && !CLASS_TYPE_P (parser
->object_scope
))
2444 error_at (location
, "request for member %qE in non-class type %qT",
2445 name
, parser
->object_scope
);
2446 else if (parser
->object_scope
)
2447 error_at (location
, "%<%T::%E%> has not been declared",
2448 parser
->object_scope
, name
);
2450 error_at (location
, "%qE has not been declared", name
);
2452 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2457 error_at (location
, "%<%E::%E%> is not a type",
2458 parser
->scope
, name
);
2461 error_at (location
, "%<%E::%E%> is not a class or namespace",
2462 parser
->scope
, name
);
2466 "%<%E::%E%> is not a class, namespace, or enumeration",
2467 parser
->scope
, name
);
2474 else if (parser
->scope
== global_namespace
)
2479 error_at (location
, "%<::%E%> is not a type", name
);
2482 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2486 "%<::%E%> is not a class, namespace, or enumeration",
2498 error_at (location
, "%qE is not a type", name
);
2501 error_at (location
, "%qE is not a class or namespace", name
);
2505 "%qE is not a class, namespace, or enumeration", name
);
2513 /* If we are parsing tentatively, remember that an error has occurred
2514 during this tentative parse. Returns true if the error was
2515 simulated; false if a message should be issued by the caller. */
2518 cp_parser_simulate_error (cp_parser
* parser
)
2520 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2522 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2528 /* This function is called when a type is defined. If type
2529 definitions are forbidden at this point, an error message is
2533 cp_parser_check_type_definition (cp_parser
* parser
)
2535 /* If types are forbidden here, issue a message. */
2536 if (parser
->type_definition_forbidden_message
)
2538 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2539 in the message need to be interpreted. */
2540 error (parser
->type_definition_forbidden_message
);
2546 /* This function is called when the DECLARATOR is processed. The TYPE
2547 was a type defined in the decl-specifiers. If it is invalid to
2548 define a type in the decl-specifiers for DECLARATOR, an error is
2549 issued. TYPE_LOCATION is the location of TYPE and is used
2550 for error reporting. */
2553 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2554 tree type
, location_t type_location
)
2556 /* [dcl.fct] forbids type definitions in return types.
2557 Unfortunately, it's not easy to know whether or not we are
2558 processing a return type until after the fact. */
2560 && (declarator
->kind
== cdk_pointer
2561 || declarator
->kind
== cdk_reference
2562 || declarator
->kind
== cdk_ptrmem
))
2563 declarator
= declarator
->declarator
;
2565 && declarator
->kind
== cdk_function
)
2567 error_at (type_location
,
2568 "new types may not be defined in a return type");
2569 inform (type_location
,
2570 "(perhaps a semicolon is missing after the definition of %qT)",
2575 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2576 "<" in any valid C++ program. If the next token is indeed "<",
2577 issue a message warning the user about what appears to be an
2578 invalid attempt to form a template-id. LOCATION is the location
2579 of the type-specifier (TYPE) */
2582 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2584 enum tag_types tag_type
,
2585 location_t location
)
2587 cp_token_position start
= 0;
2589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2592 error_at (location
, "%qT is not a template", type
);
2593 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2595 if (tag_type
!= none_type
)
2596 error_at (location
, "%qE is not a class template", type
);
2598 error_at (location
, "%qE is not a template", type
);
2601 error_at (location
, "invalid template-id");
2602 /* Remember the location of the invalid "<". */
2603 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2604 start
= cp_lexer_token_position (parser
->lexer
, true);
2605 /* Consume the "<". */
2606 cp_lexer_consume_token (parser
->lexer
);
2607 /* Parse the template arguments. */
2608 cp_parser_enclosed_template_argument_list (parser
);
2609 /* Permanently remove the invalid template arguments so that
2610 this error message is not issued again. */
2612 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2616 /* If parsing an integral constant-expression, issue an error message
2617 about the fact that THING appeared and return true. Otherwise,
2618 return false. In either case, set
2619 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2622 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2623 non_integral_constant thing
)
2625 parser
->non_integral_constant_expression_p
= true;
2626 if (parser
->integral_constant_expression_p
)
2628 if (!parser
->allow_non_integral_constant_expression_p
)
2630 const char *msg
= NULL
;
2634 error ("floating-point literal "
2635 "cannot appear in a constant-expression");
2638 error ("a cast to a type other than an integral or "
2639 "enumeration type cannot appear in a "
2640 "constant-expression");
2643 error ("%<typeid%> operator "
2644 "cannot appear in a constant-expression");
2647 error ("non-constant compound literals "
2648 "cannot appear in a constant-expression");
2651 error ("a function call "
2652 "cannot appear in a constant-expression");
2655 error ("an increment "
2656 "cannot appear in a constant-expression");
2659 error ("an decrement "
2660 "cannot appear in a constant-expression");
2663 error ("an array reference "
2664 "cannot appear in a constant-expression");
2666 case NIC_ADDR_LABEL
:
2667 error ("the address of a label "
2668 "cannot appear in a constant-expression");
2670 case NIC_OVERLOADED
:
2671 error ("calls to overloaded operators "
2672 "cannot appear in a constant-expression");
2674 case NIC_ASSIGNMENT
:
2675 error ("an assignment cannot appear in a constant-expression");
2678 error ("a comma operator "
2679 "cannot appear in a constant-expression");
2681 case NIC_CONSTRUCTOR
:
2682 error ("a call to a constructor "
2683 "cannot appear in a constant-expression");
2685 case NIC_TRANSACTION
:
2686 error ("a transaction expression "
2687 "cannot appear in a constant-expression");
2693 msg
= "__FUNCTION__";
2695 case NIC_PRETTY_FUNC
:
2696 msg
= "__PRETTY_FUNCTION__";
2716 case NIC_PREINCREMENT
:
2719 case NIC_PREDECREMENT
:
2732 error ("%qs cannot appear in a constant-expression", msg
);
2739 /* Emit a diagnostic for an invalid type name. SCOPE is the
2740 qualifying scope (or NULL, if none) for ID. This function commits
2741 to the current active tentative parse, if any. (Otherwise, the
2742 problematic construct might be encountered again later, resulting
2743 in duplicate error messages.) LOCATION is the location of ID. */
2746 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2747 tree scope
, tree id
,
2748 location_t location
)
2750 tree decl
, old_scope
;
2751 cp_parser_commit_to_tentative_parse (parser
);
2752 /* Try to lookup the identifier. */
2753 old_scope
= parser
->scope
;
2754 parser
->scope
= scope
;
2755 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2756 parser
->scope
= old_scope
;
2757 /* If the lookup found a template-name, it means that the user forgot
2758 to specify an argument list. Emit a useful error message. */
2759 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2761 "invalid use of template-name %qE without an argument list",
2763 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2764 error_at (location
, "invalid use of destructor %qD as a type", id
);
2765 else if (TREE_CODE (decl
) == TYPE_DECL
)
2766 /* Something like 'unsigned A a;' */
2767 error_at (location
, "invalid combination of multiple type-specifiers");
2768 else if (!parser
->scope
)
2770 /* Issue an error message. */
2771 error_at (location
, "%qE does not name a type", id
);
2772 /* If we're in a template class, it's possible that the user was
2773 referring to a type from a base class. For example:
2775 template <typename T> struct A { typedef T X; };
2776 template <typename T> struct B : public A<T> { X x; };
2778 The user should have said "typename A<T>::X". */
2779 if (cxx_dialect
< cxx0x
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2780 inform (location
, "C++11 %<constexpr%> only available with "
2781 "-std=c++11 or -std=gnu++11");
2782 else if (processing_template_decl
&& current_class_type
2783 && TYPE_BINFO (current_class_type
))
2787 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2791 tree base_type
= BINFO_TYPE (b
);
2792 if (CLASS_TYPE_P (base_type
)
2793 && dependent_type_p (base_type
))
2796 /* Go from a particular instantiation of the
2797 template (which will have an empty TYPE_FIELDs),
2798 to the main version. */
2799 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2800 for (field
= TYPE_FIELDS (base_type
);
2802 field
= DECL_CHAIN (field
))
2803 if (TREE_CODE (field
) == TYPE_DECL
2804 && DECL_NAME (field
) == id
)
2807 "(perhaps %<typename %T::%E%> was intended)",
2808 BINFO_TYPE (b
), id
);
2817 /* Here we diagnose qualified-ids where the scope is actually correct,
2818 but the identifier does not resolve to a valid type name. */
2819 else if (parser
->scope
!= error_mark_node
)
2821 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2822 error_at (location
, "%qE in namespace %qE does not name a type",
2824 else if (CLASS_TYPE_P (parser
->scope
)
2825 && constructor_name_p (id
, parser
->scope
))
2828 error_at (location
, "%<%T::%E%> names the constructor, not"
2829 " the type", parser
->scope
, id
);
2830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2831 error_at (location
, "and %qT has no template constructors",
2834 else if (TYPE_P (parser
->scope
)
2835 && dependent_scope_p (parser
->scope
))
2836 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2837 "%qT is a dependent scope",
2838 parser
->scope
, id
, parser
->scope
);
2839 else if (TYPE_P (parser
->scope
))
2840 error_at (location
, "%qE in %q#T does not name a type",
2847 /* Check for a common situation where a type-name should be present,
2848 but is not, and issue a sensible error message. Returns true if an
2849 invalid type-name was detected.
2851 The situation handled by this function are variable declarations of the
2852 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2853 Usually, `ID' should name a type, but if we got here it means that it
2854 does not. We try to emit the best possible error message depending on
2855 how exactly the id-expression looks like. */
2858 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2861 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2863 /* Avoid duplicate error about ambiguous lookup. */
2864 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2866 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2867 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2871 cp_parser_parse_tentatively (parser
);
2872 id
= cp_parser_id_expression (parser
,
2873 /*template_keyword_p=*/false,
2874 /*check_dependency_p=*/true,
2875 /*template_p=*/NULL
,
2876 /*declarator_p=*/true,
2877 /*optional_p=*/false);
2878 /* If the next token is a (, this is a function with no explicit return
2879 type, i.e. constructor, destructor or conversion op. */
2880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
2881 || TREE_CODE (id
) == TYPE_DECL
)
2883 cp_parser_abort_tentative_parse (parser
);
2886 if (!cp_parser_parse_definitely (parser
))
2889 /* Emit a diagnostic for the invalid type. */
2890 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2891 id
, token
->location
);
2893 /* If we aren't in the middle of a declarator (i.e. in a
2894 parameter-declaration-clause), skip to the end of the declaration;
2895 there's no point in trying to process it. */
2896 if (!parser
->in_declarator_p
)
2897 cp_parser_skip_to_end_of_block_or_statement (parser
);
2901 /* Consume tokens up to, and including, the next non-nested closing `)'.
2902 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2903 are doing error recovery. Returns -1 if OR_COMMA is true and we
2904 found an unnested comma. */
2907 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2912 unsigned paren_depth
= 0;
2913 unsigned brace_depth
= 0;
2914 unsigned square_depth
= 0;
2916 if (recovering
&& !or_comma
2917 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2922 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2924 switch (token
->type
)
2927 case CPP_PRAGMA_EOL
:
2928 /* If we've run out of tokens, then there is no closing `)'. */
2931 /* This is good for lambda expression capture-lists. */
2932 case CPP_OPEN_SQUARE
:
2935 case CPP_CLOSE_SQUARE
:
2936 if (!square_depth
--)
2941 /* This matches the processing in skip_to_end_of_statement. */
2946 case CPP_OPEN_BRACE
:
2949 case CPP_CLOSE_BRACE
:
2955 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
2960 case CPP_OPEN_PAREN
:
2965 case CPP_CLOSE_PAREN
:
2966 if (!brace_depth
&& !paren_depth
--)
2969 cp_lexer_consume_token (parser
->lexer
);
2978 /* Consume the token. */
2979 cp_lexer_consume_token (parser
->lexer
);
2983 /* Consume tokens until we reach the end of the current statement.
2984 Normally, that will be just before consuming a `;'. However, if a
2985 non-nested `}' comes first, then we stop before consuming that. */
2988 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2990 unsigned nesting_depth
= 0;
2994 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2996 switch (token
->type
)
2999 case CPP_PRAGMA_EOL
:
3000 /* If we've run out of tokens, stop. */
3004 /* If the next token is a `;', we have reached the end of the
3010 case CPP_CLOSE_BRACE
:
3011 /* If this is a non-nested '}', stop before consuming it.
3012 That way, when confronted with something like:
3016 we stop before consuming the closing '}', even though we
3017 have not yet reached a `;'. */
3018 if (nesting_depth
== 0)
3021 /* If it is the closing '}' for a block that we have
3022 scanned, stop -- but only after consuming the token.
3028 we will stop after the body of the erroneously declared
3029 function, but before consuming the following `typedef'
3031 if (--nesting_depth
== 0)
3033 cp_lexer_consume_token (parser
->lexer
);
3037 case CPP_OPEN_BRACE
:
3045 /* Consume the token. */
3046 cp_lexer_consume_token (parser
->lexer
);
3050 /* This function is called at the end of a statement or declaration.
3051 If the next token is a semicolon, it is consumed; otherwise, error
3052 recovery is attempted. */
3055 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3057 /* Look for the trailing `;'. */
3058 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3060 /* If there is additional (erroneous) input, skip to the end of
3062 cp_parser_skip_to_end_of_statement (parser
);
3063 /* If the next token is now a `;', consume it. */
3064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3065 cp_lexer_consume_token (parser
->lexer
);
3069 /* Skip tokens until we have consumed an entire block, or until we
3070 have consumed a non-nested `;'. */
3073 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3075 int nesting_depth
= 0;
3077 while (nesting_depth
>= 0)
3079 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3081 switch (token
->type
)
3084 case CPP_PRAGMA_EOL
:
3085 /* If we've run out of tokens, stop. */
3089 /* Stop if this is an unnested ';'. */
3094 case CPP_CLOSE_BRACE
:
3095 /* Stop if this is an unnested '}', or closes the outermost
3098 if (nesting_depth
< 0)
3104 case CPP_OPEN_BRACE
:
3113 /* Consume the token. */
3114 cp_lexer_consume_token (parser
->lexer
);
3118 /* Skip tokens until a non-nested closing curly brace is the next
3119 token, or there are no more tokens. Return true in the first case,
3123 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3125 unsigned nesting_depth
= 0;
3129 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3131 switch (token
->type
)
3134 case CPP_PRAGMA_EOL
:
3135 /* If we've run out of tokens, stop. */
3138 case CPP_CLOSE_BRACE
:
3139 /* If the next token is a non-nested `}', then we have reached
3140 the end of the current block. */
3141 if (nesting_depth
-- == 0)
3145 case CPP_OPEN_BRACE
:
3146 /* If it the next token is a `{', then we are entering a new
3147 block. Consume the entire block. */
3155 /* Consume the token. */
3156 cp_lexer_consume_token (parser
->lexer
);
3160 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3161 parameter is the PRAGMA token, allowing us to purge the entire pragma
3165 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3169 parser
->lexer
->in_pragma
= false;
3172 token
= cp_lexer_consume_token (parser
->lexer
);
3173 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3175 /* Ensure that the pragma is not parsed again. */
3176 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3179 /* Require pragma end of line, resyncing with it as necessary. The
3180 arguments are as for cp_parser_skip_to_pragma_eol. */
3183 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3185 parser
->lexer
->in_pragma
= false;
3186 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3187 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3190 /* This is a simple wrapper around make_typename_type. When the id is
3191 an unresolved identifier node, we can provide a superior diagnostic
3192 using cp_parser_diagnose_invalid_type_name. */
3195 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3196 tree id
, location_t id_location
)
3199 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3201 result
= make_typename_type (scope
, id
, typename_type
,
3202 /*complain=*/tf_none
);
3203 if (result
== error_mark_node
)
3204 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3207 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3210 /* This is a wrapper around the
3211 make_{pointer,ptrmem,reference}_declarator functions that decides
3212 which one to call based on the CODE and CLASS_TYPE arguments. The
3213 CODE argument should be one of the values returned by
3214 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3215 appertain to the pointer or reference. */
3217 static cp_declarator
*
3218 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3219 cp_cv_quals cv_qualifiers
,
3220 cp_declarator
*target
,
3223 if (code
== ERROR_MARK
)
3224 return cp_error_declarator
;
3226 if (code
== INDIRECT_REF
)
3227 if (class_type
== NULL_TREE
)
3228 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3230 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3231 target
, attributes
);
3232 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3233 return make_reference_declarator (cv_qualifiers
, target
,
3235 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3236 return make_reference_declarator (cv_qualifiers
, target
,
3241 /* Create a new C++ parser. */
3244 cp_parser_new (void)
3250 /* cp_lexer_new_main is called before doing GC allocation because
3251 cp_lexer_new_main might load a PCH file. */
3252 lexer
= cp_lexer_new_main ();
3254 /* Initialize the binops_by_token so that we can get the tree
3255 directly from the token. */
3256 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3257 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3259 parser
= ggc_alloc_cleared_cp_parser ();
3260 parser
->lexer
= lexer
;
3261 parser
->context
= cp_parser_context_new (NULL
);
3263 /* For now, we always accept GNU extensions. */
3264 parser
->allow_gnu_extensions_p
= 1;
3266 /* The `>' token is a greater-than operator, not the end of a
3268 parser
->greater_than_is_operator_p
= true;
3270 parser
->default_arg_ok_p
= true;
3272 /* We are not parsing a constant-expression. */
3273 parser
->integral_constant_expression_p
= false;
3274 parser
->allow_non_integral_constant_expression_p
= false;
3275 parser
->non_integral_constant_expression_p
= false;
3277 /* Local variable names are not forbidden. */
3278 parser
->local_variables_forbidden_p
= false;
3280 /* We are not processing an `extern "C"' declaration. */
3281 parser
->in_unbraced_linkage_specification_p
= false;
3283 /* We are not processing a declarator. */
3284 parser
->in_declarator_p
= false;
3286 /* We are not processing a template-argument-list. */
3287 parser
->in_template_argument_list_p
= false;
3289 /* We are not in an iteration statement. */
3290 parser
->in_statement
= 0;
3292 /* We are not in a switch statement. */
3293 parser
->in_switch_statement_p
= false;
3295 /* We are not parsing a type-id inside an expression. */
3296 parser
->in_type_id_in_expr_p
= false;
3298 /* Declarations aren't implicitly extern "C". */
3299 parser
->implicit_extern_c
= false;
3301 /* String literals should be translated to the execution character set. */
3302 parser
->translate_strings_p
= true;
3304 /* We are not parsing a function body. */
3305 parser
->in_function_body
= false;
3307 /* We can correct until told otherwise. */
3308 parser
->colon_corrects_to_scope_p
= true;
3310 /* The unparsed function queue is empty. */
3311 push_unparsed_function_queues (parser
);
3313 /* There are no classes being defined. */
3314 parser
->num_classes_being_defined
= 0;
3316 /* No template parameters apply. */
3317 parser
->num_template_parameter_lists
= 0;
3322 /* Create a cp_lexer structure which will emit the tokens in CACHE
3323 and push it onto the parser's lexer stack. This is used for delayed
3324 parsing of in-class method bodies and default arguments, and should
3325 not be confused with tentative parsing. */
3327 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3329 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3330 lexer
->next
= parser
->lexer
;
3331 parser
->lexer
= lexer
;
3333 /* Move the current source position to that of the first token in the
3335 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3338 /* Pop the top lexer off the parser stack. This is never used for the
3339 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3341 cp_parser_pop_lexer (cp_parser
*parser
)
3343 cp_lexer
*lexer
= parser
->lexer
;
3344 parser
->lexer
= lexer
->next
;
3345 cp_lexer_destroy (lexer
);
3347 /* Put the current source position back where it was before this
3348 lexer was pushed. */
3349 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3352 /* Lexical conventions [gram.lex] */
3354 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3358 cp_parser_identifier (cp_parser
* parser
)
3362 /* Look for the identifier. */
3363 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3364 /* Return the value. */
3365 return token
? token
->u
.value
: error_mark_node
;
3368 /* Parse a sequence of adjacent string constants. Returns a
3369 TREE_STRING representing the combined, nul-terminated string
3370 constant. If TRANSLATE is true, translate the string to the
3371 execution character set. If WIDE_OK is true, a wide string is
3374 C++98 [lex.string] says that if a narrow string literal token is
3375 adjacent to a wide string literal token, the behavior is undefined.
3376 However, C99 6.4.5p4 says that this results in a wide string literal.
3377 We follow C99 here, for consistency with the C front end.
3379 This code is largely lifted from lex_string() in c-lex.c.
3381 FUTURE: ObjC++ will need to handle @-strings here. */
3383 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3387 struct obstack str_ob
;
3388 cpp_string str
, istr
, *strs
;
3390 enum cpp_ttype type
, curr_type
;
3391 int have_suffix_p
= 0;
3393 tree suffix_id
= NULL_TREE
;
3394 bool curr_tok_is_userdef_p
= false;
3396 tok
= cp_lexer_peek_token (parser
->lexer
);
3397 if (!cp_parser_is_string_literal (tok
))
3399 cp_parser_error (parser
, "expected string-literal");
3400 return error_mark_node
;
3403 if (cpp_userdef_string_p (tok
->type
))
3405 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3406 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3407 curr_tok_is_userdef_p
= true;
3411 string_tree
= tok
->u
.value
;
3412 curr_type
= tok
->type
;
3416 /* Try to avoid the overhead of creating and destroying an obstack
3417 for the common case of just one string. */
3418 if (!cp_parser_is_string_literal
3419 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3421 cp_lexer_consume_token (parser
->lexer
);
3423 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3424 str
.len
= TREE_STRING_LENGTH (string_tree
);
3427 if (curr_tok_is_userdef_p
)
3429 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3431 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3434 curr_type
= tok
->type
;
3440 gcc_obstack_init (&str_ob
);
3445 cp_lexer_consume_token (parser
->lexer
);
3447 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3448 str
.len
= TREE_STRING_LENGTH (string_tree
);
3450 if (curr_tok_is_userdef_p
)
3452 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3453 if (have_suffix_p
== 0)
3455 suffix_id
= curr_suffix_id
;
3458 else if (have_suffix_p
== 1
3459 && curr_suffix_id
!= suffix_id
)
3461 error ("inconsistent user-defined literal suffixes"
3462 " %qD and %qD in string literal",
3463 suffix_id
, curr_suffix_id
);
3466 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3469 curr_type
= tok
->type
;
3471 if (type
!= curr_type
)
3473 if (type
== CPP_STRING
)
3475 else if (curr_type
!= CPP_STRING
)
3476 error_at (tok
->location
,
3477 "unsupported non-standard concatenation "
3478 "of string literals");
3481 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3483 tok
= cp_lexer_peek_token (parser
->lexer
);
3484 if (cpp_userdef_string_p (tok
->type
))
3486 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3487 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3488 curr_tok_is_userdef_p
= true;
3492 string_tree
= tok
->u
.value
;
3493 curr_type
= tok
->type
;
3494 curr_tok_is_userdef_p
= false;
3497 while (cp_parser_is_string_literal (tok
));
3499 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3502 if (type
!= CPP_STRING
&& !wide_ok
)
3504 cp_parser_error (parser
, "a wide string is invalid in this context");
3508 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3509 (parse_in
, strs
, count
, &istr
, type
))
3511 value
= build_string (istr
.len
, (const char *)istr
.text
);
3512 free (CONST_CAST (unsigned char *, istr
.text
));
3518 case CPP_UTF8STRING
:
3519 TREE_TYPE (value
) = char_array_type_node
;
3522 TREE_TYPE (value
) = char16_array_type_node
;
3525 TREE_TYPE (value
) = char32_array_type_node
;
3528 TREE_TYPE (value
) = wchar_array_type_node
;
3532 value
= fix_string_type (value
);
3536 tree literal
= build_userdef_literal (suffix_id
, value
, NULL_TREE
);
3537 tok
->u
.value
= literal
;
3538 return cp_parser_userdef_string_literal (tok
);
3542 /* cpp_interpret_string has issued an error. */
3543 value
= error_mark_node
;
3546 obstack_free (&str_ob
, 0);
3551 /* Look up a literal operator with the name and the exact arguments. */
3554 lookup_literal_operator (tree name
, VEC(tree
,gc
) *args
)
3557 decl
= lookup_name (name
);
3558 if (!decl
|| !is_overloaded_fn (decl
))
3559 return error_mark_node
;
3561 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3565 tree fn
= OVL_CURRENT (fns
);
3566 tree argtypes
= NULL_TREE
;
3567 argtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3568 if (argtypes
!= NULL_TREE
)
3570 for (ix
= 0; ix
< VEC_length (tree
, args
) && argtypes
!= NULL_TREE
;
3571 ++ix
, argtypes
= TREE_CHAIN (argtypes
))
3573 tree targ
= TREE_VALUE (argtypes
);
3574 tree tparm
= TREE_TYPE (VEC_index (tree
, args
, ix
));
3575 bool ptr
= TREE_CODE (targ
) == POINTER_TYPE
;
3576 bool arr
= TREE_CODE (tparm
) == ARRAY_TYPE
;
3577 if ((ptr
|| arr
|| !same_type_p (targ
, tparm
))
3579 || !same_type_p (TREE_TYPE (targ
),
3580 TREE_TYPE (tparm
))))
3584 && ix
== VEC_length (tree
, args
)
3585 /* May be this should be sufficient_parms_p instead,
3586 depending on how exactly should user-defined literals
3587 work in presence of default arguments on the literal
3588 operator parameters. */
3589 && argtypes
== void_list_node
)
3594 return error_mark_node
;
3597 /* Parse a user-defined char constant. Returns a call to a user-defined
3598 literal operator taking the character as an argument. */
3601 cp_parser_userdef_char_literal (cp_parser
*parser
)
3603 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3604 tree literal
= token
->u
.value
;
3605 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3606 tree value
= USERDEF_LITERAL_VALUE (literal
);
3607 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3610 /* Build up a call to the user-defined operator */
3611 /* Lookup the name we got back from the id-expression. */
3612 VEC(tree
,gc
) *args
= make_tree_vector ();
3613 VEC_safe_push (tree
, gc
, args
, value
);
3614 decl
= lookup_literal_operator (name
, args
);
3615 if (!decl
|| decl
== error_mark_node
)
3617 error ("unable to find character literal operator %qD with %qT argument",
3618 name
, TREE_TYPE (value
));
3619 release_tree_vector (args
);
3620 return error_mark_node
;
3622 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3623 release_tree_vector (args
);
3624 if (result
!= error_mark_node
)
3627 error ("unable to find character literal operator %qD with %qT argument",
3628 name
, TREE_TYPE (value
));
3629 return error_mark_node
;
3632 /* A subroutine of cp_parser_userdef_numeric_literal to
3633 create a char... template parameter pack from a string node. */
3636 make_char_string_pack (tree value
)
3639 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3640 const char *str
= TREE_STRING_POINTER (value
);
3641 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3642 tree argvec
= make_tree_vec (1);
3644 /* Fill in CHARVEC with all of the parameters. */
3645 charvec
= make_tree_vec (len
);
3646 for (i
= 0; i
< len
; ++i
)
3647 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3649 /* Build the argument packs. */
3650 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3651 TREE_TYPE (argpack
) = char_type_node
;
3653 TREE_VEC_ELT (argvec
, 0) = argpack
;
3658 /* Parse a user-defined numeric constant. returns a call to a user-defined
3659 literal operator. */
3662 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3664 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3665 tree literal
= token
->u
.value
;
3666 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3667 tree value
= USERDEF_LITERAL_VALUE (literal
);
3668 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3669 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3673 /* Look for a literal operator taking the exact type of numeric argument
3674 as the literal value. */
3675 args
= make_tree_vector ();
3676 VEC_safe_push (tree
, gc
, args
, value
);
3677 decl
= lookup_literal_operator (name
, args
);
3678 if (decl
&& decl
!= error_mark_node
)
3680 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3681 if (result
!= error_mark_node
)
3683 release_tree_vector (args
);
3687 release_tree_vector (args
);
3689 /* If the numeric argument didn't work, look for a raw literal
3690 operator taking a const char* argument consisting of the number
3691 in string format. */
3692 args
= make_tree_vector ();
3693 VEC_safe_push (tree
, gc
, args
, num_string
);
3694 decl
= lookup_literal_operator (name
, args
);
3695 if (decl
&& decl
!= error_mark_node
)
3697 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3698 if (result
!= error_mark_node
)
3700 release_tree_vector (args
);
3704 release_tree_vector (args
);
3706 /* If the raw literal didn't work, look for a non-type template
3707 function with parameter pack char.... Call the function with
3708 template parameter characters representing the number. */
3709 args
= make_tree_vector ();
3710 decl
= lookup_literal_operator (name
, args
);
3711 if (decl
&& decl
!= error_mark_node
)
3713 tree tmpl_args
= make_char_string_pack (num_string
);
3714 decl
= lookup_template_function (decl
, tmpl_args
);
3715 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3716 if (result
!= error_mark_node
)
3718 release_tree_vector (args
);
3722 release_tree_vector (args
);
3724 error ("unable to find numeric literal operator %qD", name
);
3725 return error_mark_node
;
3728 /* Parse a user-defined string constant. Returns a call to a user-defined
3729 literal operator taking a character pointer and the length of the string
3733 cp_parser_userdef_string_literal (cp_token
*token
)
3735 tree literal
= token
->u
.value
;
3736 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3737 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3738 tree value
= USERDEF_LITERAL_VALUE (literal
);
3739 int len
= TREE_STRING_LENGTH (value
)
3740 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
3743 /* Build up a call to the user-defined operator */
3744 /* Lookup the name we got back from the id-expression. */
3745 VEC(tree
,gc
) *args
= make_tree_vector ();
3746 VEC_safe_push (tree
, gc
, args
, value
);
3747 VEC_safe_push (tree
, gc
, args
, build_int_cst (size_type_node
, len
));
3748 decl
= lookup_name (name
);
3749 if (!decl
|| decl
== error_mark_node
)
3751 error ("unable to find string literal operator %qD", name
);
3752 release_tree_vector (args
);
3753 return error_mark_node
;
3755 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3756 release_tree_vector (args
);
3757 if (result
!= error_mark_node
)
3760 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3761 name
, TREE_TYPE (value
), size_type_node
);
3762 return error_mark_node
;
3766 /* Basic concepts [gram.basic] */
3768 /* Parse a translation-unit.
3771 declaration-seq [opt]
3773 Returns TRUE if all went well. */
3776 cp_parser_translation_unit (cp_parser
* parser
)
3778 /* The address of the first non-permanent object on the declarator
3780 static void *declarator_obstack_base
;
3784 /* Create the declarator obstack, if necessary. */
3785 if (!cp_error_declarator
)
3787 gcc_obstack_init (&declarator_obstack
);
3788 /* Create the error declarator. */
3789 cp_error_declarator
= make_declarator (cdk_error
);
3790 /* Create the empty parameter list. */
3791 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3792 /* Remember where the base of the declarator obstack lies. */
3793 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3796 cp_parser_declaration_seq_opt (parser
);
3798 /* If there are no tokens left then all went well. */
3799 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3801 /* Get rid of the token array; we don't need it any more. */
3802 cp_lexer_destroy (parser
->lexer
);
3803 parser
->lexer
= NULL
;
3805 /* This file might have been a context that's implicitly extern
3806 "C". If so, pop the lang context. (Only relevant for PCH.) */
3807 if (parser
->implicit_extern_c
)
3809 pop_lang_context ();
3810 parser
->implicit_extern_c
= false;
3814 finish_translation_unit ();
3820 cp_parser_error (parser
, "expected declaration");
3824 /* Make sure the declarator obstack was fully cleaned up. */
3825 gcc_assert (obstack_next_free (&declarator_obstack
)
3826 == declarator_obstack_base
);
3828 /* All went well. */
3832 /* Expressions [gram.expr] */
3834 /* Parse a primary-expression.
3845 ( compound-statement )
3846 __builtin_va_arg ( assignment-expression , type-id )
3847 __builtin_offsetof ( type-id , offsetof-expression )
3850 __has_nothrow_assign ( type-id )
3851 __has_nothrow_constructor ( type-id )
3852 __has_nothrow_copy ( type-id )
3853 __has_trivial_assign ( type-id )
3854 __has_trivial_constructor ( type-id )
3855 __has_trivial_copy ( type-id )
3856 __has_trivial_destructor ( type-id )
3857 __has_virtual_destructor ( type-id )
3858 __is_abstract ( type-id )
3859 __is_base_of ( type-id , type-id )
3860 __is_class ( type-id )
3861 __is_convertible_to ( type-id , type-id )
3862 __is_empty ( type-id )
3863 __is_enum ( type-id )
3864 __is_final ( type-id )
3865 __is_literal_type ( type-id )
3866 __is_pod ( type-id )
3867 __is_polymorphic ( type-id )
3868 __is_std_layout ( type-id )
3869 __is_trivial ( type-id )
3870 __is_union ( type-id )
3872 Objective-C++ Extension:
3880 ADDRESS_P is true iff this expression was immediately preceded by
3881 "&" and therefore might denote a pointer-to-member. CAST_P is true
3882 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3883 true iff this expression is a template argument.
3885 Returns a representation of the expression. Upon return, *IDK
3886 indicates what kind of id-expression (if any) was present. */
3889 cp_parser_primary_expression (cp_parser
*parser
,
3892 bool template_arg_p
,
3895 cp_token
*token
= NULL
;
3897 /* Assume the primary expression is not an id-expression. */
3898 *idk
= CP_ID_KIND_NONE
;
3900 /* Peek at the next token. */
3901 token
= cp_lexer_peek_token (parser
->lexer
);
3902 switch (token
->type
)
3911 user-defined-literal */
3917 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
3918 return cp_parser_userdef_numeric_literal (parser
);
3919 token
= cp_lexer_consume_token (parser
->lexer
);
3920 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
3922 error_at (token
->location
,
3923 "fixed-point types not supported in C++");
3924 return error_mark_node
;
3926 /* Floating-point literals are only allowed in an integral
3927 constant expression if they are cast to an integral or
3928 enumeration type. */
3929 if (TREE_CODE (token
->u
.value
) == REAL_CST
3930 && parser
->integral_constant_expression_p
3933 /* CAST_P will be set even in invalid code like "int(2.7 +
3934 ...)". Therefore, we have to check that the next token
3935 is sure to end the cast. */
3938 cp_token
*next_token
;
3940 next_token
= cp_lexer_peek_token (parser
->lexer
);
3941 if (/* The comma at the end of an
3942 enumerator-definition. */
3943 next_token
->type
!= CPP_COMMA
3944 /* The curly brace at the end of an enum-specifier. */
3945 && next_token
->type
!= CPP_CLOSE_BRACE
3946 /* The end of a statement. */
3947 && next_token
->type
!= CPP_SEMICOLON
3948 /* The end of the cast-expression. */
3949 && next_token
->type
!= CPP_CLOSE_PAREN
3950 /* The end of an array bound. */
3951 && next_token
->type
!= CPP_CLOSE_SQUARE
3952 /* The closing ">" in a template-argument-list. */
3953 && (next_token
->type
!= CPP_GREATER
3954 || parser
->greater_than_is_operator_p
)
3955 /* C++0x only: A ">>" treated like two ">" tokens,
3956 in a template-argument-list. */
3957 && (next_token
->type
!= CPP_RSHIFT
3958 || (cxx_dialect
== cxx98
)
3959 || parser
->greater_than_is_operator_p
))
3963 /* If we are within a cast, then the constraint that the
3964 cast is to an integral or enumeration type will be
3965 checked at that point. If we are not within a cast, then
3966 this code is invalid. */
3968 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
3970 return token
->u
.value
;
3972 case CPP_CHAR_USERDEF
:
3973 case CPP_CHAR16_USERDEF
:
3974 case CPP_CHAR32_USERDEF
:
3975 case CPP_WCHAR_USERDEF
:
3976 return cp_parser_userdef_char_literal (parser
);
3982 case CPP_UTF8STRING
:
3983 case CPP_STRING_USERDEF
:
3984 case CPP_STRING16_USERDEF
:
3985 case CPP_STRING32_USERDEF
:
3986 case CPP_WSTRING_USERDEF
:
3987 case CPP_UTF8STRING_USERDEF
:
3988 /* ??? Should wide strings be allowed when parser->translate_strings_p
3989 is false (i.e. in attributes)? If not, we can kill the third
3990 argument to cp_parser_string_literal. */
3991 return cp_parser_string_literal (parser
,
3992 parser
->translate_strings_p
,
3995 case CPP_OPEN_PAREN
:
3998 bool saved_greater_than_is_operator_p
;
4000 /* Consume the `('. */
4001 cp_lexer_consume_token (parser
->lexer
);
4002 /* Within a parenthesized expression, a `>' token is always
4003 the greater-than operator. */
4004 saved_greater_than_is_operator_p
4005 = parser
->greater_than_is_operator_p
;
4006 parser
->greater_than_is_operator_p
= true;
4007 /* If we see `( { ' then we are looking at the beginning of
4008 a GNU statement-expression. */
4009 if (cp_parser_allow_gnu_extensions_p (parser
)
4010 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
4012 /* Statement-expressions are not allowed by the standard. */
4013 pedwarn (token
->location
, OPT_Wpedantic
,
4014 "ISO C++ forbids braced-groups within expressions");
4016 /* And they're not allowed outside of a function-body; you
4017 cannot, for example, write:
4019 int i = ({ int j = 3; j + 1; });
4021 at class or namespace scope. */
4022 if (!parser
->in_function_body
4023 || parser
->in_template_argument_list_p
)
4025 error_at (token
->location
,
4026 "statement-expressions are not allowed outside "
4027 "functions nor in template-argument lists");
4028 cp_parser_skip_to_end_of_block_or_statement (parser
);
4029 expr
= error_mark_node
;
4033 /* Start the statement-expression. */
4034 expr
= begin_stmt_expr ();
4035 /* Parse the compound-statement. */
4036 cp_parser_compound_statement (parser
, expr
, false, false);
4038 expr
= finish_stmt_expr (expr
, false);
4043 /* Parse the parenthesized expression. */
4044 expr
= cp_parser_expression (parser
, cast_p
, idk
);
4045 /* Let the front end know that this expression was
4046 enclosed in parentheses. This matters in case, for
4047 example, the expression is of the form `A::B', since
4048 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4050 finish_parenthesized_expr (expr
);
4051 /* DR 705: Wrapping an unqualified name in parentheses
4052 suppresses arg-dependent lookup. We want to pass back
4053 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4054 (c++/37862), but none of the others. */
4055 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4056 *idk
= CP_ID_KIND_NONE
;
4058 /* The `>' token might be the end of a template-id or
4059 template-parameter-list now. */
4060 parser
->greater_than_is_operator_p
4061 = saved_greater_than_is_operator_p
;
4062 /* Consume the `)'. */
4063 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4064 cp_parser_skip_to_end_of_statement (parser
);
4069 case CPP_OPEN_SQUARE
:
4070 if (c_dialect_objc ())
4071 /* We have an Objective-C++ message. */
4072 return cp_parser_objc_expression (parser
);
4074 tree lam
= cp_parser_lambda_expression (parser
);
4075 /* Don't warn about a failed tentative parse. */
4076 if (cp_parser_error_occurred (parser
))
4077 return error_mark_node
;
4078 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4082 case CPP_OBJC_STRING
:
4083 if (c_dialect_objc ())
4084 /* We have an Objective-C++ string literal. */
4085 return cp_parser_objc_expression (parser
);
4086 cp_parser_error (parser
, "expected primary-expression");
4087 return error_mark_node
;
4090 switch (token
->keyword
)
4092 /* These two are the boolean literals. */
4094 cp_lexer_consume_token (parser
->lexer
);
4095 return boolean_true_node
;
4097 cp_lexer_consume_token (parser
->lexer
);
4098 return boolean_false_node
;
4100 /* The `__null' literal. */
4102 cp_lexer_consume_token (parser
->lexer
);
4105 /* The `nullptr' literal. */
4107 cp_lexer_consume_token (parser
->lexer
);
4108 return nullptr_node
;
4110 /* Recognize the `this' keyword. */
4112 cp_lexer_consume_token (parser
->lexer
);
4113 if (parser
->local_variables_forbidden_p
)
4115 error_at (token
->location
,
4116 "%<this%> may not be used in this context");
4117 return error_mark_node
;
4119 /* Pointers cannot appear in constant-expressions. */
4120 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4121 return error_mark_node
;
4122 return finish_this_expr ();
4124 /* The `operator' keyword can be the beginning of an
4129 case RID_FUNCTION_NAME
:
4130 case RID_PRETTY_FUNCTION_NAME
:
4131 case RID_C99_FUNCTION_NAME
:
4133 non_integral_constant name
;
4135 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4136 __func__ are the names of variables -- but they are
4137 treated specially. Therefore, they are handled here,
4138 rather than relying on the generic id-expression logic
4139 below. Grammatically, these names are id-expressions.
4141 Consume the token. */
4142 token
= cp_lexer_consume_token (parser
->lexer
);
4144 switch (token
->keyword
)
4146 case RID_FUNCTION_NAME
:
4147 name
= NIC_FUNC_NAME
;
4149 case RID_PRETTY_FUNCTION_NAME
:
4150 name
= NIC_PRETTY_FUNC
;
4152 case RID_C99_FUNCTION_NAME
:
4153 name
= NIC_C99_FUNC
;
4159 if (cp_parser_non_integral_constant_expression (parser
, name
))
4160 return error_mark_node
;
4162 /* Look up the name. */
4163 return finish_fname (token
->u
.value
);
4170 source_location type_location
;
4172 /* The `__builtin_va_arg' construct is used to handle
4173 `va_arg'. Consume the `__builtin_va_arg' token. */
4174 cp_lexer_consume_token (parser
->lexer
);
4175 /* Look for the opening `('. */
4176 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4177 /* Now, parse the assignment-expression. */
4178 expression
= cp_parser_assignment_expression (parser
,
4179 /*cast_p=*/false, NULL
);
4180 /* Look for the `,'. */
4181 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4182 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4183 /* Parse the type-id. */
4184 type
= cp_parser_type_id (parser
);
4185 /* Look for the closing `)'. */
4186 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4187 /* Using `va_arg' in a constant-expression is not
4189 if (cp_parser_non_integral_constant_expression (parser
,
4191 return error_mark_node
;
4192 return build_x_va_arg (type_location
, expression
, type
);
4196 return cp_parser_builtin_offsetof (parser
);
4198 case RID_HAS_NOTHROW_ASSIGN
:
4199 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4200 case RID_HAS_NOTHROW_COPY
:
4201 case RID_HAS_TRIVIAL_ASSIGN
:
4202 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4203 case RID_HAS_TRIVIAL_COPY
:
4204 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4205 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4206 case RID_IS_ABSTRACT
:
4207 case RID_IS_BASE_OF
:
4209 case RID_IS_CONVERTIBLE_TO
:
4213 case RID_IS_LITERAL_TYPE
:
4215 case RID_IS_POLYMORPHIC
:
4216 case RID_IS_STD_LAYOUT
:
4217 case RID_IS_TRIVIAL
:
4219 return cp_parser_trait_expr (parser
, token
->keyword
);
4221 /* Objective-C++ expressions. */
4223 case RID_AT_PROTOCOL
:
4224 case RID_AT_SELECTOR
:
4225 return cp_parser_objc_expression (parser
);
4228 if (parser
->in_function_body
4229 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4232 error_at (token
->location
,
4233 "a template declaration cannot appear at block scope");
4234 cp_parser_skip_to_end_of_block_or_statement (parser
);
4235 return error_mark_node
;
4238 cp_parser_error (parser
, "expected primary-expression");
4239 return error_mark_node
;
4242 /* An id-expression can start with either an identifier, a
4243 `::' as the beginning of a qualified-id, or the "operator"
4247 case CPP_TEMPLATE_ID
:
4248 case CPP_NESTED_NAME_SPECIFIER
:
4252 const char *error_msg
;
4255 cp_token
*id_expr_token
;
4258 /* Parse the id-expression. */
4260 = cp_parser_id_expression (parser
,
4261 /*template_keyword_p=*/false,
4262 /*check_dependency_p=*/true,
4264 /*declarator_p=*/false,
4265 /*optional_p=*/false);
4266 if (id_expression
== error_mark_node
)
4267 return error_mark_node
;
4268 id_expr_token
= token
;
4269 token
= cp_lexer_peek_token (parser
->lexer
);
4270 done
= (token
->type
!= CPP_OPEN_SQUARE
4271 && token
->type
!= CPP_OPEN_PAREN
4272 && token
->type
!= CPP_DOT
4273 && token
->type
!= CPP_DEREF
4274 && token
->type
!= CPP_PLUS_PLUS
4275 && token
->type
!= CPP_MINUS_MINUS
);
4276 /* If we have a template-id, then no further lookup is
4277 required. If the template-id was for a template-class, we
4278 will sometimes have a TYPE_DECL at this point. */
4279 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4280 || TREE_CODE (id_expression
) == TYPE_DECL
)
4281 decl
= id_expression
;
4282 /* Look up the name. */
4285 tree ambiguous_decls
;
4287 /* If we already know that this lookup is ambiguous, then
4288 we've already issued an error message; there's no reason
4290 if (id_expr_token
->type
== CPP_NAME
4291 && id_expr_token
->ambiguous_p
)
4293 cp_parser_simulate_error (parser
);
4294 return error_mark_node
;
4297 decl
= cp_parser_lookup_name (parser
, id_expression
,
4300 /*is_namespace=*/false,
4301 /*check_dependency=*/true,
4303 id_expr_token
->location
);
4304 /* If the lookup was ambiguous, an error will already have
4306 if (ambiguous_decls
)
4307 return error_mark_node
;
4309 /* In Objective-C++, we may have an Objective-C 2.0
4310 dot-syntax for classes here. */
4311 if (c_dialect_objc ()
4312 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4313 && TREE_CODE (decl
) == TYPE_DECL
4314 && objc_is_class_name (decl
))
4317 cp_lexer_consume_token (parser
->lexer
);
4318 component
= cp_parser_identifier (parser
);
4319 if (component
== error_mark_node
)
4320 return error_mark_node
;
4322 return objc_build_class_component_ref (id_expression
, component
);
4325 /* In Objective-C++, an instance variable (ivar) may be preferred
4326 to whatever cp_parser_lookup_name() found. */
4327 decl
= objc_lookup_ivar (decl
, id_expression
);
4329 /* If name lookup gives us a SCOPE_REF, then the
4330 qualifying scope was dependent. */
4331 if (TREE_CODE (decl
) == SCOPE_REF
)
4333 /* At this point, we do not know if DECL is a valid
4334 integral constant expression. We assume that it is
4335 in fact such an expression, so that code like:
4337 template <int N> struct A {
4341 is accepted. At template-instantiation time, we
4342 will check that B<N>::i is actually a constant. */
4345 /* Check to see if DECL is a local variable in a context
4346 where that is forbidden. */
4347 if (parser
->local_variables_forbidden_p
4348 && local_variable_p (decl
))
4350 /* It might be that we only found DECL because we are
4351 trying to be generous with pre-ISO scoping rules.
4352 For example, consider:
4356 for (int i = 0; i < 10; ++i) {}
4357 extern void f(int j = i);
4360 Here, name look up will originally find the out
4361 of scope `i'. We need to issue a warning message,
4362 but then use the global `i'. */
4363 decl
= check_for_out_of_scope_variable (decl
);
4364 if (local_variable_p (decl
))
4366 error_at (id_expr_token
->location
,
4367 "local variable %qD may not appear in this context",
4369 return error_mark_node
;
4374 decl
= (finish_id_expression
4375 (id_expression
, decl
, parser
->scope
,
4377 parser
->integral_constant_expression_p
,
4378 parser
->allow_non_integral_constant_expression_p
,
4379 &parser
->non_integral_constant_expression_p
,
4380 template_p
, done
, address_p
,
4383 id_expr_token
->location
));
4385 cp_parser_error (parser
, error_msg
);
4389 /* Anything else is an error. */
4391 cp_parser_error (parser
, "expected primary-expression");
4392 return error_mark_node
;
4396 /* Parse an id-expression.
4403 :: [opt] nested-name-specifier template [opt] unqualified-id
4405 :: operator-function-id
4408 Return a representation of the unqualified portion of the
4409 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4410 a `::' or nested-name-specifier.
4412 Often, if the id-expression was a qualified-id, the caller will
4413 want to make a SCOPE_REF to represent the qualified-id. This
4414 function does not do this in order to avoid wastefully creating
4415 SCOPE_REFs when they are not required.
4417 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4420 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4421 uninstantiated templates.
4423 If *TEMPLATE_P is non-NULL, it is set to true iff the
4424 `template' keyword is used to explicitly indicate that the entity
4425 named is a template.
4427 If DECLARATOR_P is true, the id-expression is appearing as part of
4428 a declarator, rather than as part of an expression. */
4431 cp_parser_id_expression (cp_parser
*parser
,
4432 bool template_keyword_p
,
4433 bool check_dependency_p
,
4438 bool global_scope_p
;
4439 bool nested_name_specifier_p
;
4441 /* Assume the `template' keyword was not used. */
4443 *template_p
= template_keyword_p
;
4445 /* Look for the optional `::' operator. */
4447 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4449 /* Look for the optional nested-name-specifier. */
4450 nested_name_specifier_p
4451 = (cp_parser_nested_name_specifier_opt (parser
,
4452 /*typename_keyword_p=*/false,
4457 /* If there is a nested-name-specifier, then we are looking at
4458 the first qualified-id production. */
4459 if (nested_name_specifier_p
)
4462 tree saved_object_scope
;
4463 tree saved_qualifying_scope
;
4464 tree unqualified_id
;
4467 /* See if the next token is the `template' keyword. */
4469 template_p
= &is_template
;
4470 *template_p
= cp_parser_optional_template_keyword (parser
);
4471 /* Name lookup we do during the processing of the
4472 unqualified-id might obliterate SCOPE. */
4473 saved_scope
= parser
->scope
;
4474 saved_object_scope
= parser
->object_scope
;
4475 saved_qualifying_scope
= parser
->qualifying_scope
;
4476 /* Process the final unqualified-id. */
4477 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4480 /*optional_p=*/false);
4481 /* Restore the SAVED_SCOPE for our caller. */
4482 parser
->scope
= saved_scope
;
4483 parser
->object_scope
= saved_object_scope
;
4484 parser
->qualifying_scope
= saved_qualifying_scope
;
4486 return unqualified_id
;
4488 /* Otherwise, if we are in global scope, then we are looking at one
4489 of the other qualified-id productions. */
4490 else if (global_scope_p
)
4495 /* Peek at the next token. */
4496 token
= cp_lexer_peek_token (parser
->lexer
);
4498 /* If it's an identifier, and the next token is not a "<", then
4499 we can avoid the template-id case. This is an optimization
4500 for this common case. */
4501 if (token
->type
== CPP_NAME
4502 && !cp_parser_nth_token_starts_template_argument_list_p
4504 return cp_parser_identifier (parser
);
4506 cp_parser_parse_tentatively (parser
);
4507 /* Try a template-id. */
4508 id
= cp_parser_template_id (parser
,
4509 /*template_keyword_p=*/false,
4510 /*check_dependency_p=*/true,
4513 /* If that worked, we're done. */
4514 if (cp_parser_parse_definitely (parser
))
4517 /* Peek at the next token. (Changes in the token buffer may
4518 have invalidated the pointer obtained above.) */
4519 token
= cp_lexer_peek_token (parser
->lexer
);
4521 switch (token
->type
)
4524 return cp_parser_identifier (parser
);
4527 if (token
->keyword
== RID_OPERATOR
)
4528 return cp_parser_operator_function_id (parser
);
4532 cp_parser_error (parser
, "expected id-expression");
4533 return error_mark_node
;
4537 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4538 /*check_dependency_p=*/true,
4543 /* Parse an unqualified-id.
4547 operator-function-id
4548 conversion-function-id
4552 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4553 keyword, in a construct like `A::template ...'.
4555 Returns a representation of unqualified-id. For the `identifier'
4556 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4557 production a BIT_NOT_EXPR is returned; the operand of the
4558 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4559 other productions, see the documentation accompanying the
4560 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4561 names are looked up in uninstantiated templates. If DECLARATOR_P
4562 is true, the unqualified-id is appearing as part of a declarator,
4563 rather than as part of an expression. */
4566 cp_parser_unqualified_id (cp_parser
* parser
,
4567 bool template_keyword_p
,
4568 bool check_dependency_p
,
4574 /* Peek at the next token. */
4575 token
= cp_lexer_peek_token (parser
->lexer
);
4577 switch (token
->type
)
4583 /* We don't know yet whether or not this will be a
4585 cp_parser_parse_tentatively (parser
);
4586 /* Try a template-id. */
4587 id
= cp_parser_template_id (parser
, template_keyword_p
,
4591 /* If it worked, we're done. */
4592 if (cp_parser_parse_definitely (parser
))
4594 /* Otherwise, it's an ordinary identifier. */
4595 return cp_parser_identifier (parser
);
4598 case CPP_TEMPLATE_ID
:
4599 return cp_parser_template_id (parser
, template_keyword_p
,
4607 tree qualifying_scope
;
4612 /* Consume the `~' token. */
4613 cp_lexer_consume_token (parser
->lexer
);
4614 /* Parse the class-name. The standard, as written, seems to
4617 template <typename T> struct S { ~S (); };
4618 template <typename T> S<T>::~S() {}
4620 is invalid, since `~' must be followed by a class-name, but
4621 `S<T>' is dependent, and so not known to be a class.
4622 That's not right; we need to look in uninstantiated
4623 templates. A further complication arises from:
4625 template <typename T> void f(T t) {
4629 Here, it is not possible to look up `T' in the scope of `T'
4630 itself. We must look in both the current scope, and the
4631 scope of the containing complete expression.
4633 Yet another issue is:
4642 The standard does not seem to say that the `S' in `~S'
4643 should refer to the type `S' and not the data member
4646 /* DR 244 says that we look up the name after the "~" in the
4647 same scope as we looked up the qualifying name. That idea
4648 isn't fully worked out; it's more complicated than that. */
4649 scope
= parser
->scope
;
4650 object_scope
= parser
->object_scope
;
4651 qualifying_scope
= parser
->qualifying_scope
;
4653 /* Check for invalid scopes. */
4654 if (scope
== error_mark_node
)
4656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4657 cp_lexer_consume_token (parser
->lexer
);
4658 return error_mark_node
;
4660 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4662 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4663 error_at (token
->location
,
4664 "scope %qT before %<~%> is not a class-name",
4666 cp_parser_simulate_error (parser
);
4667 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4668 cp_lexer_consume_token (parser
->lexer
);
4669 return error_mark_node
;
4671 gcc_assert (!scope
|| TYPE_P (scope
));
4673 /* If the name is of the form "X::~X" it's OK even if X is a
4675 token
= cp_lexer_peek_token (parser
->lexer
);
4677 && token
->type
== CPP_NAME
4678 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4680 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4681 || (CLASS_TYPE_P (scope
)
4682 && constructor_name_p (token
->u
.value
, scope
))))
4684 cp_lexer_consume_token (parser
->lexer
);
4685 return build_nt (BIT_NOT_EXPR
, scope
);
4688 /* If there was an explicit qualification (S::~T), first look
4689 in the scope given by the qualification (i.e., S).
4691 Note: in the calls to cp_parser_class_name below we pass
4692 typename_type so that lookup finds the injected-class-name
4693 rather than the constructor. */
4695 type_decl
= NULL_TREE
;
4698 cp_parser_parse_tentatively (parser
);
4699 type_decl
= cp_parser_class_name (parser
,
4700 /*typename_keyword_p=*/false,
4701 /*template_keyword_p=*/false,
4703 /*check_dependency=*/false,
4704 /*class_head_p=*/false,
4706 if (cp_parser_parse_definitely (parser
))
4709 /* In "N::S::~S", look in "N" as well. */
4710 if (!done
&& scope
&& qualifying_scope
)
4712 cp_parser_parse_tentatively (parser
);
4713 parser
->scope
= qualifying_scope
;
4714 parser
->object_scope
= NULL_TREE
;
4715 parser
->qualifying_scope
= NULL_TREE
;
4717 = cp_parser_class_name (parser
,
4718 /*typename_keyword_p=*/false,
4719 /*template_keyword_p=*/false,
4721 /*check_dependency=*/false,
4722 /*class_head_p=*/false,
4724 if (cp_parser_parse_definitely (parser
))
4727 /* In "p->S::~T", look in the scope given by "*p" as well. */
4728 else if (!done
&& object_scope
)
4730 cp_parser_parse_tentatively (parser
);
4731 parser
->scope
= object_scope
;
4732 parser
->object_scope
= NULL_TREE
;
4733 parser
->qualifying_scope
= NULL_TREE
;
4735 = cp_parser_class_name (parser
,
4736 /*typename_keyword_p=*/false,
4737 /*template_keyword_p=*/false,
4739 /*check_dependency=*/false,
4740 /*class_head_p=*/false,
4742 if (cp_parser_parse_definitely (parser
))
4745 /* Look in the surrounding context. */
4748 parser
->scope
= NULL_TREE
;
4749 parser
->object_scope
= NULL_TREE
;
4750 parser
->qualifying_scope
= NULL_TREE
;
4751 if (processing_template_decl
)
4752 cp_parser_parse_tentatively (parser
);
4754 = cp_parser_class_name (parser
,
4755 /*typename_keyword_p=*/false,
4756 /*template_keyword_p=*/false,
4758 /*check_dependency=*/false,
4759 /*class_head_p=*/false,
4761 if (processing_template_decl
4762 && ! cp_parser_parse_definitely (parser
))
4764 /* We couldn't find a type with this name, so just accept
4765 it and check for a match at instantiation time. */
4766 type_decl
= cp_parser_identifier (parser
);
4767 if (type_decl
!= error_mark_node
)
4768 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
4772 /* If an error occurred, assume that the name of the
4773 destructor is the same as the name of the qualifying
4774 class. That allows us to keep parsing after running
4775 into ill-formed destructor names. */
4776 if (type_decl
== error_mark_node
&& scope
)
4777 return build_nt (BIT_NOT_EXPR
, scope
);
4778 else if (type_decl
== error_mark_node
)
4779 return error_mark_node
;
4781 /* Check that destructor name and scope match. */
4782 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
4784 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4785 error_at (token
->location
,
4786 "declaration of %<~%T%> as member of %qT",
4788 cp_parser_simulate_error (parser
);
4789 return error_mark_node
;
4794 A typedef-name that names a class shall not be used as the
4795 identifier in the declarator for a destructor declaration. */
4797 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
4798 && !DECL_SELF_REFERENCE_P (type_decl
)
4799 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4800 error_at (token
->location
,
4801 "typedef-name %qD used as destructor declarator",
4804 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
4808 if (token
->keyword
== RID_OPERATOR
)
4812 /* This could be a template-id, so we try that first. */
4813 cp_parser_parse_tentatively (parser
);
4814 /* Try a template-id. */
4815 id
= cp_parser_template_id (parser
, template_keyword_p
,
4816 /*check_dependency_p=*/true,
4819 /* If that worked, we're done. */
4820 if (cp_parser_parse_definitely (parser
))
4822 /* We still don't know whether we're looking at an
4823 operator-function-id or a conversion-function-id. */
4824 cp_parser_parse_tentatively (parser
);
4825 /* Try an operator-function-id. */
4826 id
= cp_parser_operator_function_id (parser
);
4827 /* If that didn't work, try a conversion-function-id. */
4828 if (!cp_parser_parse_definitely (parser
))
4829 id
= cp_parser_conversion_function_id (parser
);
4830 else if (UDLIT_OPER_P (id
))
4833 const char *name
= UDLIT_OP_SUFFIX (id
);
4834 if (name
[0] != '_' && !in_system_header
)
4835 warning (0, "literal operator suffixes not preceded by %<_%>"
4836 " are reserved for future standardization");
4846 cp_parser_error (parser
, "expected unqualified-id");
4847 return error_mark_node
;
4851 /* Parse an (optional) nested-name-specifier.
4853 nested-name-specifier: [C++98]
4854 class-or-namespace-name :: nested-name-specifier [opt]
4855 class-or-namespace-name :: template nested-name-specifier [opt]
4857 nested-name-specifier: [C++0x]
4860 nested-name-specifier identifier ::
4861 nested-name-specifier template [opt] simple-template-id ::
4863 PARSER->SCOPE should be set appropriately before this function is
4864 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4865 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4868 Sets PARSER->SCOPE to the class (TYPE) or namespace
4869 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4870 it unchanged if there is no nested-name-specifier. Returns the new
4871 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4873 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4874 part of a declaration and/or decl-specifier. */
4877 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
4878 bool typename_keyword_p
,
4879 bool check_dependency_p
,
4881 bool is_declaration
)
4883 bool success
= false;
4884 cp_token_position start
= 0;
4887 /* Remember where the nested-name-specifier starts. */
4888 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4890 start
= cp_lexer_token_position (parser
->lexer
, false);
4891 push_deferring_access_checks (dk_deferred
);
4898 tree saved_qualifying_scope
;
4899 bool template_keyword_p
;
4901 /* Spot cases that cannot be the beginning of a
4902 nested-name-specifier. */
4903 token
= cp_lexer_peek_token (parser
->lexer
);
4905 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4906 the already parsed nested-name-specifier. */
4907 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
4909 /* Grab the nested-name-specifier and continue the loop. */
4910 cp_parser_pre_parsed_nested_name_specifier (parser
);
4911 /* If we originally encountered this nested-name-specifier
4912 with IS_DECLARATION set to false, we will not have
4913 resolved TYPENAME_TYPEs, so we must do so here. */
4915 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4917 new_scope
= resolve_typename_type (parser
->scope
,
4918 /*only_current_p=*/false);
4919 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
4920 parser
->scope
= new_scope
;
4926 /* Spot cases that cannot be the beginning of a
4927 nested-name-specifier. On the second and subsequent times
4928 through the loop, we look for the `template' keyword. */
4929 if (success
&& token
->keyword
== RID_TEMPLATE
)
4931 /* A template-id can start a nested-name-specifier. */
4932 else if (token
->type
== CPP_TEMPLATE_ID
)
4934 /* DR 743: decltype can be used in a nested-name-specifier. */
4935 else if (token_is_decltype (token
))
4939 /* If the next token is not an identifier, then it is
4940 definitely not a type-name or namespace-name. */
4941 if (token
->type
!= CPP_NAME
)
4943 /* If the following token is neither a `<' (to begin a
4944 template-id), nor a `::', then we are not looking at a
4945 nested-name-specifier. */
4946 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
4948 if (token
->type
== CPP_COLON
4949 && parser
->colon_corrects_to_scope_p
4950 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
4952 error_at (token
->location
,
4953 "found %<:%> in nested-name-specifier, expected %<::%>");
4954 token
->type
= CPP_SCOPE
;
4957 if (token
->type
!= CPP_SCOPE
4958 && !cp_parser_nth_token_starts_template_argument_list_p
4963 /* The nested-name-specifier is optional, so we parse
4965 cp_parser_parse_tentatively (parser
);
4967 /* Look for the optional `template' keyword, if this isn't the
4968 first time through the loop. */
4970 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
4972 template_keyword_p
= false;
4974 /* Save the old scope since the name lookup we are about to do
4975 might destroy it. */
4976 old_scope
= parser
->scope
;
4977 saved_qualifying_scope
= parser
->qualifying_scope
;
4978 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4979 look up names in "X<T>::I" in order to determine that "Y" is
4980 a template. So, if we have a typename at this point, we make
4981 an effort to look through it. */
4983 && !typename_keyword_p
4985 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4986 parser
->scope
= resolve_typename_type (parser
->scope
,
4987 /*only_current_p=*/false);
4988 /* Parse the qualifying entity. */
4990 = cp_parser_qualifying_entity (parser
,
4996 /* Look for the `::' token. */
4997 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
4999 /* If we found what we wanted, we keep going; otherwise, we're
5001 if (!cp_parser_parse_definitely (parser
))
5003 bool error_p
= false;
5005 /* Restore the OLD_SCOPE since it was valid before the
5006 failed attempt at finding the last
5007 class-or-namespace-name. */
5008 parser
->scope
= old_scope
;
5009 parser
->qualifying_scope
= saved_qualifying_scope
;
5011 /* If the next token is a decltype, and the one after that is a
5012 `::', then the decltype has failed to resolve to a class or
5013 enumeration type. Give this error even when parsing
5014 tentatively since it can't possibly be valid--and we're going
5015 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5016 won't get another chance.*/
5017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5018 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5021 token
= cp_lexer_consume_token (parser
->lexer
);
5022 error_at (token
->location
, "decltype evaluates to %qT, "
5023 "which is not a class or enumeration type",
5025 parser
->scope
= error_mark_node
;
5029 cp_lexer_consume_token (parser
->lexer
);
5032 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5034 /* If the next token is an identifier, and the one after
5035 that is a `::', then any valid interpretation would have
5036 found a class-or-namespace-name. */
5037 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5038 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5040 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5043 token
= cp_lexer_consume_token (parser
->lexer
);
5046 if (!token
->ambiguous_p
)
5049 tree ambiguous_decls
;
5051 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5053 /*is_template=*/false,
5054 /*is_namespace=*/false,
5055 /*check_dependency=*/true,
5058 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5059 error_at (token
->location
,
5060 "%qD used without template parameters",
5062 else if (ambiguous_decls
)
5064 error_at (token
->location
,
5065 "reference to %qD is ambiguous",
5067 print_candidates (ambiguous_decls
);
5068 decl
= error_mark_node
;
5072 if (cxx_dialect
!= cxx98
)
5073 cp_parser_name_lookup_error
5074 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5077 cp_parser_name_lookup_error
5078 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5082 parser
->scope
= error_mark_node
;
5084 /* Treat this as a successful nested-name-specifier
5089 If the name found is not a class-name (clause
5090 _class_) or namespace-name (_namespace.def_), the
5091 program is ill-formed. */
5094 cp_lexer_consume_token (parser
->lexer
);
5098 /* We've found one valid nested-name-specifier. */
5100 /* Name lookup always gives us a DECL. */
5101 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5102 new_scope
= TREE_TYPE (new_scope
);
5103 /* Uses of "template" must be followed by actual templates. */
5104 if (template_keyword_p
5105 && !(CLASS_TYPE_P (new_scope
)
5106 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5107 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5108 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5109 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5110 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5111 == TEMPLATE_ID_EXPR
)))
5112 permerror (input_location
, TYPE_P (new_scope
)
5113 ? G_("%qT is not a template")
5114 : G_("%qD is not a template"),
5116 /* If it is a class scope, try to complete it; we are about to
5117 be looking up names inside the class. */
5118 if (TYPE_P (new_scope
)
5119 /* Since checking types for dependency can be expensive,
5120 avoid doing it if the type is already complete. */
5121 && !COMPLETE_TYPE_P (new_scope
)
5122 /* Do not try to complete dependent types. */
5123 && !dependent_type_p (new_scope
))
5125 new_scope
= complete_type (new_scope
);
5126 /* If it is a typedef to current class, use the current
5127 class instead, as the typedef won't have any names inside
5129 if (!COMPLETE_TYPE_P (new_scope
)
5130 && currently_open_class (new_scope
))
5131 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5133 /* Make sure we look in the right scope the next time through
5135 parser
->scope
= new_scope
;
5138 /* If parsing tentatively, replace the sequence of tokens that makes
5139 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5140 token. That way, should we re-parse the token stream, we will
5141 not have to repeat the effort required to do the parse, nor will
5142 we issue duplicate error messages. */
5143 if (success
&& start
)
5147 token
= cp_lexer_token_at (parser
->lexer
, start
);
5148 /* Reset the contents of the START token. */
5149 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5150 /* Retrieve any deferred checks. Do not pop this access checks yet
5151 so the memory will not be reclaimed during token replacing below. */
5152 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
5153 token
->u
.tree_check_value
->value
= parser
->scope
;
5154 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5155 token
->u
.tree_check_value
->qualifying_scope
=
5156 parser
->qualifying_scope
;
5157 token
->keyword
= RID_MAX
;
5159 /* Purge all subsequent tokens. */
5160 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5164 pop_to_parent_deferring_access_checks ();
5166 return success
? parser
->scope
: NULL_TREE
;
5169 /* Parse a nested-name-specifier. See
5170 cp_parser_nested_name_specifier_opt for details. This function
5171 behaves identically, except that it will an issue an error if no
5172 nested-name-specifier is present. */
5175 cp_parser_nested_name_specifier (cp_parser
*parser
,
5176 bool typename_keyword_p
,
5177 bool check_dependency_p
,
5179 bool is_declaration
)
5183 /* Look for the nested-name-specifier. */
5184 scope
= cp_parser_nested_name_specifier_opt (parser
,
5189 /* If it was not present, issue an error message. */
5192 cp_parser_error (parser
, "expected nested-name-specifier");
5193 parser
->scope
= NULL_TREE
;
5199 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5200 this is either a class-name or a namespace-name (which corresponds
5201 to the class-or-namespace-name production in the grammar). For
5202 C++0x, it can also be a type-name that refers to an enumeration
5203 type or a simple-template-id.
5205 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5206 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5207 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5208 TYPE_P is TRUE iff the next name should be taken as a class-name,
5209 even the same name is declared to be another entity in the same
5212 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5213 specified by the class-or-namespace-name. If neither is found the
5214 ERROR_MARK_NODE is returned. */
5217 cp_parser_qualifying_entity (cp_parser
*parser
,
5218 bool typename_keyword_p
,
5219 bool template_keyword_p
,
5220 bool check_dependency_p
,
5222 bool is_declaration
)
5225 tree saved_qualifying_scope
;
5226 tree saved_object_scope
;
5229 bool successful_parse_p
;
5231 /* DR 743: decltype can appear in a nested-name-specifier. */
5232 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5234 scope
= cp_parser_decltype (parser
);
5235 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5236 && !MAYBE_CLASS_TYPE_P (scope
))
5238 cp_parser_simulate_error (parser
);
5239 return error_mark_node
;
5241 if (TYPE_NAME (scope
))
5242 scope
= TYPE_NAME (scope
);
5246 /* Before we try to parse the class-name, we must save away the
5247 current PARSER->SCOPE since cp_parser_class_name will destroy
5249 saved_scope
= parser
->scope
;
5250 saved_qualifying_scope
= parser
->qualifying_scope
;
5251 saved_object_scope
= parser
->object_scope
;
5252 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5253 there is no need to look for a namespace-name. */
5254 only_class_p
= template_keyword_p
5255 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5257 cp_parser_parse_tentatively (parser
);
5258 scope
= cp_parser_class_name (parser
,
5261 type_p
? class_type
: none_type
,
5263 /*class_head_p=*/false,
5265 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5266 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5268 && cxx_dialect
!= cxx98
5269 && !successful_parse_p
)
5271 /* Restore the saved scope. */
5272 parser
->scope
= saved_scope
;
5273 parser
->qualifying_scope
= saved_qualifying_scope
;
5274 parser
->object_scope
= saved_object_scope
;
5276 /* Parse tentatively. */
5277 cp_parser_parse_tentatively (parser
);
5279 /* Parse a type-name */
5280 scope
= cp_parser_type_name (parser
);
5282 /* "If the name found does not designate a namespace or a class,
5283 enumeration, or dependent type, the program is ill-formed."
5285 We cover classes and dependent types above and namespaces below,
5286 so this code is only looking for enums. */
5287 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5288 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5289 cp_parser_simulate_error (parser
);
5291 successful_parse_p
= cp_parser_parse_definitely (parser
);
5293 /* If that didn't work, try for a namespace-name. */
5294 if (!only_class_p
&& !successful_parse_p
)
5296 /* Restore the saved scope. */
5297 parser
->scope
= saved_scope
;
5298 parser
->qualifying_scope
= saved_qualifying_scope
;
5299 parser
->object_scope
= saved_object_scope
;
5300 /* If we are not looking at an identifier followed by the scope
5301 resolution operator, then this is not part of a
5302 nested-name-specifier. (Note that this function is only used
5303 to parse the components of a nested-name-specifier.) */
5304 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5305 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5306 return error_mark_node
;
5307 scope
= cp_parser_namespace_name (parser
);
5313 /* Parse a postfix-expression.
5317 postfix-expression [ expression ]
5318 postfix-expression ( expression-list [opt] )
5319 simple-type-specifier ( expression-list [opt] )
5320 typename :: [opt] nested-name-specifier identifier
5321 ( expression-list [opt] )
5322 typename :: [opt] nested-name-specifier template [opt] template-id
5323 ( expression-list [opt] )
5324 postfix-expression . template [opt] id-expression
5325 postfix-expression -> template [opt] id-expression
5326 postfix-expression . pseudo-destructor-name
5327 postfix-expression -> pseudo-destructor-name
5328 postfix-expression ++
5329 postfix-expression --
5330 dynamic_cast < type-id > ( expression )
5331 static_cast < type-id > ( expression )
5332 reinterpret_cast < type-id > ( expression )
5333 const_cast < type-id > ( expression )
5334 typeid ( expression )
5340 ( type-id ) { initializer-list , [opt] }
5342 This extension is a GNU version of the C99 compound-literal
5343 construct. (The C99 grammar uses `type-name' instead of `type-id',
5344 but they are essentially the same concept.)
5346 If ADDRESS_P is true, the postfix expression is the operand of the
5347 `&' operator. CAST_P is true if this expression is the target of a
5350 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5351 class member access expressions [expr.ref].
5353 Returns a representation of the expression. */
5356 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5357 bool member_access_only_p
,
5358 cp_id_kind
* pidk_return
)
5362 cp_id_kind idk
= CP_ID_KIND_NONE
;
5363 tree postfix_expression
= NULL_TREE
;
5364 bool is_member_access
= false;
5366 /* Peek at the next token. */
5367 token
= cp_lexer_peek_token (parser
->lexer
);
5368 /* Some of the productions are determined by keywords. */
5369 keyword
= token
->keyword
;
5379 const char *saved_message
;
5381 /* All of these can be handled in the same way from the point
5382 of view of parsing. Begin by consuming the token
5383 identifying the cast. */
5384 cp_lexer_consume_token (parser
->lexer
);
5386 /* New types cannot be defined in the cast. */
5387 saved_message
= parser
->type_definition_forbidden_message
;
5388 parser
->type_definition_forbidden_message
5389 = G_("types may not be defined in casts");
5391 /* Look for the opening `<'. */
5392 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5393 /* Parse the type to which we are casting. */
5394 type
= cp_parser_type_id (parser
);
5395 /* Look for the closing `>'. */
5396 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5397 /* Restore the old message. */
5398 parser
->type_definition_forbidden_message
= saved_message
;
5400 /* And the expression which is being cast. */
5401 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5402 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
5403 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5405 /* Only type conversions to integral or enumeration types
5406 can be used in constant-expressions. */
5407 if (!cast_valid_in_integral_constant_expression_p (type
)
5408 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5409 return error_mark_node
;
5415 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5419 = build_static_cast (type
, expression
, tf_warning_or_error
);
5423 = build_reinterpret_cast (type
, expression
,
5424 tf_warning_or_error
);
5428 = build_const_cast (type
, expression
, tf_warning_or_error
);
5439 const char *saved_message
;
5440 bool saved_in_type_id_in_expr_p
;
5442 /* Consume the `typeid' token. */
5443 cp_lexer_consume_token (parser
->lexer
);
5444 /* Look for the `(' token. */
5445 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5446 /* Types cannot be defined in a `typeid' expression. */
5447 saved_message
= parser
->type_definition_forbidden_message
;
5448 parser
->type_definition_forbidden_message
5449 = G_("types may not be defined in a %<typeid%> expression");
5450 /* We can't be sure yet whether we're looking at a type-id or an
5452 cp_parser_parse_tentatively (parser
);
5453 /* Try a type-id first. */
5454 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5455 parser
->in_type_id_in_expr_p
= true;
5456 type
= cp_parser_type_id (parser
);
5457 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5458 /* Look for the `)' token. Otherwise, we can't be sure that
5459 we're not looking at an expression: consider `typeid (int
5460 (3))', for example. */
5461 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5462 /* If all went well, simply lookup the type-id. */
5463 if (cp_parser_parse_definitely (parser
))
5464 postfix_expression
= get_typeid (type
);
5465 /* Otherwise, fall back to the expression variant. */
5470 /* Look for an expression. */
5471 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
5472 /* Compute its typeid. */
5473 postfix_expression
= build_typeid (expression
);
5474 /* Look for the `)' token. */
5475 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5477 /* Restore the saved message. */
5478 parser
->type_definition_forbidden_message
= saved_message
;
5479 /* `typeid' may not appear in an integral constant expression. */
5480 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5481 return error_mark_node
;
5488 /* The syntax permitted here is the same permitted for an
5489 elaborated-type-specifier. */
5490 type
= cp_parser_elaborated_type_specifier (parser
,
5491 /*is_friend=*/false,
5492 /*is_declaration=*/false);
5493 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5497 case RID_BUILTIN_SHUFFLE
:
5502 location_t loc
= token
->location
;
5504 cp_lexer_consume_token (parser
->lexer
);
5505 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5506 /*cast_p=*/false, /*allow_expansion_p=*/true,
5507 /*non_constant_p=*/NULL
);
5509 return error_mark_node
;
5511 FOR_EACH_VEC_ELT (tree
, vec
, i
, p
)
5514 if (VEC_length (tree
, vec
) == 2)
5516 c_build_vec_perm_expr
5517 (loc
, VEC_index (tree
, vec
, 0),
5518 NULL_TREE
, VEC_index (tree
, vec
, 1));
5520 else if (VEC_length (tree
, vec
) == 3)
5522 c_build_vec_perm_expr
5523 (loc
, VEC_index (tree
, vec
, 0),
5524 VEC_index (tree
, vec
, 1),
5525 VEC_index (tree
, vec
, 2));
5528 error_at (loc
, "wrong number of arguments to "
5529 "%<__builtin_shuffle%>");
5530 return error_mark_node
;
5539 /* If the next thing is a simple-type-specifier, we may be
5540 looking at a functional cast. We could also be looking at
5541 an id-expression. So, we try the functional cast, and if
5542 that doesn't work we fall back to the primary-expression. */
5543 cp_parser_parse_tentatively (parser
);
5544 /* Look for the simple-type-specifier. */
5545 type
= cp_parser_simple_type_specifier (parser
,
5546 /*decl_specs=*/NULL
,
5547 CP_PARSER_FLAGS_NONE
);
5548 /* Parse the cast itself. */
5549 if (!cp_parser_error_occurred (parser
))
5551 = cp_parser_functional_cast (parser
, type
);
5552 /* If that worked, we're done. */
5553 if (cp_parser_parse_definitely (parser
))
5556 /* If the functional-cast didn't work out, try a
5557 compound-literal. */
5558 if (cp_parser_allow_gnu_extensions_p (parser
)
5559 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5561 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
5562 bool saved_in_type_id_in_expr_p
;
5564 cp_parser_parse_tentatively (parser
);
5565 /* Consume the `('. */
5566 cp_lexer_consume_token (parser
->lexer
);
5567 /* Parse the type. */
5568 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5569 parser
->in_type_id_in_expr_p
= true;
5570 type
= cp_parser_type_id (parser
);
5571 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5572 /* Look for the `)'. */
5573 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5574 /* Look for the `{'. */
5575 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
5576 /* If things aren't going well, there's no need to
5578 if (!cp_parser_error_occurred (parser
))
5580 bool non_constant_p
;
5581 /* Parse the initializer-list. */
5583 = cp_parser_initializer_list (parser
, &non_constant_p
);
5584 /* Allow a trailing `,'. */
5585 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
5586 cp_lexer_consume_token (parser
->lexer
);
5587 /* Look for the final `}'. */
5588 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
5590 /* If that worked, we're definitely looking at a
5591 compound-literal expression. */
5592 if (cp_parser_parse_definitely (parser
))
5594 /* Warn the user that a compound literal is not
5595 allowed in standard C++. */
5596 pedwarn (input_location
, OPT_Wpedantic
, "ISO C++ forbids compound-literals");
5597 /* For simplicity, we disallow compound literals in
5598 constant-expressions. We could
5599 allow compound literals of integer type, whose
5600 initializer was a constant, in constant
5601 expressions. Permitting that usage, as a further
5602 extension, would not change the meaning of any
5603 currently accepted programs. (Of course, as
5604 compound literals are not part of ISO C++, the
5605 standard has nothing to say.) */
5606 if (cp_parser_non_integral_constant_expression (parser
,
5609 postfix_expression
= error_mark_node
;
5612 /* Form the representation of the compound-literal. */
5614 = (finish_compound_literal
5615 (type
, build_constructor (init_list_type_node
,
5617 tf_warning_or_error
));
5622 /* It must be a primary-expression. */
5624 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5625 /*template_arg_p=*/false,
5631 /* Keep looping until the postfix-expression is complete. */
5634 if (idk
== CP_ID_KIND_UNQUALIFIED
5635 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
5636 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
5637 /* It is not a Koenig lookup function call. */
5639 = unqualified_name_lookup_error (postfix_expression
);
5641 /* Peek at the next token. */
5642 token
= cp_lexer_peek_token (parser
->lexer
);
5644 switch (token
->type
)
5646 case CPP_OPEN_SQUARE
:
5647 if (cp_next_tokens_can_be_std_attribute_p (parser
))
5649 cp_parser_error (parser
,
5650 "two consecutive %<[%> shall "
5651 "only introduce an attribute");
5652 return error_mark_node
;
5655 = cp_parser_postfix_open_square_expression (parser
,
5658 idk
= CP_ID_KIND_NONE
;
5659 is_member_access
= false;
5662 case CPP_OPEN_PAREN
:
5663 /* postfix-expression ( expression-list [opt] ) */
5666 bool is_builtin_constant_p
;
5667 bool saved_integral_constant_expression_p
= false;
5668 bool saved_non_integral_constant_expression_p
= false;
5671 is_member_access
= false;
5673 is_builtin_constant_p
5674 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
5675 if (is_builtin_constant_p
)
5677 /* The whole point of __builtin_constant_p is to allow
5678 non-constant expressions to appear as arguments. */
5679 saved_integral_constant_expression_p
5680 = parser
->integral_constant_expression_p
;
5681 saved_non_integral_constant_expression_p
5682 = parser
->non_integral_constant_expression_p
;
5683 parser
->integral_constant_expression_p
= false;
5685 args
= (cp_parser_parenthesized_expression_list
5687 /*cast_p=*/false, /*allow_expansion_p=*/true,
5688 /*non_constant_p=*/NULL
));
5689 if (is_builtin_constant_p
)
5691 parser
->integral_constant_expression_p
5692 = saved_integral_constant_expression_p
;
5693 parser
->non_integral_constant_expression_p
5694 = saved_non_integral_constant_expression_p
;
5699 postfix_expression
= error_mark_node
;
5703 /* Function calls are not permitted in
5704 constant-expressions. */
5705 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
5706 && cp_parser_non_integral_constant_expression (parser
,
5709 postfix_expression
= error_mark_node
;
5710 release_tree_vector (args
);
5715 if (idk
== CP_ID_KIND_UNQUALIFIED
5716 || idk
== CP_ID_KIND_TEMPLATE_ID
)
5718 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
5720 if (!VEC_empty (tree
, args
))
5723 if (!any_type_dependent_arguments_p (args
))
5725 = perform_koenig_lookup (postfix_expression
, args
,
5726 /*include_std=*/false,
5727 tf_warning_or_error
);
5731 = unqualified_fn_lookup_error (postfix_expression
);
5733 /* We do not perform argument-dependent lookup if
5734 normal lookup finds a non-function, in accordance
5735 with the expected resolution of DR 218. */
5736 else if (!VEC_empty (tree
, args
)
5737 && is_overloaded_fn (postfix_expression
))
5739 tree fn
= get_first_fn (postfix_expression
);
5740 fn
= STRIP_TEMPLATE (fn
);
5742 /* Do not do argument dependent lookup if regular
5743 lookup finds a member function or a block-scope
5744 function declaration. [basic.lookup.argdep]/3 */
5745 if (!DECL_FUNCTION_MEMBER_P (fn
)
5746 && !DECL_LOCAL_FUNCTION_P (fn
))
5749 if (!any_type_dependent_arguments_p (args
))
5751 = perform_koenig_lookup (postfix_expression
, args
,
5752 /*include_std=*/false,
5753 tf_warning_or_error
);
5758 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
5760 tree instance
= TREE_OPERAND (postfix_expression
, 0);
5761 tree fn
= TREE_OPERAND (postfix_expression
, 1);
5763 if (processing_template_decl
5764 && (type_dependent_expression_p (instance
)
5765 || (!BASELINK_P (fn
)
5766 && TREE_CODE (fn
) != FIELD_DECL
)
5767 || type_dependent_expression_p (fn
)
5768 || any_type_dependent_arguments_p (args
)))
5771 = build_nt_call_vec (postfix_expression
, args
);
5772 release_tree_vector (args
);
5776 if (BASELINK_P (fn
))
5779 = (build_new_method_call
5780 (instance
, fn
, &args
, NULL_TREE
,
5781 (idk
== CP_ID_KIND_QUALIFIED
5782 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
5785 tf_warning_or_error
));
5789 = finish_call_expr (postfix_expression
, &args
,
5790 /*disallow_virtual=*/false,
5792 tf_warning_or_error
);
5794 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
5795 || TREE_CODE (postfix_expression
) == MEMBER_REF
5796 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
5797 postfix_expression
= (build_offset_ref_call_from_tree
5798 (postfix_expression
, &args
,
5799 tf_warning_or_error
));
5800 else if (idk
== CP_ID_KIND_QUALIFIED
)
5801 /* A call to a static class member, or a namespace-scope
5804 = finish_call_expr (postfix_expression
, &args
,
5805 /*disallow_virtual=*/true,
5807 tf_warning_or_error
);
5809 /* All other function calls. */
5811 = finish_call_expr (postfix_expression
, &args
,
5812 /*disallow_virtual=*/false,
5814 tf_warning_or_error
);
5816 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5817 idk
= CP_ID_KIND_NONE
;
5819 release_tree_vector (args
);
5825 /* postfix-expression . template [opt] id-expression
5826 postfix-expression . pseudo-destructor-name
5827 postfix-expression -> template [opt] id-expression
5828 postfix-expression -> pseudo-destructor-name */
5830 /* Consume the `.' or `->' operator. */
5831 cp_lexer_consume_token (parser
->lexer
);
5834 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
5839 is_member_access
= true;
5843 /* postfix-expression ++ */
5844 /* Consume the `++' token. */
5845 cp_lexer_consume_token (parser
->lexer
);
5846 /* Generate a representation for the complete expression. */
5848 = finish_increment_expr (postfix_expression
,
5849 POSTINCREMENT_EXPR
);
5850 /* Increments may not appear in constant-expressions. */
5851 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
5852 postfix_expression
= error_mark_node
;
5853 idk
= CP_ID_KIND_NONE
;
5854 is_member_access
= false;
5857 case CPP_MINUS_MINUS
:
5858 /* postfix-expression -- */
5859 /* Consume the `--' token. */
5860 cp_lexer_consume_token (parser
->lexer
);
5861 /* Generate a representation for the complete expression. */
5863 = finish_increment_expr (postfix_expression
,
5864 POSTDECREMENT_EXPR
);
5865 /* Decrements may not appear in constant-expressions. */
5866 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
5867 postfix_expression
= error_mark_node
;
5868 idk
= CP_ID_KIND_NONE
;
5869 is_member_access
= false;
5873 if (pidk_return
!= NULL
)
5874 * pidk_return
= idk
;
5875 if (member_access_only_p
)
5876 return is_member_access
? postfix_expression
: error_mark_node
;
5878 return postfix_expression
;
5882 /* We should never get here. */
5884 return error_mark_node
;
5887 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5888 by cp_parser_builtin_offsetof. We're looking for
5890 postfix-expression [ expression ]
5891 postfix-expression [ braced-init-list ] (C++11)
5893 FOR_OFFSETOF is set if we're being called in that context, which
5894 changes how we deal with integer constant expressions. */
5897 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
5898 tree postfix_expression
,
5902 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5904 /* Consume the `[' token. */
5905 cp_lexer_consume_token (parser
->lexer
);
5907 /* Parse the index expression. */
5908 /* ??? For offsetof, there is a question of what to allow here. If
5909 offsetof is not being used in an integral constant expression context,
5910 then we *could* get the right answer by computing the value at runtime.
5911 If we are in an integral constant expression context, then we might
5912 could accept any constant expression; hard to say without analysis.
5913 Rather than open the barn door too wide right away, allow only integer
5914 constant expressions here. */
5916 index
= cp_parser_constant_expression (parser
, false, NULL
);
5919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5921 bool expr_nonconst_p
;
5922 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
5923 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
5926 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5929 /* Look for the closing `]'. */
5930 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
5932 /* Build the ARRAY_REF. */
5933 postfix_expression
= grok_array_decl (loc
, postfix_expression
, index
);
5935 /* When not doing offsetof, array references are not permitted in
5936 constant-expressions. */
5938 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
5939 postfix_expression
= error_mark_node
;
5941 return postfix_expression
;
5944 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5945 by cp_parser_builtin_offsetof. We're looking for
5947 postfix-expression . template [opt] id-expression
5948 postfix-expression . pseudo-destructor-name
5949 postfix-expression -> template [opt] id-expression
5950 postfix-expression -> pseudo-destructor-name
5952 FOR_OFFSETOF is set if we're being called in that context. That sorta
5953 limits what of the above we'll actually accept, but nevermind.
5954 TOKEN_TYPE is the "." or "->" token, which will already have been
5955 removed from the stream. */
5958 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
5959 enum cpp_ttype token_type
,
5960 tree postfix_expression
,
5961 bool for_offsetof
, cp_id_kind
*idk
,
5962 location_t location
)
5966 bool pseudo_destructor_p
;
5967 tree scope
= NULL_TREE
;
5969 /* If this is a `->' operator, dereference the pointer. */
5970 if (token_type
== CPP_DEREF
)
5971 postfix_expression
= build_x_arrow (location
, postfix_expression
,
5972 tf_warning_or_error
);
5973 /* Check to see whether or not the expression is type-dependent. */
5974 dependent_p
= type_dependent_expression_p (postfix_expression
);
5975 /* The identifier following the `->' or `.' is not qualified. */
5976 parser
->scope
= NULL_TREE
;
5977 parser
->qualifying_scope
= NULL_TREE
;
5978 parser
->object_scope
= NULL_TREE
;
5979 *idk
= CP_ID_KIND_NONE
;
5981 /* Enter the scope corresponding to the type of the object
5982 given by the POSTFIX_EXPRESSION. */
5983 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
5985 scope
= TREE_TYPE (postfix_expression
);
5986 /* According to the standard, no expression should ever have
5987 reference type. Unfortunately, we do not currently match
5988 the standard in this respect in that our internal representation
5989 of an expression may have reference type even when the standard
5990 says it does not. Therefore, we have to manually obtain the
5991 underlying type here. */
5992 scope
= non_reference (scope
);
5993 /* The type of the POSTFIX_EXPRESSION must be complete. */
5994 if (scope
== unknown_type_node
)
5996 error_at (location
, "%qE does not have class type",
5997 postfix_expression
);
6000 /* Unlike the object expression in other contexts, *this is not
6001 required to be of complete type for purposes of class member
6002 access (5.2.5) outside the member function body. */
6003 else if (scope
!= current_class_ref
6004 && !(processing_template_decl
&& scope
== current_class_type
))
6005 scope
= complete_type_or_else (scope
, NULL_TREE
);
6006 /* Let the name lookup machinery know that we are processing a
6007 class member access expression. */
6008 parser
->context
->object_type
= scope
;
6009 /* If something went wrong, we want to be able to discern that case,
6010 as opposed to the case where there was no SCOPE due to the type
6011 of expression being dependent. */
6013 scope
= error_mark_node
;
6014 /* If the SCOPE was erroneous, make the various semantic analysis
6015 functions exit quickly -- and without issuing additional error
6017 if (scope
== error_mark_node
)
6018 postfix_expression
= error_mark_node
;
6021 /* Assume this expression is not a pseudo-destructor access. */
6022 pseudo_destructor_p
= false;
6024 /* If the SCOPE is a scalar type, then, if this is a valid program,
6025 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6026 is type dependent, it can be pseudo-destructor-name or something else.
6027 Try to parse it as pseudo-destructor-name first. */
6028 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6033 cp_parser_parse_tentatively (parser
);
6034 /* Parse the pseudo-destructor-name. */
6036 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
6038 && (cp_parser_error_occurred (parser
)
6039 || TREE_CODE (type
) != TYPE_DECL
6040 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
6041 cp_parser_abort_tentative_parse (parser
);
6042 else if (cp_parser_parse_definitely (parser
))
6044 pseudo_destructor_p
= true;
6046 = finish_pseudo_destructor_expr (postfix_expression
,
6047 s
, TREE_TYPE (type
));
6051 if (!pseudo_destructor_p
)
6053 /* If the SCOPE is not a scalar type, we are looking at an
6054 ordinary class member access expression, rather than a
6055 pseudo-destructor-name. */
6057 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6058 /* Parse the id-expression. */
6059 name
= (cp_parser_id_expression
6061 cp_parser_optional_template_keyword (parser
),
6062 /*check_dependency_p=*/true,
6064 /*declarator_p=*/false,
6065 /*optional_p=*/false));
6066 /* In general, build a SCOPE_REF if the member name is qualified.
6067 However, if the name was not dependent and has already been
6068 resolved; there is no need to build the SCOPE_REF. For example;
6070 struct X { void f(); };
6071 template <typename T> void f(T* t) { t->X::f(); }
6073 Even though "t" is dependent, "X::f" is not and has been resolved
6074 to a BASELINK; there is no need to include scope information. */
6076 /* But we do need to remember that there was an explicit scope for
6077 virtual function calls. */
6079 *idk
= CP_ID_KIND_QUALIFIED
;
6081 /* If the name is a template-id that names a type, we will get a
6082 TYPE_DECL here. That is invalid code. */
6083 if (TREE_CODE (name
) == TYPE_DECL
)
6085 error_at (token
->location
, "invalid use of %qD", name
);
6086 postfix_expression
= error_mark_node
;
6090 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6092 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6094 error_at (token
->location
, "%<%D::%D%> is not a class member",
6095 parser
->scope
, name
);
6096 postfix_expression
= error_mark_node
;
6099 name
= build_qualified_name (/*type=*/NULL_TREE
,
6103 parser
->scope
= NULL_TREE
;
6104 parser
->qualifying_scope
= NULL_TREE
;
6105 parser
->object_scope
= NULL_TREE
;
6107 if (parser
->scope
&& name
&& BASELINK_P (name
))
6108 adjust_result_of_qualified_name_lookup
6109 (name
, parser
->scope
, scope
);
6111 = finish_class_member_access_expr (postfix_expression
, name
,
6113 tf_warning_or_error
);
6117 /* We no longer need to look up names in the scope of the object on
6118 the left-hand side of the `.' or `->' operator. */
6119 parser
->context
->object_type
= NULL_TREE
;
6121 /* Outside of offsetof, these operators may not appear in
6122 constant-expressions. */
6124 && (cp_parser_non_integral_constant_expression
6125 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6126 postfix_expression
= error_mark_node
;
6128 return postfix_expression
;
6131 /* Parse a parenthesized expression-list.
6134 assignment-expression
6135 expression-list, assignment-expression
6140 identifier, expression-list
6142 CAST_P is true if this expression is the target of a cast.
6144 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6147 Returns a vector of trees. Each element is a representation of an
6148 assignment-expression. NULL is returned if the ( and or ) are
6149 missing. An empty, but allocated, vector is returned on no
6150 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6151 if we are parsing an attribute list for an attribute that wants a
6152 plain identifier argument, normal_attr for an attribute that wants
6153 an expression, or non_attr if we aren't parsing an attribute list. If
6154 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6155 not all of the expressions in the list were constant. */
6157 static VEC(tree
,gc
) *
6158 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6159 int is_attribute_list
,
6161 bool allow_expansion_p
,
6162 bool *non_constant_p
)
6164 VEC(tree
,gc
) *expression_list
;
6165 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6166 tree identifier
= NULL_TREE
;
6167 bool saved_greater_than_is_operator_p
;
6169 /* Assume all the expressions will be constant. */
6171 *non_constant_p
= false;
6173 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6176 expression_list
= make_tree_vector ();
6178 /* Within a parenthesized expression, a `>' token is always
6179 the greater-than operator. */
6180 saved_greater_than_is_operator_p
6181 = parser
->greater_than_is_operator_p
;
6182 parser
->greater_than_is_operator_p
= true;
6184 /* Consume expressions until there are no more. */
6185 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6190 /* At the beginning of attribute lists, check to see if the
6191 next token is an identifier. */
6192 if (is_attribute_list
== id_attr
6193 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6197 /* Consume the identifier. */
6198 token
= cp_lexer_consume_token (parser
->lexer
);
6199 /* Save the identifier. */
6200 identifier
= token
->u
.value
;
6204 bool expr_non_constant_p
;
6206 /* Parse the next assignment-expression. */
6207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6209 /* A braced-init-list. */
6210 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6211 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6212 if (non_constant_p
&& expr_non_constant_p
)
6213 *non_constant_p
= true;
6215 else if (non_constant_p
)
6217 expr
= (cp_parser_constant_expression
6218 (parser
, /*allow_non_constant_p=*/true,
6219 &expr_non_constant_p
));
6220 if (expr_non_constant_p
)
6221 *non_constant_p
= true;
6224 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
6227 expr
= fold_non_dependent_expr (expr
);
6229 /* If we have an ellipsis, then this is an expression
6231 if (allow_expansion_p
6232 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6234 /* Consume the `...'. */
6235 cp_lexer_consume_token (parser
->lexer
);
6237 /* Build the argument pack. */
6238 expr
= make_pack_expansion (expr
);
6241 /* Add it to the list. We add error_mark_node
6242 expressions to the list, so that we can still tell if
6243 the correct form for a parenthesized expression-list
6244 is found. That gives better errors. */
6245 VEC_safe_push (tree
, gc
, expression_list
, expr
);
6247 if (expr
== error_mark_node
)
6251 /* After the first item, attribute lists look the same as
6252 expression lists. */
6253 is_attribute_list
= non_attr
;
6256 /* If the next token isn't a `,', then we are done. */
6257 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6260 /* Otherwise, consume the `,' and keep going. */
6261 cp_lexer_consume_token (parser
->lexer
);
6264 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6269 /* We try and resync to an unnested comma, as that will give the
6270 user better diagnostics. */
6271 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6272 /*recovering=*/true,
6274 /*consume_paren=*/true);
6279 parser
->greater_than_is_operator_p
6280 = saved_greater_than_is_operator_p
;
6285 parser
->greater_than_is_operator_p
6286 = saved_greater_than_is_operator_p
;
6289 VEC_safe_insert (tree
, gc
, expression_list
, 0, identifier
);
6291 return expression_list
;
6294 /* Parse a pseudo-destructor-name.
6296 pseudo-destructor-name:
6297 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6298 :: [opt] nested-name-specifier template template-id :: ~ type-name
6299 :: [opt] nested-name-specifier [opt] ~ type-name
6301 If either of the first two productions is used, sets *SCOPE to the
6302 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6303 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6304 or ERROR_MARK_NODE if the parse fails. */
6307 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6311 bool nested_name_specifier_p
;
6313 /* Assume that things will not work out. */
6314 *type
= error_mark_node
;
6316 /* Look for the optional `::' operator. */
6317 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
6318 /* Look for the optional nested-name-specifier. */
6319 nested_name_specifier_p
6320 = (cp_parser_nested_name_specifier_opt (parser
,
6321 /*typename_keyword_p=*/false,
6322 /*check_dependency_p=*/true,
6324 /*is_declaration=*/false)
6326 /* Now, if we saw a nested-name-specifier, we might be doing the
6327 second production. */
6328 if (nested_name_specifier_p
6329 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
6331 /* Consume the `template' keyword. */
6332 cp_lexer_consume_token (parser
->lexer
);
6333 /* Parse the template-id. */
6334 cp_parser_template_id (parser
,
6335 /*template_keyword_p=*/true,
6336 /*check_dependency_p=*/false,
6338 /*is_declaration=*/true);
6339 /* Look for the `::' token. */
6340 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6342 /* If the next token is not a `~', then there might be some
6343 additional qualification. */
6344 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
6346 /* At this point, we're looking for "type-name :: ~". The type-name
6347 must not be a class-name, since this is a pseudo-destructor. So,
6348 it must be either an enum-name, or a typedef-name -- both of which
6349 are just identifiers. So, we peek ahead to check that the "::"
6350 and "~" tokens are present; if they are not, then we can avoid
6351 calling type_name. */
6352 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
6353 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
6354 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
6356 cp_parser_error (parser
, "non-scalar type");
6360 /* Look for the type-name. */
6361 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6362 if (*scope
== error_mark_node
)
6365 /* Look for the `::' token. */
6366 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6371 /* Look for the `~'. */
6372 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
6374 /* Once we see the ~, this has to be a pseudo-destructor. */
6375 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
6376 cp_parser_commit_to_tentative_parse (parser
);
6378 /* Look for the type-name again. We are not responsible for
6379 checking that it matches the first type-name. */
6380 *type
= cp_parser_nonclass_name (parser
);
6383 /* Parse a unary-expression.
6389 unary-operator cast-expression
6390 sizeof unary-expression
6392 alignof ( type-id ) [C++0x]
6399 __extension__ cast-expression
6400 __alignof__ unary-expression
6401 __alignof__ ( type-id )
6402 alignof unary-expression [C++0x]
6403 __real__ cast-expression
6404 __imag__ cast-expression
6407 ADDRESS_P is true iff the unary-expression is appearing as the
6408 operand of the `&' operator. CAST_P is true if this expression is
6409 the target of a cast.
6411 Returns a representation of the expression. */
6414 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6418 enum tree_code unary_operator
;
6420 /* Peek at the next token. */
6421 token
= cp_lexer_peek_token (parser
->lexer
);
6422 /* Some keywords give away the kind of expression. */
6423 if (token
->type
== CPP_KEYWORD
)
6425 enum rid keyword
= token
->keyword
;
6434 location_t first_loc
;
6436 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
6437 /* Consume the token. */
6438 cp_lexer_consume_token (parser
->lexer
);
6439 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6440 /* Parse the operand. */
6441 operand
= cp_parser_sizeof_operand (parser
, keyword
);
6443 if (TYPE_P (operand
))
6444 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
6447 /* ISO C++ defines alignof only with types, not with
6448 expressions. So pedwarn if alignof is used with a non-
6449 type expression. However, __alignof__ is ok. */
6450 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
6451 pedwarn (token
->location
, OPT_Wpedantic
,
6452 "ISO C++ does not allow %<alignof%> "
6455 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
6457 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6458 SIZEOF_EXPR with the original operand. */
6459 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
6461 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
6463 if (!processing_template_decl
&& TYPE_P (operand
))
6465 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
6466 build1 (NOP_EXPR
, operand
,
6468 SIZEOF_EXPR_TYPE_P (ret
) = 1;
6471 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
6472 TREE_SIDE_EFFECTS (ret
) = 0;
6473 TREE_READONLY (ret
) = 1;
6475 SET_EXPR_LOCATION (ret
, first_loc
);
6481 return cp_parser_new_expression (parser
);
6484 return cp_parser_delete_expression (parser
);
6488 /* The saved value of the PEDANTIC flag. */
6492 /* Save away the PEDANTIC flag. */
6493 cp_parser_extension_opt (parser
, &saved_pedantic
);
6494 /* Parse the cast-expression. */
6495 expr
= cp_parser_simple_cast_expression (parser
);
6496 /* Restore the PEDANTIC flag. */
6497 pedantic
= saved_pedantic
;
6507 /* Consume the `__real__' or `__imag__' token. */
6508 cp_lexer_consume_token (parser
->lexer
);
6509 /* Parse the cast-expression. */
6510 expression
= cp_parser_simple_cast_expression (parser
);
6511 /* Create the complete representation. */
6512 return build_x_unary_op (token
->location
,
6513 (keyword
== RID_REALPART
6514 ? REALPART_EXPR
: IMAGPART_EXPR
),
6516 tf_warning_or_error
);
6520 case RID_TRANSACTION_ATOMIC
:
6521 case RID_TRANSACTION_RELAXED
:
6522 return cp_parser_transaction_expression (parser
, keyword
);
6527 const char *saved_message
;
6528 bool saved_integral_constant_expression_p
;
6529 bool saved_non_integral_constant_expression_p
;
6530 bool saved_greater_than_is_operator_p
;
6532 cp_lexer_consume_token (parser
->lexer
);
6533 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6535 saved_message
= parser
->type_definition_forbidden_message
;
6536 parser
->type_definition_forbidden_message
6537 = G_("types may not be defined in %<noexcept%> expressions");
6539 saved_integral_constant_expression_p
6540 = parser
->integral_constant_expression_p
;
6541 saved_non_integral_constant_expression_p
6542 = parser
->non_integral_constant_expression_p
;
6543 parser
->integral_constant_expression_p
= false;
6545 saved_greater_than_is_operator_p
6546 = parser
->greater_than_is_operator_p
;
6547 parser
->greater_than_is_operator_p
= true;
6549 ++cp_unevaluated_operand
;
6550 ++c_inhibit_evaluation_warnings
;
6551 expr
= cp_parser_expression (parser
, false, NULL
);
6552 --c_inhibit_evaluation_warnings
;
6553 --cp_unevaluated_operand
;
6555 parser
->greater_than_is_operator_p
6556 = saved_greater_than_is_operator_p
;
6558 parser
->integral_constant_expression_p
6559 = saved_integral_constant_expression_p
;
6560 parser
->non_integral_constant_expression_p
6561 = saved_non_integral_constant_expression_p
;
6563 parser
->type_definition_forbidden_message
= saved_message
;
6565 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6566 return finish_noexcept_expr (expr
, tf_warning_or_error
);
6574 /* Look for the `:: new' and `:: delete', which also signal the
6575 beginning of a new-expression, or delete-expression,
6576 respectively. If the next token is `::', then it might be one of
6578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
6582 /* See if the token after the `::' is one of the keywords in
6583 which we're interested. */
6584 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
6585 /* If it's `new', we have a new-expression. */
6586 if (keyword
== RID_NEW
)
6587 return cp_parser_new_expression (parser
);
6588 /* Similarly, for `delete'. */
6589 else if (keyword
== RID_DELETE
)
6590 return cp_parser_delete_expression (parser
);
6593 /* Look for a unary operator. */
6594 unary_operator
= cp_parser_unary_operator (token
);
6595 /* The `++' and `--' operators can be handled similarly, even though
6596 they are not technically unary-operators in the grammar. */
6597 if (unary_operator
== ERROR_MARK
)
6599 if (token
->type
== CPP_PLUS_PLUS
)
6600 unary_operator
= PREINCREMENT_EXPR
;
6601 else if (token
->type
== CPP_MINUS_MINUS
)
6602 unary_operator
= PREDECREMENT_EXPR
;
6603 /* Handle the GNU address-of-label extension. */
6604 else if (cp_parser_allow_gnu_extensions_p (parser
)
6605 && token
->type
== CPP_AND_AND
)
6609 location_t loc
= token
->location
;
6611 /* Consume the '&&' token. */
6612 cp_lexer_consume_token (parser
->lexer
);
6613 /* Look for the identifier. */
6614 identifier
= cp_parser_identifier (parser
);
6615 /* Create an expression representing the address. */
6616 expression
= finish_label_address_expr (identifier
, loc
);
6617 if (cp_parser_non_integral_constant_expression (parser
,
6619 expression
= error_mark_node
;
6623 if (unary_operator
!= ERROR_MARK
)
6625 tree cast_expression
;
6626 tree expression
= error_mark_node
;
6627 non_integral_constant non_constant_p
= NIC_NONE
;
6628 location_t loc
= token
->location
;
6630 /* Consume the operator token. */
6631 token
= cp_lexer_consume_token (parser
->lexer
);
6632 /* Parse the cast-expression. */
6634 = cp_parser_cast_expression (parser
,
6635 unary_operator
== ADDR_EXPR
,
6636 /*cast_p=*/false, pidk
);
6637 /* Now, build an appropriate representation. */
6638 switch (unary_operator
)
6641 non_constant_p
= NIC_STAR
;
6642 expression
= build_x_indirect_ref (loc
, cast_expression
,
6644 tf_warning_or_error
);
6648 non_constant_p
= NIC_ADDR
;
6651 expression
= build_x_unary_op (loc
, unary_operator
,
6653 tf_warning_or_error
);
6656 case PREINCREMENT_EXPR
:
6657 case PREDECREMENT_EXPR
:
6658 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
6659 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
6661 case UNARY_PLUS_EXPR
:
6663 case TRUTH_NOT_EXPR
:
6664 expression
= finish_unary_op_expr (loc
, unary_operator
,
6672 if (non_constant_p
!= NIC_NONE
6673 && cp_parser_non_integral_constant_expression (parser
,
6675 expression
= error_mark_node
;
6680 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
6681 /*member_access_only_p=*/false,
6685 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6686 unary-operator, the corresponding tree code is returned. */
6688 static enum tree_code
6689 cp_parser_unary_operator (cp_token
* token
)
6691 switch (token
->type
)
6694 return INDIRECT_REF
;
6700 return UNARY_PLUS_EXPR
;
6706 return TRUTH_NOT_EXPR
;
6709 return BIT_NOT_EXPR
;
6716 /* Parse a new-expression.
6719 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6720 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6722 Returns a representation of the expression. */
6725 cp_parser_new_expression (cp_parser
* parser
)
6727 bool global_scope_p
;
6728 VEC(tree
,gc
) *placement
;
6730 VEC(tree
,gc
) *initializer
;
6731 tree nelts
= NULL_TREE
;
6734 /* Look for the optional `::' operator. */
6736 = (cp_parser_global_scope_opt (parser
,
6737 /*current_scope_valid_p=*/false)
6739 /* Look for the `new' operator. */
6740 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
6741 /* There's no easy way to tell a new-placement from the
6742 `( type-id )' construct. */
6743 cp_parser_parse_tentatively (parser
);
6744 /* Look for a new-placement. */
6745 placement
= cp_parser_new_placement (parser
);
6746 /* If that didn't work out, there's no new-placement. */
6747 if (!cp_parser_parse_definitely (parser
))
6749 if (placement
!= NULL
)
6750 release_tree_vector (placement
);
6754 /* If the next token is a `(', then we have a parenthesized
6756 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6759 const char *saved_message
= parser
->type_definition_forbidden_message
;
6761 /* Consume the `('. */
6762 cp_lexer_consume_token (parser
->lexer
);
6764 /* Parse the type-id. */
6765 parser
->type_definition_forbidden_message
6766 = G_("types may not be defined in a new-expression");
6767 type
= cp_parser_type_id (parser
);
6768 parser
->type_definition_forbidden_message
= saved_message
;
6770 /* Look for the closing `)'. */
6771 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6772 token
= cp_lexer_peek_token (parser
->lexer
);
6773 /* There should not be a direct-new-declarator in this production,
6774 but GCC used to allowed this, so we check and emit a sensible error
6775 message for this case. */
6776 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6778 error_at (token
->location
,
6779 "array bound forbidden after parenthesized type-id");
6780 inform (token
->location
,
6781 "try removing the parentheses around the type-id");
6782 cp_parser_direct_new_declarator (parser
);
6785 /* Otherwise, there must be a new-type-id. */
6787 type
= cp_parser_new_type_id (parser
, &nelts
);
6789 /* If the next token is a `(' or '{', then we have a new-initializer. */
6790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
6791 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6792 initializer
= cp_parser_new_initializer (parser
);
6796 /* A new-expression may not appear in an integral constant
6798 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
6799 ret
= error_mark_node
;
6802 /* Create a representation of the new-expression. */
6803 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
6804 tf_warning_or_error
);
6807 if (placement
!= NULL
)
6808 release_tree_vector (placement
);
6809 if (initializer
!= NULL
)
6810 release_tree_vector (initializer
);
6815 /* Parse a new-placement.
6820 Returns the same representation as for an expression-list. */
6822 static VEC(tree
,gc
) *
6823 cp_parser_new_placement (cp_parser
* parser
)
6825 VEC(tree
,gc
) *expression_list
;
6827 /* Parse the expression-list. */
6828 expression_list
= (cp_parser_parenthesized_expression_list
6829 (parser
, non_attr
, /*cast_p=*/false,
6830 /*allow_expansion_p=*/true,
6831 /*non_constant_p=*/NULL
));
6833 return expression_list
;
6836 /* Parse a new-type-id.
6839 type-specifier-seq new-declarator [opt]
6841 Returns the TYPE allocated. If the new-type-id indicates an array
6842 type, *NELTS is set to the number of elements in the last array
6843 bound; the TYPE will not include the last array bound. */
6846 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
6848 cp_decl_specifier_seq type_specifier_seq
;
6849 cp_declarator
*new_declarator
;
6850 cp_declarator
*declarator
;
6851 cp_declarator
*outer_declarator
;
6852 const char *saved_message
;
6854 /* The type-specifier sequence must not contain type definitions.
6855 (It cannot contain declarations of new types either, but if they
6856 are not definitions we will catch that because they are not
6858 saved_message
= parser
->type_definition_forbidden_message
;
6859 parser
->type_definition_forbidden_message
6860 = G_("types may not be defined in a new-type-id");
6861 /* Parse the type-specifier-seq. */
6862 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
6863 /*is_trailing_return=*/false,
6864 &type_specifier_seq
);
6865 /* Restore the old message. */
6866 parser
->type_definition_forbidden_message
= saved_message
;
6868 if (type_specifier_seq
.type
== error_mark_node
)
6869 return error_mark_node
;
6871 /* Parse the new-declarator. */
6872 new_declarator
= cp_parser_new_declarator_opt (parser
);
6874 /* Determine the number of elements in the last array dimension, if
6877 /* Skip down to the last array dimension. */
6878 declarator
= new_declarator
;
6879 outer_declarator
= NULL
;
6880 while (declarator
&& (declarator
->kind
== cdk_pointer
6881 || declarator
->kind
== cdk_ptrmem
))
6883 outer_declarator
= declarator
;
6884 declarator
= declarator
->declarator
;
6887 && declarator
->kind
== cdk_array
6888 && declarator
->declarator
6889 && declarator
->declarator
->kind
== cdk_array
)
6891 outer_declarator
= declarator
;
6892 declarator
= declarator
->declarator
;
6895 if (declarator
&& declarator
->kind
== cdk_array
)
6897 *nelts
= declarator
->u
.array
.bounds
;
6898 if (*nelts
== error_mark_node
)
6899 *nelts
= integer_one_node
;
6901 if (outer_declarator
)
6902 outer_declarator
->declarator
= declarator
->declarator
;
6904 new_declarator
= NULL
;
6907 return groktypename (&type_specifier_seq
, new_declarator
, false);
6910 /* Parse an (optional) new-declarator.
6913 ptr-operator new-declarator [opt]
6914 direct-new-declarator
6916 Returns the declarator. */
6918 static cp_declarator
*
6919 cp_parser_new_declarator_opt (cp_parser
* parser
)
6921 enum tree_code code
;
6922 tree type
, std_attributes
= NULL_TREE
;
6923 cp_cv_quals cv_quals
;
6925 /* We don't know if there's a ptr-operator next, or not. */
6926 cp_parser_parse_tentatively (parser
);
6927 /* Look for a ptr-operator. */
6928 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
6929 /* If that worked, look for more new-declarators. */
6930 if (cp_parser_parse_definitely (parser
))
6932 cp_declarator
*declarator
;
6934 /* Parse another optional declarator. */
6935 declarator
= cp_parser_new_declarator_opt (parser
);
6937 declarator
= cp_parser_make_indirect_declarator
6938 (code
, type
, cv_quals
, declarator
, std_attributes
);
6943 /* If the next token is a `[', there is a direct-new-declarator. */
6944 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6945 return cp_parser_direct_new_declarator (parser
);
6950 /* Parse a direct-new-declarator.
6952 direct-new-declarator:
6954 direct-new-declarator [constant-expression]
6958 static cp_declarator
*
6959 cp_parser_direct_new_declarator (cp_parser
* parser
)
6961 cp_declarator
*declarator
= NULL
;
6968 /* Look for the opening `['. */
6969 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
6971 token
= cp_lexer_peek_token (parser
->lexer
);
6972 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6973 /* The standard requires that the expression have integral
6974 type. DR 74 adds enumeration types. We believe that the
6975 real intent is that these expressions be handled like the
6976 expression in a `switch' condition, which also allows
6977 classes with a single conversion to integral or
6978 enumeration type. */
6979 if (!processing_template_decl
)
6982 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
6987 error_at (token
->location
,
6988 "expression in new-declarator must have integral "
6989 "or enumeration type");
6990 expression
= error_mark_node
;
6994 /* Look for the closing `]'. */
6995 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6997 /* Add this bound to the declarator. */
6998 declarator
= make_array_declarator (declarator
, expression
);
7000 /* If the next token is not a `[', then there are no more
7002 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7009 /* Parse a new-initializer.
7012 ( expression-list [opt] )
7015 Returns a representation of the expression-list. */
7017 static VEC(tree
,gc
) *
7018 cp_parser_new_initializer (cp_parser
* parser
)
7020 VEC(tree
,gc
) *expression_list
;
7022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7025 bool expr_non_constant_p
;
7026 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7027 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7028 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7029 expression_list
= make_tree_vector_single (t
);
7032 expression_list
= (cp_parser_parenthesized_expression_list
7033 (parser
, non_attr
, /*cast_p=*/false,
7034 /*allow_expansion_p=*/true,
7035 /*non_constant_p=*/NULL
));
7037 return expression_list
;
7040 /* Parse a delete-expression.
7043 :: [opt] delete cast-expression
7044 :: [opt] delete [ ] cast-expression
7046 Returns a representation of the expression. */
7049 cp_parser_delete_expression (cp_parser
* parser
)
7051 bool global_scope_p
;
7055 /* Look for the optional `::' operator. */
7057 = (cp_parser_global_scope_opt (parser
,
7058 /*current_scope_valid_p=*/false)
7060 /* Look for the `delete' keyword. */
7061 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7062 /* See if the array syntax is in use. */
7063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7065 /* Consume the `[' token. */
7066 cp_lexer_consume_token (parser
->lexer
);
7067 /* Look for the `]' token. */
7068 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7069 /* Remember that this is the `[]' construct. */
7075 /* Parse the cast-expression. */
7076 expression
= cp_parser_simple_cast_expression (parser
);
7078 /* A delete-expression may not appear in an integral constant
7080 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7081 return error_mark_node
;
7083 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7084 tf_warning_or_error
);
7087 /* Returns true if TOKEN may start a cast-expression and false
7091 cp_parser_token_starts_cast_expression (cp_token
*token
)
7093 switch (token
->type
)
7099 case CPP_CLOSE_SQUARE
:
7100 case CPP_CLOSE_PAREN
:
7101 case CPP_CLOSE_BRACE
:
7105 case CPP_DEREF_STAR
:
7113 case CPP_GREATER_EQ
:
7133 /* '[' may start a primary-expression in obj-c++. */
7134 case CPP_OPEN_SQUARE
:
7135 return c_dialect_objc ();
7142 /* Parse a cast-expression.
7146 ( type-id ) cast-expression
7148 ADDRESS_P is true iff the unary-expression is appearing as the
7149 operand of the `&' operator. CAST_P is true if this expression is
7150 the target of a cast.
7152 Returns a representation of the expression. */
7155 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7158 /* If it's a `(', then we might be looking at a cast. */
7159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7161 tree type
= NULL_TREE
;
7162 tree expr
= NULL_TREE
;
7163 bool compound_literal_p
;
7164 const char *saved_message
;
7166 /* There's no way to know yet whether or not this is a cast.
7167 For example, `(int (3))' is a unary-expression, while `(int)
7168 3' is a cast. So, we resort to parsing tentatively. */
7169 cp_parser_parse_tentatively (parser
);
7170 /* Types may not be defined in a cast. */
7171 saved_message
= parser
->type_definition_forbidden_message
;
7172 parser
->type_definition_forbidden_message
7173 = G_("types may not be defined in casts");
7174 /* Consume the `('. */
7175 cp_lexer_consume_token (parser
->lexer
);
7176 /* A very tricky bit is that `(struct S) { 3 }' is a
7177 compound-literal (which we permit in C++ as an extension).
7178 But, that construct is not a cast-expression -- it is a
7179 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7180 is legal; if the compound-literal were a cast-expression,
7181 you'd need an extra set of parentheses.) But, if we parse
7182 the type-id, and it happens to be a class-specifier, then we
7183 will commit to the parse at that point, because we cannot
7184 undo the action that is done when creating a new class. So,
7185 then we cannot back up and do a postfix-expression.
7187 Therefore, we scan ahead to the closing `)', and check to see
7188 if the token after the `)' is a `{'. If so, we are not
7189 looking at a cast-expression.
7191 Save tokens so that we can put them back. */
7192 cp_lexer_save_tokens (parser
->lexer
);
7193 /* Skip tokens until the next token is a closing parenthesis.
7194 If we find the closing `)', and the next token is a `{', then
7195 we are looking at a compound-literal. */
7197 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7198 /*consume_paren=*/true)
7199 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
7200 /* Roll back the tokens we skipped. */
7201 cp_lexer_rollback_tokens (parser
->lexer
);
7202 /* If we were looking at a compound-literal, simulate an error
7203 so that the call to cp_parser_parse_definitely below will
7205 if (compound_literal_p
)
7206 cp_parser_simulate_error (parser
);
7209 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7210 parser
->in_type_id_in_expr_p
= true;
7211 /* Look for the type-id. */
7212 type
= cp_parser_type_id (parser
);
7213 /* Look for the closing `)'. */
7214 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7215 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7218 /* Restore the saved message. */
7219 parser
->type_definition_forbidden_message
= saved_message
;
7221 /* At this point this can only be either a cast or a
7222 parenthesized ctor such as `(T ())' that looks like a cast to
7223 function returning T. */
7224 if (!cp_parser_error_occurred (parser
)
7225 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7228 cp_parser_parse_definitely (parser
);
7229 expr
= cp_parser_cast_expression (parser
,
7230 /*address_p=*/false,
7231 /*cast_p=*/true, pidk
);
7233 /* Warn about old-style casts, if so requested. */
7234 if (warn_old_style_cast
7235 && !in_system_header
7236 && !VOID_TYPE_P (type
)
7237 && current_lang_name
!= lang_name_c
)
7238 warning (OPT_Wold_style_cast
, "use of old-style cast");
7240 /* Only type conversions to integral or enumeration types
7241 can be used in constant-expressions. */
7242 if (!cast_valid_in_integral_constant_expression_p (type
)
7243 && cp_parser_non_integral_constant_expression (parser
,
7245 return error_mark_node
;
7247 /* Perform the cast. */
7248 expr
= build_c_cast (input_location
, type
, expr
);
7252 cp_parser_abort_tentative_parse (parser
);
7255 /* If we get here, then it's not a cast, so it must be a
7256 unary-expression. */
7257 return cp_parser_unary_expression (parser
, address_p
, cast_p
, pidk
);
7260 /* Parse a binary expression of the general form:
7264 pm-expression .* cast-expression
7265 pm-expression ->* cast-expression
7267 multiplicative-expression:
7269 multiplicative-expression * pm-expression
7270 multiplicative-expression / pm-expression
7271 multiplicative-expression % pm-expression
7273 additive-expression:
7274 multiplicative-expression
7275 additive-expression + multiplicative-expression
7276 additive-expression - multiplicative-expression
7280 shift-expression << additive-expression
7281 shift-expression >> additive-expression
7283 relational-expression:
7285 relational-expression < shift-expression
7286 relational-expression > shift-expression
7287 relational-expression <= shift-expression
7288 relational-expression >= shift-expression
7292 relational-expression:
7293 relational-expression <? shift-expression
7294 relational-expression >? shift-expression
7296 equality-expression:
7297 relational-expression
7298 equality-expression == relational-expression
7299 equality-expression != relational-expression
7303 and-expression & equality-expression
7305 exclusive-or-expression:
7307 exclusive-or-expression ^ and-expression
7309 inclusive-or-expression:
7310 exclusive-or-expression
7311 inclusive-or-expression | exclusive-or-expression
7313 logical-and-expression:
7314 inclusive-or-expression
7315 logical-and-expression && inclusive-or-expression
7317 logical-or-expression:
7318 logical-and-expression
7319 logical-or-expression || logical-and-expression
7321 All these are implemented with a single function like:
7324 simple-cast-expression
7325 binary-expression <token> binary-expression
7327 CAST_P is true if this expression is the target of a cast.
7329 The binops_by_token map is used to get the tree codes for each <token> type.
7330 binary-expressions are associated according to a precedence table. */
7332 #define TOKEN_PRECEDENCE(token) \
7333 (((token->type == CPP_GREATER \
7334 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7335 && !parser->greater_than_is_operator_p) \
7336 ? PREC_NOT_OPERATOR \
7337 : binops_by_token[token->type].prec)
7340 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7341 bool no_toplevel_fold_p
,
7342 enum cp_parser_prec prec
,
7345 cp_parser_expression_stack stack
;
7346 cp_parser_expression_stack_entry
*sp
= &stack
[0];
7347 cp_parser_expression_stack_entry current
;
7350 enum tree_code rhs_type
;
7351 enum cp_parser_prec new_prec
, lookahead_prec
;
7354 /* Parse the first expression. */
7355 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
7357 current
.lhs_type
= ERROR_MARK
;
7358 current
.prec
= prec
;
7360 if (cp_parser_error_occurred (parser
))
7361 return error_mark_node
;
7365 /* Get an operator token. */
7366 token
= cp_lexer_peek_token (parser
->lexer
);
7368 if (warn_cxx0x_compat
7369 && token
->type
== CPP_RSHIFT
7370 && !parser
->greater_than_is_operator_p
)
7372 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
7373 "%<>>%> operator is treated"
7374 " as two right angle brackets in C++11"))
7375 inform (token
->location
,
7376 "suggest parentheses around %<>>%> expression");
7379 new_prec
= TOKEN_PRECEDENCE (token
);
7381 /* Popping an entry off the stack means we completed a subexpression:
7382 - either we found a token which is not an operator (`>' where it is not
7383 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7384 will happen repeatedly;
7385 - or, we found an operator which has lower priority. This is the case
7386 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7388 if (new_prec
<= current
.prec
)
7397 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
7398 current
.loc
= token
->location
;
7400 /* We used the operator token. */
7401 cp_lexer_consume_token (parser
->lexer
);
7403 /* For "false && x" or "true || x", x will never be executed;
7404 disable warnings while evaluating it. */
7405 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7406 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
7407 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7408 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
7410 /* Extract another operand. It may be the RHS of this expression
7411 or the LHS of a new, higher priority expression. */
7412 rhs
= cp_parser_simple_cast_expression (parser
);
7413 rhs_type
= ERROR_MARK
;
7415 /* Get another operator token. Look up its precedence to avoid
7416 building a useless (immediately popped) stack entry for common
7417 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7418 token
= cp_lexer_peek_token (parser
->lexer
);
7419 lookahead_prec
= TOKEN_PRECEDENCE (token
);
7420 if (lookahead_prec
> new_prec
)
7422 /* ... and prepare to parse the RHS of the new, higher priority
7423 expression. Since precedence levels on the stack are
7424 monotonically increasing, we do not have to care about
7429 current
.lhs_type
= rhs_type
;
7430 current
.prec
= new_prec
;
7431 new_prec
= lookahead_prec
;
7435 lookahead_prec
= new_prec
;
7436 /* If the stack is not empty, we have parsed into LHS the right side
7437 (`4' in the example above) of an expression we had suspended.
7438 We can use the information on the stack to recover the LHS (`3')
7439 from the stack together with the tree code (`MULT_EXPR'), and
7440 the precedence of the higher level subexpression
7441 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7442 which will be used to actually build the additive expression. */
7444 rhs_type
= current
.lhs_type
;
7449 /* Undo the disabling of warnings done above. */
7450 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7451 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
7452 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7453 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
7456 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7457 ERROR_MARK for everything that is not a binary expression.
7458 This makes warn_about_parentheses miss some warnings that
7459 involve unary operators. For unary expressions we should
7460 pass the correct tree_code unless the unary expression was
7461 surrounded by parentheses.
7463 if (no_toplevel_fold_p
7464 && lookahead_prec
<= current
.prec
7466 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
)
7467 current
.lhs
= build2 (current
.tree_type
, boolean_type_node
,
7470 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
7471 current
.lhs
, current
.lhs_type
,
7472 rhs
, rhs_type
, &overload
,
7473 tf_warning_or_error
);
7474 current
.lhs_type
= current
.tree_type
;
7475 if (EXPR_P (current
.lhs
))
7476 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
7478 /* If the binary operator required the use of an overloaded operator,
7479 then this expression cannot be an integral constant-expression.
7480 An overloaded operator can be used even if both operands are
7481 otherwise permissible in an integral constant-expression if at
7482 least one of the operands is of enumeration type. */
7485 && cp_parser_non_integral_constant_expression (parser
,
7487 return error_mark_node
;
7494 /* Parse the `? expression : assignment-expression' part of a
7495 conditional-expression. The LOGICAL_OR_EXPR is the
7496 logical-or-expression that started the conditional-expression.
7497 Returns a representation of the entire conditional-expression.
7499 This routine is used by cp_parser_assignment_expression.
7501 ? expression : assignment-expression
7505 ? : assignment-expression */
7508 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
7511 tree assignment_expr
;
7512 struct cp_token
*token
;
7513 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7515 /* Consume the `?' token. */
7516 cp_lexer_consume_token (parser
->lexer
);
7517 token
= cp_lexer_peek_token (parser
->lexer
);
7518 if (cp_parser_allow_gnu_extensions_p (parser
)
7519 && token
->type
== CPP_COLON
)
7521 pedwarn (token
->location
, OPT_Wpedantic
,
7522 "ISO C++ does not allow ?: with omitted middle operand");
7523 /* Implicit true clause. */
7525 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
7526 warn_for_omitted_condop (token
->location
, logical_or_expr
);
7530 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
7531 parser
->colon_corrects_to_scope_p
= false;
7532 /* Parse the expression. */
7533 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
7534 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7535 c_inhibit_evaluation_warnings
+=
7536 ((logical_or_expr
== truthvalue_true_node
)
7537 - (logical_or_expr
== truthvalue_false_node
));
7538 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
7541 /* The next token should be a `:'. */
7542 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
7543 /* Parse the assignment-expression. */
7544 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7545 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
7547 /* Build the conditional-expression. */
7548 return build_x_conditional_expr (loc
, logical_or_expr
,
7551 tf_warning_or_error
);
7554 /* Parse an assignment-expression.
7556 assignment-expression:
7557 conditional-expression
7558 logical-or-expression assignment-operator assignment_expression
7561 CAST_P is true if this expression is the target of a cast.
7563 Returns a representation for the expression. */
7566 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
7571 /* If the next token is the `throw' keyword, then we're looking at
7572 a throw-expression. */
7573 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
7574 expr
= cp_parser_throw_expression (parser
);
7575 /* Otherwise, it must be that we are looking at a
7576 logical-or-expression. */
7579 /* Parse the binary expressions (logical-or-expression). */
7580 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
7581 PREC_NOT_OPERATOR
, pidk
);
7582 /* If the next token is a `?' then we're actually looking at a
7583 conditional-expression. */
7584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
7585 return cp_parser_question_colon_clause (parser
, expr
);
7588 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7590 /* If it's an assignment-operator, we're using the second
7592 enum tree_code assignment_operator
7593 = cp_parser_assignment_operator_opt (parser
);
7594 if (assignment_operator
!= ERROR_MARK
)
7596 bool non_constant_p
;
7597 location_t saved_input_location
;
7599 /* Parse the right-hand side of the assignment. */
7600 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
7602 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7603 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7605 /* An assignment may not appear in a
7606 constant-expression. */
7607 if (cp_parser_non_integral_constant_expression (parser
,
7609 return error_mark_node
;
7610 /* Build the assignment expression. Its default
7611 location is the location of the '=' token. */
7612 saved_input_location
= input_location
;
7613 input_location
= loc
;
7614 expr
= build_x_modify_expr (loc
, expr
,
7615 assignment_operator
,
7617 tf_warning_or_error
);
7618 input_location
= saved_input_location
;
7626 /* Parse an (optional) assignment-operator.
7628 assignment-operator: one of
7629 = *= /= %= += -= >>= <<= &= ^= |=
7633 assignment-operator: one of
7636 If the next token is an assignment operator, the corresponding tree
7637 code is returned, and the token is consumed. For example, for
7638 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7639 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7640 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7641 operator, ERROR_MARK is returned. */
7643 static enum tree_code
7644 cp_parser_assignment_operator_opt (cp_parser
* parser
)
7649 /* Peek at the next token. */
7650 token
= cp_lexer_peek_token (parser
->lexer
);
7652 switch (token
->type
)
7663 op
= TRUNC_DIV_EXPR
;
7667 op
= TRUNC_MOD_EXPR
;
7699 /* Nothing else is an assignment operator. */
7703 /* If it was an assignment operator, consume it. */
7704 if (op
!= ERROR_MARK
)
7705 cp_lexer_consume_token (parser
->lexer
);
7710 /* Parse an expression.
7713 assignment-expression
7714 expression , assignment-expression
7716 CAST_P is true if this expression is the target of a cast.
7718 Returns a representation of the expression. */
7721 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
7723 tree expression
= NULL_TREE
;
7724 location_t loc
= UNKNOWN_LOCATION
;
7728 tree assignment_expression
;
7730 /* Parse the next assignment-expression. */
7731 assignment_expression
7732 = cp_parser_assignment_expression (parser
, cast_p
, pidk
);
7733 /* If this is the first assignment-expression, we can just
7736 expression
= assignment_expression
;
7738 expression
= build_x_compound_expr (loc
, expression
,
7739 assignment_expression
,
7740 tf_warning_or_error
);
7741 /* If the next token is not a comma, then we are done with the
7743 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7745 /* Consume the `,'. */
7746 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7747 cp_lexer_consume_token (parser
->lexer
);
7748 /* A comma operator cannot appear in a constant-expression. */
7749 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
7750 expression
= error_mark_node
;
7756 /* Parse a constant-expression.
7758 constant-expression:
7759 conditional-expression
7761 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7762 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7763 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7764 is false, NON_CONSTANT_P should be NULL. */
7767 cp_parser_constant_expression (cp_parser
* parser
,
7768 bool allow_non_constant_p
,
7769 bool *non_constant_p
)
7771 bool saved_integral_constant_expression_p
;
7772 bool saved_allow_non_integral_constant_expression_p
;
7773 bool saved_non_integral_constant_expression_p
;
7776 /* It might seem that we could simply parse the
7777 conditional-expression, and then check to see if it were
7778 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7779 one that the compiler can figure out is constant, possibly after
7780 doing some simplifications or optimizations. The standard has a
7781 precise definition of constant-expression, and we must honor
7782 that, even though it is somewhat more restrictive.
7788 is not a legal declaration, because `(2, 3)' is not a
7789 constant-expression. The `,' operator is forbidden in a
7790 constant-expression. However, GCC's constant-folding machinery
7791 will fold this operation to an INTEGER_CST for `3'. */
7793 /* Save the old settings. */
7794 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
7795 saved_allow_non_integral_constant_expression_p
7796 = parser
->allow_non_integral_constant_expression_p
;
7797 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
7798 /* We are now parsing a constant-expression. */
7799 parser
->integral_constant_expression_p
= true;
7800 parser
->allow_non_integral_constant_expression_p
7801 = (allow_non_constant_p
|| cxx_dialect
>= cxx0x
);
7802 parser
->non_integral_constant_expression_p
= false;
7803 /* Although the grammar says "conditional-expression", we parse an
7804 "assignment-expression", which also permits "throw-expression"
7805 and the use of assignment operators. In the case that
7806 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7807 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7808 actually essential that we look for an assignment-expression.
7809 For example, cp_parser_initializer_clauses uses this function to
7810 determine whether a particular assignment-expression is in fact
7812 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7813 /* Restore the old settings. */
7814 parser
->integral_constant_expression_p
7815 = saved_integral_constant_expression_p
;
7816 parser
->allow_non_integral_constant_expression_p
7817 = saved_allow_non_integral_constant_expression_p
;
7818 if (cxx_dialect
>= cxx0x
)
7820 /* Require an rvalue constant expression here; that's what our
7821 callers expect. Reference constant expressions are handled
7822 separately in e.g. cp_parser_template_argument. */
7823 bool is_const
= potential_rvalue_constant_expression (expression
);
7824 parser
->non_integral_constant_expression_p
= !is_const
;
7825 if (!is_const
&& !allow_non_constant_p
)
7826 require_potential_rvalue_constant_expression (expression
);
7828 if (allow_non_constant_p
)
7829 *non_constant_p
= parser
->non_integral_constant_expression_p
;
7830 parser
->non_integral_constant_expression_p
7831 = saved_non_integral_constant_expression_p
;
7836 /* Parse __builtin_offsetof.
7838 offsetof-expression:
7839 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7841 offsetof-member-designator:
7843 | offsetof-member-designator "." id-expression
7844 | offsetof-member-designator "[" expression "]"
7845 | offsetof-member-designator "->" id-expression */
7848 cp_parser_builtin_offsetof (cp_parser
*parser
)
7850 int save_ice_p
, save_non_ice_p
;
7855 /* We're about to accept non-integral-constant things, but will
7856 definitely yield an integral constant expression. Save and
7857 restore these values around our local parsing. */
7858 save_ice_p
= parser
->integral_constant_expression_p
;
7859 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
7861 /* Consume the "__builtin_offsetof" token. */
7862 cp_lexer_consume_token (parser
->lexer
);
7863 /* Consume the opening `('. */
7864 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7865 /* Parse the type-id. */
7866 type
= cp_parser_type_id (parser
);
7867 /* Look for the `,'. */
7868 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7869 token
= cp_lexer_peek_token (parser
->lexer
);
7871 /* Build the (type *)null that begins the traditional offsetof macro. */
7872 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
7873 tf_warning_or_error
);
7875 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7876 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
7877 true, &dummy
, token
->location
);
7880 token
= cp_lexer_peek_token (parser
->lexer
);
7881 switch (token
->type
)
7883 case CPP_OPEN_SQUARE
:
7884 /* offsetof-member-designator "[" expression "]" */
7885 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
7889 /* offsetof-member-designator "->" identifier */
7890 expr
= grok_array_decl (token
->location
, expr
, integer_zero_node
);
7894 /* offsetof-member-designator "." identifier */
7895 cp_lexer_consume_token (parser
->lexer
);
7896 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
7901 case CPP_CLOSE_PAREN
:
7902 /* Consume the ")" token. */
7903 cp_lexer_consume_token (parser
->lexer
);
7907 /* Error. We know the following require will fail, but
7908 that gives the proper error message. */
7909 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7910 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
7911 expr
= error_mark_node
;
7917 /* If we're processing a template, we can't finish the semantics yet.
7918 Otherwise we can fold the entire expression now. */
7919 if (processing_template_decl
)
7920 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
7922 expr
= finish_offsetof (expr
);
7925 parser
->integral_constant_expression_p
= save_ice_p
;
7926 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
7931 /* Parse a trait expression.
7933 Returns a representation of the expression, the underlying type
7934 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7937 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
7940 tree type1
, type2
= NULL_TREE
;
7941 bool binary
= false;
7942 cp_decl_specifier_seq decl_specs
;
7946 case RID_HAS_NOTHROW_ASSIGN
:
7947 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
7949 case RID_HAS_NOTHROW_CONSTRUCTOR
:
7950 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
7952 case RID_HAS_NOTHROW_COPY
:
7953 kind
= CPTK_HAS_NOTHROW_COPY
;
7955 case RID_HAS_TRIVIAL_ASSIGN
:
7956 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
7958 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
7959 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
7961 case RID_HAS_TRIVIAL_COPY
:
7962 kind
= CPTK_HAS_TRIVIAL_COPY
;
7964 case RID_HAS_TRIVIAL_DESTRUCTOR
:
7965 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
7967 case RID_HAS_VIRTUAL_DESTRUCTOR
:
7968 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
7970 case RID_IS_ABSTRACT
:
7971 kind
= CPTK_IS_ABSTRACT
;
7973 case RID_IS_BASE_OF
:
7974 kind
= CPTK_IS_BASE_OF
;
7978 kind
= CPTK_IS_CLASS
;
7980 case RID_IS_CONVERTIBLE_TO
:
7981 kind
= CPTK_IS_CONVERTIBLE_TO
;
7985 kind
= CPTK_IS_EMPTY
;
7988 kind
= CPTK_IS_ENUM
;
7991 kind
= CPTK_IS_FINAL
;
7993 case RID_IS_LITERAL_TYPE
:
7994 kind
= CPTK_IS_LITERAL_TYPE
;
7999 case RID_IS_POLYMORPHIC
:
8000 kind
= CPTK_IS_POLYMORPHIC
;
8002 case RID_IS_STD_LAYOUT
:
8003 kind
= CPTK_IS_STD_LAYOUT
;
8005 case RID_IS_TRIVIAL
:
8006 kind
= CPTK_IS_TRIVIAL
;
8009 kind
= CPTK_IS_UNION
;
8011 case RID_UNDERLYING_TYPE
:
8012 kind
= CPTK_UNDERLYING_TYPE
;
8017 case RID_DIRECT_BASES
:
8018 kind
= CPTK_DIRECT_BASES
;
8024 /* Consume the token. */
8025 cp_lexer_consume_token (parser
->lexer
);
8027 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8029 type1
= cp_parser_type_id (parser
);
8031 if (type1
== error_mark_node
)
8032 return error_mark_node
;
8034 /* Build a trivial decl-specifier-seq. */
8035 clear_decl_specs (&decl_specs
);
8036 decl_specs
.type
= type1
;
8038 /* Call grokdeclarator to figure out what type this is. */
8039 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8040 /*initialized=*/0, /*attrlist=*/NULL
);
8044 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8046 type2
= cp_parser_type_id (parser
);
8048 if (type2
== error_mark_node
)
8049 return error_mark_node
;
8051 /* Build a trivial decl-specifier-seq. */
8052 clear_decl_specs (&decl_specs
);
8053 decl_specs
.type
= type2
;
8055 /* Call grokdeclarator to figure out what type this is. */
8056 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
8057 /*initialized=*/0, /*attrlist=*/NULL
);
8060 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8062 /* Complete the trait expression, which may mean either processing
8063 the trait expr now or saving it for template instantiation. */
8066 case CPTK_UNDERLYING_TYPE
:
8067 return finish_underlying_type (type1
);
8069 return finish_bases (type1
, false);
8070 case CPTK_DIRECT_BASES
:
8071 return finish_bases (type1
, true);
8073 return finish_trait_expr (kind
, type1
, type2
);
8077 /* Lambdas that appear in variable initializer or default argument scope
8078 get that in their mangling, so we need to record it. We might as well
8079 use the count for function and namespace scopes as well. */
8080 static GTY(()) tree lambda_scope
;
8081 static GTY(()) int lambda_count
;
8082 typedef struct GTY(()) tree_int
8087 DEF_VEC_O(tree_int
);
8088 DEF_VEC_ALLOC_O(tree_int
,gc
);
8089 static GTY(()) VEC(tree_int
,gc
) *lambda_scope_stack
;
8092 start_lambda_scope (tree decl
)
8096 /* Once we're inside a function, we ignore other scopes and just push
8097 the function again so that popping works properly. */
8098 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8099 decl
= current_function_decl
;
8100 ti
.t
= lambda_scope
;
8101 ti
.i
= lambda_count
;
8102 VEC_safe_push (tree_int
, gc
, lambda_scope_stack
, ti
);
8103 if (lambda_scope
!= decl
)
8105 /* Don't reset the count if we're still in the same function. */
8106 lambda_scope
= decl
;
8112 record_lambda_scope (tree lambda
)
8114 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8115 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8119 finish_lambda_scope (void)
8121 tree_int
*p
= &VEC_last (tree_int
, lambda_scope_stack
);
8122 if (lambda_scope
!= p
->t
)
8124 lambda_scope
= p
->t
;
8125 lambda_count
= p
->i
;
8127 VEC_pop (tree_int
, lambda_scope_stack
);
8130 /* Parse a lambda expression.
8133 lambda-introducer lambda-declarator [opt] compound-statement
8135 Returns a representation of the expression. */
8138 cp_parser_lambda_expression (cp_parser
* parser
)
8140 tree lambda_expr
= build_lambda_expr ();
8144 LAMBDA_EXPR_LOCATION (lambda_expr
)
8145 = cp_lexer_peek_token (parser
->lexer
)->location
;
8147 if (cp_unevaluated_operand
)
8148 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8149 "lambda-expression in unevaluated context");
8151 /* We may be in the middle of deferred access check. Disable
8153 push_deferring_access_checks (dk_no_deferred
);
8155 cp_parser_lambda_introducer (parser
, lambda_expr
);
8157 type
= begin_lambda_type (lambda_expr
);
8158 if (type
== error_mark_node
)
8159 return error_mark_node
;
8161 record_lambda_scope (lambda_expr
);
8163 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8164 determine_visibility (TYPE_NAME (type
));
8166 /* Now that we've started the type, add the capture fields for any
8167 explicit captures. */
8168 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8171 /* Inside the class, surrounding template-parameter-lists do not apply. */
8172 unsigned int saved_num_template_parameter_lists
8173 = parser
->num_template_parameter_lists
;
8174 unsigned char in_statement
= parser
->in_statement
;
8175 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
8177 parser
->num_template_parameter_lists
= 0;
8178 parser
->in_statement
= 0;
8179 parser
->in_switch_statement_p
= false;
8181 /* By virtue of defining a local class, a lambda expression has access to
8182 the private variables of enclosing classes. */
8184 ok
= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
8187 cp_parser_lambda_body (parser
, lambda_expr
);
8188 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8189 cp_parser_skip_to_end_of_block_or_statement (parser
);
8191 /* The capture list was built up in reverse order; fix that now. */
8193 tree newlist
= NULL_TREE
;
8196 for (elt
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
8199 next
= TREE_CHAIN (elt
);
8200 TREE_CHAIN (elt
) = newlist
;
8203 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
) = newlist
;
8207 maybe_add_lambda_conv_op (type
);
8209 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
8211 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
8212 parser
->in_statement
= in_statement
;
8213 parser
->in_switch_statement_p
= in_switch_statement_p
;
8216 pop_deferring_access_checks ();
8218 /* This field is only used during parsing of the lambda. */
8219 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
8221 /* This lambda shouldn't have any proxies left at this point. */
8222 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
8223 /* And now that we're done, push proxies for an enclosing lambda. */
8224 insert_pending_capture_proxies ();
8227 return build_lambda_object (lambda_expr
);
8229 return error_mark_node
;
8232 /* Parse the beginning of a lambda expression.
8235 [ lambda-capture [opt] ]
8237 LAMBDA_EXPR is the current representation of the lambda expression. */
8240 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
8242 /* Need commas after the first capture. */
8245 /* Eat the leading `['. */
8246 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8248 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
8250 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
8251 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
8252 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
8255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
8257 cp_lexer_consume_token (parser
->lexer
);
8261 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
8263 cp_token
* capture_token
;
8265 tree capture_init_expr
;
8266 cp_id_kind idk
= CP_ID_KIND_NONE
;
8267 bool explicit_init_p
= false;
8269 enum capture_kind_type
8274 enum capture_kind_type capture_kind
= BY_COPY
;
8276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
8278 error ("expected end of capture-list");
8285 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8287 /* Possibly capture `this'. */
8288 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
8290 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8291 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
8292 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
8293 "with by-copy capture default");
8294 cp_lexer_consume_token (parser
->lexer
);
8295 add_capture (lambda_expr
,
8296 /*id=*/this_identifier
,
8297 /*initializer=*/finish_this_expr(),
8298 /*by_reference_p=*/false,
8303 /* Remember whether we want to capture as a reference or not. */
8304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
8306 capture_kind
= BY_REFERENCE
;
8307 cp_lexer_consume_token (parser
->lexer
);
8310 /* Get the identifier. */
8311 capture_token
= cp_lexer_peek_token (parser
->lexer
);
8312 capture_id
= cp_parser_identifier (parser
);
8314 if (capture_id
== error_mark_node
)
8315 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8316 delimiters, but I modified this to stop on unnested ']' as well. It
8317 was already changed to stop on unnested '}', so the
8318 "closing_parenthesis" name is no more misleading with my change. */
8320 cp_parser_skip_to_closing_parenthesis (parser
,
8321 /*recovering=*/true,
8323 /*consume_paren=*/true);
8327 /* Find the initializer for this capture. */
8328 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8330 /* An explicit expression exists. */
8331 cp_lexer_consume_token (parser
->lexer
);
8332 pedwarn (input_location
, OPT_Wpedantic
,
8333 "ISO C++ does not allow initializers "
8334 "in lambda expression capture lists");
8335 capture_init_expr
= cp_parser_assignment_expression (parser
,
8338 explicit_init_p
= true;
8342 const char* error_msg
;
8344 /* Turn the identifier into an id-expression. */
8346 = cp_parser_lookup_name
8350 /*is_template=*/false,
8351 /*is_namespace=*/false,
8352 /*check_dependency=*/true,
8353 /*ambiguous_decls=*/NULL
,
8354 capture_token
->location
);
8356 if (capture_init_expr
== error_mark_node
)
8358 unqualified_name_lookup_error (capture_id
);
8361 else if (DECL_P (capture_init_expr
)
8362 && (TREE_CODE (capture_init_expr
) != VAR_DECL
8363 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
8365 error_at (capture_token
->location
,
8366 "capture of non-variable %qD ",
8368 inform (0, "%q+#D declared here", capture_init_expr
);
8371 if (TREE_CODE (capture_init_expr
) == VAR_DECL
8372 && decl_storage_duration (capture_init_expr
) != dk_auto
)
8374 pedwarn (capture_token
->location
, 0, "capture of variable "
8375 "%qD with non-automatic storage duration",
8377 inform (0, "%q+#D declared here", capture_init_expr
);
8382 = finish_id_expression
8387 /*integral_constant_expression_p=*/false,
8388 /*allow_non_integral_constant_expression_p=*/false,
8389 /*non_integral_constant_expression_p=*/NULL
,
8390 /*template_p=*/false,
8392 /*address_p=*/false,
8393 /*template_arg_p=*/false,
8395 capture_token
->location
);
8398 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
8399 && !explicit_init_p
)
8401 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
8402 && capture_kind
== BY_COPY
)
8403 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
8404 "of %qD redundant with by-copy capture default",
8406 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
8407 && capture_kind
== BY_REFERENCE
)
8408 pedwarn (capture_token
->location
, 0, "explicit by-reference "
8409 "capture of %qD redundant with by-reference capture "
8410 "default", capture_id
);
8413 add_capture (lambda_expr
,
8416 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
8420 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8423 /* Parse the (optional) middle of a lambda expression.
8426 ( parameter-declaration-clause [opt] )
8427 attribute-specifier [opt]
8429 exception-specification [opt]
8430 lambda-return-type-clause [opt]
8432 LAMBDA_EXPR is the current representation of the lambda expression. */
8435 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
8437 /* 5.1.1.4 of the standard says:
8438 If a lambda-expression does not include a lambda-declarator, it is as if
8439 the lambda-declarator were ().
8440 This means an empty parameter list, no attributes, and no exception
8442 tree param_list
= void_list_node
;
8443 tree attributes
= NULL_TREE
;
8444 tree exception_spec
= NULL_TREE
;
8447 /* The lambda-declarator is optional, but must begin with an opening
8448 parenthesis if present. */
8449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8451 cp_lexer_consume_token (parser
->lexer
);
8453 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
8455 /* Parse parameters. */
8456 param_list
= cp_parser_parameter_declaration_clause (parser
);
8458 /* Default arguments shall not be specified in the
8459 parameter-declaration-clause of a lambda-declarator. */
8460 for (t
= param_list
; t
; t
= TREE_CHAIN (t
))
8461 if (TREE_PURPOSE (t
))
8462 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
8463 "default argument specified for lambda parameter");
8465 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8467 attributes
= cp_parser_attributes_opt (parser
);
8469 /* Parse optional `mutable' keyword. */
8470 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
8472 cp_lexer_consume_token (parser
->lexer
);
8473 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
8476 /* Parse optional exception specification. */
8477 exception_spec
= cp_parser_exception_specification_opt (parser
);
8479 /* Parse optional trailing return type. */
8480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
8482 cp_lexer_consume_token (parser
->lexer
);
8483 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
) = cp_parser_type_id (parser
);
8486 /* The function parameters must be in scope all the way until after the
8487 trailing-return-type in case of decltype. */
8488 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
8489 pop_binding (DECL_NAME (t
), t
);
8494 /* Create the function call operator.
8496 Messing with declarators like this is no uglier than building up the
8497 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8500 cp_decl_specifier_seq return_type_specs
;
8501 cp_declarator
* declarator
;
8506 clear_decl_specs (&return_type_specs
);
8507 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
8508 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
8510 /* Maybe we will deduce the return type later. */
8511 return_type_specs
.type
= make_auto ();
8513 p
= obstack_alloc (&declarator_obstack
, 0);
8515 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
8518 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
8519 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
8520 declarator
= make_call_declarator (declarator
, param_list
, quals
,
8521 VIRT_SPEC_UNSPECIFIED
,
8523 /*late_return_type=*/NULL_TREE
);
8524 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
8526 fco
= grokmethod (&return_type_specs
,
8529 if (fco
!= error_mark_node
)
8531 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
8532 DECL_ARTIFICIAL (fco
) = 1;
8533 /* Give the object parameter a different name. */
8534 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
8537 finish_member_declaration (fco
);
8539 obstack_free (&declarator_obstack
, p
);
8541 return (fco
!= error_mark_node
);
8545 /* Parse the body of a lambda expression, which is simply
8549 but which requires special handling.
8550 LAMBDA_EXPR is the current representation of the lambda expression. */
8553 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
8555 bool nested
= (current_function_decl
!= NULL_TREE
);
8556 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
8558 push_function_context ();
8560 /* Still increment function_depth so that we don't GC in the
8561 middle of an expression. */
8563 /* Clear this in case we're in the middle of a default argument. */
8564 parser
->local_variables_forbidden_p
= false;
8566 /* Finish the function call operator
8568 + late_parsing_for_member
8569 + function_definition_after_declarator
8570 + ctor_initializer_opt_and_function_body */
8572 tree fco
= lambda_function (lambda_expr
);
8578 /* Let the front end know that we are going to be defining this
8580 start_preparsed_function (fco
,
8582 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
8584 start_lambda_scope (fco
);
8585 body
= begin_function_body ();
8587 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8590 /* Push the proxies for any explicit captures. */
8591 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
8592 cap
= TREE_CHAIN (cap
))
8593 build_capture_proxy (TREE_PURPOSE (cap
));
8595 compound_stmt
= begin_compound_stmt (0);
8597 /* 5.1.1.4 of the standard says:
8598 If a lambda-expression does not include a trailing-return-type, it
8599 is as if the trailing-return-type denotes the following type:
8600 * if the compound-statement is of the form
8601 { return attribute-specifier [opt] expression ; }
8602 the type of the returned expression after lvalue-to-rvalue
8603 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8604 (_conv.array_ 4.2), and function-to-pointer conversion
8606 * otherwise, void. */
8608 /* In a lambda that has neither a lambda-return-type-clause
8609 nor a deducible form, errors should be reported for return statements
8610 in the body. Since we used void as the placeholder return type, parsing
8611 the body as usual will give such desired behavior. */
8612 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
8613 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
8614 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
8616 tree expr
= NULL_TREE
;
8617 cp_id_kind idk
= CP_ID_KIND_NONE
;
8619 /* Parse tentatively in case there's more after the initial return
8621 cp_parser_parse_tentatively (parser
);
8623 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
8625 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
8627 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8628 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8630 if (cp_parser_parse_definitely (parser
))
8632 if (!processing_template_decl
)
8633 apply_deduced_return_type (fco
, lambda_return_type (expr
));
8635 /* Will get error here if type not deduced yet. */
8636 finish_return_stmt (expr
);
8644 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8645 cp_parser_label_declaration (parser
);
8646 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
8647 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8650 finish_compound_stmt (compound_stmt
);
8653 finish_function_body (body
);
8654 finish_lambda_scope ();
8656 /* Finish the function and generate code for it if necessary. */
8657 expand_or_defer_fn (finish_function (/*inline*/2));
8660 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
8662 pop_function_context();
8667 /* Statements [gram.stmt.stmt] */
8669 /* Parse a statement.
8673 expression-statement
8678 declaration-statement
8685 attribute-specifier-seq (opt) expression-statement
8686 attribute-specifier-seq (opt) compound-statement
8687 attribute-specifier-seq (opt) selection-statement
8688 attribute-specifier-seq (opt) iteration-statement
8689 attribute-specifier-seq (opt) jump-statement
8690 declaration-statement
8691 attribute-specifier-seq (opt) try-block
8698 IN_COMPOUND is true when the statement is nested inside a
8699 cp_parser_compound_statement; this matters for certain pragmas.
8701 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8702 is a (possibly labeled) if statement which is not enclosed in braces
8703 and has an else clause. This is used to implement -Wparentheses. */
8706 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
8707 bool in_compound
, bool *if_p
)
8709 tree statement
, std_attrs
= NULL_TREE
;
8711 location_t statement_location
, attrs_location
;
8716 /* There is no statement yet. */
8717 statement
= NULL_TREE
;
8719 cp_lexer_save_tokens (parser
->lexer
);
8720 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
8721 if (c_dialect_objc ())
8722 /* In obj-c++, seing '[[' might be the either the beginning of
8723 c++11 attributes, or a nested objc-message-expression. So
8724 let's parse the c++11 attributes tentatively. */
8725 cp_parser_parse_tentatively (parser
);
8726 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
8727 if (c_dialect_objc ())
8729 if (!cp_parser_parse_definitely (parser
))
8730 std_attrs
= NULL_TREE
;
8733 /* Peek at the next token. */
8734 token
= cp_lexer_peek_token (parser
->lexer
);
8735 /* Remember the location of the first token in the statement. */
8736 statement_location
= token
->location
;
8737 /* If this is a keyword, then that will often determine what kind of
8738 statement we have. */
8739 if (token
->type
== CPP_KEYWORD
)
8741 enum rid keyword
= token
->keyword
;
8747 /* Looks like a labeled-statement with a case label.
8748 Parse the label, and then use tail recursion to parse
8750 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
8755 statement
= cp_parser_selection_statement (parser
, if_p
);
8761 statement
= cp_parser_iteration_statement (parser
);
8768 statement
= cp_parser_jump_statement (parser
);
8771 /* Objective-C++ exception-handling constructs. */
8774 case RID_AT_FINALLY
:
8775 case RID_AT_SYNCHRONIZED
:
8777 statement
= cp_parser_objc_statement (parser
);
8781 statement
= cp_parser_try_block (parser
);
8785 /* This must be a namespace alias definition. */
8786 cp_parser_declaration_statement (parser
);
8789 case RID_TRANSACTION_ATOMIC
:
8790 case RID_TRANSACTION_RELAXED
:
8791 statement
= cp_parser_transaction (parser
, keyword
);
8793 case RID_TRANSACTION_CANCEL
:
8794 statement
= cp_parser_transaction_cancel (parser
);
8798 /* It might be a keyword like `int' that can start a
8799 declaration-statement. */
8803 else if (token
->type
== CPP_NAME
)
8805 /* If the next token is a `:', then we are looking at a
8806 labeled-statement. */
8807 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8808 if (token
->type
== CPP_COLON
)
8810 /* Looks like a labeled-statement with an ordinary label.
8811 Parse the label, and then use tail recursion to parse
8814 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
8818 /* Anything that starts with a `{' must be a compound-statement. */
8819 else if (token
->type
== CPP_OPEN_BRACE
)
8820 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
8821 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8822 a statement all its own. */
8823 else if (token
->type
== CPP_PRAGMA
)
8825 /* Only certain OpenMP pragmas are attached to statements, and thus
8826 are considered statements themselves. All others are not. In
8827 the context of a compound, accept the pragma as a "statement" and
8828 return so that we can check for a close brace. Otherwise we
8829 require a real statement and must go back and read one. */
8831 cp_parser_pragma (parser
, pragma_compound
);
8832 else if (!cp_parser_pragma (parser
, pragma_stmt
))
8836 else if (token
->type
== CPP_EOF
)
8838 cp_parser_error (parser
, "expected statement");
8842 /* Everything else must be a declaration-statement or an
8843 expression-statement. Try for the declaration-statement
8844 first, unless we are looking at a `;', in which case we know that
8845 we have an expression-statement. */
8848 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8850 if (std_attrs
!= NULL_TREE
)
8852 /* Attributes should be parsed as part of the the
8853 declaration, so let's un-parse them. */
8854 cp_lexer_rollback_tokens (parser
->lexer
);
8855 std_attrs
= NULL_TREE
;
8858 cp_parser_parse_tentatively (parser
);
8859 /* Try to parse the declaration-statement. */
8860 cp_parser_declaration_statement (parser
);
8861 /* If that worked, we're done. */
8862 if (cp_parser_parse_definitely (parser
))
8865 /* Look for an expression-statement instead. */
8866 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
8869 /* Set the line number for the statement. */
8870 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
8871 SET_EXPR_LOCATION (statement
, statement_location
);
8873 /* Note that for now, we don't do anything with c++11 statements
8874 parsed at this level. */
8875 if (std_attrs
!= NULL_TREE
)
8876 warning_at (attrs_location
,
8878 "attributes at the beginning of statement are ignored");
8881 /* Parse the label for a labeled-statement, i.e.
8884 case constant-expression :
8888 case constant-expression ... constant-expression : statement
8890 When a label is parsed without errors, the label is added to the
8891 parse tree by the finish_* functions, so this function doesn't
8892 have to return the label. */
8895 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
8898 tree label
= NULL_TREE
;
8899 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8901 /* The next token should be an identifier. */
8902 token
= cp_lexer_peek_token (parser
->lexer
);
8903 if (token
->type
!= CPP_NAME
8904 && token
->type
!= CPP_KEYWORD
)
8906 cp_parser_error (parser
, "expected labeled-statement");
8910 parser
->colon_corrects_to_scope_p
= false;
8911 switch (token
->keyword
)
8918 /* Consume the `case' token. */
8919 cp_lexer_consume_token (parser
->lexer
);
8920 /* Parse the constant-expression. */
8921 expr
= cp_parser_constant_expression (parser
,
8922 /*allow_non_constant_p=*/false,
8925 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
8926 if (ellipsis
->type
== CPP_ELLIPSIS
)
8928 /* Consume the `...' token. */
8929 cp_lexer_consume_token (parser
->lexer
);
8931 cp_parser_constant_expression (parser
,
8932 /*allow_non_constant_p=*/false,
8934 /* We don't need to emit warnings here, as the common code
8935 will do this for us. */
8938 expr_hi
= NULL_TREE
;
8940 if (parser
->in_switch_statement_p
)
8941 finish_case_label (token
->location
, expr
, expr_hi
);
8943 error_at (token
->location
,
8944 "case label %qE not within a switch statement",
8950 /* Consume the `default' token. */
8951 cp_lexer_consume_token (parser
->lexer
);
8953 if (parser
->in_switch_statement_p
)
8954 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
8956 error_at (token
->location
, "case label not within a switch statement");
8960 /* Anything else must be an ordinary label. */
8961 label
= finish_label_stmt (cp_parser_identifier (parser
));
8965 /* Require the `:' token. */
8966 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8968 /* An ordinary label may optionally be followed by attributes.
8969 However, this is only permitted if the attributes are then
8970 followed by a semicolon. This is because, for backward
8971 compatibility, when parsing
8972 lab: __attribute__ ((unused)) int i;
8973 we want the attribute to attach to "i", not "lab". */
8974 if (label
!= NULL_TREE
8975 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
8978 cp_parser_parse_tentatively (parser
);
8979 attrs
= cp_parser_gnu_attributes_opt (parser
);
8980 if (attrs
== NULL_TREE
8981 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8982 cp_parser_abort_tentative_parse (parser
);
8983 else if (!cp_parser_parse_definitely (parser
))
8986 attributes
= chainon (attributes
, attrs
);
8989 if (attributes
!= NULL_TREE
)
8990 cplus_decl_attributes (&label
, attributes
, 0);
8992 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8995 /* Parse an expression-statement.
8997 expression-statement:
9000 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9001 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9002 indicates whether this expression-statement is part of an
9003 expression statement. */
9006 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9008 tree statement
= NULL_TREE
;
9009 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9011 /* If the next token is a ';', then there is no expression
9013 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9014 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9016 /* Give a helpful message for "A<T>::type t;" and the like. */
9017 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9018 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9020 if (TREE_CODE (statement
) == SCOPE_REF
)
9021 error_at (token
->location
, "need %<typename%> before %qE because "
9022 "%qT is a dependent scope",
9023 statement
, TREE_OPERAND (statement
, 0));
9024 else if (is_overloaded_fn (statement
)
9025 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9028 tree fn
= get_first_fn (statement
);
9029 error_at (token
->location
,
9030 "%<%T::%D%> names the constructor, not the type",
9031 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9035 /* Consume the final `;'. */
9036 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9038 if (in_statement_expr
9039 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9040 /* This is the final expression statement of a statement
9042 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9044 statement
= finish_expr_stmt (statement
);
9051 /* Parse a compound-statement.
9054 { statement-seq [opt] }
9059 { label-declaration-seq [opt] statement-seq [opt] }
9061 label-declaration-seq:
9063 label-declaration-seq label-declaration
9065 Returns a tree representing the statement. */
9068 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
9069 bool in_try
, bool function_body
)
9073 /* Consume the `{'. */
9074 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9075 return error_mark_node
;
9076 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
9078 pedwarn (input_location
, OPT_Wpedantic
,
9079 "compound-statement in constexpr function");
9080 /* Begin the compound-statement. */
9081 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
9082 /* If the next keyword is `__label__' we have a label declaration. */
9083 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9084 cp_parser_label_declaration (parser
);
9085 /* Parse an (optional) statement-seq. */
9086 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
9087 /* Finish the compound-statement. */
9088 finish_compound_stmt (compound_stmt
);
9089 /* Consume the `}'. */
9090 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9092 return compound_stmt
;
9095 /* Parse an (optional) statement-seq.
9099 statement-seq [opt] statement */
9102 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
9104 /* Scan statements until there aren't any more. */
9107 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9109 /* If we are looking at a `}', then we have run out of
9110 statements; the same is true if we have reached the end
9111 of file, or have stumbled upon a stray '@end'. */
9112 if (token
->type
== CPP_CLOSE_BRACE
9113 || token
->type
== CPP_EOF
9114 || token
->type
== CPP_PRAGMA_EOL
9115 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
9118 /* If we are in a compound statement and find 'else' then
9119 something went wrong. */
9120 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
9122 if (parser
->in_statement
& IN_IF_STMT
)
9126 token
= cp_lexer_consume_token (parser
->lexer
);
9127 error_at (token
->location
, "%<else%> without a previous %<if%>");
9131 /* Parse the statement. */
9132 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
9136 /* Parse a selection-statement.
9138 selection-statement:
9139 if ( condition ) statement
9140 if ( condition ) statement else statement
9141 switch ( condition ) statement
9143 Returns the new IF_STMT or SWITCH_STMT.
9145 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9146 is a (possibly labeled) if statement which is not enclosed in
9147 braces and has an else clause. This is used to implement
9151 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
9159 /* Peek at the next token. */
9160 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
9162 /* See what kind of keyword it is. */
9163 keyword
= token
->keyword
;
9172 /* Look for the `('. */
9173 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
9175 cp_parser_skip_to_end_of_statement (parser
);
9176 return error_mark_node
;
9179 /* Begin the selection-statement. */
9180 if (keyword
== RID_IF
)
9181 statement
= begin_if_stmt ();
9183 statement
= begin_switch_stmt ();
9185 /* Parse the condition. */
9186 condition
= cp_parser_condition (parser
);
9187 /* Look for the `)'. */
9188 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
9189 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9190 /*consume_paren=*/true);
9192 if (keyword
== RID_IF
)
9195 unsigned char in_statement
;
9197 /* Add the condition. */
9198 finish_if_stmt_cond (condition
, statement
);
9200 /* Parse the then-clause. */
9201 in_statement
= parser
->in_statement
;
9202 parser
->in_statement
|= IN_IF_STMT
;
9203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9205 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9206 add_stmt (build_empty_stmt (loc
));
9207 cp_lexer_consume_token (parser
->lexer
);
9208 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
9209 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
9210 "empty body in an %<if%> statement");
9214 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
9215 parser
->in_statement
= in_statement
;
9217 finish_then_clause (statement
);
9219 /* If the next token is `else', parse the else-clause. */
9220 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9223 /* Consume the `else' keyword. */
9224 cp_lexer_consume_token (parser
->lexer
);
9225 begin_else_clause (statement
);
9226 /* Parse the else-clause. */
9227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9230 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9232 OPT_Wempty_body
, "suggest braces around "
9233 "empty body in an %<else%> statement");
9234 add_stmt (build_empty_stmt (loc
));
9235 cp_lexer_consume_token (parser
->lexer
);
9238 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9240 finish_else_clause (statement
);
9242 /* If we are currently parsing a then-clause, then
9243 IF_P will not be NULL. We set it to true to
9244 indicate that this if statement has an else clause.
9245 This may trigger the Wparentheses warning below
9246 when we get back up to the parent if statement. */
9252 /* This if statement does not have an else clause. If
9253 NESTED_IF is true, then the then-clause is an if
9254 statement which does have an else clause. We warn
9255 about the potential ambiguity. */
9257 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
9258 "suggest explicit braces to avoid ambiguous"
9262 /* Now we're all done with the if-statement. */
9263 finish_if_stmt (statement
);
9267 bool in_switch_statement_p
;
9268 unsigned char in_statement
;
9270 /* Add the condition. */
9271 finish_switch_cond (condition
, statement
);
9273 /* Parse the body of the switch-statement. */
9274 in_switch_statement_p
= parser
->in_switch_statement_p
;
9275 in_statement
= parser
->in_statement
;
9276 parser
->in_switch_statement_p
= true;
9277 parser
->in_statement
|= IN_SWITCH_STMT
;
9278 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9279 parser
->in_switch_statement_p
= in_switch_statement_p
;
9280 parser
->in_statement
= in_statement
;
9282 /* Now we're all done with the switch-statement. */
9283 finish_switch_stmt (statement
);
9291 cp_parser_error (parser
, "expected selection-statement");
9292 return error_mark_node
;
9296 /* Parse a condition.
9300 type-specifier-seq declarator = initializer-clause
9301 type-specifier-seq declarator braced-init-list
9306 type-specifier-seq declarator asm-specification [opt]
9307 attributes [opt] = assignment-expression
9309 Returns the expression that should be tested. */
9312 cp_parser_condition (cp_parser
* parser
)
9314 cp_decl_specifier_seq type_specifiers
;
9315 const char *saved_message
;
9316 int declares_class_or_enum
;
9318 /* Try the declaration first. */
9319 cp_parser_parse_tentatively (parser
);
9320 /* New types are not allowed in the type-specifier-seq for a
9322 saved_message
= parser
->type_definition_forbidden_message
;
9323 parser
->type_definition_forbidden_message
9324 = G_("types may not be defined in conditions");
9325 /* Parse the type-specifier-seq. */
9326 cp_parser_decl_specifier_seq (parser
,
9327 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
9329 &declares_class_or_enum
);
9330 /* Restore the saved message. */
9331 parser
->type_definition_forbidden_message
= saved_message
;
9332 /* If all is well, we might be looking at a declaration. */
9333 if (!cp_parser_error_occurred (parser
))
9336 tree asm_specification
;
9338 cp_declarator
*declarator
;
9339 tree initializer
= NULL_TREE
;
9341 /* Parse the declarator. */
9342 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9343 /*ctor_dtor_or_conv_p=*/NULL
,
9344 /*parenthesized_p=*/NULL
,
9345 /*member_p=*/false);
9346 /* Parse the attributes. */
9347 attributes
= cp_parser_attributes_opt (parser
);
9348 /* Parse the asm-specification. */
9349 asm_specification
= cp_parser_asm_specification_opt (parser
);
9350 /* If the next token is not an `=' or '{', then we might still be
9351 looking at an expression. For example:
9355 looks like a decl-specifier-seq and a declarator -- but then
9356 there is no `=', so this is an expression. */
9357 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9358 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9359 cp_parser_simulate_error (parser
);
9361 /* If we did see an `=' or '{', then we are looking at a declaration
9363 if (cp_parser_parse_definitely (parser
))
9366 bool non_constant_p
;
9367 bool flags
= LOOKUP_ONLYCONVERTING
;
9369 /* Create the declaration. */
9370 decl
= start_decl (declarator
, &type_specifiers
,
9371 /*initialized_p=*/true,
9372 attributes
, /*prefix_attributes=*/NULL_TREE
,
9375 /* Parse the initializer. */
9376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9378 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
9379 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
9384 /* Consume the `='. */
9385 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
9386 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
9388 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
9389 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9391 /* Process the initializer. */
9392 cp_finish_decl (decl
,
9393 initializer
, !non_constant_p
,
9398 pop_scope (pushed_scope
);
9400 return convert_from_reference (decl
);
9403 /* If we didn't even get past the declarator successfully, we are
9404 definitely not looking at a declaration. */
9406 cp_parser_abort_tentative_parse (parser
);
9408 /* Otherwise, we are looking at an expression. */
9409 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9412 /* Parses a for-statement or range-for-statement until the closing ')',
9416 cp_parser_for (cp_parser
*parser
)
9418 tree init
, scope
, decl
;
9421 /* Begin the for-statement. */
9422 scope
= begin_for_scope (&init
);
9424 /* Parse the initialization. */
9425 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
9428 return cp_parser_range_for (parser
, scope
, init
, decl
);
9430 return cp_parser_c_for (parser
, scope
, init
);
9434 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
)
9436 /* Normal for loop */
9437 tree condition
= NULL_TREE
;
9438 tree expression
= NULL_TREE
;
9441 stmt
= begin_for_stmt (scope
, init
);
9442 /* The for-init-statement has already been parsed in
9443 cp_parser_for_init_statement, so no work is needed here. */
9444 finish_for_init_stmt (stmt
);
9446 /* If there's a condition, process it. */
9447 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9448 condition
= cp_parser_condition (parser
);
9449 finish_for_cond (condition
, stmt
);
9450 /* Look for the `;'. */
9451 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9453 /* If there's an expression, process it. */
9454 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
9455 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9456 finish_for_expr (expression
, stmt
);
9461 /* Tries to parse a range-based for-statement:
9464 decl-specifier-seq declarator : expression
9466 The decl-specifier-seq declarator and the `:' are already parsed by
9467 cp_parser_for_init_statement. If processing_template_decl it returns a
9468 newly created RANGE_FOR_STMT; if not, it is converted to a
9469 regular FOR_STMT. */
9472 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
)
9474 tree stmt
, range_expr
;
9476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9478 bool expr_non_constant_p
;
9479 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9482 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9484 /* If in template, STMT is converted to a normal for-statement
9485 at instantiation. If not, it is done just ahead. */
9486 if (processing_template_decl
)
9488 if (check_for_bare_parameter_packs (range_expr
))
9489 range_expr
= error_mark_node
;
9490 stmt
= begin_range_for_stmt (scope
, init
);
9491 finish_range_for_decl (stmt
, range_decl
, range_expr
);
9492 if (!type_dependent_expression_p (range_expr
)
9493 /* do_auto_deduction doesn't mess with template init-lists. */
9494 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
9495 do_range_for_auto_deduction (range_decl
, range_expr
);
9499 stmt
= begin_for_stmt (scope
, init
);
9500 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
);
9505 /* Subroutine of cp_convert_range_for: given the initializer expression,
9506 builds up the range temporary. */
9509 build_range_temp (tree range_expr
)
9511 tree range_type
, range_temp
;
9513 /* Find out the type deduced by the declaration
9514 `auto &&__range = range_expr'. */
9515 range_type
= cp_build_reference_type (make_auto (), true);
9516 range_type
= do_auto_deduction (range_type
, range_expr
,
9517 type_uses_auto (range_type
));
9519 /* Create the __range variable. */
9520 range_temp
= build_decl (input_location
, VAR_DECL
,
9521 get_identifier ("__for_range"), range_type
);
9522 TREE_USED (range_temp
) = 1;
9523 DECL_ARTIFICIAL (range_temp
) = 1;
9528 /* Used by cp_parser_range_for in template context: we aren't going to
9529 do a full conversion yet, but we still need to resolve auto in the
9530 type of the for-range-declaration if present. This is basically
9531 a shortcut version of cp_convert_range_for. */
9534 do_range_for_auto_deduction (tree decl
, tree range_expr
)
9536 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
9539 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
9540 range_temp
= convert_from_reference (build_range_temp (range_expr
));
9541 iter_type
= (cp_parser_perform_range_for_lookup
9542 (range_temp
, &begin_dummy
, &end_dummy
));
9543 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
9544 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
9545 tf_warning_or_error
);
9546 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
9547 iter_decl
, auto_node
);
9551 /* Converts a range-based for-statement into a normal
9552 for-statement, as per the definition.
9554 for (RANGE_DECL : RANGE_EXPR)
9557 should be equivalent to:
9560 auto &&__range = RANGE_EXPR;
9561 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9565 RANGE_DECL = *__begin;
9570 If RANGE_EXPR is an array:
9571 BEGIN_EXPR = __range
9572 END_EXPR = __range + ARRAY_SIZE(__range)
9573 Else if RANGE_EXPR has a member 'begin' or 'end':
9574 BEGIN_EXPR = __range.begin()
9575 END_EXPR = __range.end()
9577 BEGIN_EXPR = begin(__range)
9578 END_EXPR = end(__range);
9580 If __range has a member 'begin' but not 'end', or vice versa, we must
9581 still use the second alternative (it will surely fail, however).
9582 When calling begin()/end() in the third alternative we must use
9583 argument dependent lookup, but always considering 'std' as an associated
9587 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
)
9590 tree iter_type
, begin_expr
, end_expr
;
9591 tree condition
, expression
;
9593 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
9594 /* If an error happened previously do nothing or else a lot of
9595 unhelpful errors would be issued. */
9596 begin_expr
= end_expr
= iter_type
= error_mark_node
;
9599 tree range_temp
= build_range_temp (range_expr
);
9600 pushdecl (range_temp
);
9601 cp_finish_decl (range_temp
, range_expr
,
9602 /*is_constant_init*/false, NULL_TREE
,
9603 LOOKUP_ONLYCONVERTING
);
9605 range_temp
= convert_from_reference (range_temp
);
9606 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
9607 &begin_expr
, &end_expr
);
9610 /* The new for initialization statement. */
9611 begin
= build_decl (input_location
, VAR_DECL
,
9612 get_identifier ("__for_begin"), iter_type
);
9613 TREE_USED (begin
) = 1;
9614 DECL_ARTIFICIAL (begin
) = 1;
9616 cp_finish_decl (begin
, begin_expr
,
9617 /*is_constant_init*/false, NULL_TREE
,
9618 LOOKUP_ONLYCONVERTING
);
9620 end
= build_decl (input_location
, VAR_DECL
,
9621 get_identifier ("__for_end"), iter_type
);
9622 TREE_USED (end
) = 1;
9623 DECL_ARTIFICIAL (end
) = 1;
9625 cp_finish_decl (end
, end_expr
,
9626 /*is_constant_init*/false, NULL_TREE
,
9627 LOOKUP_ONLYCONVERTING
);
9629 finish_for_init_stmt (statement
);
9631 /* The new for condition. */
9632 condition
= build_x_binary_op (input_location
, NE_EXPR
,
9635 NULL
, tf_warning_or_error
);
9636 finish_for_cond (condition
, statement
);
9638 /* The new increment expression. */
9639 expression
= finish_unary_op_expr (input_location
,
9640 PREINCREMENT_EXPR
, begin
);
9641 finish_for_expr (expression
, statement
);
9643 /* The declaration is initialized with *__begin inside the loop body. */
9644 cp_finish_decl (range_decl
,
9645 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
9646 tf_warning_or_error
),
9647 /*is_constant_init*/false, NULL_TREE
,
9648 LOOKUP_ONLYCONVERTING
);
9653 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9654 We need to solve both at the same time because the method used
9655 depends on the existence of members begin or end.
9656 Returns the type deduced for the iterator expression. */
9659 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
9661 if (error_operand_p (range
))
9663 *begin
= *end
= error_mark_node
;
9664 return error_mark_node
;
9667 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
9669 error ("range-based %<for%> expression of type %qT "
9670 "has incomplete type", TREE_TYPE (range
));
9671 *begin
= *end
= error_mark_node
;
9672 return error_mark_node
;
9674 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
9676 /* If RANGE is an array, we will use pointer arithmetic. */
9678 *end
= build_binary_op (input_location
, PLUS_EXPR
,
9680 array_type_nelts_top (TREE_TYPE (range
)),
9682 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
9686 /* If it is not an array, we must do a bit of magic. */
9687 tree id_begin
, id_end
;
9688 tree member_begin
, member_end
;
9690 *begin
= *end
= error_mark_node
;
9692 id_begin
= get_identifier ("begin");
9693 id_end
= get_identifier ("end");
9694 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
9695 /*protect=*/2, /*want_type=*/false,
9696 tf_warning_or_error
);
9697 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
9698 /*protect=*/2, /*want_type=*/false,
9699 tf_warning_or_error
);
9701 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
9703 /* Use the member functions. */
9704 if (member_begin
!= NULL_TREE
)
9705 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
9707 error ("range-based %<for%> expression of type %qT has an "
9708 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
9710 if (member_end
!= NULL_TREE
)
9711 *end
= cp_parser_range_for_member_function (range
, id_end
);
9713 error ("range-based %<for%> expression of type %qT has a "
9714 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
9718 /* Use global functions with ADL. */
9720 vec
= make_tree_vector ();
9722 VEC_safe_push (tree
, gc
, vec
, range
);
9724 member_begin
= perform_koenig_lookup (id_begin
, vec
,
9725 /*include_std=*/true,
9726 tf_warning_or_error
);
9727 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
9728 tf_warning_or_error
);
9729 member_end
= perform_koenig_lookup (id_end
, vec
,
9730 /*include_std=*/true,
9731 tf_warning_or_error
);
9732 *end
= finish_call_expr (member_end
, &vec
, false, true,
9733 tf_warning_or_error
);
9735 release_tree_vector (vec
);
9738 /* Last common checks. */
9739 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
9741 /* If one of the expressions is an error do no more checks. */
9742 *begin
= *end
= error_mark_node
;
9743 return error_mark_node
;
9747 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
9748 /* The unqualified type of the __begin and __end temporaries should
9749 be the same, as required by the multiple auto declaration. */
9750 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
9751 error ("inconsistent begin/end types in range-based %<for%> "
9752 "statement: %qT and %qT",
9753 TREE_TYPE (*begin
), TREE_TYPE (*end
));
9759 /* Helper function for cp_parser_perform_range_for_lookup.
9760 Builds a tree for RANGE.IDENTIFIER(). */
9763 cp_parser_range_for_member_function (tree range
, tree identifier
)
9768 member
= finish_class_member_access_expr (range
, identifier
,
9769 false, tf_warning_or_error
);
9770 if (member
== error_mark_node
)
9771 return error_mark_node
;
9773 vec
= make_tree_vector ();
9774 res
= finish_call_expr (member
, &vec
,
9775 /*disallow_virtual=*/false,
9777 tf_warning_or_error
);
9778 release_tree_vector (vec
);
9782 /* Parse an iteration-statement.
9784 iteration-statement:
9785 while ( condition ) statement
9786 do statement while ( expression ) ;
9787 for ( for-init-statement condition [opt] ; expression [opt] )
9790 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9793 cp_parser_iteration_statement (cp_parser
* parser
)
9798 unsigned char in_statement
;
9800 /* Peek at the next token. */
9801 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
9803 return error_mark_node
;
9805 /* Remember whether or not we are already within an iteration
9807 in_statement
= parser
->in_statement
;
9809 /* See what kind of keyword it is. */
9810 keyword
= token
->keyword
;
9817 /* Begin the while-statement. */
9818 statement
= begin_while_stmt ();
9819 /* Look for the `('. */
9820 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9821 /* Parse the condition. */
9822 condition
= cp_parser_condition (parser
);
9823 finish_while_stmt_cond (condition
, statement
);
9824 /* Look for the `)'. */
9825 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9826 /* Parse the dependent statement. */
9827 parser
->in_statement
= IN_ITERATION_STMT
;
9828 cp_parser_already_scoped_statement (parser
);
9829 parser
->in_statement
= in_statement
;
9830 /* We're done with the while-statement. */
9831 finish_while_stmt (statement
);
9839 /* Begin the do-statement. */
9840 statement
= begin_do_stmt ();
9841 /* Parse the body of the do-statement. */
9842 parser
->in_statement
= IN_ITERATION_STMT
;
9843 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9844 parser
->in_statement
= in_statement
;
9845 finish_do_body (statement
);
9846 /* Look for the `while' keyword. */
9847 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
9848 /* Look for the `('. */
9849 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9850 /* Parse the expression. */
9851 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9852 /* We're done with the do-statement. */
9853 finish_do_stmt (expression
, statement
);
9854 /* Look for the `)'. */
9855 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9856 /* Look for the `;'. */
9857 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9863 /* Look for the `('. */
9864 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9866 statement
= cp_parser_for (parser
);
9868 /* Look for the `)'. */
9869 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9871 /* Parse the body of the for-statement. */
9872 parser
->in_statement
= IN_ITERATION_STMT
;
9873 cp_parser_already_scoped_statement (parser
);
9874 parser
->in_statement
= in_statement
;
9876 /* We're done with the for-statement. */
9877 finish_for_stmt (statement
);
9882 cp_parser_error (parser
, "expected iteration-statement");
9883 statement
= error_mark_node
;
9890 /* Parse a for-init-statement or the declarator of a range-based-for.
9891 Returns true if a range-based-for declaration is seen.
9894 expression-statement
9895 simple-declaration */
9898 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
9900 /* If the next token is a `;', then we have an empty
9901 expression-statement. Grammatically, this is also a
9902 simple-declaration, but an invalid one, because it does not
9903 declare anything. Therefore, if we did not handle this case
9904 specially, we would issue an error message about an invalid
9906 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9908 bool is_range_for
= false;
9909 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9911 parser
->colon_corrects_to_scope_p
= false;
9913 /* We're going to speculatively look for a declaration, falling back
9914 to an expression, if necessary. */
9915 cp_parser_parse_tentatively (parser
);
9916 /* Parse the declaration. */
9917 cp_parser_simple_declaration (parser
,
9918 /*function_definition_allowed_p=*/false,
9920 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9921 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9923 /* It is a range-for, consume the ':' */
9924 cp_lexer_consume_token (parser
->lexer
);
9925 is_range_for
= true;
9926 if (cxx_dialect
< cxx0x
)
9928 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
9929 "range-based %<for%> loops are not allowed "
9931 *decl
= error_mark_node
;
9935 /* The ';' is not consumed yet because we told
9936 cp_parser_simple_declaration not to. */
9937 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9939 if (cp_parser_parse_definitely (parser
))
9940 return is_range_for
;
9941 /* If the tentative parse failed, then we shall need to look for an
9942 expression-statement. */
9944 /* If we are here, it is an expression-statement. */
9945 cp_parser_expression_statement (parser
, NULL_TREE
);
9949 /* Parse a jump-statement.
9954 return expression [opt] ;
9955 return braced-init-list ;
9963 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9966 cp_parser_jump_statement (cp_parser
* parser
)
9968 tree statement
= error_mark_node
;
9971 unsigned char in_statement
;
9973 /* Peek at the next token. */
9974 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
9976 return error_mark_node
;
9978 /* See what kind of keyword it is. */
9979 keyword
= token
->keyword
;
9983 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
9984 switch (in_statement
)
9987 error_at (token
->location
, "break statement not within loop or switch");
9990 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
9991 || in_statement
== IN_ITERATION_STMT
);
9992 statement
= finish_break_stmt ();
9995 error_at (token
->location
, "invalid exit from OpenMP structured block");
9998 error_at (token
->location
, "break statement used with OpenMP for loop");
10001 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10005 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10008 error_at (token
->location
, "continue statement not within a loop");
10010 case IN_ITERATION_STMT
:
10012 statement
= finish_continue_stmt ();
10015 error_at (token
->location
, "invalid exit from OpenMP structured block");
10018 gcc_unreachable ();
10020 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10026 bool expr_non_constant_p
;
10028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10030 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10031 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10033 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10034 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
10036 /* If the next token is a `;', then there is no
10039 /* Build the return-statement. */
10040 statement
= finish_return_stmt (expr
);
10041 /* Look for the final `;'. */
10042 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10047 /* Create the goto-statement. */
10048 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
10050 /* Issue a warning about this use of a GNU extension. */
10051 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
10052 /* Consume the '*' token. */
10053 cp_lexer_consume_token (parser
->lexer
);
10054 /* Parse the dependent expression. */
10055 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
10058 finish_goto_stmt (cp_parser_identifier (parser
));
10059 /* Look for the final `;'. */
10060 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10064 cp_parser_error (parser
, "expected jump-statement");
10071 /* Parse a declaration-statement.
10073 declaration-statement:
10074 block-declaration */
10077 cp_parser_declaration_statement (cp_parser
* parser
)
10081 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10082 p
= obstack_alloc (&declarator_obstack
, 0);
10084 /* Parse the block-declaration. */
10085 cp_parser_block_declaration (parser
, /*statement_p=*/true);
10087 /* Free any declarators allocated. */
10088 obstack_free (&declarator_obstack
, p
);
10090 /* Finish off the statement. */
10094 /* Some dependent statements (like `if (cond) statement'), are
10095 implicitly in their own scope. In other words, if the statement is
10096 a single statement (as opposed to a compound-statement), it is
10097 none-the-less treated as if it were enclosed in braces. Any
10098 declarations appearing in the dependent statement are out of scope
10099 after control passes that point. This function parses a statement,
10100 but ensures that is in its own scope, even if it is not a
10101 compound-statement.
10103 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10104 is a (possibly labeled) if statement which is not enclosed in
10105 braces and has an else clause. This is used to implement
10108 Returns the new statement. */
10111 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
10118 /* Mark if () ; with a special NOP_EXPR. */
10119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10121 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10122 cp_lexer_consume_token (parser
->lexer
);
10123 statement
= add_stmt (build_empty_stmt (loc
));
10125 /* if a compound is opened, we simply parse the statement directly. */
10126 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10127 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
10128 /* If the token is not a `{', then we must take special action. */
10131 /* Create a compound-statement. */
10132 statement
= begin_compound_stmt (0);
10133 /* Parse the dependent-statement. */
10134 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
10135 /* Finish the dummy compound-statement. */
10136 finish_compound_stmt (statement
);
10139 /* Return the statement. */
10143 /* For some dependent statements (like `while (cond) statement'), we
10144 have already created a scope. Therefore, even if the dependent
10145 statement is a compound-statement, we do not want to create another
10149 cp_parser_already_scoped_statement (cp_parser
* parser
)
10151 /* If the token is a `{', then we must take special action. */
10152 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10153 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
10156 /* Avoid calling cp_parser_compound_statement, so that we
10157 don't create a new scope. Do everything else by hand. */
10158 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
10159 /* If the next keyword is `__label__' we have a label declaration. */
10160 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10161 cp_parser_label_declaration (parser
);
10162 /* Parse an (optional) statement-seq. */
10163 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10164 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10168 /* Declarations [gram.dcl.dcl] */
10170 /* Parse an optional declaration-sequence.
10174 declaration-seq declaration */
10177 cp_parser_declaration_seq_opt (cp_parser
* parser
)
10183 token
= cp_lexer_peek_token (parser
->lexer
);
10185 if (token
->type
== CPP_CLOSE_BRACE
10186 || token
->type
== CPP_EOF
10187 || token
->type
== CPP_PRAGMA_EOL
)
10190 if (token
->type
== CPP_SEMICOLON
)
10192 /* A declaration consisting of a single semicolon is
10193 invalid. Allow it unless we're being pedantic. */
10194 cp_lexer_consume_token (parser
->lexer
);
10195 if (!in_system_header
)
10196 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
10200 /* If we're entering or exiting a region that's implicitly
10201 extern "C", modify the lang context appropriately. */
10202 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
10204 push_lang_context (lang_name_c
);
10205 parser
->implicit_extern_c
= true;
10207 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
10209 pop_lang_context ();
10210 parser
->implicit_extern_c
= false;
10213 if (token
->type
== CPP_PRAGMA
)
10215 /* A top-level declaration can consist solely of a #pragma.
10216 A nested declaration cannot, so this is done here and not
10217 in cp_parser_declaration. (A #pragma at block scope is
10218 handled in cp_parser_statement.) */
10219 cp_parser_pragma (parser
, pragma_external
);
10223 /* Parse the declaration itself. */
10224 cp_parser_declaration (parser
);
10228 /* Parse a declaration.
10232 function-definition
10233 template-declaration
10234 explicit-instantiation
10235 explicit-specialization
10236 linkage-specification
10237 namespace-definition
10242 __extension__ declaration */
10245 cp_parser_declaration (cp_parser
* parser
)
10249 int saved_pedantic
;
10251 tree attributes
= NULL_TREE
;
10253 /* Check for the `__extension__' keyword. */
10254 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10256 /* Parse the qualified declaration. */
10257 cp_parser_declaration (parser
);
10258 /* Restore the PEDANTIC flag. */
10259 pedantic
= saved_pedantic
;
10264 /* Try to figure out what kind of declaration is present. */
10265 token1
= *cp_lexer_peek_token (parser
->lexer
);
10267 if (token1
.type
!= CPP_EOF
)
10268 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
10271 token2
.type
= CPP_EOF
;
10272 token2
.keyword
= RID_MAX
;
10275 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10276 p
= obstack_alloc (&declarator_obstack
, 0);
10278 /* If the next token is `extern' and the following token is a string
10279 literal, then we have a linkage specification. */
10280 if (token1
.keyword
== RID_EXTERN
10281 && cp_parser_is_pure_string_literal (&token2
))
10282 cp_parser_linkage_specification (parser
);
10283 /* If the next token is `template', then we have either a template
10284 declaration, an explicit instantiation, or an explicit
10286 else if (token1
.keyword
== RID_TEMPLATE
)
10288 /* `template <>' indicates a template specialization. */
10289 if (token2
.type
== CPP_LESS
10290 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
10291 cp_parser_explicit_specialization (parser
);
10292 /* `template <' indicates a template declaration. */
10293 else if (token2
.type
== CPP_LESS
)
10294 cp_parser_template_declaration (parser
, /*member_p=*/false);
10295 /* Anything else must be an explicit instantiation. */
10297 cp_parser_explicit_instantiation (parser
);
10299 /* If the next token is `export', then we have a template
10301 else if (token1
.keyword
== RID_EXPORT
)
10302 cp_parser_template_declaration (parser
, /*member_p=*/false);
10303 /* If the next token is `extern', 'static' or 'inline' and the one
10304 after that is `template', we have a GNU extended explicit
10305 instantiation directive. */
10306 else if (cp_parser_allow_gnu_extensions_p (parser
)
10307 && (token1
.keyword
== RID_EXTERN
10308 || token1
.keyword
== RID_STATIC
10309 || token1
.keyword
== RID_INLINE
)
10310 && token2
.keyword
== RID_TEMPLATE
)
10311 cp_parser_explicit_instantiation (parser
);
10312 /* If the next token is `namespace', check for a named or unnamed
10313 namespace definition. */
10314 else if (token1
.keyword
== RID_NAMESPACE
10315 && (/* A named namespace definition. */
10316 (token2
.type
== CPP_NAME
10317 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
10319 /* An unnamed namespace definition. */
10320 || token2
.type
== CPP_OPEN_BRACE
10321 || token2
.keyword
== RID_ATTRIBUTE
))
10322 cp_parser_namespace_definition (parser
);
10323 /* An inline (associated) namespace definition. */
10324 else if (token1
.keyword
== RID_INLINE
10325 && token2
.keyword
== RID_NAMESPACE
)
10326 cp_parser_namespace_definition (parser
);
10327 /* Objective-C++ declaration/definition. */
10328 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
10329 cp_parser_objc_declaration (parser
, NULL_TREE
);
10330 else if (c_dialect_objc ()
10331 && token1
.keyword
== RID_ATTRIBUTE
10332 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
10333 cp_parser_objc_declaration (parser
, attributes
);
10334 /* We must have either a block declaration or a function
10337 /* Try to parse a block-declaration, or a function-definition. */
10338 cp_parser_block_declaration (parser
, /*statement_p=*/false);
10340 /* Free any declarators allocated. */
10341 obstack_free (&declarator_obstack
, p
);
10344 /* Parse a block-declaration.
10349 namespace-alias-definition
10356 __extension__ block-declaration
10361 static_assert-declaration
10363 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10364 part of a declaration-statement. */
10367 cp_parser_block_declaration (cp_parser
*parser
,
10371 int saved_pedantic
;
10373 /* Check for the `__extension__' keyword. */
10374 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10376 /* Parse the qualified declaration. */
10377 cp_parser_block_declaration (parser
, statement_p
);
10378 /* Restore the PEDANTIC flag. */
10379 pedantic
= saved_pedantic
;
10384 /* Peek at the next token to figure out which kind of declaration is
10386 token1
= cp_lexer_peek_token (parser
->lexer
);
10388 /* If the next keyword is `asm', we have an asm-definition. */
10389 if (token1
->keyword
== RID_ASM
)
10392 cp_parser_commit_to_tentative_parse (parser
);
10393 cp_parser_asm_definition (parser
);
10395 /* If the next keyword is `namespace', we have a
10396 namespace-alias-definition. */
10397 else if (token1
->keyword
== RID_NAMESPACE
)
10398 cp_parser_namespace_alias_definition (parser
);
10399 /* If the next keyword is `using', we have a
10400 using-declaration, a using-directive, or an alias-declaration. */
10401 else if (token1
->keyword
== RID_USING
)
10406 cp_parser_commit_to_tentative_parse (parser
);
10407 /* If the token after `using' is `namespace', then we have a
10408 using-directive. */
10409 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10410 if (token2
->keyword
== RID_NAMESPACE
)
10411 cp_parser_using_directive (parser
);
10412 /* If the second token after 'using' is '=', then we have an
10413 alias-declaration. */
10414 else if (cxx_dialect
>= cxx0x
10415 && token2
->type
== CPP_NAME
10416 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
10417 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
10418 cp_parser_alias_declaration (parser
);
10419 /* Otherwise, it's a using-declaration. */
10421 cp_parser_using_declaration (parser
,
10422 /*access_declaration_p=*/false);
10424 /* If the next keyword is `__label__' we have a misplaced label
10426 else if (token1
->keyword
== RID_LABEL
)
10428 cp_lexer_consume_token (parser
->lexer
);
10429 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
10430 cp_parser_skip_to_end_of_statement (parser
);
10431 /* If the next token is now a `;', consume it. */
10432 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10433 cp_lexer_consume_token (parser
->lexer
);
10435 /* If the next token is `static_assert' we have a static assertion. */
10436 else if (token1
->keyword
== RID_STATIC_ASSERT
)
10437 cp_parser_static_assert (parser
, /*member_p=*/false);
10438 /* Anything else must be a simple-declaration. */
10440 cp_parser_simple_declaration (parser
, !statement_p
,
10441 /*maybe_range_for_decl*/NULL
);
10444 /* Parse a simple-declaration.
10446 simple-declaration:
10447 decl-specifier-seq [opt] init-declarator-list [opt] ;
10449 init-declarator-list:
10451 init-declarator-list , init-declarator
10453 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10454 function-definition as a simple-declaration.
10456 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10457 parsed declaration if it is an uninitialized single declarator not followed
10458 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10459 if present, will not be consumed. */
10462 cp_parser_simple_declaration (cp_parser
* parser
,
10463 bool function_definition_allowed_p
,
10464 tree
*maybe_range_for_decl
)
10466 cp_decl_specifier_seq decl_specifiers
;
10467 int declares_class_or_enum
;
10468 bool saw_declarator
;
10470 if (maybe_range_for_decl
)
10471 *maybe_range_for_decl
= NULL_TREE
;
10473 /* Defer access checks until we know what is being declared; the
10474 checks for names appearing in the decl-specifier-seq should be
10475 done as if we were in the scope of the thing being declared. */
10476 push_deferring_access_checks (dk_deferred
);
10478 /* Parse the decl-specifier-seq. We have to keep track of whether
10479 or not the decl-specifier-seq declares a named class or
10480 enumeration type, since that is the only case in which the
10481 init-declarator-list is allowed to be empty.
10485 In a simple-declaration, the optional init-declarator-list can be
10486 omitted only when declaring a class or enumeration, that is when
10487 the decl-specifier-seq contains either a class-specifier, an
10488 elaborated-type-specifier, or an enum-specifier. */
10489 cp_parser_decl_specifier_seq (parser
,
10490 CP_PARSER_FLAGS_OPTIONAL
,
10492 &declares_class_or_enum
);
10493 /* We no longer need to defer access checks. */
10494 stop_deferring_access_checks ();
10496 /* In a block scope, a valid declaration must always have a
10497 decl-specifier-seq. By not trying to parse declarators, we can
10498 resolve the declaration/expression ambiguity more quickly. */
10499 if (!function_definition_allowed_p
10500 && !decl_specifiers
.any_specifiers_p
)
10502 cp_parser_error (parser
, "expected declaration");
10506 /* If the next two tokens are both identifiers, the code is
10507 erroneous. The usual cause of this situation is code like:
10511 where "T" should name a type -- but does not. */
10512 if (!decl_specifiers
.any_type_specifiers_p
10513 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
10515 /* If parsing tentatively, we should commit; we really are
10516 looking at a declaration. */
10517 cp_parser_commit_to_tentative_parse (parser
);
10522 /* If we have seen at least one decl-specifier, and the next token
10523 is not a parenthesis, then we must be looking at a declaration.
10524 (After "int (" we might be looking at a functional cast.) */
10525 if (decl_specifiers
.any_specifiers_p
10526 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
10527 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
10528 && !cp_parser_error_occurred (parser
))
10529 cp_parser_commit_to_tentative_parse (parser
);
10531 /* Keep going until we hit the `;' at the end of the simple
10533 saw_declarator
= false;
10534 while (cp_lexer_next_token_is_not (parser
->lexer
,
10538 bool function_definition_p
;
10541 if (saw_declarator
)
10543 /* If we are processing next declarator, coma is expected */
10544 token
= cp_lexer_peek_token (parser
->lexer
);
10545 gcc_assert (token
->type
== CPP_COMMA
);
10546 cp_lexer_consume_token (parser
->lexer
);
10547 if (maybe_range_for_decl
)
10548 *maybe_range_for_decl
= error_mark_node
;
10551 saw_declarator
= true;
10553 /* Parse the init-declarator. */
10554 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
10556 function_definition_allowed_p
,
10557 /*member_p=*/false,
10558 declares_class_or_enum
,
10559 &function_definition_p
,
10560 maybe_range_for_decl
);
10561 /* If an error occurred while parsing tentatively, exit quickly.
10562 (That usually happens when in the body of a function; each
10563 statement is treated as a declaration-statement until proven
10565 if (cp_parser_error_occurred (parser
))
10567 /* Handle function definitions specially. */
10568 if (function_definition_p
)
10570 /* If the next token is a `,', then we are probably
10571 processing something like:
10575 which is erroneous. */
10576 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10578 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10579 error_at (token
->location
,
10581 " declarations and function-definitions is forbidden");
10583 /* Otherwise, we're done with the list of declarators. */
10586 pop_deferring_access_checks ();
10590 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
10591 *maybe_range_for_decl
= decl
;
10592 /* The next token should be either a `,' or a `;'. */
10593 token
= cp_lexer_peek_token (parser
->lexer
);
10594 /* If it's a `,', there are more declarators to come. */
10595 if (token
->type
== CPP_COMMA
)
10596 /* will be consumed next time around */;
10597 /* If it's a `;', we are done. */
10598 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
10600 /* Anything else is an error. */
10603 /* If we have already issued an error message we don't need
10604 to issue another one. */
10605 if (decl
!= error_mark_node
10606 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
10607 cp_parser_error (parser
, "expected %<,%> or %<;%>");
10608 /* Skip tokens until we reach the end of the statement. */
10609 cp_parser_skip_to_end_of_statement (parser
);
10610 /* If the next token is now a `;', consume it. */
10611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10612 cp_lexer_consume_token (parser
->lexer
);
10615 /* After the first time around, a function-definition is not
10616 allowed -- even if it was OK at first. For example:
10621 function_definition_allowed_p
= false;
10624 /* Issue an error message if no declarators are present, and the
10625 decl-specifier-seq does not itself declare a class or
10627 if (!saw_declarator
)
10629 if (cp_parser_declares_only_class_p (parser
))
10630 shadow_tag (&decl_specifiers
);
10631 /* Perform any deferred access checks. */
10632 perform_deferred_access_checks (tf_warning_or_error
);
10635 /* Consume the `;'. */
10636 if (!maybe_range_for_decl
)
10637 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10640 pop_deferring_access_checks ();
10643 /* Parse a decl-specifier-seq.
10645 decl-specifier-seq:
10646 decl-specifier-seq [opt] decl-specifier
10647 decl-specifier attribute-specifier-seq [opt] (C++11)
10650 storage-class-specifier
10661 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10663 The parser flags FLAGS is used to control type-specifier parsing.
10665 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10668 1: one of the decl-specifiers is an elaborated-type-specifier
10669 (i.e., a type declaration)
10670 2: one of the decl-specifiers is an enum-specifier or a
10671 class-specifier (i.e., a type definition)
10676 cp_parser_decl_specifier_seq (cp_parser
* parser
,
10677 cp_parser_flags flags
,
10678 cp_decl_specifier_seq
*decl_specs
,
10679 int* declares_class_or_enum
)
10681 bool constructor_possible_p
= !parser
->in_declarator_p
;
10682 bool found_decl_spec
= false;
10683 cp_token
*start_token
= NULL
;
10686 /* Clear DECL_SPECS. */
10687 clear_decl_specs (decl_specs
);
10689 /* Assume no class or enumeration type is declared. */
10690 *declares_class_or_enum
= 0;
10692 /* Keep reading specifiers until there are no more to read. */
10695 bool constructor_p
;
10699 /* Peek at the next token. */
10700 token
= cp_lexer_peek_token (parser
->lexer
);
10702 /* Save the first token of the decl spec list for error
10705 start_token
= token
;
10706 /* Handle attributes. */
10707 if (cp_next_tokens_can_be_attribute_p (parser
))
10709 /* Parse the attributes. */
10710 tree attrs
= cp_parser_attributes_opt (parser
);
10712 /* In a sequence of declaration specifiers, c++11 attributes
10713 appertain to the type that precede them. In that case
10716 The attribute-specifier-seq affects the type only for
10717 the declaration it appears in, not other declarations
10718 involving the same type.
10720 But for now let's force the user to position the
10721 attribute either at the beginning of the declaration or
10722 after the declarator-id, which would clearly mean that it
10723 applies to the declarator. */
10724 if (cxx11_attribute_p (attrs
))
10726 if (!found_decl_spec
)
10727 /* The c++11 attribute is at the beginning of the
10728 declaration. It appertains to the entity being
10732 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
10734 /* This is an attribute following a
10735 class-specifier. */
10736 if (decl_specs
->type_definition_p
)
10737 warn_misplaced_attr_for_class_type (token
->location
,
10743 decl_specs
->std_attributes
10744 = chainon (decl_specs
->std_attributes
,
10746 if (decl_specs
->locations
[ds_std_attribute
] == 0)
10747 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
10753 decl_specs
->attributes
10754 = chainon (decl_specs
->attributes
,
10756 if (decl_specs
->locations
[ds_attribute
] == 0)
10757 decl_specs
->locations
[ds_attribute
] = token
->location
;
10760 /* Assume we will find a decl-specifier keyword. */
10761 found_decl_spec
= true;
10762 /* If the next token is an appropriate keyword, we can simply
10763 add it to the list. */
10764 switch (token
->keyword
)
10770 if (!at_class_scope_p ())
10772 error_at (token
->location
, "%<friend%> used outside of class");
10773 cp_lexer_purge_token (parser
->lexer
);
10778 /* Consume the token. */
10779 cp_lexer_consume_token (parser
->lexer
);
10783 case RID_CONSTEXPR
:
10785 cp_lexer_consume_token (parser
->lexer
);
10788 /* function-specifier:
10795 cp_parser_function_specifier_opt (parser
, decl_specs
);
10802 /* Consume the token. */
10803 cp_lexer_consume_token (parser
->lexer
);
10804 /* A constructor declarator cannot appear in a typedef. */
10805 constructor_possible_p
= false;
10806 /* The "typedef" keyword can only occur in a declaration; we
10807 may as well commit at this point. */
10808 cp_parser_commit_to_tentative_parse (parser
);
10810 if (decl_specs
->storage_class
!= sc_none
)
10811 decl_specs
->conflicting_specifiers_p
= true;
10814 /* storage-class-specifier:
10824 if (cxx_dialect
== cxx98
)
10826 /* Consume the token. */
10827 cp_lexer_consume_token (parser
->lexer
);
10829 /* Complain about `auto' as a storage specifier, if
10830 we're complaining about C++0x compatibility. */
10831 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
10832 " changes meaning in C++11; please remove it");
10834 /* Set the storage class anyway. */
10835 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
10839 /* C++0x auto type-specifier. */
10840 found_decl_spec
= false;
10847 /* Consume the token. */
10848 cp_lexer_consume_token (parser
->lexer
);
10849 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
10853 /* Consume the token. */
10855 cp_lexer_consume_token (parser
->lexer
);
10859 /* We did not yet find a decl-specifier yet. */
10860 found_decl_spec
= false;
10864 if (found_decl_spec
10865 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
10866 && token
->keyword
!= RID_CONSTEXPR
)
10867 error ("decl-specifier invalid in condition");
10870 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
10872 /* Constructors are a special case. The `S' in `S()' is not a
10873 decl-specifier; it is the beginning of the declarator. */
10875 = (!found_decl_spec
10876 && constructor_possible_p
10877 && (cp_parser_constructor_declarator_p
10878 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
10880 /* If we don't have a DECL_SPEC yet, then we must be looking at
10881 a type-specifier. */
10882 if (!found_decl_spec
&& !constructor_p
)
10884 int decl_spec_declares_class_or_enum
;
10885 bool is_cv_qualifier
;
10889 = cp_parser_type_specifier (parser
, flags
,
10891 /*is_declaration=*/true,
10892 &decl_spec_declares_class_or_enum
,
10894 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
10896 /* If this type-specifier referenced a user-defined type
10897 (a typedef, class-name, etc.), then we can't allow any
10898 more such type-specifiers henceforth.
10902 The longest sequence of decl-specifiers that could
10903 possibly be a type name is taken as the
10904 decl-specifier-seq of a declaration. The sequence shall
10905 be self-consistent as described below.
10909 As a general rule, at most one type-specifier is allowed
10910 in the complete decl-specifier-seq of a declaration. The
10911 only exceptions are the following:
10913 -- const or volatile can be combined with any other
10916 -- signed or unsigned can be combined with char, long,
10924 void g (const int Pc);
10926 Here, Pc is *not* part of the decl-specifier seq; it's
10927 the declarator. Therefore, once we see a type-specifier
10928 (other than a cv-qualifier), we forbid any additional
10929 user-defined types. We *do* still allow things like `int
10930 int' to be considered a decl-specifier-seq, and issue the
10931 error message later. */
10932 if (type_spec
&& !is_cv_qualifier
)
10933 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
10934 /* A constructor declarator cannot follow a type-specifier. */
10937 constructor_possible_p
= false;
10938 found_decl_spec
= true;
10939 if (!is_cv_qualifier
)
10940 decl_specs
->any_type_specifiers_p
= true;
10944 /* If we still do not have a DECL_SPEC, then there are no more
10945 decl-specifiers. */
10946 if (!found_decl_spec
)
10949 decl_specs
->any_specifiers_p
= true;
10950 /* After we see one decl-specifier, further decl-specifiers are
10951 always optional. */
10952 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
10955 /* Don't allow a friend specifier with a class definition. */
10956 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
10957 && (*declares_class_or_enum
& 2))
10958 error_at (decl_specs
->locations
[ds_friend
],
10959 "class definition may not be declared a friend");
10962 /* Parse an (optional) storage-class-specifier.
10964 storage-class-specifier:
10973 storage-class-specifier:
10976 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10979 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
10981 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
10984 if (cxx_dialect
!= cxx98
)
10986 /* Fall through for C++98. */
10993 /* Consume the token. */
10994 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11001 /* Parse an (optional) function-specifier.
11003 function-specifier:
11008 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11009 Updates DECL_SPECS, if it is non-NULL. */
11012 cp_parser_function_specifier_opt (cp_parser
* parser
,
11013 cp_decl_specifier_seq
*decl_specs
)
11015 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11016 switch (token
->keyword
)
11019 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
11023 /* 14.5.2.3 [temp.mem]
11025 A member function template shall not be virtual. */
11026 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11027 error_at (token
->location
, "templates may not be %<virtual%>");
11028 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
11032 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
11039 /* Consume the token. */
11040 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11043 /* Parse a linkage-specification.
11045 linkage-specification:
11046 extern string-literal { declaration-seq [opt] }
11047 extern string-literal declaration */
11050 cp_parser_linkage_specification (cp_parser
* parser
)
11054 /* Look for the `extern' keyword. */
11055 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
11057 /* Look for the string-literal. */
11058 linkage
= cp_parser_string_literal (parser
, false, false);
11060 /* Transform the literal into an identifier. If the literal is a
11061 wide-character string, or contains embedded NULs, then we can't
11062 handle it as the user wants. */
11063 if (strlen (TREE_STRING_POINTER (linkage
))
11064 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
11066 cp_parser_error (parser
, "invalid linkage-specification");
11067 /* Assume C++ linkage. */
11068 linkage
= lang_name_cplusplus
;
11071 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
11073 /* We're now using the new linkage. */
11074 push_lang_context (linkage
);
11076 /* If the next token is a `{', then we're using the first
11078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11080 /* Consume the `{' token. */
11081 cp_lexer_consume_token (parser
->lexer
);
11082 /* Parse the declarations. */
11083 cp_parser_declaration_seq_opt (parser
);
11084 /* Look for the closing `}'. */
11085 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11087 /* Otherwise, there's just one declaration. */
11090 bool saved_in_unbraced_linkage_specification_p
;
11092 saved_in_unbraced_linkage_specification_p
11093 = parser
->in_unbraced_linkage_specification_p
;
11094 parser
->in_unbraced_linkage_specification_p
= true;
11095 cp_parser_declaration (parser
);
11096 parser
->in_unbraced_linkage_specification_p
11097 = saved_in_unbraced_linkage_specification_p
;
11100 /* We're done with the linkage-specification. */
11101 pop_lang_context ();
11104 /* Parse a static_assert-declaration.
11106 static_assert-declaration:
11107 static_assert ( constant-expression , string-literal ) ;
11109 If MEMBER_P, this static_assert is a class member. */
11112 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
11117 location_t saved_loc
;
11120 /* Peek at the `static_assert' token so we can keep track of exactly
11121 where the static assertion started. */
11122 token
= cp_lexer_peek_token (parser
->lexer
);
11123 saved_loc
= token
->location
;
11125 /* Look for the `static_assert' keyword. */
11126 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
11130 /* We know we are in a static assertion; commit to any tentative
11132 if (cp_parser_parsing_tentatively (parser
))
11133 cp_parser_commit_to_tentative_parse (parser
);
11135 /* Parse the `(' starting the static assertion condition. */
11136 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11138 /* Parse the constant-expression. Allow a non-constant expression
11139 here in order to give better diagnostics in finish_static_assert. */
11141 cp_parser_constant_expression (parser
,
11142 /*allow_non_constant_p=*/true,
11143 /*non_constant_p=*/&dummy
);
11145 /* Parse the separating `,'. */
11146 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
11148 /* Parse the string-literal message. */
11149 message
= cp_parser_string_literal (parser
,
11150 /*translate=*/false,
11153 /* A `)' completes the static assertion. */
11154 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11155 cp_parser_skip_to_closing_parenthesis (parser
,
11156 /*recovering=*/true,
11157 /*or_comma=*/false,
11158 /*consume_paren=*/true);
11160 /* A semicolon terminates the declaration. */
11161 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11163 /* Complete the static assertion, which may mean either processing
11164 the static assert now or saving it for template instantiation. */
11165 finish_static_assert (condition
, message
, saved_loc
, member_p
);
11168 /* Parse a `decltype' type. Returns the type.
11170 simple-type-specifier:
11171 decltype ( expression ) */
11174 cp_parser_decltype (cp_parser
*parser
)
11177 bool id_expression_or_member_access_p
= false;
11178 const char *saved_message
;
11179 bool saved_integral_constant_expression_p
;
11180 bool saved_non_integral_constant_expression_p
;
11181 cp_token
*id_expr_start_token
;
11182 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11184 if (start_token
->type
== CPP_DECLTYPE
)
11186 /* Already parsed. */
11187 cp_lexer_consume_token (parser
->lexer
);
11188 return start_token
->u
.value
;
11191 /* Look for the `decltype' token. */
11192 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
11193 return error_mark_node
;
11195 /* Types cannot be defined in a `decltype' expression. Save away the
11197 saved_message
= parser
->type_definition_forbidden_message
;
11199 /* And create the new one. */
11200 parser
->type_definition_forbidden_message
11201 = G_("types may not be defined in %<decltype%> expressions");
11203 /* The restrictions on constant-expressions do not apply inside
11204 decltype expressions. */
11205 saved_integral_constant_expression_p
11206 = parser
->integral_constant_expression_p
;
11207 saved_non_integral_constant_expression_p
11208 = parser
->non_integral_constant_expression_p
;
11209 parser
->integral_constant_expression_p
= false;
11211 /* Do not actually evaluate the expression. */
11212 ++cp_unevaluated_operand
;
11214 /* Do not warn about problems with the expression. */
11215 ++c_inhibit_evaluation_warnings
;
11217 /* Parse the opening `('. */
11218 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11219 return error_mark_node
;
11221 /* First, try parsing an id-expression. */
11222 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
11223 cp_parser_parse_tentatively (parser
);
11224 expr
= cp_parser_id_expression (parser
,
11225 /*template_keyword_p=*/false,
11226 /*check_dependency_p=*/true,
11227 /*template_p=*/NULL
,
11228 /*declarator_p=*/false,
11229 /*optional_p=*/false);
11231 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
11233 bool non_integral_constant_expression_p
= false;
11234 tree id_expression
= expr
;
11236 const char *error_msg
;
11238 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
11239 /* Lookup the name we got back from the id-expression. */
11240 expr
= cp_parser_lookup_name (parser
, expr
,
11242 /*is_template=*/false,
11243 /*is_namespace=*/false,
11244 /*check_dependency=*/true,
11245 /*ambiguous_decls=*/NULL
,
11246 id_expr_start_token
->location
);
11249 && expr
!= error_mark_node
11250 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
11251 && TREE_CODE (expr
) != TYPE_DECL
11252 && (TREE_CODE (expr
) != BIT_NOT_EXPR
11253 || !TYPE_P (TREE_OPERAND (expr
, 0)))
11254 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11256 /* Complete lookup of the id-expression. */
11257 expr
= (finish_id_expression
11258 (id_expression
, expr
, parser
->scope
, &idk
,
11259 /*integral_constant_expression_p=*/false,
11260 /*allow_non_integral_constant_expression_p=*/true,
11261 &non_integral_constant_expression_p
,
11262 /*template_p=*/false,
11264 /*address_p=*/false,
11265 /*template_arg_p=*/false,
11267 id_expr_start_token
->location
));
11269 if (expr
== error_mark_node
)
11270 /* We found an id-expression, but it was something that we
11271 should not have found. This is an error, not something
11272 we can recover from, so note that we found an
11273 id-expression and we'll recover as gracefully as
11275 id_expression_or_member_access_p
= true;
11279 && expr
!= error_mark_node
11280 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11281 /* We have an id-expression. */
11282 id_expression_or_member_access_p
= true;
11285 if (!id_expression_or_member_access_p
)
11287 /* Abort the id-expression parse. */
11288 cp_parser_abort_tentative_parse (parser
);
11290 /* Parsing tentatively, again. */
11291 cp_parser_parse_tentatively (parser
);
11293 /* Parse a class member access. */
11294 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
11296 /*member_access_only_p=*/true, NULL
);
11299 && expr
!= error_mark_node
11300 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11301 /* We have an id-expression. */
11302 id_expression_or_member_access_p
= true;
11305 if (id_expression_or_member_access_p
)
11306 /* We have parsed the complete id-expression or member access. */
11307 cp_parser_parse_definitely (parser
);
11310 bool saved_greater_than_is_operator_p
;
11312 /* Abort our attempt to parse an id-expression or member access
11314 cp_parser_abort_tentative_parse (parser
);
11316 /* Within a parenthesized expression, a `>' token is always
11317 the greater-than operator. */
11318 saved_greater_than_is_operator_p
11319 = parser
->greater_than_is_operator_p
;
11320 parser
->greater_than_is_operator_p
= true;
11322 /* Parse a full expression. */
11323 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
11325 /* The `>' token might be the end of a template-id or
11326 template-parameter-list now. */
11327 parser
->greater_than_is_operator_p
11328 = saved_greater_than_is_operator_p
;
11331 /* Go back to evaluating expressions. */
11332 --cp_unevaluated_operand
;
11333 --c_inhibit_evaluation_warnings
;
11335 /* Restore the old message and the integral constant expression
11337 parser
->type_definition_forbidden_message
= saved_message
;
11338 parser
->integral_constant_expression_p
11339 = saved_integral_constant_expression_p
;
11340 parser
->non_integral_constant_expression_p
11341 = saved_non_integral_constant_expression_p
;
11343 /* Parse to the closing `)'. */
11344 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11346 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11347 /*consume_paren=*/true);
11348 return error_mark_node
;
11351 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
11352 tf_warning_or_error
);
11354 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11356 start_token
->type
= CPP_DECLTYPE
;
11357 start_token
->u
.value
= expr
;
11358 start_token
->keyword
= RID_MAX
;
11359 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
11364 /* Special member functions [gram.special] */
11366 /* Parse a conversion-function-id.
11368 conversion-function-id:
11369 operator conversion-type-id
11371 Returns an IDENTIFIER_NODE representing the operator. */
11374 cp_parser_conversion_function_id (cp_parser
* parser
)
11378 tree saved_qualifying_scope
;
11379 tree saved_object_scope
;
11380 tree pushed_scope
= NULL_TREE
;
11382 /* Look for the `operator' token. */
11383 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
11384 return error_mark_node
;
11385 /* When we parse the conversion-type-id, the current scope will be
11386 reset. However, we need that information in able to look up the
11387 conversion function later, so we save it here. */
11388 saved_scope
= parser
->scope
;
11389 saved_qualifying_scope
= parser
->qualifying_scope
;
11390 saved_object_scope
= parser
->object_scope
;
11391 /* We must enter the scope of the class so that the names of
11392 entities declared within the class are available in the
11393 conversion-type-id. For example, consider:
11400 S::operator I() { ... }
11402 In order to see that `I' is a type-name in the definition, we
11403 must be in the scope of `S'. */
11405 pushed_scope
= push_scope (saved_scope
);
11406 /* Parse the conversion-type-id. */
11407 type
= cp_parser_conversion_type_id (parser
);
11408 /* Leave the scope of the class, if any. */
11410 pop_scope (pushed_scope
);
11411 /* Restore the saved scope. */
11412 parser
->scope
= saved_scope
;
11413 parser
->qualifying_scope
= saved_qualifying_scope
;
11414 parser
->object_scope
= saved_object_scope
;
11415 /* If the TYPE is invalid, indicate failure. */
11416 if (type
== error_mark_node
)
11417 return error_mark_node
;
11418 return mangle_conv_op_name_for_type (type
);
11421 /* Parse a conversion-type-id:
11423 conversion-type-id:
11424 type-specifier-seq conversion-declarator [opt]
11426 Returns the TYPE specified. */
11429 cp_parser_conversion_type_id (cp_parser
* parser
)
11432 cp_decl_specifier_seq type_specifiers
;
11433 cp_declarator
*declarator
;
11434 tree type_specified
;
11436 /* Parse the attributes. */
11437 attributes
= cp_parser_attributes_opt (parser
);
11438 /* Parse the type-specifiers. */
11439 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
11440 /*is_trailing_return=*/false,
11442 /* If that didn't work, stop. */
11443 if (type_specifiers
.type
== error_mark_node
)
11444 return error_mark_node
;
11445 /* Parse the conversion-declarator. */
11446 declarator
= cp_parser_conversion_declarator_opt (parser
);
11448 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
11449 /*initialized=*/0, &attributes
);
11451 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
11453 /* Don't give this error when parsing tentatively. This happens to
11454 work because we always parse this definitively once. */
11455 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
11456 && type_uses_auto (type_specified
))
11458 if (cxx_dialect
< cxx1y
)
11460 error ("invalid use of %<auto%> in conversion operator");
11461 return error_mark_node
;
11463 else if (template_parm_scope_p ())
11464 warning (0, "use of %<auto%> in member template "
11465 "conversion operator can never be deduced");
11468 return type_specified
;
11471 /* Parse an (optional) conversion-declarator.
11473 conversion-declarator:
11474 ptr-operator conversion-declarator [opt]
11478 static cp_declarator
*
11479 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
11481 enum tree_code code
;
11482 tree class_type
, std_attributes
= NULL_TREE
;
11483 cp_cv_quals cv_quals
;
11485 /* We don't know if there's a ptr-operator next, or not. */
11486 cp_parser_parse_tentatively (parser
);
11487 /* Try the ptr-operator. */
11488 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
11490 /* If it worked, look for more conversion-declarators. */
11491 if (cp_parser_parse_definitely (parser
))
11493 cp_declarator
*declarator
;
11495 /* Parse another optional declarator. */
11496 declarator
= cp_parser_conversion_declarator_opt (parser
);
11498 declarator
= cp_parser_make_indirect_declarator
11499 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
11507 /* Parse an (optional) ctor-initializer.
11510 : mem-initializer-list
11512 Returns TRUE iff the ctor-initializer was actually present. */
11515 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
11517 /* If the next token is not a `:', then there is no
11518 ctor-initializer. */
11519 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
11521 /* Do default initialization of any bases and members. */
11522 if (DECL_CONSTRUCTOR_P (current_function_decl
))
11523 finish_mem_initializers (NULL_TREE
);
11528 /* Consume the `:' token. */
11529 cp_lexer_consume_token (parser
->lexer
);
11530 /* And the mem-initializer-list. */
11531 cp_parser_mem_initializer_list (parser
);
11536 /* Parse a mem-initializer-list.
11538 mem-initializer-list:
11539 mem-initializer ... [opt]
11540 mem-initializer ... [opt] , mem-initializer-list */
11543 cp_parser_mem_initializer_list (cp_parser
* parser
)
11545 tree mem_initializer_list
= NULL_TREE
;
11546 tree target_ctor
= error_mark_node
;
11547 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11549 /* Let the semantic analysis code know that we are starting the
11550 mem-initializer-list. */
11551 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
11552 error_at (token
->location
,
11553 "only constructors take member initializers");
11555 /* Loop through the list. */
11558 tree mem_initializer
;
11560 token
= cp_lexer_peek_token (parser
->lexer
);
11561 /* Parse the mem-initializer. */
11562 mem_initializer
= cp_parser_mem_initializer (parser
);
11563 /* If the next token is a `...', we're expanding member initializers. */
11564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11566 /* Consume the `...'. */
11567 cp_lexer_consume_token (parser
->lexer
);
11569 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11570 can be expanded but members cannot. */
11571 if (mem_initializer
!= error_mark_node
11572 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
11574 error_at (token
->location
,
11575 "cannot expand initializer for member %<%D%>",
11576 TREE_PURPOSE (mem_initializer
));
11577 mem_initializer
= error_mark_node
;
11580 /* Construct the pack expansion type. */
11581 if (mem_initializer
!= error_mark_node
)
11582 mem_initializer
= make_pack_expansion (mem_initializer
);
11584 if (target_ctor
!= error_mark_node
11585 && mem_initializer
!= error_mark_node
)
11587 error ("mem-initializer for %qD follows constructor delegation",
11588 TREE_PURPOSE (mem_initializer
));
11589 mem_initializer
= error_mark_node
;
11591 /* Look for a target constructor. */
11592 if (mem_initializer
!= error_mark_node
11593 && TYPE_P (TREE_PURPOSE (mem_initializer
))
11594 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
11596 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
11597 if (mem_initializer_list
)
11599 error ("constructor delegation follows mem-initializer for %qD",
11600 TREE_PURPOSE (mem_initializer_list
));
11601 mem_initializer
= error_mark_node
;
11603 target_ctor
= mem_initializer
;
11605 /* Add it to the list, unless it was erroneous. */
11606 if (mem_initializer
!= error_mark_node
)
11608 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
11609 mem_initializer_list
= mem_initializer
;
11611 /* If the next token is not a `,', we're done. */
11612 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11614 /* Consume the `,' token. */
11615 cp_lexer_consume_token (parser
->lexer
);
11618 /* Perform semantic analysis. */
11619 if (DECL_CONSTRUCTOR_P (current_function_decl
))
11620 finish_mem_initializers (mem_initializer_list
);
11623 /* Parse a mem-initializer.
11626 mem-initializer-id ( expression-list [opt] )
11627 mem-initializer-id braced-init-list
11632 ( expression-list [opt] )
11634 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11635 class) or FIELD_DECL (for a non-static data member) to initialize;
11636 the TREE_VALUE is the expression-list. An empty initialization
11637 list is represented by void_list_node. */
11640 cp_parser_mem_initializer (cp_parser
* parser
)
11642 tree mem_initializer_id
;
11643 tree expression_list
;
11645 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11647 /* Find out what is being initialized. */
11648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
11650 permerror (token
->location
,
11651 "anachronistic old-style base class initializer");
11652 mem_initializer_id
= NULL_TREE
;
11656 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
11657 if (mem_initializer_id
== error_mark_node
)
11658 return mem_initializer_id
;
11660 member
= expand_member_init (mem_initializer_id
);
11661 if (member
&& !DECL_P (member
))
11662 in_base_initializer
= 1;
11664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11666 bool expr_non_constant_p
;
11667 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11668 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11669 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
11670 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
11675 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
11677 /*allow_expansion_p=*/true,
11678 /*non_constant_p=*/NULL
);
11680 return error_mark_node
;
11681 expression_list
= build_tree_list_vec (vec
);
11682 release_tree_vector (vec
);
11685 if (expression_list
== error_mark_node
)
11686 return error_mark_node
;
11687 if (!expression_list
)
11688 expression_list
= void_type_node
;
11690 in_base_initializer
= 0;
11692 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
11695 /* Parse a mem-initializer-id.
11697 mem-initializer-id:
11698 :: [opt] nested-name-specifier [opt] class-name
11701 Returns a TYPE indicating the class to be initializer for the first
11702 production. Returns an IDENTIFIER_NODE indicating the data member
11703 to be initialized for the second production. */
11706 cp_parser_mem_initializer_id (cp_parser
* parser
)
11708 bool global_scope_p
;
11709 bool nested_name_specifier_p
;
11710 bool template_p
= false;
11713 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11715 /* `typename' is not allowed in this context ([temp.res]). */
11716 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
11718 error_at (token
->location
,
11719 "keyword %<typename%> not allowed in this context (a qualified "
11720 "member initializer is implicitly a type)");
11721 cp_lexer_consume_token (parser
->lexer
);
11723 /* Look for the optional `::' operator. */
11725 = (cp_parser_global_scope_opt (parser
,
11726 /*current_scope_valid_p=*/false)
11728 /* Look for the optional nested-name-specifier. The simplest way to
11733 The keyword `typename' is not permitted in a base-specifier or
11734 mem-initializer; in these contexts a qualified name that
11735 depends on a template-parameter is implicitly assumed to be a
11738 is to assume that we have seen the `typename' keyword at this
11740 nested_name_specifier_p
11741 = (cp_parser_nested_name_specifier_opt (parser
,
11742 /*typename_keyword_p=*/true,
11743 /*check_dependency_p=*/true,
11745 /*is_declaration=*/true)
11747 if (nested_name_specifier_p
)
11748 template_p
= cp_parser_optional_template_keyword (parser
);
11749 /* If there is a `::' operator or a nested-name-specifier, then we
11750 are definitely looking for a class-name. */
11751 if (global_scope_p
|| nested_name_specifier_p
)
11752 return cp_parser_class_name (parser
,
11753 /*typename_keyword_p=*/true,
11754 /*template_keyword_p=*/template_p
,
11756 /*check_dependency_p=*/true,
11757 /*class_head_p=*/false,
11758 /*is_declaration=*/true);
11759 /* Otherwise, we could also be looking for an ordinary identifier. */
11760 cp_parser_parse_tentatively (parser
);
11761 /* Try a class-name. */
11762 id
= cp_parser_class_name (parser
,
11763 /*typename_keyword_p=*/true,
11764 /*template_keyword_p=*/false,
11766 /*check_dependency_p=*/true,
11767 /*class_head_p=*/false,
11768 /*is_declaration=*/true);
11769 /* If we found one, we're done. */
11770 if (cp_parser_parse_definitely (parser
))
11772 /* Otherwise, look for an ordinary identifier. */
11773 return cp_parser_identifier (parser
);
11776 /* Overloading [gram.over] */
11778 /* Parse an operator-function-id.
11780 operator-function-id:
11783 Returns an IDENTIFIER_NODE for the operator which is a
11784 human-readable spelling of the identifier, e.g., `operator +'. */
11787 cp_parser_operator_function_id (cp_parser
* parser
)
11789 /* Look for the `operator' keyword. */
11790 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
11791 return error_mark_node
;
11792 /* And then the name of the operator itself. */
11793 return cp_parser_operator (parser
);
11796 /* Return an identifier node for a user-defined literal operator.
11797 The suffix identifier is chained to the operator name identifier. */
11800 cp_literal_operator_id (const char* name
)
11803 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
11804 + strlen (name
) + 10);
11805 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
11806 identifier
= get_identifier (buffer
);
11807 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11812 /* Parse an operator.
11815 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11816 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11817 || ++ -- , ->* -> () []
11824 Returns an IDENTIFIER_NODE for the operator which is a
11825 human-readable spelling of the identifier, e.g., `operator +'. */
11828 cp_parser_operator (cp_parser
* parser
)
11830 tree id
= NULL_TREE
;
11833 /* Peek at the next token. */
11834 token
= cp_lexer_peek_token (parser
->lexer
);
11835 /* Figure out which operator we have. */
11836 switch (token
->type
)
11842 /* The keyword should be either `new' or `delete'. */
11843 if (token
->keyword
== RID_NEW
)
11845 else if (token
->keyword
== RID_DELETE
)
11850 /* Consume the `new' or `delete' token. */
11851 cp_lexer_consume_token (parser
->lexer
);
11853 /* Peek at the next token. */
11854 token
= cp_lexer_peek_token (parser
->lexer
);
11855 /* If it's a `[' token then this is the array variant of the
11857 if (token
->type
== CPP_OPEN_SQUARE
)
11859 /* Consume the `[' token. */
11860 cp_lexer_consume_token (parser
->lexer
);
11861 /* Look for the `]' token. */
11862 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
11863 id
= ansi_opname (op
== NEW_EXPR
11864 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
11866 /* Otherwise, we have the non-array variant. */
11868 id
= ansi_opname (op
);
11874 id
= ansi_opname (PLUS_EXPR
);
11878 id
= ansi_opname (MINUS_EXPR
);
11882 id
= ansi_opname (MULT_EXPR
);
11886 id
= ansi_opname (TRUNC_DIV_EXPR
);
11890 id
= ansi_opname (TRUNC_MOD_EXPR
);
11894 id
= ansi_opname (BIT_XOR_EXPR
);
11898 id
= ansi_opname (BIT_AND_EXPR
);
11902 id
= ansi_opname (BIT_IOR_EXPR
);
11906 id
= ansi_opname (BIT_NOT_EXPR
);
11910 id
= ansi_opname (TRUTH_NOT_EXPR
);
11914 id
= ansi_assopname (NOP_EXPR
);
11918 id
= ansi_opname (LT_EXPR
);
11922 id
= ansi_opname (GT_EXPR
);
11926 id
= ansi_assopname (PLUS_EXPR
);
11930 id
= ansi_assopname (MINUS_EXPR
);
11934 id
= ansi_assopname (MULT_EXPR
);
11938 id
= ansi_assopname (TRUNC_DIV_EXPR
);
11942 id
= ansi_assopname (TRUNC_MOD_EXPR
);
11946 id
= ansi_assopname (BIT_XOR_EXPR
);
11950 id
= ansi_assopname (BIT_AND_EXPR
);
11954 id
= ansi_assopname (BIT_IOR_EXPR
);
11958 id
= ansi_opname (LSHIFT_EXPR
);
11962 id
= ansi_opname (RSHIFT_EXPR
);
11965 case CPP_LSHIFT_EQ
:
11966 id
= ansi_assopname (LSHIFT_EXPR
);
11969 case CPP_RSHIFT_EQ
:
11970 id
= ansi_assopname (RSHIFT_EXPR
);
11974 id
= ansi_opname (EQ_EXPR
);
11978 id
= ansi_opname (NE_EXPR
);
11982 id
= ansi_opname (LE_EXPR
);
11985 case CPP_GREATER_EQ
:
11986 id
= ansi_opname (GE_EXPR
);
11990 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
11994 id
= ansi_opname (TRUTH_ORIF_EXPR
);
11997 case CPP_PLUS_PLUS
:
11998 id
= ansi_opname (POSTINCREMENT_EXPR
);
12001 case CPP_MINUS_MINUS
:
12002 id
= ansi_opname (PREDECREMENT_EXPR
);
12006 id
= ansi_opname (COMPOUND_EXPR
);
12009 case CPP_DEREF_STAR
:
12010 id
= ansi_opname (MEMBER_REF
);
12014 id
= ansi_opname (COMPONENT_REF
);
12017 case CPP_OPEN_PAREN
:
12018 /* Consume the `('. */
12019 cp_lexer_consume_token (parser
->lexer
);
12020 /* Look for the matching `)'. */
12021 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
12022 return ansi_opname (CALL_EXPR
);
12024 case CPP_OPEN_SQUARE
:
12025 /* Consume the `['. */
12026 cp_lexer_consume_token (parser
->lexer
);
12027 /* Look for the matching `]'. */
12028 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12029 return ansi_opname (ARRAY_REF
);
12032 if (cxx_dialect
== cxx98
)
12033 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
12034 if (TREE_STRING_LENGTH (token
->u
.value
) > 2)
12036 error ("expected empty string after %<operator%> keyword");
12037 return error_mark_node
;
12039 /* Consume the string. */
12040 cp_lexer_consume_token (parser
->lexer
);
12041 /* Look for the suffix identifier. */
12042 token
= cp_lexer_peek_token (parser
->lexer
);
12043 if (token
->type
== CPP_NAME
)
12045 id
= cp_parser_identifier (parser
);
12046 if (id
!= error_mark_node
)
12048 const char *name
= IDENTIFIER_POINTER (id
);
12049 return cp_literal_operator_id (name
);
12054 error ("expected suffix identifier");
12055 return error_mark_node
;
12058 case CPP_STRING_USERDEF
:
12059 error ("missing space between %<\"\"%> and suffix identifier");
12060 return error_mark_node
;
12063 /* Anything else is an error. */
12067 /* If we have selected an identifier, we need to consume the
12070 cp_lexer_consume_token (parser
->lexer
);
12071 /* Otherwise, no valid operator name was present. */
12074 cp_parser_error (parser
, "expected operator");
12075 id
= error_mark_node
;
12081 /* Parse a template-declaration.
12083 template-declaration:
12084 export [opt] template < template-parameter-list > declaration
12086 If MEMBER_P is TRUE, this template-declaration occurs within a
12089 The grammar rule given by the standard isn't correct. What
12090 is really meant is:
12092 template-declaration:
12093 export [opt] template-parameter-list-seq
12094 decl-specifier-seq [opt] init-declarator [opt] ;
12095 export [opt] template-parameter-list-seq
12096 function-definition
12098 template-parameter-list-seq:
12099 template-parameter-list-seq [opt]
12100 template < template-parameter-list > */
12103 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
12105 /* Check for `export'. */
12106 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
12108 /* Consume the `export' token. */
12109 cp_lexer_consume_token (parser
->lexer
);
12110 /* Warn that we do not support `export'. */
12111 warning (0, "keyword %<export%> not implemented, and will be ignored");
12114 cp_parser_template_declaration_after_export (parser
, member_p
);
12117 /* Parse a template-parameter-list.
12119 template-parameter-list:
12121 template-parameter-list , template-parameter
12123 Returns a TREE_LIST. Each node represents a template parameter.
12124 The nodes are connected via their TREE_CHAINs. */
12127 cp_parser_template_parameter_list (cp_parser
* parser
)
12129 tree parameter_list
= NULL_TREE
;
12131 begin_template_parm_list ();
12133 /* The loop below parses the template parms. We first need to know
12134 the total number of template parms to be able to compute proper
12135 canonical types of each dependent type. So after the loop, when
12136 we know the total number of template parms,
12137 end_template_parm_list computes the proper canonical types and
12138 fixes up the dependent types accordingly. */
12143 bool is_parameter_pack
;
12144 location_t parm_loc
;
12146 /* Parse the template-parameter. */
12147 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12148 parameter
= cp_parser_template_parameter (parser
,
12150 &is_parameter_pack
);
12151 /* Add it to the list. */
12152 if (parameter
!= error_mark_node
)
12153 parameter_list
= process_template_parm (parameter_list
,
12157 is_parameter_pack
);
12160 tree err_parm
= build_tree_list (parameter
, parameter
);
12161 parameter_list
= chainon (parameter_list
, err_parm
);
12164 /* If the next token is not a `,', we're done. */
12165 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12167 /* Otherwise, consume the `,' token. */
12168 cp_lexer_consume_token (parser
->lexer
);
12171 return end_template_parm_list (parameter_list
);
12174 /* Parse a template-parameter.
12176 template-parameter:
12178 parameter-declaration
12180 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12181 the parameter. The TREE_PURPOSE is the default value, if any.
12182 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12183 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12184 set to true iff this parameter is a parameter pack. */
12187 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
12188 bool *is_parameter_pack
)
12191 cp_parameter_declarator
*parameter_declarator
;
12192 cp_declarator
*id_declarator
;
12195 /* Assume it is a type parameter or a template parameter. */
12196 *is_non_type
= false;
12197 /* Assume it not a parameter pack. */
12198 *is_parameter_pack
= false;
12199 /* Peek at the next token. */
12200 token
= cp_lexer_peek_token (parser
->lexer
);
12201 /* If it is `class' or `template', we have a type-parameter. */
12202 if (token
->keyword
== RID_TEMPLATE
)
12203 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12204 /* If it is `class' or `typename' we do not know yet whether it is a
12205 type parameter or a non-type parameter. Consider:
12207 template <typename T, typename T::X X> ...
12211 template <class C, class D*> ...
12213 Here, the first parameter is a type parameter, and the second is
12214 a non-type parameter. We can tell by looking at the token after
12215 the identifier -- if it is a `,', `=', or `>' then we have a type
12217 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
12219 /* Peek at the token after `class' or `typename'. */
12220 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12221 /* If it's an ellipsis, we have a template type parameter
12223 if (token
->type
== CPP_ELLIPSIS
)
12224 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12225 /* If it's an identifier, skip it. */
12226 if (token
->type
== CPP_NAME
)
12227 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
12228 /* Now, see if the token looks like the end of a template
12230 if (token
->type
== CPP_COMMA
12231 || token
->type
== CPP_EQ
12232 || token
->type
== CPP_GREATER
)
12233 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12236 /* Otherwise, it is a non-type parameter.
12240 When parsing a default template-argument for a non-type
12241 template-parameter, the first non-nested `>' is taken as the end
12242 of the template parameter-list rather than a greater-than
12244 *is_non_type
= true;
12245 parameter_declarator
12246 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
12247 /*parenthesized_p=*/NULL
);
12249 /* If the parameter declaration is marked as a parameter pack, set
12250 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12251 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12253 if (parameter_declarator
12254 && parameter_declarator
->declarator
12255 && parameter_declarator
->declarator
->parameter_pack_p
)
12257 *is_parameter_pack
= true;
12258 parameter_declarator
->declarator
->parameter_pack_p
= false;
12261 if (parameter_declarator
12262 && parameter_declarator
->default_argument
)
12264 /* Can happen in some cases of erroneous input (c++/34892). */
12265 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12266 /* Consume the `...' for better error recovery. */
12267 cp_lexer_consume_token (parser
->lexer
);
12269 /* If the next token is an ellipsis, and we don't already have it
12270 marked as a parameter pack, then we have a parameter pack (that
12271 has no declarator). */
12272 else if (!*is_parameter_pack
12273 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12274 && (declarator_can_be_parameter_pack
12275 (parameter_declarator
->declarator
)))
12277 /* Consume the `...'. */
12278 cp_lexer_consume_token (parser
->lexer
);
12279 maybe_warn_variadic_templates ();
12281 *is_parameter_pack
= true;
12283 /* We might end up with a pack expansion as the type of the non-type
12284 template parameter, in which case this is a non-type template
12286 else if (parameter_declarator
12287 && parameter_declarator
->decl_specifiers
.type
12288 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
12290 *is_parameter_pack
= true;
12291 parameter_declarator
->decl_specifiers
.type
=
12292 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
12295 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12297 /* Parameter packs cannot have default arguments. However, a
12298 user may try to do so, so we'll parse them and give an
12299 appropriate diagnostic here. */
12301 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12303 /* Find the name of the parameter pack. */
12304 id_declarator
= parameter_declarator
->declarator
;
12305 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
12306 id_declarator
= id_declarator
->declarator
;
12308 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
12309 error_at (start_token
->location
,
12310 "template parameter pack %qD cannot have a default argument",
12311 id_declarator
->u
.id
.unqualified_name
);
12313 error_at (start_token
->location
,
12314 "template parameter pack cannot have a default argument");
12316 /* Parse the default argument, but throw away the result. */
12317 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
12320 parm
= grokdeclarator (parameter_declarator
->declarator
,
12321 ¶meter_declarator
->decl_specifiers
,
12322 TPARM
, /*initialized=*/0,
12323 /*attrlist=*/NULL
);
12324 if (parm
== error_mark_node
)
12325 return error_mark_node
;
12327 return build_tree_list (parameter_declarator
->default_argument
, parm
);
12330 /* Parse a type-parameter.
12333 class identifier [opt]
12334 class identifier [opt] = type-id
12335 typename identifier [opt]
12336 typename identifier [opt] = type-id
12337 template < template-parameter-list > class identifier [opt]
12338 template < template-parameter-list > class identifier [opt]
12341 GNU Extension (variadic templates):
12344 class ... identifier [opt]
12345 typename ... identifier [opt]
12347 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12348 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12349 the declaration of the parameter.
12351 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12354 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
12359 /* Look for a keyword to tell us what kind of parameter this is. */
12360 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
12362 return error_mark_node
;
12364 switch (token
->keyword
)
12370 tree default_argument
;
12372 /* If the next token is an ellipsis, we have a template
12374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12376 /* Consume the `...' token. */
12377 cp_lexer_consume_token (parser
->lexer
);
12378 maybe_warn_variadic_templates ();
12380 *is_parameter_pack
= true;
12383 /* If the next token is an identifier, then it names the
12385 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12386 identifier
= cp_parser_identifier (parser
);
12388 identifier
= NULL_TREE
;
12390 /* Create the parameter. */
12391 parameter
= finish_template_type_parm (class_type_node
, identifier
);
12393 /* If the next token is an `=', we have a default argument. */
12394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12396 /* Consume the `=' token. */
12397 cp_lexer_consume_token (parser
->lexer
);
12398 /* Parse the default-argument. */
12399 push_deferring_access_checks (dk_no_deferred
);
12400 default_argument
= cp_parser_type_id (parser
);
12402 /* Template parameter packs cannot have default
12404 if (*is_parameter_pack
)
12407 error_at (token
->location
,
12408 "template parameter pack %qD cannot have a "
12409 "default argument", identifier
);
12411 error_at (token
->location
,
12412 "template parameter packs cannot have "
12413 "default arguments");
12414 default_argument
= NULL_TREE
;
12416 pop_deferring_access_checks ();
12419 default_argument
= NULL_TREE
;
12421 /* Create the combined representation of the parameter and the
12422 default argument. */
12423 parameter
= build_tree_list (default_argument
, parameter
);
12430 tree default_argument
;
12432 /* Look for the `<'. */
12433 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
12434 /* Parse the template-parameter-list. */
12435 cp_parser_template_parameter_list (parser
);
12436 /* Look for the `>'. */
12437 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
12438 /* Look for the `class' keyword. */
12439 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
12440 /* If the next token is an ellipsis, we have a template
12442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12444 /* Consume the `...' token. */
12445 cp_lexer_consume_token (parser
->lexer
);
12446 maybe_warn_variadic_templates ();
12448 *is_parameter_pack
= true;
12450 /* If the next token is an `=', then there is a
12451 default-argument. If the next token is a `>', we are at
12452 the end of the parameter-list. If the next token is a `,',
12453 then we are at the end of this parameter. */
12454 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12455 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
12456 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12458 identifier
= cp_parser_identifier (parser
);
12459 /* Treat invalid names as if the parameter were nameless. */
12460 if (identifier
== error_mark_node
)
12461 identifier
= NULL_TREE
;
12464 identifier
= NULL_TREE
;
12466 /* Create the template parameter. */
12467 parameter
= finish_template_template_parm (class_type_node
,
12470 /* If the next token is an `=', then there is a
12471 default-argument. */
12472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12476 /* Consume the `='. */
12477 cp_lexer_consume_token (parser
->lexer
);
12478 /* Parse the id-expression. */
12479 push_deferring_access_checks (dk_no_deferred
);
12480 /* save token before parsing the id-expression, for error
12482 token
= cp_lexer_peek_token (parser
->lexer
);
12484 = cp_parser_id_expression (parser
,
12485 /*template_keyword_p=*/false,
12486 /*check_dependency_p=*/true,
12487 /*template_p=*/&is_template
,
12488 /*declarator_p=*/false,
12489 /*optional_p=*/false);
12490 if (TREE_CODE (default_argument
) == TYPE_DECL
)
12491 /* If the id-expression was a template-id that refers to
12492 a template-class, we already have the declaration here,
12493 so no further lookup is needed. */
12496 /* Look up the name. */
12498 = cp_parser_lookup_name (parser
, default_argument
,
12500 /*is_template=*/is_template
,
12501 /*is_namespace=*/false,
12502 /*check_dependency=*/true,
12503 /*ambiguous_decls=*/NULL
,
12505 /* See if the default argument is valid. */
12507 = check_template_template_default_arg (default_argument
);
12509 /* Template parameter packs cannot have default
12511 if (*is_parameter_pack
)
12514 error_at (token
->location
,
12515 "template parameter pack %qD cannot "
12516 "have a default argument",
12519 error_at (token
->location
, "template parameter packs cannot "
12520 "have default arguments");
12521 default_argument
= NULL_TREE
;
12523 pop_deferring_access_checks ();
12526 default_argument
= NULL_TREE
;
12528 /* Create the combined representation of the parameter and the
12529 default argument. */
12530 parameter
= build_tree_list (default_argument
, parameter
);
12535 gcc_unreachable ();
12542 /* Parse a template-id.
12545 template-name < template-argument-list [opt] >
12547 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12548 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12549 returned. Otherwise, if the template-name names a function, or set
12550 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12551 names a class, returns a TYPE_DECL for the specialization.
12553 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12554 uninstantiated templates. */
12557 cp_parser_template_id (cp_parser
*parser
,
12558 bool template_keyword_p
,
12559 bool check_dependency_p
,
12560 enum tag_types tag_type
,
12561 bool is_declaration
)
12567 cp_token_position start_of_id
= 0;
12568 deferred_access_check
*chk
;
12569 VEC (deferred_access_check
,gc
) *access_check
;
12570 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
12571 bool is_identifier
;
12573 /* If the next token corresponds to a template-id, there is no need
12575 next_token
= cp_lexer_peek_token (parser
->lexer
);
12576 if (next_token
->type
== CPP_TEMPLATE_ID
)
12578 struct tree_check
*check_value
;
12580 /* Get the stored value. */
12581 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
12582 /* Perform any access checks that were deferred. */
12583 access_check
= check_value
->checks
;
12586 FOR_EACH_VEC_ELT (deferred_access_check
, access_check
, i
, chk
)
12587 perform_or_defer_access_check (chk
->binfo
,
12590 tf_warning_or_error
);
12592 /* Return the stored value. */
12593 return check_value
->value
;
12596 /* Avoid performing name lookup if there is no possibility of
12597 finding a template-id. */
12598 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
12599 || (next_token
->type
== CPP_NAME
12600 && !cp_parser_nth_token_starts_template_argument_list_p
12603 cp_parser_error (parser
, "expected template-id");
12604 return error_mark_node
;
12607 /* Remember where the template-id starts. */
12608 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
12609 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
12611 push_deferring_access_checks (dk_deferred
);
12613 /* Parse the template-name. */
12614 is_identifier
= false;
12615 templ
= cp_parser_template_name (parser
, template_keyword_p
,
12616 check_dependency_p
,
12620 if (templ
== error_mark_node
|| is_identifier
)
12622 pop_deferring_access_checks ();
12626 /* If we find the sequence `[:' after a template-name, it's probably
12627 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12628 parse correctly the argument list. */
12629 next_token
= cp_lexer_peek_token (parser
->lexer
);
12630 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12631 if (next_token
->type
== CPP_OPEN_SQUARE
12632 && next_token
->flags
& DIGRAPH
12633 && next_token_2
->type
== CPP_COLON
12634 && !(next_token_2
->flags
& PREV_WHITE
))
12636 cp_parser_parse_tentatively (parser
);
12637 /* Change `:' into `::'. */
12638 next_token_2
->type
= CPP_SCOPE
;
12639 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12641 cp_lexer_consume_token (parser
->lexer
);
12643 /* Parse the arguments. */
12644 arguments
= cp_parser_enclosed_template_argument_list (parser
);
12645 if (!cp_parser_parse_definitely (parser
))
12647 /* If we couldn't parse an argument list, then we revert our changes
12648 and return simply an error. Maybe this is not a template-id
12650 next_token_2
->type
= CPP_COLON
;
12651 cp_parser_error (parser
, "expected %<<%>");
12652 pop_deferring_access_checks ();
12653 return error_mark_node
;
12655 /* Otherwise, emit an error about the invalid digraph, but continue
12656 parsing because we got our argument list. In C++11 do not emit
12657 any error, per 2.5/3. */
12658 if (cxx_dialect
< cxx0x
12659 && permerror (next_token
->location
,
12660 "%<<::%> cannot begin a template-argument list"))
12662 static bool hint
= false;
12663 inform (next_token
->location
,
12664 "%<<:%> is an alternate spelling for %<[%>."
12665 " Insert whitespace between %<<%> and %<::%>");
12666 if (!hint
&& !flag_permissive
)
12668 inform (next_token
->location
, "(if you use %<-fpermissive%> "
12669 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12670 "accept your code)");
12677 /* Look for the `<' that starts the template-argument-list. */
12678 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
12680 pop_deferring_access_checks ();
12681 return error_mark_node
;
12683 /* Parse the arguments. */
12684 arguments
= cp_parser_enclosed_template_argument_list (parser
);
12687 /* Build a representation of the specialization. */
12688 if (TREE_CODE (templ
) == IDENTIFIER_NODE
)
12689 template_id
= build_min_nt_loc (next_token
->location
,
12692 else if (DECL_TYPE_TEMPLATE_P (templ
)
12693 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
12695 bool entering_scope
;
12696 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12697 template (rather than some instantiation thereof) only if
12698 is not nested within some other construct. For example, in
12699 "template <typename T> void f(T) { A<T>::", A<T> is just an
12700 instantiation of A. */
12701 entering_scope
= (template_parm_scope_p ()
12702 && cp_lexer_next_token_is (parser
->lexer
,
12705 = finish_template_type (templ
, arguments
, entering_scope
);
12709 /* If it's not a class-template or a template-template, it should be
12710 a function-template. */
12711 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
12712 || TREE_CODE (templ
) == OVERLOAD
12713 || BASELINK_P (templ
)));
12715 template_id
= lookup_template_function (templ
, arguments
);
12718 /* If parsing tentatively, replace the sequence of tokens that makes
12719 up the template-id with a CPP_TEMPLATE_ID token. That way,
12720 should we re-parse the token stream, we will not have to repeat
12721 the effort required to do the parse, nor will we issue duplicate
12722 error messages about problems during instantiation of the
12726 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
12728 /* Reset the contents of the START_OF_ID token. */
12729 token
->type
= CPP_TEMPLATE_ID
;
12730 /* Retrieve any deferred checks. Do not pop this access checks yet
12731 so the memory will not be reclaimed during token replacing below. */
12732 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
12733 token
->u
.tree_check_value
->value
= template_id
;
12734 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
12735 token
->keyword
= RID_MAX
;
12737 /* Purge all subsequent tokens. */
12738 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
12740 /* ??? Can we actually assume that, if template_id ==
12741 error_mark_node, we will have issued a diagnostic to the
12742 user, as opposed to simply marking the tentative parse as
12744 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
12745 error_at (token
->location
, "parse error in template argument list");
12748 pop_deferring_access_checks ();
12749 return template_id
;
12752 /* Parse a template-name.
12757 The standard should actually say:
12761 operator-function-id
12763 A defect report has been filed about this issue.
12765 A conversion-function-id cannot be a template name because they cannot
12766 be part of a template-id. In fact, looking at this code:
12768 a.operator K<int>()
12770 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12771 It is impossible to call a templated conversion-function-id with an
12772 explicit argument list, since the only allowed template parameter is
12773 the type to which it is converting.
12775 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12776 `template' keyword, in a construction like:
12780 In that case `f' is taken to be a template-name, even though there
12781 is no way of knowing for sure.
12783 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12784 name refers to a set of overloaded functions, at least one of which
12785 is a template, or an IDENTIFIER_NODE with the name of the template,
12786 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12787 names are looked up inside uninstantiated templates. */
12790 cp_parser_template_name (cp_parser
* parser
,
12791 bool template_keyword_p
,
12792 bool check_dependency_p
,
12793 bool is_declaration
,
12794 enum tag_types tag_type
,
12795 bool *is_identifier
)
12800 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12802 /* If the next token is `operator', then we have either an
12803 operator-function-id or a conversion-function-id. */
12804 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
12806 /* We don't know whether we're looking at an
12807 operator-function-id or a conversion-function-id. */
12808 cp_parser_parse_tentatively (parser
);
12809 /* Try an operator-function-id. */
12810 identifier
= cp_parser_operator_function_id (parser
);
12811 /* If that didn't work, try a conversion-function-id. */
12812 if (!cp_parser_parse_definitely (parser
))
12814 cp_parser_error (parser
, "expected template-name");
12815 return error_mark_node
;
12818 /* Look for the identifier. */
12820 identifier
= cp_parser_identifier (parser
);
12822 /* If we didn't find an identifier, we don't have a template-id. */
12823 if (identifier
== error_mark_node
)
12824 return error_mark_node
;
12826 /* If the name immediately followed the `template' keyword, then it
12827 is a template-name. However, if the next token is not `<', then
12828 we do not treat it as a template-name, since it is not being used
12829 as part of a template-id. This enables us to handle constructs
12832 template <typename T> struct S { S(); };
12833 template <typename T> S<T>::S();
12835 correctly. We would treat `S' as a template -- if it were `S<T>'
12836 -- but we do not if there is no `<'. */
12838 if (processing_template_decl
12839 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
12841 /* In a declaration, in a dependent context, we pretend that the
12842 "template" keyword was present in order to improve error
12843 recovery. For example, given:
12845 template <typename T> void f(T::X<int>);
12847 we want to treat "X<int>" as a template-id. */
12849 && !template_keyword_p
12850 && parser
->scope
&& TYPE_P (parser
->scope
)
12851 && check_dependency_p
12852 && dependent_scope_p (parser
->scope
)
12853 /* Do not do this for dtors (or ctors), since they never
12854 need the template keyword before their name. */
12855 && !constructor_name_p (identifier
, parser
->scope
))
12857 cp_token_position start
= 0;
12859 /* Explain what went wrong. */
12860 error_at (token
->location
, "non-template %qD used as template",
12862 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
12863 parser
->scope
, identifier
);
12864 /* If parsing tentatively, find the location of the "<" token. */
12865 if (cp_parser_simulate_error (parser
))
12866 start
= cp_lexer_token_position (parser
->lexer
, true);
12867 /* Parse the template arguments so that we can issue error
12868 messages about them. */
12869 cp_lexer_consume_token (parser
->lexer
);
12870 cp_parser_enclosed_template_argument_list (parser
);
12871 /* Skip tokens until we find a good place from which to
12872 continue parsing. */
12873 cp_parser_skip_to_closing_parenthesis (parser
,
12874 /*recovering=*/true,
12876 /*consume_paren=*/false);
12877 /* If parsing tentatively, permanently remove the
12878 template argument list. That will prevent duplicate
12879 error messages from being issued about the missing
12880 "template" keyword. */
12882 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
12884 *is_identifier
= true;
12888 /* If the "template" keyword is present, then there is generally
12889 no point in doing name-lookup, so we just return IDENTIFIER.
12890 But, if the qualifying scope is non-dependent then we can
12891 (and must) do name-lookup normally. */
12892 if (template_keyword_p
12894 || (TYPE_P (parser
->scope
)
12895 && dependent_type_p (parser
->scope
))))
12899 /* Look up the name. */
12900 decl
= cp_parser_lookup_name (parser
, identifier
,
12902 /*is_template=*/true,
12903 /*is_namespace=*/false,
12904 check_dependency_p
,
12905 /*ambiguous_decls=*/NULL
,
12908 /* If DECL is a template, then the name was a template-name. */
12909 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
12913 tree fn
= NULL_TREE
;
12915 /* The standard does not explicitly indicate whether a name that
12916 names a set of overloaded declarations, some of which are
12917 templates, is a template-name. However, such a name should
12918 be a template-name; otherwise, there is no way to form a
12919 template-id for the overloaded templates. */
12920 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
12921 if (TREE_CODE (fns
) == OVERLOAD
)
12922 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
12923 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
12928 /* The name does not name a template. */
12929 cp_parser_error (parser
, "expected template-name");
12930 return error_mark_node
;
12934 /* If DECL is dependent, and refers to a function, then just return
12935 its name; we will look it up again during template instantiation. */
12936 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
12938 tree scope
= ovl_scope (decl
);
12939 if (TYPE_P (scope
) && dependent_type_p (scope
))
12946 /* Parse a template-argument-list.
12948 template-argument-list:
12949 template-argument ... [opt]
12950 template-argument-list , template-argument ... [opt]
12952 Returns a TREE_VEC containing the arguments. */
12955 cp_parser_template_argument_list (cp_parser
* parser
)
12957 tree fixed_args
[10];
12958 unsigned n_args
= 0;
12959 unsigned alloced
= 10;
12960 tree
*arg_ary
= fixed_args
;
12962 bool saved_in_template_argument_list_p
;
12964 bool saved_non_ice_p
;
12966 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
12967 parser
->in_template_argument_list_p
= true;
12968 /* Even if the template-id appears in an integral
12969 constant-expression, the contents of the argument list do
12971 saved_ice_p
= parser
->integral_constant_expression_p
;
12972 parser
->integral_constant_expression_p
= false;
12973 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
12974 parser
->non_integral_constant_expression_p
= false;
12976 /* Parse the arguments. */
12982 /* Consume the comma. */
12983 cp_lexer_consume_token (parser
->lexer
);
12985 /* Parse the template-argument. */
12986 argument
= cp_parser_template_argument (parser
);
12988 /* If the next token is an ellipsis, we're expanding a template
12990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12992 if (argument
== error_mark_node
)
12994 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12995 error_at (token
->location
,
12996 "expected parameter pack before %<...%>");
12998 /* Consume the `...' token. */
12999 cp_lexer_consume_token (parser
->lexer
);
13001 /* Make the argument into a TYPE_PACK_EXPANSION or
13002 EXPR_PACK_EXPANSION. */
13003 argument
= make_pack_expansion (argument
);
13006 if (n_args
== alloced
)
13010 if (arg_ary
== fixed_args
)
13012 arg_ary
= XNEWVEC (tree
, alloced
);
13013 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
13016 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
13018 arg_ary
[n_args
++] = argument
;
13020 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
13022 vec
= make_tree_vec (n_args
);
13025 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
13027 if (arg_ary
!= fixed_args
)
13029 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
13030 parser
->integral_constant_expression_p
= saved_ice_p
;
13031 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
13032 #ifdef ENABLE_CHECKING
13033 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
13038 /* Parse a template-argument.
13041 assignment-expression
13045 The representation is that of an assignment-expression, type-id, or
13046 id-expression -- except that the qualified id-expression is
13047 evaluated, so that the value returned is either a DECL or an
13050 Although the standard says "assignment-expression", it forbids
13051 throw-expressions or assignments in the template argument.
13052 Therefore, we use "conditional-expression" instead. */
13055 cp_parser_template_argument (cp_parser
* parser
)
13060 bool maybe_type_id
= false;
13061 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
13062 location_t loc
= 0;
13065 /* There's really no way to know what we're looking at, so we just
13066 try each alternative in order.
13070 In a template-argument, an ambiguity between a type-id and an
13071 expression is resolved to a type-id, regardless of the form of
13072 the corresponding template-parameter.
13074 Therefore, we try a type-id first. */
13075 cp_parser_parse_tentatively (parser
);
13076 argument
= cp_parser_template_type_arg (parser
);
13077 /* If there was no error parsing the type-id but the next token is a
13078 '>>', our behavior depends on which dialect of C++ we're
13079 parsing. In C++98, we probably found a typo for '> >'. But there
13080 are type-id which are also valid expressions. For instance:
13082 struct X { int operator >> (int); };
13083 template <int V> struct Foo {};
13086 Here 'X()' is a valid type-id of a function type, but the user just
13087 wanted to write the expression "X() >> 5". Thus, we remember that we
13088 found a valid type-id, but we still try to parse the argument as an
13089 expression to see what happens.
13091 In C++0x, the '>>' will be considered two separate '>'
13093 if (!cp_parser_error_occurred (parser
)
13094 && cxx_dialect
== cxx98
13095 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
13097 maybe_type_id
= true;
13098 cp_parser_abort_tentative_parse (parser
);
13102 /* If the next token isn't a `,' or a `>', then this argument wasn't
13103 really finished. This means that the argument is not a valid
13105 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13106 cp_parser_error (parser
, "expected template-argument");
13107 /* If that worked, we're done. */
13108 if (cp_parser_parse_definitely (parser
))
13111 /* We're still not sure what the argument will be. */
13112 cp_parser_parse_tentatively (parser
);
13113 /* Try a template. */
13114 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
13115 argument
= cp_parser_id_expression (parser
,
13116 /*template_keyword_p=*/false,
13117 /*check_dependency_p=*/true,
13119 /*declarator_p=*/false,
13120 /*optional_p=*/false);
13121 /* If the next token isn't a `,' or a `>', then this argument wasn't
13122 really finished. */
13123 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13124 cp_parser_error (parser
, "expected template-argument");
13125 if (!cp_parser_error_occurred (parser
))
13127 /* Figure out what is being referred to. If the id-expression
13128 was for a class template specialization, then we will have a
13129 TYPE_DECL at this point. There is no need to do name lookup
13130 at this point in that case. */
13131 if (TREE_CODE (argument
) != TYPE_DECL
)
13132 argument
= cp_parser_lookup_name (parser
, argument
,
13134 /*is_template=*/template_p
,
13135 /*is_namespace=*/false,
13136 /*check_dependency=*/true,
13137 /*ambiguous_decls=*/NULL
,
13138 argument_start_token
->location
);
13139 if (TREE_CODE (argument
) != TEMPLATE_DECL
13140 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
13141 cp_parser_error (parser
, "expected template-name");
13143 if (cp_parser_parse_definitely (parser
))
13145 /* It must be a non-type argument. There permitted cases are given
13146 in [temp.arg.nontype]:
13148 -- an integral constant-expression of integral or enumeration
13151 -- the name of a non-type template-parameter; or
13153 -- the name of an object or function with external linkage...
13155 -- the address of an object or function with external linkage...
13157 -- a pointer to member... */
13158 /* Look for a non-type template parameter. */
13159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13161 cp_parser_parse_tentatively (parser
);
13162 argument
= cp_parser_primary_expression (parser
,
13163 /*address_p=*/false,
13165 /*template_arg_p=*/true,
13167 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
13168 || !cp_parser_next_token_ends_template_argument_p (parser
))
13169 cp_parser_simulate_error (parser
);
13170 if (cp_parser_parse_definitely (parser
))
13174 /* If the next token is "&", the argument must be the address of an
13175 object or function with external linkage. */
13176 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
13179 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13180 cp_lexer_consume_token (parser
->lexer
);
13182 /* See if we might have an id-expression. */
13183 token
= cp_lexer_peek_token (parser
->lexer
);
13184 if (token
->type
== CPP_NAME
13185 || token
->keyword
== RID_OPERATOR
13186 || token
->type
== CPP_SCOPE
13187 || token
->type
== CPP_TEMPLATE_ID
13188 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
13190 cp_parser_parse_tentatively (parser
);
13191 argument
= cp_parser_primary_expression (parser
,
13194 /*template_arg_p=*/true,
13196 if (cp_parser_error_occurred (parser
)
13197 || !cp_parser_next_token_ends_template_argument_p (parser
))
13198 cp_parser_abort_tentative_parse (parser
);
13203 if (TREE_CODE (argument
) == INDIRECT_REF
)
13205 gcc_assert (REFERENCE_REF_P (argument
));
13206 argument
= TREE_OPERAND (argument
, 0);
13209 /* If we're in a template, we represent a qualified-id referring
13210 to a static data member as a SCOPE_REF even if the scope isn't
13211 dependent so that we can check access control later. */
13213 if (TREE_CODE (probe
) == SCOPE_REF
)
13214 probe
= TREE_OPERAND (probe
, 1);
13215 if (TREE_CODE (probe
) == VAR_DECL
)
13217 /* A variable without external linkage might still be a
13218 valid constant-expression, so no error is issued here
13219 if the external-linkage check fails. */
13220 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
13221 cp_parser_simulate_error (parser
);
13223 else if (is_overloaded_fn (argument
))
13224 /* All overloaded functions are allowed; if the external
13225 linkage test does not pass, an error will be issued
13229 && (TREE_CODE (argument
) == OFFSET_REF
13230 || TREE_CODE (argument
) == SCOPE_REF
))
13231 /* A pointer-to-member. */
13233 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
13236 cp_parser_simulate_error (parser
);
13238 if (cp_parser_parse_definitely (parser
))
13241 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
13242 tf_warning_or_error
);
13247 /* If the argument started with "&", there are no other valid
13248 alternatives at this point. */
13251 cp_parser_error (parser
, "invalid non-type template argument");
13252 return error_mark_node
;
13255 /* If the argument wasn't successfully parsed as a type-id followed
13256 by '>>', the argument can only be a constant expression now.
13257 Otherwise, we try parsing the constant-expression tentatively,
13258 because the argument could really be a type-id. */
13260 cp_parser_parse_tentatively (parser
);
13261 argument
= cp_parser_constant_expression (parser
,
13262 /*allow_non_constant_p=*/false,
13263 /*non_constant_p=*/NULL
);
13264 argument
= fold_non_dependent_expr (argument
);
13265 if (!maybe_type_id
)
13267 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13268 cp_parser_error (parser
, "expected template-argument");
13269 if (cp_parser_parse_definitely (parser
))
13271 /* We did our best to parse the argument as a non type-id, but that
13272 was the only alternative that matched (albeit with a '>' after
13273 it). We can assume it's just a typo from the user, and a
13274 diagnostic will then be issued. */
13275 return cp_parser_template_type_arg (parser
);
13278 /* Parse an explicit-instantiation.
13280 explicit-instantiation:
13281 template declaration
13283 Although the standard says `declaration', what it really means is:
13285 explicit-instantiation:
13286 template decl-specifier-seq [opt] declarator [opt] ;
13288 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13289 supposed to be allowed. A defect report has been filed about this
13294 explicit-instantiation:
13295 storage-class-specifier template
13296 decl-specifier-seq [opt] declarator [opt] ;
13297 function-specifier template
13298 decl-specifier-seq [opt] declarator [opt] ; */
13301 cp_parser_explicit_instantiation (cp_parser
* parser
)
13303 int declares_class_or_enum
;
13304 cp_decl_specifier_seq decl_specifiers
;
13305 tree extension_specifier
= NULL_TREE
;
13307 timevar_push (TV_TEMPLATE_INST
);
13309 /* Look for an (optional) storage-class-specifier or
13310 function-specifier. */
13311 if (cp_parser_allow_gnu_extensions_p (parser
))
13313 extension_specifier
13314 = cp_parser_storage_class_specifier_opt (parser
);
13315 if (!extension_specifier
)
13316 extension_specifier
13317 = cp_parser_function_specifier_opt (parser
,
13318 /*decl_specs=*/NULL
);
13321 /* Look for the `template' keyword. */
13322 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
13323 /* Let the front end know that we are processing an explicit
13325 begin_explicit_instantiation ();
13326 /* [temp.explicit] says that we are supposed to ignore access
13327 control while processing explicit instantiation directives. */
13328 push_deferring_access_checks (dk_no_check
);
13329 /* Parse a decl-specifier-seq. */
13330 cp_parser_decl_specifier_seq (parser
,
13331 CP_PARSER_FLAGS_OPTIONAL
,
13333 &declares_class_or_enum
);
13334 /* If there was exactly one decl-specifier, and it declared a class,
13335 and there's no declarator, then we have an explicit type
13337 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
13341 type
= check_tag_decl (&decl_specifiers
,
13342 /*explicit_type_instantiation_p=*/true);
13343 /* Turn access control back on for names used during
13344 template instantiation. */
13345 pop_deferring_access_checks ();
13347 do_type_instantiation (type
, extension_specifier
,
13348 /*complain=*/tf_error
);
13352 cp_declarator
*declarator
;
13355 /* Parse the declarator. */
13357 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13358 /*ctor_dtor_or_conv_p=*/NULL
,
13359 /*parenthesized_p=*/NULL
,
13360 /*member_p=*/false);
13361 if (declares_class_or_enum
& 2)
13362 cp_parser_check_for_definition_in_return_type (declarator
,
13363 decl_specifiers
.type
,
13364 decl_specifiers
.locations
[ds_type_spec
]);
13365 if (declarator
!= cp_error_declarator
)
13367 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
13368 permerror (decl_specifiers
.locations
[ds_inline
],
13369 "explicit instantiation shall not use"
13370 " %<inline%> specifier");
13371 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
13372 permerror (decl_specifiers
.locations
[ds_constexpr
],
13373 "explicit instantiation shall not use"
13374 " %<constexpr%> specifier");
13376 decl
= grokdeclarator (declarator
, &decl_specifiers
,
13377 NORMAL
, 0, &decl_specifiers
.attributes
);
13378 /* Turn access control back on for names used during
13379 template instantiation. */
13380 pop_deferring_access_checks ();
13381 /* Do the explicit instantiation. */
13382 do_decl_instantiation (decl
, extension_specifier
);
13386 pop_deferring_access_checks ();
13387 /* Skip the body of the explicit instantiation. */
13388 cp_parser_skip_to_end_of_statement (parser
);
13391 /* We're done with the instantiation. */
13392 end_explicit_instantiation ();
13394 cp_parser_consume_semicolon_at_end_of_statement (parser
);
13396 timevar_pop (TV_TEMPLATE_INST
);
13399 /* Parse an explicit-specialization.
13401 explicit-specialization:
13402 template < > declaration
13404 Although the standard says `declaration', what it really means is:
13406 explicit-specialization:
13407 template <> decl-specifier [opt] init-declarator [opt] ;
13408 template <> function-definition
13409 template <> explicit-specialization
13410 template <> template-declaration */
13413 cp_parser_explicit_specialization (cp_parser
* parser
)
13415 bool need_lang_pop
;
13416 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13418 /* Look for the `template' keyword. */
13419 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
13420 /* Look for the `<'. */
13421 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13422 /* Look for the `>'. */
13423 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13424 /* We have processed another parameter list. */
13425 ++parser
->num_template_parameter_lists
;
13428 A template ... explicit specialization ... shall not have C
13430 if (current_lang_name
== lang_name_c
)
13432 error_at (token
->location
, "template specialization with C linkage");
13433 /* Give it C++ linkage to avoid confusing other parts of the
13435 push_lang_context (lang_name_cplusplus
);
13436 need_lang_pop
= true;
13439 need_lang_pop
= false;
13440 /* Let the front end know that we are beginning a specialization. */
13441 if (!begin_specialization ())
13443 end_specialization ();
13447 /* If the next keyword is `template', we need to figure out whether
13448 or not we're looking a template-declaration. */
13449 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13451 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13452 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
13453 cp_parser_template_declaration_after_export (parser
,
13454 /*member_p=*/false);
13456 cp_parser_explicit_specialization (parser
);
13459 /* Parse the dependent declaration. */
13460 cp_parser_single_declaration (parser
,
13462 /*member_p=*/false,
13463 /*explicit_specialization_p=*/true,
13464 /*friend_p=*/NULL
);
13465 /* We're done with the specialization. */
13466 end_specialization ();
13467 /* For the erroneous case of a template with C linkage, we pushed an
13468 implicit C++ linkage scope; exit that scope now. */
13470 pop_lang_context ();
13471 /* We're done with this parameter list. */
13472 --parser
->num_template_parameter_lists
;
13475 /* Parse a type-specifier.
13478 simple-type-specifier
13481 elaborated-type-specifier
13489 Returns a representation of the type-specifier. For a
13490 class-specifier, enum-specifier, or elaborated-type-specifier, a
13491 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13493 The parser flags FLAGS is used to control type-specifier parsing.
13495 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13496 in a decl-specifier-seq.
13498 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13499 class-specifier, enum-specifier, or elaborated-type-specifier, then
13500 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13501 if a type is declared; 2 if it is defined. Otherwise, it is set to
13504 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13505 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13506 is set to FALSE. */
13509 cp_parser_type_specifier (cp_parser
* parser
,
13510 cp_parser_flags flags
,
13511 cp_decl_specifier_seq
*decl_specs
,
13512 bool is_declaration
,
13513 int* declares_class_or_enum
,
13514 bool* is_cv_qualifier
)
13516 tree type_spec
= NULL_TREE
;
13519 cp_decl_spec ds
= ds_last
;
13521 /* Assume this type-specifier does not declare a new type. */
13522 if (declares_class_or_enum
)
13523 *declares_class_or_enum
= 0;
13524 /* And that it does not specify a cv-qualifier. */
13525 if (is_cv_qualifier
)
13526 *is_cv_qualifier
= false;
13527 /* Peek at the next token. */
13528 token
= cp_lexer_peek_token (parser
->lexer
);
13530 /* If we're looking at a keyword, we can use that to guide the
13531 production we choose. */
13532 keyword
= token
->keyword
;
13536 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
13537 goto elaborated_type_specifier
;
13539 /* Look for the enum-specifier. */
13540 type_spec
= cp_parser_enum_specifier (parser
);
13541 /* If that worked, we're done. */
13544 if (declares_class_or_enum
)
13545 *declares_class_or_enum
= 2;
13547 cp_parser_set_decl_spec_type (decl_specs
,
13550 /*type_definition_p=*/true);
13554 goto elaborated_type_specifier
;
13556 /* Any of these indicate either a class-specifier, or an
13557 elaborated-type-specifier. */
13561 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
13562 goto elaborated_type_specifier
;
13564 /* Parse tentatively so that we can back up if we don't find a
13565 class-specifier. */
13566 cp_parser_parse_tentatively (parser
);
13567 /* Look for the class-specifier. */
13568 type_spec
= cp_parser_class_specifier (parser
);
13569 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
13570 /* If that worked, we're done. */
13571 if (cp_parser_parse_definitely (parser
))
13573 if (declares_class_or_enum
)
13574 *declares_class_or_enum
= 2;
13576 cp_parser_set_decl_spec_type (decl_specs
,
13579 /*type_definition_p=*/true);
13583 /* Fall through. */
13584 elaborated_type_specifier
:
13585 /* We're declaring (not defining) a class or enum. */
13586 if (declares_class_or_enum
)
13587 *declares_class_or_enum
= 1;
13589 /* Fall through. */
13591 /* Look for an elaborated-type-specifier. */
13593 = (cp_parser_elaborated_type_specifier
13595 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
13598 cp_parser_set_decl_spec_type (decl_specs
,
13601 /*type_definition_p=*/false);
13606 if (is_cv_qualifier
)
13607 *is_cv_qualifier
= true;
13612 if (is_cv_qualifier
)
13613 *is_cv_qualifier
= true;
13618 if (is_cv_qualifier
)
13619 *is_cv_qualifier
= true;
13623 /* The `__complex__' keyword is a GNU extension. */
13631 /* Handle simple keywords. */
13636 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13637 decl_specs
->any_specifiers_p
= true;
13639 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13642 /* If we do not already have a type-specifier, assume we are looking
13643 at a simple-type-specifier. */
13644 type_spec
= cp_parser_simple_type_specifier (parser
,
13648 /* If we didn't find a type-specifier, and a type-specifier was not
13649 optional in this context, issue an error message. */
13650 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
13652 cp_parser_error (parser
, "expected type specifier");
13653 return error_mark_node
;
13659 /* Parse a simple-type-specifier.
13661 simple-type-specifier:
13662 :: [opt] nested-name-specifier [opt] type-name
13663 :: [opt] nested-name-specifier template template-id
13678 simple-type-specifier:
13680 decltype ( expression )
13683 __underlying_type ( type-id )
13687 simple-type-specifier:
13689 __typeof__ unary-expression
13690 __typeof__ ( type-id )
13692 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13693 appropriately updated. */
13696 cp_parser_simple_type_specifier (cp_parser
* parser
,
13697 cp_decl_specifier_seq
*decl_specs
,
13698 cp_parser_flags flags
)
13700 tree type
= NULL_TREE
;
13703 /* Peek at the next token. */
13704 token
= cp_lexer_peek_token (parser
->lexer
);
13706 /* If we're looking at a keyword, things are easy. */
13707 switch (token
->keyword
)
13711 decl_specs
->explicit_char_p
= true;
13712 type
= char_type_node
;
13715 type
= char16_type_node
;
13718 type
= char32_type_node
;
13721 type
= wchar_type_node
;
13724 type
= boolean_type_node
;
13727 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
13728 type
= short_integer_type_node
;
13732 decl_specs
->explicit_int_p
= true;
13733 type
= integer_type_node
;
13736 if (!int128_integer_type_node
)
13739 decl_specs
->explicit_int128_p
= true;
13740 type
= int128_integer_type_node
;
13744 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
13745 type
= long_integer_type_node
;
13748 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
13749 type
= integer_type_node
;
13752 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
13753 type
= unsigned_type_node
;
13756 type
= float_type_node
;
13759 type
= double_type_node
;
13762 type
= void_type_node
;
13766 maybe_warn_cpp0x (CPP0X_AUTO
);
13767 type
= make_auto ();
13771 /* Since DR 743, decltype can either be a simple-type-specifier by
13772 itself or begin a nested-name-specifier. Parsing it will replace
13773 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13774 handling below decide what to do. */
13775 cp_parser_decltype (parser
);
13776 cp_lexer_set_token_position (parser
->lexer
, token
);
13780 /* Consume the `typeof' token. */
13781 cp_lexer_consume_token (parser
->lexer
);
13782 /* Parse the operand to `typeof'. */
13783 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
13784 /* If it is not already a TYPE, take its type. */
13785 if (!TYPE_P (type
))
13786 type
= finish_typeof (type
);
13789 cp_parser_set_decl_spec_type (decl_specs
, type
,
13791 /*type_definition_p=*/false);
13795 case RID_UNDERLYING_TYPE
:
13796 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
13798 cp_parser_set_decl_spec_type (decl_specs
, type
,
13800 /*type_definition_p=*/false);
13805 case RID_DIRECT_BASES
:
13806 type
= cp_parser_trait_expr (parser
, token
->keyword
);
13808 cp_parser_set_decl_spec_type (decl_specs
, type
,
13810 /*type_definition_p=*/false);
13816 /* If token is an already-parsed decltype not followed by ::,
13817 it's a simple-type-specifier. */
13818 if (token
->type
== CPP_DECLTYPE
13819 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
13821 type
= token
->u
.value
;
13823 cp_parser_set_decl_spec_type (decl_specs
, type
,
13825 /*type_definition_p=*/false);
13826 cp_lexer_consume_token (parser
->lexer
);
13830 /* If the type-specifier was for a built-in type, we're done. */
13833 /* Record the type. */
13835 && (token
->keyword
!= RID_SIGNED
13836 && token
->keyword
!= RID_UNSIGNED
13837 && token
->keyword
!= RID_SHORT
13838 && token
->keyword
!= RID_LONG
))
13839 cp_parser_set_decl_spec_type (decl_specs
,
13842 /*type_definition_p=*/false);
13844 decl_specs
->any_specifiers_p
= true;
13846 /* Consume the token. */
13847 cp_lexer_consume_token (parser
->lexer
);
13849 /* There is no valid C++ program where a non-template type is
13850 followed by a "<". That usually indicates that the user thought
13851 that the type was a template. */
13852 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
13855 return TYPE_NAME (type
);
13858 /* The type-specifier must be a user-defined type. */
13859 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
13864 /* Don't gobble tokens or issue error messages if this is an
13865 optional type-specifier. */
13866 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
13867 cp_parser_parse_tentatively (parser
);
13869 /* Look for the optional `::' operator. */
13871 = (cp_parser_global_scope_opt (parser
,
13872 /*current_scope_valid_p=*/false)
13874 /* Look for the nested-name specifier. */
13876 = (cp_parser_nested_name_specifier_opt (parser
,
13877 /*typename_keyword_p=*/false,
13878 /*check_dependency_p=*/true,
13880 /*is_declaration=*/false)
13882 token
= cp_lexer_peek_token (parser
->lexer
);
13883 /* If we have seen a nested-name-specifier, and the next token
13884 is `template', then we are using the template-id production. */
13886 && cp_parser_optional_template_keyword (parser
))
13888 /* Look for the template-id. */
13889 type
= cp_parser_template_id (parser
,
13890 /*template_keyword_p=*/true,
13891 /*check_dependency_p=*/true,
13893 /*is_declaration=*/false);
13894 /* If the template-id did not name a type, we are out of
13896 if (TREE_CODE (type
) != TYPE_DECL
)
13898 cp_parser_error (parser
, "expected template-id for type");
13902 /* Otherwise, look for a type-name. */
13904 type
= cp_parser_type_name (parser
);
13905 /* Keep track of all name-lookups performed in class scopes. */
13909 && TREE_CODE (type
) == TYPE_DECL
13910 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
13911 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
13912 /* If it didn't work out, we don't have a TYPE. */
13913 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
13914 && !cp_parser_parse_definitely (parser
))
13916 if (type
&& decl_specs
)
13917 cp_parser_set_decl_spec_type (decl_specs
, type
,
13919 /*type_definition_p=*/false);
13922 /* If we didn't get a type-name, issue an error message. */
13923 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
13925 cp_parser_error (parser
, "expected type-name");
13926 return error_mark_node
;
13929 if (type
&& type
!= error_mark_node
)
13931 /* See if TYPE is an Objective-C type, and if so, parse and
13932 accept any protocol references following it. Do this before
13933 the cp_parser_check_for_invalid_template_id() call, because
13934 Objective-C types can be followed by '<...>' which would
13935 enclose protocol names rather than template arguments, and so
13936 everything is fine. */
13937 if (c_dialect_objc () && !parser
->scope
13938 && (objc_is_id (type
) || objc_is_class_name (type
)))
13940 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
13941 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
13943 /* Clobber the "unqualified" type previously entered into
13944 DECL_SPECS with the new, improved protocol-qualified version. */
13946 decl_specs
->type
= qual_type
;
13951 /* There is no valid C++ program where a non-template type is
13952 followed by a "<". That usually indicates that the user
13953 thought that the type was a template. */
13954 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
13962 /* Parse a type-name.
13968 simple-template-id [in c++0x]
13976 Returns a TYPE_DECL for the type. */
13979 cp_parser_type_name (cp_parser
* parser
)
13983 /* We can't know yet whether it is a class-name or not. */
13984 cp_parser_parse_tentatively (parser
);
13985 /* Try a class-name. */
13986 type_decl
= cp_parser_class_name (parser
,
13987 /*typename_keyword_p=*/false,
13988 /*template_keyword_p=*/false,
13990 /*check_dependency_p=*/true,
13991 /*class_head_p=*/false,
13992 /*is_declaration=*/false);
13993 /* If it's not a class-name, keep looking. */
13994 if (!cp_parser_parse_definitely (parser
))
13996 if (cxx_dialect
< cxx0x
)
13997 /* It must be a typedef-name or an enum-name. */
13998 return cp_parser_nonclass_name (parser
);
14000 cp_parser_parse_tentatively (parser
);
14001 /* It is either a simple-template-id representing an
14002 instantiation of an alias template... */
14003 type_decl
= cp_parser_template_id (parser
,
14004 /*template_keyword_p=*/false,
14005 /*check_dependency_p=*/false,
14007 /*is_declaration=*/false);
14008 /* Note that this must be an instantiation of an alias template
14009 because [temp.names]/6 says:
14011 A template-id that names an alias template specialization
14014 Whereas [temp.names]/7 says:
14016 A simple-template-id that names a class template
14017 specialization is a class-name. */
14018 if (type_decl
!= NULL_TREE
14019 && TREE_CODE (type_decl
) == TYPE_DECL
14020 && TYPE_DECL_ALIAS_P (type_decl
))
14021 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
14023 cp_parser_simulate_error (parser
);
14025 if (!cp_parser_parse_definitely (parser
))
14026 /* ... Or a typedef-name or an enum-name. */
14027 return cp_parser_nonclass_name (parser
);
14033 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14041 Returns a TYPE_DECL for the type. */
14044 cp_parser_nonclass_name (cp_parser
* parser
)
14049 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14050 identifier
= cp_parser_identifier (parser
);
14051 if (identifier
== error_mark_node
)
14052 return error_mark_node
;
14054 /* Look up the type-name. */
14055 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
14057 if (TREE_CODE (type_decl
) == USING_DECL
)
14059 if (!DECL_DEPENDENT_P (type_decl
))
14060 type_decl
= strip_using_decl (type_decl
);
14061 else if (USING_DECL_TYPENAME_P (type_decl
))
14063 /* We have found a type introduced by a using
14064 declaration at class scope that refers to a dependent
14067 using typename :: [opt] nested-name-specifier unqualified-id ;
14069 type_decl
= make_typename_type (TREE_TYPE (type_decl
),
14070 DECL_NAME (type_decl
),
14071 typename_type
, tf_error
);
14072 if (type_decl
!= error_mark_node
)
14073 type_decl
= TYPE_NAME (type_decl
);
14077 if (TREE_CODE (type_decl
) != TYPE_DECL
14078 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
14080 /* See if this is an Objective-C type. */
14081 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
14082 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
14084 type_decl
= TYPE_NAME (type
);
14087 /* Issue an error if we did not find a type-name. */
14088 if (TREE_CODE (type_decl
) != TYPE_DECL
14089 /* In Objective-C, we have the complication that class names are
14090 normally type names and start declarations (eg, the
14091 "NSObject" in "NSObject *object;"), but can be used in an
14092 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14093 is an expression. So, a classname followed by a dot is not a
14094 valid type-name. */
14095 || (objc_is_class_name (TREE_TYPE (type_decl
))
14096 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
14098 if (!cp_parser_simulate_error (parser
))
14099 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
14100 NLE_TYPE
, token
->location
);
14101 return error_mark_node
;
14103 /* Remember that the name was used in the definition of the
14104 current class so that we can check later to see if the
14105 meaning would have been different after the class was
14106 entirely defined. */
14107 else if (type_decl
!= error_mark_node
14109 maybe_note_name_used_in_class (identifier
, type_decl
);
14114 /* Parse an elaborated-type-specifier. Note that the grammar given
14115 here incorporates the resolution to DR68.
14117 elaborated-type-specifier:
14118 class-key :: [opt] nested-name-specifier [opt] identifier
14119 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14120 enum-key :: [opt] nested-name-specifier [opt] identifier
14121 typename :: [opt] nested-name-specifier identifier
14122 typename :: [opt] nested-name-specifier template [opt]
14127 elaborated-type-specifier:
14128 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14129 class-key attributes :: [opt] nested-name-specifier [opt]
14130 template [opt] template-id
14131 enum attributes :: [opt] nested-name-specifier [opt] identifier
14133 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14134 declared `friend'. If IS_DECLARATION is TRUE, then this
14135 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14136 something is being declared.
14138 Returns the TYPE specified. */
14141 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
14143 bool is_declaration
)
14145 enum tag_types tag_type
;
14147 tree type
= NULL_TREE
;
14148 tree attributes
= NULL_TREE
;
14150 cp_token
*token
= NULL
;
14152 /* See if we're looking at the `enum' keyword. */
14153 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
14155 /* Consume the `enum' token. */
14156 cp_lexer_consume_token (parser
->lexer
);
14157 /* Remember that it's an enumeration type. */
14158 tag_type
= enum_type
;
14159 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14160 enums) is used here. */
14161 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14162 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14164 pedwarn (input_location
, 0, "elaborated-type-specifier "
14165 "for a scoped enum must not use the %<%D%> keyword",
14166 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
14167 /* Consume the `struct' or `class' and parse it anyway. */
14168 cp_lexer_consume_token (parser
->lexer
);
14170 /* Parse the attributes. */
14171 attributes
= cp_parser_attributes_opt (parser
);
14173 /* Or, it might be `typename'. */
14174 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
14177 /* Consume the `typename' token. */
14178 cp_lexer_consume_token (parser
->lexer
);
14179 /* Remember that it's a `typename' type. */
14180 tag_type
= typename_type
;
14182 /* Otherwise it must be a class-key. */
14185 tag_type
= cp_parser_class_key (parser
);
14186 if (tag_type
== none_type
)
14187 return error_mark_node
;
14188 /* Parse the attributes. */
14189 attributes
= cp_parser_attributes_opt (parser
);
14192 /* Look for the `::' operator. */
14193 globalscope
= cp_parser_global_scope_opt (parser
,
14194 /*current_scope_valid_p=*/false);
14195 /* Look for the nested-name-specifier. */
14196 if (tag_type
== typename_type
&& !globalscope
)
14198 if (!cp_parser_nested_name_specifier (parser
,
14199 /*typename_keyword_p=*/true,
14200 /*check_dependency_p=*/true,
14203 return error_mark_node
;
14206 /* Even though `typename' is not present, the proposed resolution
14207 to Core Issue 180 says that in `class A<T>::B', `B' should be
14208 considered a type-name, even if `A<T>' is dependent. */
14209 cp_parser_nested_name_specifier_opt (parser
,
14210 /*typename_keyword_p=*/true,
14211 /*check_dependency_p=*/true,
14214 /* For everything but enumeration types, consider a template-id.
14215 For an enumeration type, consider only a plain identifier. */
14216 if (tag_type
!= enum_type
)
14218 bool template_p
= false;
14221 /* Allow the `template' keyword. */
14222 template_p
= cp_parser_optional_template_keyword (parser
);
14223 /* If we didn't see `template', we don't know if there's a
14224 template-id or not. */
14226 cp_parser_parse_tentatively (parser
);
14227 /* Parse the template-id. */
14228 token
= cp_lexer_peek_token (parser
->lexer
);
14229 decl
= cp_parser_template_id (parser
, template_p
,
14230 /*check_dependency_p=*/true,
14233 /* If we didn't find a template-id, look for an ordinary
14235 if (!template_p
&& !cp_parser_parse_definitely (parser
))
14237 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14238 in effect, then we must assume that, upon instantiation, the
14239 template will correspond to a class. */
14240 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
14241 && tag_type
== typename_type
)
14242 type
= make_typename_type (parser
->scope
, decl
,
14244 /*complain=*/tf_error
);
14245 /* If the `typename' keyword is in effect and DECL is not a type
14246 decl. Then type is non existant. */
14247 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
14250 type
= check_elaborated_type_specifier (tag_type
, decl
,
14251 /*allow_template_p=*/true);
14256 token
= cp_lexer_peek_token (parser
->lexer
);
14257 identifier
= cp_parser_identifier (parser
);
14259 if (identifier
== error_mark_node
)
14261 parser
->scope
= NULL_TREE
;
14262 return error_mark_node
;
14265 /* For a `typename', we needn't call xref_tag. */
14266 if (tag_type
== typename_type
14267 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
14268 return cp_parser_make_typename_type (parser
, parser
->scope
,
14271 /* Look up a qualified name in the usual way. */
14275 tree ambiguous_decls
;
14277 decl
= cp_parser_lookup_name (parser
, identifier
,
14279 /*is_template=*/false,
14280 /*is_namespace=*/false,
14281 /*check_dependency=*/true,
14285 /* If the lookup was ambiguous, an error will already have been
14287 if (ambiguous_decls
)
14288 return error_mark_node
;
14290 /* If we are parsing friend declaration, DECL may be a
14291 TEMPLATE_DECL tree node here. However, we need to check
14292 whether this TEMPLATE_DECL results in valid code. Consider
14293 the following example:
14296 template <class T> class C {};
14299 template <class T> friend class N::C; // #1, valid code
14301 template <class T> class Y {
14302 friend class N::C; // #2, invalid code
14305 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14306 name lookup of `N::C'. We see that friend declaration must
14307 be template for the code to be valid. Note that
14308 processing_template_decl does not work here since it is
14309 always 1 for the above two cases. */
14311 decl
= (cp_parser_maybe_treat_template_as_class
14312 (decl
, /*tag_name_p=*/is_friend
14313 && parser
->num_template_parameter_lists
));
14315 if (TREE_CODE (decl
) != TYPE_DECL
)
14317 cp_parser_diagnose_invalid_type_name (parser
,
14321 return error_mark_node
;
14324 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
14326 bool allow_template
= (parser
->num_template_parameter_lists
14327 || DECL_SELF_REFERENCE_P (decl
));
14328 type
= check_elaborated_type_specifier (tag_type
, decl
,
14331 if (type
== error_mark_node
)
14332 return error_mark_node
;
14335 /* Forward declarations of nested types, such as
14340 are invalid unless all components preceding the final '::'
14341 are complete. If all enclosing types are complete, these
14342 declarations become merely pointless.
14344 Invalid forward declarations of nested types are errors
14345 caught elsewhere in parsing. Those that are pointless arrive
14348 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14349 && !is_friend
&& !processing_explicit_instantiation
)
14350 warning (0, "declaration %qD does not declare anything", decl
);
14352 type
= TREE_TYPE (decl
);
14356 /* An elaborated-type-specifier sometimes introduces a new type and
14357 sometimes names an existing type. Normally, the rule is that it
14358 introduces a new type only if there is not an existing type of
14359 the same name already in scope. For example, given:
14362 void f() { struct S s; }
14364 the `struct S' in the body of `f' is the same `struct S' as in
14365 the global scope; the existing definition is used. However, if
14366 there were no global declaration, this would introduce a new
14367 local class named `S'.
14369 An exception to this rule applies to the following code:
14371 namespace N { struct S; }
14373 Here, the elaborated-type-specifier names a new type
14374 unconditionally; even if there is already an `S' in the
14375 containing scope this declaration names a new type.
14376 This exception only applies if the elaborated-type-specifier
14377 forms the complete declaration:
14381 A declaration consisting solely of `class-key identifier ;' is
14382 either a redeclaration of the name in the current scope or a
14383 forward declaration of the identifier as a class name. It
14384 introduces the name into the current scope.
14386 We are in this situation precisely when the next token is a `;'.
14388 An exception to the exception is that a `friend' declaration does
14389 *not* name a new type; i.e., given:
14391 struct S { friend struct T; };
14393 `T' is not a new type in the scope of `S'.
14395 Also, `new struct S' or `sizeof (struct S)' never results in the
14396 definition of a new type; a new type can only be declared in a
14397 declaration context. */
14403 /* Friends have special name lookup rules. */
14404 ts
= ts_within_enclosing_non_class
;
14405 else if (is_declaration
14406 && cp_lexer_next_token_is (parser
->lexer
,
14408 /* This is a `class-key identifier ;' */
14414 (parser
->num_template_parameter_lists
14415 && (cp_parser_next_token_starts_class_definition_p (parser
)
14416 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
14417 /* An unqualified name was used to reference this type, so
14418 there were no qualifying templates. */
14419 if (!cp_parser_check_template_parameters (parser
,
14420 /*num_templates=*/0,
14422 /*declarator=*/NULL
))
14423 return error_mark_node
;
14424 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
14428 if (type
== error_mark_node
)
14429 return error_mark_node
;
14431 /* Allow attributes on forward declarations of classes. */
14434 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14435 warning (OPT_Wattributes
,
14436 "attributes ignored on uninstantiated type");
14437 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
14438 && ! processing_explicit_instantiation
)
14439 warning (OPT_Wattributes
,
14440 "attributes ignored on template instantiation");
14441 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
14442 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
14444 warning (OPT_Wattributes
,
14445 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14448 if (tag_type
!= enum_type
)
14450 /* Indicate whether this class was declared as a `class' or as a
14452 if (TREE_CODE (type
) == RECORD_TYPE
)
14453 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
14454 cp_parser_check_class_key (tag_type
, type
);
14457 /* A "<" cannot follow an elaborated type specifier. If that
14458 happens, the user was probably trying to form a template-id. */
14459 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
14465 /* Parse an enum-specifier.
14468 enum-head { enumerator-list [opt] }
14469 enum-head { enumerator-list , } [C++0x]
14472 enum-key identifier [opt] enum-base [opt]
14473 enum-key nested-name-specifier identifier enum-base [opt]
14478 enum struct [C++0x]
14481 : type-specifier-seq
14483 opaque-enum-specifier:
14484 enum-key identifier enum-base [opt] ;
14487 enum-key attributes[opt] identifier [opt] enum-base [opt]
14488 { enumerator-list [opt] }attributes[opt]
14489 enum-key attributes[opt] identifier [opt] enum-base [opt]
14490 { enumerator-list, }attributes[opt] [C++0x]
14492 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14493 if the token stream isn't an enum-specifier after all. */
14496 cp_parser_enum_specifier (cp_parser
* parser
)
14499 tree type
= NULL_TREE
;
14501 tree nested_name_specifier
= NULL_TREE
;
14503 bool scoped_enum_p
= false;
14504 bool has_underlying_type
= false;
14505 bool nested_being_defined
= false;
14506 bool new_value_list
= false;
14507 bool is_new_type
= false;
14508 bool is_anonymous
= false;
14509 tree underlying_type
= NULL_TREE
;
14510 cp_token
*type_start_token
= NULL
;
14511 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
14513 parser
->colon_corrects_to_scope_p
= false;
14515 /* Parse tentatively so that we can back up if we don't find a
14517 cp_parser_parse_tentatively (parser
);
14519 /* Caller guarantees that the current token is 'enum', an identifier
14520 possibly follows, and the token after that is an opening brace.
14521 If we don't have an identifier, fabricate an anonymous name for
14522 the enumeration being defined. */
14523 cp_lexer_consume_token (parser
->lexer
);
14525 /* Parse the "class" or "struct", which indicates a scoped
14526 enumeration type in C++0x. */
14527 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14528 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14530 if (cxx_dialect
< cxx0x
)
14531 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
14533 /* Consume the `struct' or `class' token. */
14534 cp_lexer_consume_token (parser
->lexer
);
14536 scoped_enum_p
= true;
14539 attributes
= cp_parser_attributes_opt (parser
);
14541 /* Clear the qualification. */
14542 parser
->scope
= NULL_TREE
;
14543 parser
->qualifying_scope
= NULL_TREE
;
14544 parser
->object_scope
= NULL_TREE
;
14546 /* Figure out in what scope the declaration is being placed. */
14547 prev_scope
= current_scope ();
14549 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
14551 push_deferring_access_checks (dk_no_check
);
14552 nested_name_specifier
14553 = cp_parser_nested_name_specifier_opt (parser
,
14554 /*typename_keyword_p=*/true,
14555 /*check_dependency_p=*/false,
14557 /*is_declaration=*/false);
14559 if (nested_name_specifier
)
14563 identifier
= cp_parser_identifier (parser
);
14564 name
= cp_parser_lookup_name (parser
, identifier
,
14566 /*is_template=*/false,
14567 /*is_namespace=*/false,
14568 /*check_dependency=*/true,
14569 /*ambiguous_decls=*/NULL
,
14573 type
= TREE_TYPE (name
);
14574 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14576 /* Are template enums allowed in ISO? */
14577 if (template_parm_scope_p ())
14578 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
14579 "%qD is an enumeration template", name
);
14580 /* ignore a typename reference, for it will be solved by name
14586 error_at (type_start_token
->location
,
14587 "%qD is not an enumerator-name", identifier
);
14591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14592 identifier
= cp_parser_identifier (parser
);
14595 identifier
= make_anon_name ();
14596 is_anonymous
= true;
14599 pop_deferring_access_checks ();
14601 /* Check for the `:' that denotes a specified underlying type in C++0x.
14602 Note that a ':' could also indicate a bitfield width, however. */
14603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14605 cp_decl_specifier_seq type_specifiers
;
14607 /* Consume the `:'. */
14608 cp_lexer_consume_token (parser
->lexer
);
14610 /* Parse the type-specifier-seq. */
14611 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14612 /*is_trailing_return=*/false,
14615 /* At this point this is surely not elaborated type specifier. */
14616 if (!cp_parser_parse_definitely (parser
))
14619 if (cxx_dialect
< cxx0x
)
14620 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
14622 has_underlying_type
= true;
14624 /* If that didn't work, stop. */
14625 if (type_specifiers
.type
!= error_mark_node
)
14627 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
14628 /*initialized=*/0, NULL
);
14629 if (underlying_type
== error_mark_node
)
14630 underlying_type
= NULL_TREE
;
14634 /* Look for the `{' but don't consume it yet. */
14635 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14637 if (cxx_dialect
< cxx0x
|| (!scoped_enum_p
&& !underlying_type
))
14639 cp_parser_error (parser
, "expected %<{%>");
14640 if (has_underlying_type
)
14646 /* An opaque-enum-specifier must have a ';' here. */
14647 if ((scoped_enum_p
|| underlying_type
)
14648 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14650 cp_parser_error (parser
, "expected %<;%> or %<{%>");
14651 if (has_underlying_type
)
14659 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
14662 if (nested_name_specifier
)
14664 if (CLASS_TYPE_P (nested_name_specifier
))
14666 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
14667 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
14668 push_scope (nested_name_specifier
);
14670 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
14672 push_nested_namespace (nested_name_specifier
);
14676 /* Issue an error message if type-definitions are forbidden here. */
14677 if (!cp_parser_check_type_definition (parser
))
14678 type
= error_mark_node
;
14680 /* Create the new type. We do this before consuming the opening
14681 brace so the enum will be recorded as being on the line of its
14682 tag (or the 'enum' keyword, if there is no tag). */
14683 type
= start_enum (identifier
, type
, underlying_type
,
14684 scoped_enum_p
, &is_new_type
);
14686 /* If the next token is not '{' it is an opaque-enum-specifier or an
14687 elaborated-type-specifier. */
14688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14690 timevar_push (TV_PARSE_ENUM
);
14691 if (nested_name_specifier
)
14693 /* The following catches invalid code such as:
14694 enum class S<int>::E { A, B, C }; */
14695 if (!processing_specialization
14696 && CLASS_TYPE_P (nested_name_specifier
)
14697 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
14698 error_at (type_start_token
->location
, "cannot add an enumerator "
14699 "list to a template instantiation");
14701 /* If that scope does not contain the scope in which the
14702 class was originally declared, the program is invalid. */
14703 if (prev_scope
&& !is_ancestor (prev_scope
, nested_name_specifier
))
14705 if (at_namespace_scope_p ())
14706 error_at (type_start_token
->location
,
14707 "declaration of %qD in namespace %qD which does not "
14709 type
, prev_scope
, nested_name_specifier
);
14711 error_at (type_start_token
->location
,
14712 "declaration of %qD in %qD which does not enclose %qD",
14713 type
, prev_scope
, nested_name_specifier
);
14714 type
= error_mark_node
;
14719 begin_scope (sk_scoped_enum
, type
);
14721 /* Consume the opening brace. */
14722 cp_lexer_consume_token (parser
->lexer
);
14724 if (type
== error_mark_node
)
14725 ; /* Nothing to add */
14726 else if (OPAQUE_ENUM_P (type
)
14727 || (cxx_dialect
> cxx98
&& processing_specialization
))
14729 new_value_list
= true;
14730 SET_OPAQUE_ENUM_P (type
, false);
14731 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
14735 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
14736 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
14737 "previous definition here");
14738 type
= error_mark_node
;
14741 if (type
== error_mark_node
)
14742 cp_parser_skip_to_end_of_block_or_statement (parser
);
14743 /* If the next token is not '}', then there are some enumerators. */
14744 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
14745 cp_parser_enumerator_list (parser
, type
);
14747 /* Consume the final '}'. */
14748 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
14752 timevar_pop (TV_PARSE_ENUM
);
14756 /* If a ';' follows, then it is an opaque-enum-specifier
14757 and additional restrictions apply. */
14758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14761 error_at (type_start_token
->location
,
14762 "opaque-enum-specifier without name");
14763 else if (nested_name_specifier
)
14764 error_at (type_start_token
->location
,
14765 "opaque-enum-specifier must use a simple identifier");
14769 /* Look for trailing attributes to apply to this enumeration, and
14770 apply them if appropriate. */
14771 if (cp_parser_allow_gnu_extensions_p (parser
))
14773 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
14774 trailing_attr
= chainon (trailing_attr
, attributes
);
14775 cplus_decl_attributes (&type
,
14777 (int) ATTR_FLAG_TYPE_IN_PLACE
);
14780 /* Finish up the enumeration. */
14781 if (type
!= error_mark_node
)
14783 if (new_value_list
)
14784 finish_enum_value_list (type
);
14786 finish_enum (type
);
14789 if (nested_name_specifier
)
14791 if (CLASS_TYPE_P (nested_name_specifier
))
14793 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
14794 pop_scope (nested_name_specifier
);
14796 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
14798 pop_nested_namespace (nested_name_specifier
);
14802 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
14806 /* Parse an enumerator-list. The enumerators all have the indicated
14810 enumerator-definition
14811 enumerator-list , enumerator-definition */
14814 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
14818 /* Parse an enumerator-definition. */
14819 cp_parser_enumerator_definition (parser
, type
);
14821 /* If the next token is not a ',', we've reached the end of
14823 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14825 /* Otherwise, consume the `,' and keep going. */
14826 cp_lexer_consume_token (parser
->lexer
);
14827 /* If the next token is a `}', there is a trailing comma. */
14828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
14830 if (cxx_dialect
< cxx0x
&& !in_system_header
)
14831 pedwarn (input_location
, OPT_Wpedantic
,
14832 "comma at end of enumerator list");
14838 /* Parse an enumerator-definition. The enumerator has the indicated
14841 enumerator-definition:
14843 enumerator = constant-expression
14849 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
14855 /* Save the input location because we are interested in the location
14856 of the identifier and not the location of the explicit value. */
14857 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14859 /* Look for the identifier. */
14860 identifier
= cp_parser_identifier (parser
);
14861 if (identifier
== error_mark_node
)
14864 /* If the next token is an '=', then there is an explicit value. */
14865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14867 /* Consume the `=' token. */
14868 cp_lexer_consume_token (parser
->lexer
);
14869 /* Parse the value. */
14870 value
= cp_parser_constant_expression (parser
,
14871 /*allow_non_constant_p=*/false,
14877 /* If we are processing a template, make sure the initializer of the
14878 enumerator doesn't contain any bare template parameter pack. */
14879 if (check_for_bare_parameter_packs (value
))
14880 value
= error_mark_node
;
14882 /* integral_constant_value will pull out this expression, so make sure
14883 it's folded as appropriate. */
14884 value
= fold_non_dependent_expr (value
);
14886 /* Create the enumerator. */
14887 build_enumerator (identifier
, value
, type
, loc
);
14890 /* Parse a namespace-name.
14893 original-namespace-name
14896 Returns the NAMESPACE_DECL for the namespace. */
14899 cp_parser_namespace_name (cp_parser
* parser
)
14902 tree namespace_decl
;
14904 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14906 /* Get the name of the namespace. */
14907 identifier
= cp_parser_identifier (parser
);
14908 if (identifier
== error_mark_node
)
14909 return error_mark_node
;
14911 /* Look up the identifier in the currently active scope. Look only
14912 for namespaces, due to:
14914 [basic.lookup.udir]
14916 When looking up a namespace-name in a using-directive or alias
14917 definition, only namespace names are considered.
14921 [basic.lookup.qual]
14923 During the lookup of a name preceding the :: scope resolution
14924 operator, object, function, and enumerator names are ignored.
14926 (Note that cp_parser_qualifying_entity only calls this
14927 function if the token after the name is the scope resolution
14929 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
14931 /*is_template=*/false,
14932 /*is_namespace=*/true,
14933 /*check_dependency=*/true,
14934 /*ambiguous_decls=*/NULL
,
14936 /* If it's not a namespace, issue an error. */
14937 if (namespace_decl
== error_mark_node
14938 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
14940 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14941 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
14942 cp_parser_error (parser
, "expected namespace-name");
14943 namespace_decl
= error_mark_node
;
14946 return namespace_decl
;
14949 /* Parse a namespace-definition.
14951 namespace-definition:
14952 named-namespace-definition
14953 unnamed-namespace-definition
14955 named-namespace-definition:
14956 original-namespace-definition
14957 extension-namespace-definition
14959 original-namespace-definition:
14960 namespace identifier { namespace-body }
14962 extension-namespace-definition:
14963 namespace original-namespace-name { namespace-body }
14965 unnamed-namespace-definition:
14966 namespace { namespace-body } */
14969 cp_parser_namespace_definition (cp_parser
* parser
)
14971 tree identifier
, attribs
;
14972 bool has_visibility
;
14975 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
14977 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
14979 cp_lexer_consume_token (parser
->lexer
);
14984 /* Look for the `namespace' keyword. */
14985 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
14987 /* Get the name of the namespace. We do not attempt to distinguish
14988 between an original-namespace-definition and an
14989 extension-namespace-definition at this point. The semantic
14990 analysis routines are responsible for that. */
14991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14992 identifier
= cp_parser_identifier (parser
);
14994 identifier
= NULL_TREE
;
14996 /* Parse any specified attributes. */
14997 attribs
= cp_parser_attributes_opt (parser
);
14999 /* Look for the `{' to start the namespace. */
15000 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
15001 /* Start the namespace. */
15002 push_namespace (identifier
);
15004 /* "inline namespace" is equivalent to a stub namespace definition
15005 followed by a strong using directive. */
15008 tree name_space
= current_namespace
;
15009 /* Set up namespace association. */
15010 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
15011 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
15012 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
15013 /* Import the contents of the inline namespace. */
15015 do_using_directive (name_space
);
15016 push_namespace (identifier
);
15019 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
15021 /* Parse the body of the namespace. */
15022 cp_parser_namespace_body (parser
);
15024 if (has_visibility
)
15025 pop_visibility (1);
15027 /* Finish the namespace. */
15029 /* Look for the final `}'. */
15030 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15033 /* Parse a namespace-body.
15036 declaration-seq [opt] */
15039 cp_parser_namespace_body (cp_parser
* parser
)
15041 cp_parser_declaration_seq_opt (parser
);
15044 /* Parse a namespace-alias-definition.
15046 namespace-alias-definition:
15047 namespace identifier = qualified-namespace-specifier ; */
15050 cp_parser_namespace_alias_definition (cp_parser
* parser
)
15053 tree namespace_specifier
;
15055 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15057 /* Look for the `namespace' keyword. */
15058 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15059 /* Look for the identifier. */
15060 identifier
= cp_parser_identifier (parser
);
15061 if (identifier
== error_mark_node
)
15063 /* Look for the `=' token. */
15064 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
15065 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15067 error_at (token
->location
, "%<namespace%> definition is not allowed here");
15068 /* Skip the definition. */
15069 cp_lexer_consume_token (parser
->lexer
);
15070 if (cp_parser_skip_to_closing_brace (parser
))
15071 cp_lexer_consume_token (parser
->lexer
);
15074 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15075 /* Look for the qualified-namespace-specifier. */
15076 namespace_specifier
15077 = cp_parser_qualified_namespace_specifier (parser
);
15078 /* Look for the `;' token. */
15079 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15081 /* Register the alias in the symbol table. */
15082 do_namespace_alias (identifier
, namespace_specifier
);
15085 /* Parse a qualified-namespace-specifier.
15087 qualified-namespace-specifier:
15088 :: [opt] nested-name-specifier [opt] namespace-name
15090 Returns a NAMESPACE_DECL corresponding to the specified
15094 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
15096 /* Look for the optional `::'. */
15097 cp_parser_global_scope_opt (parser
,
15098 /*current_scope_valid_p=*/false);
15100 /* Look for the optional nested-name-specifier. */
15101 cp_parser_nested_name_specifier_opt (parser
,
15102 /*typename_keyword_p=*/false,
15103 /*check_dependency_p=*/true,
15105 /*is_declaration=*/true);
15107 return cp_parser_namespace_name (parser
);
15110 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15111 access declaration.
15114 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15115 using :: unqualified-id ;
15117 access-declaration:
15123 cp_parser_using_declaration (cp_parser
* parser
,
15124 bool access_declaration_p
)
15127 bool typename_p
= false;
15128 bool global_scope_p
;
15132 int oldcount
= errorcount
;
15133 cp_token
*diag_token
= NULL
;
15135 if (access_declaration_p
)
15137 diag_token
= cp_lexer_peek_token (parser
->lexer
);
15138 cp_parser_parse_tentatively (parser
);
15142 /* Look for the `using' keyword. */
15143 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15145 /* Peek at the next token. */
15146 token
= cp_lexer_peek_token (parser
->lexer
);
15147 /* See if it's `typename'. */
15148 if (token
->keyword
== RID_TYPENAME
)
15150 /* Remember that we've seen it. */
15152 /* Consume the `typename' token. */
15153 cp_lexer_consume_token (parser
->lexer
);
15157 /* Look for the optional global scope qualification. */
15159 = (cp_parser_global_scope_opt (parser
,
15160 /*current_scope_valid_p=*/false)
15163 /* If we saw `typename', or didn't see `::', then there must be a
15164 nested-name-specifier present. */
15165 if (typename_p
|| !global_scope_p
)
15166 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
15167 /*check_dependency_p=*/true,
15169 /*is_declaration=*/true);
15170 /* Otherwise, we could be in either of the two productions. In that
15171 case, treat the nested-name-specifier as optional. */
15173 qscope
= cp_parser_nested_name_specifier_opt (parser
,
15174 /*typename_keyword_p=*/false,
15175 /*check_dependency_p=*/true,
15177 /*is_declaration=*/true);
15179 qscope
= global_namespace
;
15181 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
15182 /* Something has already gone wrong; there's no need to parse
15183 further. Since an error has occurred, the return value of
15184 cp_parser_parse_definitely will be false, as required. */
15185 return cp_parser_parse_definitely (parser
);
15187 token
= cp_lexer_peek_token (parser
->lexer
);
15188 /* Parse the unqualified-id. */
15189 identifier
= cp_parser_unqualified_id (parser
,
15190 /*template_keyword_p=*/false,
15191 /*check_dependency_p=*/true,
15192 /*declarator_p=*/true,
15193 /*optional_p=*/false);
15195 if (access_declaration_p
)
15197 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15198 cp_parser_simulate_error (parser
);
15199 if (!cp_parser_parse_definitely (parser
))
15203 /* The function we call to handle a using-declaration is different
15204 depending on what scope we are in. */
15205 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
15207 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
15208 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
15209 /* [namespace.udecl]
15211 A using declaration shall not name a template-id. */
15212 error_at (token
->location
,
15213 "a template-id may not appear in a using-declaration");
15216 if (at_class_scope_p ())
15218 /* Create the USING_DECL. */
15219 decl
= do_class_using_decl (parser
->scope
, identifier
);
15221 if (decl
&& typename_p
)
15222 USING_DECL_TYPENAME_P (decl
) = 1;
15224 if (check_for_bare_parameter_packs (decl
))
15227 /* Add it to the list of members in this class. */
15228 finish_member_declaration (decl
);
15232 decl
= cp_parser_lookup_name_simple (parser
,
15235 if (decl
== error_mark_node
)
15236 cp_parser_name_lookup_error (parser
, identifier
,
15239 else if (check_for_bare_parameter_packs (decl
))
15241 else if (!at_namespace_scope_p ())
15242 do_local_using_decl (decl
, qscope
, identifier
);
15244 do_toplevel_using_decl (decl
, qscope
, identifier
);
15248 /* Look for the final `;'. */
15249 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15251 if (access_declaration_p
&& errorcount
== oldcount
)
15252 warning_at (diag_token
->location
, OPT_Wdeprecated
,
15253 "access declarations are deprecated "
15254 "in favour of using-declarations; "
15255 "suggestion: add the %<using%> keyword");
15260 /* Parse an alias-declaration.
15263 using identifier attribute-specifier-seq [opt] = type-id */
15266 cp_parser_alias_declaration (cp_parser
* parser
)
15268 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
15269 location_t id_location
;
15270 cp_declarator
*declarator
;
15271 cp_decl_specifier_seq decl_specs
;
15273 const char *saved_message
= NULL
;
15275 /* Look for the `using' keyword. */
15276 cp_token
*using_token
15277 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15278 if (using_token
== NULL
)
15279 return error_mark_node
;
15281 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
15282 id
= cp_parser_identifier (parser
);
15283 if (id
== error_mark_node
)
15284 return error_mark_node
;
15286 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
15287 attributes
= cp_parser_attributes_opt (parser
);
15288 if (attributes
== error_mark_node
)
15289 return error_mark_node
;
15291 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15293 if (cp_parser_error_occurred (parser
))
15294 return error_mark_node
;
15296 /* Now we are going to parse the type-id of the declaration. */
15301 "A type-specifier-seq shall not define a class or enumeration
15302 unless it appears in the type-id of an alias-declaration (7.1.3) that
15303 is not the declaration of a template-declaration."
15305 In other words, if we currently are in an alias template, the
15306 type-id should not define a type.
15308 So let's set parser->type_definition_forbidden_message in that
15309 case; cp_parser_check_type_definition (called by
15310 cp_parser_class_specifier) will then emit an error if a type is
15311 defined in the type-id. */
15312 if (parser
->num_template_parameter_lists
)
15314 saved_message
= parser
->type_definition_forbidden_message
;
15315 parser
->type_definition_forbidden_message
=
15316 G_("types may not be defined in alias template declarations");
15319 type
= cp_parser_type_id (parser
);
15321 /* Restore the error message if need be. */
15322 if (parser
->num_template_parameter_lists
)
15323 parser
->type_definition_forbidden_message
= saved_message
;
15325 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15327 if (cp_parser_error_occurred (parser
))
15328 return error_mark_node
;
15330 /* A typedef-name can also be introduced by an alias-declaration. The
15331 identifier following the using keyword becomes a typedef-name. It has
15332 the same semantics as if it were introduced by the typedef
15333 specifier. In particular, it does not define a new type and it shall
15334 not appear in the type-id. */
15336 clear_decl_specs (&decl_specs
);
15337 decl_specs
.type
= type
;
15338 if (attributes
!= NULL_TREE
)
15340 decl_specs
.attributes
= attributes
;
15341 set_and_check_decl_spec_loc (&decl_specs
,
15345 set_and_check_decl_spec_loc (&decl_specs
,
15348 set_and_check_decl_spec_loc (&decl_specs
,
15352 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
15353 declarator
->id_loc
= id_location
;
15355 member_p
= at_class_scope_p ();
15357 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
15358 NULL_TREE
, attributes
);
15360 decl
= start_decl (declarator
, &decl_specs
, 0,
15361 attributes
, NULL_TREE
, &pushed_scope
);
15362 if (decl
== error_mark_node
)
15365 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
15368 pop_scope (pushed_scope
);
15370 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15371 added into the symbol table; otherwise, return the TYPE_DECL. */
15372 if (DECL_LANG_SPECIFIC (decl
)
15373 && DECL_TEMPLATE_INFO (decl
)
15374 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
15376 decl
= DECL_TI_TEMPLATE (decl
);
15378 check_member_template (decl
);
15384 /* Parse a using-directive.
15387 using namespace :: [opt] nested-name-specifier [opt]
15388 namespace-name ; */
15391 cp_parser_using_directive (cp_parser
* parser
)
15393 tree namespace_decl
;
15396 /* Look for the `using' keyword. */
15397 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15398 /* And the `namespace' keyword. */
15399 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15400 /* Look for the optional `::' operator. */
15401 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
15402 /* And the optional nested-name-specifier. */
15403 cp_parser_nested_name_specifier_opt (parser
,
15404 /*typename_keyword_p=*/false,
15405 /*check_dependency_p=*/true,
15407 /*is_declaration=*/true);
15408 /* Get the namespace being used. */
15409 namespace_decl
= cp_parser_namespace_name (parser
);
15410 /* And any specified attributes. */
15411 attribs
= cp_parser_attributes_opt (parser
);
15412 /* Update the symbol table. */
15413 parse_using_directive (namespace_decl
, attribs
);
15414 /* Look for the final `;'. */
15415 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15418 /* Parse an asm-definition.
15421 asm ( string-literal ) ;
15426 asm volatile [opt] ( string-literal ) ;
15427 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15428 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15429 : asm-operand-list [opt] ) ;
15430 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15431 : asm-operand-list [opt]
15432 : asm-clobber-list [opt] ) ;
15433 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15434 : asm-clobber-list [opt]
15435 : asm-goto-list ) ; */
15438 cp_parser_asm_definition (cp_parser
* parser
)
15441 tree outputs
= NULL_TREE
;
15442 tree inputs
= NULL_TREE
;
15443 tree clobbers
= NULL_TREE
;
15444 tree labels
= NULL_TREE
;
15446 bool volatile_p
= false;
15447 bool extended_p
= false;
15448 bool invalid_inputs_p
= false;
15449 bool invalid_outputs_p
= false;
15450 bool goto_p
= false;
15451 required_token missing
= RT_NONE
;
15453 /* Look for the `asm' keyword. */
15454 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
15455 /* See if the next token is `volatile'. */
15456 if (cp_parser_allow_gnu_extensions_p (parser
)
15457 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
15459 /* Remember that we saw the `volatile' keyword. */
15461 /* Consume the token. */
15462 cp_lexer_consume_token (parser
->lexer
);
15464 if (cp_parser_allow_gnu_extensions_p (parser
)
15465 && parser
->in_function_body
15466 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
15468 /* Remember that we saw the `goto' keyword. */
15470 /* Consume the token. */
15471 cp_lexer_consume_token (parser
->lexer
);
15473 /* Look for the opening `('. */
15474 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
15476 /* Look for the string. */
15477 string
= cp_parser_string_literal (parser
, false, false);
15478 if (string
== error_mark_node
)
15480 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15481 /*consume_paren=*/true);
15485 /* If we're allowing GNU extensions, check for the extended assembly
15486 syntax. Unfortunately, the `:' tokens need not be separated by
15487 a space in C, and so, for compatibility, we tolerate that here
15488 too. Doing that means that we have to treat the `::' operator as
15490 if (cp_parser_allow_gnu_extensions_p (parser
)
15491 && parser
->in_function_body
15492 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
15493 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
15495 bool inputs_p
= false;
15496 bool clobbers_p
= false;
15497 bool labels_p
= false;
15499 /* The extended syntax was used. */
15502 /* Look for outputs. */
15503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15505 /* Consume the `:'. */
15506 cp_lexer_consume_token (parser
->lexer
);
15507 /* Parse the output-operands. */
15508 if (cp_lexer_next_token_is_not (parser
->lexer
,
15510 && cp_lexer_next_token_is_not (parser
->lexer
,
15512 && cp_lexer_next_token_is_not (parser
->lexer
,
15515 outputs
= cp_parser_asm_operand_list (parser
);
15517 if (outputs
== error_mark_node
)
15518 invalid_outputs_p
= true;
15520 /* If the next token is `::', there are no outputs, and the
15521 next token is the beginning of the inputs. */
15522 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15523 /* The inputs are coming next. */
15526 /* Look for inputs. */
15528 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15530 /* Consume the `:' or `::'. */
15531 cp_lexer_consume_token (parser
->lexer
);
15532 /* Parse the output-operands. */
15533 if (cp_lexer_next_token_is_not (parser
->lexer
,
15535 && cp_lexer_next_token_is_not (parser
->lexer
,
15537 && cp_lexer_next_token_is_not (parser
->lexer
,
15539 inputs
= cp_parser_asm_operand_list (parser
);
15541 if (inputs
== error_mark_node
)
15542 invalid_inputs_p
= true;
15544 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15545 /* The clobbers are coming next. */
15548 /* Look for clobbers. */
15550 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15553 /* Consume the `:' or `::'. */
15554 cp_lexer_consume_token (parser
->lexer
);
15555 /* Parse the clobbers. */
15556 if (cp_lexer_next_token_is_not (parser
->lexer
,
15558 && cp_lexer_next_token_is_not (parser
->lexer
,
15560 clobbers
= cp_parser_asm_clobber_list (parser
);
15563 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15564 /* The labels are coming next. */
15567 /* Look for labels. */
15569 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
15572 /* Consume the `:' or `::'. */
15573 cp_lexer_consume_token (parser
->lexer
);
15574 /* Parse the labels. */
15575 labels
= cp_parser_asm_label_list (parser
);
15578 if (goto_p
&& !labels_p
)
15579 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
15582 missing
= RT_COLON_SCOPE
;
15584 /* Look for the closing `)'. */
15585 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
15586 missing
? missing
: RT_CLOSE_PAREN
))
15587 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15588 /*consume_paren=*/true);
15589 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15591 if (!invalid_inputs_p
&& !invalid_outputs_p
)
15593 /* Create the ASM_EXPR. */
15594 if (parser
->in_function_body
)
15596 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
15597 inputs
, clobbers
, labels
);
15598 /* If the extended syntax was not used, mark the ASM_EXPR. */
15601 tree temp
= asm_stmt
;
15602 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
15603 temp
= TREE_OPERAND (temp
, 0);
15605 ASM_INPUT_P (temp
) = 1;
15609 add_asm_node (string
);
15613 /* Declarators [gram.dcl.decl] */
15615 /* Parse an init-declarator.
15618 declarator initializer [opt]
15623 declarator asm-specification [opt] attributes [opt] initializer [opt]
15625 function-definition:
15626 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15628 decl-specifier-seq [opt] declarator function-try-block
15632 function-definition:
15633 __extension__ function-definition
15637 function-definition:
15638 decl-specifier-seq [opt] declarator function-transaction-block
15640 The DECL_SPECIFIERS apply to this declarator. Returns a
15641 representation of the entity declared. If MEMBER_P is TRUE, then
15642 this declarator appears in a class scope. The new DECL created by
15643 this declarator is returned.
15645 The CHECKS are access checks that should be performed once we know
15646 what entity is being declared (and, therefore, what classes have
15649 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15650 for a function-definition here as well. If the declarator is a
15651 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15652 be TRUE upon return. By that point, the function-definition will
15653 have been completely parsed.
15655 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15658 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15659 parsed declaration if it is an uninitialized single declarator not followed
15660 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15661 if present, will not be consumed. If returned, this declarator will be
15662 created with SD_INITIALIZED but will not call cp_finish_decl. */
15665 cp_parser_init_declarator (cp_parser
* parser
,
15666 cp_decl_specifier_seq
*decl_specifiers
,
15667 VEC (deferred_access_check
,gc
)* checks
,
15668 bool function_definition_allowed_p
,
15670 int declares_class_or_enum
,
15671 bool* function_definition_p
,
15672 tree
* maybe_range_for_decl
)
15674 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
15675 *attributes_start_token
= NULL
;
15676 cp_declarator
*declarator
;
15677 tree prefix_attributes
;
15678 tree attributes
= NULL
;
15679 tree asm_specification
;
15681 tree decl
= NULL_TREE
;
15683 int is_initialized
;
15684 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15685 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15687 enum cpp_ttype initialization_kind
;
15688 bool is_direct_init
= false;
15689 bool is_non_constant_init
;
15690 int ctor_dtor_or_conv_p
;
15692 tree pushed_scope
= NULL_TREE
;
15693 bool range_for_decl_p
= false;
15695 /* Gather the attributes that were provided with the
15696 decl-specifiers. */
15697 prefix_attributes
= decl_specifiers
->attributes
;
15699 /* Assume that this is not the declarator for a function
15701 if (function_definition_p
)
15702 *function_definition_p
= false;
15704 /* Defer access checks while parsing the declarator; we cannot know
15705 what names are accessible until we know what is being
15707 resume_deferring_access_checks ();
15709 /* Parse the declarator. */
15710 token
= cp_lexer_peek_token (parser
->lexer
);
15712 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
15713 &ctor_dtor_or_conv_p
,
15714 /*parenthesized_p=*/NULL
,
15716 /* Gather up the deferred checks. */
15717 stop_deferring_access_checks ();
15719 /* If the DECLARATOR was erroneous, there's no need to go
15721 if (declarator
== cp_error_declarator
)
15722 return error_mark_node
;
15724 /* Check that the number of template-parameter-lists is OK. */
15725 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
15727 return error_mark_node
;
15729 if (declares_class_or_enum
& 2)
15730 cp_parser_check_for_definition_in_return_type (declarator
,
15731 decl_specifiers
->type
,
15732 decl_specifiers
->locations
[ds_type_spec
]);
15734 /* Figure out what scope the entity declared by the DECLARATOR is
15735 located in. `grokdeclarator' sometimes changes the scope, so
15736 we compute it now. */
15737 scope
= get_scope_of_declarator (declarator
);
15739 /* Perform any lookups in the declared type which were thought to be
15740 dependent, but are not in the scope of the declarator. */
15741 decl_specifiers
->type
15742 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
15744 /* If we're allowing GNU extensions, look for an
15745 asm-specification. */
15746 if (cp_parser_allow_gnu_extensions_p (parser
))
15748 /* Look for an asm-specification. */
15749 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
15750 asm_specification
= cp_parser_asm_specification_opt (parser
);
15753 asm_specification
= NULL_TREE
;
15755 /* Look for attributes. */
15756 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
15757 attributes
= cp_parser_attributes_opt (parser
);
15759 /* Peek at the next token. */
15760 token
= cp_lexer_peek_token (parser
->lexer
);
15761 /* Check to see if the token indicates the start of a
15762 function-definition. */
15763 if (function_declarator_p (declarator
)
15764 && cp_parser_token_starts_function_definition_p (token
))
15766 if (!function_definition_allowed_p
)
15768 /* If a function-definition should not appear here, issue an
15770 cp_parser_error (parser
,
15771 "a function-definition is not allowed here");
15772 return error_mark_node
;
15776 location_t func_brace_location
15777 = cp_lexer_peek_token (parser
->lexer
)->location
;
15779 /* Neither attributes nor an asm-specification are allowed
15780 on a function-definition. */
15781 if (asm_specification
)
15782 error_at (asm_spec_start_token
->location
,
15783 "an asm-specification is not allowed "
15784 "on a function-definition");
15786 error_at (attributes_start_token
->location
,
15787 "attributes are not allowed on a function-definition");
15788 /* This is a function-definition. */
15789 *function_definition_p
= true;
15791 /* Parse the function definition. */
15793 decl
= cp_parser_save_member_function_body (parser
,
15796 prefix_attributes
);
15799 = (cp_parser_function_definition_from_specifiers_and_declarator
15800 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
15802 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
15804 /* This is where the prologue starts... */
15805 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
15806 = func_brace_location
;
15815 Only in function declarations for constructors, destructors, and
15816 type conversions can the decl-specifier-seq be omitted.
15818 We explicitly postpone this check past the point where we handle
15819 function-definitions because we tolerate function-definitions
15820 that are missing their return types in some modes. */
15821 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
15823 cp_parser_error (parser
,
15824 "expected constructor, destructor, or type conversion");
15825 return error_mark_node
;
15828 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15829 if (token
->type
== CPP_EQ
15830 || token
->type
== CPP_OPEN_PAREN
15831 || token
->type
== CPP_OPEN_BRACE
)
15833 is_initialized
= SD_INITIALIZED
;
15834 initialization_kind
= token
->type
;
15835 if (maybe_range_for_decl
)
15836 *maybe_range_for_decl
= error_mark_node
;
15838 if (token
->type
== CPP_EQ
15839 && function_declarator_p (declarator
))
15841 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15842 if (t2
->keyword
== RID_DEFAULT
)
15843 is_initialized
= SD_DEFAULTED
;
15844 else if (t2
->keyword
== RID_DELETE
)
15845 is_initialized
= SD_DELETED
;
15850 /* If the init-declarator isn't initialized and isn't followed by a
15851 `,' or `;', it's not a valid init-declarator. */
15852 if (token
->type
!= CPP_COMMA
15853 && token
->type
!= CPP_SEMICOLON
)
15855 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
15856 range_for_decl_p
= true;
15859 cp_parser_error (parser
, "expected initializer");
15860 return error_mark_node
;
15863 is_initialized
= SD_UNINITIALIZED
;
15864 initialization_kind
= CPP_EOF
;
15867 /* Because start_decl has side-effects, we should only call it if we
15868 know we're going ahead. By this point, we know that we cannot
15869 possibly be looking at any other construct. */
15870 cp_parser_commit_to_tentative_parse (parser
);
15872 /* If the decl specifiers were bad, issue an error now that we're
15873 sure this was intended to be a declarator. Then continue
15874 declaring the variable(s), as int, to try to cut down on further
15876 if (decl_specifiers
->any_specifiers_p
15877 && decl_specifiers
->type
== error_mark_node
)
15879 cp_parser_error (parser
, "invalid type in declaration");
15880 decl_specifiers
->type
= integer_type_node
;
15883 /* Check to see whether or not this declaration is a friend. */
15884 friend_p
= cp_parser_friend_p (decl_specifiers
);
15886 /* Enter the newly declared entry in the symbol table. If we're
15887 processing a declaration in a class-specifier, we wait until
15888 after processing the initializer. */
15891 if (parser
->in_unbraced_linkage_specification_p
)
15892 decl_specifiers
->storage_class
= sc_extern
;
15893 decl
= start_decl (declarator
, decl_specifiers
,
15894 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
15895 attributes
, prefix_attributes
,
15897 /* Adjust location of decl if declarator->id_loc is more appropriate:
15898 set, and decl wasn't merged with another decl, in which case its
15899 location would be different from input_location, and more accurate. */
15901 && declarator
->id_loc
!= UNKNOWN_LOCATION
15902 && DECL_SOURCE_LOCATION (decl
) == input_location
)
15903 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
15906 /* Enter the SCOPE. That way unqualified names appearing in the
15907 initializer will be looked up in SCOPE. */
15908 pushed_scope
= push_scope (scope
);
15910 /* Perform deferred access control checks, now that we know in which
15911 SCOPE the declared entity resides. */
15912 if (!member_p
&& decl
)
15914 tree saved_current_function_decl
= NULL_TREE
;
15916 /* If the entity being declared is a function, pretend that we
15917 are in its scope. If it is a `friend', it may have access to
15918 things that would not otherwise be accessible. */
15919 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15921 saved_current_function_decl
= current_function_decl
;
15922 current_function_decl
= decl
;
15925 /* Perform access checks for template parameters. */
15926 cp_parser_perform_template_parameter_access_checks (checks
);
15928 /* Perform the access control checks for the declarator and the
15929 decl-specifiers. */
15930 perform_deferred_access_checks (tf_warning_or_error
);
15932 /* Restore the saved value. */
15933 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15934 current_function_decl
= saved_current_function_decl
;
15937 /* Parse the initializer. */
15938 initializer
= NULL_TREE
;
15939 is_direct_init
= false;
15940 is_non_constant_init
= true;
15941 if (is_initialized
)
15943 if (function_declarator_p (declarator
))
15945 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
15946 if (initialization_kind
== CPP_EQ
)
15947 initializer
= cp_parser_pure_specifier (parser
);
15950 /* If the declaration was erroneous, we don't really
15951 know what the user intended, so just silently
15952 consume the initializer. */
15953 if (decl
!= error_mark_node
)
15954 error_at (initializer_start_token
->location
,
15955 "initializer provided for function");
15956 cp_parser_skip_to_closing_parenthesis (parser
,
15957 /*recovering=*/true,
15958 /*or_comma=*/false,
15959 /*consume_paren=*/true);
15964 /* We want to record the extra mangling scope for in-class
15965 initializers of class members and initializers of static data
15966 member templates. The former involves deferring
15967 parsing of the initializer until end of class as with default
15968 arguments. So right here we only handle the latter. */
15969 if (!member_p
&& processing_template_decl
)
15970 start_lambda_scope (decl
);
15971 initializer
= cp_parser_initializer (parser
,
15973 &is_non_constant_init
);
15974 if (!member_p
&& processing_template_decl
)
15975 finish_lambda_scope ();
15976 if (initializer
== error_mark_node
)
15977 cp_parser_skip_to_end_of_statement (parser
);
15981 /* The old parser allows attributes to appear after a parenthesized
15982 initializer. Mark Mitchell proposed removing this functionality
15983 on the GCC mailing lists on 2002-08-13. This parser accepts the
15984 attributes -- but ignores them. */
15985 if (cp_parser_allow_gnu_extensions_p (parser
)
15986 && initialization_kind
== CPP_OPEN_PAREN
)
15987 if (cp_parser_attributes_opt (parser
))
15988 warning (OPT_Wattributes
,
15989 "attributes after parenthesized initializer ignored");
15991 /* For an in-class declaration, use `grokfield' to create the
15997 pop_scope (pushed_scope
);
15998 pushed_scope
= NULL_TREE
;
16000 decl
= grokfield (declarator
, decl_specifiers
,
16001 initializer
, !is_non_constant_init
,
16002 /*asmspec=*/NULL_TREE
,
16003 prefix_attributes
);
16004 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16005 cp_parser_save_default_args (parser
, decl
);
16008 /* Finish processing the declaration. But, skip member
16010 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
16012 cp_finish_decl (decl
,
16013 initializer
, !is_non_constant_init
,
16015 /* If the initializer is in parentheses, then this is
16016 a direct-initialization, which means that an
16017 `explicit' constructor is OK. Otherwise, an
16018 `explicit' constructor cannot be used. */
16019 ((is_direct_init
|| !is_initialized
)
16020 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
16022 else if ((cxx_dialect
!= cxx98
) && friend_p
16023 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
16024 /* Core issue #226 (C++0x only): A default template-argument
16025 shall not be specified in a friend class template
16027 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
16028 /*is_partial=*/false, /*is_friend_decl=*/1);
16030 if (!friend_p
&& pushed_scope
)
16031 pop_scope (pushed_scope
);
16036 /* Parse a declarator.
16040 ptr-operator declarator
16042 abstract-declarator:
16043 ptr-operator abstract-declarator [opt]
16044 direct-abstract-declarator
16049 attributes [opt] direct-declarator
16050 attributes [opt] ptr-operator declarator
16052 abstract-declarator:
16053 attributes [opt] ptr-operator abstract-declarator [opt]
16054 attributes [opt] direct-abstract-declarator
16056 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16057 detect constructor, destructor or conversion operators. It is set
16058 to -1 if the declarator is a name, and +1 if it is a
16059 function. Otherwise it is set to zero. Usually you just want to
16060 test for >0, but internally the negative value is used.
16062 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16063 a decl-specifier-seq unless it declares a constructor, destructor,
16064 or conversion. It might seem that we could check this condition in
16065 semantic analysis, rather than parsing, but that makes it difficult
16066 to handle something like `f()'. We want to notice that there are
16067 no decl-specifiers, and therefore realize that this is an
16068 expression, not a declaration.)
16070 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16071 the declarator is a direct-declarator of the form "(...)".
16073 MEMBER_P is true iff this declarator is a member-declarator. */
16075 static cp_declarator
*
16076 cp_parser_declarator (cp_parser
* parser
,
16077 cp_parser_declarator_kind dcl_kind
,
16078 int* ctor_dtor_or_conv_p
,
16079 bool* parenthesized_p
,
16082 cp_declarator
*declarator
;
16083 enum tree_code code
;
16084 cp_cv_quals cv_quals
;
16086 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
16088 /* Assume this is not a constructor, destructor, or type-conversion
16090 if (ctor_dtor_or_conv_p
)
16091 *ctor_dtor_or_conv_p
= 0;
16093 if (cp_parser_allow_gnu_extensions_p (parser
))
16094 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
16096 /* Check for the ptr-operator production. */
16097 cp_parser_parse_tentatively (parser
);
16098 /* Parse the ptr-operator. */
16099 code
= cp_parser_ptr_operator (parser
,
16104 /* If that worked, then we have a ptr-operator. */
16105 if (cp_parser_parse_definitely (parser
))
16107 /* If a ptr-operator was found, then this declarator was not
16109 if (parenthesized_p
)
16110 *parenthesized_p
= true;
16111 /* The dependent declarator is optional if we are parsing an
16112 abstract-declarator. */
16113 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16114 cp_parser_parse_tentatively (parser
);
16116 /* Parse the dependent declarator. */
16117 declarator
= cp_parser_declarator (parser
, dcl_kind
,
16118 /*ctor_dtor_or_conv_p=*/NULL
,
16119 /*parenthesized_p=*/NULL
,
16120 /*member_p=*/false);
16122 /* If we are parsing an abstract-declarator, we must handle the
16123 case where the dependent declarator is absent. */
16124 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
16125 && !cp_parser_parse_definitely (parser
))
16128 declarator
= cp_parser_make_indirect_declarator
16129 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
16131 /* Everything else is a direct-declarator. */
16134 if (parenthesized_p
)
16135 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
16137 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
16138 ctor_dtor_or_conv_p
,
16142 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
16143 declarator
->attributes
= gnu_attributes
;
16147 /* Parse a direct-declarator or direct-abstract-declarator.
16151 direct-declarator ( parameter-declaration-clause )
16152 cv-qualifier-seq [opt]
16153 exception-specification [opt]
16154 direct-declarator [ constant-expression [opt] ]
16157 direct-abstract-declarator:
16158 direct-abstract-declarator [opt]
16159 ( parameter-declaration-clause )
16160 cv-qualifier-seq [opt]
16161 exception-specification [opt]
16162 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16163 ( abstract-declarator )
16165 Returns a representation of the declarator. DCL_KIND is
16166 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16167 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16168 we are parsing a direct-declarator. It is
16169 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16170 of ambiguity we prefer an abstract declarator, as per
16171 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16172 cp_parser_declarator. */
16174 static cp_declarator
*
16175 cp_parser_direct_declarator (cp_parser
* parser
,
16176 cp_parser_declarator_kind dcl_kind
,
16177 int* ctor_dtor_or_conv_p
,
16181 cp_declarator
*declarator
= NULL
;
16182 tree scope
= NULL_TREE
;
16183 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16184 bool saved_in_declarator_p
= parser
->in_declarator_p
;
16186 tree pushed_scope
= NULL_TREE
;
16190 /* Peek at the next token. */
16191 token
= cp_lexer_peek_token (parser
->lexer
);
16192 if (token
->type
== CPP_OPEN_PAREN
)
16194 /* This is either a parameter-declaration-clause, or a
16195 parenthesized declarator. When we know we are parsing a
16196 named declarator, it must be a parenthesized declarator
16197 if FIRST is true. For instance, `(int)' is a
16198 parameter-declaration-clause, with an omitted
16199 direct-abstract-declarator. But `((*))', is a
16200 parenthesized abstract declarator. Finally, when T is a
16201 template parameter `(T)' is a
16202 parameter-declaration-clause, and not a parenthesized
16205 We first try and parse a parameter-declaration-clause,
16206 and then try a nested declarator (if FIRST is true).
16208 It is not an error for it not to be a
16209 parameter-declaration-clause, even when FIRST is
16215 The first is the declaration of a function while the
16216 second is the definition of a variable, including its
16219 Having seen only the parenthesis, we cannot know which of
16220 these two alternatives should be selected. Even more
16221 complex are examples like:
16226 The former is a function-declaration; the latter is a
16227 variable initialization.
16229 Thus again, we try a parameter-declaration-clause, and if
16230 that fails, we back out and return. */
16232 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16235 unsigned saved_num_template_parameter_lists
;
16236 bool is_declarator
= false;
16239 /* In a member-declarator, the only valid interpretation
16240 of a parenthesis is the start of a
16241 parameter-declaration-clause. (It is invalid to
16242 initialize a static data member with a parenthesized
16243 initializer; only the "=" form of initialization is
16246 cp_parser_parse_tentatively (parser
);
16248 /* Consume the `('. */
16249 cp_lexer_consume_token (parser
->lexer
);
16252 /* If this is going to be an abstract declarator, we're
16253 in a declarator and we can't have default args. */
16254 parser
->default_arg_ok_p
= false;
16255 parser
->in_declarator_p
= true;
16258 /* Inside the function parameter list, surrounding
16259 template-parameter-lists do not apply. */
16260 saved_num_template_parameter_lists
16261 = parser
->num_template_parameter_lists
;
16262 parser
->num_template_parameter_lists
= 0;
16264 begin_scope (sk_function_parms
, NULL_TREE
);
16266 /* Parse the parameter-declaration-clause. */
16267 params
= cp_parser_parameter_declaration_clause (parser
);
16269 parser
->num_template_parameter_lists
16270 = saved_num_template_parameter_lists
;
16272 /* Consume the `)'. */
16273 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
16275 /* If all went well, parse the cv-qualifier-seq and the
16276 exception-specification. */
16277 if (member_p
|| cp_parser_parse_definitely (parser
))
16279 cp_cv_quals cv_quals
;
16280 cp_virt_specifiers virt_specifiers
;
16281 tree exception_specification
;
16285 is_declarator
= true;
16287 if (ctor_dtor_or_conv_p
)
16288 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
16291 /* Parse the cv-qualifier-seq. */
16292 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
16293 /* And the exception-specification. */
16294 exception_specification
16295 = cp_parser_exception_specification_opt (parser
);
16297 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16299 late_return
= (cp_parser_late_return_type_opt
16300 (parser
, member_p
? cv_quals
: -1));
16302 /* Parse the virt-specifier-seq. */
16303 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
16305 /* Create the function-declarator. */
16306 declarator
= make_call_declarator (declarator
,
16310 exception_specification
,
16312 declarator
->std_attributes
= attrs
;
16313 /* Any subsequent parameter lists are to do with
16314 return type, so are not those of the declared
16316 parser
->default_arg_ok_p
= false;
16319 /* Remove the function parms from scope. */
16320 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
16321 pop_binding (DECL_NAME (t
), t
);
16325 /* Repeat the main loop. */
16329 /* If this is the first, we can try a parenthesized
16333 bool saved_in_type_id_in_expr_p
;
16335 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16336 parser
->in_declarator_p
= saved_in_declarator_p
;
16338 /* Consume the `('. */
16339 cp_lexer_consume_token (parser
->lexer
);
16340 /* Parse the nested declarator. */
16341 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16342 parser
->in_type_id_in_expr_p
= true;
16344 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
16345 /*parenthesized_p=*/NULL
,
16347 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16349 /* Expect a `)'. */
16350 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
16351 declarator
= cp_error_declarator
;
16352 if (declarator
== cp_error_declarator
)
16355 goto handle_declarator
;
16357 /* Otherwise, we must be done. */
16361 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16362 && token
->type
== CPP_OPEN_SQUARE
16363 && !cp_next_tokens_can_be_attribute_p (parser
))
16365 /* Parse an array-declarator. */
16366 tree bounds
, attrs
;
16368 if (ctor_dtor_or_conv_p
)
16369 *ctor_dtor_or_conv_p
= 0;
16372 parser
->default_arg_ok_p
= false;
16373 parser
->in_declarator_p
= true;
16374 /* Consume the `['. */
16375 cp_lexer_consume_token (parser
->lexer
);
16376 /* Peek at the next token. */
16377 token
= cp_lexer_peek_token (parser
->lexer
);
16378 /* If the next token is `]', then there is no
16379 constant-expression. */
16380 if (token
->type
!= CPP_CLOSE_SQUARE
)
16382 bool non_constant_p
;
16385 = cp_parser_constant_expression (parser
,
16386 /*allow_non_constant=*/true,
16388 if (!non_constant_p
)
16390 else if (error_operand_p (bounds
))
16391 /* Already gave an error. */;
16392 else if (!parser
->in_function_body
16393 || current_binding_level
->kind
== sk_function_parms
)
16395 /* Normally, the array bound must be an integral constant
16396 expression. However, as an extension, we allow VLAs
16397 in function scopes as long as they aren't part of a
16398 parameter declaration. */
16399 cp_parser_error (parser
,
16400 "array bound is not an integer constant");
16401 bounds
= error_mark_node
;
16403 else if (processing_template_decl
)
16405 /* Remember this wasn't a constant-expression. */
16406 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
16407 TREE_SIDE_EFFECTS (bounds
) = 1;
16411 bounds
= NULL_TREE
;
16412 /* Look for the closing `]'. */
16413 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
16415 declarator
= cp_error_declarator
;
16419 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16420 declarator
= make_array_declarator (declarator
, bounds
);
16421 declarator
->std_attributes
= attrs
;
16423 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
16426 tree qualifying_scope
;
16427 tree unqualified_name
;
16429 special_function_kind sfk
;
16431 bool pack_expansion_p
= false;
16432 cp_token
*declarator_id_start_token
;
16434 /* Parse a declarator-id */
16435 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
16438 cp_parser_parse_tentatively (parser
);
16440 /* If we see an ellipsis, we should be looking at a
16442 if (token
->type
== CPP_ELLIPSIS
)
16444 /* Consume the `...' */
16445 cp_lexer_consume_token (parser
->lexer
);
16447 pack_expansion_p
= true;
16451 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
16453 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
16454 qualifying_scope
= parser
->scope
;
16459 if (!unqualified_name
&& pack_expansion_p
)
16461 /* Check whether an error occurred. */
16462 okay
= !cp_parser_error_occurred (parser
);
16464 /* We already consumed the ellipsis to mark a
16465 parameter pack, but we have no way to report it,
16466 so abort the tentative parse. We will be exiting
16467 immediately anyway. */
16468 cp_parser_abort_tentative_parse (parser
);
16471 okay
= cp_parser_parse_definitely (parser
);
16474 unqualified_name
= error_mark_node
;
16475 else if (unqualified_name
16476 && (qualifying_scope
16477 || (TREE_CODE (unqualified_name
)
16478 != IDENTIFIER_NODE
)))
16480 cp_parser_error (parser
, "expected unqualified-id");
16481 unqualified_name
= error_mark_node
;
16485 if (!unqualified_name
)
16487 if (unqualified_name
== error_mark_node
)
16489 declarator
= cp_error_declarator
;
16490 pack_expansion_p
= false;
16491 declarator
->parameter_pack_p
= false;
16495 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16497 if (qualifying_scope
&& at_namespace_scope_p ()
16498 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
16500 /* In the declaration of a member of a template class
16501 outside of the class itself, the SCOPE will sometimes
16502 be a TYPENAME_TYPE. For example, given:
16504 template <typename T>
16505 int S<T>::R::i = 3;
16507 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16508 this context, we must resolve S<T>::R to an ordinary
16509 type, rather than a typename type.
16511 The reason we normally avoid resolving TYPENAME_TYPEs
16512 is that a specialization of `S' might render
16513 `S<T>::R' not a type. However, if `S' is
16514 specialized, then this `i' will not be used, so there
16515 is no harm in resolving the types here. */
16518 /* Resolve the TYPENAME_TYPE. */
16519 type
= resolve_typename_type (qualifying_scope
,
16520 /*only_current_p=*/false);
16521 /* If that failed, the declarator is invalid. */
16522 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16524 if (typedef_variant_p (type
))
16525 error_at (declarator_id_start_token
->location
,
16526 "cannot define member of dependent typedef "
16529 error_at (declarator_id_start_token
->location
,
16530 "%<%T::%E%> is not a type",
16531 TYPE_CONTEXT (qualifying_scope
),
16532 TYPE_IDENTIFIER (qualifying_scope
));
16534 qualifying_scope
= type
;
16539 if (unqualified_name
)
16543 if (qualifying_scope
16544 && CLASS_TYPE_P (qualifying_scope
))
16545 class_type
= qualifying_scope
;
16547 class_type
= current_class_type
;
16549 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
16551 tree name_type
= TREE_TYPE (unqualified_name
);
16552 if (class_type
&& same_type_p (name_type
, class_type
))
16554 if (qualifying_scope
16555 && CLASSTYPE_USE_TEMPLATE (name_type
))
16557 error_at (declarator_id_start_token
->location
,
16558 "invalid use of constructor as a template");
16559 inform (declarator_id_start_token
->location
,
16560 "use %<%T::%D%> instead of %<%T::%D%> to "
16561 "name the constructor in a qualified name",
16563 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
16564 class_type
, name_type
);
16565 declarator
= cp_error_declarator
;
16569 unqualified_name
= constructor_name (class_type
);
16573 /* We do not attempt to print the declarator
16574 here because we do not have enough
16575 information about its original syntactic
16577 cp_parser_error (parser
, "invalid declarator");
16578 declarator
= cp_error_declarator
;
16585 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
16586 sfk
= sfk_destructor
;
16587 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
16588 sfk
= sfk_conversion
;
16589 else if (/* There's no way to declare a constructor
16590 for an anonymous type, even if the type
16591 got a name for linkage purposes. */
16592 !TYPE_WAS_ANONYMOUS (class_type
)
16593 && constructor_name_p (unqualified_name
,
16596 unqualified_name
= constructor_name (class_type
);
16597 sfk
= sfk_constructor
;
16599 else if (is_overloaded_fn (unqualified_name
)
16600 && DECL_CONSTRUCTOR_P (get_first_fn
16601 (unqualified_name
)))
16602 sfk
= sfk_constructor
;
16604 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
16605 *ctor_dtor_or_conv_p
= -1;
16608 declarator
= make_id_declarator (qualifying_scope
,
16611 declarator
->std_attributes
= attrs
;
16612 declarator
->id_loc
= token
->location
;
16613 declarator
->parameter_pack_p
= pack_expansion_p
;
16615 if (pack_expansion_p
)
16616 maybe_warn_variadic_templates ();
16619 handle_declarator
:;
16620 scope
= get_scope_of_declarator (declarator
);
16622 /* Any names that appear after the declarator-id for a
16623 member are looked up in the containing scope. */
16624 pushed_scope
= push_scope (scope
);
16625 parser
->in_declarator_p
= true;
16626 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
16627 || (declarator
&& declarator
->kind
== cdk_id
))
16628 /* Default args are only allowed on function
16630 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16632 parser
->default_arg_ok_p
= false;
16641 /* For an abstract declarator, we might wind up with nothing at this
16642 point. That's an error; the declarator is not optional. */
16644 cp_parser_error (parser
, "expected declarator");
16646 /* If we entered a scope, we must exit it now. */
16648 pop_scope (pushed_scope
);
16650 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16651 parser
->in_declarator_p
= saved_in_declarator_p
;
16656 /* Parse a ptr-operator.
16659 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16660 * cv-qualifier-seq [opt]
16662 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16663 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16668 & cv-qualifier-seq [opt]
16670 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16671 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16672 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16673 filled in with the TYPE containing the member. *CV_QUALS is
16674 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16675 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16676 Note that the tree codes returned by this function have nothing
16677 to do with the types of trees that will be eventually be created
16678 to represent the pointer or reference type being parsed. They are
16679 just constants with suggestive names. */
16680 static enum tree_code
16681 cp_parser_ptr_operator (cp_parser
* parser
,
16683 cp_cv_quals
*cv_quals
,
16686 enum tree_code code
= ERROR_MARK
;
16688 tree attrs
= NULL_TREE
;
16690 /* Assume that it's not a pointer-to-member. */
16692 /* And that there are no cv-qualifiers. */
16693 *cv_quals
= TYPE_UNQUALIFIED
;
16695 /* Peek at the next token. */
16696 token
= cp_lexer_peek_token (parser
->lexer
);
16698 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16699 if (token
->type
== CPP_MULT
)
16700 code
= INDIRECT_REF
;
16701 else if (token
->type
== CPP_AND
)
16703 else if ((cxx_dialect
!= cxx98
) &&
16704 token
->type
== CPP_AND_AND
) /* C++0x only */
16705 code
= NON_LVALUE_EXPR
;
16707 if (code
!= ERROR_MARK
)
16709 /* Consume the `*', `&' or `&&'. */
16710 cp_lexer_consume_token (parser
->lexer
);
16712 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16713 `&', if we are allowing GNU extensions. (The only qualifier
16714 that can legally appear after `&' is `restrict', but that is
16715 enforced during semantic analysis. */
16716 if (code
== INDIRECT_REF
16717 || cp_parser_allow_gnu_extensions_p (parser
))
16718 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
16720 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16721 if (attributes
!= NULL
)
16722 *attributes
= attrs
;
16726 /* Try the pointer-to-member case. */
16727 cp_parser_parse_tentatively (parser
);
16728 /* Look for the optional `::' operator. */
16729 cp_parser_global_scope_opt (parser
,
16730 /*current_scope_valid_p=*/false);
16731 /* Look for the nested-name specifier. */
16732 token
= cp_lexer_peek_token (parser
->lexer
);
16733 cp_parser_nested_name_specifier (parser
,
16734 /*typename_keyword_p=*/false,
16735 /*check_dependency_p=*/true,
16737 /*is_declaration=*/false);
16738 /* If we found it, and the next token is a `*', then we are
16739 indeed looking at a pointer-to-member operator. */
16740 if (!cp_parser_error_occurred (parser
)
16741 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
16743 /* Indicate that the `*' operator was used. */
16744 code
= INDIRECT_REF
;
16746 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
16747 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
16748 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
16749 error_at (token
->location
, "cannot form pointer to member of "
16750 "non-class %q#T", parser
->scope
);
16753 /* The type of which the member is a member is given by the
16755 *type
= parser
->scope
;
16756 /* The next name will not be qualified. */
16757 parser
->scope
= NULL_TREE
;
16758 parser
->qualifying_scope
= NULL_TREE
;
16759 parser
->object_scope
= NULL_TREE
;
16760 /* Look for optional c++11 attributes. */
16761 attrs
= cp_parser_std_attribute_spec_seq (parser
);
16762 if (attributes
!= NULL
)
16763 *attributes
= attrs
;
16764 /* Look for the optional cv-qualifier-seq. */
16765 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
16768 /* If that didn't work we don't have a ptr-operator. */
16769 if (!cp_parser_parse_definitely (parser
))
16770 cp_parser_error (parser
, "expected ptr-operator");
16776 /* Parse an (optional) cv-qualifier-seq.
16779 cv-qualifier cv-qualifier-seq [opt]
16790 Returns a bitmask representing the cv-qualifiers. */
16793 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
16795 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
16800 cp_cv_quals cv_qualifier
;
16802 /* Peek at the next token. */
16803 token
= cp_lexer_peek_token (parser
->lexer
);
16804 /* See if it's a cv-qualifier. */
16805 switch (token
->keyword
)
16808 cv_qualifier
= TYPE_QUAL_CONST
;
16812 cv_qualifier
= TYPE_QUAL_VOLATILE
;
16816 cv_qualifier
= TYPE_QUAL_RESTRICT
;
16820 cv_qualifier
= TYPE_UNQUALIFIED
;
16827 if (cv_quals
& cv_qualifier
)
16829 error_at (token
->location
, "duplicate cv-qualifier");
16830 cp_lexer_purge_token (parser
->lexer
);
16834 cp_lexer_consume_token (parser
->lexer
);
16835 cv_quals
|= cv_qualifier
;
16842 /* Parse an (optional) virt-specifier-seq.
16844 virt-specifier-seq:
16845 virt-specifier virt-specifier-seq [opt]
16851 Returns a bitmask representing the virt-specifiers. */
16853 static cp_virt_specifiers
16854 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
16856 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
16861 cp_virt_specifiers virt_specifier
;
16863 /* Peek at the next token. */
16864 token
= cp_lexer_peek_token (parser
->lexer
);
16865 /* See if it's a virt-specifier-qualifier. */
16866 if (token
->type
!= CPP_NAME
)
16868 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
16870 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
16871 virt_specifier
= VIRT_SPEC_OVERRIDE
;
16873 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
16875 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
16876 virt_specifier
= VIRT_SPEC_FINAL
;
16878 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
16880 virt_specifier
= VIRT_SPEC_FINAL
;
16885 if (virt_specifiers
& virt_specifier
)
16887 error_at (token
->location
, "duplicate virt-specifier");
16888 cp_lexer_purge_token (parser
->lexer
);
16892 cp_lexer_consume_token (parser
->lexer
);
16893 virt_specifiers
|= virt_specifier
;
16896 return virt_specifiers
;
16899 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16900 is in scope even though it isn't real. */
16903 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
16907 if (current_class_ptr
)
16909 /* We don't clear this between NSDMIs. Is it already what we want? */
16910 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
16911 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
16912 && cp_type_quals (type
) == quals
)
16916 this_parm
= build_this_parm (ctype
, quals
);
16917 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16918 current_class_ptr
= NULL_TREE
;
16920 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
16921 current_class_ptr
= this_parm
;
16924 /* Parse a late-specified return type, if any. This is not a separate
16925 non-terminal, but part of a function declarator, which looks like
16927 -> trailing-type-specifier-seq abstract-declarator(opt)
16929 Returns the type indicated by the type-id.
16931 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16935 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_cv_quals quals
)
16940 /* Peek at the next token. */
16941 token
= cp_lexer_peek_token (parser
->lexer
);
16942 /* A late-specified return type is indicated by an initial '->'. */
16943 if (token
->type
!= CPP_DEREF
)
16946 /* Consume the ->. */
16947 cp_lexer_consume_token (parser
->lexer
);
16951 /* DR 1207: 'this' is in scope in the trailing return type. */
16952 gcc_assert (current_class_ptr
== NULL_TREE
);
16953 inject_this_parameter (current_class_type
, quals
);
16956 type
= cp_parser_trailing_type_id (parser
);
16959 current_class_ptr
= current_class_ref
= NULL_TREE
;
16964 /* Parse a declarator-id.
16968 :: [opt] nested-name-specifier [opt] type-name
16970 In the `id-expression' case, the value returned is as for
16971 cp_parser_id_expression if the id-expression was an unqualified-id.
16972 If the id-expression was a qualified-id, then a SCOPE_REF is
16973 returned. The first operand is the scope (either a NAMESPACE_DECL
16974 or TREE_TYPE), but the second is still just a representation of an
16978 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
16981 /* The expression must be an id-expression. Assume that qualified
16982 names are the names of types so that:
16985 int S<T>::R::i = 3;
16987 will work; we must treat `S<T>::R' as the name of a type.
16988 Similarly, assume that qualified names are templates, where
16992 int S<T>::R<T>::i = 3;
16995 id
= cp_parser_id_expression (parser
,
16996 /*template_keyword_p=*/false,
16997 /*check_dependency_p=*/false,
16998 /*template_p=*/NULL
,
16999 /*declarator_p=*/true,
17001 if (id
&& BASELINK_P (id
))
17002 id
= BASELINK_FUNCTIONS (id
);
17006 /* Parse a type-id.
17009 type-specifier-seq abstract-declarator [opt]
17011 Returns the TYPE specified. */
17014 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
17015 bool is_trailing_return
)
17017 cp_decl_specifier_seq type_specifier_seq
;
17018 cp_declarator
*abstract_declarator
;
17020 /* Parse the type-specifier-seq. */
17021 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17022 is_trailing_return
,
17023 &type_specifier_seq
);
17024 if (type_specifier_seq
.type
== error_mark_node
)
17025 return error_mark_node
;
17027 /* There might or might not be an abstract declarator. */
17028 cp_parser_parse_tentatively (parser
);
17029 /* Look for the declarator. */
17030 abstract_declarator
17031 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
17032 /*parenthesized_p=*/NULL
,
17033 /*member_p=*/false);
17034 /* Check to see if there really was a declarator. */
17035 if (!cp_parser_parse_definitely (parser
))
17036 abstract_declarator
= NULL
;
17038 if (type_specifier_seq
.type
17039 && type_uses_auto (type_specifier_seq
.type
))
17041 /* A type-id with type 'auto' is only ok if the abstract declarator
17042 is a function declarator with a late-specified return type. */
17043 if (abstract_declarator
17044 && abstract_declarator
->kind
== cdk_function
17045 && abstract_declarator
->u
.function
.late_return_type
)
17049 error ("invalid use of %<auto%>");
17050 return error_mark_node
;
17054 return groktypename (&type_specifier_seq
, abstract_declarator
,
17058 static tree
cp_parser_type_id (cp_parser
*parser
)
17060 return cp_parser_type_id_1 (parser
, false, false);
17063 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
17066 const char *saved_message
= parser
->type_definition_forbidden_message
;
17067 parser
->type_definition_forbidden_message
17068 = G_("types may not be defined in template arguments");
17069 r
= cp_parser_type_id_1 (parser
, true, false);
17070 parser
->type_definition_forbidden_message
= saved_message
;
17074 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
17076 return cp_parser_type_id_1 (parser
, false, true);
17079 /* Parse a type-specifier-seq.
17081 type-specifier-seq:
17082 type-specifier type-specifier-seq [opt]
17086 type-specifier-seq:
17087 attributes type-specifier-seq [opt]
17089 If IS_DECLARATION is true, we are at the start of a "condition" or
17090 exception-declaration, so we might be followed by a declarator-id.
17092 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17093 i.e. we've just seen "->".
17095 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17098 cp_parser_type_specifier_seq (cp_parser
* parser
,
17099 bool is_declaration
,
17100 bool is_trailing_return
,
17101 cp_decl_specifier_seq
*type_specifier_seq
)
17103 bool seen_type_specifier
= false;
17104 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
17105 cp_token
*start_token
= NULL
;
17107 /* Clear the TYPE_SPECIFIER_SEQ. */
17108 clear_decl_specs (type_specifier_seq
);
17110 /* In the context of a trailing return type, enum E { } is an
17111 elaborated-type-specifier followed by a function-body, not an
17113 if (is_trailing_return
)
17114 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
17116 /* Parse the type-specifiers and attributes. */
17119 tree type_specifier
;
17120 bool is_cv_qualifier
;
17122 /* Check for attributes first. */
17123 if (cp_next_tokens_can_be_attribute_p (parser
))
17125 type_specifier_seq
->attributes
=
17126 chainon (type_specifier_seq
->attributes
,
17127 cp_parser_attributes_opt (parser
));
17131 /* record the token of the beginning of the type specifier seq,
17132 for error reporting purposes*/
17134 start_token
= cp_lexer_peek_token (parser
->lexer
);
17136 /* Look for the type-specifier. */
17137 type_specifier
= cp_parser_type_specifier (parser
,
17139 type_specifier_seq
,
17140 /*is_declaration=*/false,
17143 if (!type_specifier
)
17145 /* If the first type-specifier could not be found, this is not a
17146 type-specifier-seq at all. */
17147 if (!seen_type_specifier
)
17149 cp_parser_error (parser
, "expected type-specifier");
17150 type_specifier_seq
->type
= error_mark_node
;
17153 /* If subsequent type-specifiers could not be found, the
17154 type-specifier-seq is complete. */
17158 seen_type_specifier
= true;
17159 /* The standard says that a condition can be:
17161 type-specifier-seq declarator = assignment-expression
17168 we should treat the "S" as a declarator, not as a
17169 type-specifier. The standard doesn't say that explicitly for
17170 type-specifier-seq, but it does say that for
17171 decl-specifier-seq in an ordinary declaration. Perhaps it
17172 would be clearer just to allow a decl-specifier-seq here, and
17173 then add a semantic restriction that if any decl-specifiers
17174 that are not type-specifiers appear, the program is invalid. */
17175 if (is_declaration
&& !is_cv_qualifier
)
17176 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
17180 /* Parse a parameter-declaration-clause.
17182 parameter-declaration-clause:
17183 parameter-declaration-list [opt] ... [opt]
17184 parameter-declaration-list , ...
17186 Returns a representation for the parameter declarations. A return
17187 value of NULL indicates a parameter-declaration-clause consisting
17188 only of an ellipsis. */
17191 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
17198 /* Peek at the next token. */
17199 token
= cp_lexer_peek_token (parser
->lexer
);
17200 /* Check for trivial parameter-declaration-clauses. */
17201 if (token
->type
== CPP_ELLIPSIS
)
17203 /* Consume the `...' token. */
17204 cp_lexer_consume_token (parser
->lexer
);
17207 else if (token
->type
== CPP_CLOSE_PAREN
)
17208 /* There are no parameters. */
17210 #ifndef NO_IMPLICIT_EXTERN_C
17211 if (in_system_header
&& current_class_type
== NULL
17212 && current_lang_name
== lang_name_c
)
17216 return void_list_node
;
17218 /* Check for `(void)', too, which is a special case. */
17219 else if (token
->keyword
== RID_VOID
17220 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17221 == CPP_CLOSE_PAREN
))
17223 /* Consume the `void' token. */
17224 cp_lexer_consume_token (parser
->lexer
);
17225 /* There are no parameters. */
17226 return void_list_node
;
17229 /* Parse the parameter-declaration-list. */
17230 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
17231 /* If a parse error occurred while parsing the
17232 parameter-declaration-list, then the entire
17233 parameter-declaration-clause is erroneous. */
17237 /* Peek at the next token. */
17238 token
= cp_lexer_peek_token (parser
->lexer
);
17239 /* If it's a `,', the clause should terminate with an ellipsis. */
17240 if (token
->type
== CPP_COMMA
)
17242 /* Consume the `,'. */
17243 cp_lexer_consume_token (parser
->lexer
);
17244 /* Expect an ellipsis. */
17246 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
17248 /* It might also be `...' if the optional trailing `,' was
17250 else if (token
->type
== CPP_ELLIPSIS
)
17252 /* Consume the `...' token. */
17253 cp_lexer_consume_token (parser
->lexer
);
17254 /* And remember that we saw it. */
17258 ellipsis_p
= false;
17260 /* Finish the parameter list. */
17262 parameters
= chainon (parameters
, void_list_node
);
17267 /* Parse a parameter-declaration-list.
17269 parameter-declaration-list:
17270 parameter-declaration
17271 parameter-declaration-list , parameter-declaration
17273 Returns a representation of the parameter-declaration-list, as for
17274 cp_parser_parameter_declaration_clause. However, the
17275 `void_list_node' is never appended to the list. Upon return,
17276 *IS_ERROR will be true iff an error occurred. */
17279 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
17281 tree parameters
= NULL_TREE
;
17282 tree
*tail
= ¶meters
;
17283 bool saved_in_unbraced_linkage_specification_p
;
17286 /* Assume all will go well. */
17288 /* The special considerations that apply to a function within an
17289 unbraced linkage specifications do not apply to the parameters
17290 to the function. */
17291 saved_in_unbraced_linkage_specification_p
17292 = parser
->in_unbraced_linkage_specification_p
;
17293 parser
->in_unbraced_linkage_specification_p
= false;
17295 /* Look for more parameters. */
17298 cp_parameter_declarator
*parameter
;
17299 tree decl
= error_mark_node
;
17300 bool parenthesized_p
= false;
17301 /* Parse the parameter. */
17303 = cp_parser_parameter_declaration (parser
,
17304 /*template_parm_p=*/false,
17307 /* We don't know yet if the enclosing context is deprecated, so wait
17308 and warn in grokparms if appropriate. */
17309 deprecated_state
= DEPRECATED_SUPPRESS
;
17312 decl
= grokdeclarator (parameter
->declarator
,
17313 ¶meter
->decl_specifiers
,
17315 parameter
->default_argument
!= NULL_TREE
,
17316 ¶meter
->decl_specifiers
.attributes
);
17318 deprecated_state
= DEPRECATED_NORMAL
;
17320 /* If a parse error occurred parsing the parameter declaration,
17321 then the entire parameter-declaration-list is erroneous. */
17322 if (decl
== error_mark_node
)
17325 parameters
= error_mark_node
;
17329 if (parameter
->decl_specifiers
.attributes
)
17330 cplus_decl_attributes (&decl
,
17331 parameter
->decl_specifiers
.attributes
,
17333 if (DECL_NAME (decl
))
17334 decl
= pushdecl (decl
);
17336 if (decl
!= error_mark_node
)
17338 retrofit_lang_decl (decl
);
17339 DECL_PARM_INDEX (decl
) = ++index
;
17340 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
17343 /* Add the new parameter to the list. */
17344 *tail
= build_tree_list (parameter
->default_argument
, decl
);
17345 tail
= &TREE_CHAIN (*tail
);
17347 /* Peek at the next token. */
17348 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
17349 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
17350 /* These are for Objective-C++ */
17351 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17352 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17353 /* The parameter-declaration-list is complete. */
17355 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17359 /* Peek at the next token. */
17360 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17361 /* If it's an ellipsis, then the list is complete. */
17362 if (token
->type
== CPP_ELLIPSIS
)
17364 /* Otherwise, there must be more parameters. Consume the
17366 cp_lexer_consume_token (parser
->lexer
);
17367 /* When parsing something like:
17369 int i(float f, double d)
17371 we can tell after seeing the declaration for "f" that we
17372 are not looking at an initialization of a variable "i",
17373 but rather at the declaration of a function "i".
17375 Due to the fact that the parsing of template arguments
17376 (as specified to a template-id) requires backtracking we
17377 cannot use this technique when inside a template argument
17379 if (!parser
->in_template_argument_list_p
17380 && !parser
->in_type_id_in_expr_p
17381 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
17382 /* However, a parameter-declaration of the form
17383 "foat(f)" (which is a valid declaration of a
17384 parameter "f") can also be interpreted as an
17385 expression (the conversion of "f" to "float"). */
17386 && !parenthesized_p
)
17387 cp_parser_commit_to_tentative_parse (parser
);
17391 cp_parser_error (parser
, "expected %<,%> or %<...%>");
17392 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
17393 cp_parser_skip_to_closing_parenthesis (parser
,
17394 /*recovering=*/true,
17395 /*or_comma=*/false,
17396 /*consume_paren=*/false);
17401 parser
->in_unbraced_linkage_specification_p
17402 = saved_in_unbraced_linkage_specification_p
;
17407 /* Parse a parameter declaration.
17409 parameter-declaration:
17410 decl-specifier-seq ... [opt] declarator
17411 decl-specifier-seq declarator = assignment-expression
17412 decl-specifier-seq ... [opt] abstract-declarator [opt]
17413 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17415 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17416 declares a template parameter. (In that case, a non-nested `>'
17417 token encountered during the parsing of the assignment-expression
17418 is not interpreted as a greater-than operator.)
17420 Returns a representation of the parameter, or NULL if an error
17421 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17422 true iff the declarator is of the form "(p)". */
17424 static cp_parameter_declarator
*
17425 cp_parser_parameter_declaration (cp_parser
*parser
,
17426 bool template_parm_p
,
17427 bool *parenthesized_p
)
17429 int declares_class_or_enum
;
17430 cp_decl_specifier_seq decl_specifiers
;
17431 cp_declarator
*declarator
;
17432 tree default_argument
;
17433 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
17434 const char *saved_message
;
17436 /* In a template parameter, `>' is not an operator.
17440 When parsing a default template-argument for a non-type
17441 template-parameter, the first non-nested `>' is taken as the end
17442 of the template parameter-list rather than a greater-than
17445 /* Type definitions may not appear in parameter types. */
17446 saved_message
= parser
->type_definition_forbidden_message
;
17447 parser
->type_definition_forbidden_message
17448 = G_("types may not be defined in parameter types");
17450 /* Parse the declaration-specifiers. */
17451 cp_parser_decl_specifier_seq (parser
,
17452 CP_PARSER_FLAGS_NONE
,
17454 &declares_class_or_enum
);
17456 /* Complain about missing 'typename' or other invalid type names. */
17457 if (!decl_specifiers
.any_type_specifiers_p
)
17458 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
17460 /* If an error occurred, there's no reason to attempt to parse the
17461 rest of the declaration. */
17462 if (cp_parser_error_occurred (parser
))
17464 parser
->type_definition_forbidden_message
= saved_message
;
17468 /* Peek at the next token. */
17469 token
= cp_lexer_peek_token (parser
->lexer
);
17471 /* If the next token is a `)', `,', `=', `>', or `...', then there
17472 is no declarator. However, when variadic templates are enabled,
17473 there may be a declarator following `...'. */
17474 if (token
->type
== CPP_CLOSE_PAREN
17475 || token
->type
== CPP_COMMA
17476 || token
->type
== CPP_EQ
17477 || token
->type
== CPP_GREATER
)
17480 if (parenthesized_p
)
17481 *parenthesized_p
= false;
17483 /* Otherwise, there should be a declarator. */
17486 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17487 parser
->default_arg_ok_p
= false;
17489 /* After seeing a decl-specifier-seq, if the next token is not a
17490 "(", there is no possibility that the code is a valid
17491 expression. Therefore, if parsing tentatively, we commit at
17493 if (!parser
->in_template_argument_list_p
17494 /* In an expression context, having seen:
17498 we cannot be sure whether we are looking at a
17499 function-type (taking a "char" as a parameter) or a cast
17500 of some object of type "char" to "int". */
17501 && !parser
->in_type_id_in_expr_p
17502 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
17503 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
17504 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
17505 cp_parser_commit_to_tentative_parse (parser
);
17506 /* Parse the declarator. */
17507 declarator_token_start
= token
;
17508 declarator
= cp_parser_declarator (parser
,
17509 CP_PARSER_DECLARATOR_EITHER
,
17510 /*ctor_dtor_or_conv_p=*/NULL
,
17512 /*member_p=*/false);
17513 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17514 /* After the declarator, allow more attributes. */
17515 decl_specifiers
.attributes
17516 = chainon (decl_specifiers
.attributes
,
17517 cp_parser_attributes_opt (parser
));
17520 /* If the next token is an ellipsis, and we have not seen a
17521 declarator name, and the type of the declarator contains parameter
17522 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17523 a parameter pack expansion expression. Otherwise, leave the
17524 ellipsis for a C-style variadic function. */
17525 token
= cp_lexer_peek_token (parser
->lexer
);
17526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17528 tree type
= decl_specifiers
.type
;
17530 if (type
&& DECL_P (type
))
17531 type
= TREE_TYPE (type
);
17534 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
17535 && declarator_can_be_parameter_pack (declarator
)
17536 && (!declarator
|| !declarator
->parameter_pack_p
)
17537 && uses_parameter_packs (type
))
17539 /* Consume the `...'. */
17540 cp_lexer_consume_token (parser
->lexer
);
17541 maybe_warn_variadic_templates ();
17543 /* Build a pack expansion type */
17545 declarator
->parameter_pack_p
= true;
17547 decl_specifiers
.type
= make_pack_expansion (type
);
17551 /* The restriction on defining new types applies only to the type
17552 of the parameter, not to the default argument. */
17553 parser
->type_definition_forbidden_message
= saved_message
;
17555 /* If the next token is `=', then process a default argument. */
17556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17558 token
= cp_lexer_peek_token (parser
->lexer
);
17559 /* If we are defining a class, then the tokens that make up the
17560 default argument must be saved and processed later. */
17561 if (!template_parm_p
&& at_class_scope_p ()
17562 && TYPE_BEING_DEFINED (current_class_type
)
17563 && !LAMBDA_TYPE_P (current_class_type
))
17564 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
17565 /* Outside of a class definition, we can just parse the
17566 assignment-expression. */
17569 = cp_parser_default_argument (parser
, template_parm_p
);
17571 if (!parser
->default_arg_ok_p
)
17573 if (flag_permissive
)
17574 warning (0, "deprecated use of default argument for parameter of non-function");
17577 error_at (token
->location
,
17578 "default arguments are only "
17579 "permitted for function parameters");
17580 default_argument
= NULL_TREE
;
17583 else if ((declarator
&& declarator
->parameter_pack_p
)
17584 || (decl_specifiers
.type
17585 && PACK_EXPANSION_P (decl_specifiers
.type
)))
17587 /* Find the name of the parameter pack. */
17588 cp_declarator
*id_declarator
= declarator
;
17589 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
17590 id_declarator
= id_declarator
->declarator
;
17592 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
17593 error_at (declarator_token_start
->location
,
17595 ? G_("template parameter pack %qD "
17596 "cannot have a default argument")
17597 : G_("parameter pack %qD cannot have "
17598 "a default argument"),
17599 id_declarator
->u
.id
.unqualified_name
);
17601 error_at (declarator_token_start
->location
,
17603 ? G_("template parameter pack cannot have "
17604 "a default argument")
17605 : G_("parameter pack cannot have a "
17606 "default argument"));
17608 default_argument
= NULL_TREE
;
17612 default_argument
= NULL_TREE
;
17614 return make_parameter_declarator (&decl_specifiers
,
17619 /* Parse a default argument and return it.
17621 TEMPLATE_PARM_P is true if this is a default argument for a
17622 non-type template parameter. */
17624 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
17626 tree default_argument
= NULL_TREE
;
17627 bool saved_greater_than_is_operator_p
;
17628 bool saved_local_variables_forbidden_p
;
17629 bool non_constant_p
, is_direct_init
;
17631 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17633 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
17634 parser
->greater_than_is_operator_p
= !template_parm_p
;
17635 /* Local variable names (and the `this' keyword) may not
17636 appear in a default argument. */
17637 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
17638 parser
->local_variables_forbidden_p
= true;
17639 /* Parse the assignment-expression. */
17640 if (template_parm_p
)
17641 push_deferring_access_checks (dk_no_deferred
);
17643 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
17644 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
17645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
17646 if (template_parm_p
)
17647 pop_deferring_access_checks ();
17648 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
17649 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
17651 return default_argument
;
17654 /* Parse a function-body.
17657 compound_statement */
17660 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
17662 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
17665 /* Parse a ctor-initializer-opt followed by a function-body. Return
17666 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17667 is true we are parsing a function-try-block. */
17670 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
17671 bool in_function_try_block
)
17674 bool ctor_initializer_p
;
17675 const bool check_body_p
=
17676 DECL_CONSTRUCTOR_P (current_function_decl
)
17677 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
17680 /* Begin the function body. */
17681 body
= begin_function_body ();
17682 /* Parse the optional ctor-initializer. */
17683 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
17685 /* If we're parsing a constexpr constructor definition, we need
17686 to check that the constructor body is indeed empty. However,
17687 before we get to cp_parser_function_body lot of junk has been
17688 generated, so we can't just check that we have an empty block.
17689 Rather we take a snapshot of the outermost block, and check whether
17690 cp_parser_function_body changed its state. */
17693 list
= cur_stmt_list
;
17694 if (STATEMENT_LIST_TAIL (list
))
17695 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
17697 /* Parse the function-body. */
17698 cp_parser_function_body (parser
, in_function_try_block
);
17700 check_constexpr_ctor_body (last
, list
);
17701 /* Finish the function body. */
17702 finish_function_body (body
);
17704 return ctor_initializer_p
;
17707 /* Parse an initializer.
17710 = initializer-clause
17711 ( expression-list )
17713 Returns an expression representing the initializer. If no
17714 initializer is present, NULL_TREE is returned.
17716 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17717 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17718 set to TRUE if there is no initializer present. If there is an
17719 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17720 is set to true; otherwise it is set to false. */
17723 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
17724 bool* non_constant_p
)
17729 /* Peek at the next token. */
17730 token
= cp_lexer_peek_token (parser
->lexer
);
17732 /* Let our caller know whether or not this initializer was
17734 *is_direct_init
= (token
->type
!= CPP_EQ
);
17735 /* Assume that the initializer is constant. */
17736 *non_constant_p
= false;
17738 if (token
->type
== CPP_EQ
)
17740 /* Consume the `='. */
17741 cp_lexer_consume_token (parser
->lexer
);
17742 /* Parse the initializer-clause. */
17743 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
17745 else if (token
->type
== CPP_OPEN_PAREN
)
17748 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
17750 /*allow_expansion_p=*/true,
17753 return error_mark_node
;
17754 init
= build_tree_list_vec (vec
);
17755 release_tree_vector (vec
);
17757 else if (token
->type
== CPP_OPEN_BRACE
)
17759 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
17760 init
= cp_parser_braced_list (parser
, non_constant_p
);
17761 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
17765 /* Anything else is an error. */
17766 cp_parser_error (parser
, "expected initializer");
17767 init
= error_mark_node
;
17773 /* Parse an initializer-clause.
17775 initializer-clause:
17776 assignment-expression
17779 Returns an expression representing the initializer.
17781 If the `assignment-expression' production is used the value
17782 returned is simply a representation for the expression.
17784 Otherwise, calls cp_parser_braced_list. */
17787 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
17791 /* Assume the expression is constant. */
17792 *non_constant_p
= false;
17794 /* If it is not a `{', then we are looking at an
17795 assignment-expression. */
17796 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
17799 = cp_parser_constant_expression (parser
,
17800 /*allow_non_constant_p=*/true,
17804 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
17806 return initializer
;
17809 /* Parse a brace-enclosed initializer list.
17812 { initializer-list , [opt] }
17815 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17816 the elements of the initializer-list (or NULL, if the last
17817 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17818 NULL_TREE. There is no way to detect whether or not the optional
17819 trailing `,' was provided. NON_CONSTANT_P is as for
17820 cp_parser_initializer. */
17823 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
17827 /* Consume the `{' token. */
17828 cp_lexer_consume_token (parser
->lexer
);
17829 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17830 initializer
= make_node (CONSTRUCTOR
);
17831 /* If it's not a `}', then there is a non-trivial initializer. */
17832 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
17834 /* Parse the initializer list. */
17835 CONSTRUCTOR_ELTS (initializer
)
17836 = cp_parser_initializer_list (parser
, non_constant_p
);
17837 /* A trailing `,' token is allowed. */
17838 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17839 cp_lexer_consume_token (parser
->lexer
);
17841 /* Now, there should be a trailing `}'. */
17842 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17843 TREE_TYPE (initializer
) = init_list_type_node
;
17844 return initializer
;
17847 /* Parse an initializer-list.
17850 initializer-clause ... [opt]
17851 initializer-list , initializer-clause ... [opt]
17856 designation initializer-clause ...[opt]
17857 initializer-list , designation initializer-clause ...[opt]
17862 [ constant-expression ] =
17864 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17865 for the initializer. If the INDEX of the elt is non-NULL, it is the
17866 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17867 as for cp_parser_initializer. */
17869 static VEC(constructor_elt
,gc
) *
17870 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
17872 VEC(constructor_elt
,gc
) *v
= NULL
;
17874 /* Assume all of the expressions are constant. */
17875 *non_constant_p
= false;
17877 /* Parse the rest of the list. */
17883 bool clause_non_constant_p
;
17885 /* If the next token is an identifier and the following one is a
17886 colon, we are looking at the GNU designated-initializer
17888 if (cp_parser_allow_gnu_extensions_p (parser
)
17889 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
17890 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
17892 /* Warn the user that they are using an extension. */
17893 pedwarn (input_location
, OPT_Wpedantic
,
17894 "ISO C++ does not allow designated initializers");
17895 /* Consume the identifier. */
17896 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17897 /* Consume the `:'. */
17898 cp_lexer_consume_token (parser
->lexer
);
17900 /* Also handle the C99 syntax, '. id ='. */
17901 else if (cp_parser_allow_gnu_extensions_p (parser
)
17902 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
17903 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
17904 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
17906 /* Warn the user that they are using an extension. */
17907 pedwarn (input_location
, OPT_Wpedantic
,
17908 "ISO C++ does not allow C99 designated initializers");
17909 /* Consume the `.'. */
17910 cp_lexer_consume_token (parser
->lexer
);
17911 /* Consume the identifier. */
17912 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17913 /* Consume the `='. */
17914 cp_lexer_consume_token (parser
->lexer
);
17916 /* Also handle C99 array designators, '[ const ] ='. */
17917 else if (cp_parser_allow_gnu_extensions_p (parser
)
17918 && !c_dialect_objc ()
17919 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
17921 /* In C++11, [ could start a lambda-introducer. */
17922 cp_parser_parse_tentatively (parser
);
17923 cp_lexer_consume_token (parser
->lexer
);
17924 designator
= cp_parser_constant_expression (parser
, false, NULL
);
17925 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
17926 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
17927 if (!cp_parser_parse_definitely (parser
))
17928 designator
= NULL_TREE
;
17931 designator
= NULL_TREE
;
17933 /* Parse the initializer. */
17934 initializer
= cp_parser_initializer_clause (parser
,
17935 &clause_non_constant_p
);
17936 /* If any clause is non-constant, so is the entire initializer. */
17937 if (clause_non_constant_p
)
17938 *non_constant_p
= true;
17940 /* If we have an ellipsis, this is an initializer pack
17942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17944 /* Consume the `...'. */
17945 cp_lexer_consume_token (parser
->lexer
);
17947 /* Turn the initializer into an initializer expansion. */
17948 initializer
= make_pack_expansion (initializer
);
17951 /* Add it to the vector. */
17952 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
17954 /* If the next token is not a comma, we have reached the end of
17956 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17959 /* Peek at the next token. */
17960 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17961 /* If the next token is a `}', then we're still done. An
17962 initializer-clause can have a trailing `,' after the
17963 initializer-list and before the closing `}'. */
17964 if (token
->type
== CPP_CLOSE_BRACE
)
17967 /* Consume the `,' token. */
17968 cp_lexer_consume_token (parser
->lexer
);
17974 /* Classes [gram.class] */
17976 /* Parse a class-name.
17982 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17983 to indicate that names looked up in dependent types should be
17984 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17985 keyword has been used to indicate that the name that appears next
17986 is a template. TAG_TYPE indicates the explicit tag given before
17987 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17988 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17989 is the class being defined in a class-head.
17991 Returns the TYPE_DECL representing the class. */
17994 cp_parser_class_name (cp_parser
*parser
,
17995 bool typename_keyword_p
,
17996 bool template_keyword_p
,
17997 enum tag_types tag_type
,
17998 bool check_dependency_p
,
18000 bool is_declaration
)
18006 tree identifier
= NULL_TREE
;
18008 /* All class-names start with an identifier. */
18009 token
= cp_lexer_peek_token (parser
->lexer
);
18010 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
18012 cp_parser_error (parser
, "expected class-name");
18013 return error_mark_node
;
18016 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18017 to a template-id, so we save it here. */
18018 scope
= parser
->scope
;
18019 if (scope
== error_mark_node
)
18020 return error_mark_node
;
18022 /* Any name names a type if we're following the `typename' keyword
18023 in a qualified name where the enclosing scope is type-dependent. */
18024 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
18025 && dependent_type_p (scope
));
18026 /* Handle the common case (an identifier, but not a template-id)
18028 if (token
->type
== CPP_NAME
18029 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
18031 cp_token
*identifier_token
;
18034 /* Look for the identifier. */
18035 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
18036 ambiguous_p
= identifier_token
->ambiguous_p
;
18037 identifier
= cp_parser_identifier (parser
);
18038 /* If the next token isn't an identifier, we are certainly not
18039 looking at a class-name. */
18040 if (identifier
== error_mark_node
)
18041 decl
= error_mark_node
;
18042 /* If we know this is a type-name, there's no need to look it
18044 else if (typename_p
)
18048 tree ambiguous_decls
;
18049 /* If we already know that this lookup is ambiguous, then
18050 we've already issued an error message; there's no reason
18054 cp_parser_simulate_error (parser
);
18055 return error_mark_node
;
18057 /* If the next token is a `::', then the name must be a type
18060 [basic.lookup.qual]
18062 During the lookup for a name preceding the :: scope
18063 resolution operator, object, function, and enumerator
18064 names are ignored. */
18065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18066 tag_type
= typename_type
;
18067 /* Look up the name. */
18068 decl
= cp_parser_lookup_name (parser
, identifier
,
18070 /*is_template=*/false,
18071 /*is_namespace=*/false,
18072 check_dependency_p
,
18074 identifier_token
->location
);
18075 if (ambiguous_decls
)
18077 if (cp_parser_parsing_tentatively (parser
))
18078 cp_parser_simulate_error (parser
);
18079 return error_mark_node
;
18085 /* Try a template-id. */
18086 decl
= cp_parser_template_id (parser
, template_keyword_p
,
18087 check_dependency_p
,
18090 if (decl
== error_mark_node
)
18091 return error_mark_node
;
18094 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
18096 /* If this is a typename, create a TYPENAME_TYPE. */
18097 if (typename_p
&& decl
!= error_mark_node
)
18099 decl
= make_typename_type (scope
, decl
, typename_type
,
18100 /*complain=*/tf_error
);
18101 if (decl
!= error_mark_node
)
18102 decl
= TYPE_NAME (decl
);
18105 decl
= strip_using_decl (decl
);
18107 /* Check to see that it is really the name of a class. */
18108 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18109 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
18110 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18111 /* Situations like this:
18113 template <typename T> struct A {
18114 typename T::template X<int>::I i;
18117 are problematic. Is `T::template X<int>' a class-name? The
18118 standard does not seem to be definitive, but there is no other
18119 valid interpretation of the following `::'. Therefore, those
18120 names are considered class-names. */
18122 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
18123 if (decl
!= error_mark_node
)
18124 decl
= TYPE_NAME (decl
);
18126 else if (TREE_CODE (decl
) != TYPE_DECL
18127 || TREE_TYPE (decl
) == error_mark_node
18128 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
18129 /* In Objective-C 2.0, a classname followed by '.' starts a
18130 dot-syntax expression, and it's not a type-name. */
18131 || (c_dialect_objc ()
18132 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
18133 && objc_is_class_name (decl
)))
18134 decl
= error_mark_node
;
18136 if (decl
== error_mark_node
)
18137 cp_parser_error (parser
, "expected class-name");
18138 else if (identifier
&& !parser
->scope
)
18139 maybe_note_name_used_in_class (identifier
, decl
);
18144 /* Parse a class-specifier.
18147 class-head { member-specification [opt] }
18149 Returns the TREE_TYPE representing the class. */
18152 cp_parser_class_specifier_1 (cp_parser
* parser
)
18155 tree attributes
= NULL_TREE
;
18156 bool nested_name_specifier_p
;
18157 unsigned saved_num_template_parameter_lists
;
18158 bool saved_in_function_body
;
18159 unsigned char in_statement
;
18160 bool in_switch_statement_p
;
18161 bool saved_in_unbraced_linkage_specification_p
;
18162 tree old_scope
= NULL_TREE
;
18163 tree scope
= NULL_TREE
;
18164 cp_token
*closing_brace
;
18166 push_deferring_access_checks (dk_no_deferred
);
18168 /* Parse the class-head. */
18169 type
= cp_parser_class_head (parser
,
18170 &nested_name_specifier_p
);
18171 /* If the class-head was a semantic disaster, skip the entire body
18175 cp_parser_skip_to_end_of_block_or_statement (parser
);
18176 pop_deferring_access_checks ();
18177 return error_mark_node
;
18180 /* Look for the `{'. */
18181 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
18183 pop_deferring_access_checks ();
18184 return error_mark_node
;
18187 /* Issue an error message if type-definitions are forbidden here. */
18188 cp_parser_check_type_definition (parser
);
18189 /* Remember that we are defining one more class. */
18190 ++parser
->num_classes_being_defined
;
18191 /* Inside the class, surrounding template-parameter-lists do not
18193 saved_num_template_parameter_lists
18194 = parser
->num_template_parameter_lists
;
18195 parser
->num_template_parameter_lists
= 0;
18196 /* We are not in a function body. */
18197 saved_in_function_body
= parser
->in_function_body
;
18198 parser
->in_function_body
= false;
18199 /* Or in a loop. */
18200 in_statement
= parser
->in_statement
;
18201 parser
->in_statement
= 0;
18202 /* Or in a switch. */
18203 in_switch_statement_p
= parser
->in_switch_statement_p
;
18204 parser
->in_switch_statement_p
= false;
18205 /* We are not immediately inside an extern "lang" block. */
18206 saved_in_unbraced_linkage_specification_p
18207 = parser
->in_unbraced_linkage_specification_p
;
18208 parser
->in_unbraced_linkage_specification_p
= false;
18210 /* Start the class. */
18211 if (nested_name_specifier_p
)
18213 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
18214 old_scope
= push_inner_scope (scope
);
18216 type
= begin_class_definition (type
);
18218 if (type
== error_mark_node
)
18219 /* If the type is erroneous, skip the entire body of the class. */
18220 cp_parser_skip_to_closing_brace (parser
);
18222 /* Parse the member-specification. */
18223 cp_parser_member_specification_opt (parser
);
18225 /* Look for the trailing `}'. */
18226 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18227 /* Look for trailing attributes to apply to this class. */
18228 if (cp_parser_allow_gnu_extensions_p (parser
))
18229 attributes
= cp_parser_gnu_attributes_opt (parser
);
18230 if (type
!= error_mark_node
)
18231 type
= finish_struct (type
, attributes
);
18232 if (nested_name_specifier_p
)
18233 pop_inner_scope (old_scope
, scope
);
18235 /* We've finished a type definition. Check for the common syntax
18236 error of forgetting a semicolon after the definition. We need to
18237 be careful, as we can't just check for not-a-semicolon and be done
18238 with it; the user might have typed:
18240 class X { } c = ...;
18241 class X { } *p = ...;
18243 and so forth. Instead, enumerate all the possible tokens that
18244 might follow this production; if we don't see one of them, then
18245 complain and silently insert the semicolon. */
18247 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18248 bool want_semicolon
= true;
18250 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18251 /* Don't try to parse c++11 attributes here. As per the
18252 grammar, that should be a task for
18253 cp_parser_decl_specifier_seq. */
18254 want_semicolon
= false;
18256 switch (token
->type
)
18259 case CPP_SEMICOLON
:
18262 case CPP_OPEN_PAREN
:
18263 case CPP_CLOSE_PAREN
:
18265 want_semicolon
= false;
18268 /* While it's legal for type qualifiers and storage class
18269 specifiers to follow type definitions in the grammar, only
18270 compiler testsuites contain code like that. Assume that if
18271 we see such code, then what we're really seeing is a case
18275 const <type> var = ...;
18280 static <type> func (...) ...
18282 i.e. the qualifier or specifier applies to the next
18283 declaration. To do so, however, we need to look ahead one
18284 more token to see if *that* token is a type specifier.
18286 This code could be improved to handle:
18289 static const <type> var = ...; */
18291 if (keyword_is_decl_specifier (token
->keyword
))
18293 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18295 /* Handling user-defined types here would be nice, but very
18298 = (lookahead
->type
== CPP_KEYWORD
18299 && keyword_begins_type_specifier (lookahead
->keyword
));
18306 /* If we don't have a type, then something is very wrong and we
18307 shouldn't try to do anything clever. Likewise for not seeing the
18309 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
18311 cp_token_position prev
18312 = cp_lexer_previous_token_position (parser
->lexer
);
18313 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
18314 location_t loc
= prev_token
->location
;
18316 if (CLASSTYPE_DECLARED_CLASS (type
))
18317 error_at (loc
, "expected %<;%> after class definition");
18318 else if (TREE_CODE (type
) == RECORD_TYPE
)
18319 error_at (loc
, "expected %<;%> after struct definition");
18320 else if (TREE_CODE (type
) == UNION_TYPE
)
18321 error_at (loc
, "expected %<;%> after union definition");
18323 gcc_unreachable ();
18325 /* Unget one token and smash it to look as though we encountered
18326 a semicolon in the input stream. */
18327 cp_lexer_set_token_position (parser
->lexer
, prev
);
18328 token
= cp_lexer_peek_token (parser
->lexer
);
18329 token
->type
= CPP_SEMICOLON
;
18330 token
->keyword
= RID_MAX
;
18334 /* If this class is not itself within the scope of another class,
18335 then we need to parse the bodies of all of the queued function
18336 definitions. Note that the queued functions defined in a class
18337 are not always processed immediately following the
18338 class-specifier for that class. Consider:
18341 struct B { void f() { sizeof (A); } };
18344 If `f' were processed before the processing of `A' were
18345 completed, there would be no way to compute the size of `A'.
18346 Note that the nesting we are interested in here is lexical --
18347 not the semantic nesting given by TYPE_CONTEXT. In particular,
18350 struct A { struct B; };
18351 struct A::B { void f() { } };
18353 there is no need to delay the parsing of `A::B::f'. */
18354 if (--parser
->num_classes_being_defined
== 0)
18357 tree class_type
= NULL_TREE
;
18358 tree pushed_scope
= NULL_TREE
;
18360 cp_default_arg_entry
*e
;
18361 tree save_ccp
, save_ccr
;
18363 /* In a first pass, parse default arguments to the functions.
18364 Then, in a second pass, parse the bodies of the functions.
18365 This two-phased approach handles cases like:
18373 FOR_EACH_VEC_ELT (cp_default_arg_entry
, unparsed_funs_with_default_args
,
18377 /* If there are default arguments that have not yet been processed,
18378 take care of them now. */
18379 if (class_type
!= e
->class_type
)
18382 pop_scope (pushed_scope
);
18383 class_type
= e
->class_type
;
18384 pushed_scope
= push_scope (class_type
);
18386 /* Make sure that any template parameters are in scope. */
18387 maybe_begin_member_template_processing (decl
);
18388 /* Parse the default argument expressions. */
18389 cp_parser_late_parsing_default_args (parser
, decl
);
18390 /* Remove any template parameters from the symbol table. */
18391 maybe_end_member_template_processing ();
18393 VEC_truncate (cp_default_arg_entry
, unparsed_funs_with_default_args
, 0);
18394 /* Now parse any NSDMIs. */
18395 save_ccp
= current_class_ptr
;
18396 save_ccr
= current_class_ref
;
18397 FOR_EACH_VEC_ELT (tree
, unparsed_nsdmis
, ix
, decl
)
18399 if (class_type
!= DECL_CONTEXT (decl
))
18402 pop_scope (pushed_scope
);
18403 class_type
= DECL_CONTEXT (decl
);
18404 pushed_scope
= push_scope (class_type
);
18406 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
18407 cp_parser_late_parsing_nsdmi (parser
, decl
);
18409 VEC_truncate (tree
, unparsed_nsdmis
, 0);
18410 current_class_ptr
= save_ccp
;
18411 current_class_ref
= save_ccr
;
18413 pop_scope (pushed_scope
);
18414 /* Now parse the body of the functions. */
18415 FOR_EACH_VEC_ELT (tree
, unparsed_funs_with_definitions
, ix
, decl
)
18416 cp_parser_late_parsing_for_member (parser
, decl
);
18417 VEC_truncate (tree
, unparsed_funs_with_definitions
, 0);
18420 /* Put back any saved access checks. */
18421 pop_deferring_access_checks ();
18423 /* Restore saved state. */
18424 parser
->in_switch_statement_p
= in_switch_statement_p
;
18425 parser
->in_statement
= in_statement
;
18426 parser
->in_function_body
= saved_in_function_body
;
18427 parser
->num_template_parameter_lists
18428 = saved_num_template_parameter_lists
;
18429 parser
->in_unbraced_linkage_specification_p
18430 = saved_in_unbraced_linkage_specification_p
;
18436 cp_parser_class_specifier (cp_parser
* parser
)
18439 timevar_push (TV_PARSE_STRUCT
);
18440 ret
= cp_parser_class_specifier_1 (parser
);
18441 timevar_pop (TV_PARSE_STRUCT
);
18445 /* Parse a class-head.
18448 class-key identifier [opt] base-clause [opt]
18449 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18450 class-key nested-name-specifier [opt] template-id
18453 class-virt-specifier:
18457 class-key attributes identifier [opt] base-clause [opt]
18458 class-key attributes nested-name-specifier identifier base-clause [opt]
18459 class-key attributes nested-name-specifier [opt] template-id
18462 Upon return BASES is initialized to the list of base classes (or
18463 NULL, if there are none) in the same form returned by
18464 cp_parser_base_clause.
18466 Returns the TYPE of the indicated class. Sets
18467 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18468 involving a nested-name-specifier was used, and FALSE otherwise.
18470 Returns error_mark_node if this is not a class-head.
18472 Returns NULL_TREE if the class-head is syntactically valid, but
18473 semantically invalid in a way that means we should skip the entire
18474 body of the class. */
18477 cp_parser_class_head (cp_parser
* parser
,
18478 bool* nested_name_specifier_p
)
18480 tree nested_name_specifier
;
18481 enum tag_types class_key
;
18482 tree id
= NULL_TREE
;
18483 tree type
= NULL_TREE
;
18486 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18487 bool template_id_p
= false;
18488 bool qualified_p
= false;
18489 bool invalid_nested_name_p
= false;
18490 bool invalid_explicit_specialization_p
= false;
18491 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18492 tree pushed_scope
= NULL_TREE
;
18493 unsigned num_templates
;
18494 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
18495 /* Assume no nested-name-specifier will be present. */
18496 *nested_name_specifier_p
= false;
18497 /* Assume no template parameter lists will be used in defining the
18500 parser
->colon_corrects_to_scope_p
= false;
18502 /* Look for the class-key. */
18503 class_key
= cp_parser_class_key (parser
);
18504 if (class_key
== none_type
)
18505 return error_mark_node
;
18507 /* Parse the attributes. */
18508 attributes
= cp_parser_attributes_opt (parser
);
18510 /* If the next token is `::', that is invalid -- but sometimes
18511 people do try to write:
18515 Handle this gracefully by accepting the extra qualifier, and then
18516 issuing an error about it later if this really is a
18517 class-head. If it turns out just to be an elaborated type
18518 specifier, remain silent. */
18519 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
18520 qualified_p
= true;
18522 push_deferring_access_checks (dk_no_check
);
18524 /* Determine the name of the class. Begin by looking for an
18525 optional nested-name-specifier. */
18526 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
18527 nested_name_specifier
18528 = cp_parser_nested_name_specifier_opt (parser
,
18529 /*typename_keyword_p=*/false,
18530 /*check_dependency_p=*/false,
18532 /*is_declaration=*/false);
18533 /* If there was a nested-name-specifier, then there *must* be an
18535 if (nested_name_specifier
)
18537 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18538 /* Although the grammar says `identifier', it really means
18539 `class-name' or `template-name'. You are only allowed to
18540 define a class that has already been declared with this
18543 The proposed resolution for Core Issue 180 says that wherever
18544 you see `class T::X' you should treat `X' as a type-name.
18546 It is OK to define an inaccessible class; for example:
18548 class A { class B; };
18551 We do not know if we will see a class-name, or a
18552 template-name. We look for a class-name first, in case the
18553 class-name is a template-id; if we looked for the
18554 template-name first we would stop after the template-name. */
18555 cp_parser_parse_tentatively (parser
);
18556 type
= cp_parser_class_name (parser
,
18557 /*typename_keyword_p=*/false,
18558 /*template_keyword_p=*/false,
18560 /*check_dependency_p=*/false,
18561 /*class_head_p=*/true,
18562 /*is_declaration=*/false);
18563 /* If that didn't work, ignore the nested-name-specifier. */
18564 if (!cp_parser_parse_definitely (parser
))
18566 invalid_nested_name_p
= true;
18567 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18568 id
= cp_parser_identifier (parser
);
18569 if (id
== error_mark_node
)
18572 /* If we could not find a corresponding TYPE, treat this
18573 declaration like an unqualified declaration. */
18574 if (type
== error_mark_node
)
18575 nested_name_specifier
= NULL_TREE
;
18576 /* Otherwise, count the number of templates used in TYPE and its
18577 containing scopes. */
18582 for (scope
= TREE_TYPE (type
);
18583 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
18584 scope
= (TYPE_P (scope
)
18585 ? TYPE_CONTEXT (scope
)
18586 : DECL_CONTEXT (scope
)))
18588 && CLASS_TYPE_P (scope
)
18589 && CLASSTYPE_TEMPLATE_INFO (scope
)
18590 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
18591 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
18595 /* Otherwise, the identifier is optional. */
18598 /* We don't know whether what comes next is a template-id,
18599 an identifier, or nothing at all. */
18600 cp_parser_parse_tentatively (parser
);
18601 /* Check for a template-id. */
18602 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18603 id
= cp_parser_template_id (parser
,
18604 /*template_keyword_p=*/false,
18605 /*check_dependency_p=*/true,
18607 /*is_declaration=*/true);
18608 /* If that didn't work, it could still be an identifier. */
18609 if (!cp_parser_parse_definitely (parser
))
18611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18613 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18614 id
= cp_parser_identifier (parser
);
18621 template_id_p
= true;
18626 pop_deferring_access_checks ();
18630 cp_parser_check_for_invalid_template_id (parser
, id
,
18632 type_start_token
->location
);
18634 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
18636 /* If it's not a `:' or a `{' then we can't really be looking at a
18637 class-head, since a class-head only appears as part of a
18638 class-specifier. We have to detect this situation before calling
18639 xref_tag, since that has irreversible side-effects. */
18640 if (!cp_parser_next_token_starts_class_definition_p (parser
))
18642 cp_parser_error (parser
, "expected %<{%> or %<:%>");
18643 type
= error_mark_node
;
18647 /* At this point, we're going ahead with the class-specifier, even
18648 if some other problem occurs. */
18649 cp_parser_commit_to_tentative_parse (parser
);
18650 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
18652 cp_parser_error (parser
,
18653 "cannot specify %<override%> for a class");
18654 type
= error_mark_node
;
18657 /* Issue the error about the overly-qualified name now. */
18660 cp_parser_error (parser
,
18661 "global qualification of class name is invalid");
18662 type
= error_mark_node
;
18665 else if (invalid_nested_name_p
)
18667 cp_parser_error (parser
,
18668 "qualified name does not name a class");
18669 type
= error_mark_node
;
18672 else if (nested_name_specifier
)
18676 /* Reject typedef-names in class heads. */
18677 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
18679 error_at (type_start_token
->location
,
18680 "invalid class name in declaration of %qD",
18686 /* Figure out in what scope the declaration is being placed. */
18687 scope
= current_scope ();
18688 /* If that scope does not contain the scope in which the
18689 class was originally declared, the program is invalid. */
18690 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
18692 if (at_namespace_scope_p ())
18693 error_at (type_start_token
->location
,
18694 "declaration of %qD in namespace %qD which does not "
18696 type
, scope
, nested_name_specifier
);
18698 error_at (type_start_token
->location
,
18699 "declaration of %qD in %qD which does not enclose %qD",
18700 type
, scope
, nested_name_specifier
);
18706 A declarator-id shall not be qualified except for the
18707 definition of a ... nested class outside of its class
18708 ... [or] the definition or explicit instantiation of a
18709 class member of a namespace outside of its namespace. */
18710 if (scope
== nested_name_specifier
)
18712 permerror (nested_name_specifier_token_start
->location
,
18713 "extra qualification not allowed");
18714 nested_name_specifier
= NULL_TREE
;
18718 /* An explicit-specialization must be preceded by "template <>". If
18719 it is not, try to recover gracefully. */
18720 if (at_namespace_scope_p ()
18721 && parser
->num_template_parameter_lists
== 0
18724 error_at (type_start_token
->location
,
18725 "an explicit specialization must be preceded by %<template <>%>");
18726 invalid_explicit_specialization_p
= true;
18727 /* Take the same action that would have been taken by
18728 cp_parser_explicit_specialization. */
18729 ++parser
->num_template_parameter_lists
;
18730 begin_specialization ();
18732 /* There must be no "return" statements between this point and the
18733 end of this function; set "type "to the correct return value and
18734 use "goto done;" to return. */
18735 /* Make sure that the right number of template parameters were
18737 if (!cp_parser_check_template_parameters (parser
, num_templates
,
18738 type_start_token
->location
,
18739 /*declarator=*/NULL
))
18741 /* If something went wrong, there is no point in even trying to
18742 process the class-definition. */
18747 /* Look up the type. */
18750 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
18751 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
18752 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
18754 error_at (type_start_token
->location
,
18755 "function template %qD redeclared as a class template", id
);
18756 type
= error_mark_node
;
18760 type
= TREE_TYPE (id
);
18761 type
= maybe_process_partial_specialization (type
);
18763 if (nested_name_specifier
)
18764 pushed_scope
= push_scope (nested_name_specifier
);
18766 else if (nested_name_specifier
)
18772 template <typename T> struct S { struct T };
18773 template <typename T> struct S<T>::T { };
18775 we will get a TYPENAME_TYPE when processing the definition of
18776 `S::T'. We need to resolve it to the actual type before we
18777 try to define it. */
18778 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
18780 class_type
= resolve_typename_type (TREE_TYPE (type
),
18781 /*only_current_p=*/false);
18782 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
18783 type
= TYPE_NAME (class_type
);
18786 cp_parser_error (parser
, "could not resolve typename type");
18787 type
= error_mark_node
;
18791 if (maybe_process_partial_specialization (TREE_TYPE (type
))
18792 == error_mark_node
)
18798 class_type
= current_class_type
;
18799 /* Enter the scope indicated by the nested-name-specifier. */
18800 pushed_scope
= push_scope (nested_name_specifier
);
18801 /* Get the canonical version of this type. */
18802 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
18803 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18804 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
18806 type
= push_template_decl (type
);
18807 if (type
== error_mark_node
)
18814 type
= TREE_TYPE (type
);
18815 *nested_name_specifier_p
= true;
18817 else /* The name is not a nested name. */
18819 /* If the class was unnamed, create a dummy name. */
18821 id
= make_anon_name ();
18822 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
18823 parser
->num_template_parameter_lists
);
18826 /* Indicate whether this class was declared as a `class' or as a
18828 if (TREE_CODE (type
) == RECORD_TYPE
)
18829 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
18830 cp_parser_check_class_key (class_key
, type
);
18832 /* If this type was already complete, and we see another definition,
18833 that's an error. */
18834 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
18836 error_at (type_start_token
->location
, "redefinition of %q#T",
18838 error_at (type_start_token
->location
, "previous definition of %q+#T",
18843 else if (type
== error_mark_node
)
18848 /* Apply attributes now, before any use of the class as a template
18849 argument in its base list. */
18850 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
18851 fixup_attribute_variants (type
);
18854 /* We will have entered the scope containing the class; the names of
18855 base classes should be looked up in that context. For example:
18857 struct A { struct B {}; struct C; };
18858 struct A::C : B {};
18862 /* Get the list of base-classes, if there is one. */
18863 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18864 bases
= cp_parser_base_clause (parser
);
18868 /* If we're really defining a class, process the base classes.
18869 If they're invalid, fail. */
18870 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
18871 && !xref_basetypes (type
, bases
))
18875 /* Leave the scope given by the nested-name-specifier. We will
18876 enter the class scope itself while processing the members. */
18878 pop_scope (pushed_scope
);
18880 if (invalid_explicit_specialization_p
)
18882 end_specialization ();
18883 --parser
->num_template_parameter_lists
;
18887 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18888 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
18889 CLASSTYPE_FINAL (type
) = 1;
18891 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18895 /* Parse a class-key.
18902 Returns the kind of class-key specified, or none_type to indicate
18905 static enum tag_types
18906 cp_parser_class_key (cp_parser
* parser
)
18909 enum tag_types tag_type
;
18911 /* Look for the class-key. */
18912 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
18916 /* Check to see if the TOKEN is a class-key. */
18917 tag_type
= cp_parser_token_is_class_key (token
);
18919 cp_parser_error (parser
, "expected class-key");
18923 /* Parse an (optional) member-specification.
18925 member-specification:
18926 member-declaration member-specification [opt]
18927 access-specifier : member-specification [opt] */
18930 cp_parser_member_specification_opt (cp_parser
* parser
)
18937 /* Peek at the next token. */
18938 token
= cp_lexer_peek_token (parser
->lexer
);
18939 /* If it's a `}', or EOF then we've seen all the members. */
18940 if (token
->type
== CPP_CLOSE_BRACE
18941 || token
->type
== CPP_EOF
18942 || token
->type
== CPP_PRAGMA_EOL
)
18945 /* See if this token is a keyword. */
18946 keyword
= token
->keyword
;
18950 case RID_PROTECTED
:
18952 /* Consume the access-specifier. */
18953 cp_lexer_consume_token (parser
->lexer
);
18954 /* Remember which access-specifier is active. */
18955 current_access_specifier
= token
->u
.value
;
18956 /* Look for the `:'. */
18957 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
18961 /* Accept #pragmas at class scope. */
18962 if (token
->type
== CPP_PRAGMA
)
18964 cp_parser_pragma (parser
, pragma_external
);
18968 /* Otherwise, the next construction must be a
18969 member-declaration. */
18970 cp_parser_member_declaration (parser
);
18975 /* Parse a member-declaration.
18977 member-declaration:
18978 decl-specifier-seq [opt] member-declarator-list [opt] ;
18979 function-definition ; [opt]
18980 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18982 template-declaration
18985 member-declarator-list:
18987 member-declarator-list , member-declarator
18990 declarator pure-specifier [opt]
18991 declarator constant-initializer [opt]
18992 identifier [opt] : constant-expression
18996 member-declaration:
18997 __extension__ member-declaration
19000 declarator attributes [opt] pure-specifier [opt]
19001 declarator attributes [opt] constant-initializer [opt]
19002 identifier [opt] attributes [opt] : constant-expression
19006 member-declaration:
19007 static_assert-declaration */
19010 cp_parser_member_declaration (cp_parser
* parser
)
19012 cp_decl_specifier_seq decl_specifiers
;
19013 tree prefix_attributes
;
19015 int declares_class_or_enum
;
19017 cp_token
*token
= NULL
;
19018 cp_token
*decl_spec_token_start
= NULL
;
19019 cp_token
*initializer_token_start
= NULL
;
19020 int saved_pedantic
;
19021 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
19023 /* Check for the `__extension__' keyword. */
19024 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
19027 cp_parser_member_declaration (parser
);
19028 /* Restore the old value of the PEDANTIC flag. */
19029 pedantic
= saved_pedantic
;
19034 /* Check for a template-declaration. */
19035 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
19037 /* An explicit specialization here is an error condition, and we
19038 expect the specialization handler to detect and report this. */
19039 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
19040 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
19041 cp_parser_explicit_specialization (parser
);
19043 cp_parser_template_declaration (parser
, /*member_p=*/true);
19048 /* Check for a using-declaration. */
19049 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
19051 if (cxx_dialect
< cxx0x
)
19053 /* Parse the using-declaration. */
19054 cp_parser_using_declaration (parser
,
19055 /*access_declaration_p=*/false);
19061 cp_parser_parse_tentatively (parser
);
19062 decl
= cp_parser_alias_declaration (parser
);
19063 if (cp_parser_parse_definitely (parser
))
19064 finish_member_declaration (decl
);
19066 cp_parser_using_declaration (parser
,
19067 /*access_declaration_p=*/false);
19072 /* Check for @defs. */
19073 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
19076 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
19077 ivar
= ivar_chains
;
19081 ivar
= TREE_CHAIN (member
);
19082 TREE_CHAIN (member
) = NULL_TREE
;
19083 finish_member_declaration (member
);
19088 /* If the next token is `static_assert' we have a static assertion. */
19089 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
19091 cp_parser_static_assert (parser
, /*member_p=*/true);
19095 parser
->colon_corrects_to_scope_p
= false;
19097 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
19100 /* Parse the decl-specifier-seq. */
19101 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
19102 cp_parser_decl_specifier_seq (parser
,
19103 CP_PARSER_FLAGS_OPTIONAL
,
19105 &declares_class_or_enum
);
19106 /* Check for an invalid type-name. */
19107 if (!decl_specifiers
.any_type_specifiers_p
19108 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19110 /* If there is no declarator, then the decl-specifier-seq should
19112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19114 /* If there was no decl-specifier-seq, and the next token is a
19115 `;', then we have something like:
19121 Each member-declaration shall declare at least one member
19122 name of the class. */
19123 if (!decl_specifiers
.any_specifiers_p
)
19125 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19126 if (!in_system_header_at (token
->location
))
19127 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
19133 /* See if this declaration is a friend. */
19134 friend_p
= cp_parser_friend_p (&decl_specifiers
);
19135 /* If there were decl-specifiers, check to see if there was
19136 a class-declaration. */
19137 type
= check_tag_decl (&decl_specifiers
,
19138 /*explicit_type_instantiation_p=*/false);
19139 /* Nested classes have already been added to the class, but
19140 a `friend' needs to be explicitly registered. */
19143 /* If the `friend' keyword was present, the friend must
19144 be introduced with a class-key. */
19145 if (!declares_class_or_enum
&& cxx_dialect
< cxx0x
)
19146 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
19147 "in C++03 a class-key must be used "
19148 "when declaring a friend");
19151 template <typename T> struct A {
19152 friend struct A<T>::B;
19155 A<T>::B will be represented by a TYPENAME_TYPE, and
19156 therefore not recognized by check_tag_decl. */
19159 type
= decl_specifiers
.type
;
19160 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
19161 type
= TREE_TYPE (type
);
19163 if (!type
|| !TYPE_P (type
))
19164 error_at (decl_spec_token_start
->location
,
19165 "friend declaration does not name a class or "
19168 make_friend_class (current_class_type
, type
,
19169 /*complain=*/true);
19171 /* If there is no TYPE, an error message will already have
19173 else if (!type
|| type
== error_mark_node
)
19175 /* An anonymous aggregate has to be handled specially; such
19176 a declaration really declares a data member (with a
19177 particular type), as opposed to a nested class. */
19178 else if (ANON_AGGR_TYPE_P (type
))
19181 if (decl_specifiers
.storage_class
!= sc_none
)
19182 error_at (decl_spec_token_start
->location
,
19183 "a storage class on an anonymous aggregate "
19184 "in class scope is not allowed");
19186 /* Remove constructors and such from TYPE, now that we
19187 know it is an anonymous aggregate. */
19188 fixup_anonymous_aggr (type
);
19189 /* And make the corresponding data member. */
19190 decl
= build_decl (decl_spec_token_start
->location
,
19191 FIELD_DECL
, NULL_TREE
, type
);
19192 /* Add it to the class. */
19193 finish_member_declaration (decl
);
19196 cp_parser_check_access_in_redeclaration
19198 decl_spec_token_start
->location
);
19203 bool assume_semicolon
= false;
19205 /* Clear attributes from the decl_specifiers but keep them
19206 around as prefix attributes that apply them to the entity
19208 prefix_attributes
= decl_specifiers
.attributes
;
19209 decl_specifiers
.attributes
= NULL_TREE
;
19211 /* See if these declarations will be friends. */
19212 friend_p
= cp_parser_friend_p (&decl_specifiers
);
19214 /* Keep going until we hit the `;' at the end of the
19216 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19218 tree attributes
= NULL_TREE
;
19219 tree first_attribute
;
19221 /* Peek at the next token. */
19222 token
= cp_lexer_peek_token (parser
->lexer
);
19224 /* Check for a bitfield declaration. */
19225 if (token
->type
== CPP_COLON
19226 || (token
->type
== CPP_NAME
19227 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
19233 /* Get the name of the bitfield. Note that we cannot just
19234 check TOKEN here because it may have been invalidated by
19235 the call to cp_lexer_peek_nth_token above. */
19236 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
19237 identifier
= cp_parser_identifier (parser
);
19239 identifier
= NULL_TREE
;
19241 /* Consume the `:' token. */
19242 cp_lexer_consume_token (parser
->lexer
);
19243 /* Get the width of the bitfield. */
19245 = cp_parser_constant_expression (parser
,
19246 /*allow_non_constant=*/false,
19249 /* Look for attributes that apply to the bitfield. */
19250 attributes
= cp_parser_attributes_opt (parser
);
19251 /* Remember which attributes are prefix attributes and
19253 first_attribute
= attributes
;
19254 /* Combine the attributes. */
19255 attributes
= chainon (prefix_attributes
, attributes
);
19257 /* Create the bitfield declaration. */
19258 decl
= grokbitfield (identifier
19259 ? make_id_declarator (NULL_TREE
,
19269 cp_declarator
*declarator
;
19271 tree asm_specification
;
19272 int ctor_dtor_or_conv_p
;
19274 /* Parse the declarator. */
19276 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19277 &ctor_dtor_or_conv_p
,
19278 /*parenthesized_p=*/NULL
,
19279 /*member_p=*/true);
19281 /* If something went wrong parsing the declarator, make sure
19282 that we at least consume some tokens. */
19283 if (declarator
== cp_error_declarator
)
19285 /* Skip to the end of the statement. */
19286 cp_parser_skip_to_end_of_statement (parser
);
19287 /* If the next token is not a semicolon, that is
19288 probably because we just skipped over the body of
19289 a function. So, we consume a semicolon if
19290 present, but do not issue an error message if it
19292 if (cp_lexer_next_token_is (parser
->lexer
,
19294 cp_lexer_consume_token (parser
->lexer
);
19298 if (declares_class_or_enum
& 2)
19299 cp_parser_check_for_definition_in_return_type
19300 (declarator
, decl_specifiers
.type
,
19301 decl_specifiers
.locations
[ds_type_spec
]);
19303 /* Look for an asm-specification. */
19304 asm_specification
= cp_parser_asm_specification_opt (parser
);
19305 /* Look for attributes that apply to the declaration. */
19306 attributes
= cp_parser_attributes_opt (parser
);
19307 /* Remember which attributes are prefix attributes and
19309 first_attribute
= attributes
;
19310 /* Combine the attributes. */
19311 attributes
= chainon (prefix_attributes
, attributes
);
19313 /* If it's an `=', then we have a constant-initializer or a
19314 pure-specifier. It is not correct to parse the
19315 initializer before registering the member declaration
19316 since the member declaration should be in scope while
19317 its initializer is processed. However, the rest of the
19318 front end does not yet provide an interface that allows
19319 us to handle this correctly. */
19320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19324 A pure-specifier shall be used only in the declaration of
19325 a virtual function.
19327 A member-declarator can contain a constant-initializer
19328 only if it declares a static member of integral or
19331 Therefore, if the DECLARATOR is for a function, we look
19332 for a pure-specifier; otherwise, we look for a
19333 constant-initializer. When we call `grokfield', it will
19334 perform more stringent semantics checks. */
19335 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
19336 if (function_declarator_p (declarator
)
19337 || (decl_specifiers
.type
19338 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
19339 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
19340 == FUNCTION_TYPE
)))
19341 initializer
= cp_parser_pure_specifier (parser
);
19342 else if (decl_specifiers
.storage_class
!= sc_static
)
19343 initializer
= cp_parser_save_nsdmi (parser
);
19344 else if (cxx_dialect
>= cxx0x
)
19347 /* Don't require a constant rvalue in C++11, since we
19348 might want a reference constant. We'll enforce
19349 constancy later. */
19350 cp_lexer_consume_token (parser
->lexer
);
19351 /* Parse the initializer. */
19352 initializer
= cp_parser_initializer_clause (parser
,
19356 /* Parse the initializer. */
19357 initializer
= cp_parser_constant_initializer (parser
);
19359 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
19360 && !function_declarator_p (declarator
))
19363 if (decl_specifiers
.storage_class
!= sc_static
)
19364 initializer
= cp_parser_save_nsdmi (parser
);
19366 initializer
= cp_parser_initializer (parser
, &x
, &x
);
19368 /* Otherwise, there is no initializer. */
19370 initializer
= NULL_TREE
;
19372 /* See if we are probably looking at a function
19373 definition. We are certainly not looking at a
19374 member-declarator. Calling `grokfield' has
19375 side-effects, so we must not do it unless we are sure
19376 that we are looking at a member-declarator. */
19377 if (cp_parser_token_starts_function_definition_p
19378 (cp_lexer_peek_token (parser
->lexer
)))
19380 /* The grammar does not allow a pure-specifier to be
19381 used when a member function is defined. (It is
19382 possible that this fact is an oversight in the
19383 standard, since a pure function may be defined
19384 outside of the class-specifier. */
19385 if (initializer
&& initializer_token_start
)
19386 error_at (initializer_token_start
->location
,
19387 "pure-specifier on function-definition");
19388 decl
= cp_parser_save_member_function_body (parser
,
19392 /* If the member was not a friend, declare it here. */
19394 finish_member_declaration (decl
);
19395 /* Peek at the next token. */
19396 token
= cp_lexer_peek_token (parser
->lexer
);
19397 /* If the next token is a semicolon, consume it. */
19398 if (token
->type
== CPP_SEMICOLON
)
19399 cp_lexer_consume_token (parser
->lexer
);
19403 if (declarator
->kind
== cdk_function
)
19404 declarator
->id_loc
= token
->location
;
19405 /* Create the declaration. */
19406 decl
= grokfield (declarator
, &decl_specifiers
,
19407 initializer
, /*init_const_expr_p=*/true,
19412 /* Reset PREFIX_ATTRIBUTES. */
19413 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
19414 attributes
= TREE_CHAIN (attributes
);
19416 TREE_CHAIN (attributes
) = NULL_TREE
;
19418 /* If there is any qualification still in effect, clear it
19419 now; we will be starting fresh with the next declarator. */
19420 parser
->scope
= NULL_TREE
;
19421 parser
->qualifying_scope
= NULL_TREE
;
19422 parser
->object_scope
= NULL_TREE
;
19423 /* If it's a `,', then there are more declarators. */
19424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19425 cp_lexer_consume_token (parser
->lexer
);
19426 /* If the next token isn't a `;', then we have a parse error. */
19427 else if (cp_lexer_next_token_is_not (parser
->lexer
,
19430 /* The next token might be a ways away from where the
19431 actual semicolon is missing. Find the previous token
19432 and use that for our error position. */
19433 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
19434 error_at (token
->location
,
19435 "expected %<;%> at end of member declaration");
19437 /* Assume that the user meant to provide a semicolon. If
19438 we were to cp_parser_skip_to_end_of_statement, we might
19439 skip to a semicolon inside a member function definition
19440 and issue nonsensical error messages. */
19441 assume_semicolon
= true;
19446 /* Add DECL to the list of members. */
19448 finish_member_declaration (decl
);
19450 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19451 cp_parser_save_default_args (parser
, decl
);
19452 else if (TREE_CODE (decl
) == FIELD_DECL
19453 && !DECL_C_BIT_FIELD (decl
)
19454 && DECL_INITIAL (decl
))
19455 /* Add DECL to the queue of NSDMI to be parsed later. */
19456 VEC_safe_push (tree
, gc
, unparsed_nsdmis
, decl
);
19459 if (assume_semicolon
)
19464 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19466 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19469 /* Parse a pure-specifier.
19474 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19475 Otherwise, ERROR_MARK_NODE is returned. */
19478 cp_parser_pure_specifier (cp_parser
* parser
)
19482 /* Look for the `=' token. */
19483 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
19484 return error_mark_node
;
19485 /* Look for the `0' token. */
19486 token
= cp_lexer_peek_token (parser
->lexer
);
19488 if (token
->type
== CPP_EOF
19489 || token
->type
== CPP_PRAGMA_EOL
)
19490 return error_mark_node
;
19492 cp_lexer_consume_token (parser
->lexer
);
19494 /* Accept = default or = delete in c++0x mode. */
19495 if (token
->keyword
== RID_DEFAULT
19496 || token
->keyword
== RID_DELETE
)
19498 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
19499 return token
->u
.value
;
19502 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19503 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
19505 cp_parser_error (parser
,
19506 "invalid pure specifier (only %<= 0%> is allowed)");
19507 cp_parser_skip_to_end_of_statement (parser
);
19508 return error_mark_node
;
19510 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19512 error_at (token
->location
, "templates may not be %<virtual%>");
19513 return error_mark_node
;
19516 return integer_zero_node
;
19519 /* Parse a constant-initializer.
19521 constant-initializer:
19522 = constant-expression
19524 Returns a representation of the constant-expression. */
19527 cp_parser_constant_initializer (cp_parser
* parser
)
19529 /* Look for the `=' token. */
19530 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
19531 return error_mark_node
;
19533 /* It is invalid to write:
19535 struct S { static const int i = { 7 }; };
19538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19540 cp_parser_error (parser
,
19541 "a brace-enclosed initializer is not allowed here");
19542 /* Consume the opening brace. */
19543 cp_lexer_consume_token (parser
->lexer
);
19544 /* Skip the initializer. */
19545 cp_parser_skip_to_closing_brace (parser
);
19546 /* Look for the trailing `}'. */
19547 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19549 return error_mark_node
;
19552 return cp_parser_constant_expression (parser
,
19553 /*allow_non_constant=*/false,
19557 /* Derived classes [gram.class.derived] */
19559 /* Parse a base-clause.
19562 : base-specifier-list
19564 base-specifier-list:
19565 base-specifier ... [opt]
19566 base-specifier-list , base-specifier ... [opt]
19568 Returns a TREE_LIST representing the base-classes, in the order in
19569 which they were declared. The representation of each node is as
19570 described by cp_parser_base_specifier.
19572 In the case that no bases are specified, this function will return
19573 NULL_TREE, not ERROR_MARK_NODE. */
19576 cp_parser_base_clause (cp_parser
* parser
)
19578 tree bases
= NULL_TREE
;
19580 /* Look for the `:' that begins the list. */
19581 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
19583 /* Scan the base-specifier-list. */
19588 bool pack_expansion_p
= false;
19590 /* Look for the base-specifier. */
19591 base
= cp_parser_base_specifier (parser
);
19592 /* Look for the (optional) ellipsis. */
19593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19595 /* Consume the `...'. */
19596 cp_lexer_consume_token (parser
->lexer
);
19598 pack_expansion_p
= true;
19601 /* Add BASE to the front of the list. */
19602 if (base
&& base
!= error_mark_node
)
19604 if (pack_expansion_p
)
19605 /* Make this a pack expansion type. */
19606 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
19608 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
19610 TREE_CHAIN (base
) = bases
;
19614 /* Peek at the next token. */
19615 token
= cp_lexer_peek_token (parser
->lexer
);
19616 /* If it's not a comma, then the list is complete. */
19617 if (token
->type
!= CPP_COMMA
)
19619 /* Consume the `,'. */
19620 cp_lexer_consume_token (parser
->lexer
);
19623 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19624 base class had a qualified name. However, the next name that
19625 appears is certainly not qualified. */
19626 parser
->scope
= NULL_TREE
;
19627 parser
->qualifying_scope
= NULL_TREE
;
19628 parser
->object_scope
= NULL_TREE
;
19630 return nreverse (bases
);
19633 /* Parse a base-specifier.
19636 :: [opt] nested-name-specifier [opt] class-name
19637 virtual access-specifier [opt] :: [opt] nested-name-specifier
19639 access-specifier virtual [opt] :: [opt] nested-name-specifier
19642 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19643 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19644 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19645 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19648 cp_parser_base_specifier (cp_parser
* parser
)
19652 bool virtual_p
= false;
19653 bool duplicate_virtual_error_issued_p
= false;
19654 bool duplicate_access_error_issued_p
= false;
19655 bool class_scope_p
, template_p
;
19656 tree access
= access_default_node
;
19659 /* Process the optional `virtual' and `access-specifier'. */
19662 /* Peek at the next token. */
19663 token
= cp_lexer_peek_token (parser
->lexer
);
19664 /* Process `virtual'. */
19665 switch (token
->keyword
)
19668 /* If `virtual' appears more than once, issue an error. */
19669 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
19671 cp_parser_error (parser
,
19672 "%<virtual%> specified more than once in base-specified");
19673 duplicate_virtual_error_issued_p
= true;
19678 /* Consume the `virtual' token. */
19679 cp_lexer_consume_token (parser
->lexer
);
19684 case RID_PROTECTED
:
19686 /* If more than one access specifier appears, issue an
19688 if (access
!= access_default_node
19689 && !duplicate_access_error_issued_p
)
19691 cp_parser_error (parser
,
19692 "more than one access specifier in base-specified");
19693 duplicate_access_error_issued_p
= true;
19696 access
= ridpointers
[(int) token
->keyword
];
19698 /* Consume the access-specifier. */
19699 cp_lexer_consume_token (parser
->lexer
);
19708 /* It is not uncommon to see programs mechanically, erroneously, use
19709 the 'typename' keyword to denote (dependent) qualified types
19710 as base classes. */
19711 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
19713 token
= cp_lexer_peek_token (parser
->lexer
);
19714 if (!processing_template_decl
)
19715 error_at (token
->location
,
19716 "keyword %<typename%> not allowed outside of templates");
19718 error_at (token
->location
,
19719 "keyword %<typename%> not allowed in this context "
19720 "(the base class is implicitly a type)");
19721 cp_lexer_consume_token (parser
->lexer
);
19724 /* Look for the optional `::' operator. */
19725 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19726 /* Look for the nested-name-specifier. The simplest way to
19731 The keyword `typename' is not permitted in a base-specifier or
19732 mem-initializer; in these contexts a qualified name that
19733 depends on a template-parameter is implicitly assumed to be a
19736 is to pretend that we have seen the `typename' keyword at this
19738 cp_parser_nested_name_specifier_opt (parser
,
19739 /*typename_keyword_p=*/true,
19740 /*check_dependency_p=*/true,
19742 /*is_declaration=*/true);
19743 /* If the base class is given by a qualified name, assume that names
19744 we see are type names or templates, as appropriate. */
19745 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
19746 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
19749 && cp_lexer_next_token_is_decltype (parser
->lexer
))
19750 /* DR 950 allows decltype as a base-specifier. */
19751 type
= cp_parser_decltype (parser
);
19754 /* Otherwise, look for the class-name. */
19755 type
= cp_parser_class_name (parser
,
19759 /*check_dependency_p=*/true,
19760 /*class_head_p=*/false,
19761 /*is_declaration=*/true);
19762 type
= TREE_TYPE (type
);
19765 if (type
== error_mark_node
)
19766 return error_mark_node
;
19768 return finish_base_specifier (type
, access
, virtual_p
);
19771 /* Exception handling [gram.exception] */
19773 /* Parse an (optional) noexcept-specification.
19775 noexcept-specification:
19776 noexcept ( constant-expression ) [opt]
19778 If no noexcept-specification is present, returns NULL_TREE.
19779 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19780 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19781 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19782 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19783 in which case a boolean condition is returned instead. */
19786 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
19787 bool require_constexpr
,
19788 bool* consumed_expr
,
19792 const char *saved_message
;
19794 /* Peek at the next token. */
19795 token
= cp_lexer_peek_token (parser
->lexer
);
19797 /* Is it a noexcept-specification? */
19798 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
19801 cp_lexer_consume_token (parser
->lexer
);
19803 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
19805 cp_lexer_consume_token (parser
->lexer
);
19807 if (require_constexpr
)
19809 /* Types may not be defined in an exception-specification. */
19810 saved_message
= parser
->type_definition_forbidden_message
;
19811 parser
->type_definition_forbidden_message
19812 = G_("types may not be defined in an exception-specification");
19814 expr
= cp_parser_constant_expression (parser
, false, NULL
);
19816 /* Restore the saved message. */
19817 parser
->type_definition_forbidden_message
= saved_message
;
19821 expr
= cp_parser_expression (parser
, false, NULL
);
19822 *consumed_expr
= true;
19825 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19829 expr
= boolean_true_node
;
19830 if (!require_constexpr
)
19831 *consumed_expr
= false;
19834 /* We cannot build a noexcept-spec right away because this will check
19835 that expr is a constexpr. */
19837 return build_noexcept_spec (expr
, tf_warning_or_error
);
19845 /* Parse an (optional) exception-specification.
19847 exception-specification:
19848 throw ( type-id-list [opt] )
19850 Returns a TREE_LIST representing the exception-specification. The
19851 TREE_VALUE of each node is a type. */
19854 cp_parser_exception_specification_opt (cp_parser
* parser
)
19858 const char *saved_message
;
19860 /* Peek at the next token. */
19861 token
= cp_lexer_peek_token (parser
->lexer
);
19863 /* Is it a noexcept-specification? */
19864 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
19866 if (type_id_list
!= NULL_TREE
)
19867 return type_id_list
;
19869 /* If it's not `throw', then there's no exception-specification. */
19870 if (!cp_parser_is_keyword (token
, RID_THROW
))
19874 /* Enable this once a lot of code has transitioned to noexcept? */
19875 if (cxx_dialect
>= cxx0x
&& !in_system_header
)
19876 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
19877 "deprecated in C++0x; use %<noexcept%> instead");
19880 /* Consume the `throw'. */
19881 cp_lexer_consume_token (parser
->lexer
);
19883 /* Look for the `('. */
19884 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
19886 /* Peek at the next token. */
19887 token
= cp_lexer_peek_token (parser
->lexer
);
19888 /* If it's not a `)', then there is a type-id-list. */
19889 if (token
->type
!= CPP_CLOSE_PAREN
)
19891 /* Types may not be defined in an exception-specification. */
19892 saved_message
= parser
->type_definition_forbidden_message
;
19893 parser
->type_definition_forbidden_message
19894 = G_("types may not be defined in an exception-specification");
19895 /* Parse the type-id-list. */
19896 type_id_list
= cp_parser_type_id_list (parser
);
19897 /* Restore the saved message. */
19898 parser
->type_definition_forbidden_message
= saved_message
;
19901 type_id_list
= empty_except_spec
;
19903 /* Look for the `)'. */
19904 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19906 return type_id_list
;
19909 /* Parse an (optional) type-id-list.
19913 type-id-list , type-id ... [opt]
19915 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19916 in the order that the types were presented. */
19919 cp_parser_type_id_list (cp_parser
* parser
)
19921 tree types
= NULL_TREE
;
19928 /* Get the next type-id. */
19929 type
= cp_parser_type_id (parser
);
19930 /* Parse the optional ellipsis. */
19931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19933 /* Consume the `...'. */
19934 cp_lexer_consume_token (parser
->lexer
);
19936 /* Turn the type into a pack expansion expression. */
19937 type
= make_pack_expansion (type
);
19939 /* Add it to the list. */
19940 types
= add_exception_specifier (types
, type
, /*complain=*/1);
19941 /* Peek at the next token. */
19942 token
= cp_lexer_peek_token (parser
->lexer
);
19943 /* If it is not a `,', we are done. */
19944 if (token
->type
!= CPP_COMMA
)
19946 /* Consume the `,'. */
19947 cp_lexer_consume_token (parser
->lexer
);
19950 return nreverse (types
);
19953 /* Parse a try-block.
19956 try compound-statement handler-seq */
19959 cp_parser_try_block (cp_parser
* parser
)
19963 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
19964 try_block
= begin_try_block ();
19965 cp_parser_compound_statement (parser
, NULL
, true, false);
19966 finish_try_block (try_block
);
19967 cp_parser_handler_seq (parser
);
19968 finish_handler_sequence (try_block
);
19973 /* Parse a function-try-block.
19975 function-try-block:
19976 try ctor-initializer [opt] function-body handler-seq */
19979 cp_parser_function_try_block (cp_parser
* parser
)
19981 tree compound_stmt
;
19983 bool ctor_initializer_p
;
19985 /* Look for the `try' keyword. */
19986 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
19988 /* Let the rest of the front end know where we are. */
19989 try_block
= begin_function_try_block (&compound_stmt
);
19990 /* Parse the function-body. */
19991 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
19992 (parser
, /*in_function_try_block=*/true);
19993 /* We're done with the `try' part. */
19994 finish_function_try_block (try_block
);
19995 /* Parse the handlers. */
19996 cp_parser_handler_seq (parser
);
19997 /* We're done with the handlers. */
19998 finish_function_handler_sequence (try_block
, compound_stmt
);
20000 return ctor_initializer_p
;
20003 /* Parse a handler-seq.
20006 handler handler-seq [opt] */
20009 cp_parser_handler_seq (cp_parser
* parser
)
20015 /* Parse the handler. */
20016 cp_parser_handler (parser
);
20017 /* Peek at the next token. */
20018 token
= cp_lexer_peek_token (parser
->lexer
);
20019 /* If it's not `catch' then there are no more handlers. */
20020 if (!cp_parser_is_keyword (token
, RID_CATCH
))
20025 /* Parse a handler.
20028 catch ( exception-declaration ) compound-statement */
20031 cp_parser_handler (cp_parser
* parser
)
20036 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
20037 handler
= begin_handler ();
20038 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20039 declaration
= cp_parser_exception_declaration (parser
);
20040 finish_handler_parms (declaration
, handler
);
20041 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20042 cp_parser_compound_statement (parser
, NULL
, false, false);
20043 finish_handler (handler
);
20046 /* Parse an exception-declaration.
20048 exception-declaration:
20049 type-specifier-seq declarator
20050 type-specifier-seq abstract-declarator
20054 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20055 ellipsis variant is used. */
20058 cp_parser_exception_declaration (cp_parser
* parser
)
20060 cp_decl_specifier_seq type_specifiers
;
20061 cp_declarator
*declarator
;
20062 const char *saved_message
;
20064 /* If it's an ellipsis, it's easy to handle. */
20065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20067 /* Consume the `...' token. */
20068 cp_lexer_consume_token (parser
->lexer
);
20072 /* Types may not be defined in exception-declarations. */
20073 saved_message
= parser
->type_definition_forbidden_message
;
20074 parser
->type_definition_forbidden_message
20075 = G_("types may not be defined in exception-declarations");
20077 /* Parse the type-specifier-seq. */
20078 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
20079 /*is_trailing_return=*/false,
20081 /* If it's a `)', then there is no declarator. */
20082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
20085 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
20086 /*ctor_dtor_or_conv_p=*/NULL
,
20087 /*parenthesized_p=*/NULL
,
20088 /*member_p=*/false);
20090 /* Restore the saved message. */
20091 parser
->type_definition_forbidden_message
= saved_message
;
20093 if (!type_specifiers
.any_specifiers_p
)
20094 return error_mark_node
;
20096 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
20099 /* Parse a throw-expression.
20102 throw assignment-expression [opt]
20104 Returns a THROW_EXPR representing the throw-expression. */
20107 cp_parser_throw_expression (cp_parser
* parser
)
20112 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
20113 token
= cp_lexer_peek_token (parser
->lexer
);
20114 /* Figure out whether or not there is an assignment-expression
20115 following the "throw" keyword. */
20116 if (token
->type
== CPP_COMMA
20117 || token
->type
== CPP_SEMICOLON
20118 || token
->type
== CPP_CLOSE_PAREN
20119 || token
->type
== CPP_CLOSE_SQUARE
20120 || token
->type
== CPP_CLOSE_BRACE
20121 || token
->type
== CPP_COLON
)
20122 expression
= NULL_TREE
;
20124 expression
= cp_parser_assignment_expression (parser
,
20125 /*cast_p=*/false, NULL
);
20127 return build_throw (expression
);
20130 /* GNU Extensions */
20132 /* Parse an (optional) asm-specification.
20135 asm ( string-literal )
20137 If the asm-specification is present, returns a STRING_CST
20138 corresponding to the string-literal. Otherwise, returns
20142 cp_parser_asm_specification_opt (cp_parser
* parser
)
20145 tree asm_specification
;
20147 /* Peek at the next token. */
20148 token
= cp_lexer_peek_token (parser
->lexer
);
20149 /* If the next token isn't the `asm' keyword, then there's no
20150 asm-specification. */
20151 if (!cp_parser_is_keyword (token
, RID_ASM
))
20154 /* Consume the `asm' token. */
20155 cp_lexer_consume_token (parser
->lexer
);
20156 /* Look for the `('. */
20157 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20159 /* Look for the string-literal. */
20160 asm_specification
= cp_parser_string_literal (parser
, false, false);
20162 /* Look for the `)'. */
20163 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20165 return asm_specification
;
20168 /* Parse an asm-operand-list.
20172 asm-operand-list , asm-operand
20175 string-literal ( expression )
20176 [ string-literal ] string-literal ( expression )
20178 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20179 each node is the expression. The TREE_PURPOSE is itself a
20180 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20181 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20182 is a STRING_CST for the string literal before the parenthesis. Returns
20183 ERROR_MARK_NODE if any of the operands are invalid. */
20186 cp_parser_asm_operand_list (cp_parser
* parser
)
20188 tree asm_operands
= NULL_TREE
;
20189 bool invalid_operands
= false;
20193 tree string_literal
;
20197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
20199 /* Consume the `[' token. */
20200 cp_lexer_consume_token (parser
->lexer
);
20201 /* Read the operand name. */
20202 name
= cp_parser_identifier (parser
);
20203 if (name
!= error_mark_node
)
20204 name
= build_string (IDENTIFIER_LENGTH (name
),
20205 IDENTIFIER_POINTER (name
));
20206 /* Look for the closing `]'. */
20207 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
20211 /* Look for the string-literal. */
20212 string_literal
= cp_parser_string_literal (parser
, false, false);
20214 /* Look for the `('. */
20215 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20216 /* Parse the expression. */
20217 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
20218 /* Look for the `)'. */
20219 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20221 if (name
== error_mark_node
20222 || string_literal
== error_mark_node
20223 || expression
== error_mark_node
)
20224 invalid_operands
= true;
20226 /* Add this operand to the list. */
20227 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
20230 /* If the next token is not a `,', there are no more
20232 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20234 /* Consume the `,'. */
20235 cp_lexer_consume_token (parser
->lexer
);
20238 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
20241 /* Parse an asm-clobber-list.
20245 asm-clobber-list , string-literal
20247 Returns a TREE_LIST, indicating the clobbers in the order that they
20248 appeared. The TREE_VALUE of each node is a STRING_CST. */
20251 cp_parser_asm_clobber_list (cp_parser
* parser
)
20253 tree clobbers
= NULL_TREE
;
20257 tree string_literal
;
20259 /* Look for the string literal. */
20260 string_literal
= cp_parser_string_literal (parser
, false, false);
20261 /* Add it to the list. */
20262 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
20263 /* If the next token is not a `,', then the list is
20265 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20267 /* Consume the `,' token. */
20268 cp_lexer_consume_token (parser
->lexer
);
20274 /* Parse an asm-label-list.
20278 asm-label-list , identifier
20280 Returns a TREE_LIST, indicating the labels in the order that they
20281 appeared. The TREE_VALUE of each node is a label. */
20284 cp_parser_asm_label_list (cp_parser
* parser
)
20286 tree labels
= NULL_TREE
;
20290 tree identifier
, label
, name
;
20292 /* Look for the identifier. */
20293 identifier
= cp_parser_identifier (parser
);
20294 if (!error_operand_p (identifier
))
20296 label
= lookup_label (identifier
);
20297 if (TREE_CODE (label
) == LABEL_DECL
)
20299 TREE_USED (label
) = 1;
20300 check_goto (label
);
20301 name
= build_string (IDENTIFIER_LENGTH (identifier
),
20302 IDENTIFIER_POINTER (identifier
));
20303 labels
= tree_cons (name
, label
, labels
);
20306 /* If the next token is not a `,', then the list is
20308 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20310 /* Consume the `,' token. */
20311 cp_lexer_consume_token (parser
->lexer
);
20314 return nreverse (labels
);
20317 /* Return TRUE iff the next tokens in the stream are possibly the
20318 beginning of a GNU extension attribute. */
20321 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
20323 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
20326 /* Return TRUE iff the next tokens in the stream are possibly the
20327 beginning of a standard C++-11 attribute. */
20330 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
20332 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
20335 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20336 beginning of a standard C++-11 attribute. */
20339 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
20341 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
20343 return (cxx_dialect
>= cxx0x
20344 && token
->type
== CPP_OPEN_SQUARE
20345 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
20346 && token
->type
== CPP_OPEN_SQUARE
);
20349 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20350 beginning of a GNU extension attribute. */
20353 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
20355 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
20357 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
20360 /* Return true iff the next tokens can be the beginning of either a
20361 GNU attribute list, or a standard C++11 attribute sequence. */
20364 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
20366 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
20367 || cp_next_tokens_can_be_std_attribute_p (parser
));
20370 /* Return true iff the next Nth tokens can be the beginning of either
20371 a GNU attribute list, or a standard C++11 attribute sequence. */
20374 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
20376 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
20377 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
20380 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20381 of GNU attributes, or return NULL. */
20384 cp_parser_attributes_opt (cp_parser
*parser
)
20386 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
20387 return cp_parser_gnu_attributes_opt (parser
);
20388 return cp_parser_std_attribute_spec_seq (parser
);
20391 /* Parse an (optional) series of attributes.
20394 attributes attribute
20397 __attribute__ (( attribute-list [opt] ))
20399 The return value is as for cp_parser_gnu_attribute_list. */
20402 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
20404 tree attributes
= NULL_TREE
;
20409 tree attribute_list
;
20412 /* Peek at the next token. */
20413 token
= cp_lexer_peek_token (parser
->lexer
);
20414 /* If it's not `__attribute__', then we're done. */
20415 if (token
->keyword
!= RID_ATTRIBUTE
)
20418 /* Consume the `__attribute__' keyword. */
20419 cp_lexer_consume_token (parser
->lexer
);
20420 /* Look for the two `(' tokens. */
20421 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20422 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20424 /* Peek at the next token. */
20425 token
= cp_lexer_peek_token (parser
->lexer
);
20426 if (token
->type
!= CPP_CLOSE_PAREN
)
20427 /* Parse the attribute-list. */
20428 attribute_list
= cp_parser_gnu_attribute_list (parser
);
20430 /* If the next token is a `)', then there is no attribute
20432 attribute_list
= NULL
;
20434 /* Look for the two `)' tokens. */
20435 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
20437 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
20440 cp_parser_skip_to_end_of_statement (parser
);
20442 /* Add these new attributes to the list. */
20443 attributes
= chainon (attributes
, attribute_list
);
20449 /* Parse a GNU attribute-list.
20453 attribute-list , attribute
20457 identifier ( identifier )
20458 identifier ( identifier , expression-list )
20459 identifier ( expression-list )
20461 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20462 to an attribute. The TREE_PURPOSE of each node is the identifier
20463 indicating which attribute is in use. The TREE_VALUE represents
20464 the arguments, if any. */
20467 cp_parser_gnu_attribute_list (cp_parser
* parser
)
20469 tree attribute_list
= NULL_TREE
;
20470 bool save_translate_strings_p
= parser
->translate_strings_p
;
20472 parser
->translate_strings_p
= false;
20479 /* Look for the identifier. We also allow keywords here; for
20480 example `__attribute__ ((const))' is legal. */
20481 token
= cp_lexer_peek_token (parser
->lexer
);
20482 if (token
->type
== CPP_NAME
20483 || token
->type
== CPP_KEYWORD
)
20485 tree arguments
= NULL_TREE
;
20487 /* Consume the token. */
20488 token
= cp_lexer_consume_token (parser
->lexer
);
20490 /* Save away the identifier that indicates which attribute
20492 identifier
= (token
->type
== CPP_KEYWORD
)
20493 /* For keywords, use the canonical spelling, not the
20494 parsed identifier. */
20495 ? ridpointers
[(int) token
->keyword
]
20498 attribute
= build_tree_list (identifier
, NULL_TREE
);
20500 /* Peek at the next token. */
20501 token
= cp_lexer_peek_token (parser
->lexer
);
20502 /* If it's an `(', then parse the attribute arguments. */
20503 if (token
->type
== CPP_OPEN_PAREN
)
20506 int attr_flag
= (attribute_takes_identifier_p (identifier
)
20507 ? id_attr
: normal_attr
);
20508 vec
= cp_parser_parenthesized_expression_list
20509 (parser
, attr_flag
, /*cast_p=*/false,
20510 /*allow_expansion_p=*/false,
20511 /*non_constant_p=*/NULL
);
20513 arguments
= error_mark_node
;
20516 arguments
= build_tree_list_vec (vec
);
20517 release_tree_vector (vec
);
20519 /* Save the arguments away. */
20520 TREE_VALUE (attribute
) = arguments
;
20523 if (arguments
!= error_mark_node
)
20525 /* Add this attribute to the list. */
20526 TREE_CHAIN (attribute
) = attribute_list
;
20527 attribute_list
= attribute
;
20530 token
= cp_lexer_peek_token (parser
->lexer
);
20532 /* Now, look for more attributes. If the next token isn't a
20533 `,', we're done. */
20534 if (token
->type
!= CPP_COMMA
)
20537 /* Consume the comma and keep going. */
20538 cp_lexer_consume_token (parser
->lexer
);
20540 parser
->translate_strings_p
= save_translate_strings_p
;
20542 /* We built up the list in reverse order. */
20543 return nreverse (attribute_list
);
20546 /* Parse a standard C++11 attribute.
20548 The returned representation is a TREE_LIST which TREE_PURPOSE is
20549 the scoped name of the attribute, and the TREE_VALUE is its
20552 Note that the scoped name of the attribute is itself a TREE_LIST
20553 which TREE_PURPOSE is the namespace of the attribute, and
20554 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20555 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20556 and which TREE_PURPOSE is directly the attribute name.
20558 Clients of the attribute code should use get_attribute_namespace
20559 and get_attribute_name to get the actual namespace and name of
20560 attributes, regardless of their being GNU or C++11 attributes.
20563 attribute-token attribute-argument-clause [opt]
20567 attribute-scoped-token
20569 attribute-scoped-token:
20570 attribute-namespace :: identifier
20572 attribute-namespace:
20575 attribute-argument-clause:
20576 ( balanced-token-seq )
20578 balanced-token-seq:
20579 balanced-token [opt]
20580 balanced-token-seq balanced-token
20583 ( balanced-token-seq )
20584 [ balanced-token-seq ]
20585 { balanced-token-seq }. */
20588 cp_parser_std_attribute (cp_parser
*parser
)
20590 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
20593 /* First, parse name of the the attribute, a.k.a
20594 attribute-token. */
20596 token
= cp_lexer_peek_token (parser
->lexer
);
20597 if (token
->type
== CPP_NAME
)
20598 attr_id
= token
->u
.value
;
20599 else if (token
->type
== CPP_KEYWORD
)
20600 attr_id
= ridpointers
[(int) token
->keyword
];
20601 else if (token
->flags
& NAMED_OP
)
20602 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
20604 if (attr_id
== NULL_TREE
)
20607 cp_lexer_consume_token (parser
->lexer
);
20609 token
= cp_lexer_peek_token (parser
->lexer
);
20610 if (token
->type
== CPP_SCOPE
)
20612 /* We are seeing a scoped attribute token. */
20614 cp_lexer_consume_token (parser
->lexer
);
20617 token
= cp_lexer_consume_token (parser
->lexer
);
20618 if (token
->type
== CPP_NAME
)
20619 attr_id
= token
->u
.value
;
20620 else if (token
->type
== CPP_KEYWORD
)
20621 attr_id
= ridpointers
[(int) token
->keyword
];
20624 error_at (token
->location
,
20625 "expected an identifier for the attribute name");
20626 return error_mark_node
;
20628 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
20630 token
= cp_lexer_peek_token (parser
->lexer
);
20633 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
20636 /* Now parse the optional argument clause of the attribute. */
20638 if (token
->type
!= CPP_OPEN_PAREN
)
20642 VEC(tree
, gc
) *vec
;
20643 int attr_flag
= normal_attr
;
20645 if (attr_ns
== get_identifier ("gnu")
20646 && attribute_takes_identifier_p (attr_id
))
20647 /* A GNU attribute that takes an identifier in parameter. */
20648 attr_flag
= id_attr
;
20650 vec
= cp_parser_parenthesized_expression_list
20651 (parser
, attr_flag
, /*cast_p=*/false,
20652 /*allow_expansion_p=*/true,
20653 /*non_constant_p=*/NULL
);
20655 arguments
= error_mark_node
;
20658 arguments
= build_tree_list_vec (vec
);
20659 release_tree_vector (vec
);
20662 if (arguments
== error_mark_node
)
20663 attribute
= error_mark_node
;
20665 TREE_VALUE (attribute
) = arguments
;
20671 /* Parse a list of standard C++-11 attributes.
20675 attribute-list , attribute[opt]
20677 attribute-list , attribute ...
20681 cp_parser_std_attribute_list (cp_parser
*parser
)
20683 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
20684 cp_token
*token
= NULL
;
20688 attribute
= cp_parser_std_attribute (parser
);
20689 if (attribute
== error_mark_node
)
20691 if (attribute
!= NULL_TREE
)
20693 TREE_CHAIN (attribute
) = attributes
;
20694 attributes
= attribute
;
20696 token
= cp_lexer_peek_token (parser
->lexer
);
20697 if (token
->type
!= CPP_COMMA
)
20699 cp_lexer_consume_token (parser
->lexer
);
20701 attributes
= nreverse (attributes
);
20705 /* Parse a standard C++-11 attribute specifier.
20707 attribute-specifier:
20708 [ [ attribute-list ] ]
20709 alignment-specifier
20711 alignment-specifier:
20712 alignas ( type-id ... [opt] )
20713 alignas ( alignment-expression ... [opt] ). */
20716 cp_parser_std_attribute_spec (cp_parser
*parser
)
20718 tree attributes
= NULL_TREE
;
20719 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20721 if (token
->type
== CPP_OPEN_SQUARE
20722 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
20724 cp_lexer_consume_token (parser
->lexer
);
20725 cp_lexer_consume_token (parser
->lexer
);
20727 attributes
= cp_parser_std_attribute_list (parser
);
20729 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
20730 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20731 cp_parser_skip_to_end_of_statement (parser
);
20733 /* Warn about parsing c++11 attribute in non-c++1 mode, only
20734 when we are sure that we have actually parsed them. */
20735 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
20741 /* Look for an alignment-specifier. */
20743 token
= cp_lexer_peek_token (parser
->lexer
);
20745 if (token
->type
!= CPP_KEYWORD
20746 || token
->keyword
!= RID_ALIGNAS
)
20749 cp_lexer_consume_token (parser
->lexer
);
20750 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
20752 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
20754 cp_parser_error (parser
, "expected %<(%>");
20755 return error_mark_node
;
20758 cp_parser_parse_tentatively (parser
);
20759 alignas_expr
= cp_parser_type_id (parser
);
20761 if (!cp_parser_parse_definitely (parser
))
20763 gcc_assert (alignas_expr
== error_mark_node
20764 || alignas_expr
== NULL_TREE
);
20767 cp_parser_assignment_expression (parser
, /*cast_p=*/false,
20768 /**cp_id_kind=*/NULL
);
20769 if (alignas_expr
== NULL_TREE
20770 || alignas_expr
== error_mark_node
)
20771 return alignas_expr
;
20774 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
20776 cp_parser_error (parser
, "expected %<)%>");
20777 return error_mark_node
;
20780 alignas_expr
= cxx_alignas_expr (alignas_expr
);
20782 /* Build the C++-11 representation of an 'aligned'
20785 build_tree_list (build_tree_list (get_identifier ("gnu"),
20786 get_identifier ("aligned")),
20787 build_tree_list (NULL_TREE
, alignas_expr
));
20793 /* Parse a standard C++-11 attribute-specifier-seq.
20795 attribute-specifier-seq:
20796 attribute-specifier-seq [opt] attribute-specifier
20800 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
20802 tree attr_specs
= NULL
;
20806 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
20807 if (attr_spec
== NULL_TREE
)
20809 if (attr_spec
== error_mark_node
)
20810 return error_mark_node
;
20812 TREE_CHAIN (attr_spec
) = attr_specs
;
20813 attr_specs
= attr_spec
;
20816 attr_specs
= nreverse (attr_specs
);
20820 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20821 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20822 current value of the PEDANTIC flag, regardless of whether or not
20823 the `__extension__' keyword is present. The caller is responsible
20824 for restoring the value of the PEDANTIC flag. */
20827 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
20829 /* Save the old value of the PEDANTIC flag. */
20830 *saved_pedantic
= pedantic
;
20832 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
20834 /* Consume the `__extension__' token. */
20835 cp_lexer_consume_token (parser
->lexer
);
20836 /* We're not being pedantic while the `__extension__' keyword is
20846 /* Parse a label declaration.
20849 __label__ label-declarator-seq ;
20851 label-declarator-seq:
20852 identifier , label-declarator-seq
20856 cp_parser_label_declaration (cp_parser
* parser
)
20858 /* Look for the `__label__' keyword. */
20859 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
20865 /* Look for an identifier. */
20866 identifier
= cp_parser_identifier (parser
);
20867 /* If we failed, stop. */
20868 if (identifier
== error_mark_node
)
20870 /* Declare it as a label. */
20871 finish_label_decl (identifier
);
20872 /* If the next token is a `;', stop. */
20873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20875 /* Look for the `,' separating the label declarations. */
20876 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
20879 /* Look for the final `;'. */
20880 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20883 /* Support Functions */
20885 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20886 NAME should have one of the representations used for an
20887 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20888 is returned. If PARSER->SCOPE is a dependent type, then a
20889 SCOPE_REF is returned.
20891 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20892 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20893 was formed. Abstractly, such entities should not be passed to this
20894 function, because they do not need to be looked up, but it is
20895 simpler to check for this special case here, rather than at the
20898 In cases not explicitly covered above, this function returns a
20899 DECL, OVERLOAD, or baselink representing the result of the lookup.
20900 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20903 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20904 (e.g., "struct") that was used. In that case bindings that do not
20905 refer to types are ignored.
20907 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20910 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20913 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20916 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20917 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20918 NULL_TREE otherwise. */
20921 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
20922 enum tag_types tag_type
,
20925 bool check_dependency
,
20926 tree
*ambiguous_decls
,
20927 location_t name_location
)
20930 tree object_type
= parser
->context
->object_type
;
20932 /* Assume that the lookup will be unambiguous. */
20933 if (ambiguous_decls
)
20934 *ambiguous_decls
= NULL_TREE
;
20936 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20937 no longer valid. Note that if we are parsing tentatively, and
20938 the parse fails, OBJECT_TYPE will be automatically restored. */
20939 parser
->context
->object_type
= NULL_TREE
;
20941 if (name
== error_mark_node
)
20942 return error_mark_node
;
20944 /* A template-id has already been resolved; there is no lookup to
20946 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
20948 if (BASELINK_P (name
))
20950 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
20951 == TEMPLATE_ID_EXPR
);
20955 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20956 it should already have been checked to make sure that the name
20957 used matches the type being destroyed. */
20958 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
20962 /* Figure out to which type this destructor applies. */
20964 type
= parser
->scope
;
20965 else if (object_type
)
20966 type
= object_type
;
20968 type
= current_class_type
;
20969 /* If that's not a class type, there is no destructor. */
20970 if (!type
|| !CLASS_TYPE_P (type
))
20971 return error_mark_node
;
20972 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
20973 lazily_declare_fn (sfk_destructor
, type
);
20974 if (!CLASSTYPE_DESTRUCTORS (type
))
20975 return error_mark_node
;
20976 /* If it was a class type, return the destructor. */
20977 return CLASSTYPE_DESTRUCTORS (type
);
20980 /* By this point, the NAME should be an ordinary identifier. If
20981 the id-expression was a qualified name, the qualifying scope is
20982 stored in PARSER->SCOPE at this point. */
20983 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
20985 /* Perform the lookup. */
20990 if (parser
->scope
== error_mark_node
)
20991 return error_mark_node
;
20993 /* If the SCOPE is dependent, the lookup must be deferred until
20994 the template is instantiated -- unless we are explicitly
20995 looking up names in uninstantiated templates. Even then, we
20996 cannot look up the name if the scope is not a class type; it
20997 might, for example, be a template type parameter. */
20998 dependent_p
= (TYPE_P (parser
->scope
)
20999 && dependent_scope_p (parser
->scope
));
21000 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
21002 /* Defer lookup. */
21003 decl
= error_mark_node
;
21006 tree pushed_scope
= NULL_TREE
;
21008 /* If PARSER->SCOPE is a dependent type, then it must be a
21009 class type, and we must not be checking dependencies;
21010 otherwise, we would have processed this lookup above. So
21011 that PARSER->SCOPE is not considered a dependent base by
21012 lookup_member, we must enter the scope here. */
21014 pushed_scope
= push_scope (parser
->scope
);
21016 /* If the PARSER->SCOPE is a template specialization, it
21017 may be instantiated during name lookup. In that case,
21018 errors may be issued. Even if we rollback the current
21019 tentative parse, those errors are valid. */
21020 decl
= lookup_qualified_name (parser
->scope
, name
,
21021 tag_type
!= none_type
,
21022 /*complain=*/true);
21024 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21025 lookup result and the nested-name-specifier nominates a class C:
21026 * if the name specified after the nested-name-specifier, when
21027 looked up in C, is the injected-class-name of C (Clause 9), or
21028 * if the name specified after the nested-name-specifier is the
21029 same as the identifier or the simple-template-id's template-
21030 name in the last component of the nested-name-specifier,
21031 the name is instead considered to name the constructor of
21032 class C. [ Note: for example, the constructor is not an
21033 acceptable lookup result in an elaborated-type-specifier so
21034 the constructor would not be used in place of the
21035 injected-class-name. --end note ] Such a constructor name
21036 shall be used only in the declarator-id of a declaration that
21037 names a constructor or in a using-declaration. */
21038 if (tag_type
== none_type
21039 && DECL_SELF_REFERENCE_P (decl
)
21040 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
21041 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
21042 tag_type
!= none_type
,
21043 /*complain=*/true);
21045 /* If we have a single function from a using decl, pull it out. */
21046 if (TREE_CODE (decl
) == OVERLOAD
21047 && !really_overloaded_fn (decl
))
21048 decl
= OVL_FUNCTION (decl
);
21051 pop_scope (pushed_scope
);
21054 /* If the scope is a dependent type and either we deferred lookup or
21055 we did lookup but didn't find the name, rememeber the name. */
21056 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
21057 && dependent_type_p (parser
->scope
))
21063 /* The resolution to Core Issue 180 says that `struct
21064 A::B' should be considered a type-name, even if `A'
21066 type
= make_typename_type (parser
->scope
, name
, tag_type
,
21067 /*complain=*/tf_error
);
21068 decl
= TYPE_NAME (type
);
21070 else if (is_template
21071 && (cp_parser_next_token_ends_template_argument_p (parser
)
21072 || cp_lexer_next_token_is (parser
->lexer
,
21074 decl
= make_unbound_class_template (parser
->scope
,
21076 /*complain=*/tf_error
);
21078 decl
= build_qualified_name (/*type=*/NULL_TREE
,
21079 parser
->scope
, name
,
21082 parser
->qualifying_scope
= parser
->scope
;
21083 parser
->object_scope
= NULL_TREE
;
21085 else if (object_type
)
21087 tree object_decl
= NULL_TREE
;
21088 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21089 OBJECT_TYPE is not a class. */
21090 if (CLASS_TYPE_P (object_type
))
21091 /* If the OBJECT_TYPE is a template specialization, it may
21092 be instantiated during name lookup. In that case, errors
21093 may be issued. Even if we rollback the current tentative
21094 parse, those errors are valid. */
21095 object_decl
= lookup_member (object_type
,
21098 tag_type
!= none_type
,
21099 tf_warning_or_error
);
21100 /* Look it up in the enclosing context, too. */
21101 decl
= lookup_name_real (name
, tag_type
!= none_type
,
21103 /*block_p=*/true, is_namespace
, 0);
21104 parser
->object_scope
= object_type
;
21105 parser
->qualifying_scope
= NULL_TREE
;
21107 decl
= object_decl
;
21111 decl
= lookup_name_real (name
, tag_type
!= none_type
,
21113 /*block_p=*/true, is_namespace
, 0);
21114 parser
->qualifying_scope
= NULL_TREE
;
21115 parser
->object_scope
= NULL_TREE
;
21118 /* If the lookup failed, let our caller know. */
21119 if (!decl
|| decl
== error_mark_node
)
21120 return error_mark_node
;
21122 /* Pull out the template from an injected-class-name (or multiple). */
21124 decl
= maybe_get_template_decl_from_type_decl (decl
);
21126 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21127 if (TREE_CODE (decl
) == TREE_LIST
)
21129 if (ambiguous_decls
)
21130 *ambiguous_decls
= decl
;
21131 /* The error message we have to print is too complicated for
21132 cp_parser_error, so we incorporate its actions directly. */
21133 if (!cp_parser_simulate_error (parser
))
21135 error_at (name_location
, "reference to %qD is ambiguous",
21137 print_candidates (decl
);
21139 return error_mark_node
;
21142 gcc_assert (DECL_P (decl
)
21143 || TREE_CODE (decl
) == OVERLOAD
21144 || TREE_CODE (decl
) == SCOPE_REF
21145 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
21146 || BASELINK_P (decl
));
21148 /* If we have resolved the name of a member declaration, check to
21149 see if the declaration is accessible. When the name resolves to
21150 set of overloaded functions, accessibility is checked when
21151 overload resolution is done.
21153 During an explicit instantiation, access is not checked at all,
21154 as per [temp.explicit]. */
21156 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
21158 maybe_record_typedef_use (decl
);
21163 /* Like cp_parser_lookup_name, but for use in the typical case where
21164 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21165 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21168 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
21170 return cp_parser_lookup_name (parser
, name
,
21172 /*is_template=*/false,
21173 /*is_namespace=*/false,
21174 /*check_dependency=*/true,
21175 /*ambiguous_decls=*/NULL
,
21179 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21180 the current context, return the TYPE_DECL. If TAG_NAME_P is
21181 true, the DECL indicates the class being defined in a class-head,
21182 or declared in an elaborated-type-specifier.
21184 Otherwise, return DECL. */
21187 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
21189 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21190 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21193 template <typename T> struct B;
21196 template <typename T> struct A::B {};
21198 Similarly, in an elaborated-type-specifier:
21200 namespace N { struct X{}; }
21203 template <typename T> friend struct N::X;
21206 However, if the DECL refers to a class type, and we are in
21207 the scope of the class, then the name lookup automatically
21208 finds the TYPE_DECL created by build_self_reference rather
21209 than a TEMPLATE_DECL. For example, in:
21211 template <class T> struct S {
21215 there is no need to handle such case. */
21217 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
21218 return DECL_TEMPLATE_RESULT (decl
);
21223 /* If too many, or too few, template-parameter lists apply to the
21224 declarator, issue an error message. Returns TRUE if all went well,
21225 and FALSE otherwise. */
21228 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
21229 cp_declarator
*declarator
,
21230 location_t declarator_location
)
21232 switch (declarator
->kind
)
21236 unsigned num_templates
= 0;
21237 tree scope
= declarator
->u
.id
.qualifying_scope
;
21240 num_templates
= num_template_headers_for_class (scope
);
21241 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
21242 == TEMPLATE_ID_EXPR
)
21243 /* If the DECLARATOR has the form `X<y>' then it uses one
21244 additional level of template parameters. */
21247 return cp_parser_check_template_parameters
21248 (parser
, num_templates
, declarator_location
, declarator
);
21254 case cdk_reference
:
21256 return (cp_parser_check_declarator_template_parameters
21257 (parser
, declarator
->declarator
, declarator_location
));
21263 gcc_unreachable ();
21268 /* NUM_TEMPLATES were used in the current declaration. If that is
21269 invalid, return FALSE and issue an error messages. Otherwise,
21270 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21271 declarator and we can print more accurate diagnostics. */
21274 cp_parser_check_template_parameters (cp_parser
* parser
,
21275 unsigned num_templates
,
21276 location_t location
,
21277 cp_declarator
*declarator
)
21279 /* If there are the same number of template classes and parameter
21280 lists, that's OK. */
21281 if (parser
->num_template_parameter_lists
== num_templates
)
21283 /* If there are more, but only one more, then we are referring to a
21284 member template. That's OK too. */
21285 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
21287 /* If there are more template classes than parameter lists, we have
21290 template <class T> void S<T>::R<T>::f (); */
21291 if (parser
->num_template_parameter_lists
< num_templates
)
21293 if (declarator
&& !current_function_decl
)
21294 error_at (location
, "specializing member %<%T::%E%> "
21295 "requires %<template<>%> syntax",
21296 declarator
->u
.id
.qualifying_scope
,
21297 declarator
->u
.id
.unqualified_name
);
21298 else if (declarator
)
21299 error_at (location
, "invalid declaration of %<%T::%E%>",
21300 declarator
->u
.id
.qualifying_scope
,
21301 declarator
->u
.id
.unqualified_name
);
21303 error_at (location
, "too few template-parameter-lists");
21306 /* Otherwise, there are too many template parameter lists. We have
21309 template <class T> template <class U> void S::f(); */
21310 error_at (location
, "too many template-parameter-lists");
21314 /* Parse an optional `::' token indicating that the following name is
21315 from the global namespace. If so, PARSER->SCOPE is set to the
21316 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21317 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21318 Returns the new value of PARSER->SCOPE, if the `::' token is
21319 present, and NULL_TREE otherwise. */
21322 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
21326 /* Peek at the next token. */
21327 token
= cp_lexer_peek_token (parser
->lexer
);
21328 /* If we're looking at a `::' token then we're starting from the
21329 global namespace, not our current location. */
21330 if (token
->type
== CPP_SCOPE
)
21332 /* Consume the `::' token. */
21333 cp_lexer_consume_token (parser
->lexer
);
21334 /* Set the SCOPE so that we know where to start the lookup. */
21335 parser
->scope
= global_namespace
;
21336 parser
->qualifying_scope
= global_namespace
;
21337 parser
->object_scope
= NULL_TREE
;
21339 return parser
->scope
;
21341 else if (!current_scope_valid_p
)
21343 parser
->scope
= NULL_TREE
;
21344 parser
->qualifying_scope
= NULL_TREE
;
21345 parser
->object_scope
= NULL_TREE
;
21351 /* Returns TRUE if the upcoming token sequence is the start of a
21352 constructor declarator. If FRIEND_P is true, the declarator is
21353 preceded by the `friend' specifier. */
21356 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
21358 bool constructor_p
;
21359 tree nested_name_specifier
;
21360 cp_token
*next_token
;
21362 /* The common case is that this is not a constructor declarator, so
21363 try to avoid doing lots of work if at all possible. It's not
21364 valid declare a constructor at function scope. */
21365 if (parser
->in_function_body
)
21367 /* And only certain tokens can begin a constructor declarator. */
21368 next_token
= cp_lexer_peek_token (parser
->lexer
);
21369 if (next_token
->type
!= CPP_NAME
21370 && next_token
->type
!= CPP_SCOPE
21371 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
21372 && next_token
->type
!= CPP_TEMPLATE_ID
)
21375 /* Parse tentatively; we are going to roll back all of the tokens
21377 cp_parser_parse_tentatively (parser
);
21378 /* Assume that we are looking at a constructor declarator. */
21379 constructor_p
= true;
21381 /* Look for the optional `::' operator. */
21382 cp_parser_global_scope_opt (parser
,
21383 /*current_scope_valid_p=*/false);
21384 /* Look for the nested-name-specifier. */
21385 nested_name_specifier
21386 = (cp_parser_nested_name_specifier_opt (parser
,
21387 /*typename_keyword_p=*/false,
21388 /*check_dependency_p=*/false,
21390 /*is_declaration=*/false));
21391 /* Outside of a class-specifier, there must be a
21392 nested-name-specifier. */
21393 if (!nested_name_specifier
&&
21394 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
21396 constructor_p
= false;
21397 else if (nested_name_specifier
== error_mark_node
)
21398 constructor_p
= false;
21400 /* If we have a class scope, this is easy; DR 147 says that S::S always
21401 names the constructor, and no other qualified name could. */
21402 if (constructor_p
&& nested_name_specifier
21403 && CLASS_TYPE_P (nested_name_specifier
))
21405 tree id
= cp_parser_unqualified_id (parser
,
21406 /*template_keyword_p=*/false,
21407 /*check_dependency_p=*/false,
21408 /*declarator_p=*/true,
21409 /*optional_p=*/false);
21410 if (is_overloaded_fn (id
))
21411 id
= DECL_NAME (get_first_fn (id
));
21412 if (!constructor_name_p (id
, nested_name_specifier
))
21413 constructor_p
= false;
21415 /* If we still think that this might be a constructor-declarator,
21416 look for a class-name. */
21417 else if (constructor_p
)
21421 template <typename T> struct S {
21425 we must recognize that the nested `S' names a class. */
21427 type_decl
= cp_parser_class_name (parser
,
21428 /*typename_keyword_p=*/false,
21429 /*template_keyword_p=*/false,
21431 /*check_dependency_p=*/false,
21432 /*class_head_p=*/false,
21433 /*is_declaration=*/false);
21434 /* If there was no class-name, then this is not a constructor. */
21435 constructor_p
= !cp_parser_error_occurred (parser
);
21437 /* If we're still considering a constructor, we have to see a `(',
21438 to begin the parameter-declaration-clause, followed by either a
21439 `)', an `...', or a decl-specifier. We need to check for a
21440 type-specifier to avoid being fooled into thinking that:
21444 is a constructor. (It is actually a function named `f' that
21445 takes one parameter (of type `int') and returns a value of type
21448 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
21449 constructor_p
= false;
21452 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
21453 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
21454 /* A parameter declaration begins with a decl-specifier,
21455 which is either the "attribute" keyword, a storage class
21456 specifier, or (usually) a type-specifier. */
21457 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
21460 tree pushed_scope
= NULL_TREE
;
21461 unsigned saved_num_template_parameter_lists
;
21463 /* Names appearing in the type-specifier should be looked up
21464 in the scope of the class. */
21465 if (current_class_type
)
21469 type
= TREE_TYPE (type_decl
);
21470 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21472 type
= resolve_typename_type (type
,
21473 /*only_current_p=*/false);
21474 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21476 cp_parser_abort_tentative_parse (parser
);
21480 pushed_scope
= push_scope (type
);
21483 /* Inside the constructor parameter list, surrounding
21484 template-parameter-lists do not apply. */
21485 saved_num_template_parameter_lists
21486 = parser
->num_template_parameter_lists
;
21487 parser
->num_template_parameter_lists
= 0;
21489 /* Look for the type-specifier. */
21490 cp_parser_type_specifier (parser
,
21491 CP_PARSER_FLAGS_NONE
,
21492 /*decl_specs=*/NULL
,
21493 /*is_declarator=*/true,
21494 /*declares_class_or_enum=*/NULL
,
21495 /*is_cv_qualifier=*/NULL
);
21497 parser
->num_template_parameter_lists
21498 = saved_num_template_parameter_lists
;
21500 /* Leave the scope of the class. */
21502 pop_scope (pushed_scope
);
21504 constructor_p
= !cp_parser_error_occurred (parser
);
21508 /* We did not really want to consume any tokens. */
21509 cp_parser_abort_tentative_parse (parser
);
21511 return constructor_p
;
21514 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21515 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21516 they must be performed once we are in the scope of the function.
21518 Returns the function defined. */
21521 cp_parser_function_definition_from_specifiers_and_declarator
21522 (cp_parser
* parser
,
21523 cp_decl_specifier_seq
*decl_specifiers
,
21525 const cp_declarator
*declarator
)
21530 /* Begin the function-definition. */
21531 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
21533 /* The things we're about to see are not directly qualified by any
21534 template headers we've seen thus far. */
21535 reset_specialization ();
21537 /* If there were names looked up in the decl-specifier-seq that we
21538 did not check, check them now. We must wait until we are in the
21539 scope of the function to perform the checks, since the function
21540 might be a friend. */
21541 perform_deferred_access_checks (tf_warning_or_error
);
21545 /* Skip the entire function. */
21546 cp_parser_skip_to_end_of_block_or_statement (parser
);
21547 fn
= error_mark_node
;
21549 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
21551 /* Seen already, skip it. An error message has already been output. */
21552 cp_parser_skip_to_end_of_block_or_statement (parser
);
21553 fn
= current_function_decl
;
21554 current_function_decl
= NULL_TREE
;
21555 /* If this is a function from a class, pop the nested class. */
21556 if (current_class_name
)
21557 pop_nested_class ();
21562 if (DECL_DECLARED_INLINE_P (current_function_decl
))
21563 tv
= TV_PARSE_INLINE
;
21565 tv
= TV_PARSE_FUNC
;
21567 fn
= cp_parser_function_definition_after_declarator (parser
,
21568 /*inline_p=*/false);
21575 /* Parse the part of a function-definition that follows the
21576 declarator. INLINE_P is TRUE iff this function is an inline
21577 function defined within a class-specifier.
21579 Returns the function defined. */
21582 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
21586 bool ctor_initializer_p
= false;
21587 bool saved_in_unbraced_linkage_specification_p
;
21588 bool saved_in_function_body
;
21589 unsigned saved_num_template_parameter_lists
;
21592 saved_in_function_body
= parser
->in_function_body
;
21593 parser
->in_function_body
= true;
21594 /* If the next token is `return', then the code may be trying to
21595 make use of the "named return value" extension that G++ used to
21597 token
= cp_lexer_peek_token (parser
->lexer
);
21598 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
21600 /* Consume the `return' keyword. */
21601 cp_lexer_consume_token (parser
->lexer
);
21602 /* Look for the identifier that indicates what value is to be
21604 cp_parser_identifier (parser
);
21605 /* Issue an error message. */
21606 error_at (token
->location
,
21607 "named return values are no longer supported");
21608 /* Skip tokens until we reach the start of the function body. */
21611 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21612 if (token
->type
== CPP_OPEN_BRACE
21613 || token
->type
== CPP_EOF
21614 || token
->type
== CPP_PRAGMA_EOL
)
21616 cp_lexer_consume_token (parser
->lexer
);
21619 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21620 anything declared inside `f'. */
21621 saved_in_unbraced_linkage_specification_p
21622 = parser
->in_unbraced_linkage_specification_p
;
21623 parser
->in_unbraced_linkage_specification_p
= false;
21624 /* Inside the function, surrounding template-parameter-lists do not
21626 saved_num_template_parameter_lists
21627 = parser
->num_template_parameter_lists
;
21628 parser
->num_template_parameter_lists
= 0;
21630 start_lambda_scope (current_function_decl
);
21632 /* If the next token is `try', `__transaction_atomic', or
21633 `__transaction_relaxed`, then we are looking at either function-try-block
21634 or function-transaction-block. Note that all of these include the
21636 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
21637 ctor_initializer_p
= cp_parser_function_transaction (parser
,
21638 RID_TRANSACTION_ATOMIC
);
21639 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
21640 RID_TRANSACTION_RELAXED
))
21641 ctor_initializer_p
= cp_parser_function_transaction (parser
,
21642 RID_TRANSACTION_RELAXED
);
21643 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
21644 ctor_initializer_p
= cp_parser_function_try_block (parser
);
21646 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21647 (parser
, /*in_function_try_block=*/false);
21649 finish_lambda_scope ();
21651 /* Finish the function. */
21652 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
21653 (inline_p
? 2 : 0));
21654 /* Generate code for it, if necessary. */
21655 expand_or_defer_fn (fn
);
21656 /* Restore the saved values. */
21657 parser
->in_unbraced_linkage_specification_p
21658 = saved_in_unbraced_linkage_specification_p
;
21659 parser
->num_template_parameter_lists
21660 = saved_num_template_parameter_lists
;
21661 parser
->in_function_body
= saved_in_function_body
;
21666 /* Parse a template-declaration, assuming that the `export' (and
21667 `extern') keywords, if present, has already been scanned. MEMBER_P
21668 is as for cp_parser_template_declaration. */
21671 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
21673 tree decl
= NULL_TREE
;
21674 VEC (deferred_access_check
,gc
) *checks
;
21675 tree parameter_list
;
21676 bool friend_p
= false;
21677 bool need_lang_pop
;
21680 /* Look for the `template' keyword. */
21681 token
= cp_lexer_peek_token (parser
->lexer
);
21682 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
21686 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
21688 if (at_class_scope_p () && current_function_decl
)
21690 /* 14.5.2.2 [temp.mem]
21692 A local class shall not have member templates. */
21693 error_at (token
->location
,
21694 "invalid declaration of member template in local class");
21695 cp_parser_skip_to_end_of_block_or_statement (parser
);
21700 A template ... shall not have C linkage. */
21701 if (current_lang_name
== lang_name_c
)
21703 error_at (token
->location
, "template with C linkage");
21704 /* Give it C++ linkage to avoid confusing other parts of the
21706 push_lang_context (lang_name_cplusplus
);
21707 need_lang_pop
= true;
21710 need_lang_pop
= false;
21712 /* We cannot perform access checks on the template parameter
21713 declarations until we know what is being declared, just as we
21714 cannot check the decl-specifier list. */
21715 push_deferring_access_checks (dk_deferred
);
21717 /* If the next token is `>', then we have an invalid
21718 specialization. Rather than complain about an invalid template
21719 parameter, issue an error message here. */
21720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
21722 cp_parser_error (parser
, "invalid explicit specialization");
21723 begin_specialization ();
21724 parameter_list
= NULL_TREE
;
21728 /* Parse the template parameters. */
21729 parameter_list
= cp_parser_template_parameter_list (parser
);
21732 /* Get the deferred access checks from the parameter list. These
21733 will be checked once we know what is being declared, as for a
21734 member template the checks must be performed in the scope of the
21735 class containing the member. */
21736 checks
= get_deferred_access_checks ();
21738 /* Look for the `>'. */
21739 cp_parser_skip_to_end_of_template_parameter_list (parser
);
21740 /* We just processed one more parameter list. */
21741 ++parser
->num_template_parameter_lists
;
21742 /* If the next token is `template', there are more template
21744 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
21746 cp_parser_template_declaration_after_export (parser
, member_p
);
21747 else if (cxx_dialect
>= cxx0x
21748 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
21749 decl
= cp_parser_alias_declaration (parser
);
21752 /* There are no access checks when parsing a template, as we do not
21753 know if a specialization will be a friend. */
21754 push_deferring_access_checks (dk_no_check
);
21755 token
= cp_lexer_peek_token (parser
->lexer
);
21756 decl
= cp_parser_single_declaration (parser
,
21759 /*explicit_specialization_p=*/false,
21761 pop_deferring_access_checks ();
21763 /* If this is a member template declaration, let the front
21765 if (member_p
&& !friend_p
&& decl
)
21767 if (TREE_CODE (decl
) == TYPE_DECL
)
21768 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
21770 decl
= finish_member_template_decl (decl
);
21772 else if (friend_p
&& decl
21773 && (TREE_CODE (decl
) == TYPE_DECL
21774 || DECL_TYPE_TEMPLATE_P (decl
)))
21775 make_friend_class (current_class_type
, TREE_TYPE (decl
),
21776 /*complain=*/true);
21778 /* We are done with the current parameter list. */
21779 --parser
->num_template_parameter_lists
;
21781 pop_deferring_access_checks ();
21784 finish_template_decl (parameter_list
);
21786 /* Check the template arguments for a literal operator template. */
21788 && (TREE_CODE (decl
) == FUNCTION_DECL
|| DECL_FUNCTION_TEMPLATE_P (decl
))
21789 && UDLIT_OPER_P (DECL_NAME (decl
)))
21792 if (parameter_list
== NULL_TREE
)
21796 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
21797 if (num_parms
!= 1)
21801 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
21802 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
21803 if (TREE_TYPE (parm
) != char_type_node
21804 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
21809 error ("literal operator template %qD has invalid parameter list."
21810 " Expected non-type template argument pack <char...>",
21813 /* Register member declarations. */
21814 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
21815 finish_member_declaration (decl
);
21816 /* For the erroneous case of a template with C linkage, we pushed an
21817 implicit C++ linkage scope; exit that scope now. */
21819 pop_lang_context ();
21820 /* If DECL is a function template, we must return to parse it later.
21821 (Even though there is no definition, there might be default
21822 arguments that need handling.) */
21823 if (member_p
&& decl
21824 && (TREE_CODE (decl
) == FUNCTION_DECL
21825 || DECL_FUNCTION_TEMPLATE_P (decl
)))
21826 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, decl
);
21829 /* Perform the deferred access checks from a template-parameter-list.
21830 CHECKS is a TREE_LIST of access checks, as returned by
21831 get_deferred_access_checks. */
21834 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
21836 ++processing_template_parmlist
;
21837 perform_access_checks (checks
, tf_warning_or_error
);
21838 --processing_template_parmlist
;
21841 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21842 `function-definition' sequence that follows a template header.
21843 If MEMBER_P is true, this declaration appears in a class scope.
21845 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21846 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21849 cp_parser_single_declaration (cp_parser
* parser
,
21850 VEC (deferred_access_check
,gc
)* checks
,
21852 bool explicit_specialization_p
,
21855 int declares_class_or_enum
;
21856 tree decl
= NULL_TREE
;
21857 cp_decl_specifier_seq decl_specifiers
;
21858 bool function_definition_p
= false;
21859 cp_token
*decl_spec_token_start
;
21861 /* This function is only used when processing a template
21863 gcc_assert (innermost_scope_kind () == sk_template_parms
21864 || innermost_scope_kind () == sk_template_spec
);
21866 /* Defer access checks until we know what is being declared. */
21867 push_deferring_access_checks (dk_deferred
);
21869 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21871 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21872 cp_parser_decl_specifier_seq (parser
,
21873 CP_PARSER_FLAGS_OPTIONAL
,
21875 &declares_class_or_enum
);
21877 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
21879 /* There are no template typedefs. */
21880 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
21882 error_at (decl_spec_token_start
->location
,
21883 "template declaration of %<typedef%>");
21884 decl
= error_mark_node
;
21887 /* Gather up the access checks that occurred the
21888 decl-specifier-seq. */
21889 stop_deferring_access_checks ();
21891 /* Check for the declaration of a template class. */
21892 if (declares_class_or_enum
)
21894 if (cp_parser_declares_only_class_p (parser
))
21896 decl
= shadow_tag (&decl_specifiers
);
21901 friend template <typename T> struct A<T>::B;
21904 A<T>::B will be represented by a TYPENAME_TYPE, and
21905 therefore not recognized by shadow_tag. */
21906 if (friend_p
&& *friend_p
21908 && decl_specifiers
.type
21909 && TYPE_P (decl_specifiers
.type
))
21910 decl
= decl_specifiers
.type
;
21912 if (decl
&& decl
!= error_mark_node
)
21913 decl
= TYPE_NAME (decl
);
21915 decl
= error_mark_node
;
21917 /* Perform access checks for template parameters. */
21918 cp_parser_perform_template_parameter_access_checks (checks
);
21922 /* Complain about missing 'typename' or other invalid type names. */
21923 if (!decl_specifiers
.any_type_specifiers_p
21924 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21926 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21927 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21928 the rest of this declaration. */
21929 decl
= error_mark_node
;
21933 /* If it's not a template class, try for a template function. If
21934 the next token is a `;', then this declaration does not declare
21935 anything. But, if there were errors in the decl-specifiers, then
21936 the error might well have come from an attempted class-specifier.
21937 In that case, there's no need to warn about a missing declarator. */
21939 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
21940 || decl_specifiers
.type
!= error_mark_node
))
21942 decl
= cp_parser_init_declarator (parser
,
21945 /*function_definition_allowed_p=*/true,
21947 declares_class_or_enum
,
21948 &function_definition_p
,
21951 /* 7.1.1-1 [dcl.stc]
21953 A storage-class-specifier shall not be specified in an explicit
21954 specialization... */
21956 && explicit_specialization_p
21957 && decl_specifiers
.storage_class
!= sc_none
)
21959 error_at (decl_spec_token_start
->location
,
21960 "explicit template specialization cannot have a storage class");
21961 decl
= error_mark_node
;
21964 if (decl
&& TREE_CODE (decl
) == VAR_DECL
)
21965 check_template_variable (decl
);
21968 /* Look for a trailing `;' after the declaration. */
21969 if (!function_definition_p
21970 && (decl
== error_mark_node
21971 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
21972 cp_parser_skip_to_end_of_block_or_statement (parser
);
21975 pop_deferring_access_checks ();
21977 /* Clear any current qualification; whatever comes next is the start
21978 of something new. */
21979 parser
->scope
= NULL_TREE
;
21980 parser
->qualifying_scope
= NULL_TREE
;
21981 parser
->object_scope
= NULL_TREE
;
21986 /* Parse a cast-expression that is not the operand of a unary "&". */
21989 cp_parser_simple_cast_expression (cp_parser
*parser
)
21991 return cp_parser_cast_expression (parser
, /*address_p=*/false,
21992 /*cast_p=*/false, NULL
);
21995 /* Parse a functional cast to TYPE. Returns an expression
21996 representing the cast. */
21999 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
22002 tree expression_list
;
22006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22008 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22009 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
22010 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
22011 if (TREE_CODE (type
) == TYPE_DECL
)
22012 type
= TREE_TYPE (type
);
22013 return finish_compound_literal (type
, expression_list
,
22014 tf_warning_or_error
);
22018 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22020 /*allow_expansion_p=*/true,
22021 /*non_constant_p=*/NULL
);
22023 expression_list
= error_mark_node
;
22026 expression_list
= build_tree_list_vec (vec
);
22027 release_tree_vector (vec
);
22030 cast
= build_functional_cast (type
, expression_list
,
22031 tf_warning_or_error
);
22032 /* [expr.const]/1: In an integral constant expression "only type
22033 conversions to integral or enumeration type can be used". */
22034 if (TREE_CODE (type
) == TYPE_DECL
)
22035 type
= TREE_TYPE (type
);
22036 if (cast
!= error_mark_node
22037 && !cast_valid_in_integral_constant_expression_p (type
)
22038 && cp_parser_non_integral_constant_expression (parser
,
22040 return error_mark_node
;
22044 /* Save the tokens that make up the body of a member function defined
22045 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22046 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22047 specifiers applied to the declaration. Returns the FUNCTION_DECL
22048 for the member function. */
22051 cp_parser_save_member_function_body (cp_parser
* parser
,
22052 cp_decl_specifier_seq
*decl_specifiers
,
22053 cp_declarator
*declarator
,
22060 /* Create the FUNCTION_DECL. */
22061 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
22062 /* If something went badly wrong, bail out now. */
22063 if (fn
== error_mark_node
)
22065 /* If there's a function-body, skip it. */
22066 if (cp_parser_token_starts_function_definition_p
22067 (cp_lexer_peek_token (parser
->lexer
)))
22068 cp_parser_skip_to_end_of_block_or_statement (parser
);
22069 return error_mark_node
;
22072 /* Remember it, if there default args to post process. */
22073 cp_parser_save_default_args (parser
, fn
);
22075 /* Save away the tokens that make up the body of the
22077 first
= parser
->lexer
->next_token
;
22078 /* We can have braced-init-list mem-initializers before the fn body. */
22079 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22081 cp_lexer_consume_token (parser
->lexer
);
22082 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22083 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
22085 /* cache_group will stop after an un-nested { } pair, too. */
22086 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
22089 /* variadic mem-inits have ... after the ')'. */
22090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22091 cp_lexer_consume_token (parser
->lexer
);
22094 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
22095 /* Handle function try blocks. */
22096 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
22097 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
22098 last
= parser
->lexer
->next_token
;
22100 /* Save away the inline definition; we will process it when the
22101 class is complete. */
22102 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
22103 DECL_PENDING_INLINE_P (fn
) = 1;
22105 /* We need to know that this was defined in the class, so that
22106 friend templates are handled correctly. */
22107 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
22109 /* Add FN to the queue of functions to be parsed later. */
22110 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, fn
);
22115 /* Save the tokens that make up the in-class initializer for a non-static
22116 data member. Returns a DEFAULT_ARG. */
22119 cp_parser_save_nsdmi (cp_parser
* parser
)
22121 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
22124 /* Parse a template-argument-list, as well as the trailing ">" (but
22125 not the opening "<"). See cp_parser_template_argument_list for the
22129 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
22133 tree saved_qualifying_scope
;
22134 tree saved_object_scope
;
22135 bool saved_greater_than_is_operator_p
;
22136 int saved_unevaluated_operand
;
22137 int saved_inhibit_evaluation_warnings
;
22141 When parsing a template-id, the first non-nested `>' is taken as
22142 the end of the template-argument-list rather than a greater-than
22144 saved_greater_than_is_operator_p
22145 = parser
->greater_than_is_operator_p
;
22146 parser
->greater_than_is_operator_p
= false;
22147 /* Parsing the argument list may modify SCOPE, so we save it
22149 saved_scope
= parser
->scope
;
22150 saved_qualifying_scope
= parser
->qualifying_scope
;
22151 saved_object_scope
= parser
->object_scope
;
22152 /* We need to evaluate the template arguments, even though this
22153 template-id may be nested within a "sizeof". */
22154 saved_unevaluated_operand
= cp_unevaluated_operand
;
22155 cp_unevaluated_operand
= 0;
22156 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
22157 c_inhibit_evaluation_warnings
= 0;
22158 /* Parse the template-argument-list itself. */
22159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
22160 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
22161 arguments
= NULL_TREE
;
22163 arguments
= cp_parser_template_argument_list (parser
);
22164 /* Look for the `>' that ends the template-argument-list. If we find
22165 a '>>' instead, it's probably just a typo. */
22166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
22168 if (cxx_dialect
!= cxx98
)
22170 /* In C++0x, a `>>' in a template argument list or cast
22171 expression is considered to be two separate `>'
22172 tokens. So, change the current token to a `>', but don't
22173 consume it: it will be consumed later when the outer
22174 template argument list (or cast expression) is parsed.
22175 Note that this replacement of `>' for `>>' is necessary
22176 even if we are parsing tentatively: in the tentative
22177 case, after calling
22178 cp_parser_enclosed_template_argument_list we will always
22179 throw away all of the template arguments and the first
22180 closing `>', either because the template argument list
22181 was erroneous or because we are replacing those tokens
22182 with a CPP_TEMPLATE_ID token. The second `>' (which will
22183 not have been thrown away) is needed either to close an
22184 outer template argument list or to complete a new-style
22186 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22187 token
->type
= CPP_GREATER
;
22189 else if (!saved_greater_than_is_operator_p
)
22191 /* If we're in a nested template argument list, the '>>' has
22192 to be a typo for '> >'. We emit the error message, but we
22193 continue parsing and we push a '>' as next token, so that
22194 the argument list will be parsed correctly. Note that the
22195 global source location is still on the token before the
22196 '>>', so we need to say explicitly where we want it. */
22197 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22198 error_at (token
->location
, "%<>>%> should be %<> >%> "
22199 "within a nested template argument list");
22201 token
->type
= CPP_GREATER
;
22205 /* If this is not a nested template argument list, the '>>'
22206 is a typo for '>'. Emit an error message and continue.
22207 Same deal about the token location, but here we can get it
22208 right by consuming the '>>' before issuing the diagnostic. */
22209 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
22210 error_at (token
->location
,
22211 "spurious %<>>%>, use %<>%> to terminate "
22212 "a template argument list");
22216 cp_parser_skip_to_end_of_template_parameter_list (parser
);
22217 /* The `>' token might be a greater-than operator again now. */
22218 parser
->greater_than_is_operator_p
22219 = saved_greater_than_is_operator_p
;
22220 /* Restore the SAVED_SCOPE. */
22221 parser
->scope
= saved_scope
;
22222 parser
->qualifying_scope
= saved_qualifying_scope
;
22223 parser
->object_scope
= saved_object_scope
;
22224 cp_unevaluated_operand
= saved_unevaluated_operand
;
22225 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
22230 /* MEMBER_FUNCTION is a member function, or a friend. If default
22231 arguments, or the body of the function have not yet been parsed,
22235 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
22237 timevar_push (TV_PARSE_INMETH
);
22238 /* If this member is a template, get the underlying
22240 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
22241 member_function
= DECL_TEMPLATE_RESULT (member_function
);
22243 /* There should not be any class definitions in progress at this
22244 point; the bodies of members are only parsed outside of all class
22246 gcc_assert (parser
->num_classes_being_defined
== 0);
22247 /* While we're parsing the member functions we might encounter more
22248 classes. We want to handle them right away, but we don't want
22249 them getting mixed up with functions that are currently in the
22251 push_unparsed_function_queues (parser
);
22253 /* Make sure that any template parameters are in scope. */
22254 maybe_begin_member_template_processing (member_function
);
22256 /* If the body of the function has not yet been parsed, parse it
22258 if (DECL_PENDING_INLINE_P (member_function
))
22260 tree function_scope
;
22261 cp_token_cache
*tokens
;
22263 /* The function is no longer pending; we are processing it. */
22264 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
22265 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
22266 DECL_PENDING_INLINE_P (member_function
) = 0;
22268 /* If this is a local class, enter the scope of the containing
22270 function_scope
= current_function_decl
;
22271 if (function_scope
)
22272 push_function_context ();
22274 /* Push the body of the function onto the lexer stack. */
22275 cp_parser_push_lexer_for_tokens (parser
, tokens
);
22277 /* Let the front end know that we going to be defining this
22279 start_preparsed_function (member_function
, NULL_TREE
,
22280 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
22282 /* Don't do access checking if it is a templated function. */
22283 if (processing_template_decl
)
22284 push_deferring_access_checks (dk_no_check
);
22286 /* Now, parse the body of the function. */
22287 cp_parser_function_definition_after_declarator (parser
,
22288 /*inline_p=*/true);
22290 if (processing_template_decl
)
22291 pop_deferring_access_checks ();
22293 /* Leave the scope of the containing function. */
22294 if (function_scope
)
22295 pop_function_context ();
22296 cp_parser_pop_lexer (parser
);
22299 /* Remove any template parameters from the symbol table. */
22300 maybe_end_member_template_processing ();
22302 /* Restore the queue. */
22303 pop_unparsed_function_queues (parser
);
22304 timevar_pop (TV_PARSE_INMETH
);
22307 /* If DECL contains any default args, remember it on the unparsed
22308 functions queue. */
22311 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
22315 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
22317 probe
= TREE_CHAIN (probe
))
22318 if (TREE_PURPOSE (probe
))
22320 cp_default_arg_entry entry
= {current_class_type
, decl
};
22321 VEC_safe_push (cp_default_arg_entry
, gc
,
22322 unparsed_funs_with_default_args
, entry
);
22327 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22328 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22329 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22330 from the parameter-type-list. */
22333 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
22334 tree default_arg
, tree parmtype
)
22336 cp_token_cache
*tokens
;
22340 if (default_arg
== error_mark_node
)
22341 return error_mark_node
;
22343 /* Push the saved tokens for the default argument onto the parser's
22345 tokens
= DEFARG_TOKENS (default_arg
);
22346 cp_parser_push_lexer_for_tokens (parser
, tokens
);
22348 start_lambda_scope (decl
);
22350 /* Parse the default argument. */
22351 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
22352 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
22353 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22355 finish_lambda_scope ();
22357 if (parsed_arg
== error_mark_node
)
22358 cp_parser_skip_to_end_of_statement (parser
);
22360 if (!processing_template_decl
)
22362 /* In a non-template class, check conversions now. In a template,
22363 we'll wait and instantiate these as needed. */
22364 if (TREE_CODE (decl
) == PARM_DECL
)
22365 parsed_arg
= check_default_argument (parmtype
, parsed_arg
);
22368 int flags
= LOOKUP_IMPLICIT
;
22369 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
)
22370 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg
))
22371 flags
= LOOKUP_NORMAL
;
22372 parsed_arg
= digest_init_flags (TREE_TYPE (decl
), parsed_arg
, flags
);
22376 /* If the token stream has not been completely used up, then
22377 there was extra junk after the end of the default
22379 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
22381 if (TREE_CODE (decl
) == PARM_DECL
)
22382 cp_parser_error (parser
, "expected %<,%>");
22384 cp_parser_error (parser
, "expected %<;%>");
22387 /* Revert to the main lexer. */
22388 cp_parser_pop_lexer (parser
);
22393 /* FIELD is a non-static data member with an initializer which we saved for
22394 later; parse it now. */
22397 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
22401 push_unparsed_function_queues (parser
);
22402 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
22403 DECL_INITIAL (field
),
22405 pop_unparsed_function_queues (parser
);
22407 DECL_INITIAL (field
) = def
;
22410 /* FN is a FUNCTION_DECL which may contains a parameter with an
22411 unparsed DEFAULT_ARG. Parse the default args now. This function
22412 assumes that the current scope is the scope in which the default
22413 argument should be processed. */
22416 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
22418 bool saved_local_variables_forbidden_p
;
22419 tree parm
, parmdecl
;
22421 /* While we're parsing the default args, we might (due to the
22422 statement expression extension) encounter more classes. We want
22423 to handle them right away, but we don't want them getting mixed
22424 up with default args that are currently in the queue. */
22425 push_unparsed_function_queues (parser
);
22427 /* Local variable names (and the `this' keyword) may not appear
22428 in a default argument. */
22429 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22430 parser
->local_variables_forbidden_p
= true;
22432 push_defarg_context (fn
);
22434 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
22435 parmdecl
= DECL_ARGUMENTS (fn
);
22436 parm
&& parm
!= void_list_node
;
22437 parm
= TREE_CHAIN (parm
),
22438 parmdecl
= DECL_CHAIN (parmdecl
))
22440 tree default_arg
= TREE_PURPOSE (parm
);
22442 VEC(tree
,gc
) *insts
;
22449 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
22450 /* This can happen for a friend declaration for a function
22451 already declared with default arguments. */
22455 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
22457 TREE_VALUE (parm
));
22458 if (parsed_arg
== error_mark_node
)
22463 TREE_PURPOSE (parm
) = parsed_arg
;
22465 /* Update any instantiations we've already created. */
22466 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
22467 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
22468 TREE_PURPOSE (copy
) = parsed_arg
;
22471 pop_defarg_context ();
22473 /* Make sure no default arg is missing. */
22474 check_default_args (fn
);
22476 /* Restore the state of local_variables_forbidden_p. */
22477 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22479 /* Restore the queue. */
22480 pop_unparsed_function_queues (parser
);
22483 /* Parse the operand of `sizeof' (or a similar operator). Returns
22484 either a TYPE or an expression, depending on the form of the
22485 input. The KEYWORD indicates which kind of expression we have
22489 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
22491 tree expr
= NULL_TREE
;
22492 const char *saved_message
;
22494 bool saved_integral_constant_expression_p
;
22495 bool saved_non_integral_constant_expression_p
;
22496 bool pack_expansion_p
= false;
22498 /* Types cannot be defined in a `sizeof' expression. Save away the
22500 saved_message
= parser
->type_definition_forbidden_message
;
22501 /* And create the new one. */
22502 tmp
= concat ("types may not be defined in %<",
22503 IDENTIFIER_POINTER (ridpointers
[keyword
]),
22504 "%> expressions", NULL
);
22505 parser
->type_definition_forbidden_message
= tmp
;
22507 /* The restrictions on constant-expressions do not apply inside
22508 sizeof expressions. */
22509 saved_integral_constant_expression_p
22510 = parser
->integral_constant_expression_p
;
22511 saved_non_integral_constant_expression_p
22512 = parser
->non_integral_constant_expression_p
;
22513 parser
->integral_constant_expression_p
= false;
22515 /* If it's a `...', then we are computing the length of a parameter
22517 if (keyword
== RID_SIZEOF
22518 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22520 /* Consume the `...'. */
22521 cp_lexer_consume_token (parser
->lexer
);
22522 maybe_warn_variadic_templates ();
22524 /* Note that this is an expansion. */
22525 pack_expansion_p
= true;
22528 /* Do not actually evaluate the expression. */
22529 ++cp_unevaluated_operand
;
22530 ++c_inhibit_evaluation_warnings
;
22531 /* If it's a `(', then we might be looking at the type-id
22533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22536 bool saved_in_type_id_in_expr_p
;
22538 /* We can't be sure yet whether we're looking at a type-id or an
22540 cp_parser_parse_tentatively (parser
);
22541 /* Consume the `('. */
22542 cp_lexer_consume_token (parser
->lexer
);
22543 /* Parse the type-id. */
22544 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
22545 parser
->in_type_id_in_expr_p
= true;
22546 type
= cp_parser_type_id (parser
);
22547 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
22548 /* Now, look for the trailing `)'. */
22549 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22550 /* If all went well, then we're done. */
22551 if (cp_parser_parse_definitely (parser
))
22553 cp_decl_specifier_seq decl_specs
;
22555 /* Build a trivial decl-specifier-seq. */
22556 clear_decl_specs (&decl_specs
);
22557 decl_specs
.type
= type
;
22559 /* Call grokdeclarator to figure out what type this is. */
22560 expr
= grokdeclarator (NULL
,
22564 /*attrlist=*/NULL
);
22567 else if (pack_expansion_p
)
22568 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
22569 "%<sizeof...%> argument must be surrounded by parentheses");
22571 /* If the type-id production did not work out, then we must be
22572 looking at the unary-expression production. */
22574 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
22575 /*cast_p=*/false, NULL
);
22577 if (pack_expansion_p
)
22578 /* Build a pack expansion. */
22579 expr
= make_pack_expansion (expr
);
22581 /* Go back to evaluating expressions. */
22582 --cp_unevaluated_operand
;
22583 --c_inhibit_evaluation_warnings
;
22585 /* Free the message we created. */
22587 /* And restore the old one. */
22588 parser
->type_definition_forbidden_message
= saved_message
;
22589 parser
->integral_constant_expression_p
22590 = saved_integral_constant_expression_p
;
22591 parser
->non_integral_constant_expression_p
22592 = saved_non_integral_constant_expression_p
;
22597 /* If the current declaration has no declarator, return true. */
22600 cp_parser_declares_only_class_p (cp_parser
*parser
)
22602 /* If the next token is a `;' or a `,' then there is no
22604 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22605 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
22608 /* Update the DECL_SPECS to reflect the storage class indicated by
22612 cp_parser_set_storage_class (cp_parser
*parser
,
22613 cp_decl_specifier_seq
*decl_specs
,
22617 cp_storage_class storage_class
;
22619 if (parser
->in_unbraced_linkage_specification_p
)
22621 error_at (token
->location
, "invalid use of %qD in linkage specification",
22622 ridpointers
[keyword
]);
22625 else if (decl_specs
->storage_class
!= sc_none
)
22627 decl_specs
->conflicting_specifiers_p
= true;
22631 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
22632 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
22633 && decl_specs
->gnu_thread_keyword_p
)
22635 pedwarn (decl_specs
->locations
[ds_thread
], 0,
22636 "%<__thread%> before %qD", ridpointers
[keyword
]);
22642 storage_class
= sc_auto
;
22645 storage_class
= sc_register
;
22648 storage_class
= sc_static
;
22651 storage_class
= sc_extern
;
22654 storage_class
= sc_mutable
;
22657 gcc_unreachable ();
22659 decl_specs
->storage_class
= storage_class
;
22660 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
22662 /* A storage class specifier cannot be applied alongside a typedef
22663 specifier. If there is a typedef specifier present then set
22664 conflicting_specifiers_p which will trigger an error later
22665 on in grokdeclarator. */
22666 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
22667 decl_specs
->conflicting_specifiers_p
= true;
22670 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22671 is true, the type is a class or enum definition. */
22674 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
22677 bool type_definition_p
)
22679 decl_specs
->any_specifiers_p
= true;
22681 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22682 (with, for example, in "typedef int wchar_t;") we remember that
22683 this is what happened. In system headers, we ignore these
22684 declarations so that G++ can work with system headers that are not
22686 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
22687 && !type_definition_p
22688 && (type_spec
== boolean_type_node
22689 || type_spec
== char16_type_node
22690 || type_spec
== char32_type_node
22691 || type_spec
== wchar_type_node
)
22692 && (decl_specs
->type
22693 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
22694 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
22695 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
22696 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
22698 decl_specs
->redefined_builtin_type
= type_spec
;
22699 set_and_check_decl_spec_loc (decl_specs
,
22700 ds_redefined_builtin_type_spec
,
22702 if (!decl_specs
->type
)
22704 decl_specs
->type
= type_spec
;
22705 decl_specs
->type_definition_p
= false;
22706 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
22709 else if (decl_specs
->type
)
22710 decl_specs
->multiple_types_p
= true;
22713 decl_specs
->type
= type_spec
;
22714 decl_specs
->type_definition_p
= type_definition_p
;
22715 decl_specs
->redefined_builtin_type
= NULL_TREE
;
22716 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
22720 /* True iff TOKEN is the GNU keyword __thread. */
22723 token_is__thread (cp_token
*token
)
22725 gcc_assert (token
->keyword
== RID_THREAD
);
22726 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
22729 /* Set the location for a declarator specifier and check if it is
22732 DECL_SPECS is the sequence of declarator specifiers onto which to
22735 DS is the single declarator specifier to set which location is to
22736 be set onto the existing sequence of declarators.
22738 LOCATION is the location for the declarator specifier to
22742 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
22743 cp_decl_spec ds
, cp_token
*token
)
22745 gcc_assert (ds
< ds_last
);
22747 if (decl_specs
== NULL
)
22750 source_location location
= token
->location
;
22752 if (decl_specs
->locations
[ds
] == 0)
22754 decl_specs
->locations
[ds
] = location
;
22755 if (ds
== ds_thread
)
22756 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
22762 if (decl_specs
->locations
[ds_long_long
] != 0)
22763 error_at (location
,
22764 "%<long long long%> is too long for GCC");
22767 decl_specs
->locations
[ds_long_long
] = location
;
22768 pedwarn_cxx98 (location
,
22770 "ISO C++ 1998 does not support %<long long%>");
22773 else if (ds
== ds_thread
)
22775 bool gnu
= token_is__thread (token
);
22776 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
22777 error_at (location
,
22778 "both %<__thread%> and %<thread_local%> specified");
22780 error_at (location
, "duplicate %qD", token
->u
.value
);
22784 static const char *const decl_spec_names
[] = {
22801 error_at (location
,
22802 "duplicate %qs", decl_spec_names
[ds
]);
22807 /* Return true iff the declarator specifier DS is present in the
22808 sequence of declarator specifiers DECL_SPECS. */
22811 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
22814 gcc_assert (ds
< ds_last
);
22816 if (decl_specs
== NULL
)
22819 return decl_specs
->locations
[ds
] != 0;
22822 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22823 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22826 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
22828 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
22831 /* Issue an error message indicating that TOKEN_DESC was expected.
22832 If KEYWORD is true, it indicated this function is called by
22833 cp_parser_require_keword and the required token can only be
22834 a indicated keyword. */
22837 cp_parser_required_error (cp_parser
*parser
,
22838 required_token token_desc
,
22841 switch (token_desc
)
22844 cp_parser_error (parser
, "expected %<new%>");
22847 cp_parser_error (parser
, "expected %<delete%>");
22850 cp_parser_error (parser
, "expected %<return%>");
22853 cp_parser_error (parser
, "expected %<while%>");
22856 cp_parser_error (parser
, "expected %<extern%>");
22858 case RT_STATIC_ASSERT
:
22859 cp_parser_error (parser
, "expected %<static_assert%>");
22862 cp_parser_error (parser
, "expected %<decltype%>");
22865 cp_parser_error (parser
, "expected %<operator%>");
22868 cp_parser_error (parser
, "expected %<class%>");
22871 cp_parser_error (parser
, "expected %<template%>");
22874 cp_parser_error (parser
, "expected %<namespace%>");
22877 cp_parser_error (parser
, "expected %<using%>");
22880 cp_parser_error (parser
, "expected %<asm%>");
22883 cp_parser_error (parser
, "expected %<try%>");
22886 cp_parser_error (parser
, "expected %<catch%>");
22889 cp_parser_error (parser
, "expected %<throw%>");
22892 cp_parser_error (parser
, "expected %<__label__%>");
22895 cp_parser_error (parser
, "expected %<@try%>");
22897 case RT_AT_SYNCHRONIZED
:
22898 cp_parser_error (parser
, "expected %<@synchronized%>");
22901 cp_parser_error (parser
, "expected %<@throw%>");
22903 case RT_TRANSACTION_ATOMIC
:
22904 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
22906 case RT_TRANSACTION_RELAXED
:
22907 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
22914 switch (token_desc
)
22917 cp_parser_error (parser
, "expected %<;%>");
22919 case RT_OPEN_PAREN
:
22920 cp_parser_error (parser
, "expected %<(%>");
22922 case RT_CLOSE_BRACE
:
22923 cp_parser_error (parser
, "expected %<}%>");
22925 case RT_OPEN_BRACE
:
22926 cp_parser_error (parser
, "expected %<{%>");
22928 case RT_CLOSE_SQUARE
:
22929 cp_parser_error (parser
, "expected %<]%>");
22931 case RT_OPEN_SQUARE
:
22932 cp_parser_error (parser
, "expected %<[%>");
22935 cp_parser_error (parser
, "expected %<,%>");
22938 cp_parser_error (parser
, "expected %<::%>");
22941 cp_parser_error (parser
, "expected %<<%>");
22944 cp_parser_error (parser
, "expected %<>%>");
22947 cp_parser_error (parser
, "expected %<=%>");
22950 cp_parser_error (parser
, "expected %<...%>");
22953 cp_parser_error (parser
, "expected %<*%>");
22956 cp_parser_error (parser
, "expected %<~%>");
22959 cp_parser_error (parser
, "expected %<:%>");
22961 case RT_COLON_SCOPE
:
22962 cp_parser_error (parser
, "expected %<:%> or %<::%>");
22964 case RT_CLOSE_PAREN
:
22965 cp_parser_error (parser
, "expected %<)%>");
22967 case RT_COMMA_CLOSE_PAREN
:
22968 cp_parser_error (parser
, "expected %<,%> or %<)%>");
22970 case RT_PRAGMA_EOL
:
22971 cp_parser_error (parser
, "expected end of line");
22974 cp_parser_error (parser
, "expected identifier");
22977 cp_parser_error (parser
, "expected selection-statement");
22979 case RT_INTERATION
:
22980 cp_parser_error (parser
, "expected iteration-statement");
22983 cp_parser_error (parser
, "expected jump-statement");
22986 cp_parser_error (parser
, "expected class-key");
22988 case RT_CLASS_TYPENAME_TEMPLATE
:
22989 cp_parser_error (parser
,
22990 "expected %<class%>, %<typename%>, or %<template%>");
22993 gcc_unreachable ();
22997 gcc_unreachable ();
23002 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23003 issue an error message indicating that TOKEN_DESC was expected.
23005 Returns the token consumed, if the token had the appropriate type.
23006 Otherwise, returns NULL. */
23009 cp_parser_require (cp_parser
* parser
,
23010 enum cpp_ttype type
,
23011 required_token token_desc
)
23013 if (cp_lexer_next_token_is (parser
->lexer
, type
))
23014 return cp_lexer_consume_token (parser
->lexer
);
23017 /* Output the MESSAGE -- unless we're parsing tentatively. */
23018 if (!cp_parser_simulate_error (parser
))
23019 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
23024 /* An error message is produced if the next token is not '>'.
23025 All further tokens are skipped until the desired token is
23026 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23029 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
23031 /* Current level of '< ... >'. */
23032 unsigned level
= 0;
23033 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23034 unsigned nesting_depth
= 0;
23036 /* Are we ready, yet? If not, issue error message. */
23037 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
23040 /* Skip tokens until the desired token is found. */
23043 /* Peek at the next token. */
23044 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
23047 if (!nesting_depth
)
23052 if (cxx_dialect
== cxx98
)
23053 /* C++0x views the `>>' operator as two `>' tokens, but
23056 else if (!nesting_depth
&& level
-- == 0)
23058 /* We've hit a `>>' where the first `>' closes the
23059 template argument list, and the second `>' is
23060 spurious. Just consume the `>>' and stop; we've
23061 already produced at least one error. */
23062 cp_lexer_consume_token (parser
->lexer
);
23065 /* Fall through for C++0x, so we handle the second `>' in
23069 if (!nesting_depth
&& level
-- == 0)
23071 /* We've reached the token we want, consume it and stop. */
23072 cp_lexer_consume_token (parser
->lexer
);
23077 case CPP_OPEN_PAREN
:
23078 case CPP_OPEN_SQUARE
:
23082 case CPP_CLOSE_PAREN
:
23083 case CPP_CLOSE_SQUARE
:
23084 if (nesting_depth
-- == 0)
23089 case CPP_PRAGMA_EOL
:
23090 case CPP_SEMICOLON
:
23091 case CPP_OPEN_BRACE
:
23092 case CPP_CLOSE_BRACE
:
23093 /* The '>' was probably forgotten, don't look further. */
23100 /* Consume this token. */
23101 cp_lexer_consume_token (parser
->lexer
);
23105 /* If the next token is the indicated keyword, consume it. Otherwise,
23106 issue an error message indicating that TOKEN_DESC was expected.
23108 Returns the token consumed, if the token had the appropriate type.
23109 Otherwise, returns NULL. */
23112 cp_parser_require_keyword (cp_parser
* parser
,
23114 required_token token_desc
)
23116 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
23118 if (token
&& token
->keyword
!= keyword
)
23120 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
23127 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23128 function-definition. */
23131 cp_parser_token_starts_function_definition_p (cp_token
* token
)
23133 return (/* An ordinary function-body begins with an `{'. */
23134 token
->type
== CPP_OPEN_BRACE
23135 /* A ctor-initializer begins with a `:'. */
23136 || token
->type
== CPP_COLON
23137 /* A function-try-block begins with `try'. */
23138 || token
->keyword
== RID_TRY
23139 /* A function-transaction-block begins with `__transaction_atomic'
23140 or `__transaction_relaxed'. */
23141 || token
->keyword
== RID_TRANSACTION_ATOMIC
23142 || token
->keyword
== RID_TRANSACTION_RELAXED
23143 /* The named return value extension begins with `return'. */
23144 || token
->keyword
== RID_RETURN
);
23147 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23151 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
23155 token
= cp_lexer_peek_token (parser
->lexer
);
23156 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
23159 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23160 C++0x) ending a template-argument. */
23163 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
23167 token
= cp_lexer_peek_token (parser
->lexer
);
23168 return (token
->type
== CPP_COMMA
23169 || token
->type
== CPP_GREATER
23170 || token
->type
== CPP_ELLIPSIS
23171 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
23174 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23175 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23178 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
23183 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23184 if (token
->type
== CPP_LESS
)
23186 /* Check for the sequence `<::' in the original code. It would be lexed as
23187 `[:', where `[' is a digraph, and there is no whitespace before
23189 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
23192 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
23193 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
23199 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23200 or none_type otherwise. */
23202 static enum tag_types
23203 cp_parser_token_is_class_key (cp_token
* token
)
23205 switch (token
->keyword
)
23210 return record_type
;
23219 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23222 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
23224 if (type
== error_mark_node
)
23226 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
23228 permerror (input_location
, "%qs tag used in naming %q#T",
23229 class_key
== union_type
? "union"
23230 : class_key
== record_type
? "struct" : "class",
23232 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
23233 "%q#T was previously declared here", type
);
23237 /* Issue an error message if DECL is redeclared with different
23238 access than its original declaration [class.access.spec/3].
23239 This applies to nested classes and nested class templates.
23243 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
23245 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
23248 if ((TREE_PRIVATE (decl
)
23249 != (current_access_specifier
== access_private_node
))
23250 || (TREE_PROTECTED (decl
)
23251 != (current_access_specifier
== access_protected_node
)))
23252 error_at (location
, "%qD redeclared with different access", decl
);
23255 /* Look for the `template' keyword, as a syntactic disambiguator.
23256 Return TRUE iff it is present, in which case it will be
23260 cp_parser_optional_template_keyword (cp_parser
*parser
)
23262 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23264 /* In C++98 the `template' keyword can only be used within templates;
23265 outside templates the parser can always figure out what is a
23266 template and what is not. In C++11, per the resolution of DR 468,
23267 `template' is allowed in cases where it is not strictly necessary. */
23268 if (!processing_template_decl
23269 && pedantic
&& cxx_dialect
== cxx98
)
23271 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23272 pedwarn (token
->location
, OPT_Wpedantic
,
23273 "in C++98 %<template%> (as a disambiguator) is only "
23274 "allowed within templates");
23275 /* If this part of the token stream is rescanned, the same
23276 error message would be generated. So, we purge the token
23277 from the stream. */
23278 cp_lexer_purge_token (parser
->lexer
);
23283 /* Consume the `template' keyword. */
23284 cp_lexer_consume_token (parser
->lexer
);
23291 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23292 set PARSER->SCOPE, and perform other related actions. */
23295 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
23298 struct tree_check
*check_value
;
23299 deferred_access_check
*chk
;
23300 VEC (deferred_access_check
,gc
) *checks
;
23302 /* Get the stored value. */
23303 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
23304 /* Perform any access checks that were deferred. */
23305 checks
= check_value
->checks
;
23308 FOR_EACH_VEC_ELT (deferred_access_check
, checks
, i
, chk
)
23309 perform_or_defer_access_check (chk
->binfo
,
23311 chk
->diag_decl
, tf_warning_or_error
);
23313 /* Set the scope from the stored value. */
23314 parser
->scope
= check_value
->value
;
23315 parser
->qualifying_scope
= check_value
->qualifying_scope
;
23316 parser
->object_scope
= NULL_TREE
;
23319 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23320 encounter the end of a block before what we were looking for. */
23323 cp_parser_cache_group (cp_parser
*parser
,
23324 enum cpp_ttype end
,
23329 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23331 /* Abort a parenthesized expression if we encounter a semicolon. */
23332 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
23333 && token
->type
== CPP_SEMICOLON
)
23335 /* If we've reached the end of the file, stop. */
23336 if (token
->type
== CPP_EOF
23337 || (end
!= CPP_PRAGMA_EOL
23338 && token
->type
== CPP_PRAGMA_EOL
))
23340 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
23341 /* We've hit the end of an enclosing block, so there's been some
23342 kind of syntax error. */
23345 /* Consume the token. */
23346 cp_lexer_consume_token (parser
->lexer
);
23347 /* See if it starts a new group. */
23348 if (token
->type
== CPP_OPEN_BRACE
)
23350 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
23351 /* In theory this should probably check end == '}', but
23352 cp_parser_save_member_function_body needs it to exit
23353 after either '}' or ')' when called with ')'. */
23357 else if (token
->type
== CPP_OPEN_PAREN
)
23359 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
23360 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
23363 else if (token
->type
== CPP_PRAGMA
)
23364 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
23365 else if (token
->type
== end
)
23370 /* Like above, for caching a default argument or NSDMI. Both of these are
23371 terminated by a non-nested comma, but it can be unclear whether or not a
23372 comma is nested in a template argument list unless we do more parsing.
23373 In order to handle this ambiguity, when we encounter a ',' after a '<'
23374 we try to parse what follows as a parameter-declaration-list (in the
23375 case of a default argument) or a member-declarator (in the case of an
23376 NSDMI). If that succeeds, then we stop caching. */
23379 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
23381 unsigned depth
= 0;
23382 int maybe_template_id
= 0;
23383 cp_token
*first_token
;
23385 tree default_argument
;
23387 /* Add tokens until we have processed the entire default
23388 argument. We add the range [first_token, token). */
23389 first_token
= cp_lexer_peek_token (parser
->lexer
);
23390 if (first_token
->type
== CPP_OPEN_BRACE
)
23392 /* For list-initialization, this is straightforward. */
23393 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23394 token
= cp_lexer_peek_token (parser
->lexer
);
23400 /* Peek at the next token. */
23401 token
= cp_lexer_peek_token (parser
->lexer
);
23402 /* What we do depends on what token we have. */
23403 switch (token
->type
)
23405 /* In valid code, a default argument must be
23406 immediately followed by a `,' `)', or `...'. */
23408 if (depth
== 0 && maybe_template_id
)
23410 /* If we've seen a '<', we might be in a
23411 template-argument-list. Until Core issue 325 is
23412 resolved, we don't know how this situation ought
23413 to be handled, so try to DTRT. We check whether
23414 what comes after the comma is a valid parameter
23415 declaration list. If it is, then the comma ends
23416 the default argument; otherwise the default
23417 argument continues. */
23418 bool error
= false;
23421 /* Set ITALP so cp_parser_parameter_declaration_list
23422 doesn't decide to commit to this parse. */
23423 bool saved_italp
= parser
->in_template_argument_list_p
;
23424 parser
->in_template_argument_list_p
= true;
23426 cp_parser_parse_tentatively (parser
);
23427 cp_lexer_consume_token (parser
->lexer
);
23431 int ctor_dtor_or_conv_p
;
23432 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23433 &ctor_dtor_or_conv_p
,
23434 /*parenthesized_p=*/NULL
,
23435 /*member_p=*/true);
23439 begin_scope (sk_function_parms
, NULL_TREE
);
23440 cp_parser_parameter_declaration_list (parser
, &error
);
23441 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
23442 pop_binding (DECL_NAME (t
), t
);
23445 if (!cp_parser_error_occurred (parser
) && !error
)
23447 cp_parser_abort_tentative_parse (parser
);
23449 parser
->in_template_argument_list_p
= saved_italp
;
23452 case CPP_CLOSE_PAREN
:
23454 /* If we run into a non-nested `;', `}', or `]',
23455 then the code is invalid -- but the default
23456 argument is certainly over. */
23457 case CPP_SEMICOLON
:
23458 case CPP_CLOSE_BRACE
:
23459 case CPP_CLOSE_SQUARE
:
23462 /* Update DEPTH, if necessary. */
23463 else if (token
->type
== CPP_CLOSE_PAREN
23464 || token
->type
== CPP_CLOSE_BRACE
23465 || token
->type
== CPP_CLOSE_SQUARE
)
23469 case CPP_OPEN_PAREN
:
23470 case CPP_OPEN_SQUARE
:
23471 case CPP_OPEN_BRACE
:
23477 /* This might be the comparison operator, or it might
23478 start a template argument list. */
23479 ++maybe_template_id
;
23483 if (cxx_dialect
== cxx98
)
23485 /* Fall through for C++0x, which treats the `>>'
23486 operator like two `>' tokens in certain
23492 /* This might be an operator, or it might close a
23493 template argument list. But if a previous '<'
23494 started a template argument list, this will have
23495 closed it, so we can't be in one anymore. */
23496 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
23497 if (maybe_template_id
< 0)
23498 maybe_template_id
= 0;
23502 /* If we run out of tokens, issue an error message. */
23504 case CPP_PRAGMA_EOL
:
23505 error_at (token
->location
, "file ends in default argument");
23511 /* In these cases, we should look for template-ids.
23512 For example, if the default argument is
23513 `X<int, double>()', we need to do name lookup to
23514 figure out whether or not `X' is a template; if
23515 so, the `,' does not end the default argument.
23517 That is not yet done. */
23524 /* If we've reached the end, stop. */
23528 /* Add the token to the token block. */
23529 token
= cp_lexer_consume_token (parser
->lexer
);
23532 /* Create a DEFAULT_ARG to represent the unparsed default
23534 default_argument
= make_node (DEFAULT_ARG
);
23535 DEFARG_TOKENS (default_argument
)
23536 = cp_token_cache_new (first_token
, token
);
23537 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
23539 return default_argument
;
23542 /* Begin parsing tentatively. We always save tokens while parsing
23543 tentatively so that if the tentative parsing fails we can restore the
23547 cp_parser_parse_tentatively (cp_parser
* parser
)
23549 /* Enter a new parsing context. */
23550 parser
->context
= cp_parser_context_new (parser
->context
);
23551 /* Begin saving tokens. */
23552 cp_lexer_save_tokens (parser
->lexer
);
23553 /* In order to avoid repetitive access control error messages,
23554 access checks are queued up until we are no longer parsing
23556 push_deferring_access_checks (dk_deferred
);
23559 /* Commit to the currently active tentative parse. */
23562 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
23564 cp_parser_context
*context
;
23567 /* Mark all of the levels as committed. */
23568 lexer
= parser
->lexer
;
23569 for (context
= parser
->context
; context
->next
; context
= context
->next
)
23571 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
23573 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
23574 while (!cp_lexer_saving_tokens (lexer
))
23575 lexer
= lexer
->next
;
23576 cp_lexer_commit_tokens (lexer
);
23580 /* Abort the currently active tentative parse. All consumed tokens
23581 will be rolled back, and no diagnostics will be issued. */
23584 cp_parser_abort_tentative_parse (cp_parser
* parser
)
23586 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
23587 || errorcount
> 0);
23588 cp_parser_simulate_error (parser
);
23589 /* Now, pretend that we want to see if the construct was
23590 successfully parsed. */
23591 cp_parser_parse_definitely (parser
);
23594 /* Stop parsing tentatively. If a parse error has occurred, restore the
23595 token stream. Otherwise, commit to the tokens we have consumed.
23596 Returns true if no error occurred; false otherwise. */
23599 cp_parser_parse_definitely (cp_parser
* parser
)
23601 bool error_occurred
;
23602 cp_parser_context
*context
;
23604 /* Remember whether or not an error occurred, since we are about to
23605 destroy that information. */
23606 error_occurred
= cp_parser_error_occurred (parser
);
23607 /* Remove the topmost context from the stack. */
23608 context
= parser
->context
;
23609 parser
->context
= context
->next
;
23610 /* If no parse errors occurred, commit to the tentative parse. */
23611 if (!error_occurred
)
23613 /* Commit to the tokens read tentatively, unless that was
23615 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
23616 cp_lexer_commit_tokens (parser
->lexer
);
23618 pop_to_parent_deferring_access_checks ();
23620 /* Otherwise, if errors occurred, roll back our state so that things
23621 are just as they were before we began the tentative parse. */
23624 cp_lexer_rollback_tokens (parser
->lexer
);
23625 pop_deferring_access_checks ();
23627 /* Add the context to the front of the free list. */
23628 context
->next
= cp_parser_context_free_list
;
23629 cp_parser_context_free_list
= context
;
23631 return !error_occurred
;
23634 /* Returns true if we are parsing tentatively and are not committed to
23635 this tentative parse. */
23638 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
23640 return (cp_parser_parsing_tentatively (parser
)
23641 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
23644 /* Returns nonzero iff an error has occurred during the most recent
23645 tentative parse. */
23648 cp_parser_error_occurred (cp_parser
* parser
)
23650 return (cp_parser_parsing_tentatively (parser
)
23651 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
23654 /* Returns nonzero if GNU extensions are allowed. */
23657 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
23659 return parser
->allow_gnu_extensions_p
;
23662 /* Objective-C++ Productions */
23665 /* Parse an Objective-C expression, which feeds into a primary-expression
23669 objc-message-expression
23670 objc-string-literal
23671 objc-encode-expression
23672 objc-protocol-expression
23673 objc-selector-expression
23675 Returns a tree representation of the expression. */
23678 cp_parser_objc_expression (cp_parser
* parser
)
23680 /* Try to figure out what kind of declaration is present. */
23681 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
23685 case CPP_OPEN_SQUARE
:
23686 return cp_parser_objc_message_expression (parser
);
23688 case CPP_OBJC_STRING
:
23689 kwd
= cp_lexer_consume_token (parser
->lexer
);
23690 return objc_build_string_object (kwd
->u
.value
);
23693 switch (kwd
->keyword
)
23695 case RID_AT_ENCODE
:
23696 return cp_parser_objc_encode_expression (parser
);
23698 case RID_AT_PROTOCOL
:
23699 return cp_parser_objc_protocol_expression (parser
);
23701 case RID_AT_SELECTOR
:
23702 return cp_parser_objc_selector_expression (parser
);
23708 error_at (kwd
->location
,
23709 "misplaced %<@%D%> Objective-C++ construct",
23711 cp_parser_skip_to_end_of_block_or_statement (parser
);
23714 return error_mark_node
;
23717 /* Parse an Objective-C message expression.
23719 objc-message-expression:
23720 [ objc-message-receiver objc-message-args ]
23722 Returns a representation of an Objective-C message. */
23725 cp_parser_objc_message_expression (cp_parser
* parser
)
23727 tree receiver
, messageargs
;
23729 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
23730 receiver
= cp_parser_objc_message_receiver (parser
);
23731 messageargs
= cp_parser_objc_message_args (parser
);
23732 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23734 return objc_build_message_expr (receiver
, messageargs
);
23737 /* Parse an objc-message-receiver.
23739 objc-message-receiver:
23741 simple-type-specifier
23743 Returns a representation of the type or expression. */
23746 cp_parser_objc_message_receiver (cp_parser
* parser
)
23750 /* An Objective-C message receiver may be either (1) a type
23751 or (2) an expression. */
23752 cp_parser_parse_tentatively (parser
);
23753 rcv
= cp_parser_expression (parser
, false, NULL
);
23755 if (cp_parser_parse_definitely (parser
))
23758 rcv
= cp_parser_simple_type_specifier (parser
,
23759 /*decl_specs=*/NULL
,
23760 CP_PARSER_FLAGS_NONE
);
23762 return objc_get_class_reference (rcv
);
23765 /* Parse the arguments and selectors comprising an Objective-C message.
23770 objc-selector-args , objc-comma-args
23772 objc-selector-args:
23773 objc-selector [opt] : assignment-expression
23774 objc-selector-args objc-selector [opt] : assignment-expression
23777 assignment-expression
23778 objc-comma-args , assignment-expression
23780 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23781 selector arguments and TREE_VALUE containing a list of comma
23785 cp_parser_objc_message_args (cp_parser
* parser
)
23787 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
23788 bool maybe_unary_selector_p
= true;
23789 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23791 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
23793 tree selector
= NULL_TREE
, arg
;
23795 if (token
->type
!= CPP_COLON
)
23796 selector
= cp_parser_objc_selector (parser
);
23798 /* Detect if we have a unary selector. */
23799 if (maybe_unary_selector_p
23800 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
23801 return build_tree_list (selector
, NULL_TREE
);
23803 maybe_unary_selector_p
= false;
23804 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23805 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
23808 = chainon (sel_args
,
23809 build_tree_list (selector
, arg
));
23811 token
= cp_lexer_peek_token (parser
->lexer
);
23814 /* Handle non-selector arguments, if any. */
23815 while (token
->type
== CPP_COMMA
)
23819 cp_lexer_consume_token (parser
->lexer
);
23820 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
23823 = chainon (addl_args
,
23824 build_tree_list (NULL_TREE
, arg
));
23826 token
= cp_lexer_peek_token (parser
->lexer
);
23829 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
23831 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
23832 return build_tree_list (error_mark_node
, error_mark_node
);
23835 return build_tree_list (sel_args
, addl_args
);
23838 /* Parse an Objective-C encode expression.
23840 objc-encode-expression:
23841 @encode objc-typename
23843 Returns an encoded representation of the type argument. */
23846 cp_parser_objc_encode_expression (cp_parser
* parser
)
23851 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
23852 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23853 token
= cp_lexer_peek_token (parser
->lexer
);
23854 type
= complete_type (cp_parser_type_id (parser
));
23855 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23859 error_at (token
->location
,
23860 "%<@encode%> must specify a type as an argument");
23861 return error_mark_node
;
23864 /* This happens if we find @encode(T) (where T is a template
23865 typename or something dependent on a template typename) when
23866 parsing a template. In that case, we can't compile it
23867 immediately, but we rather create an AT_ENCODE_EXPR which will
23868 need to be instantiated when the template is used.
23870 if (dependent_type_p (type
))
23872 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
23873 TREE_READONLY (value
) = 1;
23877 return objc_build_encode_expr (type
);
23880 /* Parse an Objective-C @defs expression. */
23883 cp_parser_objc_defs_expression (cp_parser
*parser
)
23887 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
23888 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23889 name
= cp_parser_identifier (parser
);
23890 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23892 return objc_get_class_ivars (name
);
23895 /* Parse an Objective-C protocol expression.
23897 objc-protocol-expression:
23898 @protocol ( identifier )
23900 Returns a representation of the protocol expression. */
23903 cp_parser_objc_protocol_expression (cp_parser
* parser
)
23907 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
23908 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23909 proto
= cp_parser_identifier (parser
);
23910 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23912 return objc_build_protocol_expr (proto
);
23915 /* Parse an Objective-C selector expression.
23917 objc-selector-expression:
23918 @selector ( objc-method-signature )
23920 objc-method-signature:
23926 objc-selector-seq objc-selector :
23928 Returns a representation of the method selector. */
23931 cp_parser_objc_selector_expression (cp_parser
* parser
)
23933 tree sel_seq
= NULL_TREE
;
23934 bool maybe_unary_selector_p
= true;
23936 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23938 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
23939 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23940 token
= cp_lexer_peek_token (parser
->lexer
);
23942 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
23943 || token
->type
== CPP_SCOPE
)
23945 tree selector
= NULL_TREE
;
23947 if (token
->type
!= CPP_COLON
23948 || token
->type
== CPP_SCOPE
)
23949 selector
= cp_parser_objc_selector (parser
);
23951 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
23952 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
23954 /* Detect if we have a unary selector. */
23955 if (maybe_unary_selector_p
)
23957 sel_seq
= selector
;
23958 goto finish_selector
;
23962 cp_parser_error (parser
, "expected %<:%>");
23965 maybe_unary_selector_p
= false;
23966 token
= cp_lexer_consume_token (parser
->lexer
);
23968 if (token
->type
== CPP_SCOPE
)
23971 = chainon (sel_seq
,
23972 build_tree_list (selector
, NULL_TREE
));
23974 = chainon (sel_seq
,
23975 build_tree_list (NULL_TREE
, NULL_TREE
));
23979 = chainon (sel_seq
,
23980 build_tree_list (selector
, NULL_TREE
));
23982 token
= cp_lexer_peek_token (parser
->lexer
);
23986 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23988 return objc_build_selector_expr (loc
, sel_seq
);
23991 /* Parse a list of identifiers.
23993 objc-identifier-list:
23995 objc-identifier-list , identifier
23997 Returns a TREE_LIST of identifier nodes. */
24000 cp_parser_objc_identifier_list (cp_parser
* parser
)
24006 identifier
= cp_parser_identifier (parser
);
24007 if (identifier
== error_mark_node
)
24008 return error_mark_node
;
24010 list
= build_tree_list (NULL_TREE
, identifier
);
24011 sep
= cp_lexer_peek_token (parser
->lexer
);
24013 while (sep
->type
== CPP_COMMA
)
24015 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
24016 identifier
= cp_parser_identifier (parser
);
24017 if (identifier
== error_mark_node
)
24020 list
= chainon (list
, build_tree_list (NULL_TREE
,
24022 sep
= cp_lexer_peek_token (parser
->lexer
);
24028 /* Parse an Objective-C alias declaration.
24030 objc-alias-declaration:
24031 @compatibility_alias identifier identifier ;
24033 This function registers the alias mapping with the Objective-C front end.
24034 It returns nothing. */
24037 cp_parser_objc_alias_declaration (cp_parser
* parser
)
24041 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
24042 alias
= cp_parser_identifier (parser
);
24043 orig
= cp_parser_identifier (parser
);
24044 objc_declare_alias (alias
, orig
);
24045 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24048 /* Parse an Objective-C class forward-declaration.
24050 objc-class-declaration:
24051 @class objc-identifier-list ;
24053 The function registers the forward declarations with the Objective-C
24054 front end. It returns nothing. */
24057 cp_parser_objc_class_declaration (cp_parser
* parser
)
24059 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
24064 id
= cp_parser_identifier (parser
);
24065 if (id
== error_mark_node
)
24068 objc_declare_class (id
);
24070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24071 cp_lexer_consume_token (parser
->lexer
);
24075 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24078 /* Parse a list of Objective-C protocol references.
24080 objc-protocol-refs-opt:
24081 objc-protocol-refs [opt]
24083 objc-protocol-refs:
24084 < objc-identifier-list >
24086 Returns a TREE_LIST of identifiers, if any. */
24089 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
24091 tree protorefs
= NULL_TREE
;
24093 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
24095 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
24096 protorefs
= cp_parser_objc_identifier_list (parser
);
24097 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
24103 /* Parse a Objective-C visibility specification. */
24106 cp_parser_objc_visibility_spec (cp_parser
* parser
)
24108 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
24110 switch (vis
->keyword
)
24112 case RID_AT_PRIVATE
:
24113 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
24115 case RID_AT_PROTECTED
:
24116 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
24118 case RID_AT_PUBLIC
:
24119 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
24121 case RID_AT_PACKAGE
:
24122 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
24128 /* Eat '@private'/'@protected'/'@public'. */
24129 cp_lexer_consume_token (parser
->lexer
);
24132 /* Parse an Objective-C method type. Return 'true' if it is a class
24133 (+) method, and 'false' if it is an instance (-) method. */
24136 cp_parser_objc_method_type (cp_parser
* parser
)
24138 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
24144 /* Parse an Objective-C protocol qualifier. */
24147 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
24149 tree quals
= NULL_TREE
, node
;
24150 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24152 node
= token
->u
.value
;
24154 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
24155 && (node
== ridpointers
[(int) RID_IN
]
24156 || node
== ridpointers
[(int) RID_OUT
]
24157 || node
== ridpointers
[(int) RID_INOUT
]
24158 || node
== ridpointers
[(int) RID_BYCOPY
]
24159 || node
== ridpointers
[(int) RID_BYREF
]
24160 || node
== ridpointers
[(int) RID_ONEWAY
]))
24162 quals
= tree_cons (NULL_TREE
, node
, quals
);
24163 cp_lexer_consume_token (parser
->lexer
);
24164 token
= cp_lexer_peek_token (parser
->lexer
);
24165 node
= token
->u
.value
;
24171 /* Parse an Objective-C typename. */
24174 cp_parser_objc_typename (cp_parser
* parser
)
24176 tree type_name
= NULL_TREE
;
24178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24180 tree proto_quals
, cp_type
= NULL_TREE
;
24182 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
24183 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
24185 /* An ObjC type name may consist of just protocol qualifiers, in which
24186 case the type shall default to 'id'. */
24187 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
24189 cp_type
= cp_parser_type_id (parser
);
24191 /* If the type could not be parsed, an error has already
24192 been produced. For error recovery, behave as if it had
24193 not been specified, which will use the default type
24195 if (cp_type
== error_mark_node
)
24197 cp_type
= NULL_TREE
;
24198 /* We need to skip to the closing parenthesis as
24199 cp_parser_type_id() does not seem to do it for
24201 cp_parser_skip_to_closing_parenthesis (parser
,
24202 /*recovering=*/true,
24203 /*or_comma=*/false,
24204 /*consume_paren=*/false);
24208 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24209 type_name
= build_tree_list (proto_quals
, cp_type
);
24215 /* Check to see if TYPE refers to an Objective-C selector name. */
24218 cp_parser_objc_selector_p (enum cpp_ttype type
)
24220 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
24221 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
24222 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
24223 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
24224 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
24227 /* Parse an Objective-C selector. */
24230 cp_parser_objc_selector (cp_parser
* parser
)
24232 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
24234 if (!cp_parser_objc_selector_p (token
->type
))
24236 error_at (token
->location
, "invalid Objective-C++ selector name");
24237 return error_mark_node
;
24240 /* C++ operator names are allowed to appear in ObjC selectors. */
24241 switch (token
->type
)
24243 case CPP_AND_AND
: return get_identifier ("and");
24244 case CPP_AND_EQ
: return get_identifier ("and_eq");
24245 case CPP_AND
: return get_identifier ("bitand");
24246 case CPP_OR
: return get_identifier ("bitor");
24247 case CPP_COMPL
: return get_identifier ("compl");
24248 case CPP_NOT
: return get_identifier ("not");
24249 case CPP_NOT_EQ
: return get_identifier ("not_eq");
24250 case CPP_OR_OR
: return get_identifier ("or");
24251 case CPP_OR_EQ
: return get_identifier ("or_eq");
24252 case CPP_XOR
: return get_identifier ("xor");
24253 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
24254 default: return token
->u
.value
;
24258 /* Parse an Objective-C params list. */
24261 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
24263 tree params
= NULL_TREE
;
24264 bool maybe_unary_selector_p
= true;
24265 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24267 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
24269 tree selector
= NULL_TREE
, type_name
, identifier
;
24270 tree parm_attr
= NULL_TREE
;
24272 if (token
->keyword
== RID_ATTRIBUTE
)
24275 if (token
->type
!= CPP_COLON
)
24276 selector
= cp_parser_objc_selector (parser
);
24278 /* Detect if we have a unary selector. */
24279 if (maybe_unary_selector_p
24280 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
24282 params
= selector
; /* Might be followed by attributes. */
24286 maybe_unary_selector_p
= false;
24287 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
24289 /* Something went quite wrong. There should be a colon
24290 here, but there is not. Stop parsing parameters. */
24293 type_name
= cp_parser_objc_typename (parser
);
24294 /* New ObjC allows attributes on parameters too. */
24295 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
24296 parm_attr
= cp_parser_attributes_opt (parser
);
24297 identifier
= cp_parser_identifier (parser
);
24301 objc_build_keyword_decl (selector
,
24306 token
= cp_lexer_peek_token (parser
->lexer
);
24309 if (params
== NULL_TREE
)
24311 cp_parser_error (parser
, "objective-c++ method declaration is expected");
24312 return error_mark_node
;
24315 /* We allow tail attributes for the method. */
24316 if (token
->keyword
== RID_ATTRIBUTE
)
24318 *attributes
= cp_parser_attributes_opt (parser
);
24319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24320 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24322 cp_parser_error (parser
,
24323 "method attributes must be specified at the end");
24324 return error_mark_node
;
24327 if (params
== NULL_TREE
)
24329 cp_parser_error (parser
, "objective-c++ method declaration is expected");
24330 return error_mark_node
;
24335 /* Parse the non-keyword Objective-C params. */
24338 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
24341 tree params
= make_node (TREE_LIST
);
24342 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24343 *ellipsisp
= false; /* Initially, assume no ellipsis. */
24345 while (token
->type
== CPP_COMMA
)
24347 cp_parameter_declarator
*parmdecl
;
24350 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
24351 token
= cp_lexer_peek_token (parser
->lexer
);
24353 if (token
->type
== CPP_ELLIPSIS
)
24355 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
24357 token
= cp_lexer_peek_token (parser
->lexer
);
24361 /* TODO: parse attributes for tail parameters. */
24362 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
24363 parm
= grokdeclarator (parmdecl
->declarator
,
24364 &parmdecl
->decl_specifiers
,
24365 PARM
, /*initialized=*/0,
24366 /*attrlist=*/NULL
);
24368 chainon (params
, build_tree_list (NULL_TREE
, parm
));
24369 token
= cp_lexer_peek_token (parser
->lexer
);
24372 /* We allow tail attributes for the method. */
24373 if (token
->keyword
== RID_ATTRIBUTE
)
24375 if (*attributes
== NULL_TREE
)
24377 *attributes
= cp_parser_attributes_opt (parser
);
24378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24379 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24383 /* We have an error, but parse the attributes, so that we can
24385 *attributes
= cp_parser_attributes_opt (parser
);
24387 cp_parser_error (parser
,
24388 "method attributes must be specified at the end");
24389 return error_mark_node
;
24395 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24398 cp_parser_objc_interstitial_code (cp_parser
* parser
)
24400 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24402 /* If the next token is `extern' and the following token is a string
24403 literal, then we have a linkage specification. */
24404 if (token
->keyword
== RID_EXTERN
24405 && cp_parser_is_pure_string_literal
24406 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
24407 cp_parser_linkage_specification (parser
);
24408 /* Handle #pragma, if any. */
24409 else if (token
->type
== CPP_PRAGMA
)
24410 cp_parser_pragma (parser
, pragma_external
);
24411 /* Allow stray semicolons. */
24412 else if (token
->type
== CPP_SEMICOLON
)
24413 cp_lexer_consume_token (parser
->lexer
);
24414 /* Mark methods as optional or required, when building protocols. */
24415 else if (token
->keyword
== RID_AT_OPTIONAL
)
24417 cp_lexer_consume_token (parser
->lexer
);
24418 objc_set_method_opt (true);
24420 else if (token
->keyword
== RID_AT_REQUIRED
)
24422 cp_lexer_consume_token (parser
->lexer
);
24423 objc_set_method_opt (false);
24425 else if (token
->keyword
== RID_NAMESPACE
)
24426 cp_parser_namespace_definition (parser
);
24427 /* Other stray characters must generate errors. */
24428 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
24430 cp_lexer_consume_token (parser
->lexer
);
24431 error ("stray %qs between Objective-C++ methods",
24432 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
24434 /* Finally, try to parse a block-declaration, or a function-definition. */
24436 cp_parser_block_declaration (parser
, /*statement_p=*/false);
24439 /* Parse a method signature. */
24442 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
24444 tree rettype
, kwdparms
, optparms
;
24445 bool ellipsis
= false;
24446 bool is_class_method
;
24448 is_class_method
= cp_parser_objc_method_type (parser
);
24449 rettype
= cp_parser_objc_typename (parser
);
24450 *attributes
= NULL_TREE
;
24451 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
24452 if (kwdparms
== error_mark_node
)
24453 return error_mark_node
;
24454 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
24455 if (optparms
== error_mark_node
)
24456 return error_mark_node
;
24458 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
24462 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
24465 cp_lexer_save_tokens (parser
->lexer
);
24466 tattr
= cp_parser_attributes_opt (parser
);
24467 gcc_assert (tattr
) ;
24469 /* If the attributes are followed by a method introducer, this is not allowed.
24470 Dump the attributes and flag the situation. */
24471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
24472 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
24475 /* Otherwise, the attributes introduce some interstitial code, possibly so
24476 rewind to allow that check. */
24477 cp_lexer_rollback_tokens (parser
->lexer
);
24481 /* Parse an Objective-C method prototype list. */
24484 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
24486 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24488 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
24490 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
24492 tree attributes
, sig
;
24493 bool is_class_method
;
24494 if (token
->type
== CPP_PLUS
)
24495 is_class_method
= true;
24497 is_class_method
= false;
24498 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
24499 if (sig
== error_mark_node
)
24501 cp_parser_skip_to_end_of_block_or_statement (parser
);
24502 token
= cp_lexer_peek_token (parser
->lexer
);
24505 objc_add_method_declaration (is_class_method
, sig
, attributes
);
24506 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24508 else if (token
->keyword
== RID_AT_PROPERTY
)
24509 cp_parser_objc_at_property_declaration (parser
);
24510 else if (token
->keyword
== RID_ATTRIBUTE
24511 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
24512 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
24514 "prefix attributes are ignored for methods");
24516 /* Allow for interspersed non-ObjC++ code. */
24517 cp_parser_objc_interstitial_code (parser
);
24519 token
= cp_lexer_peek_token (parser
->lexer
);
24522 if (token
->type
!= CPP_EOF
)
24523 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
24525 cp_parser_error (parser
, "expected %<@end%>");
24527 objc_finish_interface ();
24530 /* Parse an Objective-C method definition list. */
24533 cp_parser_objc_method_definition_list (cp_parser
* parser
)
24535 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24537 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
24541 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
24544 tree sig
, attribute
;
24545 bool is_class_method
;
24546 if (token
->type
== CPP_PLUS
)
24547 is_class_method
= true;
24549 is_class_method
= false;
24550 push_deferring_access_checks (dk_deferred
);
24551 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
24552 if (sig
== error_mark_node
)
24554 cp_parser_skip_to_end_of_block_or_statement (parser
);
24555 token
= cp_lexer_peek_token (parser
->lexer
);
24558 objc_start_method_definition (is_class_method
, sig
, attribute
,
24561 /* For historical reasons, we accept an optional semicolon. */
24562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24563 cp_lexer_consume_token (parser
->lexer
);
24565 ptk
= cp_lexer_peek_token (parser
->lexer
);
24566 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
24567 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
24569 perform_deferred_access_checks (tf_warning_or_error
);
24570 stop_deferring_access_checks ();
24571 meth
= cp_parser_function_definition_after_declarator (parser
,
24573 pop_deferring_access_checks ();
24574 objc_finish_method_definition (meth
);
24577 /* The following case will be removed once @synthesize is
24578 completely implemented. */
24579 else if (token
->keyword
== RID_AT_PROPERTY
)
24580 cp_parser_objc_at_property_declaration (parser
);
24581 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
24582 cp_parser_objc_at_synthesize_declaration (parser
);
24583 else if (token
->keyword
== RID_AT_DYNAMIC
)
24584 cp_parser_objc_at_dynamic_declaration (parser
);
24585 else if (token
->keyword
== RID_ATTRIBUTE
24586 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
24587 warning_at (token
->location
, OPT_Wattributes
,
24588 "prefix attributes are ignored for methods");
24590 /* Allow for interspersed non-ObjC++ code. */
24591 cp_parser_objc_interstitial_code (parser
);
24593 token
= cp_lexer_peek_token (parser
->lexer
);
24596 if (token
->type
!= CPP_EOF
)
24597 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
24599 cp_parser_error (parser
, "expected %<@end%>");
24601 objc_finish_implementation ();
24604 /* Parse Objective-C ivars. */
24607 cp_parser_objc_class_ivars (cp_parser
* parser
)
24609 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24611 if (token
->type
!= CPP_OPEN_BRACE
)
24612 return; /* No ivars specified. */
24614 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
24615 token
= cp_lexer_peek_token (parser
->lexer
);
24617 while (token
->type
!= CPP_CLOSE_BRACE
24618 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
24620 cp_decl_specifier_seq declspecs
;
24621 int decl_class_or_enum_p
;
24622 tree prefix_attributes
;
24624 cp_parser_objc_visibility_spec (parser
);
24626 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
24629 cp_parser_decl_specifier_seq (parser
,
24630 CP_PARSER_FLAGS_OPTIONAL
,
24632 &decl_class_or_enum_p
);
24634 /* auto, register, static, extern, mutable. */
24635 if (declspecs
.storage_class
!= sc_none
)
24637 cp_parser_error (parser
, "invalid type for instance variable");
24638 declspecs
.storage_class
= sc_none
;
24641 /* thread_local. */
24642 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
24644 cp_parser_error (parser
, "invalid type for instance variable");
24645 declspecs
.locations
[ds_thread
] = 0;
24649 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
24651 cp_parser_error (parser
, "invalid type for instance variable");
24652 declspecs
.locations
[ds_thread
] = 0;
24655 prefix_attributes
= declspecs
.attributes
;
24656 declspecs
.attributes
= NULL_TREE
;
24658 /* Keep going until we hit the `;' at the end of the
24660 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24662 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
24663 cp_declarator
*declarator
= NULL
;
24664 int ctor_dtor_or_conv_p
;
24666 /* Check for a (possibly unnamed) bitfield declaration. */
24667 token
= cp_lexer_peek_token (parser
->lexer
);
24668 if (token
->type
== CPP_COLON
)
24671 if (token
->type
== CPP_NAME
24672 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
24675 /* Get the name of the bitfield. */
24676 declarator
= make_id_declarator (NULL_TREE
,
24677 cp_parser_identifier (parser
),
24681 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
24682 /* Get the width of the bitfield. */
24684 = cp_parser_constant_expression (parser
,
24685 /*allow_non_constant=*/false,
24690 /* Parse the declarator. */
24692 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24693 &ctor_dtor_or_conv_p
,
24694 /*parenthesized_p=*/NULL
,
24695 /*member_p=*/false);
24698 /* Look for attributes that apply to the ivar. */
24699 attributes
= cp_parser_attributes_opt (parser
);
24700 /* Remember which attributes are prefix attributes and
24702 first_attribute
= attributes
;
24703 /* Combine the attributes. */
24704 attributes
= chainon (prefix_attributes
, attributes
);
24707 /* Create the bitfield declaration. */
24708 decl
= grokbitfield (declarator
, &declspecs
,
24712 decl
= grokfield (declarator
, &declspecs
,
24713 NULL_TREE
, /*init_const_expr_p=*/false,
24714 NULL_TREE
, attributes
);
24716 /* Add the instance variable. */
24717 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
24718 objc_add_instance_variable (decl
);
24720 /* Reset PREFIX_ATTRIBUTES. */
24721 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24722 attributes
= TREE_CHAIN (attributes
);
24724 TREE_CHAIN (attributes
) = NULL_TREE
;
24726 token
= cp_lexer_peek_token (parser
->lexer
);
24728 if (token
->type
== CPP_COMMA
)
24730 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
24736 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24737 token
= cp_lexer_peek_token (parser
->lexer
);
24740 if (token
->keyword
== RID_AT_END
)
24741 cp_parser_error (parser
, "expected %<}%>");
24743 /* Do not consume the RID_AT_END, so it will be read again as terminating
24744 the @interface of @implementation. */
24745 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
24746 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
24748 /* For historical reasons, we accept an optional semicolon. */
24749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24750 cp_lexer_consume_token (parser
->lexer
);
24753 /* Parse an Objective-C protocol declaration. */
24756 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
24758 tree proto
, protorefs
;
24761 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
24762 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
24764 tok
= cp_lexer_peek_token (parser
->lexer
);
24765 error_at (tok
->location
, "identifier expected after %<@protocol%>");
24766 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24770 /* See if we have a forward declaration or a definition. */
24771 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24773 /* Try a forward declaration first. */
24774 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
24780 id
= cp_parser_identifier (parser
);
24781 if (id
== error_mark_node
)
24784 objc_declare_protocol (id
, attributes
);
24786 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24787 cp_lexer_consume_token (parser
->lexer
);
24791 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24794 /* Ok, we got a full-fledged definition (or at least should). */
24797 proto
= cp_parser_identifier (parser
);
24798 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
24799 objc_start_protocol (proto
, protorefs
, attributes
);
24800 cp_parser_objc_method_prototype_list (parser
);
24804 /* Parse an Objective-C superclass or category. */
24807 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
24810 tree
*categ
, bool *is_class_extension
)
24812 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
24814 *super
= *categ
= NULL_TREE
;
24815 *is_class_extension
= false;
24816 if (next
->type
== CPP_COLON
)
24818 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
24819 *super
= cp_parser_identifier (parser
);
24821 else if (next
->type
== CPP_OPEN_PAREN
)
24823 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
24825 /* If there is no category name, and this is an @interface, we
24826 have a class extension. */
24827 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24829 *categ
= NULL_TREE
;
24830 *is_class_extension
= true;
24833 *categ
= cp_parser_identifier (parser
);
24835 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24839 /* Parse an Objective-C class interface. */
24842 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
24844 tree name
, super
, categ
, protos
;
24845 bool is_class_extension
;
24847 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
24848 name
= cp_parser_identifier (parser
);
24849 if (name
== error_mark_node
)
24851 /* It's hard to recover because even if valid @interface stuff
24852 is to follow, we can't compile it (or validate it) if we
24853 don't even know which class it refers to. Let's assume this
24854 was a stray '@interface' token in the stream and skip it.
24858 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
24859 &is_class_extension
);
24860 protos
= cp_parser_objc_protocol_refs_opt (parser
);
24862 /* We have either a class or a category on our hands. */
24863 if (categ
|| is_class_extension
)
24864 objc_start_category_interface (name
, categ
, protos
, attributes
);
24867 objc_start_class_interface (name
, super
, protos
, attributes
);
24868 /* Handle instance variable declarations, if any. */
24869 cp_parser_objc_class_ivars (parser
);
24870 objc_continue_interface ();
24873 cp_parser_objc_method_prototype_list (parser
);
24876 /* Parse an Objective-C class implementation. */
24879 cp_parser_objc_class_implementation (cp_parser
* parser
)
24881 tree name
, super
, categ
;
24882 bool is_class_extension
;
24884 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
24885 name
= cp_parser_identifier (parser
);
24886 if (name
== error_mark_node
)
24888 /* It's hard to recover because even if valid @implementation
24889 stuff is to follow, we can't compile it (or validate it) if
24890 we don't even know which class it refers to. Let's assume
24891 this was a stray '@implementation' token in the stream and
24896 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
24897 &is_class_extension
);
24899 /* We have either a class or a category on our hands. */
24901 objc_start_category_implementation (name
, categ
);
24904 objc_start_class_implementation (name
, super
);
24905 /* Handle instance variable declarations, if any. */
24906 cp_parser_objc_class_ivars (parser
);
24907 objc_continue_implementation ();
24910 cp_parser_objc_method_definition_list (parser
);
24913 /* Consume the @end token and finish off the implementation. */
24916 cp_parser_objc_end_implementation (cp_parser
* parser
)
24918 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
24919 objc_finish_implementation ();
24922 /* Parse an Objective-C declaration. */
24925 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
24927 /* Try to figure out what kind of declaration is present. */
24928 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
24931 switch (kwd
->keyword
)
24936 error_at (kwd
->location
, "attributes may not be specified before"
24937 " the %<@%D%> Objective-C++ keyword",
24941 case RID_AT_IMPLEMENTATION
:
24942 warning_at (kwd
->location
, OPT_Wattributes
,
24943 "prefix attributes are ignored before %<@%D%>",
24950 switch (kwd
->keyword
)
24953 cp_parser_objc_alias_declaration (parser
);
24956 cp_parser_objc_class_declaration (parser
);
24958 case RID_AT_PROTOCOL
:
24959 cp_parser_objc_protocol_declaration (parser
, attributes
);
24961 case RID_AT_INTERFACE
:
24962 cp_parser_objc_class_interface (parser
, attributes
);
24964 case RID_AT_IMPLEMENTATION
:
24965 cp_parser_objc_class_implementation (parser
);
24968 cp_parser_objc_end_implementation (parser
);
24971 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
24973 cp_parser_skip_to_end_of_block_or_statement (parser
);
24977 /* Parse an Objective-C try-catch-finally statement.
24979 objc-try-catch-finally-stmt:
24980 @try compound-statement objc-catch-clause-seq [opt]
24981 objc-finally-clause [opt]
24983 objc-catch-clause-seq:
24984 objc-catch-clause objc-catch-clause-seq [opt]
24987 @catch ( objc-exception-declaration ) compound-statement
24989 objc-finally-clause:
24990 @finally compound-statement
24992 objc-exception-declaration:
24993 parameter-declaration
24996 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25000 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25001 for C. Keep them in sync. */
25004 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
25006 location_t location
;
25009 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
25010 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
25011 objc_maybe_warn_exceptions (location
);
25012 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25013 node, lest it get absorbed into the surrounding block. */
25014 stmt
= push_stmt_list ();
25015 cp_parser_compound_statement (parser
, NULL
, false, false);
25016 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
25018 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
25020 cp_parameter_declarator
*parm
;
25021 tree parameter_declaration
= error_mark_node
;
25022 bool seen_open_paren
= false;
25024 cp_lexer_consume_token (parser
->lexer
);
25025 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25026 seen_open_paren
= true;
25027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25029 /* We have "@catch (...)" (where the '...' are literally
25030 what is in the code). Skip the '...'.
25031 parameter_declaration is set to NULL_TREE, and
25032 objc_being_catch_clauses() knows that that means
25034 cp_lexer_consume_token (parser
->lexer
);
25035 parameter_declaration
= NULL_TREE
;
25039 /* We have "@catch (NSException *exception)" or something
25040 like that. Parse the parameter declaration. */
25041 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
25043 parameter_declaration
= error_mark_node
;
25045 parameter_declaration
= grokdeclarator (parm
->declarator
,
25046 &parm
->decl_specifiers
,
25047 PARM
, /*initialized=*/0,
25048 /*attrlist=*/NULL
);
25050 if (seen_open_paren
)
25051 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25054 /* If there was no open parenthesis, we are recovering from
25055 an error, and we are trying to figure out what mistake
25056 the user has made. */
25058 /* If there is an immediate closing parenthesis, the user
25059 probably forgot the opening one (ie, they typed "@catch
25060 NSException *e)". Parse the closing parenthesis and keep
25062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25063 cp_lexer_consume_token (parser
->lexer
);
25065 /* If these is no immediate closing parenthesis, the user
25066 probably doesn't know that parenthesis are required at
25067 all (ie, they typed "@catch NSException *e"). So, just
25068 forget about the closing parenthesis and keep going. */
25070 objc_begin_catch_clause (parameter_declaration
);
25071 cp_parser_compound_statement (parser
, NULL
, false, false);
25072 objc_finish_catch_clause ();
25074 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
25076 cp_lexer_consume_token (parser
->lexer
);
25077 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
25078 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25079 node, lest it get absorbed into the surrounding block. */
25080 stmt
= push_stmt_list ();
25081 cp_parser_compound_statement (parser
, NULL
, false, false);
25082 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
25085 return objc_finish_try_stmt ();
25088 /* Parse an Objective-C synchronized statement.
25090 objc-synchronized-stmt:
25091 @synchronized ( expression ) compound-statement
25093 Returns NULL_TREE. */
25096 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
25098 location_t location
;
25101 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
25103 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
25104 objc_maybe_warn_exceptions (location
);
25105 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25106 lock
= cp_parser_expression (parser
, false, NULL
);
25107 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25109 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25110 node, lest it get absorbed into the surrounding block. */
25111 stmt
= push_stmt_list ();
25112 cp_parser_compound_statement (parser
, NULL
, false, false);
25114 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
25117 /* Parse an Objective-C throw statement.
25120 @throw assignment-expression [opt] ;
25122 Returns a constructed '@throw' statement. */
25125 cp_parser_objc_throw_statement (cp_parser
*parser
)
25127 tree expr
= NULL_TREE
;
25128 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25130 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
25132 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25133 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
25135 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25137 return objc_build_throw_stmt (loc
, expr
);
25140 /* Parse an Objective-C statement. */
25143 cp_parser_objc_statement (cp_parser
* parser
)
25145 /* Try to figure out what kind of declaration is present. */
25146 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25148 switch (kwd
->keyword
)
25151 return cp_parser_objc_try_catch_finally_statement (parser
);
25152 case RID_AT_SYNCHRONIZED
:
25153 return cp_parser_objc_synchronized_statement (parser
);
25155 return cp_parser_objc_throw_statement (parser
);
25157 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
25159 cp_parser_skip_to_end_of_block_or_statement (parser
);
25162 return error_mark_node
;
25165 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25166 look ahead to see if an objc keyword follows the attributes. This
25167 is to detect the use of prefix attributes on ObjC @interface and
25171 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
25173 cp_lexer_save_tokens (parser
->lexer
);
25174 *attrib
= cp_parser_attributes_opt (parser
);
25175 gcc_assert (*attrib
);
25176 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
25178 cp_lexer_commit_tokens (parser
->lexer
);
25181 cp_lexer_rollback_tokens (parser
->lexer
);
25185 /* This routine is a minimal replacement for
25186 c_parser_struct_declaration () used when parsing the list of
25187 types/names or ObjC++ properties. For example, when parsing the
25190 @property (readonly) int a, b, c;
25192 this function is responsible for parsing "int a, int b, int c" and
25193 returning the declarations as CHAIN of DECLs.
25195 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25196 similar parsing. */
25198 cp_parser_objc_struct_declaration (cp_parser
*parser
)
25200 tree decls
= NULL_TREE
;
25201 cp_decl_specifier_seq declspecs
;
25202 int decl_class_or_enum_p
;
25203 tree prefix_attributes
;
25205 cp_parser_decl_specifier_seq (parser
,
25206 CP_PARSER_FLAGS_NONE
,
25208 &decl_class_or_enum_p
);
25210 if (declspecs
.type
== error_mark_node
)
25211 return error_mark_node
;
25213 /* auto, register, static, extern, mutable. */
25214 if (declspecs
.storage_class
!= sc_none
)
25216 cp_parser_error (parser
, "invalid type for property");
25217 declspecs
.storage_class
= sc_none
;
25220 /* thread_local. */
25221 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
25223 cp_parser_error (parser
, "invalid type for property");
25224 declspecs
.locations
[ds_thread
] = 0;
25228 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
25230 cp_parser_error (parser
, "invalid type for property");
25231 declspecs
.locations
[ds_typedef
] = 0;
25234 prefix_attributes
= declspecs
.attributes
;
25235 declspecs
.attributes
= NULL_TREE
;
25237 /* Keep going until we hit the `;' at the end of the declaration. */
25238 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25240 tree attributes
, first_attribute
, decl
;
25241 cp_declarator
*declarator
;
25244 /* Parse the declarator. */
25245 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25246 NULL
, NULL
, false);
25248 /* Look for attributes that apply to the ivar. */
25249 attributes
= cp_parser_attributes_opt (parser
);
25250 /* Remember which attributes are prefix attributes and
25252 first_attribute
= attributes
;
25253 /* Combine the attributes. */
25254 attributes
= chainon (prefix_attributes
, attributes
);
25256 decl
= grokfield (declarator
, &declspecs
,
25257 NULL_TREE
, /*init_const_expr_p=*/false,
25258 NULL_TREE
, attributes
);
25260 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
25261 return error_mark_node
;
25263 /* Reset PREFIX_ATTRIBUTES. */
25264 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
25265 attributes
= TREE_CHAIN (attributes
);
25267 TREE_CHAIN (attributes
) = NULL_TREE
;
25269 DECL_CHAIN (decl
) = decls
;
25272 token
= cp_lexer_peek_token (parser
->lexer
);
25273 if (token
->type
== CPP_COMMA
)
25275 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25284 /* Parse an Objective-C @property declaration. The syntax is:
25286 objc-property-declaration:
25287 '@property' objc-property-attributes[opt] struct-declaration ;
25289 objc-property-attributes:
25290 '(' objc-property-attribute-list ')'
25292 objc-property-attribute-list:
25293 objc-property-attribute
25294 objc-property-attribute-list, objc-property-attribute
25296 objc-property-attribute
25297 'getter' = identifier
25298 'setter' = identifier
25307 @property NSString *name;
25308 @property (readonly) id object;
25309 @property (retain, nonatomic, getter=getTheName) id name;
25310 @property int a, b, c;
25312 PS: This function is identical to
25313 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25315 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
25317 /* The following variables hold the attributes of the properties as
25318 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25319 seen. When we see an attribute, we set them to 'true' (if they
25320 are boolean properties) or to the identifier (if they have an
25321 argument, ie, for getter and setter). Note that here we only
25322 parse the list of attributes, check the syntax and accumulate the
25323 attributes that we find. objc_add_property_declaration() will
25324 then process the information. */
25325 bool property_assign
= false;
25326 bool property_copy
= false;
25327 tree property_getter_ident
= NULL_TREE
;
25328 bool property_nonatomic
= false;
25329 bool property_readonly
= false;
25330 bool property_readwrite
= false;
25331 bool property_retain
= false;
25332 tree property_setter_ident
= NULL_TREE
;
25334 /* 'properties' is the list of properties that we read. Usually a
25335 single one, but maybe more (eg, in "@property int a, b, c;" there
25340 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25342 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
25344 /* Parse the optional attribute list... */
25345 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25348 cp_lexer_consume_token (parser
->lexer
);
25352 bool syntax_error
= false;
25353 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25356 if (token
->type
!= CPP_NAME
)
25358 cp_parser_error (parser
, "expected identifier");
25361 keyword
= C_RID_CODE (token
->u
.value
);
25362 cp_lexer_consume_token (parser
->lexer
);
25365 case RID_ASSIGN
: property_assign
= true; break;
25366 case RID_COPY
: property_copy
= true; break;
25367 case RID_NONATOMIC
: property_nonatomic
= true; break;
25368 case RID_READONLY
: property_readonly
= true; break;
25369 case RID_READWRITE
: property_readwrite
= true; break;
25370 case RID_RETAIN
: property_retain
= true; break;
25374 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
25376 if (keyword
== RID_GETTER
)
25377 cp_parser_error (parser
,
25378 "missing %<=%> (after %<getter%> attribute)");
25380 cp_parser_error (parser
,
25381 "missing %<=%> (after %<setter%> attribute)");
25382 syntax_error
= true;
25385 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
25386 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
25388 cp_parser_error (parser
, "expected identifier");
25389 syntax_error
= true;
25392 if (keyword
== RID_SETTER
)
25394 if (property_setter_ident
!= NULL_TREE
)
25396 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
25397 cp_lexer_consume_token (parser
->lexer
);
25400 property_setter_ident
= cp_parser_objc_selector (parser
);
25401 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25402 cp_parser_error (parser
, "setter name must terminate with %<:%>");
25404 cp_lexer_consume_token (parser
->lexer
);
25408 if (property_getter_ident
!= NULL_TREE
)
25410 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
25411 cp_lexer_consume_token (parser
->lexer
);
25414 property_getter_ident
= cp_parser_objc_selector (parser
);
25418 cp_parser_error (parser
, "unknown property attribute");
25419 syntax_error
= true;
25426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25427 cp_lexer_consume_token (parser
->lexer
);
25432 /* FIXME: "@property (setter, assign);" will generate a spurious
25433 "error: expected ‘)’ before ‘,’ token". This is because
25434 cp_parser_require, unlike the C counterpart, will produce an
25435 error even if we are in error recovery. */
25436 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25438 cp_parser_skip_to_closing_parenthesis (parser
,
25439 /*recovering=*/true,
25440 /*or_comma=*/false,
25441 /*consume_paren=*/true);
25445 /* ... and the property declaration(s). */
25446 properties
= cp_parser_objc_struct_declaration (parser
);
25448 if (properties
== error_mark_node
)
25450 cp_parser_skip_to_end_of_statement (parser
);
25451 /* If the next token is now a `;', consume it. */
25452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25453 cp_lexer_consume_token (parser
->lexer
);
25457 if (properties
== NULL_TREE
)
25458 cp_parser_error (parser
, "expected identifier");
25461 /* Comma-separated properties are chained together in
25462 reverse order; add them one by one. */
25463 properties
= nreverse (properties
);
25465 for (; properties
; properties
= TREE_CHAIN (properties
))
25466 objc_add_property_declaration (loc
, copy_node (properties
),
25467 property_readonly
, property_readwrite
,
25468 property_assign
, property_retain
,
25469 property_copy
, property_nonatomic
,
25470 property_getter_ident
, property_setter_ident
);
25473 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25476 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25478 objc-synthesize-declaration:
25479 @synthesize objc-synthesize-identifier-list ;
25481 objc-synthesize-identifier-list:
25482 objc-synthesize-identifier
25483 objc-synthesize-identifier-list, objc-synthesize-identifier
25485 objc-synthesize-identifier
25487 identifier = identifier
25490 @synthesize MyProperty;
25491 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25493 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25494 for C. Keep them in sync.
25497 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
25499 tree list
= NULL_TREE
;
25501 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25503 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
25506 tree property
, ivar
;
25507 property
= cp_parser_identifier (parser
);
25508 if (property
== error_mark_node
)
25510 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25513 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
25515 cp_lexer_consume_token (parser
->lexer
);
25516 ivar
= cp_parser_identifier (parser
);
25517 if (ivar
== error_mark_node
)
25519 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25525 list
= chainon (list
, build_tree_list (ivar
, property
));
25526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25527 cp_lexer_consume_token (parser
->lexer
);
25531 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25532 objc_add_synthesize_declaration (loc
, list
);
25535 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25537 objc-dynamic-declaration:
25538 @dynamic identifier-list ;
25541 @dynamic MyProperty;
25542 @dynamic MyProperty, AnotherProperty;
25544 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25545 for C. Keep them in sync.
25548 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
25550 tree list
= NULL_TREE
;
25552 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25554 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
25558 property
= cp_parser_identifier (parser
);
25559 if (property
== error_mark_node
)
25561 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25564 list
= chainon (list
, build_tree_list (NULL
, property
));
25565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25566 cp_lexer_consume_token (parser
->lexer
);
25570 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25571 objc_add_dynamic_declaration (loc
, list
);
25575 /* OpenMP 2.5 parsing routines. */
25577 /* Returns name of the next clause.
25578 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25579 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25580 returned and the token is consumed. */
25582 static pragma_omp_clause
25583 cp_parser_omp_clause_name (cp_parser
*parser
)
25585 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
25587 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
25588 result
= PRAGMA_OMP_CLAUSE_IF
;
25589 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
25590 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
25591 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
25592 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
25593 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
25595 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
25596 const char *p
= IDENTIFIER_POINTER (id
);
25601 if (!strcmp ("collapse", p
))
25602 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
25603 else if (!strcmp ("copyin", p
))
25604 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
25605 else if (!strcmp ("copyprivate", p
))
25606 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
25609 if (!strcmp ("final", p
))
25610 result
= PRAGMA_OMP_CLAUSE_FINAL
;
25611 else if (!strcmp ("firstprivate", p
))
25612 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
25615 if (!strcmp ("lastprivate", p
))
25616 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
25619 if (!strcmp ("mergeable", p
))
25620 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
25623 if (!strcmp ("nowait", p
))
25624 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
25625 else if (!strcmp ("num_threads", p
))
25626 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
25629 if (!strcmp ("ordered", p
))
25630 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
25633 if (!strcmp ("reduction", p
))
25634 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
25637 if (!strcmp ("schedule", p
))
25638 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
25639 else if (!strcmp ("shared", p
))
25640 result
= PRAGMA_OMP_CLAUSE_SHARED
;
25643 if (!strcmp ("untied", p
))
25644 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
25649 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
25650 cp_lexer_consume_token (parser
->lexer
);
25655 /* Validate that a clause of the given type does not already exist. */
25658 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
25659 const char *name
, location_t location
)
25663 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
25664 if (OMP_CLAUSE_CODE (c
) == code
)
25666 error_at (location
, "too many %qs clauses", name
);
25674 variable-list , identifier
25676 In addition, we match a closing parenthesis. An opening parenthesis
25677 will have been consumed by the caller.
25679 If KIND is nonzero, create the appropriate node and install the decl
25680 in OMP_CLAUSE_DECL and add the node to the head of the list.
25682 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25683 return the list created. */
25686 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
25694 token
= cp_lexer_peek_token (parser
->lexer
);
25695 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
25696 /*check_dependency_p=*/true,
25697 /*template_p=*/NULL
,
25698 /*declarator_p=*/false,
25699 /*optional_p=*/false);
25700 if (name
== error_mark_node
)
25703 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
25704 if (decl
== error_mark_node
)
25705 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
25707 else if (kind
!= 0)
25709 tree u
= build_omp_clause (token
->location
, kind
);
25710 OMP_CLAUSE_DECL (u
) = decl
;
25711 OMP_CLAUSE_CHAIN (u
) = list
;
25715 list
= tree_cons (decl
, NULL_TREE
, list
);
25718 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25720 cp_lexer_consume_token (parser
->lexer
);
25723 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25727 /* Try to resync to an unnested comma. Copied from
25728 cp_parser_parenthesized_expression_list. */
25730 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
25731 /*recovering=*/true,
25733 /*consume_paren=*/true);
25741 /* Similarly, but expect leading and trailing parenthesis. This is a very
25742 common case for omp clauses. */
25745 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
25747 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25748 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
25753 collapse ( constant-expression ) */
25756 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
25762 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25763 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25766 num
= cp_parser_constant_expression (parser
, false, NULL
);
25768 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25769 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25770 /*or_comma=*/false,
25771 /*consume_paren=*/true);
25773 if (num
== error_mark_node
)
25775 num
= fold_non_dependent_expr (num
);
25776 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
25777 || !host_integerp (num
, 0)
25778 || (n
= tree_low_cst (num
, 0)) <= 0
25781 error_at (loc
, "collapse argument needs positive constant integer expression");
25785 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
25786 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
25787 OMP_CLAUSE_CHAIN (c
) = list
;
25788 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
25794 default ( shared | none ) */
25797 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
25799 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
25802 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25804 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
25806 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
25807 const char *p
= IDENTIFIER_POINTER (id
);
25812 if (strcmp ("none", p
) != 0)
25814 kind
= OMP_CLAUSE_DEFAULT_NONE
;
25818 if (strcmp ("shared", p
) != 0)
25820 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
25827 cp_lexer_consume_token (parser
->lexer
);
25832 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
25835 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25836 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25837 /*or_comma=*/false,
25838 /*consume_paren=*/true);
25840 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
25843 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
25844 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
25845 OMP_CLAUSE_CHAIN (c
) = list
;
25846 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
25852 final ( expression ) */
25855 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
25859 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25862 t
= cp_parser_condition (parser
);
25864 if (t
== error_mark_node
25865 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25866 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25867 /*or_comma=*/false,
25868 /*consume_paren=*/true);
25870 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
25872 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
25873 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
25874 OMP_CLAUSE_CHAIN (c
) = list
;
25880 if ( expression ) */
25883 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
25887 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25890 t
= cp_parser_condition (parser
);
25892 if (t
== error_mark_node
25893 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25894 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25895 /*or_comma=*/false,
25896 /*consume_paren=*/true);
25898 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
25900 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
25901 OMP_CLAUSE_IF_EXPR (c
) = t
;
25902 OMP_CLAUSE_CHAIN (c
) = list
;
25911 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
25912 tree list
, location_t location
)
25916 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
25919 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
25920 OMP_CLAUSE_CHAIN (c
) = list
;
25928 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
25929 tree list
, location_t location
)
25933 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
25935 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
25936 OMP_CLAUSE_CHAIN (c
) = list
;
25941 num_threads ( expression ) */
25944 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
25945 location_t location
)
25949 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25952 t
= cp_parser_expression (parser
, false, NULL
);
25954 if (t
== error_mark_node
25955 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25956 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25957 /*or_comma=*/false,
25958 /*consume_paren=*/true);
25960 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
25961 "num_threads", location
);
25963 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
25964 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
25965 OMP_CLAUSE_CHAIN (c
) = list
;
25974 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
25975 tree list
, location_t location
)
25979 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
25980 "ordered", location
);
25982 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
25983 OMP_CLAUSE_CHAIN (c
) = list
;
25988 reduction ( reduction-operator : variable-list )
25990 reduction-operator:
25991 One of: + * - & ^ | && ||
25995 reduction-operator:
25996 One of: + * - & ^ | && || min max */
25999 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
26001 enum tree_code code
;
26004 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26007 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
26019 code
= BIT_AND_EXPR
;
26022 code
= BIT_XOR_EXPR
;
26025 code
= BIT_IOR_EXPR
;
26028 code
= TRUTH_ANDIF_EXPR
;
26031 code
= TRUTH_ORIF_EXPR
;
26035 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26036 const char *p
= IDENTIFIER_POINTER (id
);
26038 if (strcmp (p
, "min") == 0)
26043 if (strcmp (p
, "max") == 0)
26051 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26052 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26054 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26055 /*or_comma=*/false,
26056 /*consume_paren=*/true);
26059 cp_lexer_consume_token (parser
->lexer
);
26061 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26064 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
26065 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
26066 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
26072 schedule ( schedule-kind )
26073 schedule ( schedule-kind , expression )
26076 static | dynamic | guided | runtime | auto */
26079 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
26083 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26086 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
26088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26090 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26091 const char *p
= IDENTIFIER_POINTER (id
);
26096 if (strcmp ("dynamic", p
) != 0)
26098 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
26102 if (strcmp ("guided", p
) != 0)
26104 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
26108 if (strcmp ("runtime", p
) != 0)
26110 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
26117 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
26118 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
26119 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
26120 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
26123 cp_lexer_consume_token (parser
->lexer
);
26125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26128 cp_lexer_consume_token (parser
->lexer
);
26130 token
= cp_lexer_peek_token (parser
->lexer
);
26131 t
= cp_parser_assignment_expression (parser
, false, NULL
);
26133 if (t
== error_mark_node
)
26135 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
26136 error_at (token
->location
, "schedule %<runtime%> does not take "
26137 "a %<chunk_size%> parameter");
26138 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
26139 error_at (token
->location
, "schedule %<auto%> does not take "
26140 "a %<chunk_size%> parameter");
26142 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
26144 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26147 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
26150 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
26151 OMP_CLAUSE_CHAIN (c
) = list
;
26155 cp_parser_error (parser
, "invalid schedule kind");
26157 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26158 /*or_comma=*/false,
26159 /*consume_paren=*/true);
26167 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
26168 tree list
, location_t location
)
26172 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
26174 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
26175 OMP_CLAUSE_CHAIN (c
) = list
;
26179 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26180 is a bitmask in MASK. Return the list of clauses found; the result
26181 of clause default goes in *pdefault. */
26184 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
26185 const char *where
, cp_token
*pragma_tok
)
26187 tree clauses
= NULL
;
26189 cp_token
*token
= NULL
;
26191 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
26193 pragma_omp_clause c_kind
;
26194 const char *c_name
;
26195 tree prev
= clauses
;
26197 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26198 cp_lexer_consume_token (parser
->lexer
);
26200 token
= cp_lexer_peek_token (parser
->lexer
);
26201 c_kind
= cp_parser_omp_clause_name (parser
);
26206 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
26207 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
26209 c_name
= "collapse";
26211 case PRAGMA_OMP_CLAUSE_COPYIN
:
26212 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
26215 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
26216 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
26218 c_name
= "copyprivate";
26220 case PRAGMA_OMP_CLAUSE_DEFAULT
:
26221 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
26223 c_name
= "default";
26225 case PRAGMA_OMP_CLAUSE_FINAL
:
26226 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
26229 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
26230 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
26232 c_name
= "firstprivate";
26234 case PRAGMA_OMP_CLAUSE_IF
:
26235 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
26238 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
26239 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
26241 c_name
= "lastprivate";
26243 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
26244 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
26246 c_name
= "mergeable";
26248 case PRAGMA_OMP_CLAUSE_NOWAIT
:
26249 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
26252 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
26253 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
26255 c_name
= "num_threads";
26257 case PRAGMA_OMP_CLAUSE_ORDERED
:
26258 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
26260 c_name
= "ordered";
26262 case PRAGMA_OMP_CLAUSE_PRIVATE
:
26263 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
26265 c_name
= "private";
26267 case PRAGMA_OMP_CLAUSE_REDUCTION
:
26268 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
26269 c_name
= "reduction";
26271 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
26272 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
26274 c_name
= "schedule";
26276 case PRAGMA_OMP_CLAUSE_SHARED
:
26277 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
26281 case PRAGMA_OMP_CLAUSE_UNTIED
:
26282 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
26287 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
26291 if (((mask
>> c_kind
) & 1) == 0)
26293 /* Remove the invalid clause(s) from the list to avoid
26294 confusing the rest of the compiler. */
26296 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
26300 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
26301 return finish_omp_clauses (clauses
);
26308 In practice, we're also interested in adding the statement to an
26309 outer node. So it is convenient if we work around the fact that
26310 cp_parser_statement calls add_stmt. */
26313 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
26315 unsigned save
= parser
->in_statement
;
26317 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26318 This preserves the "not within loop or switch" style error messages
26319 for nonsense cases like
26325 if (parser
->in_statement
)
26326 parser
->in_statement
= IN_OMP_BLOCK
;
26332 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
26334 parser
->in_statement
= save
;
26338 cp_parser_omp_structured_block (cp_parser
*parser
)
26340 tree stmt
= begin_omp_structured_block ();
26341 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
26343 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
26345 cp_parser_end_omp_structured_block (parser
, save
);
26346 return finish_omp_structured_block (stmt
);
26350 # pragma omp atomic new-line
26354 x binop= expr | x++ | ++x | x-- | --x
26356 +, *, -, /, &, ^, |, <<, >>
26358 where x is an lvalue expression with scalar type.
26361 # pragma omp atomic new-line
26364 # pragma omp atomic read new-line
26367 # pragma omp atomic write new-line
26370 # pragma omp atomic update new-line
26373 # pragma omp atomic capture new-line
26376 # pragma omp atomic capture new-line
26384 expression-stmt | x = x binop expr
26386 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26388 { v = x; update-stmt; } | { update-stmt; v = x; }
26390 where x and v are lvalue expressions with scalar type. */
26393 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
26395 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
26396 tree rhs1
= NULL_TREE
, orig_lhs
;
26397 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
26398 bool structured_block
= false;
26400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26402 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26403 const char *p
= IDENTIFIER_POINTER (id
);
26405 if (!strcmp (p
, "read"))
26406 code
= OMP_ATOMIC_READ
;
26407 else if (!strcmp (p
, "write"))
26409 else if (!strcmp (p
, "update"))
26411 else if (!strcmp (p
, "capture"))
26412 code
= OMP_ATOMIC_CAPTURE_NEW
;
26416 cp_lexer_consume_token (parser
->lexer
);
26418 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26422 case OMP_ATOMIC_READ
:
26423 case NOP_EXPR
: /* atomic write */
26424 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26425 /*cast_p=*/false, NULL
);
26426 if (v
== error_mark_node
)
26428 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
26430 if (code
== NOP_EXPR
)
26431 lhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
26433 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26434 /*cast_p=*/false, NULL
);
26435 if (lhs
== error_mark_node
)
26437 if (code
== NOP_EXPR
)
26439 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26447 case OMP_ATOMIC_CAPTURE_NEW
:
26448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26450 cp_lexer_consume_token (parser
->lexer
);
26451 structured_block
= true;
26455 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26456 /*cast_p=*/false, NULL
);
26457 if (v
== error_mark_node
)
26459 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
26467 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26468 /*cast_p=*/false, NULL
);
26470 switch (TREE_CODE (lhs
))
26475 case POSTINCREMENT_EXPR
:
26476 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
26477 code
= OMP_ATOMIC_CAPTURE_OLD
;
26479 case PREINCREMENT_EXPR
:
26480 lhs
= TREE_OPERAND (lhs
, 0);
26481 opcode
= PLUS_EXPR
;
26482 rhs
= integer_one_node
;
26485 case POSTDECREMENT_EXPR
:
26486 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
26487 code
= OMP_ATOMIC_CAPTURE_OLD
;
26489 case PREDECREMENT_EXPR
:
26490 lhs
= TREE_OPERAND (lhs
, 0);
26491 opcode
= MINUS_EXPR
;
26492 rhs
= integer_one_node
;
26495 case COMPOUND_EXPR
:
26496 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
26497 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
26498 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
26499 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
26500 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26501 (TREE_OPERAND (lhs
, 1), 0), 0)))
26503 /* Undo effects of boolean_increment for post {in,de}crement. */
26504 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
26507 if (TREE_CODE (lhs
) == MODIFY_EXPR
26508 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
26510 /* Undo effects of boolean_increment. */
26511 if (integer_onep (TREE_OPERAND (lhs
, 1)))
26513 /* This is pre or post increment. */
26514 rhs
= TREE_OPERAND (lhs
, 1);
26515 lhs
= TREE_OPERAND (lhs
, 0);
26517 if (code
== OMP_ATOMIC_CAPTURE_NEW
26518 && !structured_block
26519 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
26520 code
= OMP_ATOMIC_CAPTURE_OLD
;
26526 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
26529 opcode
= MULT_EXPR
;
26532 opcode
= TRUNC_DIV_EXPR
;
26535 opcode
= PLUS_EXPR
;
26538 opcode
= MINUS_EXPR
;
26540 case CPP_LSHIFT_EQ
:
26541 opcode
= LSHIFT_EXPR
;
26543 case CPP_RSHIFT_EQ
:
26544 opcode
= RSHIFT_EXPR
;
26547 opcode
= BIT_AND_EXPR
;
26550 opcode
= BIT_IOR_EXPR
;
26553 opcode
= BIT_XOR_EXPR
;
26556 if (structured_block
|| code
== OMP_ATOMIC
)
26558 enum cp_parser_prec oprec
;
26560 cp_lexer_consume_token (parser
->lexer
);
26561 rhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26562 /*cast_p=*/false, NULL
);
26563 if (rhs1
== error_mark_node
)
26565 token
= cp_lexer_peek_token (parser
->lexer
);
26566 switch (token
->type
)
26568 case CPP_SEMICOLON
:
26569 if (code
== OMP_ATOMIC_CAPTURE_NEW
)
26571 code
= OMP_ATOMIC_CAPTURE_OLD
;
26576 cp_lexer_consume_token (parser
->lexer
);
26579 cp_parser_error (parser
,
26580 "invalid form of %<#pragma omp atomic%>");
26583 opcode
= MULT_EXPR
;
26586 opcode
= TRUNC_DIV_EXPR
;
26589 opcode
= PLUS_EXPR
;
26592 opcode
= MINUS_EXPR
;
26595 opcode
= LSHIFT_EXPR
;
26598 opcode
= RSHIFT_EXPR
;
26601 opcode
= BIT_AND_EXPR
;
26604 opcode
= BIT_IOR_EXPR
;
26607 opcode
= BIT_XOR_EXPR
;
26610 cp_parser_error (parser
,
26611 "invalid operator for %<#pragma omp atomic%>");
26614 oprec
= TOKEN_PRECEDENCE (token
);
26615 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
26616 if (commutative_tree_code (opcode
))
26617 oprec
= (enum cp_parser_prec
) (oprec
- 1);
26618 cp_lexer_consume_token (parser
->lexer
);
26619 rhs
= cp_parser_binary_expression (parser
, false, false,
26621 if (rhs
== error_mark_node
)
26627 cp_parser_error (parser
,
26628 "invalid operator for %<#pragma omp atomic%>");
26631 cp_lexer_consume_token (parser
->lexer
);
26633 rhs
= cp_parser_expression (parser
, false, NULL
);
26634 if (rhs
== error_mark_node
)
26639 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
26641 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26643 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26644 /*cast_p=*/false, NULL
);
26645 if (v
== error_mark_node
)
26647 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
26649 lhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26650 /*cast_p=*/false, NULL
);
26651 if (lhs1
== error_mark_node
)
26654 if (structured_block
)
26656 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26657 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
26660 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
26661 if (!structured_block
)
26662 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26666 cp_parser_skip_to_end_of_block_or_statement (parser
);
26667 if (structured_block
)
26669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26670 cp_lexer_consume_token (parser
->lexer
);
26671 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
26673 cp_parser_skip_to_end_of_block_or_statement (parser
);
26674 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26675 cp_lexer_consume_token (parser
->lexer
);
26682 # pragma omp barrier new-line */
26685 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
26687 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26688 finish_omp_barrier ();
26692 # pragma omp critical [(name)] new-line
26693 structured-block */
26696 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
26698 tree stmt
, name
= NULL
;
26700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26702 cp_lexer_consume_token (parser
->lexer
);
26704 name
= cp_parser_identifier (parser
);
26706 if (name
== error_mark_node
26707 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26708 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26709 /*or_comma=*/false,
26710 /*consume_paren=*/true);
26711 if (name
== error_mark_node
)
26714 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26716 stmt
= cp_parser_omp_structured_block (parser
);
26717 return c_finish_omp_critical (input_location
, stmt
, name
);
26721 # pragma omp flush flush-vars[opt] new-line
26724 ( variable-list ) */
26727 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
26729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26730 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
26731 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26733 finish_omp_flush ();
26736 /* Helper function, to parse omp for increment expression. */
26739 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
26741 tree cond
= cp_parser_binary_expression (parser
, false, true,
26742 PREC_NOT_OPERATOR
, NULL
);
26743 if (cond
== error_mark_node
26744 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26746 cp_parser_skip_to_end_of_statement (parser
);
26747 return error_mark_node
;
26750 switch (TREE_CODE (cond
))
26758 return error_mark_node
;
26761 /* If decl is an iterator, preserve LHS and RHS of the relational
26762 expr until finish_omp_for. */
26764 && (type_dependent_expression_p (decl
)
26765 || CLASS_TYPE_P (TREE_TYPE (decl
))))
26768 return build_x_binary_op (input_location
, TREE_CODE (cond
),
26769 TREE_OPERAND (cond
, 0), ERROR_MARK
,
26770 TREE_OPERAND (cond
, 1), ERROR_MARK
,
26771 /*overload=*/NULL
, tf_warning_or_error
);
26774 /* Helper function, to parse omp for increment expression. */
26777 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
26779 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26785 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
26787 op
= (token
->type
== CPP_PLUS_PLUS
26788 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
26789 cp_lexer_consume_token (parser
->lexer
);
26790 lhs
= cp_parser_cast_expression (parser
, false, false, NULL
);
26792 return error_mark_node
;
26793 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
26796 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
26798 return error_mark_node
;
26800 token
= cp_lexer_peek_token (parser
->lexer
);
26801 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
26803 op
= (token
->type
== CPP_PLUS_PLUS
26804 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
26805 cp_lexer_consume_token (parser
->lexer
);
26806 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
26809 op
= cp_parser_assignment_operator_opt (parser
);
26810 if (op
== ERROR_MARK
)
26811 return error_mark_node
;
26813 if (op
!= NOP_EXPR
)
26815 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
26816 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
26817 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
26820 lhs
= cp_parser_binary_expression (parser
, false, false,
26821 PREC_ADDITIVE_EXPRESSION
, NULL
);
26822 token
= cp_lexer_peek_token (parser
->lexer
);
26823 decl_first
= lhs
== decl
;
26826 if (token
->type
!= CPP_PLUS
26827 && token
->type
!= CPP_MINUS
)
26828 return error_mark_node
;
26832 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
26833 cp_lexer_consume_token (parser
->lexer
);
26834 rhs
= cp_parser_binary_expression (parser
, false, false,
26835 PREC_ADDITIVE_EXPRESSION
, NULL
);
26836 token
= cp_lexer_peek_token (parser
->lexer
);
26837 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
26839 if (lhs
== NULL_TREE
)
26841 if (op
== PLUS_EXPR
)
26844 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
26845 tf_warning_or_error
);
26848 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
26849 ERROR_MARK
, NULL
, tf_warning_or_error
);
26852 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
26856 if (rhs
!= decl
|| op
== MINUS_EXPR
)
26857 return error_mark_node
;
26858 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
26861 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
26863 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
26866 /* Parse the restricted form of the for statement allowed by OpenMP. */
26869 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
26871 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
26872 tree real_decl
, initv
, condv
, incrv
, declv
;
26873 tree this_pre_body
, cl
;
26874 location_t loc_first
;
26875 bool collapse_err
= false;
26876 int i
, collapse
= 1, nbraces
= 0;
26877 VEC(tree
,gc
) *for_block
= make_tree_vector ();
26879 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
26880 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
26881 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
26883 gcc_assert (collapse
>= 1);
26885 declv
= make_tree_vec (collapse
);
26886 initv
= make_tree_vec (collapse
);
26887 condv
= make_tree_vec (collapse
);
26888 incrv
= make_tree_vec (collapse
);
26890 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
26892 for (i
= 0; i
< collapse
; i
++)
26894 int bracecount
= 0;
26895 bool add_private_clause
= false;
26898 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
26900 cp_parser_error (parser
, "for statement expected");
26903 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26905 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26908 init
= decl
= real_decl
= NULL
;
26909 this_pre_body
= push_stmt_list ();
26910 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26912 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26916 integer-type var = lb
26917 random-access-iterator-type var = lb
26918 pointer-type var = lb
26920 cp_decl_specifier_seq type_specifiers
;
26922 /* First, try to parse as an initialized declaration. See
26923 cp_parser_condition, from whence the bulk of this is copied. */
26925 cp_parser_parse_tentatively (parser
);
26926 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
26927 /*is_trailing_return=*/false,
26929 if (cp_parser_parse_definitely (parser
))
26931 /* If parsing a type specifier seq succeeded, then this
26932 MUST be a initialized declaration. */
26933 tree asm_specification
, attributes
;
26934 cp_declarator
*declarator
;
26936 declarator
= cp_parser_declarator (parser
,
26937 CP_PARSER_DECLARATOR_NAMED
,
26938 /*ctor_dtor_or_conv_p=*/NULL
,
26939 /*parenthesized_p=*/NULL
,
26940 /*member_p=*/false);
26941 attributes
= cp_parser_attributes_opt (parser
);
26942 asm_specification
= cp_parser_asm_specification_opt (parser
);
26944 if (declarator
== cp_error_declarator
)
26945 cp_parser_skip_to_end_of_statement (parser
);
26949 tree pushed_scope
, auto_node
;
26951 decl
= start_decl (declarator
, &type_specifiers
,
26952 SD_INITIALIZED
, attributes
,
26953 /*prefix_attributes=*/NULL_TREE
,
26956 auto_node
= type_uses_auto (TREE_TYPE (decl
));
26957 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
26959 if (cp_lexer_next_token_is (parser
->lexer
,
26961 error ("parenthesized initialization is not allowed in "
26962 "OpenMP %<for%> loop");
26964 /* Trigger an error. */
26965 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
26967 init
= error_mark_node
;
26968 cp_parser_skip_to_end_of_statement (parser
);
26970 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
26971 || type_dependent_expression_p (decl
)
26974 bool is_direct_init
, is_non_constant_init
;
26976 init
= cp_parser_initializer (parser
,
26978 &is_non_constant_init
);
26983 = do_auto_deduction (TREE_TYPE (decl
), init
,
26986 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
26987 && !type_dependent_expression_p (decl
))
26991 cp_finish_decl (decl
, init
, !is_non_constant_init
,
26993 LOOKUP_ONLYCONVERTING
);
26994 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
26996 VEC_safe_push (tree
, gc
, for_block
, this_pre_body
);
27000 init
= pop_stmt_list (this_pre_body
);
27001 this_pre_body
= NULL_TREE
;
27006 cp_lexer_consume_token (parser
->lexer
);
27007 init
= cp_parser_assignment_expression (parser
, false, NULL
);
27010 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
27011 init
= error_mark_node
;
27013 cp_finish_decl (decl
, NULL_TREE
,
27014 /*init_const_expr_p=*/false,
27016 LOOKUP_ONLYCONVERTING
);
27020 pop_scope (pushed_scope
);
27026 /* If parsing a type specifier sequence failed, then
27027 this MUST be a simple expression. */
27028 cp_parser_parse_tentatively (parser
);
27029 decl
= cp_parser_primary_expression (parser
, false, false,
27031 if (!cp_parser_error_occurred (parser
)
27034 && CLASS_TYPE_P (TREE_TYPE (decl
)))
27038 cp_parser_parse_definitely (parser
);
27039 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
27040 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
27041 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
27044 tf_warning_or_error
));
27045 add_private_clause
= true;
27050 cp_parser_abort_tentative_parse (parser
);
27051 init
= cp_parser_expression (parser
, false, NULL
);
27054 if (TREE_CODE (init
) == MODIFY_EXPR
27055 || TREE_CODE (init
) == MODOP_EXPR
)
27056 real_decl
= TREE_OPERAND (init
, 0);
27061 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27064 this_pre_body
= pop_stmt_list (this_pre_body
);
27068 pre_body
= push_stmt_list ();
27070 add_stmt (this_pre_body
);
27071 pre_body
= pop_stmt_list (pre_body
);
27074 pre_body
= this_pre_body
;
27079 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
27082 for (c
= par_clauses
; *c
; )
27083 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
27084 && OMP_CLAUSE_DECL (*c
) == real_decl
)
27086 error_at (loc
, "iteration variable %qD"
27087 " should not be firstprivate", real_decl
);
27088 *c
= OMP_CLAUSE_CHAIN (*c
);
27090 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
27091 && OMP_CLAUSE_DECL (*c
) == real_decl
)
27093 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27094 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27095 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
27096 OMP_CLAUSE_DECL (l
) = real_decl
;
27097 OMP_CLAUSE_CHAIN (l
) = clauses
;
27098 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
27100 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
27101 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
27102 add_private_clause
= false;
27106 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
27107 && OMP_CLAUSE_DECL (*c
) == real_decl
)
27108 add_private_clause
= false;
27109 c
= &OMP_CLAUSE_CHAIN (*c
);
27113 if (add_private_clause
)
27116 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27118 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
27119 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
27120 && OMP_CLAUSE_DECL (c
) == decl
)
27122 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
27123 && OMP_CLAUSE_DECL (c
) == decl
)
27124 error_at (loc
, "iteration variable %qD "
27125 "should not be firstprivate",
27127 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
27128 && OMP_CLAUSE_DECL (c
) == decl
)
27129 error_at (loc
, "iteration variable %qD should not be reduction",
27134 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
27135 OMP_CLAUSE_DECL (c
) = decl
;
27136 c
= finish_omp_clauses (c
);
27139 OMP_CLAUSE_CHAIN (c
) = clauses
;
27146 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27147 cond
= cp_parser_omp_for_cond (parser
, decl
);
27148 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27151 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
27153 /* If decl is an iterator, preserve the operator on decl
27154 until finish_omp_for. */
27156 && ((processing_template_decl
27157 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
27158 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
27159 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
27161 incr
= cp_parser_expression (parser
, false, NULL
);
27162 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
27163 SET_EXPR_LOCATION (incr
, input_location
);
27166 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27167 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27168 /*or_comma=*/false,
27169 /*consume_paren=*/true);
27171 TREE_VEC_ELT (declv
, i
) = decl
;
27172 TREE_VEC_ELT (initv
, i
) = init
;
27173 TREE_VEC_ELT (condv
, i
) = cond
;
27174 TREE_VEC_ELT (incrv
, i
) = incr
;
27176 if (i
== collapse
- 1)
27179 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27180 in between the collapsed for loops to be still considered perfectly
27181 nested. Hopefully the final version clarifies this.
27182 For now handle (multiple) {'s and empty statements. */
27183 cp_parser_parse_tentatively (parser
);
27186 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27188 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27190 cp_lexer_consume_token (parser
->lexer
);
27193 else if (bracecount
27194 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27195 cp_lexer_consume_token (parser
->lexer
);
27198 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27199 error_at (loc
, "not enough collapsed for loops");
27200 collapse_err
= true;
27201 cp_parser_abort_tentative_parse (parser
);
27210 cp_parser_parse_definitely (parser
);
27211 nbraces
+= bracecount
;
27215 /* Note that we saved the original contents of this flag when we entered
27216 the structured block, and so we don't need to re-save it here. */
27217 parser
->in_statement
= IN_OMP_FOR
;
27219 /* Note that the grammar doesn't call for a structured block here,
27220 though the loop as a whole is a structured block. */
27221 body
= push_stmt_list ();
27222 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
27223 body
= pop_stmt_list (body
);
27225 if (declv
== NULL_TREE
)
27228 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
27229 pre_body
, clauses
);
27233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
27235 cp_lexer_consume_token (parser
->lexer
);
27238 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27239 cp_lexer_consume_token (parser
->lexer
);
27244 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27245 "collapsed loops not perfectly nested");
27247 collapse_err
= true;
27248 cp_parser_statement_seq_opt (parser
, NULL
);
27249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27254 while (!VEC_empty (tree
, for_block
))
27255 add_stmt (pop_stmt_list (VEC_pop (tree
, for_block
)));
27256 release_tree_vector (for_block
);
27262 #pragma omp for for-clause[optseq] new-line
27265 #define OMP_FOR_CLAUSE_MASK \
27266 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27267 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27268 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27269 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27270 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27271 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27272 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27273 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27276 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
27278 tree clauses
, sb
, ret
;
27281 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
27282 "#pragma omp for", pragma_tok
);
27284 sb
= begin_omp_structured_block ();
27285 save
= cp_parser_begin_omp_structured_block (parser
);
27287 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
27289 cp_parser_end_omp_structured_block (parser
, save
);
27290 add_stmt (finish_omp_structured_block (sb
));
27296 # pragma omp master new-line
27297 structured-block */
27300 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
27302 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27303 return c_finish_omp_master (input_location
,
27304 cp_parser_omp_structured_block (parser
));
27308 # pragma omp ordered new-line
27309 structured-block */
27312 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
27314 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27315 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27316 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
27322 { section-sequence }
27325 section-directive[opt] structured-block
27326 section-sequence section-directive structured-block */
27329 cp_parser_omp_sections_scope (cp_parser
*parser
)
27331 tree stmt
, substmt
;
27332 bool error_suppress
= false;
27335 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
27338 stmt
= push_stmt_list ();
27340 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
27344 substmt
= begin_omp_structured_block ();
27345 save
= cp_parser_begin_omp_structured_block (parser
);
27349 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
27351 tok
= cp_lexer_peek_token (parser
->lexer
);
27352 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
27354 if (tok
->type
== CPP_CLOSE_BRACE
)
27356 if (tok
->type
== CPP_EOF
)
27360 cp_parser_end_omp_structured_block (parser
, save
);
27361 substmt
= finish_omp_structured_block (substmt
);
27362 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
27363 add_stmt (substmt
);
27368 tok
= cp_lexer_peek_token (parser
->lexer
);
27369 if (tok
->type
== CPP_CLOSE_BRACE
)
27371 if (tok
->type
== CPP_EOF
)
27374 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
27376 cp_lexer_consume_token (parser
->lexer
);
27377 cp_parser_require_pragma_eol (parser
, tok
);
27378 error_suppress
= false;
27380 else if (!error_suppress
)
27382 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
27383 error_suppress
= true;
27386 substmt
= cp_parser_omp_structured_block (parser
);
27387 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
27388 add_stmt (substmt
);
27390 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
27392 substmt
= pop_stmt_list (stmt
);
27394 stmt
= make_node (OMP_SECTIONS
);
27395 TREE_TYPE (stmt
) = void_type_node
;
27396 OMP_SECTIONS_BODY (stmt
) = substmt
;
27403 # pragma omp sections sections-clause[optseq] newline
27406 #define OMP_SECTIONS_CLAUSE_MASK \
27407 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27408 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27409 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27410 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27411 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27414 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
27418 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
27419 "#pragma omp sections", pragma_tok
);
27421 ret
= cp_parser_omp_sections_scope (parser
);
27423 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
27429 # pragma parallel parallel-clause new-line
27430 # pragma parallel for parallel-for-clause new-line
27431 # pragma parallel sections parallel-sections-clause new-line */
27433 #define OMP_PARALLEL_CLAUSE_MASK \
27434 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27435 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27436 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27437 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27438 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27439 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27440 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27441 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27444 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
27446 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
27447 const char *p_name
= "#pragma omp parallel";
27448 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
27449 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
27451 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27453 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27455 cp_lexer_consume_token (parser
->lexer
);
27456 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
27457 p_name
= "#pragma omp parallel for";
27458 mask
|= OMP_FOR_CLAUSE_MASK
;
27459 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
27461 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27463 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27464 const char *p
= IDENTIFIER_POINTER (id
);
27465 if (strcmp (p
, "sections") == 0)
27467 cp_lexer_consume_token (parser
->lexer
);
27468 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
27469 p_name
= "#pragma omp parallel sections";
27470 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
27471 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
27475 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
27476 block
= begin_omp_parallel ();
27477 save
= cp_parser_begin_omp_structured_block (parser
);
27481 case PRAGMA_OMP_PARALLEL
:
27482 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
27483 par_clause
= clauses
;
27486 case PRAGMA_OMP_PARALLEL_FOR
:
27487 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
27488 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
27491 case PRAGMA_OMP_PARALLEL_SECTIONS
:
27492 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
27493 stmt
= cp_parser_omp_sections_scope (parser
);
27495 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
27499 gcc_unreachable ();
27502 cp_parser_end_omp_structured_block (parser
, save
);
27503 stmt
= finish_omp_parallel (par_clause
, block
);
27504 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
27505 OMP_PARALLEL_COMBINED (stmt
) = 1;
27510 # pragma omp single single-clause[optseq] new-line
27511 structured-block */
27513 #define OMP_SINGLE_CLAUSE_MASK \
27514 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27515 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27516 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27517 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27520 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
27522 tree stmt
= make_node (OMP_SINGLE
);
27523 TREE_TYPE (stmt
) = void_type_node
;
27525 OMP_SINGLE_CLAUSES (stmt
)
27526 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
27527 "#pragma omp single", pragma_tok
);
27528 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
27530 return add_stmt (stmt
);
27534 # pragma omp task task-clause[optseq] new-line
27535 structured-block */
27537 #define OMP_TASK_CLAUSE_MASK \
27538 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27539 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27540 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27541 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27542 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27543 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27544 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27545 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27548 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
27550 tree clauses
, block
;
27553 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
27554 "#pragma omp task", pragma_tok
);
27555 block
= begin_omp_task ();
27556 save
= cp_parser_begin_omp_structured_block (parser
);
27557 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
27558 cp_parser_end_omp_structured_block (parser
, save
);
27559 return finish_omp_task (clauses
, block
);
27563 # pragma omp taskwait new-line */
27566 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
27568 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27569 finish_omp_taskwait ();
27573 # pragma omp taskyield new-line */
27576 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
27578 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27579 finish_omp_taskyield ();
27583 # pragma omp threadprivate (variable-list) */
27586 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
27590 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
27591 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27593 finish_omp_threadprivate (vars
);
27596 /* Main entry point to OpenMP statement pragmas. */
27599 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
27603 switch (pragma_tok
->pragma_kind
)
27605 case PRAGMA_OMP_ATOMIC
:
27606 cp_parser_omp_atomic (parser
, pragma_tok
);
27608 case PRAGMA_OMP_CRITICAL
:
27609 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
27611 case PRAGMA_OMP_FOR
:
27612 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
27614 case PRAGMA_OMP_MASTER
:
27615 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
27617 case PRAGMA_OMP_ORDERED
:
27618 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
27620 case PRAGMA_OMP_PARALLEL
:
27621 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
27623 case PRAGMA_OMP_SECTIONS
:
27624 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
27626 case PRAGMA_OMP_SINGLE
:
27627 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
27629 case PRAGMA_OMP_TASK
:
27630 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
27633 gcc_unreachable ();
27637 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
27640 /* Transactional Memory parsing routines. */
27642 /* Parse a transaction attribute.
27648 ??? Simplify this when C++0x bracket attributes are
27649 implemented properly. */
27652 cp_parser_txn_attribute_opt (cp_parser
*parser
)
27655 tree attr_name
, attr
= NULL
;
27657 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
27658 return cp_parser_attributes_opt (parser
);
27660 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
27662 cp_lexer_consume_token (parser
->lexer
);
27663 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
27666 token
= cp_lexer_peek_token (parser
->lexer
);
27667 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
27669 token
= cp_lexer_consume_token (parser
->lexer
);
27671 attr_name
= (token
->type
== CPP_KEYWORD
27672 /* For keywords, use the canonical spelling,
27673 not the parsed identifier. */
27674 ? ridpointers
[(int) token
->keyword
]
27676 attr
= build_tree_list (attr_name
, NULL_TREE
);
27679 cp_parser_error (parser
, "expected identifier");
27681 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
27683 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
27687 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27689 transaction-statement:
27690 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27692 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27696 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
27698 unsigned char old_in
= parser
->in_transaction
;
27699 unsigned char this_in
= 1, new_in
;
27701 tree stmt
, attrs
, noex
;
27703 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
27704 || keyword
== RID_TRANSACTION_RELAXED
);
27705 token
= cp_parser_require_keyword (parser
, keyword
,
27706 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
27707 : RT_TRANSACTION_RELAXED
));
27708 gcc_assert (token
!= NULL
);
27710 if (keyword
== RID_TRANSACTION_RELAXED
)
27711 this_in
|= TM_STMT_ATTR_RELAXED
;
27714 attrs
= cp_parser_txn_attribute_opt (parser
);
27716 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
27719 /* Parse a noexcept specification. */
27720 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
27722 /* Keep track if we're in the lexical scope of an outer transaction. */
27723 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
27725 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
27727 parser
->in_transaction
= new_in
;
27728 cp_parser_compound_statement (parser
, NULL
, false, false);
27729 parser
->in_transaction
= old_in
;
27731 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
27736 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27738 transaction-expression:
27739 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27740 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27744 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
27746 unsigned char old_in
= parser
->in_transaction
;
27747 unsigned char this_in
= 1;
27752 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
27753 || keyword
== RID_TRANSACTION_RELAXED
);
27756 error (keyword
== RID_TRANSACTION_RELAXED
27757 ? G_("%<__transaction_relaxed%> without transactional memory "
27759 : G_("%<__transaction_atomic%> without transactional memory "
27760 "support enabled"));
27762 token
= cp_parser_require_keyword (parser
, keyword
,
27763 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
27764 : RT_TRANSACTION_RELAXED
));
27765 gcc_assert (token
!= NULL
);
27767 if (keyword
== RID_TRANSACTION_RELAXED
)
27768 this_in
|= TM_STMT_ATTR_RELAXED
;
27770 /* Set this early. This might mean that we allow transaction_cancel in
27771 an expression that we find out later actually has to be a constexpr.
27772 However, we expect that cxx_constant_value will be able to deal with
27773 this; also, if the noexcept has no constexpr, then what we parse next
27774 really is a transaction's body. */
27775 parser
->in_transaction
= this_in
;
27777 /* Parse a noexcept specification. */
27778 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
27781 if (!noex
|| !noex_expr
27782 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
27784 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27786 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
27787 finish_parenthesized_expr (expr
);
27789 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27793 /* The only expression that is available got parsed for the noexcept
27794 already. noexcept is true then. */
27796 noex
= boolean_true_node
;
27799 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
27800 parser
->in_transaction
= old_in
;
27802 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
27803 return error_mark_node
;
27805 return (flag_tm
? expr
: error_mark_node
);
27808 /* Parse a function-transaction-block.
27810 function-transaction-block:
27811 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27813 __transaction_atomic txn-attribute[opt] function-try-block
27814 __transaction_relaxed ctor-initializer[opt] function-body
27815 __transaction_relaxed function-try-block
27819 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
27821 unsigned char old_in
= parser
->in_transaction
;
27822 unsigned char new_in
= 1;
27823 tree compound_stmt
, stmt
, attrs
;
27824 bool ctor_initializer_p
;
27827 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
27828 || keyword
== RID_TRANSACTION_RELAXED
);
27829 token
= cp_parser_require_keyword (parser
, keyword
,
27830 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
27831 : RT_TRANSACTION_RELAXED
));
27832 gcc_assert (token
!= NULL
);
27834 if (keyword
== RID_TRANSACTION_RELAXED
)
27835 new_in
|= TM_STMT_ATTR_RELAXED
;
27838 attrs
= cp_parser_txn_attribute_opt (parser
);
27840 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
27843 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
27845 parser
->in_transaction
= new_in
;
27847 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27848 ctor_initializer_p
= cp_parser_function_try_block (parser
);
27850 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
27851 (parser
, /*in_function_try_block=*/false);
27853 parser
->in_transaction
= old_in
;
27855 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
27857 return ctor_initializer_p
;
27860 /* Parse a __transaction_cancel statement.
27863 __transaction_cancel txn-attribute[opt] ;
27864 __transaction_cancel txn-attribute[opt] throw-expression ;
27866 ??? Cancel and throw is not yet implemented. */
27869 cp_parser_transaction_cancel (cp_parser
*parser
)
27872 bool is_outer
= false;
27875 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
27876 RT_TRANSACTION_CANCEL
);
27877 gcc_assert (token
!= NULL
);
27879 attrs
= cp_parser_txn_attribute_opt (parser
);
27881 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
27883 /* ??? Parse cancel-and-throw here. */
27885 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27889 error_at (token
->location
, "%<__transaction_cancel%> without "
27890 "transactional memory support enabled");
27891 return error_mark_node
;
27893 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
27895 error_at (token
->location
, "%<__transaction_cancel%> within a "
27896 "%<__transaction_relaxed%>");
27897 return error_mark_node
;
27901 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
27902 && !is_tm_may_cancel_outer (current_function_decl
))
27904 error_at (token
->location
, "outer %<__transaction_cancel%> not "
27905 "within outer %<__transaction_atomic%>");
27906 error_at (token
->location
,
27907 " or a %<transaction_may_cancel_outer%> function");
27908 return error_mark_node
;
27911 else if (parser
->in_transaction
== 0)
27913 error_at (token
->location
, "%<__transaction_cancel%> not within "
27914 "%<__transaction_atomic%>");
27915 return error_mark_node
;
27918 stmt
= build_tm_abort_call (token
->location
, is_outer
);
27927 static GTY (()) cp_parser
*the_parser
;
27930 /* Special handling for the first token or line in the file. The first
27931 thing in the file might be #pragma GCC pch_preprocess, which loads a
27932 PCH file, which is a GC collection point. So we need to handle this
27933 first pragma without benefit of an existing lexer structure.
27935 Always returns one token to the caller in *FIRST_TOKEN. This is
27936 either the true first token of the file, or the first token after
27937 the initial pragma. */
27940 cp_parser_initial_pragma (cp_token
*first_token
)
27944 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27945 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
27948 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27949 if (first_token
->type
== CPP_STRING
)
27951 name
= first_token
->u
.value
;
27953 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27954 if (first_token
->type
!= CPP_PRAGMA_EOL
)
27955 error_at (first_token
->location
,
27956 "junk at end of %<#pragma GCC pch_preprocess%>");
27959 error_at (first_token
->location
, "expected string literal");
27961 /* Skip to the end of the pragma. */
27962 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
27963 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27965 /* Now actually load the PCH file. */
27967 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
27969 /* Read one more token to return to our caller. We have to do this
27970 after reading the PCH file in, since its pointers have to be
27972 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27975 /* Normal parsing of a pragma token. Here we can (and must) use the
27979 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
27981 cp_token
*pragma_tok
;
27984 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
27985 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
27986 parser
->lexer
->in_pragma
= true;
27988 id
= pragma_tok
->pragma_kind
;
27991 case PRAGMA_GCC_PCH_PREPROCESS
:
27992 error_at (pragma_tok
->location
,
27993 "%<#pragma GCC pch_preprocess%> must be first");
27996 case PRAGMA_OMP_BARRIER
:
27999 case pragma_compound
:
28000 cp_parser_omp_barrier (parser
, pragma_tok
);
28003 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
28004 "used in compound statements");
28011 case PRAGMA_OMP_FLUSH
:
28014 case pragma_compound
:
28015 cp_parser_omp_flush (parser
, pragma_tok
);
28018 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
28019 "used in compound statements");
28026 case PRAGMA_OMP_TASKWAIT
:
28029 case pragma_compound
:
28030 cp_parser_omp_taskwait (parser
, pragma_tok
);
28033 error_at (pragma_tok
->location
,
28034 "%<#pragma omp taskwait%> may only be "
28035 "used in compound statements");
28042 case PRAGMA_OMP_TASKYIELD
:
28045 case pragma_compound
:
28046 cp_parser_omp_taskyield (parser
, pragma_tok
);
28049 error_at (pragma_tok
->location
,
28050 "%<#pragma omp taskyield%> may only be "
28051 "used in compound statements");
28058 case PRAGMA_OMP_THREADPRIVATE
:
28059 cp_parser_omp_threadprivate (parser
, pragma_tok
);
28062 case PRAGMA_OMP_ATOMIC
:
28063 case PRAGMA_OMP_CRITICAL
:
28064 case PRAGMA_OMP_FOR
:
28065 case PRAGMA_OMP_MASTER
:
28066 case PRAGMA_OMP_ORDERED
:
28067 case PRAGMA_OMP_PARALLEL
:
28068 case PRAGMA_OMP_SECTIONS
:
28069 case PRAGMA_OMP_SINGLE
:
28070 case PRAGMA_OMP_TASK
:
28071 if (context
== pragma_external
)
28073 cp_parser_omp_construct (parser
, pragma_tok
);
28076 case PRAGMA_OMP_SECTION
:
28077 error_at (pragma_tok
->location
,
28078 "%<#pragma omp section%> may only be used in "
28079 "%<#pragma omp sections%> construct");
28083 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
28084 c_invoke_pragma_handler (id
);
28088 cp_parser_error (parser
, "expected declaration specifiers");
28092 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
28096 /* The interface the pragma parsers have to the lexer. */
28099 pragma_lex (tree
*value
)
28102 enum cpp_ttype ret
;
28104 tok
= cp_lexer_peek_token (the_parser
->lexer
);
28107 *value
= tok
->u
.value
;
28109 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
28111 else if (ret
== CPP_STRING
)
28112 *value
= cp_parser_string_literal (the_parser
, false, false);
28115 cp_lexer_consume_token (the_parser
->lexer
);
28116 if (ret
== CPP_KEYWORD
)
28124 /* External interface. */
28126 /* Parse one entire translation unit. */
28129 c_parse_file (void)
28131 static bool already_called
= false;
28133 if (already_called
)
28135 sorry ("inter-module optimizations not implemented for C++");
28138 already_called
= true;
28140 the_parser
= cp_parser_new ();
28141 push_deferring_access_checks (flag_access_control
28142 ? dk_no_deferred
: dk_no_check
);
28143 cp_parser_translation_unit (the_parser
);
28147 #include "gt-cp-parser.h"