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 ATTRIBUTE_UNUSED
, 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
*);
1201 static cp_declarator
*make_reference_declarator
1202 (cp_cv_quals
, cp_declarator
*, bool);
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
*);
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
->declarator
= NULL
;
1235 declarator
->parameter_pack_p
= false;
1236 declarator
->id_loc
= UNKNOWN_LOCATION
;
1241 /* Make a declarator for a generalized identifier. If
1242 QUALIFYING_SCOPE is non-NULL, the identifier is
1243 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1244 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1247 static cp_declarator
*
1248 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1249 special_function_kind sfk
)
1251 cp_declarator
*declarator
;
1253 /* It is valid to write:
1255 class C { void f(); };
1259 The standard is not clear about whether `typedef const C D' is
1260 legal; as of 2002-09-15 the committee is considering that
1261 question. EDG 3.0 allows that syntax. Therefore, we do as
1263 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1264 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1266 gcc_assert (TREE_CODE (unqualified_name
) == IDENTIFIER_NODE
1267 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1268 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1270 declarator
= make_declarator (cdk_id
);
1271 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1272 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1273 declarator
->u
.id
.sfk
= sfk
;
1278 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1279 of modifiers such as const or volatile to apply to the pointer
1280 type, represented as identifiers. */
1283 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
)
1285 cp_declarator
*declarator
;
1287 declarator
= make_declarator (cdk_pointer
);
1288 declarator
->declarator
= target
;
1289 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1290 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1293 declarator
->id_loc
= target
->id_loc
;
1294 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1295 target
->parameter_pack_p
= false;
1298 declarator
->parameter_pack_p
= false;
1303 /* Like make_pointer_declarator -- but for references. */
1306 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1309 cp_declarator
*declarator
;
1311 declarator
= make_declarator (cdk_reference
);
1312 declarator
->declarator
= target
;
1313 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1314 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1317 declarator
->id_loc
= target
->id_loc
;
1318 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1319 target
->parameter_pack_p
= false;
1322 declarator
->parameter_pack_p
= false;
1327 /* Like make_pointer_declarator -- but for a pointer to a non-static
1328 member of CLASS_TYPE. */
1331 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1332 cp_declarator
*pointee
)
1334 cp_declarator
*declarator
;
1336 declarator
= make_declarator (cdk_ptrmem
);
1337 declarator
->declarator
= pointee
;
1338 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1339 declarator
->u
.pointer
.class_type
= class_type
;
1343 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1344 pointee
->parameter_pack_p
= false;
1347 declarator
->parameter_pack_p
= false;
1352 /* Make a declarator for the function given by TARGET, with the
1353 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1354 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1355 indicates what exceptions can be thrown. */
1358 make_call_declarator (cp_declarator
*target
,
1360 cp_cv_quals cv_qualifiers
,
1361 cp_virt_specifiers virt_specifiers
,
1362 tree exception_specification
,
1363 tree late_return_type
)
1365 cp_declarator
*declarator
;
1367 declarator
= make_declarator (cdk_function
);
1368 declarator
->declarator
= target
;
1369 declarator
->u
.function
.parameters
= parms
;
1370 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1371 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1372 declarator
->u
.function
.exception_specification
= exception_specification
;
1373 declarator
->u
.function
.late_return_type
= late_return_type
;
1376 declarator
->id_loc
= target
->id_loc
;
1377 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1378 target
->parameter_pack_p
= false;
1381 declarator
->parameter_pack_p
= false;
1386 /* Make a declarator for an array of BOUNDS elements, each of which is
1387 defined by ELEMENT. */
1390 make_array_declarator (cp_declarator
*element
, tree bounds
)
1392 cp_declarator
*declarator
;
1394 declarator
= make_declarator (cdk_array
);
1395 declarator
->declarator
= element
;
1396 declarator
->u
.array
.bounds
= bounds
;
1399 declarator
->id_loc
= element
->id_loc
;
1400 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1401 element
->parameter_pack_p
= false;
1404 declarator
->parameter_pack_p
= false;
1409 /* Determine whether the declarator we've seen so far can be a
1410 parameter pack, when followed by an ellipsis. */
1412 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1414 /* Search for a declarator name, or any other declarator that goes
1415 after the point where the ellipsis could appear in a parameter
1416 pack. If we find any of these, then this declarator can not be
1417 made into a parameter pack. */
1419 while (declarator
&& !found
)
1421 switch ((int)declarator
->kind
)
1432 declarator
= declarator
->declarator
;
1440 cp_parameter_declarator
*no_parameters
;
1442 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1443 DECLARATOR and DEFAULT_ARGUMENT. */
1445 cp_parameter_declarator
*
1446 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1447 cp_declarator
*declarator
,
1448 tree default_argument
)
1450 cp_parameter_declarator
*parameter
;
1452 parameter
= ((cp_parameter_declarator
*)
1453 alloc_declarator (sizeof (cp_parameter_declarator
)));
1454 parameter
->next
= NULL
;
1455 if (decl_specifiers
)
1456 parameter
->decl_specifiers
= *decl_specifiers
;
1458 clear_decl_specs (¶meter
->decl_specifiers
);
1459 parameter
->declarator
= declarator
;
1460 parameter
->default_argument
= default_argument
;
1461 parameter
->ellipsis_p
= false;
1466 /* Returns true iff DECLARATOR is a declaration for a function. */
1469 function_declarator_p (const cp_declarator
*declarator
)
1473 if (declarator
->kind
== cdk_function
1474 && declarator
->declarator
->kind
== cdk_id
)
1476 if (declarator
->kind
== cdk_id
1477 || declarator
->kind
== cdk_error
)
1479 declarator
= declarator
->declarator
;
1489 A cp_parser parses the token stream as specified by the C++
1490 grammar. Its job is purely parsing, not semantic analysis. For
1491 example, the parser breaks the token stream into declarators,
1492 expressions, statements, and other similar syntactic constructs.
1493 It does not check that the types of the expressions on either side
1494 of an assignment-statement are compatible, or that a function is
1495 not declared with a parameter of type `void'.
1497 The parser invokes routines elsewhere in the compiler to perform
1498 semantic analysis and to build up the abstract syntax tree for the
1501 The parser (and the template instantiation code, which is, in a
1502 way, a close relative of parsing) are the only parts of the
1503 compiler that should be calling push_scope and pop_scope, or
1504 related functions. The parser (and template instantiation code)
1505 keeps track of what scope is presently active; everything else
1506 should simply honor that. (The code that generates static
1507 initializers may also need to set the scope, in order to check
1508 access control correctly when emitting the initializers.)
1513 The parser is of the standard recursive-descent variety. Upcoming
1514 tokens in the token stream are examined in order to determine which
1515 production to use when parsing a non-terminal. Some C++ constructs
1516 require arbitrary look ahead to disambiguate. For example, it is
1517 impossible, in the general case, to tell whether a statement is an
1518 expression or declaration without scanning the entire statement.
1519 Therefore, the parser is capable of "parsing tentatively." When the
1520 parser is not sure what construct comes next, it enters this mode.
1521 Then, while we attempt to parse the construct, the parser queues up
1522 error messages, rather than issuing them immediately, and saves the
1523 tokens it consumes. If the construct is parsed successfully, the
1524 parser "commits", i.e., it issues any queued error messages and
1525 the tokens that were being preserved are permanently discarded.
1526 If, however, the construct is not parsed successfully, the parser
1527 rolls back its state completely so that it can resume parsing using
1528 a different alternative.
1533 The performance of the parser could probably be improved substantially.
1534 We could often eliminate the need to parse tentatively by looking ahead
1535 a little bit. In some places, this approach might not entirely eliminate
1536 the need to parse tentatively, but it might still speed up the average
1539 /* Flags that are passed to some parsing functions. These values can
1540 be bitwise-ored together. */
1545 CP_PARSER_FLAGS_NONE
= 0x0,
1546 /* The construct is optional. If it is not present, then no error
1547 should be issued. */
1548 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1549 /* When parsing a type-specifier, treat user-defined type-names
1550 as non-type identifiers. */
1551 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1552 /* When parsing a type-specifier, do not try to parse a class-specifier
1553 or enum-specifier. */
1554 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1555 /* When parsing a decl-specifier-seq, only allow type-specifier or
1557 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1560 /* This type is used for parameters and variables which hold
1561 combinations of the above flags. */
1562 typedef int cp_parser_flags
;
1564 /* The different kinds of declarators we want to parse. */
1566 typedef enum cp_parser_declarator_kind
1568 /* We want an abstract declarator. */
1569 CP_PARSER_DECLARATOR_ABSTRACT
,
1570 /* We want a named declarator. */
1571 CP_PARSER_DECLARATOR_NAMED
,
1572 /* We don't mind, but the name must be an unqualified-id. */
1573 CP_PARSER_DECLARATOR_EITHER
1574 } cp_parser_declarator_kind
;
1576 /* The precedence values used to parse binary expressions. The minimum value
1577 of PREC must be 1, because zero is reserved to quickly discriminate
1578 binary operators from other tokens. */
1583 PREC_LOGICAL_OR_EXPRESSION
,
1584 PREC_LOGICAL_AND_EXPRESSION
,
1585 PREC_INCLUSIVE_OR_EXPRESSION
,
1586 PREC_EXCLUSIVE_OR_EXPRESSION
,
1587 PREC_AND_EXPRESSION
,
1588 PREC_EQUALITY_EXPRESSION
,
1589 PREC_RELATIONAL_EXPRESSION
,
1590 PREC_SHIFT_EXPRESSION
,
1591 PREC_ADDITIVE_EXPRESSION
,
1592 PREC_MULTIPLICATIVE_EXPRESSION
,
1594 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1597 /* A mapping from a token type to a corresponding tree node type, with a
1598 precedence value. */
1600 typedef struct cp_parser_binary_operations_map_node
1602 /* The token type. */
1603 enum cpp_ttype token_type
;
1604 /* The corresponding tree code. */
1605 enum tree_code tree_type
;
1606 /* The precedence of this operator. */
1607 enum cp_parser_prec prec
;
1608 } cp_parser_binary_operations_map_node
;
1610 typedef struct cp_parser_expression_stack_entry
1612 /* Left hand side of the binary operation we are currently
1615 /* Original tree code for left hand side, if it was a binary
1616 expression itself (used for -Wparentheses). */
1617 enum tree_code lhs_type
;
1618 /* Tree code for the binary operation we are parsing. */
1619 enum tree_code tree_type
;
1620 /* Precedence of the binary operation we are parsing. */
1621 enum cp_parser_prec prec
;
1622 /* Location of the binary operation we are parsing. */
1624 } cp_parser_expression_stack_entry
;
1626 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1627 entries because precedence levels on the stack are monotonically
1629 typedef struct cp_parser_expression_stack_entry
1630 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1634 /* Constructors and destructors. */
1636 static cp_parser_context
*cp_parser_context_new
1637 (cp_parser_context
*);
1639 /* Class variables. */
1641 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644 Transformed into an associative array (binops_by_token) by
1647 static const cp_parser_binary_operations_map_node binops
[] = {
1648 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1649 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1651 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1652 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1653 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1655 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1656 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1658 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1659 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1661 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1662 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1663 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1664 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1666 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1667 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1669 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1671 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1673 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1675 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1677 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1680 /* The same as binops, but initialized by cp_parser_new so that
1681 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1683 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1685 /* Constructors and destructors. */
1687 /* Construct a new context. The context below this one on the stack
1688 is given by NEXT. */
1690 static cp_parser_context
*
1691 cp_parser_context_new (cp_parser_context
* next
)
1693 cp_parser_context
*context
;
1695 /* Allocate the storage. */
1696 if (cp_parser_context_free_list
!= NULL
)
1698 /* Pull the first entry from the free list. */
1699 context
= cp_parser_context_free_list
;
1700 cp_parser_context_free_list
= context
->next
;
1701 memset (context
, 0, sizeof (*context
));
1704 context
= ggc_alloc_cleared_cp_parser_context ();
1706 /* No errors have occurred yet in this context. */
1707 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1708 /* If this is not the bottommost context, copy information that we
1709 need from the previous context. */
1712 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713 expression, then we are parsing one in this context, too. */
1714 context
->object_type
= next
->object_type
;
1715 /* Thread the stack. */
1716 context
->next
= next
;
1722 /* Managing the unparsed function queues. */
1724 #define unparsed_funs_with_default_args \
1725 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_definitions
1728 #define unparsed_nsdmis \
1729 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).nsdmis
1732 push_unparsed_function_queues (cp_parser
*parser
)
1734 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
};
1735 VEC_safe_push (cp_unparsed_functions_entry
, gc
, parser
->unparsed_queues
, e
);
1739 pop_unparsed_function_queues (cp_parser
*parser
)
1741 release_tree_vector (unparsed_funs_with_definitions
);
1742 VEC_pop (cp_unparsed_functions_entry
, parser
->unparsed_queues
);
1747 /* Constructors and destructors. */
1749 static cp_parser
*cp_parser_new
1752 /* Routines to parse various constructs.
1754 Those that return `tree' will return the error_mark_node (rather
1755 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1756 Sometimes, they will return an ordinary node if error-recovery was
1757 attempted, even though a parse error occurred. So, to check
1758 whether or not a parse error occurred, you should always use
1759 cp_parser_error_occurred. If the construct is optional (indicated
1760 either by an `_opt' in the name of the function that does the
1761 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1762 the construct is not present. */
1764 /* Lexical conventions [gram.lex] */
1766 static tree cp_parser_identifier
1768 static tree cp_parser_string_literal
1769 (cp_parser
*, bool, bool);
1770 static tree cp_parser_userdef_char_literal
1772 static tree cp_parser_userdef_string_literal
1774 static tree cp_parser_userdef_numeric_literal
1777 /* Basic concepts [gram.basic] */
1779 static bool cp_parser_translation_unit
1782 /* Expressions [gram.expr] */
1784 static tree cp_parser_primary_expression
1785 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1786 static tree cp_parser_id_expression
1787 (cp_parser
*, bool, bool, bool *, bool, bool);
1788 static tree cp_parser_unqualified_id
1789 (cp_parser
*, bool, bool, bool, bool);
1790 static tree cp_parser_nested_name_specifier_opt
1791 (cp_parser
*, bool, bool, bool, bool);
1792 static tree cp_parser_nested_name_specifier
1793 (cp_parser
*, bool, bool, bool, bool);
1794 static tree cp_parser_qualifying_entity
1795 (cp_parser
*, bool, bool, bool, bool, bool);
1796 static tree cp_parser_postfix_expression
1797 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1798 static tree cp_parser_postfix_open_square_expression
1799 (cp_parser
*, tree
, bool);
1800 static tree cp_parser_postfix_dot_deref_expression
1801 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1802 static VEC(tree
,gc
) *cp_parser_parenthesized_expression_list
1803 (cp_parser
*, int, bool, bool, bool *);
1804 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1805 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1806 static void cp_parser_pseudo_destructor_name
1807 (cp_parser
*, tree
*, tree
*);
1808 static tree cp_parser_unary_expression
1809 (cp_parser
*, bool, bool, cp_id_kind
*);
1810 static enum tree_code cp_parser_unary_operator
1812 static tree cp_parser_new_expression
1814 static VEC(tree
,gc
) *cp_parser_new_placement
1816 static tree cp_parser_new_type_id
1817 (cp_parser
*, tree
*);
1818 static cp_declarator
*cp_parser_new_declarator_opt
1820 static cp_declarator
*cp_parser_direct_new_declarator
1822 static VEC(tree
,gc
) *cp_parser_new_initializer
1824 static tree cp_parser_delete_expression
1826 static tree cp_parser_cast_expression
1827 (cp_parser
*, bool, bool, cp_id_kind
*);
1828 static tree cp_parser_binary_expression
1829 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1830 static tree cp_parser_question_colon_clause
1831 (cp_parser
*, tree
);
1832 static tree cp_parser_assignment_expression
1833 (cp_parser
*, bool, cp_id_kind
*);
1834 static enum tree_code cp_parser_assignment_operator_opt
1836 static tree cp_parser_expression
1837 (cp_parser
*, bool, cp_id_kind
*);
1838 static tree cp_parser_constant_expression
1839 (cp_parser
*, bool, bool *);
1840 static tree cp_parser_builtin_offsetof
1842 static tree cp_parser_lambda_expression
1844 static void cp_parser_lambda_introducer
1845 (cp_parser
*, tree
);
1846 static bool cp_parser_lambda_declarator_opt
1847 (cp_parser
*, tree
);
1848 static void cp_parser_lambda_body
1849 (cp_parser
*, tree
);
1851 /* Statements [gram.stmt.stmt] */
1853 static void cp_parser_statement
1854 (cp_parser
*, tree
, bool, bool *);
1855 static void cp_parser_label_for_labeled_statement
1857 static tree cp_parser_expression_statement
1858 (cp_parser
*, tree
);
1859 static tree cp_parser_compound_statement
1860 (cp_parser
*, tree
, bool, bool);
1861 static void cp_parser_statement_seq_opt
1862 (cp_parser
*, tree
);
1863 static tree cp_parser_selection_statement
1864 (cp_parser
*, bool *);
1865 static tree cp_parser_condition
1867 static tree cp_parser_iteration_statement
1869 static bool cp_parser_for_init_statement
1870 (cp_parser
*, tree
*decl
);
1871 static tree cp_parser_for
1873 static tree cp_parser_c_for
1874 (cp_parser
*, tree
, tree
);
1875 static tree cp_parser_range_for
1876 (cp_parser
*, tree
, tree
, tree
);
1877 static void do_range_for_auto_deduction
1879 static tree cp_parser_perform_range_for_lookup
1880 (tree
, tree
*, tree
*);
1881 static tree cp_parser_range_for_member_function
1883 static tree cp_parser_jump_statement
1885 static void cp_parser_declaration_statement
1888 static tree cp_parser_implicitly_scoped_statement
1889 (cp_parser
*, bool *);
1890 static void cp_parser_already_scoped_statement
1893 /* Declarations [gram.dcl.dcl] */
1895 static void cp_parser_declaration_seq_opt
1897 static void cp_parser_declaration
1899 static void cp_parser_block_declaration
1900 (cp_parser
*, bool);
1901 static void cp_parser_simple_declaration
1902 (cp_parser
*, bool, tree
*);
1903 static void cp_parser_decl_specifier_seq
1904 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
1905 static tree cp_parser_storage_class_specifier_opt
1907 static tree cp_parser_function_specifier_opt
1908 (cp_parser
*, cp_decl_specifier_seq
*);
1909 static tree cp_parser_type_specifier
1910 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
1912 static tree cp_parser_simple_type_specifier
1913 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
1914 static tree cp_parser_type_name
1916 static tree cp_parser_nonclass_name
1917 (cp_parser
* parser
);
1918 static tree cp_parser_elaborated_type_specifier
1919 (cp_parser
*, bool, bool);
1920 static tree cp_parser_enum_specifier
1922 static void cp_parser_enumerator_list
1923 (cp_parser
*, tree
);
1924 static void cp_parser_enumerator_definition
1925 (cp_parser
*, tree
);
1926 static tree cp_parser_namespace_name
1928 static void cp_parser_namespace_definition
1930 static void cp_parser_namespace_body
1932 static tree cp_parser_qualified_namespace_specifier
1934 static void cp_parser_namespace_alias_definition
1936 static bool cp_parser_using_declaration
1937 (cp_parser
*, bool);
1938 static void cp_parser_using_directive
1940 static tree cp_parser_alias_declaration
1942 static void cp_parser_asm_definition
1944 static void cp_parser_linkage_specification
1946 static void cp_parser_static_assert
1947 (cp_parser
*, bool);
1948 static tree cp_parser_decltype
1951 /* Declarators [gram.dcl.decl] */
1953 static tree cp_parser_init_declarator
1954 (cp_parser
*, cp_decl_specifier_seq
*, VEC (deferred_access_check
,gc
)*, bool, bool, int, bool *, tree
*);
1955 static cp_declarator
*cp_parser_declarator
1956 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool);
1957 static cp_declarator
*cp_parser_direct_declarator
1958 (cp_parser
*, cp_parser_declarator_kind
, int *, bool);
1959 static enum tree_code cp_parser_ptr_operator
1960 (cp_parser
*, tree
*, cp_cv_quals
*);
1961 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1963 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1965 static tree cp_parser_late_return_type_opt
1966 (cp_parser
*, cp_cv_quals
);
1967 static tree cp_parser_declarator_id
1968 (cp_parser
*, bool);
1969 static tree cp_parser_type_id
1971 static tree cp_parser_template_type_arg
1973 static tree
cp_parser_trailing_type_id (cp_parser
*);
1974 static tree cp_parser_type_id_1
1975 (cp_parser
*, bool, bool);
1976 static void cp_parser_type_specifier_seq
1977 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
1978 static tree cp_parser_parameter_declaration_clause
1980 static tree cp_parser_parameter_declaration_list
1981 (cp_parser
*, bool *);
1982 static cp_parameter_declarator
*cp_parser_parameter_declaration
1983 (cp_parser
*, bool, bool *);
1984 static tree cp_parser_default_argument
1985 (cp_parser
*, bool);
1986 static void cp_parser_function_body
1987 (cp_parser
*, bool);
1988 static tree cp_parser_initializer
1989 (cp_parser
*, bool *, bool *);
1990 static tree cp_parser_initializer_clause
1991 (cp_parser
*, bool *);
1992 static tree cp_parser_braced_list
1993 (cp_parser
*, bool*);
1994 static VEC(constructor_elt
,gc
) *cp_parser_initializer_list
1995 (cp_parser
*, bool *);
1997 static bool cp_parser_ctor_initializer_opt_and_function_body
1998 (cp_parser
*, bool);
2000 /* Classes [gram.class] */
2002 static tree cp_parser_class_name
2003 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2004 static tree cp_parser_class_specifier
2006 static tree cp_parser_class_head
2007 (cp_parser
*, bool *);
2008 static enum tag_types cp_parser_class_key
2010 static void cp_parser_member_specification_opt
2012 static void cp_parser_member_declaration
2014 static tree cp_parser_pure_specifier
2016 static tree cp_parser_constant_initializer
2019 /* Derived classes [gram.class.derived] */
2021 static tree cp_parser_base_clause
2023 static tree cp_parser_base_specifier
2026 /* Special member functions [gram.special] */
2028 static tree cp_parser_conversion_function_id
2030 static tree cp_parser_conversion_type_id
2032 static cp_declarator
*cp_parser_conversion_declarator_opt
2034 static bool cp_parser_ctor_initializer_opt
2036 static void cp_parser_mem_initializer_list
2038 static tree cp_parser_mem_initializer
2040 static tree cp_parser_mem_initializer_id
2043 /* Overloading [gram.over] */
2045 static tree cp_parser_operator_function_id
2047 static tree cp_parser_operator
2050 /* Templates [gram.temp] */
2052 static void cp_parser_template_declaration
2053 (cp_parser
*, bool);
2054 static tree cp_parser_template_parameter_list
2056 static tree cp_parser_template_parameter
2057 (cp_parser
*, bool *, bool *);
2058 static tree cp_parser_type_parameter
2059 (cp_parser
*, bool *);
2060 static tree cp_parser_template_id
2061 (cp_parser
*, bool, bool, enum tag_types
, bool);
2062 static tree cp_parser_template_name
2063 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2064 static tree cp_parser_template_argument_list
2066 static tree cp_parser_template_argument
2068 static void cp_parser_explicit_instantiation
2070 static void cp_parser_explicit_specialization
2073 /* Exception handling [gram.exception] */
2075 static tree cp_parser_try_block
2077 static bool cp_parser_function_try_block
2079 static void cp_parser_handler_seq
2081 static void cp_parser_handler
2083 static tree cp_parser_exception_declaration
2085 static tree cp_parser_throw_expression
2087 static tree cp_parser_exception_specification_opt
2089 static tree cp_parser_type_id_list
2092 /* GNU Extensions */
2094 static tree cp_parser_asm_specification_opt
2096 static tree cp_parser_asm_operand_list
2098 static tree cp_parser_asm_clobber_list
2100 static tree cp_parser_asm_label_list
2102 static tree cp_parser_attributes_opt
2104 static tree cp_parser_attribute_list
2106 static bool cp_parser_extension_opt
2107 (cp_parser
*, int *);
2108 static void cp_parser_label_declaration
2111 /* Transactional Memory Extensions */
2113 static tree cp_parser_transaction
2114 (cp_parser
*, enum rid
);
2115 static tree cp_parser_transaction_expression
2116 (cp_parser
*, enum rid
);
2117 static bool cp_parser_function_transaction
2118 (cp_parser
*, enum rid
);
2119 static tree cp_parser_transaction_cancel
2122 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
2123 static bool cp_parser_pragma
2124 (cp_parser
*, enum pragma_context
);
2126 /* Objective-C++ Productions */
2128 static tree cp_parser_objc_message_receiver
2130 static tree cp_parser_objc_message_args
2132 static tree cp_parser_objc_message_expression
2134 static tree cp_parser_objc_encode_expression
2136 static tree cp_parser_objc_defs_expression
2138 static tree cp_parser_objc_protocol_expression
2140 static tree cp_parser_objc_selector_expression
2142 static tree cp_parser_objc_expression
2144 static bool cp_parser_objc_selector_p
2146 static tree cp_parser_objc_selector
2148 static tree cp_parser_objc_protocol_refs_opt
2150 static void cp_parser_objc_declaration
2151 (cp_parser
*, tree
);
2152 static tree cp_parser_objc_statement
2154 static bool cp_parser_objc_valid_prefix_attributes
2155 (cp_parser
*, tree
*);
2156 static void cp_parser_objc_at_property_declaration
2158 static void cp_parser_objc_at_synthesize_declaration
2160 static void cp_parser_objc_at_dynamic_declaration
2162 static tree cp_parser_objc_struct_declaration
2165 /* Utility Routines */
2167 static tree cp_parser_lookup_name
2168 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2169 static tree cp_parser_lookup_name_simple
2170 (cp_parser
*, tree
, location_t
);
2171 static tree cp_parser_maybe_treat_template_as_class
2173 static bool cp_parser_check_declarator_template_parameters
2174 (cp_parser
*, cp_declarator
*, location_t
);
2175 static bool cp_parser_check_template_parameters
2176 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2177 static tree cp_parser_simple_cast_expression
2179 static tree cp_parser_global_scope_opt
2180 (cp_parser
*, bool);
2181 static bool cp_parser_constructor_declarator_p
2182 (cp_parser
*, bool);
2183 static tree cp_parser_function_definition_from_specifiers_and_declarator
2184 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2185 static tree cp_parser_function_definition_after_declarator
2186 (cp_parser
*, bool);
2187 static void cp_parser_template_declaration_after_export
2188 (cp_parser
*, bool);
2189 static void cp_parser_perform_template_parameter_access_checks
2190 (VEC (deferred_access_check
,gc
)*);
2191 static tree cp_parser_single_declaration
2192 (cp_parser
*, VEC (deferred_access_check
,gc
)*, bool, bool, bool *);
2193 static tree cp_parser_functional_cast
2194 (cp_parser
*, tree
);
2195 static tree cp_parser_save_member_function_body
2196 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2197 static tree cp_parser_save_nsdmi
2199 static tree cp_parser_enclosed_template_argument_list
2201 static void cp_parser_save_default_args
2202 (cp_parser
*, tree
);
2203 static void cp_parser_late_parsing_for_member
2204 (cp_parser
*, tree
);
2205 static tree cp_parser_late_parse_one_default_arg
2206 (cp_parser
*, tree
, tree
, tree
);
2207 static void cp_parser_late_parsing_nsdmi
2208 (cp_parser
*, tree
);
2209 static void cp_parser_late_parsing_default_args
2210 (cp_parser
*, tree
);
2211 static tree cp_parser_sizeof_operand
2212 (cp_parser
*, enum rid
);
2213 static tree cp_parser_trait_expr
2214 (cp_parser
*, enum rid
);
2215 static bool cp_parser_declares_only_class_p
2217 static void cp_parser_set_storage_class
2218 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, location_t
);
2219 static void cp_parser_set_decl_spec_type
2220 (cp_decl_specifier_seq
*, tree
, location_t
, bool);
2221 static void set_and_check_decl_spec_loc
2222 (cp_decl_specifier_seq
*decl_specs
,
2223 cp_decl_spec ds
, source_location location
);
2224 static bool cp_parser_friend_p
2225 (const cp_decl_specifier_seq
*);
2226 static void cp_parser_required_error
2227 (cp_parser
*, required_token
, bool);
2228 static cp_token
*cp_parser_require
2229 (cp_parser
*, enum cpp_ttype
, required_token
);
2230 static cp_token
*cp_parser_require_keyword
2231 (cp_parser
*, enum rid
, required_token
);
2232 static bool cp_parser_token_starts_function_definition_p
2234 static bool cp_parser_next_token_starts_class_definition_p
2236 static bool cp_parser_next_token_ends_template_argument_p
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239 (cp_parser
*, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2242 static void cp_parser_check_class_key
2243 (enum tag_types
, tree type
);
2244 static void cp_parser_check_access_in_redeclaration
2245 (tree type
, location_t location
);
2246 static bool cp_parser_optional_template_keyword
2248 static void cp_parser_pre_parsed_nested_name_specifier
2250 static bool cp_parser_cache_group
2251 (cp_parser
*, enum cpp_ttype
, unsigned);
2252 static tree cp_parser_cache_defarg
2253 (cp_parser
*parser
, bool nsdmi
);
2254 static void cp_parser_parse_tentatively
2256 static void cp_parser_commit_to_tentative_parse
2258 static void cp_parser_abort_tentative_parse
2260 static bool cp_parser_parse_definitely
2262 static inline bool cp_parser_parsing_tentatively
2264 static bool cp_parser_uncommitted_to_tentative_parse_p
2266 static void cp_parser_error
2267 (cp_parser
*, const char *);
2268 static void cp_parser_name_lookup_error
2269 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2270 static bool cp_parser_simulate_error
2272 static bool cp_parser_check_type_definition
2274 static void cp_parser_check_for_definition_in_return_type
2275 (cp_declarator
*, tree
, location_t type_location
);
2276 static void cp_parser_check_for_invalid_template_id
2277 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2278 static bool cp_parser_non_integral_constant_expression
2279 (cp_parser
*, non_integral_constant
);
2280 static void cp_parser_diagnose_invalid_type_name
2281 (cp_parser
*, tree
, tree
, location_t
);
2282 static bool cp_parser_parse_and_diagnose_invalid_type_name
2284 static int cp_parser_skip_to_closing_parenthesis
2285 (cp_parser
*, bool, bool, bool);
2286 static void cp_parser_skip_to_end_of_statement
2288 static void cp_parser_consume_semicolon_at_end_of_statement
2290 static void cp_parser_skip_to_end_of_block_or_statement
2292 static bool cp_parser_skip_to_closing_brace
2294 static void cp_parser_skip_to_end_of_template_parameter_list
2296 static void cp_parser_skip_to_pragma_eol
2297 (cp_parser
*, cp_token
*);
2298 static bool cp_parser_error_occurred
2300 static bool cp_parser_allow_gnu_extensions_p
2302 static bool cp_parser_is_pure_string_literal
2304 static bool cp_parser_is_string_literal
2306 static bool cp_parser_is_keyword
2307 (cp_token
*, enum rid
);
2308 static tree cp_parser_make_typename_type
2309 (cp_parser
*, tree
, tree
, location_t location
);
2310 static cp_declarator
* cp_parser_make_indirect_declarator
2311 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*);
2313 /* Returns nonzero if we are parsing tentatively. */
2316 cp_parser_parsing_tentatively (cp_parser
* parser
)
2318 return parser
->context
->next
!= NULL
;
2321 /* Returns nonzero if TOKEN is a string literal. */
2324 cp_parser_is_pure_string_literal (cp_token
* token
)
2326 return (token
->type
== CPP_STRING
||
2327 token
->type
== CPP_STRING16
||
2328 token
->type
== CPP_STRING32
||
2329 token
->type
== CPP_WSTRING
||
2330 token
->type
== CPP_UTF8STRING
);
2333 /* Returns nonzero if TOKEN is a string literal
2334 of a user-defined string literal. */
2337 cp_parser_is_string_literal (cp_token
* token
)
2339 return (cp_parser_is_pure_string_literal (token
) ||
2340 token
->type
== CPP_STRING_USERDEF
||
2341 token
->type
== CPP_STRING16_USERDEF
||
2342 token
->type
== CPP_STRING32_USERDEF
||
2343 token
->type
== CPP_WSTRING_USERDEF
||
2344 token
->type
== CPP_UTF8STRING_USERDEF
);
2347 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2350 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2352 return token
->keyword
== keyword
;
2355 /* If not parsing tentatively, issue a diagnostic of the form
2356 FILE:LINE: MESSAGE before TOKEN
2357 where TOKEN is the next token in the input stream. MESSAGE
2358 (specified by the caller) is usually of the form "expected
2362 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2364 if (!cp_parser_simulate_error (parser
))
2366 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2367 /* This diagnostic makes more sense if it is tagged to the line
2368 of the token we just peeked at. */
2369 cp_lexer_set_source_position_from_token (token
);
2371 if (token
->type
== CPP_PRAGMA
)
2373 error_at (token
->location
,
2374 "%<#pragma%> is not allowed here");
2375 cp_parser_skip_to_pragma_eol (parser
, token
);
2379 c_parse_error (gmsgid
,
2380 /* Because c_parser_error does not understand
2381 CPP_KEYWORD, keywords are treated like
2383 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2384 token
->u
.value
, token
->flags
);
2388 /* Issue an error about name-lookup failing. NAME is the
2389 IDENTIFIER_NODE DECL is the result of
2390 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2391 the thing that we hoped to find. */
2394 cp_parser_name_lookup_error (cp_parser
* parser
,
2397 name_lookup_error desired
,
2398 location_t location
)
2400 /* If name lookup completely failed, tell the user that NAME was not
2402 if (decl
== error_mark_node
)
2404 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2405 error_at (location
, "%<%E::%E%> has not been declared",
2406 parser
->scope
, name
);
2407 else if (parser
->scope
== global_namespace
)
2408 error_at (location
, "%<::%E%> has not been declared", name
);
2409 else if (parser
->object_scope
2410 && !CLASS_TYPE_P (parser
->object_scope
))
2411 error_at (location
, "request for member %qE in non-class type %qT",
2412 name
, parser
->object_scope
);
2413 else if (parser
->object_scope
)
2414 error_at (location
, "%<%T::%E%> has not been declared",
2415 parser
->object_scope
, name
);
2417 error_at (location
, "%qE has not been declared", name
);
2419 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2424 error_at (location
, "%<%E::%E%> is not a type",
2425 parser
->scope
, name
);
2428 error_at (location
, "%<%E::%E%> is not a class or namespace",
2429 parser
->scope
, name
);
2433 "%<%E::%E%> is not a class, namespace, or enumeration",
2434 parser
->scope
, name
);
2441 else if (parser
->scope
== global_namespace
)
2446 error_at (location
, "%<::%E%> is not a type", name
);
2449 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2453 "%<::%E%> is not a class, namespace, or enumeration",
2465 error_at (location
, "%qE is not a type", name
);
2468 error_at (location
, "%qE is not a class or namespace", name
);
2472 "%qE is not a class, namespace, or enumeration", name
);
2480 /* If we are parsing tentatively, remember that an error has occurred
2481 during this tentative parse. Returns true if the error was
2482 simulated; false if a message should be issued by the caller. */
2485 cp_parser_simulate_error (cp_parser
* parser
)
2487 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2489 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2495 /* This function is called when a type is defined. If type
2496 definitions are forbidden at this point, an error message is
2500 cp_parser_check_type_definition (cp_parser
* parser
)
2502 /* If types are forbidden here, issue a message. */
2503 if (parser
->type_definition_forbidden_message
)
2505 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2506 in the message need to be interpreted. */
2507 error (parser
->type_definition_forbidden_message
);
2513 /* This function is called when the DECLARATOR is processed. The TYPE
2514 was a type defined in the decl-specifiers. If it is invalid to
2515 define a type in the decl-specifiers for DECLARATOR, an error is
2516 issued. TYPE_LOCATION is the location of TYPE and is used
2517 for error reporting. */
2520 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2521 tree type
, location_t type_location
)
2523 /* [dcl.fct] forbids type definitions in return types.
2524 Unfortunately, it's not easy to know whether or not we are
2525 processing a return type until after the fact. */
2527 && (declarator
->kind
== cdk_pointer
2528 || declarator
->kind
== cdk_reference
2529 || declarator
->kind
== cdk_ptrmem
))
2530 declarator
= declarator
->declarator
;
2532 && declarator
->kind
== cdk_function
)
2534 error_at (type_location
,
2535 "new types may not be defined in a return type");
2536 inform (type_location
,
2537 "(perhaps a semicolon is missing after the definition of %qT)",
2542 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2543 "<" in any valid C++ program. If the next token is indeed "<",
2544 issue a message warning the user about what appears to be an
2545 invalid attempt to form a template-id. LOCATION is the location
2546 of the type-specifier (TYPE) */
2549 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2551 enum tag_types tag_type
,
2552 location_t location
)
2554 cp_token_position start
= 0;
2556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2559 error_at (location
, "%qT is not a template", type
);
2560 else if (TREE_CODE (type
) == IDENTIFIER_NODE
)
2562 if (tag_type
!= none_type
)
2563 error_at (location
, "%qE is not a class template", type
);
2565 error_at (location
, "%qE is not a template", type
);
2568 error_at (location
, "invalid template-id");
2569 /* Remember the location of the invalid "<". */
2570 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2571 start
= cp_lexer_token_position (parser
->lexer
, true);
2572 /* Consume the "<". */
2573 cp_lexer_consume_token (parser
->lexer
);
2574 /* Parse the template arguments. */
2575 cp_parser_enclosed_template_argument_list (parser
);
2576 /* Permanently remove the invalid template arguments so that
2577 this error message is not issued again. */
2579 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2583 /* If parsing an integral constant-expression, issue an error message
2584 about the fact that THING appeared and return true. Otherwise,
2585 return false. In either case, set
2586 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2589 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2590 non_integral_constant thing
)
2592 parser
->non_integral_constant_expression_p
= true;
2593 if (parser
->integral_constant_expression_p
)
2595 if (!parser
->allow_non_integral_constant_expression_p
)
2597 const char *msg
= NULL
;
2601 error ("floating-point literal "
2602 "cannot appear in a constant-expression");
2605 error ("a cast to a type other than an integral or "
2606 "enumeration type cannot appear in a "
2607 "constant-expression");
2610 error ("%<typeid%> operator "
2611 "cannot appear in a constant-expression");
2614 error ("non-constant compound literals "
2615 "cannot appear in a constant-expression");
2618 error ("a function call "
2619 "cannot appear in a constant-expression");
2622 error ("an increment "
2623 "cannot appear in a constant-expression");
2626 error ("an decrement "
2627 "cannot appear in a constant-expression");
2630 error ("an array reference "
2631 "cannot appear in a constant-expression");
2633 case NIC_ADDR_LABEL
:
2634 error ("the address of a label "
2635 "cannot appear in a constant-expression");
2637 case NIC_OVERLOADED
:
2638 error ("calls to overloaded operators "
2639 "cannot appear in a constant-expression");
2641 case NIC_ASSIGNMENT
:
2642 error ("an assignment cannot appear in a constant-expression");
2645 error ("a comma operator "
2646 "cannot appear in a constant-expression");
2648 case NIC_CONSTRUCTOR
:
2649 error ("a call to a constructor "
2650 "cannot appear in a constant-expression");
2652 case NIC_TRANSACTION
:
2653 error ("a transaction expression "
2654 "cannot appear in a constant-expression");
2660 msg
= "__FUNCTION__";
2662 case NIC_PRETTY_FUNC
:
2663 msg
= "__PRETTY_FUNCTION__";
2683 case NIC_PREINCREMENT
:
2686 case NIC_PREDECREMENT
:
2699 error ("%qs cannot appear in a constant-expression", msg
);
2706 /* Emit a diagnostic for an invalid type name. SCOPE is the
2707 qualifying scope (or NULL, if none) for ID. This function commits
2708 to the current active tentative parse, if any. (Otherwise, the
2709 problematic construct might be encountered again later, resulting
2710 in duplicate error messages.) LOCATION is the location of ID. */
2713 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
,
2714 tree scope
, tree id
,
2715 location_t location
)
2717 tree decl
, old_scope
;
2718 cp_parser_commit_to_tentative_parse (parser
);
2719 /* Try to lookup the identifier. */
2720 old_scope
= parser
->scope
;
2721 parser
->scope
= scope
;
2722 decl
= cp_parser_lookup_name_simple (parser
, id
, location
);
2723 parser
->scope
= old_scope
;
2724 /* If the lookup found a template-name, it means that the user forgot
2725 to specify an argument list. Emit a useful error message. */
2726 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2728 "invalid use of template-name %qE without an argument list",
2730 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2731 error_at (location
, "invalid use of destructor %qD as a type", id
);
2732 else if (TREE_CODE (decl
) == TYPE_DECL
)
2733 /* Something like 'unsigned A a;' */
2734 error_at (location
, "invalid combination of multiple type-specifiers");
2735 else if (!parser
->scope
)
2737 /* Issue an error message. */
2738 error_at (location
, "%qE does not name a type", id
);
2739 /* If we're in a template class, it's possible that the user was
2740 referring to a type from a base class. For example:
2742 template <typename T> struct A { typedef T X; };
2743 template <typename T> struct B : public A<T> { X x; };
2745 The user should have said "typename A<T>::X". */
2746 if (cxx_dialect
< cxx0x
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2747 inform (location
, "C++11 %<constexpr%> only available with "
2748 "-std=c++11 or -std=gnu++11");
2749 else if (processing_template_decl
&& current_class_type
2750 && TYPE_BINFO (current_class_type
))
2754 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2758 tree base_type
= BINFO_TYPE (b
);
2759 if (CLASS_TYPE_P (base_type
)
2760 && dependent_type_p (base_type
))
2763 /* Go from a particular instantiation of the
2764 template (which will have an empty TYPE_FIELDs),
2765 to the main version. */
2766 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2767 for (field
= TYPE_FIELDS (base_type
);
2769 field
= DECL_CHAIN (field
))
2770 if (TREE_CODE (field
) == TYPE_DECL
2771 && DECL_NAME (field
) == id
)
2774 "(perhaps %<typename %T::%E%> was intended)",
2775 BINFO_TYPE (b
), id
);
2784 /* Here we diagnose qualified-ids where the scope is actually correct,
2785 but the identifier does not resolve to a valid type name. */
2786 else if (parser
->scope
!= error_mark_node
)
2788 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
2789 error_at (location
, "%qE in namespace %qE does not name a type",
2791 else if (CLASS_TYPE_P (parser
->scope
)
2792 && constructor_name_p (id
, parser
->scope
))
2795 error_at (location
, "%<%T::%E%> names the constructor, not"
2796 " the type", parser
->scope
, id
);
2797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2798 error_at (location
, "and %qT has no template constructors",
2801 else if (TYPE_P (parser
->scope
)
2802 && dependent_scope_p (parser
->scope
))
2803 error_at (location
, "need %<typename%> before %<%T::%E%> because "
2804 "%qT is a dependent scope",
2805 parser
->scope
, id
, parser
->scope
);
2806 else if (TYPE_P (parser
->scope
))
2807 error_at (location
, "%qE in %q#T does not name a type",
2814 /* Check for a common situation where a type-name should be present,
2815 but is not, and issue a sensible error message. Returns true if an
2816 invalid type-name was detected.
2818 The situation handled by this function are variable declarations of the
2819 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2820 Usually, `ID' should name a type, but if we got here it means that it
2821 does not. We try to emit the best possible error message depending on
2822 how exactly the id-expression looks like. */
2825 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
2828 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2830 /* Avoid duplicate error about ambiguous lookup. */
2831 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
2833 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
2834 if (next
->type
== CPP_NAME
&& next
->ambiguous_p
)
2838 cp_parser_parse_tentatively (parser
);
2839 id
= cp_parser_id_expression (parser
,
2840 /*template_keyword_p=*/false,
2841 /*check_dependency_p=*/true,
2842 /*template_p=*/NULL
,
2843 /*declarator_p=*/true,
2844 /*optional_p=*/false);
2845 /* If the next token is a (, this is a function with no explicit return
2846 type, i.e. constructor, destructor or conversion op. */
2847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
2848 || TREE_CODE (id
) == TYPE_DECL
)
2850 cp_parser_abort_tentative_parse (parser
);
2853 if (!cp_parser_parse_definitely (parser
))
2856 /* Emit a diagnostic for the invalid type. */
2857 cp_parser_diagnose_invalid_type_name (parser
, parser
->scope
,
2858 id
, token
->location
);
2860 /* If we aren't in the middle of a declarator (i.e. in a
2861 parameter-declaration-clause), skip to the end of the declaration;
2862 there's no point in trying to process it. */
2863 if (!parser
->in_declarator_p
)
2864 cp_parser_skip_to_end_of_block_or_statement (parser
);
2868 /* Consume tokens up to, and including, the next non-nested closing `)'.
2869 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2870 are doing error recovery. Returns -1 if OR_COMMA is true and we
2871 found an unnested comma. */
2874 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
2879 unsigned paren_depth
= 0;
2880 unsigned brace_depth
= 0;
2881 unsigned square_depth
= 0;
2883 if (recovering
&& !or_comma
2884 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
2889 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
2891 switch (token
->type
)
2894 case CPP_PRAGMA_EOL
:
2895 /* If we've run out of tokens, then there is no closing `)'. */
2898 /* This is good for lambda expression capture-lists. */
2899 case CPP_OPEN_SQUARE
:
2902 case CPP_CLOSE_SQUARE
:
2903 if (!square_depth
--)
2908 /* This matches the processing in skip_to_end_of_statement. */
2913 case CPP_OPEN_BRACE
:
2916 case CPP_CLOSE_BRACE
:
2922 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
2927 case CPP_OPEN_PAREN
:
2932 case CPP_CLOSE_PAREN
:
2933 if (!brace_depth
&& !paren_depth
--)
2936 cp_lexer_consume_token (parser
->lexer
);
2945 /* Consume the token. */
2946 cp_lexer_consume_token (parser
->lexer
);
2950 /* Consume tokens until we reach the end of the current statement.
2951 Normally, that will be just before consuming a `;'. However, if a
2952 non-nested `}' comes first, then we stop before consuming that. */
2955 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
2957 unsigned nesting_depth
= 0;
2961 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2963 switch (token
->type
)
2966 case CPP_PRAGMA_EOL
:
2967 /* If we've run out of tokens, stop. */
2971 /* If the next token is a `;', we have reached the end of the
2977 case CPP_CLOSE_BRACE
:
2978 /* If this is a non-nested '}', stop before consuming it.
2979 That way, when confronted with something like:
2983 we stop before consuming the closing '}', even though we
2984 have not yet reached a `;'. */
2985 if (nesting_depth
== 0)
2988 /* If it is the closing '}' for a block that we have
2989 scanned, stop -- but only after consuming the token.
2995 we will stop after the body of the erroneously declared
2996 function, but before consuming the following `typedef'
2998 if (--nesting_depth
== 0)
3000 cp_lexer_consume_token (parser
->lexer
);
3004 case CPP_OPEN_BRACE
:
3012 /* Consume the token. */
3013 cp_lexer_consume_token (parser
->lexer
);
3017 /* This function is called at the end of a statement or declaration.
3018 If the next token is a semicolon, it is consumed; otherwise, error
3019 recovery is attempted. */
3022 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3024 /* Look for the trailing `;'. */
3025 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3027 /* If there is additional (erroneous) input, skip to the end of
3029 cp_parser_skip_to_end_of_statement (parser
);
3030 /* If the next token is now a `;', consume it. */
3031 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3032 cp_lexer_consume_token (parser
->lexer
);
3036 /* Skip tokens until we have consumed an entire block, or until we
3037 have consumed a non-nested `;'. */
3040 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3042 int nesting_depth
= 0;
3044 while (nesting_depth
>= 0)
3046 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3048 switch (token
->type
)
3051 case CPP_PRAGMA_EOL
:
3052 /* If we've run out of tokens, stop. */
3056 /* Stop if this is an unnested ';'. */
3061 case CPP_CLOSE_BRACE
:
3062 /* Stop if this is an unnested '}', or closes the outermost
3065 if (nesting_depth
< 0)
3071 case CPP_OPEN_BRACE
:
3080 /* Consume the token. */
3081 cp_lexer_consume_token (parser
->lexer
);
3085 /* Skip tokens until a non-nested closing curly brace is the next
3086 token, or there are no more tokens. Return true in the first case,
3090 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3092 unsigned nesting_depth
= 0;
3096 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3098 switch (token
->type
)
3101 case CPP_PRAGMA_EOL
:
3102 /* If we've run out of tokens, stop. */
3105 case CPP_CLOSE_BRACE
:
3106 /* If the next token is a non-nested `}', then we have reached
3107 the end of the current block. */
3108 if (nesting_depth
-- == 0)
3112 case CPP_OPEN_BRACE
:
3113 /* If it the next token is a `{', then we are entering a new
3114 block. Consume the entire block. */
3122 /* Consume the token. */
3123 cp_lexer_consume_token (parser
->lexer
);
3127 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3128 parameter is the PRAGMA token, allowing us to purge the entire pragma
3132 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3136 parser
->lexer
->in_pragma
= false;
3139 token
= cp_lexer_consume_token (parser
->lexer
);
3140 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3142 /* Ensure that the pragma is not parsed again. */
3143 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3146 /* Require pragma end of line, resyncing with it as necessary. The
3147 arguments are as for cp_parser_skip_to_pragma_eol. */
3150 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3152 parser
->lexer
->in_pragma
= false;
3153 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3154 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3157 /* This is a simple wrapper around make_typename_type. When the id is
3158 an unresolved identifier node, we can provide a superior diagnostic
3159 using cp_parser_diagnose_invalid_type_name. */
3162 cp_parser_make_typename_type (cp_parser
*parser
, tree scope
,
3163 tree id
, location_t id_location
)
3166 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3168 result
= make_typename_type (scope
, id
, typename_type
,
3169 /*complain=*/tf_none
);
3170 if (result
== error_mark_node
)
3171 cp_parser_diagnose_invalid_type_name (parser
, scope
, id
, id_location
);
3174 return make_typename_type (scope
, id
, typename_type
, tf_error
);
3177 /* This is a wrapper around the
3178 make_{pointer,ptrmem,reference}_declarator functions that decides
3179 which one to call based on the CODE and CLASS_TYPE arguments. The
3180 CODE argument should be one of the values returned by
3181 cp_parser_ptr_operator. */
3182 static cp_declarator
*
3183 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3184 cp_cv_quals cv_qualifiers
,
3185 cp_declarator
*target
)
3187 if (code
== ERROR_MARK
)
3188 return cp_error_declarator
;
3190 if (code
== INDIRECT_REF
)
3191 if (class_type
== NULL_TREE
)
3192 return make_pointer_declarator (cv_qualifiers
, target
);
3194 return make_ptrmem_declarator (cv_qualifiers
, class_type
, target
);
3195 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3196 return make_reference_declarator (cv_qualifiers
, target
, false);
3197 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3198 return make_reference_declarator (cv_qualifiers
, target
, true);
3202 /* Create a new C++ parser. */
3205 cp_parser_new (void)
3211 /* cp_lexer_new_main is called before doing GC allocation because
3212 cp_lexer_new_main might load a PCH file. */
3213 lexer
= cp_lexer_new_main ();
3215 /* Initialize the binops_by_token so that we can get the tree
3216 directly from the token. */
3217 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3218 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3220 parser
= ggc_alloc_cleared_cp_parser ();
3221 parser
->lexer
= lexer
;
3222 parser
->context
= cp_parser_context_new (NULL
);
3224 /* For now, we always accept GNU extensions. */
3225 parser
->allow_gnu_extensions_p
= 1;
3227 /* The `>' token is a greater-than operator, not the end of a
3229 parser
->greater_than_is_operator_p
= true;
3231 parser
->default_arg_ok_p
= true;
3233 /* We are not parsing a constant-expression. */
3234 parser
->integral_constant_expression_p
= false;
3235 parser
->allow_non_integral_constant_expression_p
= false;
3236 parser
->non_integral_constant_expression_p
= false;
3238 /* Local variable names are not forbidden. */
3239 parser
->local_variables_forbidden_p
= false;
3241 /* We are not processing an `extern "C"' declaration. */
3242 parser
->in_unbraced_linkage_specification_p
= false;
3244 /* We are not processing a declarator. */
3245 parser
->in_declarator_p
= false;
3247 /* We are not processing a template-argument-list. */
3248 parser
->in_template_argument_list_p
= false;
3250 /* We are not in an iteration statement. */
3251 parser
->in_statement
= 0;
3253 /* We are not in a switch statement. */
3254 parser
->in_switch_statement_p
= false;
3256 /* We are not parsing a type-id inside an expression. */
3257 parser
->in_type_id_in_expr_p
= false;
3259 /* Declarations aren't implicitly extern "C". */
3260 parser
->implicit_extern_c
= false;
3262 /* String literals should be translated to the execution character set. */
3263 parser
->translate_strings_p
= true;
3265 /* We are not parsing a function body. */
3266 parser
->in_function_body
= false;
3268 /* We can correct until told otherwise. */
3269 parser
->colon_corrects_to_scope_p
= true;
3271 /* The unparsed function queue is empty. */
3272 push_unparsed_function_queues (parser
);
3274 /* There are no classes being defined. */
3275 parser
->num_classes_being_defined
= 0;
3277 /* No template parameters apply. */
3278 parser
->num_template_parameter_lists
= 0;
3283 /* Create a cp_lexer structure which will emit the tokens in CACHE
3284 and push it onto the parser's lexer stack. This is used for delayed
3285 parsing of in-class method bodies and default arguments, and should
3286 not be confused with tentative parsing. */
3288 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3290 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3291 lexer
->next
= parser
->lexer
;
3292 parser
->lexer
= lexer
;
3294 /* Move the current source position to that of the first token in the
3296 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3299 /* Pop the top lexer off the parser stack. This is never used for the
3300 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3302 cp_parser_pop_lexer (cp_parser
*parser
)
3304 cp_lexer
*lexer
= parser
->lexer
;
3305 parser
->lexer
= lexer
->next
;
3306 cp_lexer_destroy (lexer
);
3308 /* Put the current source position back where it was before this
3309 lexer was pushed. */
3310 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3313 /* Lexical conventions [gram.lex] */
3315 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3319 cp_parser_identifier (cp_parser
* parser
)
3323 /* Look for the identifier. */
3324 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3325 /* Return the value. */
3326 return token
? token
->u
.value
: error_mark_node
;
3329 /* Parse a sequence of adjacent string constants. Returns a
3330 TREE_STRING representing the combined, nul-terminated string
3331 constant. If TRANSLATE is true, translate the string to the
3332 execution character set. If WIDE_OK is true, a wide string is
3335 C++98 [lex.string] says that if a narrow string literal token is
3336 adjacent to a wide string literal token, the behavior is undefined.
3337 However, C99 6.4.5p4 says that this results in a wide string literal.
3338 We follow C99 here, for consistency with the C front end.
3340 This code is largely lifted from lex_string() in c-lex.c.
3342 FUTURE: ObjC++ will need to handle @-strings here. */
3344 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
)
3348 struct obstack str_ob
;
3349 cpp_string str
, istr
, *strs
;
3351 enum cpp_ttype type
, curr_type
;
3352 int have_suffix_p
= 0;
3354 tree suffix_id
= NULL_TREE
;
3355 bool curr_tok_is_userdef_p
= false;
3357 tok
= cp_lexer_peek_token (parser
->lexer
);
3358 if (!cp_parser_is_string_literal (tok
))
3360 cp_parser_error (parser
, "expected string-literal");
3361 return error_mark_node
;
3364 if (cpp_userdef_string_p (tok
->type
))
3366 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3367 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3368 curr_tok_is_userdef_p
= true;
3372 string_tree
= tok
->u
.value
;
3373 curr_type
= tok
->type
;
3377 /* Try to avoid the overhead of creating and destroying an obstack
3378 for the common case of just one string. */
3379 if (!cp_parser_is_string_literal
3380 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3382 cp_lexer_consume_token (parser
->lexer
);
3384 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3385 str
.len
= TREE_STRING_LENGTH (string_tree
);
3388 if (curr_tok_is_userdef_p
)
3390 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3392 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3395 curr_type
= tok
->type
;
3401 gcc_obstack_init (&str_ob
);
3406 cp_lexer_consume_token (parser
->lexer
);
3408 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3409 str
.len
= TREE_STRING_LENGTH (string_tree
);
3411 if (curr_tok_is_userdef_p
)
3413 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3414 if (have_suffix_p
== 0)
3416 suffix_id
= curr_suffix_id
;
3419 else if (have_suffix_p
== 1
3420 && curr_suffix_id
!= suffix_id
)
3422 error ("inconsistent user-defined literal suffixes"
3423 " %qD and %qD in string literal",
3424 suffix_id
, curr_suffix_id
);
3427 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3430 curr_type
= tok
->type
;
3432 if (type
!= curr_type
)
3434 if (type
== CPP_STRING
)
3436 else if (curr_type
!= CPP_STRING
)
3437 error_at (tok
->location
,
3438 "unsupported non-standard concatenation "
3439 "of string literals");
3442 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3444 tok
= cp_lexer_peek_token (parser
->lexer
);
3445 if (cpp_userdef_string_p (tok
->type
))
3447 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3448 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3449 curr_tok_is_userdef_p
= true;
3453 string_tree
= tok
->u
.value
;
3454 curr_type
= tok
->type
;
3455 curr_tok_is_userdef_p
= false;
3458 while (cp_parser_is_string_literal (tok
));
3460 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3463 if (type
!= CPP_STRING
&& !wide_ok
)
3465 cp_parser_error (parser
, "a wide string is invalid in this context");
3469 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3470 (parse_in
, strs
, count
, &istr
, type
))
3472 value
= build_string (istr
.len
, (const char *)istr
.text
);
3473 free (CONST_CAST (unsigned char *, istr
.text
));
3479 case CPP_UTF8STRING
:
3480 TREE_TYPE (value
) = char_array_type_node
;
3483 TREE_TYPE (value
) = char16_array_type_node
;
3486 TREE_TYPE (value
) = char32_array_type_node
;
3489 TREE_TYPE (value
) = wchar_array_type_node
;
3493 value
= fix_string_type (value
);
3497 tree literal
= build_userdef_literal (suffix_id
, value
, NULL_TREE
);
3498 tok
->u
.value
= literal
;
3499 return cp_parser_userdef_string_literal (tok
);
3503 /* cpp_interpret_string has issued an error. */
3504 value
= error_mark_node
;
3507 obstack_free (&str_ob
, 0);
3512 /* Look up a literal operator with the name and the exact arguments. */
3515 lookup_literal_operator (tree name
, VEC(tree
,gc
) *args
)
3518 decl
= lookup_name (name
);
3519 if (!decl
|| !is_overloaded_fn (decl
))
3520 return error_mark_node
;
3522 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3526 tree fn
= OVL_CURRENT (fns
);
3527 tree argtypes
= NULL_TREE
;
3528 argtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3529 if (argtypes
!= NULL_TREE
)
3531 for (ix
= 0; ix
< VEC_length (tree
, args
) && argtypes
!= NULL_TREE
;
3532 ++ix
, argtypes
= TREE_CHAIN (argtypes
))
3534 tree targ
= TREE_VALUE (argtypes
);
3535 tree tparm
= TREE_TYPE (VEC_index (tree
, args
, ix
));
3536 bool ptr
= TREE_CODE (targ
) == POINTER_TYPE
;
3537 bool arr
= TREE_CODE (tparm
) == ARRAY_TYPE
;
3538 if ((ptr
|| arr
|| !same_type_p (targ
, tparm
))
3540 || !same_type_p (TREE_TYPE (targ
),
3541 TREE_TYPE (tparm
))))
3545 && ix
== VEC_length (tree
, args
)
3546 /* May be this should be sufficient_parms_p instead,
3547 depending on how exactly should user-defined literals
3548 work in presence of default arguments on the literal
3549 operator parameters. */
3550 && argtypes
== void_list_node
)
3555 return error_mark_node
;
3558 /* Parse a user-defined char constant. Returns a call to a user-defined
3559 literal operator taking the character as an argument. */
3562 cp_parser_userdef_char_literal (cp_parser
*parser
)
3564 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3565 tree literal
= token
->u
.value
;
3566 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3567 tree value
= USERDEF_LITERAL_VALUE (literal
);
3568 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3571 /* Build up a call to the user-defined operator */
3572 /* Lookup the name we got back from the id-expression. */
3573 VEC(tree
,gc
) *args
= make_tree_vector ();
3574 VEC_safe_push (tree
, gc
, args
, value
);
3575 decl
= lookup_literal_operator (name
, args
);
3576 if (!decl
|| decl
== error_mark_node
)
3578 error ("unable to find character literal operator %qD with %qT argument",
3579 name
, TREE_TYPE (value
));
3580 release_tree_vector (args
);
3581 return error_mark_node
;
3583 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3584 release_tree_vector (args
);
3585 if (result
!= error_mark_node
)
3588 error ("unable to find character literal operator %qD with %qT argument",
3589 name
, TREE_TYPE (value
));
3590 return error_mark_node
;
3593 /* A subroutine of cp_parser_userdef_numeric_literal to
3594 create a char... template parameter pack from a string node. */
3597 make_char_string_pack (tree value
)
3600 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3601 const char *str
= TREE_STRING_POINTER (value
);
3602 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3603 tree argvec
= make_tree_vec (1);
3605 /* Fill in CHARVEC with all of the parameters. */
3606 charvec
= make_tree_vec (len
);
3607 for (i
= 0; i
< len
; ++i
)
3608 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3610 /* Build the argument packs. */
3611 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3612 TREE_TYPE (argpack
) = char_type_node
;
3614 TREE_VEC_ELT (argvec
, 0) = argpack
;
3619 /* Parse a user-defined numeric constant. returns a call to a user-defined
3620 literal operator. */
3623 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3625 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3626 tree literal
= token
->u
.value
;
3627 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3628 tree value
= USERDEF_LITERAL_VALUE (literal
);
3629 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3630 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3634 /* Look for a literal operator taking the exact type of numeric argument
3635 as the literal value. */
3636 args
= make_tree_vector ();
3637 VEC_safe_push (tree
, gc
, args
, value
);
3638 decl
= lookup_literal_operator (name
, args
);
3639 if (decl
&& decl
!= error_mark_node
)
3641 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3642 if (result
!= error_mark_node
)
3644 release_tree_vector (args
);
3648 release_tree_vector (args
);
3650 /* If the numeric argument didn't work, look for a raw literal
3651 operator taking a const char* argument consisting of the number
3652 in string format. */
3653 args
= make_tree_vector ();
3654 VEC_safe_push (tree
, gc
, args
, num_string
);
3655 decl
= lookup_literal_operator (name
, args
);
3656 if (decl
&& decl
!= error_mark_node
)
3658 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3659 if (result
!= error_mark_node
)
3661 release_tree_vector (args
);
3665 release_tree_vector (args
);
3667 /* If the raw literal didn't work, look for a non-type template
3668 function with parameter pack char.... Call the function with
3669 template parameter characters representing the number. */
3670 args
= make_tree_vector ();
3671 decl
= lookup_literal_operator (name
, args
);
3672 if (decl
&& decl
!= error_mark_node
)
3674 tree tmpl_args
= make_char_string_pack (num_string
);
3675 decl
= lookup_template_function (decl
, tmpl_args
);
3676 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3677 if (result
!= error_mark_node
)
3679 release_tree_vector (args
);
3683 release_tree_vector (args
);
3685 error ("unable to find numeric literal operator %qD", name
);
3686 return error_mark_node
;
3689 /* Parse a user-defined string constant. Returns a call to a user-defined
3690 literal operator taking a character pointer and the length of the string
3694 cp_parser_userdef_string_literal (cp_token
*token
)
3696 tree literal
= token
->u
.value
;
3697 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3698 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3699 tree value
= USERDEF_LITERAL_VALUE (literal
);
3700 int len
= TREE_STRING_LENGTH (value
)
3701 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
3704 /* Build up a call to the user-defined operator */
3705 /* Lookup the name we got back from the id-expression. */
3706 VEC(tree
,gc
) *args
= make_tree_vector ();
3707 VEC_safe_push (tree
, gc
, args
, value
);
3708 VEC_safe_push (tree
, gc
, args
, build_int_cst (size_type_node
, len
));
3709 decl
= lookup_name (name
);
3710 if (!decl
|| decl
== error_mark_node
)
3712 error ("unable to find string literal operator %qD", name
);
3713 release_tree_vector (args
);
3714 return error_mark_node
;
3716 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3717 release_tree_vector (args
);
3718 if (result
!= error_mark_node
)
3721 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3722 name
, TREE_TYPE (value
), size_type_node
);
3723 return error_mark_node
;
3727 /* Basic concepts [gram.basic] */
3729 /* Parse a translation-unit.
3732 declaration-seq [opt]
3734 Returns TRUE if all went well. */
3737 cp_parser_translation_unit (cp_parser
* parser
)
3739 /* The address of the first non-permanent object on the declarator
3741 static void *declarator_obstack_base
;
3745 /* Create the declarator obstack, if necessary. */
3746 if (!cp_error_declarator
)
3748 gcc_obstack_init (&declarator_obstack
);
3749 /* Create the error declarator. */
3750 cp_error_declarator
= make_declarator (cdk_error
);
3751 /* Create the empty parameter list. */
3752 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
3753 /* Remember where the base of the declarator obstack lies. */
3754 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
3757 cp_parser_declaration_seq_opt (parser
);
3759 /* If there are no tokens left then all went well. */
3760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
3762 /* Get rid of the token array; we don't need it any more. */
3763 cp_lexer_destroy (parser
->lexer
);
3764 parser
->lexer
= NULL
;
3766 /* This file might have been a context that's implicitly extern
3767 "C". If so, pop the lang context. (Only relevant for PCH.) */
3768 if (parser
->implicit_extern_c
)
3770 pop_lang_context ();
3771 parser
->implicit_extern_c
= false;
3775 finish_translation_unit ();
3781 cp_parser_error (parser
, "expected declaration");
3785 /* Make sure the declarator obstack was fully cleaned up. */
3786 gcc_assert (obstack_next_free (&declarator_obstack
)
3787 == declarator_obstack_base
);
3789 /* All went well. */
3793 /* Expressions [gram.expr] */
3795 /* Parse a primary-expression.
3806 ( compound-statement )
3807 __builtin_va_arg ( assignment-expression , type-id )
3808 __builtin_offsetof ( type-id , offsetof-expression )
3811 __has_nothrow_assign ( type-id )
3812 __has_nothrow_constructor ( type-id )
3813 __has_nothrow_copy ( type-id )
3814 __has_trivial_assign ( type-id )
3815 __has_trivial_constructor ( type-id )
3816 __has_trivial_copy ( type-id )
3817 __has_trivial_destructor ( type-id )
3818 __has_virtual_destructor ( type-id )
3819 __is_abstract ( type-id )
3820 __is_base_of ( type-id , type-id )
3821 __is_class ( type-id )
3822 __is_convertible_to ( type-id , type-id )
3823 __is_empty ( type-id )
3824 __is_enum ( type-id )
3825 __is_final ( type-id )
3826 __is_literal_type ( type-id )
3827 __is_pod ( type-id )
3828 __is_polymorphic ( type-id )
3829 __is_std_layout ( type-id )
3830 __is_trivial ( type-id )
3831 __is_union ( type-id )
3833 Objective-C++ Extension:
3841 ADDRESS_P is true iff this expression was immediately preceded by
3842 "&" and therefore might denote a pointer-to-member. CAST_P is true
3843 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3844 true iff this expression is a template argument.
3846 Returns a representation of the expression. Upon return, *IDK
3847 indicates what kind of id-expression (if any) was present. */
3850 cp_parser_primary_expression (cp_parser
*parser
,
3853 bool template_arg_p
,
3856 cp_token
*token
= NULL
;
3858 /* Assume the primary expression is not an id-expression. */
3859 *idk
= CP_ID_KIND_NONE
;
3861 /* Peek at the next token. */
3862 token
= cp_lexer_peek_token (parser
->lexer
);
3863 switch (token
->type
)
3872 user-defined-literal */
3878 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
3879 return cp_parser_userdef_numeric_literal (parser
);
3880 token
= cp_lexer_consume_token (parser
->lexer
);
3881 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
3883 error_at (token
->location
,
3884 "fixed-point types not supported in C++");
3885 return error_mark_node
;
3887 /* Floating-point literals are only allowed in an integral
3888 constant expression if they are cast to an integral or
3889 enumeration type. */
3890 if (TREE_CODE (token
->u
.value
) == REAL_CST
3891 && parser
->integral_constant_expression_p
3894 /* CAST_P will be set even in invalid code like "int(2.7 +
3895 ...)". Therefore, we have to check that the next token
3896 is sure to end the cast. */
3899 cp_token
*next_token
;
3901 next_token
= cp_lexer_peek_token (parser
->lexer
);
3902 if (/* The comma at the end of an
3903 enumerator-definition. */
3904 next_token
->type
!= CPP_COMMA
3905 /* The curly brace at the end of an enum-specifier. */
3906 && next_token
->type
!= CPP_CLOSE_BRACE
3907 /* The end of a statement. */
3908 && next_token
->type
!= CPP_SEMICOLON
3909 /* The end of the cast-expression. */
3910 && next_token
->type
!= CPP_CLOSE_PAREN
3911 /* The end of an array bound. */
3912 && next_token
->type
!= CPP_CLOSE_SQUARE
3913 /* The closing ">" in a template-argument-list. */
3914 && (next_token
->type
!= CPP_GREATER
3915 || parser
->greater_than_is_operator_p
)
3916 /* C++0x only: A ">>" treated like two ">" tokens,
3917 in a template-argument-list. */
3918 && (next_token
->type
!= CPP_RSHIFT
3919 || (cxx_dialect
== cxx98
)
3920 || parser
->greater_than_is_operator_p
))
3924 /* If we are within a cast, then the constraint that the
3925 cast is to an integral or enumeration type will be
3926 checked at that point. If we are not within a cast, then
3927 this code is invalid. */
3929 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
3931 return token
->u
.value
;
3933 case CPP_CHAR_USERDEF
:
3934 case CPP_CHAR16_USERDEF
:
3935 case CPP_CHAR32_USERDEF
:
3936 case CPP_WCHAR_USERDEF
:
3937 return cp_parser_userdef_char_literal (parser
);
3943 case CPP_UTF8STRING
:
3944 case CPP_STRING_USERDEF
:
3945 case CPP_STRING16_USERDEF
:
3946 case CPP_STRING32_USERDEF
:
3947 case CPP_WSTRING_USERDEF
:
3948 case CPP_UTF8STRING_USERDEF
:
3949 /* ??? Should wide strings be allowed when parser->translate_strings_p
3950 is false (i.e. in attributes)? If not, we can kill the third
3951 argument to cp_parser_string_literal. */
3952 return cp_parser_string_literal (parser
,
3953 parser
->translate_strings_p
,
3956 case CPP_OPEN_PAREN
:
3959 bool saved_greater_than_is_operator_p
;
3961 /* Consume the `('. */
3962 cp_lexer_consume_token (parser
->lexer
);
3963 /* Within a parenthesized expression, a `>' token is always
3964 the greater-than operator. */
3965 saved_greater_than_is_operator_p
3966 = parser
->greater_than_is_operator_p
;
3967 parser
->greater_than_is_operator_p
= true;
3968 /* If we see `( { ' then we are looking at the beginning of
3969 a GNU statement-expression. */
3970 if (cp_parser_allow_gnu_extensions_p (parser
)
3971 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
3973 /* Statement-expressions are not allowed by the standard. */
3974 pedwarn (token
->location
, OPT_Wpedantic
,
3975 "ISO C++ forbids braced-groups within expressions");
3977 /* And they're not allowed outside of a function-body; you
3978 cannot, for example, write:
3980 int i = ({ int j = 3; j + 1; });
3982 at class or namespace scope. */
3983 if (!parser
->in_function_body
3984 || parser
->in_template_argument_list_p
)
3986 error_at (token
->location
,
3987 "statement-expressions are not allowed outside "
3988 "functions nor in template-argument lists");
3989 cp_parser_skip_to_end_of_block_or_statement (parser
);
3990 expr
= error_mark_node
;
3994 /* Start the statement-expression. */
3995 expr
= begin_stmt_expr ();
3996 /* Parse the compound-statement. */
3997 cp_parser_compound_statement (parser
, expr
, false, false);
3999 expr
= finish_stmt_expr (expr
, false);
4004 /* Parse the parenthesized expression. */
4005 expr
= cp_parser_expression (parser
, cast_p
, idk
);
4006 /* Let the front end know that this expression was
4007 enclosed in parentheses. This matters in case, for
4008 example, the expression is of the form `A::B', since
4009 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4011 finish_parenthesized_expr (expr
);
4012 /* DR 705: Wrapping an unqualified name in parentheses
4013 suppresses arg-dependent lookup. We want to pass back
4014 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4015 (c++/37862), but none of the others. */
4016 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4017 *idk
= CP_ID_KIND_NONE
;
4019 /* The `>' token might be the end of a template-id or
4020 template-parameter-list now. */
4021 parser
->greater_than_is_operator_p
4022 = saved_greater_than_is_operator_p
;
4023 /* Consume the `)'. */
4024 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4025 cp_parser_skip_to_end_of_statement (parser
);
4030 case CPP_OPEN_SQUARE
:
4031 if (c_dialect_objc ())
4032 /* We have an Objective-C++ message. */
4033 return cp_parser_objc_expression (parser
);
4035 tree lam
= cp_parser_lambda_expression (parser
);
4036 /* Don't warn about a failed tentative parse. */
4037 if (cp_parser_error_occurred (parser
))
4038 return error_mark_node
;
4039 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4043 case CPP_OBJC_STRING
:
4044 if (c_dialect_objc ())
4045 /* We have an Objective-C++ string literal. */
4046 return cp_parser_objc_expression (parser
);
4047 cp_parser_error (parser
, "expected primary-expression");
4048 return error_mark_node
;
4051 switch (token
->keyword
)
4053 /* These two are the boolean literals. */
4055 cp_lexer_consume_token (parser
->lexer
);
4056 return boolean_true_node
;
4058 cp_lexer_consume_token (parser
->lexer
);
4059 return boolean_false_node
;
4061 /* The `__null' literal. */
4063 cp_lexer_consume_token (parser
->lexer
);
4066 /* The `nullptr' literal. */
4068 cp_lexer_consume_token (parser
->lexer
);
4069 return nullptr_node
;
4071 /* Recognize the `this' keyword. */
4073 cp_lexer_consume_token (parser
->lexer
);
4074 if (parser
->local_variables_forbidden_p
)
4076 error_at (token
->location
,
4077 "%<this%> may not be used in this context");
4078 return error_mark_node
;
4080 /* Pointers cannot appear in constant-expressions. */
4081 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4082 return error_mark_node
;
4083 return finish_this_expr ();
4085 /* The `operator' keyword can be the beginning of an
4090 case RID_FUNCTION_NAME
:
4091 case RID_PRETTY_FUNCTION_NAME
:
4092 case RID_C99_FUNCTION_NAME
:
4094 non_integral_constant name
;
4096 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4097 __func__ are the names of variables -- but they are
4098 treated specially. Therefore, they are handled here,
4099 rather than relying on the generic id-expression logic
4100 below. Grammatically, these names are id-expressions.
4102 Consume the token. */
4103 token
= cp_lexer_consume_token (parser
->lexer
);
4105 switch (token
->keyword
)
4107 case RID_FUNCTION_NAME
:
4108 name
= NIC_FUNC_NAME
;
4110 case RID_PRETTY_FUNCTION_NAME
:
4111 name
= NIC_PRETTY_FUNC
;
4113 case RID_C99_FUNCTION_NAME
:
4114 name
= NIC_C99_FUNC
;
4120 if (cp_parser_non_integral_constant_expression (parser
, name
))
4121 return error_mark_node
;
4123 /* Look up the name. */
4124 return finish_fname (token
->u
.value
);
4131 source_location type_location
;
4133 /* The `__builtin_va_arg' construct is used to handle
4134 `va_arg'. Consume the `__builtin_va_arg' token. */
4135 cp_lexer_consume_token (parser
->lexer
);
4136 /* Look for the opening `('. */
4137 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4138 /* Now, parse the assignment-expression. */
4139 expression
= cp_parser_assignment_expression (parser
,
4140 /*cast_p=*/false, NULL
);
4141 /* Look for the `,'. */
4142 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4143 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4144 /* Parse the type-id. */
4145 type
= cp_parser_type_id (parser
);
4146 /* Look for the closing `)'. */
4147 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4148 /* Using `va_arg' in a constant-expression is not
4150 if (cp_parser_non_integral_constant_expression (parser
,
4152 return error_mark_node
;
4153 return build_x_va_arg (type_location
, expression
, type
);
4157 return cp_parser_builtin_offsetof (parser
);
4159 case RID_HAS_NOTHROW_ASSIGN
:
4160 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4161 case RID_HAS_NOTHROW_COPY
:
4162 case RID_HAS_TRIVIAL_ASSIGN
:
4163 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4164 case RID_HAS_TRIVIAL_COPY
:
4165 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4166 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4167 case RID_IS_ABSTRACT
:
4168 case RID_IS_BASE_OF
:
4170 case RID_IS_CONVERTIBLE_TO
:
4174 case RID_IS_LITERAL_TYPE
:
4176 case RID_IS_POLYMORPHIC
:
4177 case RID_IS_STD_LAYOUT
:
4178 case RID_IS_TRIVIAL
:
4180 return cp_parser_trait_expr (parser
, token
->keyword
);
4182 /* Objective-C++ expressions. */
4184 case RID_AT_PROTOCOL
:
4185 case RID_AT_SELECTOR
:
4186 return cp_parser_objc_expression (parser
);
4189 if (parser
->in_function_body
4190 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4193 error_at (token
->location
,
4194 "a template declaration cannot appear at block scope");
4195 cp_parser_skip_to_end_of_block_or_statement (parser
);
4196 return error_mark_node
;
4199 cp_parser_error (parser
, "expected primary-expression");
4200 return error_mark_node
;
4203 /* An id-expression can start with either an identifier, a
4204 `::' as the beginning of a qualified-id, or the "operator"
4208 case CPP_TEMPLATE_ID
:
4209 case CPP_NESTED_NAME_SPECIFIER
:
4213 const char *error_msg
;
4216 cp_token
*id_expr_token
;
4219 /* Parse the id-expression. */
4221 = cp_parser_id_expression (parser
,
4222 /*template_keyword_p=*/false,
4223 /*check_dependency_p=*/true,
4225 /*declarator_p=*/false,
4226 /*optional_p=*/false);
4227 if (id_expression
== error_mark_node
)
4228 return error_mark_node
;
4229 id_expr_token
= token
;
4230 token
= cp_lexer_peek_token (parser
->lexer
);
4231 done
= (token
->type
!= CPP_OPEN_SQUARE
4232 && token
->type
!= CPP_OPEN_PAREN
4233 && token
->type
!= CPP_DOT
4234 && token
->type
!= CPP_DEREF
4235 && token
->type
!= CPP_PLUS_PLUS
4236 && token
->type
!= CPP_MINUS_MINUS
);
4237 /* If we have a template-id, then no further lookup is
4238 required. If the template-id was for a template-class, we
4239 will sometimes have a TYPE_DECL at this point. */
4240 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4241 || TREE_CODE (id_expression
) == TYPE_DECL
)
4242 decl
= id_expression
;
4243 /* Look up the name. */
4246 tree ambiguous_decls
;
4248 /* If we already know that this lookup is ambiguous, then
4249 we've already issued an error message; there's no reason
4251 if (id_expr_token
->type
== CPP_NAME
4252 && id_expr_token
->ambiguous_p
)
4254 cp_parser_simulate_error (parser
);
4255 return error_mark_node
;
4258 decl
= cp_parser_lookup_name (parser
, id_expression
,
4261 /*is_namespace=*/false,
4262 /*check_dependency=*/true,
4264 id_expr_token
->location
);
4265 /* If the lookup was ambiguous, an error will already have
4267 if (ambiguous_decls
)
4268 return error_mark_node
;
4270 /* In Objective-C++, we may have an Objective-C 2.0
4271 dot-syntax for classes here. */
4272 if (c_dialect_objc ()
4273 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4274 && TREE_CODE (decl
) == TYPE_DECL
4275 && objc_is_class_name (decl
))
4278 cp_lexer_consume_token (parser
->lexer
);
4279 component
= cp_parser_identifier (parser
);
4280 if (component
== error_mark_node
)
4281 return error_mark_node
;
4283 return objc_build_class_component_ref (id_expression
, component
);
4286 /* In Objective-C++, an instance variable (ivar) may be preferred
4287 to whatever cp_parser_lookup_name() found. */
4288 decl
= objc_lookup_ivar (decl
, id_expression
);
4290 /* If name lookup gives us a SCOPE_REF, then the
4291 qualifying scope was dependent. */
4292 if (TREE_CODE (decl
) == SCOPE_REF
)
4294 /* At this point, we do not know if DECL is a valid
4295 integral constant expression. We assume that it is
4296 in fact such an expression, so that code like:
4298 template <int N> struct A {
4302 is accepted. At template-instantiation time, we
4303 will check that B<N>::i is actually a constant. */
4306 /* Check to see if DECL is a local variable in a context
4307 where that is forbidden. */
4308 if (parser
->local_variables_forbidden_p
4309 && local_variable_p (decl
))
4311 /* It might be that we only found DECL because we are
4312 trying to be generous with pre-ISO scoping rules.
4313 For example, consider:
4317 for (int i = 0; i < 10; ++i) {}
4318 extern void f(int j = i);
4321 Here, name look up will originally find the out
4322 of scope `i'. We need to issue a warning message,
4323 but then use the global `i'. */
4324 decl
= check_for_out_of_scope_variable (decl
);
4325 if (local_variable_p (decl
))
4327 error_at (id_expr_token
->location
,
4328 "local variable %qD may not appear in this context",
4330 return error_mark_node
;
4335 decl
= (finish_id_expression
4336 (id_expression
, decl
, parser
->scope
,
4338 parser
->integral_constant_expression_p
,
4339 parser
->allow_non_integral_constant_expression_p
,
4340 &parser
->non_integral_constant_expression_p
,
4341 template_p
, done
, address_p
,
4344 id_expr_token
->location
));
4346 cp_parser_error (parser
, error_msg
);
4350 /* Anything else is an error. */
4352 cp_parser_error (parser
, "expected primary-expression");
4353 return error_mark_node
;
4357 /* Parse an id-expression.
4364 :: [opt] nested-name-specifier template [opt] unqualified-id
4366 :: operator-function-id
4369 Return a representation of the unqualified portion of the
4370 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4371 a `::' or nested-name-specifier.
4373 Often, if the id-expression was a qualified-id, the caller will
4374 want to make a SCOPE_REF to represent the qualified-id. This
4375 function does not do this in order to avoid wastefully creating
4376 SCOPE_REFs when they are not required.
4378 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4381 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4382 uninstantiated templates.
4384 If *TEMPLATE_P is non-NULL, it is set to true iff the
4385 `template' keyword is used to explicitly indicate that the entity
4386 named is a template.
4388 If DECLARATOR_P is true, the id-expression is appearing as part of
4389 a declarator, rather than as part of an expression. */
4392 cp_parser_id_expression (cp_parser
*parser
,
4393 bool template_keyword_p
,
4394 bool check_dependency_p
,
4399 bool global_scope_p
;
4400 bool nested_name_specifier_p
;
4402 /* Assume the `template' keyword was not used. */
4404 *template_p
= template_keyword_p
;
4406 /* Look for the optional `::' operator. */
4408 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4410 /* Look for the optional nested-name-specifier. */
4411 nested_name_specifier_p
4412 = (cp_parser_nested_name_specifier_opt (parser
,
4413 /*typename_keyword_p=*/false,
4418 /* If there is a nested-name-specifier, then we are looking at
4419 the first qualified-id production. */
4420 if (nested_name_specifier_p
)
4423 tree saved_object_scope
;
4424 tree saved_qualifying_scope
;
4425 tree unqualified_id
;
4428 /* See if the next token is the `template' keyword. */
4430 template_p
= &is_template
;
4431 *template_p
= cp_parser_optional_template_keyword (parser
);
4432 /* Name lookup we do during the processing of the
4433 unqualified-id might obliterate SCOPE. */
4434 saved_scope
= parser
->scope
;
4435 saved_object_scope
= parser
->object_scope
;
4436 saved_qualifying_scope
= parser
->qualifying_scope
;
4437 /* Process the final unqualified-id. */
4438 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4441 /*optional_p=*/false);
4442 /* Restore the SAVED_SCOPE for our caller. */
4443 parser
->scope
= saved_scope
;
4444 parser
->object_scope
= saved_object_scope
;
4445 parser
->qualifying_scope
= saved_qualifying_scope
;
4447 return unqualified_id
;
4449 /* Otherwise, if we are in global scope, then we are looking at one
4450 of the other qualified-id productions. */
4451 else if (global_scope_p
)
4456 /* Peek at the next token. */
4457 token
= cp_lexer_peek_token (parser
->lexer
);
4459 /* If it's an identifier, and the next token is not a "<", then
4460 we can avoid the template-id case. This is an optimization
4461 for this common case. */
4462 if (token
->type
== CPP_NAME
4463 && !cp_parser_nth_token_starts_template_argument_list_p
4465 return cp_parser_identifier (parser
);
4467 cp_parser_parse_tentatively (parser
);
4468 /* Try a template-id. */
4469 id
= cp_parser_template_id (parser
,
4470 /*template_keyword_p=*/false,
4471 /*check_dependency_p=*/true,
4474 /* If that worked, we're done. */
4475 if (cp_parser_parse_definitely (parser
))
4478 /* Peek at the next token. (Changes in the token buffer may
4479 have invalidated the pointer obtained above.) */
4480 token
= cp_lexer_peek_token (parser
->lexer
);
4482 switch (token
->type
)
4485 return cp_parser_identifier (parser
);
4488 if (token
->keyword
== RID_OPERATOR
)
4489 return cp_parser_operator_function_id (parser
);
4493 cp_parser_error (parser
, "expected id-expression");
4494 return error_mark_node
;
4498 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4499 /*check_dependency_p=*/true,
4504 /* Parse an unqualified-id.
4508 operator-function-id
4509 conversion-function-id
4513 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4514 keyword, in a construct like `A::template ...'.
4516 Returns a representation of unqualified-id. For the `identifier'
4517 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4518 production a BIT_NOT_EXPR is returned; the operand of the
4519 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4520 other productions, see the documentation accompanying the
4521 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4522 names are looked up in uninstantiated templates. If DECLARATOR_P
4523 is true, the unqualified-id is appearing as part of a declarator,
4524 rather than as part of an expression. */
4527 cp_parser_unqualified_id (cp_parser
* parser
,
4528 bool template_keyword_p
,
4529 bool check_dependency_p
,
4535 /* Peek at the next token. */
4536 token
= cp_lexer_peek_token (parser
->lexer
);
4538 switch (token
->type
)
4544 /* We don't know yet whether or not this will be a
4546 cp_parser_parse_tentatively (parser
);
4547 /* Try a template-id. */
4548 id
= cp_parser_template_id (parser
, template_keyword_p
,
4552 /* If it worked, we're done. */
4553 if (cp_parser_parse_definitely (parser
))
4555 /* Otherwise, it's an ordinary identifier. */
4556 return cp_parser_identifier (parser
);
4559 case CPP_TEMPLATE_ID
:
4560 return cp_parser_template_id (parser
, template_keyword_p
,
4568 tree qualifying_scope
;
4573 /* Consume the `~' token. */
4574 cp_lexer_consume_token (parser
->lexer
);
4575 /* Parse the class-name. The standard, as written, seems to
4578 template <typename T> struct S { ~S (); };
4579 template <typename T> S<T>::~S() {}
4581 is invalid, since `~' must be followed by a class-name, but
4582 `S<T>' is dependent, and so not known to be a class.
4583 That's not right; we need to look in uninstantiated
4584 templates. A further complication arises from:
4586 template <typename T> void f(T t) {
4590 Here, it is not possible to look up `T' in the scope of `T'
4591 itself. We must look in both the current scope, and the
4592 scope of the containing complete expression.
4594 Yet another issue is:
4603 The standard does not seem to say that the `S' in `~S'
4604 should refer to the type `S' and not the data member
4607 /* DR 244 says that we look up the name after the "~" in the
4608 same scope as we looked up the qualifying name. That idea
4609 isn't fully worked out; it's more complicated than that. */
4610 scope
= parser
->scope
;
4611 object_scope
= parser
->object_scope
;
4612 qualifying_scope
= parser
->qualifying_scope
;
4614 /* Check for invalid scopes. */
4615 if (scope
== error_mark_node
)
4617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4618 cp_lexer_consume_token (parser
->lexer
);
4619 return error_mark_node
;
4621 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
4623 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4624 error_at (token
->location
,
4625 "scope %qT before %<~%> is not a class-name",
4627 cp_parser_simulate_error (parser
);
4628 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
4629 cp_lexer_consume_token (parser
->lexer
);
4630 return error_mark_node
;
4632 gcc_assert (!scope
|| TYPE_P (scope
));
4634 /* If the name is of the form "X::~X" it's OK even if X is a
4636 token
= cp_lexer_peek_token (parser
->lexer
);
4638 && token
->type
== CPP_NAME
4639 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4641 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
4642 || (CLASS_TYPE_P (scope
)
4643 && constructor_name_p (token
->u
.value
, scope
))))
4645 cp_lexer_consume_token (parser
->lexer
);
4646 return build_nt (BIT_NOT_EXPR
, scope
);
4649 /* If there was an explicit qualification (S::~T), first look
4650 in the scope given by the qualification (i.e., S).
4652 Note: in the calls to cp_parser_class_name below we pass
4653 typename_type so that lookup finds the injected-class-name
4654 rather than the constructor. */
4656 type_decl
= NULL_TREE
;
4659 cp_parser_parse_tentatively (parser
);
4660 type_decl
= cp_parser_class_name (parser
,
4661 /*typename_keyword_p=*/false,
4662 /*template_keyword_p=*/false,
4664 /*check_dependency=*/false,
4665 /*class_head_p=*/false,
4667 if (cp_parser_parse_definitely (parser
))
4670 /* In "N::S::~S", look in "N" as well. */
4671 if (!done
&& scope
&& qualifying_scope
)
4673 cp_parser_parse_tentatively (parser
);
4674 parser
->scope
= qualifying_scope
;
4675 parser
->object_scope
= NULL_TREE
;
4676 parser
->qualifying_scope
= NULL_TREE
;
4678 = cp_parser_class_name (parser
,
4679 /*typename_keyword_p=*/false,
4680 /*template_keyword_p=*/false,
4682 /*check_dependency=*/false,
4683 /*class_head_p=*/false,
4685 if (cp_parser_parse_definitely (parser
))
4688 /* In "p->S::~T", look in the scope given by "*p" as well. */
4689 else if (!done
&& object_scope
)
4691 cp_parser_parse_tentatively (parser
);
4692 parser
->scope
= object_scope
;
4693 parser
->object_scope
= NULL_TREE
;
4694 parser
->qualifying_scope
= NULL_TREE
;
4696 = cp_parser_class_name (parser
,
4697 /*typename_keyword_p=*/false,
4698 /*template_keyword_p=*/false,
4700 /*check_dependency=*/false,
4701 /*class_head_p=*/false,
4703 if (cp_parser_parse_definitely (parser
))
4706 /* Look in the surrounding context. */
4709 parser
->scope
= NULL_TREE
;
4710 parser
->object_scope
= NULL_TREE
;
4711 parser
->qualifying_scope
= NULL_TREE
;
4712 if (processing_template_decl
)
4713 cp_parser_parse_tentatively (parser
);
4715 = cp_parser_class_name (parser
,
4716 /*typename_keyword_p=*/false,
4717 /*template_keyword_p=*/false,
4719 /*check_dependency=*/false,
4720 /*class_head_p=*/false,
4722 if (processing_template_decl
4723 && ! cp_parser_parse_definitely (parser
))
4725 /* We couldn't find a type with this name, so just accept
4726 it and check for a match at instantiation time. */
4727 type_decl
= cp_parser_identifier (parser
);
4728 if (type_decl
!= error_mark_node
)
4729 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
4733 /* If an error occurred, assume that the name of the
4734 destructor is the same as the name of the qualifying
4735 class. That allows us to keep parsing after running
4736 into ill-formed destructor names. */
4737 if (type_decl
== error_mark_node
&& scope
)
4738 return build_nt (BIT_NOT_EXPR
, scope
);
4739 else if (type_decl
== error_mark_node
)
4740 return error_mark_node
;
4742 /* Check that destructor name and scope match. */
4743 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
4745 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4746 error_at (token
->location
,
4747 "declaration of %<~%T%> as member of %qT",
4749 cp_parser_simulate_error (parser
);
4750 return error_mark_node
;
4755 A typedef-name that names a class shall not be used as the
4756 identifier in the declarator for a destructor declaration. */
4758 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
4759 && !DECL_SELF_REFERENCE_P (type_decl
)
4760 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4761 error_at (token
->location
,
4762 "typedef-name %qD used as destructor declarator",
4765 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
4769 if (token
->keyword
== RID_OPERATOR
)
4773 /* This could be a template-id, so we try that first. */
4774 cp_parser_parse_tentatively (parser
);
4775 /* Try a template-id. */
4776 id
= cp_parser_template_id (parser
, template_keyword_p
,
4777 /*check_dependency_p=*/true,
4780 /* If that worked, we're done. */
4781 if (cp_parser_parse_definitely (parser
))
4783 /* We still don't know whether we're looking at an
4784 operator-function-id or a conversion-function-id. */
4785 cp_parser_parse_tentatively (parser
);
4786 /* Try an operator-function-id. */
4787 id
= cp_parser_operator_function_id (parser
);
4788 /* If that didn't work, try a conversion-function-id. */
4789 if (!cp_parser_parse_definitely (parser
))
4790 id
= cp_parser_conversion_function_id (parser
);
4791 else if (UDLIT_OPER_P (id
))
4794 const char *name
= UDLIT_OP_SUFFIX (id
);
4795 if (name
[0] != '_' && !in_system_header
)
4796 warning (0, "literal operator suffixes not preceded by %<_%>"
4797 " are reserved for future standardization");
4807 cp_parser_error (parser
, "expected unqualified-id");
4808 return error_mark_node
;
4812 /* Parse an (optional) nested-name-specifier.
4814 nested-name-specifier: [C++98]
4815 class-or-namespace-name :: nested-name-specifier [opt]
4816 class-or-namespace-name :: template nested-name-specifier [opt]
4818 nested-name-specifier: [C++0x]
4821 nested-name-specifier identifier ::
4822 nested-name-specifier template [opt] simple-template-id ::
4824 PARSER->SCOPE should be set appropriately before this function is
4825 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4826 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4829 Sets PARSER->SCOPE to the class (TYPE) or namespace
4830 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4831 it unchanged if there is no nested-name-specifier. Returns the new
4832 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4834 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4835 part of a declaration and/or decl-specifier. */
4838 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
4839 bool typename_keyword_p
,
4840 bool check_dependency_p
,
4842 bool is_declaration
)
4844 bool success
= false;
4845 cp_token_position start
= 0;
4848 /* Remember where the nested-name-specifier starts. */
4849 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4851 start
= cp_lexer_token_position (parser
->lexer
, false);
4852 push_deferring_access_checks (dk_deferred
);
4859 tree saved_qualifying_scope
;
4860 bool template_keyword_p
;
4862 /* Spot cases that cannot be the beginning of a
4863 nested-name-specifier. */
4864 token
= cp_lexer_peek_token (parser
->lexer
);
4866 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4867 the already parsed nested-name-specifier. */
4868 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
4870 /* Grab the nested-name-specifier and continue the loop. */
4871 cp_parser_pre_parsed_nested_name_specifier (parser
);
4872 /* If we originally encountered this nested-name-specifier
4873 with IS_DECLARATION set to false, we will not have
4874 resolved TYPENAME_TYPEs, so we must do so here. */
4876 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4878 new_scope
= resolve_typename_type (parser
->scope
,
4879 /*only_current_p=*/false);
4880 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
4881 parser
->scope
= new_scope
;
4887 /* Spot cases that cannot be the beginning of a
4888 nested-name-specifier. On the second and subsequent times
4889 through the loop, we look for the `template' keyword. */
4890 if (success
&& token
->keyword
== RID_TEMPLATE
)
4892 /* A template-id can start a nested-name-specifier. */
4893 else if (token
->type
== CPP_TEMPLATE_ID
)
4895 /* DR 743: decltype can be used in a nested-name-specifier. */
4896 else if (token_is_decltype (token
))
4900 /* If the next token is not an identifier, then it is
4901 definitely not a type-name or namespace-name. */
4902 if (token
->type
!= CPP_NAME
)
4904 /* If the following token is neither a `<' (to begin a
4905 template-id), nor a `::', then we are not looking at a
4906 nested-name-specifier. */
4907 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
4909 if (token
->type
== CPP_COLON
4910 && parser
->colon_corrects_to_scope_p
4911 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
4913 error_at (token
->location
,
4914 "found %<:%> in nested-name-specifier, expected %<::%>");
4915 token
->type
= CPP_SCOPE
;
4918 if (token
->type
!= CPP_SCOPE
4919 && !cp_parser_nth_token_starts_template_argument_list_p
4924 /* The nested-name-specifier is optional, so we parse
4926 cp_parser_parse_tentatively (parser
);
4928 /* Look for the optional `template' keyword, if this isn't the
4929 first time through the loop. */
4931 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
4933 template_keyword_p
= false;
4935 /* Save the old scope since the name lookup we are about to do
4936 might destroy it. */
4937 old_scope
= parser
->scope
;
4938 saved_qualifying_scope
= parser
->qualifying_scope
;
4939 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4940 look up names in "X<T>::I" in order to determine that "Y" is
4941 a template. So, if we have a typename at this point, we make
4942 an effort to look through it. */
4944 && !typename_keyword_p
4946 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
4947 parser
->scope
= resolve_typename_type (parser
->scope
,
4948 /*only_current_p=*/false);
4949 /* Parse the qualifying entity. */
4951 = cp_parser_qualifying_entity (parser
,
4957 /* Look for the `::' token. */
4958 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
4960 /* If we found what we wanted, we keep going; otherwise, we're
4962 if (!cp_parser_parse_definitely (parser
))
4964 bool error_p
= false;
4966 /* Restore the OLD_SCOPE since it was valid before the
4967 failed attempt at finding the last
4968 class-or-namespace-name. */
4969 parser
->scope
= old_scope
;
4970 parser
->qualifying_scope
= saved_qualifying_scope
;
4972 /* If the next token is a decltype, and the one after that is a
4973 `::', then the decltype has failed to resolve to a class or
4974 enumeration type. Give this error even when parsing
4975 tentatively since it can't possibly be valid--and we're going
4976 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4977 won't get another chance.*/
4978 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
4979 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4982 token
= cp_lexer_consume_token (parser
->lexer
);
4983 error_at (token
->location
, "decltype evaluates to %qT, "
4984 "which is not a class or enumeration type",
4986 parser
->scope
= error_mark_node
;
4990 cp_lexer_consume_token (parser
->lexer
);
4993 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4995 /* If the next token is an identifier, and the one after
4996 that is a `::', then any valid interpretation would have
4997 found a class-or-namespace-name. */
4998 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
4999 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5001 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5004 token
= cp_lexer_consume_token (parser
->lexer
);
5007 if (!token
->ambiguous_p
)
5010 tree ambiguous_decls
;
5012 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5014 /*is_template=*/false,
5015 /*is_namespace=*/false,
5016 /*check_dependency=*/true,
5019 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5020 error_at (token
->location
,
5021 "%qD used without template parameters",
5023 else if (ambiguous_decls
)
5025 error_at (token
->location
,
5026 "reference to %qD is ambiguous",
5028 print_candidates (ambiguous_decls
);
5029 decl
= error_mark_node
;
5033 if (cxx_dialect
!= cxx98
)
5034 cp_parser_name_lookup_error
5035 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5038 cp_parser_name_lookup_error
5039 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5043 parser
->scope
= error_mark_node
;
5045 /* Treat this as a successful nested-name-specifier
5050 If the name found is not a class-name (clause
5051 _class_) or namespace-name (_namespace.def_), the
5052 program is ill-formed. */
5055 cp_lexer_consume_token (parser
->lexer
);
5059 /* We've found one valid nested-name-specifier. */
5061 /* Name lookup always gives us a DECL. */
5062 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5063 new_scope
= TREE_TYPE (new_scope
);
5064 /* Uses of "template" must be followed by actual templates. */
5065 if (template_keyword_p
5066 && !(CLASS_TYPE_P (new_scope
)
5067 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5068 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5069 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5070 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5071 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5072 == TEMPLATE_ID_EXPR
)))
5073 permerror (input_location
, TYPE_P (new_scope
)
5074 ? G_("%qT is not a template")
5075 : G_("%qD is not a template"),
5077 /* If it is a class scope, try to complete it; we are about to
5078 be looking up names inside the class. */
5079 if (TYPE_P (new_scope
)
5080 /* Since checking types for dependency can be expensive,
5081 avoid doing it if the type is already complete. */
5082 && !COMPLETE_TYPE_P (new_scope
)
5083 /* Do not try to complete dependent types. */
5084 && !dependent_type_p (new_scope
))
5086 new_scope
= complete_type (new_scope
);
5087 /* If it is a typedef to current class, use the current
5088 class instead, as the typedef won't have any names inside
5090 if (!COMPLETE_TYPE_P (new_scope
)
5091 && currently_open_class (new_scope
))
5092 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5094 /* Make sure we look in the right scope the next time through
5096 parser
->scope
= new_scope
;
5099 /* If parsing tentatively, replace the sequence of tokens that makes
5100 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5101 token. That way, should we re-parse the token stream, we will
5102 not have to repeat the effort required to do the parse, nor will
5103 we issue duplicate error messages. */
5104 if (success
&& start
)
5108 token
= cp_lexer_token_at (parser
->lexer
, start
);
5109 /* Reset the contents of the START token. */
5110 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5111 /* Retrieve any deferred checks. Do not pop this access checks yet
5112 so the memory will not be reclaimed during token replacing below. */
5113 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
5114 token
->u
.tree_check_value
->value
= parser
->scope
;
5115 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5116 token
->u
.tree_check_value
->qualifying_scope
=
5117 parser
->qualifying_scope
;
5118 token
->keyword
= RID_MAX
;
5120 /* Purge all subsequent tokens. */
5121 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5125 pop_to_parent_deferring_access_checks ();
5127 return success
? parser
->scope
: NULL_TREE
;
5130 /* Parse a nested-name-specifier. See
5131 cp_parser_nested_name_specifier_opt for details. This function
5132 behaves identically, except that it will an issue an error if no
5133 nested-name-specifier is present. */
5136 cp_parser_nested_name_specifier (cp_parser
*parser
,
5137 bool typename_keyword_p
,
5138 bool check_dependency_p
,
5140 bool is_declaration
)
5144 /* Look for the nested-name-specifier. */
5145 scope
= cp_parser_nested_name_specifier_opt (parser
,
5150 /* If it was not present, issue an error message. */
5153 cp_parser_error (parser
, "expected nested-name-specifier");
5154 parser
->scope
= NULL_TREE
;
5160 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5161 this is either a class-name or a namespace-name (which corresponds
5162 to the class-or-namespace-name production in the grammar). For
5163 C++0x, it can also be a type-name that refers to an enumeration
5164 type or a simple-template-id.
5166 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5167 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5168 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5169 TYPE_P is TRUE iff the next name should be taken as a class-name,
5170 even the same name is declared to be another entity in the same
5173 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5174 specified by the class-or-namespace-name. If neither is found the
5175 ERROR_MARK_NODE is returned. */
5178 cp_parser_qualifying_entity (cp_parser
*parser
,
5179 bool typename_keyword_p
,
5180 bool template_keyword_p
,
5181 bool check_dependency_p
,
5183 bool is_declaration
)
5186 tree saved_qualifying_scope
;
5187 tree saved_object_scope
;
5190 bool successful_parse_p
;
5192 /* DR 743: decltype can appear in a nested-name-specifier. */
5193 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5195 scope
= cp_parser_decltype (parser
);
5196 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5197 && !MAYBE_CLASS_TYPE_P (scope
))
5199 cp_parser_simulate_error (parser
);
5200 return error_mark_node
;
5202 if (TYPE_NAME (scope
))
5203 scope
= TYPE_NAME (scope
);
5207 /* Before we try to parse the class-name, we must save away the
5208 current PARSER->SCOPE since cp_parser_class_name will destroy
5210 saved_scope
= parser
->scope
;
5211 saved_qualifying_scope
= parser
->qualifying_scope
;
5212 saved_object_scope
= parser
->object_scope
;
5213 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5214 there is no need to look for a namespace-name. */
5215 only_class_p
= template_keyword_p
5216 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5218 cp_parser_parse_tentatively (parser
);
5219 scope
= cp_parser_class_name (parser
,
5222 type_p
? class_type
: none_type
,
5224 /*class_head_p=*/false,
5226 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5227 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5229 && cxx_dialect
!= cxx98
5230 && !successful_parse_p
)
5232 /* Restore the saved scope. */
5233 parser
->scope
= saved_scope
;
5234 parser
->qualifying_scope
= saved_qualifying_scope
;
5235 parser
->object_scope
= saved_object_scope
;
5237 /* Parse tentatively. */
5238 cp_parser_parse_tentatively (parser
);
5240 /* Parse a type-name */
5241 scope
= cp_parser_type_name (parser
);
5243 /* "If the name found does not designate a namespace or a class,
5244 enumeration, or dependent type, the program is ill-formed."
5246 We cover classes and dependent types above and namespaces below,
5247 so this code is only looking for enums. */
5248 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5249 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5250 cp_parser_simulate_error (parser
);
5252 successful_parse_p
= cp_parser_parse_definitely (parser
);
5254 /* If that didn't work, try for a namespace-name. */
5255 if (!only_class_p
&& !successful_parse_p
)
5257 /* Restore the saved scope. */
5258 parser
->scope
= saved_scope
;
5259 parser
->qualifying_scope
= saved_qualifying_scope
;
5260 parser
->object_scope
= saved_object_scope
;
5261 /* If we are not looking at an identifier followed by the scope
5262 resolution operator, then this is not part of a
5263 nested-name-specifier. (Note that this function is only used
5264 to parse the components of a nested-name-specifier.) */
5265 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5266 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5267 return error_mark_node
;
5268 scope
= cp_parser_namespace_name (parser
);
5274 /* Parse a postfix-expression.
5278 postfix-expression [ expression ]
5279 postfix-expression ( expression-list [opt] )
5280 simple-type-specifier ( expression-list [opt] )
5281 typename :: [opt] nested-name-specifier identifier
5282 ( expression-list [opt] )
5283 typename :: [opt] nested-name-specifier template [opt] template-id
5284 ( expression-list [opt] )
5285 postfix-expression . template [opt] id-expression
5286 postfix-expression -> template [opt] id-expression
5287 postfix-expression . pseudo-destructor-name
5288 postfix-expression -> pseudo-destructor-name
5289 postfix-expression ++
5290 postfix-expression --
5291 dynamic_cast < type-id > ( expression )
5292 static_cast < type-id > ( expression )
5293 reinterpret_cast < type-id > ( expression )
5294 const_cast < type-id > ( expression )
5295 typeid ( expression )
5301 ( type-id ) { initializer-list , [opt] }
5303 This extension is a GNU version of the C99 compound-literal
5304 construct. (The C99 grammar uses `type-name' instead of `type-id',
5305 but they are essentially the same concept.)
5307 If ADDRESS_P is true, the postfix expression is the operand of the
5308 `&' operator. CAST_P is true if this expression is the target of a
5311 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5312 class member access expressions [expr.ref].
5314 Returns a representation of the expression. */
5317 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5318 bool member_access_only_p
,
5319 cp_id_kind
* pidk_return
)
5323 cp_id_kind idk
= CP_ID_KIND_NONE
;
5324 tree postfix_expression
= NULL_TREE
;
5325 bool is_member_access
= false;
5327 /* Peek at the next token. */
5328 token
= cp_lexer_peek_token (parser
->lexer
);
5329 /* Some of the productions are determined by keywords. */
5330 keyword
= token
->keyword
;
5340 const char *saved_message
;
5342 /* All of these can be handled in the same way from the point
5343 of view of parsing. Begin by consuming the token
5344 identifying the cast. */
5345 cp_lexer_consume_token (parser
->lexer
);
5347 /* New types cannot be defined in the cast. */
5348 saved_message
= parser
->type_definition_forbidden_message
;
5349 parser
->type_definition_forbidden_message
5350 = G_("types may not be defined in casts");
5352 /* Look for the opening `<'. */
5353 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5354 /* Parse the type to which we are casting. */
5355 type
= cp_parser_type_id (parser
);
5356 /* Look for the closing `>'. */
5357 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5358 /* Restore the old message. */
5359 parser
->type_definition_forbidden_message
= saved_message
;
5361 /* And the expression which is being cast. */
5362 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5363 expression
= cp_parser_expression (parser
, /*cast_p=*/true, & idk
);
5364 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5366 /* Only type conversions to integral or enumeration types
5367 can be used in constant-expressions. */
5368 if (!cast_valid_in_integral_constant_expression_p (type
)
5369 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5370 return error_mark_node
;
5376 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5380 = build_static_cast (type
, expression
, tf_warning_or_error
);
5384 = build_reinterpret_cast (type
, expression
,
5385 tf_warning_or_error
);
5389 = build_const_cast (type
, expression
, tf_warning_or_error
);
5400 const char *saved_message
;
5401 bool saved_in_type_id_in_expr_p
;
5403 /* Consume the `typeid' token. */
5404 cp_lexer_consume_token (parser
->lexer
);
5405 /* Look for the `(' token. */
5406 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5407 /* Types cannot be defined in a `typeid' expression. */
5408 saved_message
= parser
->type_definition_forbidden_message
;
5409 parser
->type_definition_forbidden_message
5410 = G_("types may not be defined in a %<typeid%> expression");
5411 /* We can't be sure yet whether we're looking at a type-id or an
5413 cp_parser_parse_tentatively (parser
);
5414 /* Try a type-id first. */
5415 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5416 parser
->in_type_id_in_expr_p
= true;
5417 type
= cp_parser_type_id (parser
);
5418 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5419 /* Look for the `)' token. Otherwise, we can't be sure that
5420 we're not looking at an expression: consider `typeid (int
5421 (3))', for example. */
5422 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5423 /* If all went well, simply lookup the type-id. */
5424 if (cp_parser_parse_definitely (parser
))
5425 postfix_expression
= get_typeid (type
);
5426 /* Otherwise, fall back to the expression variant. */
5431 /* Look for an expression. */
5432 expression
= cp_parser_expression (parser
, /*cast_p=*/false, & idk
);
5433 /* Compute its typeid. */
5434 postfix_expression
= build_typeid (expression
);
5435 /* Look for the `)' token. */
5436 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5438 /* Restore the saved message. */
5439 parser
->type_definition_forbidden_message
= saved_message
;
5440 /* `typeid' may not appear in an integral constant expression. */
5441 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5442 return error_mark_node
;
5449 /* The syntax permitted here is the same permitted for an
5450 elaborated-type-specifier. */
5451 type
= cp_parser_elaborated_type_specifier (parser
,
5452 /*is_friend=*/false,
5453 /*is_declaration=*/false);
5454 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5458 case RID_BUILTIN_SHUFFLE
:
5463 location_t loc
= token
->location
;
5465 cp_lexer_consume_token (parser
->lexer
);
5466 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5467 /*cast_p=*/false, /*allow_expansion_p=*/true,
5468 /*non_constant_p=*/NULL
);
5470 return error_mark_node
;
5472 FOR_EACH_VEC_ELT (tree
, vec
, i
, p
)
5475 if (VEC_length (tree
, vec
) == 2)
5477 c_build_vec_perm_expr
5478 (loc
, VEC_index (tree
, vec
, 0),
5479 NULL_TREE
, VEC_index (tree
, vec
, 1));
5481 else if (VEC_length (tree
, vec
) == 3)
5483 c_build_vec_perm_expr
5484 (loc
, VEC_index (tree
, vec
, 0),
5485 VEC_index (tree
, vec
, 1),
5486 VEC_index (tree
, vec
, 2));
5489 error_at (loc
, "wrong number of arguments to "
5490 "%<__builtin_shuffle%>");
5491 return error_mark_node
;
5500 /* If the next thing is a simple-type-specifier, we may be
5501 looking at a functional cast. We could also be looking at
5502 an id-expression. So, we try the functional cast, and if
5503 that doesn't work we fall back to the primary-expression. */
5504 cp_parser_parse_tentatively (parser
);
5505 /* Look for the simple-type-specifier. */
5506 type
= cp_parser_simple_type_specifier (parser
,
5507 /*decl_specs=*/NULL
,
5508 CP_PARSER_FLAGS_NONE
);
5509 /* Parse the cast itself. */
5510 if (!cp_parser_error_occurred (parser
))
5512 = cp_parser_functional_cast (parser
, type
);
5513 /* If that worked, we're done. */
5514 if (cp_parser_parse_definitely (parser
))
5517 /* If the functional-cast didn't work out, try a
5518 compound-literal. */
5519 if (cp_parser_allow_gnu_extensions_p (parser
)
5520 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
5522 VEC(constructor_elt
,gc
) *initializer_list
= NULL
;
5523 bool saved_in_type_id_in_expr_p
;
5525 cp_parser_parse_tentatively (parser
);
5526 /* Consume the `('. */
5527 cp_lexer_consume_token (parser
->lexer
);
5528 /* Parse the type. */
5529 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5530 parser
->in_type_id_in_expr_p
= true;
5531 type
= cp_parser_type_id (parser
);
5532 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5533 /* Look for the `)'. */
5534 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5535 /* Look for the `{'. */
5536 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
5537 /* If things aren't going well, there's no need to
5539 if (!cp_parser_error_occurred (parser
))
5541 bool non_constant_p
;
5542 /* Parse the initializer-list. */
5544 = cp_parser_initializer_list (parser
, &non_constant_p
);
5545 /* Allow a trailing `,'. */
5546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
5547 cp_lexer_consume_token (parser
->lexer
);
5548 /* Look for the final `}'. */
5549 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
5551 /* If that worked, we're definitely looking at a
5552 compound-literal expression. */
5553 if (cp_parser_parse_definitely (parser
))
5555 /* Warn the user that a compound literal is not
5556 allowed in standard C++. */
5557 pedwarn (input_location
, OPT_Wpedantic
, "ISO C++ forbids compound-literals");
5558 /* For simplicity, we disallow compound literals in
5559 constant-expressions. We could
5560 allow compound literals of integer type, whose
5561 initializer was a constant, in constant
5562 expressions. Permitting that usage, as a further
5563 extension, would not change the meaning of any
5564 currently accepted programs. (Of course, as
5565 compound literals are not part of ISO C++, the
5566 standard has nothing to say.) */
5567 if (cp_parser_non_integral_constant_expression (parser
,
5570 postfix_expression
= error_mark_node
;
5573 /* Form the representation of the compound-literal. */
5575 = (finish_compound_literal
5576 (type
, build_constructor (init_list_type_node
,
5578 tf_warning_or_error
));
5583 /* It must be a primary-expression. */
5585 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
5586 /*template_arg_p=*/false,
5592 /* Keep looping until the postfix-expression is complete. */
5595 if (idk
== CP_ID_KIND_UNQUALIFIED
5596 && TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
5597 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
5598 /* It is not a Koenig lookup function call. */
5600 = unqualified_name_lookup_error (postfix_expression
);
5602 /* Peek at the next token. */
5603 token
= cp_lexer_peek_token (parser
->lexer
);
5605 switch (token
->type
)
5607 case CPP_OPEN_SQUARE
:
5609 = cp_parser_postfix_open_square_expression (parser
,
5612 idk
= CP_ID_KIND_NONE
;
5613 is_member_access
= false;
5616 case CPP_OPEN_PAREN
:
5617 /* postfix-expression ( expression-list [opt] ) */
5620 bool is_builtin_constant_p
;
5621 bool saved_integral_constant_expression_p
= false;
5622 bool saved_non_integral_constant_expression_p
= false;
5625 is_member_access
= false;
5627 is_builtin_constant_p
5628 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
5629 if (is_builtin_constant_p
)
5631 /* The whole point of __builtin_constant_p is to allow
5632 non-constant expressions to appear as arguments. */
5633 saved_integral_constant_expression_p
5634 = parser
->integral_constant_expression_p
;
5635 saved_non_integral_constant_expression_p
5636 = parser
->non_integral_constant_expression_p
;
5637 parser
->integral_constant_expression_p
= false;
5639 args
= (cp_parser_parenthesized_expression_list
5641 /*cast_p=*/false, /*allow_expansion_p=*/true,
5642 /*non_constant_p=*/NULL
));
5643 if (is_builtin_constant_p
)
5645 parser
->integral_constant_expression_p
5646 = saved_integral_constant_expression_p
;
5647 parser
->non_integral_constant_expression_p
5648 = saved_non_integral_constant_expression_p
;
5653 postfix_expression
= error_mark_node
;
5657 /* Function calls are not permitted in
5658 constant-expressions. */
5659 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
5660 && cp_parser_non_integral_constant_expression (parser
,
5663 postfix_expression
= error_mark_node
;
5664 release_tree_vector (args
);
5669 if (idk
== CP_ID_KIND_UNQUALIFIED
5670 || idk
== CP_ID_KIND_TEMPLATE_ID
)
5672 if (TREE_CODE (postfix_expression
) == IDENTIFIER_NODE
)
5674 if (!VEC_empty (tree
, args
))
5677 if (!any_type_dependent_arguments_p (args
))
5679 = perform_koenig_lookup (postfix_expression
, args
,
5680 /*include_std=*/false,
5681 tf_warning_or_error
);
5685 = unqualified_fn_lookup_error (postfix_expression
);
5687 /* We do not perform argument-dependent lookup if
5688 normal lookup finds a non-function, in accordance
5689 with the expected resolution of DR 218. */
5690 else if (!VEC_empty (tree
, args
)
5691 && is_overloaded_fn (postfix_expression
))
5693 tree fn
= get_first_fn (postfix_expression
);
5694 fn
= STRIP_TEMPLATE (fn
);
5696 /* Do not do argument dependent lookup if regular
5697 lookup finds a member function or a block-scope
5698 function declaration. [basic.lookup.argdep]/3 */
5699 if (!DECL_FUNCTION_MEMBER_P (fn
)
5700 && !DECL_LOCAL_FUNCTION_P (fn
))
5703 if (!any_type_dependent_arguments_p (args
))
5705 = perform_koenig_lookup (postfix_expression
, args
,
5706 /*include_std=*/false,
5707 tf_warning_or_error
);
5712 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
5714 tree instance
= TREE_OPERAND (postfix_expression
, 0);
5715 tree fn
= TREE_OPERAND (postfix_expression
, 1);
5717 if (processing_template_decl
5718 && (type_dependent_expression_p (instance
)
5719 || (!BASELINK_P (fn
)
5720 && TREE_CODE (fn
) != FIELD_DECL
)
5721 || type_dependent_expression_p (fn
)
5722 || any_type_dependent_arguments_p (args
)))
5725 = build_nt_call_vec (postfix_expression
, args
);
5726 release_tree_vector (args
);
5730 if (BASELINK_P (fn
))
5733 = (build_new_method_call
5734 (instance
, fn
, &args
, NULL_TREE
,
5735 (idk
== CP_ID_KIND_QUALIFIED
5736 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
5739 tf_warning_or_error
));
5743 = finish_call_expr (postfix_expression
, &args
,
5744 /*disallow_virtual=*/false,
5746 tf_warning_or_error
);
5748 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
5749 || TREE_CODE (postfix_expression
) == MEMBER_REF
5750 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
5751 postfix_expression
= (build_offset_ref_call_from_tree
5752 (postfix_expression
, &args
));
5753 else if (idk
== CP_ID_KIND_QUALIFIED
)
5754 /* A call to a static class member, or a namespace-scope
5757 = finish_call_expr (postfix_expression
, &args
,
5758 /*disallow_virtual=*/true,
5760 tf_warning_or_error
);
5762 /* All other function calls. */
5764 = finish_call_expr (postfix_expression
, &args
,
5765 /*disallow_virtual=*/false,
5767 tf_warning_or_error
);
5769 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5770 idk
= CP_ID_KIND_NONE
;
5772 release_tree_vector (args
);
5778 /* postfix-expression . template [opt] id-expression
5779 postfix-expression . pseudo-destructor-name
5780 postfix-expression -> template [opt] id-expression
5781 postfix-expression -> pseudo-destructor-name */
5783 /* Consume the `.' or `->' operator. */
5784 cp_lexer_consume_token (parser
->lexer
);
5787 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
5792 is_member_access
= true;
5796 /* postfix-expression ++ */
5797 /* Consume the `++' token. */
5798 cp_lexer_consume_token (parser
->lexer
);
5799 /* Generate a representation for the complete expression. */
5801 = finish_increment_expr (postfix_expression
,
5802 POSTINCREMENT_EXPR
);
5803 /* Increments may not appear in constant-expressions. */
5804 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
5805 postfix_expression
= error_mark_node
;
5806 idk
= CP_ID_KIND_NONE
;
5807 is_member_access
= false;
5810 case CPP_MINUS_MINUS
:
5811 /* postfix-expression -- */
5812 /* Consume the `--' token. */
5813 cp_lexer_consume_token (parser
->lexer
);
5814 /* Generate a representation for the complete expression. */
5816 = finish_increment_expr (postfix_expression
,
5817 POSTDECREMENT_EXPR
);
5818 /* Decrements may not appear in constant-expressions. */
5819 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
5820 postfix_expression
= error_mark_node
;
5821 idk
= CP_ID_KIND_NONE
;
5822 is_member_access
= false;
5826 if (pidk_return
!= NULL
)
5827 * pidk_return
= idk
;
5828 if (member_access_only_p
)
5829 return is_member_access
? postfix_expression
: error_mark_node
;
5831 return postfix_expression
;
5835 /* We should never get here. */
5837 return error_mark_node
;
5840 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5841 by cp_parser_builtin_offsetof. We're looking for
5843 postfix-expression [ expression ]
5844 postfix-expression [ braced-init-list ] (C++11)
5846 FOR_OFFSETOF is set if we're being called in that context, which
5847 changes how we deal with integer constant expressions. */
5850 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
5851 tree postfix_expression
,
5855 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5857 /* Consume the `[' token. */
5858 cp_lexer_consume_token (parser
->lexer
);
5860 /* Parse the index expression. */
5861 /* ??? For offsetof, there is a question of what to allow here. If
5862 offsetof is not being used in an integral constant expression context,
5863 then we *could* get the right answer by computing the value at runtime.
5864 If we are in an integral constant expression context, then we might
5865 could accept any constant expression; hard to say without analysis.
5866 Rather than open the barn door too wide right away, allow only integer
5867 constant expressions here. */
5869 index
= cp_parser_constant_expression (parser
, false, NULL
);
5872 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
5874 bool expr_nonconst_p
;
5875 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
5876 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
5879 index
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
5882 /* Look for the closing `]'. */
5883 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
5885 /* Build the ARRAY_REF. */
5886 postfix_expression
= grok_array_decl (loc
, postfix_expression
, index
);
5888 /* When not doing offsetof, array references are not permitted in
5889 constant-expressions. */
5891 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
5892 postfix_expression
= error_mark_node
;
5894 return postfix_expression
;
5897 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5898 by cp_parser_builtin_offsetof. We're looking for
5900 postfix-expression . template [opt] id-expression
5901 postfix-expression . pseudo-destructor-name
5902 postfix-expression -> template [opt] id-expression
5903 postfix-expression -> pseudo-destructor-name
5905 FOR_OFFSETOF is set if we're being called in that context. That sorta
5906 limits what of the above we'll actually accept, but nevermind.
5907 TOKEN_TYPE is the "." or "->" token, which will already have been
5908 removed from the stream. */
5911 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
5912 enum cpp_ttype token_type
,
5913 tree postfix_expression
,
5914 bool for_offsetof
, cp_id_kind
*idk
,
5915 location_t location
)
5919 bool pseudo_destructor_p
;
5920 tree scope
= NULL_TREE
;
5922 /* If this is a `->' operator, dereference the pointer. */
5923 if (token_type
== CPP_DEREF
)
5924 postfix_expression
= build_x_arrow (location
, postfix_expression
,
5925 tf_warning_or_error
);
5926 /* Check to see whether or not the expression is type-dependent. */
5927 dependent_p
= type_dependent_expression_p (postfix_expression
);
5928 /* The identifier following the `->' or `.' is not qualified. */
5929 parser
->scope
= NULL_TREE
;
5930 parser
->qualifying_scope
= NULL_TREE
;
5931 parser
->object_scope
= NULL_TREE
;
5932 *idk
= CP_ID_KIND_NONE
;
5934 /* Enter the scope corresponding to the type of the object
5935 given by the POSTFIX_EXPRESSION. */
5936 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
5938 scope
= TREE_TYPE (postfix_expression
);
5939 /* According to the standard, no expression should ever have
5940 reference type. Unfortunately, we do not currently match
5941 the standard in this respect in that our internal representation
5942 of an expression may have reference type even when the standard
5943 says it does not. Therefore, we have to manually obtain the
5944 underlying type here. */
5945 scope
= non_reference (scope
);
5946 /* The type of the POSTFIX_EXPRESSION must be complete. */
5947 if (scope
== unknown_type_node
)
5949 error_at (location
, "%qE does not have class type",
5950 postfix_expression
);
5953 /* Unlike the object expression in other contexts, *this is not
5954 required to be of complete type for purposes of class member
5955 access (5.2.5) outside the member function body. */
5956 else if (scope
!= current_class_ref
5957 && !(processing_template_decl
&& scope
== current_class_type
))
5958 scope
= complete_type_or_else (scope
, NULL_TREE
);
5959 /* Let the name lookup machinery know that we are processing a
5960 class member access expression. */
5961 parser
->context
->object_type
= scope
;
5962 /* If something went wrong, we want to be able to discern that case,
5963 as opposed to the case where there was no SCOPE due to the type
5964 of expression being dependent. */
5966 scope
= error_mark_node
;
5967 /* If the SCOPE was erroneous, make the various semantic analysis
5968 functions exit quickly -- and without issuing additional error
5970 if (scope
== error_mark_node
)
5971 postfix_expression
= error_mark_node
;
5974 /* Assume this expression is not a pseudo-destructor access. */
5975 pseudo_destructor_p
= false;
5977 /* If the SCOPE is a scalar type, then, if this is a valid program,
5978 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5979 is type dependent, it can be pseudo-destructor-name or something else.
5980 Try to parse it as pseudo-destructor-name first. */
5981 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
5986 cp_parser_parse_tentatively (parser
);
5987 /* Parse the pseudo-destructor-name. */
5989 cp_parser_pseudo_destructor_name (parser
, &s
, &type
);
5991 && (cp_parser_error_occurred (parser
)
5992 || TREE_CODE (type
) != TYPE_DECL
5993 || !SCALAR_TYPE_P (TREE_TYPE (type
))))
5994 cp_parser_abort_tentative_parse (parser
);
5995 else if (cp_parser_parse_definitely (parser
))
5997 pseudo_destructor_p
= true;
5999 = finish_pseudo_destructor_expr (postfix_expression
,
6000 s
, TREE_TYPE (type
));
6004 if (!pseudo_destructor_p
)
6006 /* If the SCOPE is not a scalar type, we are looking at an
6007 ordinary class member access expression, rather than a
6008 pseudo-destructor-name. */
6010 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6011 /* Parse the id-expression. */
6012 name
= (cp_parser_id_expression
6014 cp_parser_optional_template_keyword (parser
),
6015 /*check_dependency_p=*/true,
6017 /*declarator_p=*/false,
6018 /*optional_p=*/false));
6019 /* In general, build a SCOPE_REF if the member name is qualified.
6020 However, if the name was not dependent and has already been
6021 resolved; there is no need to build the SCOPE_REF. For example;
6023 struct X { void f(); };
6024 template <typename T> void f(T* t) { t->X::f(); }
6026 Even though "t" is dependent, "X::f" is not and has been resolved
6027 to a BASELINK; there is no need to include scope information. */
6029 /* But we do need to remember that there was an explicit scope for
6030 virtual function calls. */
6032 *idk
= CP_ID_KIND_QUALIFIED
;
6034 /* If the name is a template-id that names a type, we will get a
6035 TYPE_DECL here. That is invalid code. */
6036 if (TREE_CODE (name
) == TYPE_DECL
)
6038 error_at (token
->location
, "invalid use of %qD", name
);
6039 postfix_expression
= error_mark_node
;
6043 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6045 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6047 error_at (token
->location
, "%<%D::%D%> is not a class member",
6048 parser
->scope
, name
);
6049 postfix_expression
= error_mark_node
;
6052 name
= build_qualified_name (/*type=*/NULL_TREE
,
6056 parser
->scope
= NULL_TREE
;
6057 parser
->qualifying_scope
= NULL_TREE
;
6058 parser
->object_scope
= NULL_TREE
;
6060 if (parser
->scope
&& name
&& BASELINK_P (name
))
6061 adjust_result_of_qualified_name_lookup
6062 (name
, parser
->scope
, scope
);
6064 = finish_class_member_access_expr (postfix_expression
, name
,
6066 tf_warning_or_error
);
6070 /* We no longer need to look up names in the scope of the object on
6071 the left-hand side of the `.' or `->' operator. */
6072 parser
->context
->object_type
= NULL_TREE
;
6074 /* Outside of offsetof, these operators may not appear in
6075 constant-expressions. */
6077 && (cp_parser_non_integral_constant_expression
6078 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6079 postfix_expression
= error_mark_node
;
6081 return postfix_expression
;
6084 /* Parse a parenthesized expression-list.
6087 assignment-expression
6088 expression-list, assignment-expression
6093 identifier, expression-list
6095 CAST_P is true if this expression is the target of a cast.
6097 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6100 Returns a vector of trees. Each element is a representation of an
6101 assignment-expression. NULL is returned if the ( and or ) are
6102 missing. An empty, but allocated, vector is returned on no
6103 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6104 if we are parsing an attribute list for an attribute that wants a
6105 plain identifier argument, normal_attr for an attribute that wants
6106 an expression, or non_attr if we aren't parsing an attribute list. If
6107 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6108 not all of the expressions in the list were constant. */
6110 static VEC(tree
,gc
) *
6111 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6112 int is_attribute_list
,
6114 bool allow_expansion_p
,
6115 bool *non_constant_p
)
6117 VEC(tree
,gc
) *expression_list
;
6118 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6119 tree identifier
= NULL_TREE
;
6120 bool saved_greater_than_is_operator_p
;
6122 /* Assume all the expressions will be constant. */
6124 *non_constant_p
= false;
6126 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6129 expression_list
= make_tree_vector ();
6131 /* Within a parenthesized expression, a `>' token is always
6132 the greater-than operator. */
6133 saved_greater_than_is_operator_p
6134 = parser
->greater_than_is_operator_p
;
6135 parser
->greater_than_is_operator_p
= true;
6137 /* Consume expressions until there are no more. */
6138 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6143 /* At the beginning of attribute lists, check to see if the
6144 next token is an identifier. */
6145 if (is_attribute_list
== id_attr
6146 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6150 /* Consume the identifier. */
6151 token
= cp_lexer_consume_token (parser
->lexer
);
6152 /* Save the identifier. */
6153 identifier
= token
->u
.value
;
6157 bool expr_non_constant_p
;
6159 /* Parse the next assignment-expression. */
6160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6162 /* A braced-init-list. */
6163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6164 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6165 if (non_constant_p
&& expr_non_constant_p
)
6166 *non_constant_p
= true;
6168 else if (non_constant_p
)
6170 expr
= (cp_parser_constant_expression
6171 (parser
, /*allow_non_constant_p=*/true,
6172 &expr_non_constant_p
));
6173 if (expr_non_constant_p
)
6174 *non_constant_p
= true;
6177 expr
= cp_parser_assignment_expression (parser
, cast_p
, NULL
);
6180 expr
= fold_non_dependent_expr (expr
);
6182 /* If we have an ellipsis, then this is an expression
6184 if (allow_expansion_p
6185 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6187 /* Consume the `...'. */
6188 cp_lexer_consume_token (parser
->lexer
);
6190 /* Build the argument pack. */
6191 expr
= make_pack_expansion (expr
);
6194 /* Add it to the list. We add error_mark_node
6195 expressions to the list, so that we can still tell if
6196 the correct form for a parenthesized expression-list
6197 is found. That gives better errors. */
6198 VEC_safe_push (tree
, gc
, expression_list
, expr
);
6200 if (expr
== error_mark_node
)
6204 /* After the first item, attribute lists look the same as
6205 expression lists. */
6206 is_attribute_list
= non_attr
;
6209 /* If the next token isn't a `,', then we are done. */
6210 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6213 /* Otherwise, consume the `,' and keep going. */
6214 cp_lexer_consume_token (parser
->lexer
);
6217 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6222 /* We try and resync to an unnested comma, as that will give the
6223 user better diagnostics. */
6224 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6225 /*recovering=*/true,
6227 /*consume_paren=*/true);
6232 parser
->greater_than_is_operator_p
6233 = saved_greater_than_is_operator_p
;
6238 parser
->greater_than_is_operator_p
6239 = saved_greater_than_is_operator_p
;
6242 VEC_safe_insert (tree
, gc
, expression_list
, 0, identifier
);
6244 return expression_list
;
6247 /* Parse a pseudo-destructor-name.
6249 pseudo-destructor-name:
6250 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6251 :: [opt] nested-name-specifier template template-id :: ~ type-name
6252 :: [opt] nested-name-specifier [opt] ~ type-name
6254 If either of the first two productions is used, sets *SCOPE to the
6255 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6256 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6257 or ERROR_MARK_NODE if the parse fails. */
6260 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6264 bool nested_name_specifier_p
;
6266 /* Assume that things will not work out. */
6267 *type
= error_mark_node
;
6269 /* Look for the optional `::' operator. */
6270 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
6271 /* Look for the optional nested-name-specifier. */
6272 nested_name_specifier_p
6273 = (cp_parser_nested_name_specifier_opt (parser
,
6274 /*typename_keyword_p=*/false,
6275 /*check_dependency_p=*/true,
6277 /*is_declaration=*/false)
6279 /* Now, if we saw a nested-name-specifier, we might be doing the
6280 second production. */
6281 if (nested_name_specifier_p
6282 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
6284 /* Consume the `template' keyword. */
6285 cp_lexer_consume_token (parser
->lexer
);
6286 /* Parse the template-id. */
6287 cp_parser_template_id (parser
,
6288 /*template_keyword_p=*/true,
6289 /*check_dependency_p=*/false,
6291 /*is_declaration=*/true);
6292 /* Look for the `::' token. */
6293 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6295 /* If the next token is not a `~', then there might be some
6296 additional qualification. */
6297 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
6299 /* At this point, we're looking for "type-name :: ~". The type-name
6300 must not be a class-name, since this is a pseudo-destructor. So,
6301 it must be either an enum-name, or a typedef-name -- both of which
6302 are just identifiers. So, we peek ahead to check that the "::"
6303 and "~" tokens are present; if they are not, then we can avoid
6304 calling type_name. */
6305 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
6306 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
6307 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
6309 cp_parser_error (parser
, "non-scalar type");
6313 /* Look for the type-name. */
6314 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
6315 if (*scope
== error_mark_node
)
6318 /* Look for the `::' token. */
6319 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6324 /* Look for the `~'. */
6325 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
6327 /* Once we see the ~, this has to be a pseudo-destructor. */
6328 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
6329 cp_parser_commit_to_tentative_parse (parser
);
6331 /* Look for the type-name again. We are not responsible for
6332 checking that it matches the first type-name. */
6333 *type
= cp_parser_nonclass_name (parser
);
6336 /* Parse a unary-expression.
6342 unary-operator cast-expression
6343 sizeof unary-expression
6345 alignof ( type-id ) [C++0x]
6352 __extension__ cast-expression
6353 __alignof__ unary-expression
6354 __alignof__ ( type-id )
6355 alignof unary-expression [C++0x]
6356 __real__ cast-expression
6357 __imag__ cast-expression
6360 ADDRESS_P is true iff the unary-expression is appearing as the
6361 operand of the `&' operator. CAST_P is true if this expression is
6362 the target of a cast.
6364 Returns a representation of the expression. */
6367 cp_parser_unary_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6371 enum tree_code unary_operator
;
6373 /* Peek at the next token. */
6374 token
= cp_lexer_peek_token (parser
->lexer
);
6375 /* Some keywords give away the kind of expression. */
6376 if (token
->type
== CPP_KEYWORD
)
6378 enum rid keyword
= token
->keyword
;
6388 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
6389 /* Consume the token. */
6390 cp_lexer_consume_token (parser
->lexer
);
6391 /* Parse the operand. */
6392 operand
= cp_parser_sizeof_operand (parser
, keyword
);
6394 if (TYPE_P (operand
))
6395 return cxx_sizeof_or_alignof_type (operand
, op
, true);
6398 /* ISO C++ defines alignof only with types, not with
6399 expressions. So pedwarn if alignof is used with a non-
6400 type expression. However, __alignof__ is ok. */
6401 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
6402 pedwarn (token
->location
, OPT_Wpedantic
,
6403 "ISO C++ does not allow %<alignof%> "
6406 return cxx_sizeof_or_alignof_expr (operand
, op
, true);
6411 return cp_parser_new_expression (parser
);
6414 return cp_parser_delete_expression (parser
);
6418 /* The saved value of the PEDANTIC flag. */
6422 /* Save away the PEDANTIC flag. */
6423 cp_parser_extension_opt (parser
, &saved_pedantic
);
6424 /* Parse the cast-expression. */
6425 expr
= cp_parser_simple_cast_expression (parser
);
6426 /* Restore the PEDANTIC flag. */
6427 pedantic
= saved_pedantic
;
6437 /* Consume the `__real__' or `__imag__' token. */
6438 cp_lexer_consume_token (parser
->lexer
);
6439 /* Parse the cast-expression. */
6440 expression
= cp_parser_simple_cast_expression (parser
);
6441 /* Create the complete representation. */
6442 return build_x_unary_op (token
->location
,
6443 (keyword
== RID_REALPART
6444 ? REALPART_EXPR
: IMAGPART_EXPR
),
6446 tf_warning_or_error
);
6450 case RID_TRANSACTION_ATOMIC
:
6451 case RID_TRANSACTION_RELAXED
:
6452 return cp_parser_transaction_expression (parser
, keyword
);
6457 const char *saved_message
;
6458 bool saved_integral_constant_expression_p
;
6459 bool saved_non_integral_constant_expression_p
;
6460 bool saved_greater_than_is_operator_p
;
6462 cp_lexer_consume_token (parser
->lexer
);
6463 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6465 saved_message
= parser
->type_definition_forbidden_message
;
6466 parser
->type_definition_forbidden_message
6467 = G_("types may not be defined in %<noexcept%> expressions");
6469 saved_integral_constant_expression_p
6470 = parser
->integral_constant_expression_p
;
6471 saved_non_integral_constant_expression_p
6472 = parser
->non_integral_constant_expression_p
;
6473 parser
->integral_constant_expression_p
= false;
6475 saved_greater_than_is_operator_p
6476 = parser
->greater_than_is_operator_p
;
6477 parser
->greater_than_is_operator_p
= true;
6479 ++cp_unevaluated_operand
;
6480 ++c_inhibit_evaluation_warnings
;
6481 expr
= cp_parser_expression (parser
, false, NULL
);
6482 --c_inhibit_evaluation_warnings
;
6483 --cp_unevaluated_operand
;
6485 parser
->greater_than_is_operator_p
6486 = saved_greater_than_is_operator_p
;
6488 parser
->integral_constant_expression_p
6489 = saved_integral_constant_expression_p
;
6490 parser
->non_integral_constant_expression_p
6491 = saved_non_integral_constant_expression_p
;
6493 parser
->type_definition_forbidden_message
= saved_message
;
6495 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6496 return finish_noexcept_expr (expr
, tf_warning_or_error
);
6504 /* Look for the `:: new' and `:: delete', which also signal the
6505 beginning of a new-expression, or delete-expression,
6506 respectively. If the next token is `::', then it might be one of
6508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
6512 /* See if the token after the `::' is one of the keywords in
6513 which we're interested. */
6514 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
6515 /* If it's `new', we have a new-expression. */
6516 if (keyword
== RID_NEW
)
6517 return cp_parser_new_expression (parser
);
6518 /* Similarly, for `delete'. */
6519 else if (keyword
== RID_DELETE
)
6520 return cp_parser_delete_expression (parser
);
6523 /* Look for a unary operator. */
6524 unary_operator
= cp_parser_unary_operator (token
);
6525 /* The `++' and `--' operators can be handled similarly, even though
6526 they are not technically unary-operators in the grammar. */
6527 if (unary_operator
== ERROR_MARK
)
6529 if (token
->type
== CPP_PLUS_PLUS
)
6530 unary_operator
= PREINCREMENT_EXPR
;
6531 else if (token
->type
== CPP_MINUS_MINUS
)
6532 unary_operator
= PREDECREMENT_EXPR
;
6533 /* Handle the GNU address-of-label extension. */
6534 else if (cp_parser_allow_gnu_extensions_p (parser
)
6535 && token
->type
== CPP_AND_AND
)
6539 location_t loc
= token
->location
;
6541 /* Consume the '&&' token. */
6542 cp_lexer_consume_token (parser
->lexer
);
6543 /* Look for the identifier. */
6544 identifier
= cp_parser_identifier (parser
);
6545 /* Create an expression representing the address. */
6546 expression
= finish_label_address_expr (identifier
, loc
);
6547 if (cp_parser_non_integral_constant_expression (parser
,
6549 expression
= error_mark_node
;
6553 if (unary_operator
!= ERROR_MARK
)
6555 tree cast_expression
;
6556 tree expression
= error_mark_node
;
6557 non_integral_constant non_constant_p
= NIC_NONE
;
6558 location_t loc
= token
->location
;
6560 /* Consume the operator token. */
6561 token
= cp_lexer_consume_token (parser
->lexer
);
6562 /* Parse the cast-expression. */
6564 = cp_parser_cast_expression (parser
,
6565 unary_operator
== ADDR_EXPR
,
6566 /*cast_p=*/false, pidk
);
6567 /* Now, build an appropriate representation. */
6568 switch (unary_operator
)
6571 non_constant_p
= NIC_STAR
;
6572 expression
= build_x_indirect_ref (loc
, cast_expression
,
6574 tf_warning_or_error
);
6578 non_constant_p
= NIC_ADDR
;
6581 expression
= build_x_unary_op (loc
, unary_operator
,
6583 tf_warning_or_error
);
6586 case PREINCREMENT_EXPR
:
6587 case PREDECREMENT_EXPR
:
6588 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
6589 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
6591 case UNARY_PLUS_EXPR
:
6593 case TRUTH_NOT_EXPR
:
6594 expression
= finish_unary_op_expr (loc
, unary_operator
,
6602 if (non_constant_p
!= NIC_NONE
6603 && cp_parser_non_integral_constant_expression (parser
,
6605 expression
= error_mark_node
;
6610 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
6611 /*member_access_only_p=*/false,
6615 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6616 unary-operator, the corresponding tree code is returned. */
6618 static enum tree_code
6619 cp_parser_unary_operator (cp_token
* token
)
6621 switch (token
->type
)
6624 return INDIRECT_REF
;
6630 return UNARY_PLUS_EXPR
;
6636 return TRUTH_NOT_EXPR
;
6639 return BIT_NOT_EXPR
;
6646 /* Parse a new-expression.
6649 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6650 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6652 Returns a representation of the expression. */
6655 cp_parser_new_expression (cp_parser
* parser
)
6657 bool global_scope_p
;
6658 VEC(tree
,gc
) *placement
;
6660 VEC(tree
,gc
) *initializer
;
6661 tree nelts
= NULL_TREE
;
6664 /* Look for the optional `::' operator. */
6666 = (cp_parser_global_scope_opt (parser
,
6667 /*current_scope_valid_p=*/false)
6669 /* Look for the `new' operator. */
6670 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
6671 /* There's no easy way to tell a new-placement from the
6672 `( type-id )' construct. */
6673 cp_parser_parse_tentatively (parser
);
6674 /* Look for a new-placement. */
6675 placement
= cp_parser_new_placement (parser
);
6676 /* If that didn't work out, there's no new-placement. */
6677 if (!cp_parser_parse_definitely (parser
))
6679 if (placement
!= NULL
)
6680 release_tree_vector (placement
);
6684 /* If the next token is a `(', then we have a parenthesized
6686 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6689 const char *saved_message
= parser
->type_definition_forbidden_message
;
6691 /* Consume the `('. */
6692 cp_lexer_consume_token (parser
->lexer
);
6694 /* Parse the type-id. */
6695 parser
->type_definition_forbidden_message
6696 = G_("types may not be defined in a new-expression");
6697 type
= cp_parser_type_id (parser
);
6698 parser
->type_definition_forbidden_message
= saved_message
;
6700 /* Look for the closing `)'. */
6701 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6702 token
= cp_lexer_peek_token (parser
->lexer
);
6703 /* There should not be a direct-new-declarator in this production,
6704 but GCC used to allowed this, so we check and emit a sensible error
6705 message for this case. */
6706 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6708 error_at (token
->location
,
6709 "array bound forbidden after parenthesized type-id");
6710 inform (token
->location
,
6711 "try removing the parentheses around the type-id");
6712 cp_parser_direct_new_declarator (parser
);
6715 /* Otherwise, there must be a new-type-id. */
6717 type
= cp_parser_new_type_id (parser
, &nelts
);
6719 /* If the next token is a `(' or '{', then we have a new-initializer. */
6720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
6721 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6722 initializer
= cp_parser_new_initializer (parser
);
6726 /* A new-expression may not appear in an integral constant
6728 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
6729 ret
= error_mark_node
;
6732 /* Create a representation of the new-expression. */
6733 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
6734 tf_warning_or_error
);
6737 if (placement
!= NULL
)
6738 release_tree_vector (placement
);
6739 if (initializer
!= NULL
)
6740 release_tree_vector (initializer
);
6745 /* Parse a new-placement.
6750 Returns the same representation as for an expression-list. */
6752 static VEC(tree
,gc
) *
6753 cp_parser_new_placement (cp_parser
* parser
)
6755 VEC(tree
,gc
) *expression_list
;
6757 /* Parse the expression-list. */
6758 expression_list
= (cp_parser_parenthesized_expression_list
6759 (parser
, non_attr
, /*cast_p=*/false,
6760 /*allow_expansion_p=*/true,
6761 /*non_constant_p=*/NULL
));
6763 return expression_list
;
6766 /* Parse a new-type-id.
6769 type-specifier-seq new-declarator [opt]
6771 Returns the TYPE allocated. If the new-type-id indicates an array
6772 type, *NELTS is set to the number of elements in the last array
6773 bound; the TYPE will not include the last array bound. */
6776 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
6778 cp_decl_specifier_seq type_specifier_seq
;
6779 cp_declarator
*new_declarator
;
6780 cp_declarator
*declarator
;
6781 cp_declarator
*outer_declarator
;
6782 const char *saved_message
;
6784 /* The type-specifier sequence must not contain type definitions.
6785 (It cannot contain declarations of new types either, but if they
6786 are not definitions we will catch that because they are not
6788 saved_message
= parser
->type_definition_forbidden_message
;
6789 parser
->type_definition_forbidden_message
6790 = G_("types may not be defined in a new-type-id");
6791 /* Parse the type-specifier-seq. */
6792 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
6793 /*is_trailing_return=*/false,
6794 &type_specifier_seq
);
6795 /* Restore the old message. */
6796 parser
->type_definition_forbidden_message
= saved_message
;
6798 if (type_specifier_seq
.type
== error_mark_node
)
6799 return error_mark_node
;
6801 /* Parse the new-declarator. */
6802 new_declarator
= cp_parser_new_declarator_opt (parser
);
6804 /* Determine the number of elements in the last array dimension, if
6807 /* Skip down to the last array dimension. */
6808 declarator
= new_declarator
;
6809 outer_declarator
= NULL
;
6810 while (declarator
&& (declarator
->kind
== cdk_pointer
6811 || declarator
->kind
== cdk_ptrmem
))
6813 outer_declarator
= declarator
;
6814 declarator
= declarator
->declarator
;
6817 && declarator
->kind
== cdk_array
6818 && declarator
->declarator
6819 && declarator
->declarator
->kind
== cdk_array
)
6821 outer_declarator
= declarator
;
6822 declarator
= declarator
->declarator
;
6825 if (declarator
&& declarator
->kind
== cdk_array
)
6827 *nelts
= declarator
->u
.array
.bounds
;
6828 if (*nelts
== error_mark_node
)
6829 *nelts
= integer_one_node
;
6831 if (outer_declarator
)
6832 outer_declarator
->declarator
= declarator
->declarator
;
6834 new_declarator
= NULL
;
6837 return groktypename (&type_specifier_seq
, new_declarator
, false);
6840 /* Parse an (optional) new-declarator.
6843 ptr-operator new-declarator [opt]
6844 direct-new-declarator
6846 Returns the declarator. */
6848 static cp_declarator
*
6849 cp_parser_new_declarator_opt (cp_parser
* parser
)
6851 enum tree_code code
;
6853 cp_cv_quals cv_quals
;
6855 /* We don't know if there's a ptr-operator next, or not. */
6856 cp_parser_parse_tentatively (parser
);
6857 /* Look for a ptr-operator. */
6858 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
);
6859 /* If that worked, look for more new-declarators. */
6860 if (cp_parser_parse_definitely (parser
))
6862 cp_declarator
*declarator
;
6864 /* Parse another optional declarator. */
6865 declarator
= cp_parser_new_declarator_opt (parser
);
6867 return cp_parser_make_indirect_declarator
6868 (code
, type
, cv_quals
, declarator
);
6871 /* If the next token is a `[', there is a direct-new-declarator. */
6872 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6873 return cp_parser_direct_new_declarator (parser
);
6878 /* Parse a direct-new-declarator.
6880 direct-new-declarator:
6882 direct-new-declarator [constant-expression]
6886 static cp_declarator
*
6887 cp_parser_direct_new_declarator (cp_parser
* parser
)
6889 cp_declarator
*declarator
= NULL
;
6896 /* Look for the opening `['. */
6897 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
6899 token
= cp_lexer_peek_token (parser
->lexer
);
6900 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
6901 /* The standard requires that the expression have integral
6902 type. DR 74 adds enumeration types. We believe that the
6903 real intent is that these expressions be handled like the
6904 expression in a `switch' condition, which also allows
6905 classes with a single conversion to integral or
6906 enumeration type. */
6907 if (!processing_template_decl
)
6910 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
6915 error_at (token
->location
,
6916 "expression in new-declarator must have integral "
6917 "or enumeration type");
6918 expression
= error_mark_node
;
6922 /* Look for the closing `]'. */
6923 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6925 /* Add this bound to the declarator. */
6926 declarator
= make_array_declarator (declarator
, expression
);
6928 /* If the next token is not a `[', then there are no more
6930 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
6937 /* Parse a new-initializer.
6940 ( expression-list [opt] )
6943 Returns a representation of the expression-list. */
6945 static VEC(tree
,gc
) *
6946 cp_parser_new_initializer (cp_parser
* parser
)
6948 VEC(tree
,gc
) *expression_list
;
6950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6953 bool expr_non_constant_p
;
6954 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6955 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6956 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
6957 expression_list
= make_tree_vector_single (t
);
6960 expression_list
= (cp_parser_parenthesized_expression_list
6961 (parser
, non_attr
, /*cast_p=*/false,
6962 /*allow_expansion_p=*/true,
6963 /*non_constant_p=*/NULL
));
6965 return expression_list
;
6968 /* Parse a delete-expression.
6971 :: [opt] delete cast-expression
6972 :: [opt] delete [ ] cast-expression
6974 Returns a representation of the expression. */
6977 cp_parser_delete_expression (cp_parser
* parser
)
6979 bool global_scope_p
;
6983 /* Look for the optional `::' operator. */
6985 = (cp_parser_global_scope_opt (parser
,
6986 /*current_scope_valid_p=*/false)
6988 /* Look for the `delete' keyword. */
6989 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
6990 /* See if the array syntax is in use. */
6991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
6993 /* Consume the `[' token. */
6994 cp_lexer_consume_token (parser
->lexer
);
6995 /* Look for the `]' token. */
6996 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6997 /* Remember that this is the `[]' construct. */
7003 /* Parse the cast-expression. */
7004 expression
= cp_parser_simple_cast_expression (parser
);
7006 /* A delete-expression may not appear in an integral constant
7008 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7009 return error_mark_node
;
7011 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7012 tf_warning_or_error
);
7015 /* Returns true if TOKEN may start a cast-expression and false
7019 cp_parser_token_starts_cast_expression (cp_token
*token
)
7021 switch (token
->type
)
7027 case CPP_CLOSE_SQUARE
:
7028 case CPP_CLOSE_PAREN
:
7029 case CPP_CLOSE_BRACE
:
7033 case CPP_DEREF_STAR
:
7041 case CPP_GREATER_EQ
:
7061 /* '[' may start a primary-expression in obj-c++. */
7062 case CPP_OPEN_SQUARE
:
7063 return c_dialect_objc ();
7070 /* Parse a cast-expression.
7074 ( type-id ) cast-expression
7076 ADDRESS_P is true iff the unary-expression is appearing as the
7077 operand of the `&' operator. CAST_P is true if this expression is
7078 the target of a cast.
7080 Returns a representation of the expression. */
7083 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7086 /* If it's a `(', then we might be looking at a cast. */
7087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7089 tree type
= NULL_TREE
;
7090 tree expr
= NULL_TREE
;
7091 bool compound_literal_p
;
7092 const char *saved_message
;
7094 /* There's no way to know yet whether or not this is a cast.
7095 For example, `(int (3))' is a unary-expression, while `(int)
7096 3' is a cast. So, we resort to parsing tentatively. */
7097 cp_parser_parse_tentatively (parser
);
7098 /* Types may not be defined in a cast. */
7099 saved_message
= parser
->type_definition_forbidden_message
;
7100 parser
->type_definition_forbidden_message
7101 = G_("types may not be defined in casts");
7102 /* Consume the `('. */
7103 cp_lexer_consume_token (parser
->lexer
);
7104 /* A very tricky bit is that `(struct S) { 3 }' is a
7105 compound-literal (which we permit in C++ as an extension).
7106 But, that construct is not a cast-expression -- it is a
7107 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7108 is legal; if the compound-literal were a cast-expression,
7109 you'd need an extra set of parentheses.) But, if we parse
7110 the type-id, and it happens to be a class-specifier, then we
7111 will commit to the parse at that point, because we cannot
7112 undo the action that is done when creating a new class. So,
7113 then we cannot back up and do a postfix-expression.
7115 Therefore, we scan ahead to the closing `)', and check to see
7116 if the token after the `)' is a `{'. If so, we are not
7117 looking at a cast-expression.
7119 Save tokens so that we can put them back. */
7120 cp_lexer_save_tokens (parser
->lexer
);
7121 /* Skip tokens until the next token is a closing parenthesis.
7122 If we find the closing `)', and the next token is a `{', then
7123 we are looking at a compound-literal. */
7125 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7126 /*consume_paren=*/true)
7127 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
7128 /* Roll back the tokens we skipped. */
7129 cp_lexer_rollback_tokens (parser
->lexer
);
7130 /* If we were looking at a compound-literal, simulate an error
7131 so that the call to cp_parser_parse_definitely below will
7133 if (compound_literal_p
)
7134 cp_parser_simulate_error (parser
);
7137 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7138 parser
->in_type_id_in_expr_p
= true;
7139 /* Look for the type-id. */
7140 type
= cp_parser_type_id (parser
);
7141 /* Look for the closing `)'. */
7142 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7143 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7146 /* Restore the saved message. */
7147 parser
->type_definition_forbidden_message
= saved_message
;
7149 /* At this point this can only be either a cast or a
7150 parenthesized ctor such as `(T ())' that looks like a cast to
7151 function returning T. */
7152 if (!cp_parser_error_occurred (parser
)
7153 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7156 cp_parser_parse_definitely (parser
);
7157 expr
= cp_parser_cast_expression (parser
,
7158 /*address_p=*/false,
7159 /*cast_p=*/true, pidk
);
7161 /* Warn about old-style casts, if so requested. */
7162 if (warn_old_style_cast
7163 && !in_system_header
7164 && !VOID_TYPE_P (type
)
7165 && current_lang_name
!= lang_name_c
)
7166 warning (OPT_Wold_style_cast
, "use of old-style cast");
7168 /* Only type conversions to integral or enumeration types
7169 can be used in constant-expressions. */
7170 if (!cast_valid_in_integral_constant_expression_p (type
)
7171 && cp_parser_non_integral_constant_expression (parser
,
7173 return error_mark_node
;
7175 /* Perform the cast. */
7176 expr
= build_c_cast (input_location
, type
, expr
);
7180 cp_parser_abort_tentative_parse (parser
);
7183 /* If we get here, then it's not a cast, so it must be a
7184 unary-expression. */
7185 return cp_parser_unary_expression (parser
, address_p
, cast_p
, pidk
);
7188 /* Parse a binary expression of the general form:
7192 pm-expression .* cast-expression
7193 pm-expression ->* cast-expression
7195 multiplicative-expression:
7197 multiplicative-expression * pm-expression
7198 multiplicative-expression / pm-expression
7199 multiplicative-expression % pm-expression
7201 additive-expression:
7202 multiplicative-expression
7203 additive-expression + multiplicative-expression
7204 additive-expression - multiplicative-expression
7208 shift-expression << additive-expression
7209 shift-expression >> additive-expression
7211 relational-expression:
7213 relational-expression < shift-expression
7214 relational-expression > shift-expression
7215 relational-expression <= shift-expression
7216 relational-expression >= shift-expression
7220 relational-expression:
7221 relational-expression <? shift-expression
7222 relational-expression >? shift-expression
7224 equality-expression:
7225 relational-expression
7226 equality-expression == relational-expression
7227 equality-expression != relational-expression
7231 and-expression & equality-expression
7233 exclusive-or-expression:
7235 exclusive-or-expression ^ and-expression
7237 inclusive-or-expression:
7238 exclusive-or-expression
7239 inclusive-or-expression | exclusive-or-expression
7241 logical-and-expression:
7242 inclusive-or-expression
7243 logical-and-expression && inclusive-or-expression
7245 logical-or-expression:
7246 logical-and-expression
7247 logical-or-expression || logical-and-expression
7249 All these are implemented with a single function like:
7252 simple-cast-expression
7253 binary-expression <token> binary-expression
7255 CAST_P is true if this expression is the target of a cast.
7257 The binops_by_token map is used to get the tree codes for each <token> type.
7258 binary-expressions are associated according to a precedence table. */
7260 #define TOKEN_PRECEDENCE(token) \
7261 (((token->type == CPP_GREATER \
7262 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7263 && !parser->greater_than_is_operator_p) \
7264 ? PREC_NOT_OPERATOR \
7265 : binops_by_token[token->type].prec)
7268 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
7269 bool no_toplevel_fold_p
,
7270 enum cp_parser_prec prec
,
7273 cp_parser_expression_stack stack
;
7274 cp_parser_expression_stack_entry
*sp
= &stack
[0];
7275 cp_parser_expression_stack_entry current
;
7278 enum tree_code rhs_type
;
7279 enum cp_parser_prec new_prec
, lookahead_prec
;
7282 /* Parse the first expression. */
7283 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
7285 current
.lhs_type
= ERROR_MARK
;
7286 current
.prec
= prec
;
7288 if (cp_parser_error_occurred (parser
))
7289 return error_mark_node
;
7293 /* Get an operator token. */
7294 token
= cp_lexer_peek_token (parser
->lexer
);
7296 if (warn_cxx0x_compat
7297 && token
->type
== CPP_RSHIFT
7298 && !parser
->greater_than_is_operator_p
)
7300 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
7301 "%<>>%> operator is treated"
7302 " as two right angle brackets in C++11"))
7303 inform (token
->location
,
7304 "suggest parentheses around %<>>%> expression");
7307 new_prec
= TOKEN_PRECEDENCE (token
);
7309 /* Popping an entry off the stack means we completed a subexpression:
7310 - either we found a token which is not an operator (`>' where it is not
7311 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7312 will happen repeatedly;
7313 - or, we found an operator which has lower priority. This is the case
7314 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7316 if (new_prec
<= current
.prec
)
7325 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
7326 current
.loc
= token
->location
;
7328 /* We used the operator token. */
7329 cp_lexer_consume_token (parser
->lexer
);
7331 /* For "false && x" or "true || x", x will never be executed;
7332 disable warnings while evaluating it. */
7333 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7334 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
7335 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7336 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
7338 /* Extract another operand. It may be the RHS of this expression
7339 or the LHS of a new, higher priority expression. */
7340 rhs
= cp_parser_simple_cast_expression (parser
);
7341 rhs_type
= ERROR_MARK
;
7343 /* Get another operator token. Look up its precedence to avoid
7344 building a useless (immediately popped) stack entry for common
7345 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7346 token
= cp_lexer_peek_token (parser
->lexer
);
7347 lookahead_prec
= TOKEN_PRECEDENCE (token
);
7348 if (lookahead_prec
> new_prec
)
7350 /* ... and prepare to parse the RHS of the new, higher priority
7351 expression. Since precedence levels on the stack are
7352 monotonically increasing, we do not have to care about
7357 current
.lhs_type
= rhs_type
;
7358 current
.prec
= new_prec
;
7359 new_prec
= lookahead_prec
;
7363 lookahead_prec
= new_prec
;
7364 /* If the stack is not empty, we have parsed into LHS the right side
7365 (`4' in the example above) of an expression we had suspended.
7366 We can use the information on the stack to recover the LHS (`3')
7367 from the stack together with the tree code (`MULT_EXPR'), and
7368 the precedence of the higher level subexpression
7369 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7370 which will be used to actually build the additive expression. */
7372 rhs_type
= current
.lhs_type
;
7377 /* Undo the disabling of warnings done above. */
7378 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
7379 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
7380 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
7381 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
7384 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7385 ERROR_MARK for everything that is not a binary expression.
7386 This makes warn_about_parentheses miss some warnings that
7387 involve unary operators. For unary expressions we should
7388 pass the correct tree_code unless the unary expression was
7389 surrounded by parentheses.
7391 if (no_toplevel_fold_p
7392 && lookahead_prec
<= current
.prec
7394 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
)
7395 current
.lhs
= build2 (current
.tree_type
, boolean_type_node
,
7398 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
7399 current
.lhs
, current
.lhs_type
,
7400 rhs
, rhs_type
, &overload
,
7401 tf_warning_or_error
);
7402 current
.lhs_type
= current
.tree_type
;
7404 /* If the binary operator required the use of an overloaded operator,
7405 then this expression cannot be an integral constant-expression.
7406 An overloaded operator can be used even if both operands are
7407 otherwise permissible in an integral constant-expression if at
7408 least one of the operands is of enumeration type. */
7411 && cp_parser_non_integral_constant_expression (parser
,
7413 return error_mark_node
;
7420 /* Parse the `? expression : assignment-expression' part of a
7421 conditional-expression. The LOGICAL_OR_EXPR is the
7422 logical-or-expression that started the conditional-expression.
7423 Returns a representation of the entire conditional-expression.
7425 This routine is used by cp_parser_assignment_expression.
7427 ? expression : assignment-expression
7431 ? : assignment-expression */
7434 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
7437 tree assignment_expr
;
7438 struct cp_token
*token
;
7439 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7441 /* Consume the `?' token. */
7442 cp_lexer_consume_token (parser
->lexer
);
7443 token
= cp_lexer_peek_token (parser
->lexer
);
7444 if (cp_parser_allow_gnu_extensions_p (parser
)
7445 && token
->type
== CPP_COLON
)
7447 pedwarn (token
->location
, OPT_Wpedantic
,
7448 "ISO C++ does not allow ?: with omitted middle operand");
7449 /* Implicit true clause. */
7451 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
7452 warn_for_omitted_condop (token
->location
, logical_or_expr
);
7456 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
7457 parser
->colon_corrects_to_scope_p
= false;
7458 /* Parse the expression. */
7459 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
7460 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
7461 c_inhibit_evaluation_warnings
+=
7462 ((logical_or_expr
== truthvalue_true_node
)
7463 - (logical_or_expr
== truthvalue_false_node
));
7464 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
7467 /* The next token should be a `:'. */
7468 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
7469 /* Parse the assignment-expression. */
7470 assignment_expr
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7471 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
7473 /* Build the conditional-expression. */
7474 return build_x_conditional_expr (loc
, logical_or_expr
,
7477 tf_warning_or_error
);
7480 /* Parse an assignment-expression.
7482 assignment-expression:
7483 conditional-expression
7484 logical-or-expression assignment-operator assignment_expression
7487 CAST_P is true if this expression is the target of a cast.
7489 Returns a representation for the expression. */
7492 cp_parser_assignment_expression (cp_parser
* parser
, bool cast_p
,
7497 /* If the next token is the `throw' keyword, then we're looking at
7498 a throw-expression. */
7499 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
7500 expr
= cp_parser_throw_expression (parser
);
7501 /* Otherwise, it must be that we are looking at a
7502 logical-or-expression. */
7505 /* Parse the binary expressions (logical-or-expression). */
7506 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
7507 PREC_NOT_OPERATOR
, pidk
);
7508 /* If the next token is a `?' then we're actually looking at a
7509 conditional-expression. */
7510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
7511 return cp_parser_question_colon_clause (parser
, expr
);
7514 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7516 /* If it's an assignment-operator, we're using the second
7518 enum tree_code assignment_operator
7519 = cp_parser_assignment_operator_opt (parser
);
7520 if (assignment_operator
!= ERROR_MARK
)
7522 bool non_constant_p
;
7523 location_t saved_input_location
;
7525 /* Parse the right-hand side of the assignment. */
7526 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
7528 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7529 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7531 /* An assignment may not appear in a
7532 constant-expression. */
7533 if (cp_parser_non_integral_constant_expression (parser
,
7535 return error_mark_node
;
7536 /* Build the assignment expression. Its default
7537 location is the location of the '=' token. */
7538 saved_input_location
= input_location
;
7539 input_location
= loc
;
7540 expr
= build_x_modify_expr (loc
, expr
,
7541 assignment_operator
,
7543 tf_warning_or_error
);
7544 input_location
= saved_input_location
;
7552 /* Parse an (optional) assignment-operator.
7554 assignment-operator: one of
7555 = *= /= %= += -= >>= <<= &= ^= |=
7559 assignment-operator: one of
7562 If the next token is an assignment operator, the corresponding tree
7563 code is returned, and the token is consumed. For example, for
7564 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7565 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7566 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7567 operator, ERROR_MARK is returned. */
7569 static enum tree_code
7570 cp_parser_assignment_operator_opt (cp_parser
* parser
)
7575 /* Peek at the next token. */
7576 token
= cp_lexer_peek_token (parser
->lexer
);
7578 switch (token
->type
)
7589 op
= TRUNC_DIV_EXPR
;
7593 op
= TRUNC_MOD_EXPR
;
7625 /* Nothing else is an assignment operator. */
7629 /* If it was an assignment operator, consume it. */
7630 if (op
!= ERROR_MARK
)
7631 cp_lexer_consume_token (parser
->lexer
);
7636 /* Parse an expression.
7639 assignment-expression
7640 expression , assignment-expression
7642 CAST_P is true if this expression is the target of a cast.
7644 Returns a representation of the expression. */
7647 cp_parser_expression (cp_parser
* parser
, bool cast_p
, cp_id_kind
* pidk
)
7649 tree expression
= NULL_TREE
;
7650 location_t loc
= UNKNOWN_LOCATION
;
7654 tree assignment_expression
;
7656 /* Parse the next assignment-expression. */
7657 assignment_expression
7658 = cp_parser_assignment_expression (parser
, cast_p
, pidk
);
7659 /* If this is the first assignment-expression, we can just
7662 expression
= assignment_expression
;
7664 expression
= build_x_compound_expr (loc
, expression
,
7665 assignment_expression
,
7666 tf_warning_or_error
);
7667 /* If the next token is not a comma, then we are done with the
7669 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7671 /* Consume the `,'. */
7672 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7673 cp_lexer_consume_token (parser
->lexer
);
7674 /* A comma operator cannot appear in a constant-expression. */
7675 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
7676 expression
= error_mark_node
;
7682 /* Parse a constant-expression.
7684 constant-expression:
7685 conditional-expression
7687 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7688 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7689 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7690 is false, NON_CONSTANT_P should be NULL. */
7693 cp_parser_constant_expression (cp_parser
* parser
,
7694 bool allow_non_constant_p
,
7695 bool *non_constant_p
)
7697 bool saved_integral_constant_expression_p
;
7698 bool saved_allow_non_integral_constant_expression_p
;
7699 bool saved_non_integral_constant_expression_p
;
7702 /* It might seem that we could simply parse the
7703 conditional-expression, and then check to see if it were
7704 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7705 one that the compiler can figure out is constant, possibly after
7706 doing some simplifications or optimizations. The standard has a
7707 precise definition of constant-expression, and we must honor
7708 that, even though it is somewhat more restrictive.
7714 is not a legal declaration, because `(2, 3)' is not a
7715 constant-expression. The `,' operator is forbidden in a
7716 constant-expression. However, GCC's constant-folding machinery
7717 will fold this operation to an INTEGER_CST for `3'. */
7719 /* Save the old settings. */
7720 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
7721 saved_allow_non_integral_constant_expression_p
7722 = parser
->allow_non_integral_constant_expression_p
;
7723 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
7724 /* We are now parsing a constant-expression. */
7725 parser
->integral_constant_expression_p
= true;
7726 parser
->allow_non_integral_constant_expression_p
7727 = (allow_non_constant_p
|| cxx_dialect
>= cxx0x
);
7728 parser
->non_integral_constant_expression_p
= false;
7729 /* Although the grammar says "conditional-expression", we parse an
7730 "assignment-expression", which also permits "throw-expression"
7731 and the use of assignment operators. In the case that
7732 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7733 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7734 actually essential that we look for an assignment-expression.
7735 For example, cp_parser_initializer_clauses uses this function to
7736 determine whether a particular assignment-expression is in fact
7738 expression
= cp_parser_assignment_expression (parser
, /*cast_p=*/false, NULL
);
7739 /* Restore the old settings. */
7740 parser
->integral_constant_expression_p
7741 = saved_integral_constant_expression_p
;
7742 parser
->allow_non_integral_constant_expression_p
7743 = saved_allow_non_integral_constant_expression_p
;
7744 if (cxx_dialect
>= cxx0x
)
7746 /* Require an rvalue constant expression here; that's what our
7747 callers expect. Reference constant expressions are handled
7748 separately in e.g. cp_parser_template_argument. */
7749 bool is_const
= potential_rvalue_constant_expression (expression
);
7750 parser
->non_integral_constant_expression_p
= !is_const
;
7751 if (!is_const
&& !allow_non_constant_p
)
7752 require_potential_rvalue_constant_expression (expression
);
7754 if (allow_non_constant_p
)
7755 *non_constant_p
= parser
->non_integral_constant_expression_p
;
7756 parser
->non_integral_constant_expression_p
7757 = saved_non_integral_constant_expression_p
;
7762 /* Parse __builtin_offsetof.
7764 offsetof-expression:
7765 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7767 offsetof-member-designator:
7769 | offsetof-member-designator "." id-expression
7770 | offsetof-member-designator "[" expression "]"
7771 | offsetof-member-designator "->" id-expression */
7774 cp_parser_builtin_offsetof (cp_parser
*parser
)
7776 int save_ice_p
, save_non_ice_p
;
7781 /* We're about to accept non-integral-constant things, but will
7782 definitely yield an integral constant expression. Save and
7783 restore these values around our local parsing. */
7784 save_ice_p
= parser
->integral_constant_expression_p
;
7785 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
7787 /* Consume the "__builtin_offsetof" token. */
7788 cp_lexer_consume_token (parser
->lexer
);
7789 /* Consume the opening `('. */
7790 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7791 /* Parse the type-id. */
7792 type
= cp_parser_type_id (parser
);
7793 /* Look for the `,'. */
7794 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7795 token
= cp_lexer_peek_token (parser
->lexer
);
7797 /* Build the (type *)null that begins the traditional offsetof macro. */
7798 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
7799 tf_warning_or_error
);
7801 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7802 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
7803 true, &dummy
, token
->location
);
7806 token
= cp_lexer_peek_token (parser
->lexer
);
7807 switch (token
->type
)
7809 case CPP_OPEN_SQUARE
:
7810 /* offsetof-member-designator "[" expression "]" */
7811 expr
= cp_parser_postfix_open_square_expression (parser
, expr
, true);
7815 /* offsetof-member-designator "->" identifier */
7816 expr
= grok_array_decl (token
->location
, expr
, integer_zero_node
);
7820 /* offsetof-member-designator "." identifier */
7821 cp_lexer_consume_token (parser
->lexer
);
7822 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
7827 case CPP_CLOSE_PAREN
:
7828 /* Consume the ")" token. */
7829 cp_lexer_consume_token (parser
->lexer
);
7833 /* Error. We know the following require will fail, but
7834 that gives the proper error message. */
7835 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7836 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
7837 expr
= error_mark_node
;
7843 /* If we're processing a template, we can't finish the semantics yet.
7844 Otherwise we can fold the entire expression now. */
7845 if (processing_template_decl
)
7846 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
7848 expr
= finish_offsetof (expr
);
7851 parser
->integral_constant_expression_p
= save_ice_p
;
7852 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
7857 /* Parse a trait expression.
7859 Returns a representation of the expression, the underlying type
7860 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7863 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
7866 tree type1
, type2
= NULL_TREE
;
7867 bool binary
= false;
7868 cp_decl_specifier_seq decl_specs
;
7872 case RID_HAS_NOTHROW_ASSIGN
:
7873 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
7875 case RID_HAS_NOTHROW_CONSTRUCTOR
:
7876 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
7878 case RID_HAS_NOTHROW_COPY
:
7879 kind
= CPTK_HAS_NOTHROW_COPY
;
7881 case RID_HAS_TRIVIAL_ASSIGN
:
7882 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
7884 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
7885 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
7887 case RID_HAS_TRIVIAL_COPY
:
7888 kind
= CPTK_HAS_TRIVIAL_COPY
;
7890 case RID_HAS_TRIVIAL_DESTRUCTOR
:
7891 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
7893 case RID_HAS_VIRTUAL_DESTRUCTOR
:
7894 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
7896 case RID_IS_ABSTRACT
:
7897 kind
= CPTK_IS_ABSTRACT
;
7899 case RID_IS_BASE_OF
:
7900 kind
= CPTK_IS_BASE_OF
;
7904 kind
= CPTK_IS_CLASS
;
7906 case RID_IS_CONVERTIBLE_TO
:
7907 kind
= CPTK_IS_CONVERTIBLE_TO
;
7911 kind
= CPTK_IS_EMPTY
;
7914 kind
= CPTK_IS_ENUM
;
7917 kind
= CPTK_IS_FINAL
;
7919 case RID_IS_LITERAL_TYPE
:
7920 kind
= CPTK_IS_LITERAL_TYPE
;
7925 case RID_IS_POLYMORPHIC
:
7926 kind
= CPTK_IS_POLYMORPHIC
;
7928 case RID_IS_STD_LAYOUT
:
7929 kind
= CPTK_IS_STD_LAYOUT
;
7931 case RID_IS_TRIVIAL
:
7932 kind
= CPTK_IS_TRIVIAL
;
7935 kind
= CPTK_IS_UNION
;
7937 case RID_UNDERLYING_TYPE
:
7938 kind
= CPTK_UNDERLYING_TYPE
;
7943 case RID_DIRECT_BASES
:
7944 kind
= CPTK_DIRECT_BASES
;
7950 /* Consume the token. */
7951 cp_lexer_consume_token (parser
->lexer
);
7953 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7955 type1
= cp_parser_type_id (parser
);
7957 if (type1
== error_mark_node
)
7958 return error_mark_node
;
7960 /* Build a trivial decl-specifier-seq. */
7961 clear_decl_specs (&decl_specs
);
7962 decl_specs
.type
= type1
;
7964 /* Call grokdeclarator to figure out what type this is. */
7965 type1
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7966 /*initialized=*/0, /*attrlist=*/NULL
);
7970 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7972 type2
= cp_parser_type_id (parser
);
7974 if (type2
== error_mark_node
)
7975 return error_mark_node
;
7977 /* Build a trivial decl-specifier-seq. */
7978 clear_decl_specs (&decl_specs
);
7979 decl_specs
.type
= type2
;
7981 /* Call grokdeclarator to figure out what type this is. */
7982 type2
= grokdeclarator (NULL
, &decl_specs
, TYPENAME
,
7983 /*initialized=*/0, /*attrlist=*/NULL
);
7986 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7988 /* Complete the trait expression, which may mean either processing
7989 the trait expr now or saving it for template instantiation. */
7992 case CPTK_UNDERLYING_TYPE
:
7993 return finish_underlying_type (type1
);
7995 return finish_bases (type1
, false);
7996 case CPTK_DIRECT_BASES
:
7997 return finish_bases (type1
, true);
7999 return finish_trait_expr (kind
, type1
, type2
);
8003 /* Lambdas that appear in variable initializer or default argument scope
8004 get that in their mangling, so we need to record it. We might as well
8005 use the count for function and namespace scopes as well. */
8006 static GTY(()) tree lambda_scope
;
8007 static GTY(()) int lambda_count
;
8008 typedef struct GTY(()) tree_int
8013 DEF_VEC_O(tree_int
);
8014 DEF_VEC_ALLOC_O(tree_int
,gc
);
8015 static GTY(()) VEC(tree_int
,gc
) *lambda_scope_stack
;
8018 start_lambda_scope (tree decl
)
8022 /* Once we're inside a function, we ignore other scopes and just push
8023 the function again so that popping works properly. */
8024 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8025 decl
= current_function_decl
;
8026 ti
.t
= lambda_scope
;
8027 ti
.i
= lambda_count
;
8028 VEC_safe_push (tree_int
, gc
, lambda_scope_stack
, ti
);
8029 if (lambda_scope
!= decl
)
8031 /* Don't reset the count if we're still in the same function. */
8032 lambda_scope
= decl
;
8038 record_lambda_scope (tree lambda
)
8040 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8041 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8045 finish_lambda_scope (void)
8047 tree_int
*p
= &VEC_last (tree_int
, lambda_scope_stack
);
8048 if (lambda_scope
!= p
->t
)
8050 lambda_scope
= p
->t
;
8051 lambda_count
= p
->i
;
8053 VEC_pop (tree_int
, lambda_scope_stack
);
8056 /* Parse a lambda expression.
8059 lambda-introducer lambda-declarator [opt] compound-statement
8061 Returns a representation of the expression. */
8064 cp_parser_lambda_expression (cp_parser
* parser
)
8066 tree lambda_expr
= build_lambda_expr ();
8070 LAMBDA_EXPR_LOCATION (lambda_expr
)
8071 = cp_lexer_peek_token (parser
->lexer
)->location
;
8073 if (cp_unevaluated_operand
)
8074 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8075 "lambda-expression in unevaluated context");
8077 /* We may be in the middle of deferred access check. Disable
8079 push_deferring_access_checks (dk_no_deferred
);
8081 cp_parser_lambda_introducer (parser
, lambda_expr
);
8083 type
= begin_lambda_type (lambda_expr
);
8084 if (type
== error_mark_node
)
8085 return error_mark_node
;
8087 record_lambda_scope (lambda_expr
);
8089 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8090 determine_visibility (TYPE_NAME (type
));
8092 /* Now that we've started the type, add the capture fields for any
8093 explicit captures. */
8094 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8097 /* Inside the class, surrounding template-parameter-lists do not apply. */
8098 unsigned int saved_num_template_parameter_lists
8099 = parser
->num_template_parameter_lists
;
8100 unsigned char in_statement
= parser
->in_statement
;
8101 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
8103 parser
->num_template_parameter_lists
= 0;
8104 parser
->in_statement
= 0;
8105 parser
->in_switch_statement_p
= false;
8107 /* By virtue of defining a local class, a lambda expression has access to
8108 the private variables of enclosing classes. */
8110 ok
= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
8113 cp_parser_lambda_body (parser
, lambda_expr
);
8114 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8115 cp_parser_skip_to_end_of_block_or_statement (parser
);
8117 /* The capture list was built up in reverse order; fix that now. */
8119 tree newlist
= NULL_TREE
;
8122 for (elt
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
);
8125 next
= TREE_CHAIN (elt
);
8126 TREE_CHAIN (elt
) = newlist
;
8129 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
) = newlist
;
8133 maybe_add_lambda_conv_op (type
);
8135 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
8137 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
8138 parser
->in_statement
= in_statement
;
8139 parser
->in_switch_statement_p
= in_switch_statement_p
;
8142 pop_deferring_access_checks ();
8144 /* This field is only used during parsing of the lambda. */
8145 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
8147 /* This lambda shouldn't have any proxies left at this point. */
8148 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
8149 /* And now that we're done, push proxies for an enclosing lambda. */
8150 insert_pending_capture_proxies ();
8153 return build_lambda_object (lambda_expr
);
8155 return error_mark_node
;
8158 /* Parse the beginning of a lambda expression.
8161 [ lambda-capture [opt] ]
8163 LAMBDA_EXPR is the current representation of the lambda expression. */
8166 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
8168 /* Need commas after the first capture. */
8171 /* Eat the leading `['. */
8172 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8174 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
8176 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
8177 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
8178 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8179 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
8181 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
8183 cp_lexer_consume_token (parser
->lexer
);
8187 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
8189 cp_token
* capture_token
;
8191 tree capture_init_expr
;
8192 cp_id_kind idk
= CP_ID_KIND_NONE
;
8193 bool explicit_init_p
= false;
8195 enum capture_kind_type
8200 enum capture_kind_type capture_kind
= BY_COPY
;
8202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
8204 error ("expected end of capture-list");
8211 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8213 /* Possibly capture `this'. */
8214 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
8216 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8217 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
8218 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
8219 "with by-copy capture default");
8220 cp_lexer_consume_token (parser
->lexer
);
8221 add_capture (lambda_expr
,
8222 /*id=*/this_identifier
,
8223 /*initializer=*/finish_this_expr(),
8224 /*by_reference_p=*/false,
8229 /* Remember whether we want to capture as a reference or not. */
8230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
8232 capture_kind
= BY_REFERENCE
;
8233 cp_lexer_consume_token (parser
->lexer
);
8236 /* Get the identifier. */
8237 capture_token
= cp_lexer_peek_token (parser
->lexer
);
8238 capture_id
= cp_parser_identifier (parser
);
8240 if (capture_id
== error_mark_node
)
8241 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8242 delimiters, but I modified this to stop on unnested ']' as well. It
8243 was already changed to stop on unnested '}', so the
8244 "closing_parenthesis" name is no more misleading with my change. */
8246 cp_parser_skip_to_closing_parenthesis (parser
,
8247 /*recovering=*/true,
8249 /*consume_paren=*/true);
8253 /* Find the initializer for this capture. */
8254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
8256 /* An explicit expression exists. */
8257 cp_lexer_consume_token (parser
->lexer
);
8258 pedwarn (input_location
, OPT_Wpedantic
,
8259 "ISO C++ does not allow initializers "
8260 "in lambda expression capture lists");
8261 capture_init_expr
= cp_parser_assignment_expression (parser
,
8264 explicit_init_p
= true;
8268 const char* error_msg
;
8270 /* Turn the identifier into an id-expression. */
8272 = cp_parser_lookup_name
8276 /*is_template=*/false,
8277 /*is_namespace=*/false,
8278 /*check_dependency=*/true,
8279 /*ambiguous_decls=*/NULL
,
8280 capture_token
->location
);
8282 if (capture_init_expr
== error_mark_node
)
8284 unqualified_name_lookup_error (capture_id
);
8287 else if (DECL_P (capture_init_expr
)
8288 && (TREE_CODE (capture_init_expr
) != VAR_DECL
8289 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
8291 error_at (capture_token
->location
,
8292 "capture of non-variable %qD ",
8294 inform (0, "%q+#D declared here", capture_init_expr
);
8297 if (TREE_CODE (capture_init_expr
) == VAR_DECL
8298 && decl_storage_duration (capture_init_expr
) != dk_auto
)
8300 pedwarn (capture_token
->location
, 0, "capture of variable "
8301 "%qD with non-automatic storage duration",
8303 inform (0, "%q+#D declared here", capture_init_expr
);
8308 = finish_id_expression
8313 /*integral_constant_expression_p=*/false,
8314 /*allow_non_integral_constant_expression_p=*/false,
8315 /*non_integral_constant_expression_p=*/NULL
,
8316 /*template_p=*/false,
8318 /*address_p=*/false,
8319 /*template_arg_p=*/false,
8321 capture_token
->location
);
8324 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
8325 && !explicit_init_p
)
8327 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
8328 && capture_kind
== BY_COPY
)
8329 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
8330 "of %qD redundant with by-copy capture default",
8332 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
8333 && capture_kind
== BY_REFERENCE
)
8334 pedwarn (capture_token
->location
, 0, "explicit by-reference "
8335 "capture of %qD redundant with by-reference capture "
8336 "default", capture_id
);
8339 add_capture (lambda_expr
,
8342 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
8346 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8349 /* Parse the (optional) middle of a lambda expression.
8352 ( parameter-declaration-clause [opt] )
8353 attribute-specifier [opt]
8355 exception-specification [opt]
8356 lambda-return-type-clause [opt]
8358 LAMBDA_EXPR is the current representation of the lambda expression. */
8361 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
8363 /* 5.1.1.4 of the standard says:
8364 If a lambda-expression does not include a lambda-declarator, it is as if
8365 the lambda-declarator were ().
8366 This means an empty parameter list, no attributes, and no exception
8368 tree param_list
= void_list_node
;
8369 tree attributes
= NULL_TREE
;
8370 tree exception_spec
= NULL_TREE
;
8373 /* The lambda-declarator is optional, but must begin with an opening
8374 parenthesis if present. */
8375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8377 cp_lexer_consume_token (parser
->lexer
);
8379 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
8381 /* Parse parameters. */
8382 param_list
= cp_parser_parameter_declaration_clause (parser
);
8384 /* Default arguments shall not be specified in the
8385 parameter-declaration-clause of a lambda-declarator. */
8386 for (t
= param_list
; t
; t
= TREE_CHAIN (t
))
8387 if (TREE_PURPOSE (t
))
8388 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
8389 "default argument specified for lambda parameter");
8391 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8393 attributes
= cp_parser_attributes_opt (parser
);
8395 /* Parse optional `mutable' keyword. */
8396 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
8398 cp_lexer_consume_token (parser
->lexer
);
8399 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
8402 /* Parse optional exception specification. */
8403 exception_spec
= cp_parser_exception_specification_opt (parser
);
8405 /* Parse optional trailing return type. */
8406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
8408 cp_lexer_consume_token (parser
->lexer
);
8409 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
) = cp_parser_type_id (parser
);
8412 /* The function parameters must be in scope all the way until after the
8413 trailing-return-type in case of decltype. */
8414 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
8415 pop_binding (DECL_NAME (t
), t
);
8420 /* Create the function call operator.
8422 Messing with declarators like this is no uglier than building up the
8423 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8426 cp_decl_specifier_seq return_type_specs
;
8427 cp_declarator
* declarator
;
8432 clear_decl_specs (&return_type_specs
);
8433 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
8434 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
8436 /* Maybe we will deduce the return type later. */
8437 return_type_specs
.type
= make_auto ();
8439 p
= obstack_alloc (&declarator_obstack
, 0);
8441 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
8444 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
8445 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
8446 declarator
= make_call_declarator (declarator
, param_list
, quals
,
8447 VIRT_SPEC_UNSPECIFIED
,
8449 /*late_return_type=*/NULL_TREE
);
8450 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
8452 fco
= grokmethod (&return_type_specs
,
8455 if (fco
!= error_mark_node
)
8457 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
8458 DECL_ARTIFICIAL (fco
) = 1;
8459 /* Give the object parameter a different name. */
8460 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
8463 finish_member_declaration (fco
);
8465 obstack_free (&declarator_obstack
, p
);
8467 return (fco
!= error_mark_node
);
8471 /* Parse the body of a lambda expression, which is simply
8475 but which requires special handling.
8476 LAMBDA_EXPR is the current representation of the lambda expression. */
8479 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
8481 bool nested
= (current_function_decl
!= NULL_TREE
);
8482 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
8484 push_function_context ();
8486 /* Still increment function_depth so that we don't GC in the
8487 middle of an expression. */
8489 /* Clear this in case we're in the middle of a default argument. */
8490 parser
->local_variables_forbidden_p
= false;
8492 /* Finish the function call operator
8494 + late_parsing_for_member
8495 + function_definition_after_declarator
8496 + ctor_initializer_opt_and_function_body */
8498 tree fco
= lambda_function (lambda_expr
);
8504 /* Let the front end know that we are going to be defining this
8506 start_preparsed_function (fco
,
8508 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
8510 start_lambda_scope (fco
);
8511 body
= begin_function_body ();
8513 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8516 /* Push the proxies for any explicit captures. */
8517 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
8518 cap
= TREE_CHAIN (cap
))
8519 build_capture_proxy (TREE_PURPOSE (cap
));
8521 compound_stmt
= begin_compound_stmt (0);
8523 /* 5.1.1.4 of the standard says:
8524 If a lambda-expression does not include a trailing-return-type, it
8525 is as if the trailing-return-type denotes the following type:
8526 * if the compound-statement is of the form
8527 { return attribute-specifier [opt] expression ; }
8528 the type of the returned expression after lvalue-to-rvalue
8529 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8530 (_conv.array_ 4.2), and function-to-pointer conversion
8532 * otherwise, void. */
8534 /* In a lambda that has neither a lambda-return-type-clause
8535 nor a deducible form, errors should be reported for return statements
8536 in the body. Since we used void as the placeholder return type, parsing
8537 the body as usual will give such desired behavior. */
8538 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
8539 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
8540 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
8542 tree expr
= NULL_TREE
;
8543 cp_id_kind idk
= CP_ID_KIND_NONE
;
8545 /* Parse tentatively in case there's more after the initial return
8547 cp_parser_parse_tentatively (parser
);
8549 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
8551 expr
= cp_parser_expression (parser
, /*cast_p=*/false, &idk
);
8553 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
8554 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8556 if (cp_parser_parse_definitely (parser
))
8558 if (!processing_template_decl
)
8559 apply_deduced_return_type (fco
, lambda_return_type (expr
));
8561 /* Will get error here if type not deduced yet. */
8562 finish_return_stmt (expr
);
8570 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8571 cp_parser_label_declaration (parser
);
8572 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
8573 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8576 finish_compound_stmt (compound_stmt
);
8579 finish_function_body (body
);
8580 finish_lambda_scope ();
8582 /* Finish the function and generate code for it if necessary. */
8583 expand_or_defer_fn (finish_function (/*inline*/2));
8586 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
8588 pop_function_context();
8593 /* Statements [gram.stmt.stmt] */
8595 /* Parse a statement.
8599 expression-statement
8604 declaration-statement
8612 IN_COMPOUND is true when the statement is nested inside a
8613 cp_parser_compound_statement; this matters for certain pragmas.
8615 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8616 is a (possibly labeled) if statement which is not enclosed in braces
8617 and has an else clause. This is used to implement -Wparentheses. */
8620 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
8621 bool in_compound
, bool *if_p
)
8625 location_t statement_location
;
8630 /* There is no statement yet. */
8631 statement
= NULL_TREE
;
8632 /* Peek at the next token. */
8633 token
= cp_lexer_peek_token (parser
->lexer
);
8634 /* Remember the location of the first token in the statement. */
8635 statement_location
= token
->location
;
8636 /* If this is a keyword, then that will often determine what kind of
8637 statement we have. */
8638 if (token
->type
== CPP_KEYWORD
)
8640 enum rid keyword
= token
->keyword
;
8646 /* Looks like a labeled-statement with a case label.
8647 Parse the label, and then use tail recursion to parse
8649 cp_parser_label_for_labeled_statement (parser
);
8654 statement
= cp_parser_selection_statement (parser
, if_p
);
8660 statement
= cp_parser_iteration_statement (parser
);
8667 statement
= cp_parser_jump_statement (parser
);
8670 /* Objective-C++ exception-handling constructs. */
8673 case RID_AT_FINALLY
:
8674 case RID_AT_SYNCHRONIZED
:
8676 statement
= cp_parser_objc_statement (parser
);
8680 statement
= cp_parser_try_block (parser
);
8684 /* This must be a namespace alias definition. */
8685 cp_parser_declaration_statement (parser
);
8688 case RID_TRANSACTION_ATOMIC
:
8689 case RID_TRANSACTION_RELAXED
:
8690 statement
= cp_parser_transaction (parser
, keyword
);
8692 case RID_TRANSACTION_CANCEL
:
8693 statement
= cp_parser_transaction_cancel (parser
);
8697 /* It might be a keyword like `int' that can start a
8698 declaration-statement. */
8702 else if (token
->type
== CPP_NAME
)
8704 /* If the next token is a `:', then we are looking at a
8705 labeled-statement. */
8706 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
8707 if (token
->type
== CPP_COLON
)
8709 /* Looks like a labeled-statement with an ordinary label.
8710 Parse the label, and then use tail recursion to parse
8712 cp_parser_label_for_labeled_statement (parser
);
8716 /* Anything that starts with a `{' must be a compound-statement. */
8717 else if (token
->type
== CPP_OPEN_BRACE
)
8718 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
8719 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8720 a statement all its own. */
8721 else if (token
->type
== CPP_PRAGMA
)
8723 /* Only certain OpenMP pragmas are attached to statements, and thus
8724 are considered statements themselves. All others are not. In
8725 the context of a compound, accept the pragma as a "statement" and
8726 return so that we can check for a close brace. Otherwise we
8727 require a real statement and must go back and read one. */
8729 cp_parser_pragma (parser
, pragma_compound
);
8730 else if (!cp_parser_pragma (parser
, pragma_stmt
))
8734 else if (token
->type
== CPP_EOF
)
8736 cp_parser_error (parser
, "expected statement");
8740 /* Everything else must be a declaration-statement or an
8741 expression-statement. Try for the declaration-statement
8742 first, unless we are looking at a `;', in which case we know that
8743 we have an expression-statement. */
8746 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8748 cp_parser_parse_tentatively (parser
);
8749 /* Try to parse the declaration-statement. */
8750 cp_parser_declaration_statement (parser
);
8751 /* If that worked, we're done. */
8752 if (cp_parser_parse_definitely (parser
))
8755 /* Look for an expression-statement instead. */
8756 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
8759 /* Set the line number for the statement. */
8760 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
8761 SET_EXPR_LOCATION (statement
, statement_location
);
8764 /* Parse the label for a labeled-statement, i.e.
8767 case constant-expression :
8771 case constant-expression ... constant-expression : statement
8773 When a label is parsed without errors, the label is added to the
8774 parse tree by the finish_* functions, so this function doesn't
8775 have to return the label. */
8778 cp_parser_label_for_labeled_statement (cp_parser
* parser
)
8781 tree label
= NULL_TREE
;
8782 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8784 /* The next token should be an identifier. */
8785 token
= cp_lexer_peek_token (parser
->lexer
);
8786 if (token
->type
!= CPP_NAME
8787 && token
->type
!= CPP_KEYWORD
)
8789 cp_parser_error (parser
, "expected labeled-statement");
8793 parser
->colon_corrects_to_scope_p
= false;
8794 switch (token
->keyword
)
8801 /* Consume the `case' token. */
8802 cp_lexer_consume_token (parser
->lexer
);
8803 /* Parse the constant-expression. */
8804 expr
= cp_parser_constant_expression (parser
,
8805 /*allow_non_constant_p=*/false,
8808 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
8809 if (ellipsis
->type
== CPP_ELLIPSIS
)
8811 /* Consume the `...' token. */
8812 cp_lexer_consume_token (parser
->lexer
);
8814 cp_parser_constant_expression (parser
,
8815 /*allow_non_constant_p=*/false,
8817 /* We don't need to emit warnings here, as the common code
8818 will do this for us. */
8821 expr_hi
= NULL_TREE
;
8823 if (parser
->in_switch_statement_p
)
8824 finish_case_label (token
->location
, expr
, expr_hi
);
8826 error_at (token
->location
,
8827 "case label %qE not within a switch statement",
8833 /* Consume the `default' token. */
8834 cp_lexer_consume_token (parser
->lexer
);
8836 if (parser
->in_switch_statement_p
)
8837 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
8839 error_at (token
->location
, "case label not within a switch statement");
8843 /* Anything else must be an ordinary label. */
8844 label
= finish_label_stmt (cp_parser_identifier (parser
));
8848 /* Require the `:' token. */
8849 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8851 /* An ordinary label may optionally be followed by attributes.
8852 However, this is only permitted if the attributes are then
8853 followed by a semicolon. This is because, for backward
8854 compatibility, when parsing
8855 lab: __attribute__ ((unused)) int i;
8856 we want the attribute to attach to "i", not "lab". */
8857 if (label
!= NULL_TREE
8858 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
8862 cp_parser_parse_tentatively (parser
);
8863 attrs
= cp_parser_attributes_opt (parser
);
8864 if (attrs
== NULL_TREE
8865 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8866 cp_parser_abort_tentative_parse (parser
);
8867 else if (!cp_parser_parse_definitely (parser
))
8870 cplus_decl_attributes (&label
, attrs
, 0);
8873 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8876 /* Parse an expression-statement.
8878 expression-statement:
8881 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8882 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8883 indicates whether this expression-statement is part of an
8884 expression statement. */
8887 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
8889 tree statement
= NULL_TREE
;
8890 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8892 /* If the next token is a ';', then there is no expression
8894 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
8895 statement
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
8897 /* Give a helpful message for "A<T>::type t;" and the like. */
8898 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
8899 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
8901 if (TREE_CODE (statement
) == SCOPE_REF
)
8902 error_at (token
->location
, "need %<typename%> before %qE because "
8903 "%qT is a dependent scope",
8904 statement
, TREE_OPERAND (statement
, 0));
8905 else if (is_overloaded_fn (statement
)
8906 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
8909 tree fn
= get_first_fn (statement
);
8910 error_at (token
->location
,
8911 "%<%T::%D%> names the constructor, not the type",
8912 DECL_CONTEXT (fn
), DECL_NAME (fn
));
8916 /* Consume the final `;'. */
8917 cp_parser_consume_semicolon_at_end_of_statement (parser
);
8919 if (in_statement_expr
8920 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
8921 /* This is the final expression statement of a statement
8923 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
8925 statement
= finish_expr_stmt (statement
);
8932 /* Parse a compound-statement.
8935 { statement-seq [opt] }
8940 { label-declaration-seq [opt] statement-seq [opt] }
8942 label-declaration-seq:
8944 label-declaration-seq label-declaration
8946 Returns a tree representing the statement. */
8949 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
8950 bool in_try
, bool function_body
)
8954 /* Consume the `{'. */
8955 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
8956 return error_mark_node
;
8957 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
8959 pedwarn (input_location
, OPT_Wpedantic
,
8960 "compound-statement in constexpr function");
8961 /* Begin the compound-statement. */
8962 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
8963 /* If the next keyword is `__label__' we have a label declaration. */
8964 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
8965 cp_parser_label_declaration (parser
);
8966 /* Parse an (optional) statement-seq. */
8967 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
8968 /* Finish the compound-statement. */
8969 finish_compound_stmt (compound_stmt
);
8970 /* Consume the `}'. */
8971 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
8973 return compound_stmt
;
8976 /* Parse an (optional) statement-seq.
8980 statement-seq [opt] statement */
8983 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
8985 /* Scan statements until there aren't any more. */
8988 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8990 /* If we are looking at a `}', then we have run out of
8991 statements; the same is true if we have reached the end
8992 of file, or have stumbled upon a stray '@end'. */
8993 if (token
->type
== CPP_CLOSE_BRACE
8994 || token
->type
== CPP_EOF
8995 || token
->type
== CPP_PRAGMA_EOL
8996 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
8999 /* If we are in a compound statement and find 'else' then
9000 something went wrong. */
9001 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
9003 if (parser
->in_statement
& IN_IF_STMT
)
9007 token
= cp_lexer_consume_token (parser
->lexer
);
9008 error_at (token
->location
, "%<else%> without a previous %<if%>");
9012 /* Parse the statement. */
9013 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
9017 /* Parse a selection-statement.
9019 selection-statement:
9020 if ( condition ) statement
9021 if ( condition ) statement else statement
9022 switch ( condition ) statement
9024 Returns the new IF_STMT or SWITCH_STMT.
9026 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9027 is a (possibly labeled) if statement which is not enclosed in
9028 braces and has an else clause. This is used to implement
9032 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
9040 /* Peek at the next token. */
9041 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
9043 /* See what kind of keyword it is. */
9044 keyword
= token
->keyword
;
9053 /* Look for the `('. */
9054 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
9056 cp_parser_skip_to_end_of_statement (parser
);
9057 return error_mark_node
;
9060 /* Begin the selection-statement. */
9061 if (keyword
== RID_IF
)
9062 statement
= begin_if_stmt ();
9064 statement
= begin_switch_stmt ();
9066 /* Parse the condition. */
9067 condition
= cp_parser_condition (parser
);
9068 /* Look for the `)'. */
9069 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
9070 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
9071 /*consume_paren=*/true);
9073 if (keyword
== RID_IF
)
9076 unsigned char in_statement
;
9078 /* Add the condition. */
9079 finish_if_stmt_cond (condition
, statement
);
9081 /* Parse the then-clause. */
9082 in_statement
= parser
->in_statement
;
9083 parser
->in_statement
|= IN_IF_STMT
;
9084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9086 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9087 add_stmt (build_empty_stmt (loc
));
9088 cp_lexer_consume_token (parser
->lexer
);
9089 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
9090 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
9091 "empty body in an %<if%> statement");
9095 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
9096 parser
->in_statement
= in_statement
;
9098 finish_then_clause (statement
);
9100 /* If the next token is `else', parse the else-clause. */
9101 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
9104 /* Consume the `else' keyword. */
9105 cp_lexer_consume_token (parser
->lexer
);
9106 begin_else_clause (statement
);
9107 /* Parse the else-clause. */
9108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
9111 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9113 OPT_Wempty_body
, "suggest braces around "
9114 "empty body in an %<else%> statement");
9115 add_stmt (build_empty_stmt (loc
));
9116 cp_lexer_consume_token (parser
->lexer
);
9119 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9121 finish_else_clause (statement
);
9123 /* If we are currently parsing a then-clause, then
9124 IF_P will not be NULL. We set it to true to
9125 indicate that this if statement has an else clause.
9126 This may trigger the Wparentheses warning below
9127 when we get back up to the parent if statement. */
9133 /* This if statement does not have an else clause. If
9134 NESTED_IF is true, then the then-clause is an if
9135 statement which does have an else clause. We warn
9136 about the potential ambiguity. */
9138 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
9139 "suggest explicit braces to avoid ambiguous"
9143 /* Now we're all done with the if-statement. */
9144 finish_if_stmt (statement
);
9148 bool in_switch_statement_p
;
9149 unsigned char in_statement
;
9151 /* Add the condition. */
9152 finish_switch_cond (condition
, statement
);
9154 /* Parse the body of the switch-statement. */
9155 in_switch_statement_p
= parser
->in_switch_statement_p
;
9156 in_statement
= parser
->in_statement
;
9157 parser
->in_switch_statement_p
= true;
9158 parser
->in_statement
|= IN_SWITCH_STMT
;
9159 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9160 parser
->in_switch_statement_p
= in_switch_statement_p
;
9161 parser
->in_statement
= in_statement
;
9163 /* Now we're all done with the switch-statement. */
9164 finish_switch_stmt (statement
);
9172 cp_parser_error (parser
, "expected selection-statement");
9173 return error_mark_node
;
9177 /* Parse a condition.
9181 type-specifier-seq declarator = initializer-clause
9182 type-specifier-seq declarator braced-init-list
9187 type-specifier-seq declarator asm-specification [opt]
9188 attributes [opt] = assignment-expression
9190 Returns the expression that should be tested. */
9193 cp_parser_condition (cp_parser
* parser
)
9195 cp_decl_specifier_seq type_specifiers
;
9196 const char *saved_message
;
9197 int declares_class_or_enum
;
9199 /* Try the declaration first. */
9200 cp_parser_parse_tentatively (parser
);
9201 /* New types are not allowed in the type-specifier-seq for a
9203 saved_message
= parser
->type_definition_forbidden_message
;
9204 parser
->type_definition_forbidden_message
9205 = G_("types may not be defined in conditions");
9206 /* Parse the type-specifier-seq. */
9207 cp_parser_decl_specifier_seq (parser
,
9208 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
9210 &declares_class_or_enum
);
9211 /* Restore the saved message. */
9212 parser
->type_definition_forbidden_message
= saved_message
;
9213 /* If all is well, we might be looking at a declaration. */
9214 if (!cp_parser_error_occurred (parser
))
9217 tree asm_specification
;
9219 cp_declarator
*declarator
;
9220 tree initializer
= NULL_TREE
;
9222 /* Parse the declarator. */
9223 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
9224 /*ctor_dtor_or_conv_p=*/NULL
,
9225 /*parenthesized_p=*/NULL
,
9226 /*member_p=*/false);
9227 /* Parse the attributes. */
9228 attributes
= cp_parser_attributes_opt (parser
);
9229 /* Parse the asm-specification. */
9230 asm_specification
= cp_parser_asm_specification_opt (parser
);
9231 /* If the next token is not an `=' or '{', then we might still be
9232 looking at an expression. For example:
9236 looks like a decl-specifier-seq and a declarator -- but then
9237 there is no `=', so this is an expression. */
9238 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
9239 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
9240 cp_parser_simulate_error (parser
);
9242 /* If we did see an `=' or '{', then we are looking at a declaration
9244 if (cp_parser_parse_definitely (parser
))
9247 bool non_constant_p
;
9248 bool flags
= LOOKUP_ONLYCONVERTING
;
9250 /* Create the declaration. */
9251 decl
= start_decl (declarator
, &type_specifiers
,
9252 /*initialized_p=*/true,
9253 attributes
, /*prefix_attributes=*/NULL_TREE
,
9256 /* Parse the initializer. */
9257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9259 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
9260 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
9265 /* Consume the `='. */
9266 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
9267 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
9269 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
9270 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9272 /* Process the initializer. */
9273 cp_finish_decl (decl
,
9274 initializer
, !non_constant_p
,
9279 pop_scope (pushed_scope
);
9281 return convert_from_reference (decl
);
9284 /* If we didn't even get past the declarator successfully, we are
9285 definitely not looking at a declaration. */
9287 cp_parser_abort_tentative_parse (parser
);
9289 /* Otherwise, we are looking at an expression. */
9290 return cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9293 /* Parses a for-statement or range-for-statement until the closing ')',
9297 cp_parser_for (cp_parser
*parser
)
9299 tree init
, scope
, decl
;
9302 /* Begin the for-statement. */
9303 scope
= begin_for_scope (&init
);
9305 /* Parse the initialization. */
9306 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
9309 return cp_parser_range_for (parser
, scope
, init
, decl
);
9311 return cp_parser_c_for (parser
, scope
, init
);
9315 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
)
9317 /* Normal for loop */
9318 tree condition
= NULL_TREE
;
9319 tree expression
= NULL_TREE
;
9322 stmt
= begin_for_stmt (scope
, init
);
9323 /* The for-init-statement has already been parsed in
9324 cp_parser_for_init_statement, so no work is needed here. */
9325 finish_for_init_stmt (stmt
);
9327 /* If there's a condition, process it. */
9328 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9329 condition
= cp_parser_condition (parser
);
9330 finish_for_cond (condition
, stmt
);
9331 /* Look for the `;'. */
9332 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9334 /* If there's an expression, process it. */
9335 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
9336 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9337 finish_for_expr (expression
, stmt
);
9342 /* Tries to parse a range-based for-statement:
9345 decl-specifier-seq declarator : expression
9347 The decl-specifier-seq declarator and the `:' are already parsed by
9348 cp_parser_for_init_statement. If processing_template_decl it returns a
9349 newly created RANGE_FOR_STMT; if not, it is converted to a
9350 regular FOR_STMT. */
9353 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
)
9355 tree stmt
, range_expr
;
9357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9359 bool expr_non_constant_p
;
9360 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9363 range_expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9365 /* If in template, STMT is converted to a normal for-statement
9366 at instantiation. If not, it is done just ahead. */
9367 if (processing_template_decl
)
9369 if (check_for_bare_parameter_packs (range_expr
))
9370 range_expr
= error_mark_node
;
9371 stmt
= begin_range_for_stmt (scope
, init
);
9372 finish_range_for_decl (stmt
, range_decl
, range_expr
);
9373 if (!type_dependent_expression_p (range_expr
)
9374 /* do_auto_deduction doesn't mess with template init-lists. */
9375 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
9376 do_range_for_auto_deduction (range_decl
, range_expr
);
9380 stmt
= begin_for_stmt (scope
, init
);
9381 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
);
9386 /* Subroutine of cp_convert_range_for: given the initializer expression,
9387 builds up the range temporary. */
9390 build_range_temp (tree range_expr
)
9392 tree range_type
, range_temp
;
9394 /* Find out the type deduced by the declaration
9395 `auto &&__range = range_expr'. */
9396 range_type
= cp_build_reference_type (make_auto (), true);
9397 range_type
= do_auto_deduction (range_type
, range_expr
,
9398 type_uses_auto (range_type
));
9400 /* Create the __range variable. */
9401 range_temp
= build_decl (input_location
, VAR_DECL
,
9402 get_identifier ("__for_range"), range_type
);
9403 TREE_USED (range_temp
) = 1;
9404 DECL_ARTIFICIAL (range_temp
) = 1;
9409 /* Used by cp_parser_range_for in template context: we aren't going to
9410 do a full conversion yet, but we still need to resolve auto in the
9411 type of the for-range-declaration if present. This is basically
9412 a shortcut version of cp_convert_range_for. */
9415 do_range_for_auto_deduction (tree decl
, tree range_expr
)
9417 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
9420 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
9421 range_temp
= convert_from_reference (build_range_temp (range_expr
));
9422 iter_type
= (cp_parser_perform_range_for_lookup
9423 (range_temp
, &begin_dummy
, &end_dummy
));
9424 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
9425 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
9426 tf_warning_or_error
);
9427 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
9428 iter_decl
, auto_node
);
9432 /* Converts a range-based for-statement into a normal
9433 for-statement, as per the definition.
9435 for (RANGE_DECL : RANGE_EXPR)
9438 should be equivalent to:
9441 auto &&__range = RANGE_EXPR;
9442 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9446 RANGE_DECL = *__begin;
9451 If RANGE_EXPR is an array:
9452 BEGIN_EXPR = __range
9453 END_EXPR = __range + ARRAY_SIZE(__range)
9454 Else if RANGE_EXPR has a member 'begin' or 'end':
9455 BEGIN_EXPR = __range.begin()
9456 END_EXPR = __range.end()
9458 BEGIN_EXPR = begin(__range)
9459 END_EXPR = end(__range);
9461 If __range has a member 'begin' but not 'end', or vice versa, we must
9462 still use the second alternative (it will surely fail, however).
9463 When calling begin()/end() in the third alternative we must use
9464 argument dependent lookup, but always considering 'std' as an associated
9468 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
)
9471 tree iter_type
, begin_expr
, end_expr
;
9472 tree condition
, expression
;
9474 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
9475 /* If an error happened previously do nothing or else a lot of
9476 unhelpful errors would be issued. */
9477 begin_expr
= end_expr
= iter_type
= error_mark_node
;
9480 tree range_temp
= build_range_temp (range_expr
);
9481 pushdecl (range_temp
);
9482 cp_finish_decl (range_temp
, range_expr
,
9483 /*is_constant_init*/false, NULL_TREE
,
9484 LOOKUP_ONLYCONVERTING
);
9486 range_temp
= convert_from_reference (range_temp
);
9487 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
9488 &begin_expr
, &end_expr
);
9491 /* The new for initialization statement. */
9492 begin
= build_decl (input_location
, VAR_DECL
,
9493 get_identifier ("__for_begin"), iter_type
);
9494 TREE_USED (begin
) = 1;
9495 DECL_ARTIFICIAL (begin
) = 1;
9497 cp_finish_decl (begin
, begin_expr
,
9498 /*is_constant_init*/false, NULL_TREE
,
9499 LOOKUP_ONLYCONVERTING
);
9501 end
= build_decl (input_location
, VAR_DECL
,
9502 get_identifier ("__for_end"), iter_type
);
9503 TREE_USED (end
) = 1;
9504 DECL_ARTIFICIAL (end
) = 1;
9506 cp_finish_decl (end
, end_expr
,
9507 /*is_constant_init*/false, NULL_TREE
,
9508 LOOKUP_ONLYCONVERTING
);
9510 finish_for_init_stmt (statement
);
9512 /* The new for condition. */
9513 condition
= build_x_binary_op (input_location
, NE_EXPR
,
9516 NULL
, tf_warning_or_error
);
9517 finish_for_cond (condition
, statement
);
9519 /* The new increment expression. */
9520 expression
= finish_unary_op_expr (input_location
,
9521 PREINCREMENT_EXPR
, begin
);
9522 finish_for_expr (expression
, statement
);
9524 /* The declaration is initialized with *__begin inside the loop body. */
9525 cp_finish_decl (range_decl
,
9526 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
9527 tf_warning_or_error
),
9528 /*is_constant_init*/false, NULL_TREE
,
9529 LOOKUP_ONLYCONVERTING
);
9534 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9535 We need to solve both at the same time because the method used
9536 depends on the existence of members begin or end.
9537 Returns the type deduced for the iterator expression. */
9540 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
9542 if (error_operand_p (range
))
9544 *begin
= *end
= error_mark_node
;
9545 return error_mark_node
;
9548 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
9550 error ("range-based %<for%> expression of type %qT "
9551 "has incomplete type", TREE_TYPE (range
));
9552 *begin
= *end
= error_mark_node
;
9553 return error_mark_node
;
9555 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
9557 /* If RANGE is an array, we will use pointer arithmetic. */
9559 *end
= build_binary_op (input_location
, PLUS_EXPR
,
9561 array_type_nelts_top (TREE_TYPE (range
)),
9563 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
9567 /* If it is not an array, we must do a bit of magic. */
9568 tree id_begin
, id_end
;
9569 tree member_begin
, member_end
;
9571 *begin
= *end
= error_mark_node
;
9573 id_begin
= get_identifier ("begin");
9574 id_end
= get_identifier ("end");
9575 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
9576 /*protect=*/2, /*want_type=*/false,
9577 tf_warning_or_error
);
9578 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
9579 /*protect=*/2, /*want_type=*/false,
9580 tf_warning_or_error
);
9582 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
9584 /* Use the member functions. */
9585 if (member_begin
!= NULL_TREE
)
9586 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
9588 error ("range-based %<for%> expression of type %qT has an "
9589 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
9591 if (member_end
!= NULL_TREE
)
9592 *end
= cp_parser_range_for_member_function (range
, id_end
);
9594 error ("range-based %<for%> expression of type %qT has a "
9595 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
9599 /* Use global functions with ADL. */
9601 vec
= make_tree_vector ();
9603 VEC_safe_push (tree
, gc
, vec
, range
);
9605 member_begin
= perform_koenig_lookup (id_begin
, vec
,
9606 /*include_std=*/true,
9607 tf_warning_or_error
);
9608 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
9609 tf_warning_or_error
);
9610 member_end
= perform_koenig_lookup (id_end
, vec
,
9611 /*include_std=*/true,
9612 tf_warning_or_error
);
9613 *end
= finish_call_expr (member_end
, &vec
, false, true,
9614 tf_warning_or_error
);
9616 release_tree_vector (vec
);
9619 /* Last common checks. */
9620 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
9622 /* If one of the expressions is an error do no more checks. */
9623 *begin
= *end
= error_mark_node
;
9624 return error_mark_node
;
9628 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
9629 /* The unqualified type of the __begin and __end temporaries should
9630 be the same, as required by the multiple auto declaration. */
9631 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
9632 error ("inconsistent begin/end types in range-based %<for%> "
9633 "statement: %qT and %qT",
9634 TREE_TYPE (*begin
), TREE_TYPE (*end
));
9640 /* Helper function for cp_parser_perform_range_for_lookup.
9641 Builds a tree for RANGE.IDENTIFIER(). */
9644 cp_parser_range_for_member_function (tree range
, tree identifier
)
9649 member
= finish_class_member_access_expr (range
, identifier
,
9650 false, tf_warning_or_error
);
9651 if (member
== error_mark_node
)
9652 return error_mark_node
;
9654 vec
= make_tree_vector ();
9655 res
= finish_call_expr (member
, &vec
,
9656 /*disallow_virtual=*/false,
9658 tf_warning_or_error
);
9659 release_tree_vector (vec
);
9663 /* Parse an iteration-statement.
9665 iteration-statement:
9666 while ( condition ) statement
9667 do statement while ( expression ) ;
9668 for ( for-init-statement condition [opt] ; expression [opt] )
9671 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9674 cp_parser_iteration_statement (cp_parser
* parser
)
9679 unsigned char in_statement
;
9681 /* Peek at the next token. */
9682 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
9684 return error_mark_node
;
9686 /* Remember whether or not we are already within an iteration
9688 in_statement
= parser
->in_statement
;
9690 /* See what kind of keyword it is. */
9691 keyword
= token
->keyword
;
9698 /* Begin the while-statement. */
9699 statement
= begin_while_stmt ();
9700 /* Look for the `('. */
9701 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9702 /* Parse the condition. */
9703 condition
= cp_parser_condition (parser
);
9704 finish_while_stmt_cond (condition
, statement
);
9705 /* Look for the `)'. */
9706 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9707 /* Parse the dependent statement. */
9708 parser
->in_statement
= IN_ITERATION_STMT
;
9709 cp_parser_already_scoped_statement (parser
);
9710 parser
->in_statement
= in_statement
;
9711 /* We're done with the while-statement. */
9712 finish_while_stmt (statement
);
9720 /* Begin the do-statement. */
9721 statement
= begin_do_stmt ();
9722 /* Parse the body of the do-statement. */
9723 parser
->in_statement
= IN_ITERATION_STMT
;
9724 cp_parser_implicitly_scoped_statement (parser
, NULL
);
9725 parser
->in_statement
= in_statement
;
9726 finish_do_body (statement
);
9727 /* Look for the `while' keyword. */
9728 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
9729 /* Look for the `('. */
9730 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9731 /* Parse the expression. */
9732 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9733 /* We're done with the do-statement. */
9734 finish_do_stmt (expression
, statement
);
9735 /* Look for the `)'. */
9736 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9737 /* Look for the `;'. */
9738 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9744 /* Look for the `('. */
9745 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9747 statement
= cp_parser_for (parser
);
9749 /* Look for the `)'. */
9750 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9752 /* Parse the body of the for-statement. */
9753 parser
->in_statement
= IN_ITERATION_STMT
;
9754 cp_parser_already_scoped_statement (parser
);
9755 parser
->in_statement
= in_statement
;
9757 /* We're done with the for-statement. */
9758 finish_for_stmt (statement
);
9763 cp_parser_error (parser
, "expected iteration-statement");
9764 statement
= error_mark_node
;
9771 /* Parse a for-init-statement or the declarator of a range-based-for.
9772 Returns true if a range-based-for declaration is seen.
9775 expression-statement
9776 simple-declaration */
9779 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
9781 /* If the next token is a `;', then we have an empty
9782 expression-statement. Grammatically, this is also a
9783 simple-declaration, but an invalid one, because it does not
9784 declare anything. Therefore, if we did not handle this case
9785 specially, we would issue an error message about an invalid
9787 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9789 bool is_range_for
= false;
9790 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9792 parser
->colon_corrects_to_scope_p
= false;
9794 /* We're going to speculatively look for a declaration, falling back
9795 to an expression, if necessary. */
9796 cp_parser_parse_tentatively (parser
);
9797 /* Parse the declaration. */
9798 cp_parser_simple_declaration (parser
,
9799 /*function_definition_allowed_p=*/false,
9801 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
9804 /* It is a range-for, consume the ':' */
9805 cp_lexer_consume_token (parser
->lexer
);
9806 is_range_for
= true;
9807 if (cxx_dialect
< cxx0x
)
9809 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
9810 "range-based %<for%> loops are not allowed "
9812 *decl
= error_mark_node
;
9816 /* The ';' is not consumed yet because we told
9817 cp_parser_simple_declaration not to. */
9818 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9820 if (cp_parser_parse_definitely (parser
))
9821 return is_range_for
;
9822 /* If the tentative parse failed, then we shall need to look for an
9823 expression-statement. */
9825 /* If we are here, it is an expression-statement. */
9826 cp_parser_expression_statement (parser
, NULL_TREE
);
9830 /* Parse a jump-statement.
9835 return expression [opt] ;
9836 return braced-init-list ;
9844 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9847 cp_parser_jump_statement (cp_parser
* parser
)
9849 tree statement
= error_mark_node
;
9852 unsigned char in_statement
;
9854 /* Peek at the next token. */
9855 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
9857 return error_mark_node
;
9859 /* See what kind of keyword it is. */
9860 keyword
= token
->keyword
;
9864 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
9865 switch (in_statement
)
9868 error_at (token
->location
, "break statement not within loop or switch");
9871 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
9872 || in_statement
== IN_ITERATION_STMT
);
9873 statement
= finish_break_stmt ();
9876 error_at (token
->location
, "invalid exit from OpenMP structured block");
9879 error_at (token
->location
, "break statement used with OpenMP for loop");
9882 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9886 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
9889 error_at (token
->location
, "continue statement not within a loop");
9891 case IN_ITERATION_STMT
:
9893 statement
= finish_continue_stmt ();
9896 error_at (token
->location
, "invalid exit from OpenMP structured block");
9901 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9907 bool expr_non_constant_p
;
9909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9911 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9912 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9914 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9915 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
9917 /* If the next token is a `;', then there is no
9920 /* Build the return-statement. */
9921 statement
= finish_return_stmt (expr
);
9922 /* Look for the final `;'. */
9923 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9928 /* Create the goto-statement. */
9929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
9931 /* Issue a warning about this use of a GNU extension. */
9932 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
9933 /* Consume the '*' token. */
9934 cp_lexer_consume_token (parser
->lexer
);
9935 /* Parse the dependent expression. */
9936 finish_goto_stmt (cp_parser_expression (parser
, /*cast_p=*/false, NULL
));
9939 finish_goto_stmt (cp_parser_identifier (parser
));
9940 /* Look for the final `;'. */
9941 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9945 cp_parser_error (parser
, "expected jump-statement");
9952 /* Parse a declaration-statement.
9954 declaration-statement:
9955 block-declaration */
9958 cp_parser_declaration_statement (cp_parser
* parser
)
9962 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9963 p
= obstack_alloc (&declarator_obstack
, 0);
9965 /* Parse the block-declaration. */
9966 cp_parser_block_declaration (parser
, /*statement_p=*/true);
9968 /* Free any declarators allocated. */
9969 obstack_free (&declarator_obstack
, p
);
9971 /* Finish off the statement. */
9975 /* Some dependent statements (like `if (cond) statement'), are
9976 implicitly in their own scope. In other words, if the statement is
9977 a single statement (as opposed to a compound-statement), it is
9978 none-the-less treated as if it were enclosed in braces. Any
9979 declarations appearing in the dependent statement are out of scope
9980 after control passes that point. This function parses a statement,
9981 but ensures that is in its own scope, even if it is not a
9984 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9985 is a (possibly labeled) if statement which is not enclosed in
9986 braces and has an else clause. This is used to implement
9989 Returns the new statement. */
9992 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
9999 /* Mark if () ; with a special NOP_EXPR. */
10000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10002 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10003 cp_lexer_consume_token (parser
->lexer
);
10004 statement
= add_stmt (build_empty_stmt (loc
));
10006 /* if a compound is opened, we simply parse the statement directly. */
10007 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10008 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
10009 /* If the token is not a `{', then we must take special action. */
10012 /* Create a compound-statement. */
10013 statement
= begin_compound_stmt (0);
10014 /* Parse the dependent-statement. */
10015 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
10016 /* Finish the dummy compound-statement. */
10017 finish_compound_stmt (statement
);
10020 /* Return the statement. */
10024 /* For some dependent statements (like `while (cond) statement'), we
10025 have already created a scope. Therefore, even if the dependent
10026 statement is a compound-statement, we do not want to create another
10030 cp_parser_already_scoped_statement (cp_parser
* parser
)
10032 /* If the token is a `{', then we must take special action. */
10033 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10034 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
10037 /* Avoid calling cp_parser_compound_statement, so that we
10038 don't create a new scope. Do everything else by hand. */
10039 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
10040 /* If the next keyword is `__label__' we have a label declaration. */
10041 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10042 cp_parser_label_declaration (parser
);
10043 /* Parse an (optional) statement-seq. */
10044 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10045 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10049 /* Declarations [gram.dcl.dcl] */
10051 /* Parse an optional declaration-sequence.
10055 declaration-seq declaration */
10058 cp_parser_declaration_seq_opt (cp_parser
* parser
)
10064 token
= cp_lexer_peek_token (parser
->lexer
);
10066 if (token
->type
== CPP_CLOSE_BRACE
10067 || token
->type
== CPP_EOF
10068 || token
->type
== CPP_PRAGMA_EOL
)
10071 if (token
->type
== CPP_SEMICOLON
)
10073 /* A declaration consisting of a single semicolon is
10074 invalid. Allow it unless we're being pedantic. */
10075 cp_lexer_consume_token (parser
->lexer
);
10076 if (!in_system_header
)
10077 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
10081 /* If we're entering or exiting a region that's implicitly
10082 extern "C", modify the lang context appropriately. */
10083 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
10085 push_lang_context (lang_name_c
);
10086 parser
->implicit_extern_c
= true;
10088 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
10090 pop_lang_context ();
10091 parser
->implicit_extern_c
= false;
10094 if (token
->type
== CPP_PRAGMA
)
10096 /* A top-level declaration can consist solely of a #pragma.
10097 A nested declaration cannot, so this is done here and not
10098 in cp_parser_declaration. (A #pragma at block scope is
10099 handled in cp_parser_statement.) */
10100 cp_parser_pragma (parser
, pragma_external
);
10104 /* Parse the declaration itself. */
10105 cp_parser_declaration (parser
);
10109 /* Parse a declaration.
10113 function-definition
10114 template-declaration
10115 explicit-instantiation
10116 explicit-specialization
10117 linkage-specification
10118 namespace-definition
10123 __extension__ declaration */
10126 cp_parser_declaration (cp_parser
* parser
)
10130 int saved_pedantic
;
10132 tree attributes
= NULL_TREE
;
10134 /* Check for the `__extension__' keyword. */
10135 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10137 /* Parse the qualified declaration. */
10138 cp_parser_declaration (parser
);
10139 /* Restore the PEDANTIC flag. */
10140 pedantic
= saved_pedantic
;
10145 /* Try to figure out what kind of declaration is present. */
10146 token1
= *cp_lexer_peek_token (parser
->lexer
);
10148 if (token1
.type
!= CPP_EOF
)
10149 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
10152 token2
.type
= CPP_EOF
;
10153 token2
.keyword
= RID_MAX
;
10156 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10157 p
= obstack_alloc (&declarator_obstack
, 0);
10159 /* If the next token is `extern' and the following token is a string
10160 literal, then we have a linkage specification. */
10161 if (token1
.keyword
== RID_EXTERN
10162 && cp_parser_is_pure_string_literal (&token2
))
10163 cp_parser_linkage_specification (parser
);
10164 /* If the next token is `template', then we have either a template
10165 declaration, an explicit instantiation, or an explicit
10167 else if (token1
.keyword
== RID_TEMPLATE
)
10169 /* `template <>' indicates a template specialization. */
10170 if (token2
.type
== CPP_LESS
10171 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
10172 cp_parser_explicit_specialization (parser
);
10173 /* `template <' indicates a template declaration. */
10174 else if (token2
.type
== CPP_LESS
)
10175 cp_parser_template_declaration (parser
, /*member_p=*/false);
10176 /* Anything else must be an explicit instantiation. */
10178 cp_parser_explicit_instantiation (parser
);
10180 /* If the next token is `export', then we have a template
10182 else if (token1
.keyword
== RID_EXPORT
)
10183 cp_parser_template_declaration (parser
, /*member_p=*/false);
10184 /* If the next token is `extern', 'static' or 'inline' and the one
10185 after that is `template', we have a GNU extended explicit
10186 instantiation directive. */
10187 else if (cp_parser_allow_gnu_extensions_p (parser
)
10188 && (token1
.keyword
== RID_EXTERN
10189 || token1
.keyword
== RID_STATIC
10190 || token1
.keyword
== RID_INLINE
)
10191 && token2
.keyword
== RID_TEMPLATE
)
10192 cp_parser_explicit_instantiation (parser
);
10193 /* If the next token is `namespace', check for a named or unnamed
10194 namespace definition. */
10195 else if (token1
.keyword
== RID_NAMESPACE
10196 && (/* A named namespace definition. */
10197 (token2
.type
== CPP_NAME
10198 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
10200 /* An unnamed namespace definition. */
10201 || token2
.type
== CPP_OPEN_BRACE
10202 || token2
.keyword
== RID_ATTRIBUTE
))
10203 cp_parser_namespace_definition (parser
);
10204 /* An inline (associated) namespace definition. */
10205 else if (token1
.keyword
== RID_INLINE
10206 && token2
.keyword
== RID_NAMESPACE
)
10207 cp_parser_namespace_definition (parser
);
10208 /* Objective-C++ declaration/definition. */
10209 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
10210 cp_parser_objc_declaration (parser
, NULL_TREE
);
10211 else if (c_dialect_objc ()
10212 && token1
.keyword
== RID_ATTRIBUTE
10213 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
10214 cp_parser_objc_declaration (parser
, attributes
);
10215 /* We must have either a block declaration or a function
10218 /* Try to parse a block-declaration, or a function-definition. */
10219 cp_parser_block_declaration (parser
, /*statement_p=*/false);
10221 /* Free any declarators allocated. */
10222 obstack_free (&declarator_obstack
, p
);
10225 /* Parse a block-declaration.
10230 namespace-alias-definition
10237 __extension__ block-declaration
10242 static_assert-declaration
10244 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10245 part of a declaration-statement. */
10248 cp_parser_block_declaration (cp_parser
*parser
,
10252 int saved_pedantic
;
10254 /* Check for the `__extension__' keyword. */
10255 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
10257 /* Parse the qualified declaration. */
10258 cp_parser_block_declaration (parser
, statement_p
);
10259 /* Restore the PEDANTIC flag. */
10260 pedantic
= saved_pedantic
;
10265 /* Peek at the next token to figure out which kind of declaration is
10267 token1
= cp_lexer_peek_token (parser
->lexer
);
10269 /* If the next keyword is `asm', we have an asm-definition. */
10270 if (token1
->keyword
== RID_ASM
)
10273 cp_parser_commit_to_tentative_parse (parser
);
10274 cp_parser_asm_definition (parser
);
10276 /* If the next keyword is `namespace', we have a
10277 namespace-alias-definition. */
10278 else if (token1
->keyword
== RID_NAMESPACE
)
10279 cp_parser_namespace_alias_definition (parser
);
10280 /* If the next keyword is `using', we have a
10281 using-declaration, a using-directive, or an alias-declaration. */
10282 else if (token1
->keyword
== RID_USING
)
10287 cp_parser_commit_to_tentative_parse (parser
);
10288 /* If the token after `using' is `namespace', then we have a
10289 using-directive. */
10290 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10291 if (token2
->keyword
== RID_NAMESPACE
)
10292 cp_parser_using_directive (parser
);
10293 /* If the second token after 'using' is '=', then we have an
10294 alias-declaration. */
10295 else if (cxx_dialect
>= cxx0x
10296 && token2
->type
== CPP_NAME
10297 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
10298 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->keyword
10299 == RID_ATTRIBUTE
)))
10300 cp_parser_alias_declaration (parser
);
10301 /* Otherwise, it's a using-declaration. */
10303 cp_parser_using_declaration (parser
,
10304 /*access_declaration_p=*/false);
10306 /* If the next keyword is `__label__' we have a misplaced label
10308 else if (token1
->keyword
== RID_LABEL
)
10310 cp_lexer_consume_token (parser
->lexer
);
10311 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
10312 cp_parser_skip_to_end_of_statement (parser
);
10313 /* If the next token is now a `;', consume it. */
10314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10315 cp_lexer_consume_token (parser
->lexer
);
10317 /* If the next token is `static_assert' we have a static assertion. */
10318 else if (token1
->keyword
== RID_STATIC_ASSERT
)
10319 cp_parser_static_assert (parser
, /*member_p=*/false);
10320 /* Anything else must be a simple-declaration. */
10322 cp_parser_simple_declaration (parser
, !statement_p
,
10323 /*maybe_range_for_decl*/NULL
);
10326 /* Parse a simple-declaration.
10328 simple-declaration:
10329 decl-specifier-seq [opt] init-declarator-list [opt] ;
10331 init-declarator-list:
10333 init-declarator-list , init-declarator
10335 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10336 function-definition as a simple-declaration.
10338 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10339 parsed declaration if it is an uninitialized single declarator not followed
10340 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10341 if present, will not be consumed. */
10344 cp_parser_simple_declaration (cp_parser
* parser
,
10345 bool function_definition_allowed_p
,
10346 tree
*maybe_range_for_decl
)
10348 cp_decl_specifier_seq decl_specifiers
;
10349 int declares_class_or_enum
;
10350 bool saw_declarator
;
10352 if (maybe_range_for_decl
)
10353 *maybe_range_for_decl
= NULL_TREE
;
10355 /* Defer access checks until we know what is being declared; the
10356 checks for names appearing in the decl-specifier-seq should be
10357 done as if we were in the scope of the thing being declared. */
10358 push_deferring_access_checks (dk_deferred
);
10360 /* Parse the decl-specifier-seq. We have to keep track of whether
10361 or not the decl-specifier-seq declares a named class or
10362 enumeration type, since that is the only case in which the
10363 init-declarator-list is allowed to be empty.
10367 In a simple-declaration, the optional init-declarator-list can be
10368 omitted only when declaring a class or enumeration, that is when
10369 the decl-specifier-seq contains either a class-specifier, an
10370 elaborated-type-specifier, or an enum-specifier. */
10371 cp_parser_decl_specifier_seq (parser
,
10372 CP_PARSER_FLAGS_OPTIONAL
,
10374 &declares_class_or_enum
);
10375 /* We no longer need to defer access checks. */
10376 stop_deferring_access_checks ();
10378 /* In a block scope, a valid declaration must always have a
10379 decl-specifier-seq. By not trying to parse declarators, we can
10380 resolve the declaration/expression ambiguity more quickly. */
10381 if (!function_definition_allowed_p
10382 && !decl_specifiers
.any_specifiers_p
)
10384 cp_parser_error (parser
, "expected declaration");
10388 /* If the next two tokens are both identifiers, the code is
10389 erroneous. The usual cause of this situation is code like:
10393 where "T" should name a type -- but does not. */
10394 if (!decl_specifiers
.any_type_specifiers_p
10395 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
10397 /* If parsing tentatively, we should commit; we really are
10398 looking at a declaration. */
10399 cp_parser_commit_to_tentative_parse (parser
);
10404 /* If we have seen at least one decl-specifier, and the next token
10405 is not a parenthesis, then we must be looking at a declaration.
10406 (After "int (" we might be looking at a functional cast.) */
10407 if (decl_specifiers
.any_specifiers_p
10408 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
10409 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
10410 && !cp_parser_error_occurred (parser
))
10411 cp_parser_commit_to_tentative_parse (parser
);
10413 /* Keep going until we hit the `;' at the end of the simple
10415 saw_declarator
= false;
10416 while (cp_lexer_next_token_is_not (parser
->lexer
,
10420 bool function_definition_p
;
10423 if (saw_declarator
)
10425 /* If we are processing next declarator, coma is expected */
10426 token
= cp_lexer_peek_token (parser
->lexer
);
10427 gcc_assert (token
->type
== CPP_COMMA
);
10428 cp_lexer_consume_token (parser
->lexer
);
10429 if (maybe_range_for_decl
)
10430 *maybe_range_for_decl
= error_mark_node
;
10433 saw_declarator
= true;
10435 /* Parse the init-declarator. */
10436 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
10438 function_definition_allowed_p
,
10439 /*member_p=*/false,
10440 declares_class_or_enum
,
10441 &function_definition_p
,
10442 maybe_range_for_decl
);
10443 /* If an error occurred while parsing tentatively, exit quickly.
10444 (That usually happens when in the body of a function; each
10445 statement is treated as a declaration-statement until proven
10447 if (cp_parser_error_occurred (parser
))
10449 /* Handle function definitions specially. */
10450 if (function_definition_p
)
10452 /* If the next token is a `,', then we are probably
10453 processing something like:
10457 which is erroneous. */
10458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10460 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10461 error_at (token
->location
,
10463 " declarations and function-definitions is forbidden");
10465 /* Otherwise, we're done with the list of declarators. */
10468 pop_deferring_access_checks ();
10472 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
10473 *maybe_range_for_decl
= decl
;
10474 /* The next token should be either a `,' or a `;'. */
10475 token
= cp_lexer_peek_token (parser
->lexer
);
10476 /* If it's a `,', there are more declarators to come. */
10477 if (token
->type
== CPP_COMMA
)
10478 /* will be consumed next time around */;
10479 /* If it's a `;', we are done. */
10480 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
10482 /* Anything else is an error. */
10485 /* If we have already issued an error message we don't need
10486 to issue another one. */
10487 if (decl
!= error_mark_node
10488 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
10489 cp_parser_error (parser
, "expected %<,%> or %<;%>");
10490 /* Skip tokens until we reach the end of the statement. */
10491 cp_parser_skip_to_end_of_statement (parser
);
10492 /* If the next token is now a `;', consume it. */
10493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10494 cp_lexer_consume_token (parser
->lexer
);
10497 /* After the first time around, a function-definition is not
10498 allowed -- even if it was OK at first. For example:
10503 function_definition_allowed_p
= false;
10506 /* Issue an error message if no declarators are present, and the
10507 decl-specifier-seq does not itself declare a class or
10509 if (!saw_declarator
)
10511 if (cp_parser_declares_only_class_p (parser
))
10512 shadow_tag (&decl_specifiers
);
10513 /* Perform any deferred access checks. */
10514 perform_deferred_access_checks (tf_warning_or_error
);
10517 /* Consume the `;'. */
10518 if (!maybe_range_for_decl
)
10519 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10522 pop_deferring_access_checks ();
10525 /* Parse a decl-specifier-seq.
10527 decl-specifier-seq:
10528 decl-specifier-seq [opt] decl-specifier
10531 storage-class-specifier
10542 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10544 The parser flags FLAGS is used to control type-specifier parsing.
10546 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10549 1: one of the decl-specifiers is an elaborated-type-specifier
10550 (i.e., a type declaration)
10551 2: one of the decl-specifiers is an enum-specifier or a
10552 class-specifier (i.e., a type definition)
10557 cp_parser_decl_specifier_seq (cp_parser
* parser
,
10558 cp_parser_flags flags
,
10559 cp_decl_specifier_seq
*decl_specs
,
10560 int* declares_class_or_enum
)
10562 bool constructor_possible_p
= !parser
->in_declarator_p
;
10563 cp_token
*start_token
= NULL
;
10566 /* Clear DECL_SPECS. */
10567 clear_decl_specs (decl_specs
);
10569 /* Assume no class or enumeration type is declared. */
10570 *declares_class_or_enum
= 0;
10572 /* Keep reading specifiers until there are no more to read. */
10575 bool constructor_p
;
10576 bool found_decl_spec
;
10580 /* Peek at the next token. */
10581 token
= cp_lexer_peek_token (parser
->lexer
);
10583 /* Save the first token of the decl spec list for error
10586 start_token
= token
;
10587 /* Handle attributes. */
10588 if (token
->keyword
== RID_ATTRIBUTE
)
10590 /* Parse the attributes. */
10591 decl_specs
->attributes
10592 = chainon (decl_specs
->attributes
,
10593 cp_parser_attributes_opt (parser
));
10594 if (decl_specs
->locations
[ds_attribute
] == 0)
10595 decl_specs
->locations
[ds_attribute
] = token
->location
;
10598 /* Assume we will find a decl-specifier keyword. */
10599 found_decl_spec
= true;
10600 /* If the next token is an appropriate keyword, we can simply
10601 add it to the list. */
10602 switch (token
->keyword
)
10608 if (!at_class_scope_p ())
10610 error_at (token
->location
, "%<friend%> used outside of class");
10611 cp_lexer_purge_token (parser
->lexer
);
10616 /* Consume the token. */
10617 cp_lexer_consume_token (parser
->lexer
);
10621 case RID_CONSTEXPR
:
10623 cp_lexer_consume_token (parser
->lexer
);
10626 /* function-specifier:
10633 cp_parser_function_specifier_opt (parser
, decl_specs
);
10640 /* Consume the token. */
10641 cp_lexer_consume_token (parser
->lexer
);
10642 /* A constructor declarator cannot appear in a typedef. */
10643 constructor_possible_p
= false;
10644 /* The "typedef" keyword can only occur in a declaration; we
10645 may as well commit at this point. */
10646 cp_parser_commit_to_tentative_parse (parser
);
10648 if (decl_specs
->storage_class
!= sc_none
)
10649 decl_specs
->conflicting_specifiers_p
= true;
10652 /* storage-class-specifier:
10662 if (cxx_dialect
== cxx98
)
10664 /* Consume the token. */
10665 cp_lexer_consume_token (parser
->lexer
);
10667 /* Complain about `auto' as a storage specifier, if
10668 we're complaining about C++0x compatibility. */
10669 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
10670 " changes meaning in C++11; please remove it");
10672 /* Set the storage class anyway. */
10673 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
10677 /* C++0x auto type-specifier. */
10678 found_decl_spec
= false;
10685 /* Consume the token. */
10686 cp_lexer_consume_token (parser
->lexer
);
10687 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
10691 /* Consume the token. */
10693 cp_lexer_consume_token (parser
->lexer
);
10697 /* We did not yet find a decl-specifier yet. */
10698 found_decl_spec
= false;
10702 if (found_decl_spec
10703 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
10704 && token
->keyword
!= RID_CONSTEXPR
)
10705 error ("decl-specifier invalid in condition");
10708 set_and_check_decl_spec_loc (decl_specs
, ds
, token
->location
);
10710 /* Constructors are a special case. The `S' in `S()' is not a
10711 decl-specifier; it is the beginning of the declarator. */
10713 = (!found_decl_spec
10714 && constructor_possible_p
10715 && (cp_parser_constructor_declarator_p
10716 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
10718 /* If we don't have a DECL_SPEC yet, then we must be looking at
10719 a type-specifier. */
10720 if (!found_decl_spec
&& !constructor_p
)
10722 int decl_spec_declares_class_or_enum
;
10723 bool is_cv_qualifier
;
10727 = cp_parser_type_specifier (parser
, flags
,
10729 /*is_declaration=*/true,
10730 &decl_spec_declares_class_or_enum
,
10732 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
10734 /* If this type-specifier referenced a user-defined type
10735 (a typedef, class-name, etc.), then we can't allow any
10736 more such type-specifiers henceforth.
10740 The longest sequence of decl-specifiers that could
10741 possibly be a type name is taken as the
10742 decl-specifier-seq of a declaration. The sequence shall
10743 be self-consistent as described below.
10747 As a general rule, at most one type-specifier is allowed
10748 in the complete decl-specifier-seq of a declaration. The
10749 only exceptions are the following:
10751 -- const or volatile can be combined with any other
10754 -- signed or unsigned can be combined with char, long,
10762 void g (const int Pc);
10764 Here, Pc is *not* part of the decl-specifier seq; it's
10765 the declarator. Therefore, once we see a type-specifier
10766 (other than a cv-qualifier), we forbid any additional
10767 user-defined types. We *do* still allow things like `int
10768 int' to be considered a decl-specifier-seq, and issue the
10769 error message later. */
10770 if (type_spec
&& !is_cv_qualifier
)
10771 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
10772 /* A constructor declarator cannot follow a type-specifier. */
10775 constructor_possible_p
= false;
10776 found_decl_spec
= true;
10777 if (!is_cv_qualifier
)
10778 decl_specs
->any_type_specifiers_p
= true;
10782 /* If we still do not have a DECL_SPEC, then there are no more
10783 decl-specifiers. */
10784 if (!found_decl_spec
)
10787 decl_specs
->any_specifiers_p
= true;
10788 /* After we see one decl-specifier, further decl-specifiers are
10789 always optional. */
10790 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
10793 /* Don't allow a friend specifier with a class definition. */
10794 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
10795 && (*declares_class_or_enum
& 2))
10796 error_at (decl_specs
->locations
[ds_friend
],
10797 "class definition may not be declared a friend");
10800 /* Parse an (optional) storage-class-specifier.
10802 storage-class-specifier:
10811 storage-class-specifier:
10814 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10817 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
10819 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
10822 if (cxx_dialect
!= cxx98
)
10824 /* Fall through for C++98. */
10831 /* Consume the token. */
10832 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10839 /* Parse an (optional) function-specifier.
10841 function-specifier:
10846 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10847 Updates DECL_SPECS, if it is non-NULL. */
10850 cp_parser_function_specifier_opt (cp_parser
* parser
,
10851 cp_decl_specifier_seq
*decl_specs
)
10853 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10854 switch (token
->keyword
)
10857 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
->location
);
10861 /* 14.5.2.3 [temp.mem]
10863 A member function template shall not be virtual. */
10864 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10865 error_at (token
->location
, "templates may not be %<virtual%>");
10866 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
->location
);
10870 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
->location
);
10877 /* Consume the token. */
10878 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
10881 /* Parse a linkage-specification.
10883 linkage-specification:
10884 extern string-literal { declaration-seq [opt] }
10885 extern string-literal declaration */
10888 cp_parser_linkage_specification (cp_parser
* parser
)
10892 /* Look for the `extern' keyword. */
10893 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
10895 /* Look for the string-literal. */
10896 linkage
= cp_parser_string_literal (parser
, false, false);
10898 /* Transform the literal into an identifier. If the literal is a
10899 wide-character string, or contains embedded NULs, then we can't
10900 handle it as the user wants. */
10901 if (strlen (TREE_STRING_POINTER (linkage
))
10902 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
10904 cp_parser_error (parser
, "invalid linkage-specification");
10905 /* Assume C++ linkage. */
10906 linkage
= lang_name_cplusplus
;
10909 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
10911 /* We're now using the new linkage. */
10912 push_lang_context (linkage
);
10914 /* If the next token is a `{', then we're using the first
10916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10918 /* Consume the `{' token. */
10919 cp_lexer_consume_token (parser
->lexer
);
10920 /* Parse the declarations. */
10921 cp_parser_declaration_seq_opt (parser
);
10922 /* Look for the closing `}'. */
10923 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10925 /* Otherwise, there's just one declaration. */
10928 bool saved_in_unbraced_linkage_specification_p
;
10930 saved_in_unbraced_linkage_specification_p
10931 = parser
->in_unbraced_linkage_specification_p
;
10932 parser
->in_unbraced_linkage_specification_p
= true;
10933 cp_parser_declaration (parser
);
10934 parser
->in_unbraced_linkage_specification_p
10935 = saved_in_unbraced_linkage_specification_p
;
10938 /* We're done with the linkage-specification. */
10939 pop_lang_context ();
10942 /* Parse a static_assert-declaration.
10944 static_assert-declaration:
10945 static_assert ( constant-expression , string-literal ) ;
10947 If MEMBER_P, this static_assert is a class member. */
10950 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
10955 location_t saved_loc
;
10958 /* Peek at the `static_assert' token so we can keep track of exactly
10959 where the static assertion started. */
10960 token
= cp_lexer_peek_token (parser
->lexer
);
10961 saved_loc
= token
->location
;
10963 /* Look for the `static_assert' keyword. */
10964 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
10968 /* We know we are in a static assertion; commit to any tentative
10970 if (cp_parser_parsing_tentatively (parser
))
10971 cp_parser_commit_to_tentative_parse (parser
);
10973 /* Parse the `(' starting the static assertion condition. */
10974 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10976 /* Parse the constant-expression. Allow a non-constant expression
10977 here in order to give better diagnostics in finish_static_assert. */
10979 cp_parser_constant_expression (parser
,
10980 /*allow_non_constant_p=*/true,
10981 /*non_constant_p=*/&dummy
);
10983 /* Parse the separating `,'. */
10984 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10986 /* Parse the string-literal message. */
10987 message
= cp_parser_string_literal (parser
,
10988 /*translate=*/false,
10991 /* A `)' completes the static assertion. */
10992 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10993 cp_parser_skip_to_closing_parenthesis (parser
,
10994 /*recovering=*/true,
10995 /*or_comma=*/false,
10996 /*consume_paren=*/true);
10998 /* A semicolon terminates the declaration. */
10999 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11001 /* Complete the static assertion, which may mean either processing
11002 the static assert now or saving it for template instantiation. */
11003 finish_static_assert (condition
, message
, saved_loc
, member_p
);
11006 /* Parse a `decltype' type. Returns the type.
11008 simple-type-specifier:
11009 decltype ( expression ) */
11012 cp_parser_decltype (cp_parser
*parser
)
11015 bool id_expression_or_member_access_p
= false;
11016 const char *saved_message
;
11017 bool saved_integral_constant_expression_p
;
11018 bool saved_non_integral_constant_expression_p
;
11019 cp_token
*id_expr_start_token
;
11020 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
11022 if (start_token
->type
== CPP_DECLTYPE
)
11024 /* Already parsed. */
11025 cp_lexer_consume_token (parser
->lexer
);
11026 return start_token
->u
.value
;
11029 /* Look for the `decltype' token. */
11030 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
11031 return error_mark_node
;
11033 /* Types cannot be defined in a `decltype' expression. Save away the
11035 saved_message
= parser
->type_definition_forbidden_message
;
11037 /* And create the new one. */
11038 parser
->type_definition_forbidden_message
11039 = G_("types may not be defined in %<decltype%> expressions");
11041 /* The restrictions on constant-expressions do not apply inside
11042 decltype expressions. */
11043 saved_integral_constant_expression_p
11044 = parser
->integral_constant_expression_p
;
11045 saved_non_integral_constant_expression_p
11046 = parser
->non_integral_constant_expression_p
;
11047 parser
->integral_constant_expression_p
= false;
11049 /* Do not actually evaluate the expression. */
11050 ++cp_unevaluated_operand
;
11052 /* Do not warn about problems with the expression. */
11053 ++c_inhibit_evaluation_warnings
;
11055 /* Parse the opening `('. */
11056 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
11057 return error_mark_node
;
11059 /* First, try parsing an id-expression. */
11060 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
11061 cp_parser_parse_tentatively (parser
);
11062 expr
= cp_parser_id_expression (parser
,
11063 /*template_keyword_p=*/false,
11064 /*check_dependency_p=*/true,
11065 /*template_p=*/NULL
,
11066 /*declarator_p=*/false,
11067 /*optional_p=*/false);
11069 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
11071 bool non_integral_constant_expression_p
= false;
11072 tree id_expression
= expr
;
11074 const char *error_msg
;
11076 if (TREE_CODE (expr
) == IDENTIFIER_NODE
)
11077 /* Lookup the name we got back from the id-expression. */
11078 expr
= cp_parser_lookup_name (parser
, expr
,
11080 /*is_template=*/false,
11081 /*is_namespace=*/false,
11082 /*check_dependency=*/true,
11083 /*ambiguous_decls=*/NULL
,
11084 id_expr_start_token
->location
);
11087 && expr
!= error_mark_node
11088 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
11089 && TREE_CODE (expr
) != TYPE_DECL
11090 && (TREE_CODE (expr
) != BIT_NOT_EXPR
11091 || !TYPE_P (TREE_OPERAND (expr
, 0)))
11092 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11094 /* Complete lookup of the id-expression. */
11095 expr
= (finish_id_expression
11096 (id_expression
, expr
, parser
->scope
, &idk
,
11097 /*integral_constant_expression_p=*/false,
11098 /*allow_non_integral_constant_expression_p=*/true,
11099 &non_integral_constant_expression_p
,
11100 /*template_p=*/false,
11102 /*address_p=*/false,
11103 /*template_arg_p=*/false,
11105 id_expr_start_token
->location
));
11107 if (expr
== error_mark_node
)
11108 /* We found an id-expression, but it was something that we
11109 should not have found. This is an error, not something
11110 we can recover from, so note that we found an
11111 id-expression and we'll recover as gracefully as
11113 id_expression_or_member_access_p
= true;
11117 && expr
!= error_mark_node
11118 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11119 /* We have an id-expression. */
11120 id_expression_or_member_access_p
= true;
11123 if (!id_expression_or_member_access_p
)
11125 /* Abort the id-expression parse. */
11126 cp_parser_abort_tentative_parse (parser
);
11128 /* Parsing tentatively, again. */
11129 cp_parser_parse_tentatively (parser
);
11131 /* Parse a class member access. */
11132 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
11134 /*member_access_only_p=*/true, NULL
);
11137 && expr
!= error_mark_node
11138 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
11139 /* We have an id-expression. */
11140 id_expression_or_member_access_p
= true;
11143 if (id_expression_or_member_access_p
)
11144 /* We have parsed the complete id-expression or member access. */
11145 cp_parser_parse_definitely (parser
);
11148 bool saved_greater_than_is_operator_p
;
11150 /* Abort our attempt to parse an id-expression or member access
11152 cp_parser_abort_tentative_parse (parser
);
11154 /* Within a parenthesized expression, a `>' token is always
11155 the greater-than operator. */
11156 saved_greater_than_is_operator_p
11157 = parser
->greater_than_is_operator_p
;
11158 parser
->greater_than_is_operator_p
= true;
11160 /* Parse a full expression. */
11161 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
11163 /* The `>' token might be the end of a template-id or
11164 template-parameter-list now. */
11165 parser
->greater_than_is_operator_p
11166 = saved_greater_than_is_operator_p
;
11169 /* Go back to evaluating expressions. */
11170 --cp_unevaluated_operand
;
11171 --c_inhibit_evaluation_warnings
;
11173 /* Restore the old message and the integral constant expression
11175 parser
->type_definition_forbidden_message
= saved_message
;
11176 parser
->integral_constant_expression_p
11177 = saved_integral_constant_expression_p
;
11178 parser
->non_integral_constant_expression_p
11179 = saved_non_integral_constant_expression_p
;
11181 /* Parse to the closing `)'. */
11182 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
11184 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11185 /*consume_paren=*/true);
11186 return error_mark_node
;
11189 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
11190 tf_warning_or_error
);
11192 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11194 start_token
->type
= CPP_DECLTYPE
;
11195 start_token
->u
.value
= expr
;
11196 start_token
->keyword
= RID_MAX
;
11197 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
11202 /* Special member functions [gram.special] */
11204 /* Parse a conversion-function-id.
11206 conversion-function-id:
11207 operator conversion-type-id
11209 Returns an IDENTIFIER_NODE representing the operator. */
11212 cp_parser_conversion_function_id (cp_parser
* parser
)
11216 tree saved_qualifying_scope
;
11217 tree saved_object_scope
;
11218 tree pushed_scope
= NULL_TREE
;
11220 /* Look for the `operator' token. */
11221 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
11222 return error_mark_node
;
11223 /* When we parse the conversion-type-id, the current scope will be
11224 reset. However, we need that information in able to look up the
11225 conversion function later, so we save it here. */
11226 saved_scope
= parser
->scope
;
11227 saved_qualifying_scope
= parser
->qualifying_scope
;
11228 saved_object_scope
= parser
->object_scope
;
11229 /* We must enter the scope of the class so that the names of
11230 entities declared within the class are available in the
11231 conversion-type-id. For example, consider:
11238 S::operator I() { ... }
11240 In order to see that `I' is a type-name in the definition, we
11241 must be in the scope of `S'. */
11243 pushed_scope
= push_scope (saved_scope
);
11244 /* Parse the conversion-type-id. */
11245 type
= cp_parser_conversion_type_id (parser
);
11246 /* Leave the scope of the class, if any. */
11248 pop_scope (pushed_scope
);
11249 /* Restore the saved scope. */
11250 parser
->scope
= saved_scope
;
11251 parser
->qualifying_scope
= saved_qualifying_scope
;
11252 parser
->object_scope
= saved_object_scope
;
11253 /* If the TYPE is invalid, indicate failure. */
11254 if (type
== error_mark_node
)
11255 return error_mark_node
;
11256 return mangle_conv_op_name_for_type (type
);
11259 /* Parse a conversion-type-id:
11261 conversion-type-id:
11262 type-specifier-seq conversion-declarator [opt]
11264 Returns the TYPE specified. */
11267 cp_parser_conversion_type_id (cp_parser
* parser
)
11270 cp_decl_specifier_seq type_specifiers
;
11271 cp_declarator
*declarator
;
11272 tree type_specified
;
11274 /* Parse the attributes. */
11275 attributes
= cp_parser_attributes_opt (parser
);
11276 /* Parse the type-specifiers. */
11277 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
11278 /*is_trailing_return=*/false,
11280 /* If that didn't work, stop. */
11281 if (type_specifiers
.type
== error_mark_node
)
11282 return error_mark_node
;
11283 /* Parse the conversion-declarator. */
11284 declarator
= cp_parser_conversion_declarator_opt (parser
);
11286 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
11287 /*initialized=*/0, &attributes
);
11289 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
11291 /* Don't give this error when parsing tentatively. This happens to
11292 work because we always parse this definitively once. */
11293 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
11294 && type_uses_auto (type_specified
))
11296 if (cxx_dialect
< cxx1y
)
11298 error ("invalid use of %<auto%> in conversion operator");
11299 return error_mark_node
;
11301 else if (template_parm_scope_p ())
11302 warning (0, "use of %<auto%> in member template "
11303 "conversion operator can never be deduced");
11306 return type_specified
;
11309 /* Parse an (optional) conversion-declarator.
11311 conversion-declarator:
11312 ptr-operator conversion-declarator [opt]
11316 static cp_declarator
*
11317 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
11319 enum tree_code code
;
11321 cp_cv_quals cv_quals
;
11323 /* We don't know if there's a ptr-operator next, or not. */
11324 cp_parser_parse_tentatively (parser
);
11325 /* Try the ptr-operator. */
11326 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
);
11327 /* If it worked, look for more conversion-declarators. */
11328 if (cp_parser_parse_definitely (parser
))
11330 cp_declarator
*declarator
;
11332 /* Parse another optional declarator. */
11333 declarator
= cp_parser_conversion_declarator_opt (parser
);
11335 return cp_parser_make_indirect_declarator
11336 (code
, class_type
, cv_quals
, declarator
);
11342 /* Parse an (optional) ctor-initializer.
11345 : mem-initializer-list
11347 Returns TRUE iff the ctor-initializer was actually present. */
11350 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
11352 /* If the next token is not a `:', then there is no
11353 ctor-initializer. */
11354 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
11356 /* Do default initialization of any bases and members. */
11357 if (DECL_CONSTRUCTOR_P (current_function_decl
))
11358 finish_mem_initializers (NULL_TREE
);
11363 /* Consume the `:' token. */
11364 cp_lexer_consume_token (parser
->lexer
);
11365 /* And the mem-initializer-list. */
11366 cp_parser_mem_initializer_list (parser
);
11371 /* Parse a mem-initializer-list.
11373 mem-initializer-list:
11374 mem-initializer ... [opt]
11375 mem-initializer ... [opt] , mem-initializer-list */
11378 cp_parser_mem_initializer_list (cp_parser
* parser
)
11380 tree mem_initializer_list
= NULL_TREE
;
11381 tree target_ctor
= error_mark_node
;
11382 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11384 /* Let the semantic analysis code know that we are starting the
11385 mem-initializer-list. */
11386 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
11387 error_at (token
->location
,
11388 "only constructors take member initializers");
11390 /* Loop through the list. */
11393 tree mem_initializer
;
11395 token
= cp_lexer_peek_token (parser
->lexer
);
11396 /* Parse the mem-initializer. */
11397 mem_initializer
= cp_parser_mem_initializer (parser
);
11398 /* If the next token is a `...', we're expanding member initializers. */
11399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11401 /* Consume the `...'. */
11402 cp_lexer_consume_token (parser
->lexer
);
11404 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11405 can be expanded but members cannot. */
11406 if (mem_initializer
!= error_mark_node
11407 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
11409 error_at (token
->location
,
11410 "cannot expand initializer for member %<%D%>",
11411 TREE_PURPOSE (mem_initializer
));
11412 mem_initializer
= error_mark_node
;
11415 /* Construct the pack expansion type. */
11416 if (mem_initializer
!= error_mark_node
)
11417 mem_initializer
= make_pack_expansion (mem_initializer
);
11419 if (target_ctor
!= error_mark_node
11420 && mem_initializer
!= error_mark_node
)
11422 error ("mem-initializer for %qD follows constructor delegation",
11423 TREE_PURPOSE (mem_initializer
));
11424 mem_initializer
= error_mark_node
;
11426 /* Look for a target constructor. */
11427 if (mem_initializer
!= error_mark_node
11428 && TYPE_P (TREE_PURPOSE (mem_initializer
))
11429 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
11431 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
11432 if (mem_initializer_list
)
11434 error ("constructor delegation follows mem-initializer for %qD",
11435 TREE_PURPOSE (mem_initializer_list
));
11436 mem_initializer
= error_mark_node
;
11438 target_ctor
= mem_initializer
;
11440 /* Add it to the list, unless it was erroneous. */
11441 if (mem_initializer
!= error_mark_node
)
11443 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
11444 mem_initializer_list
= mem_initializer
;
11446 /* If the next token is not a `,', we're done. */
11447 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
11449 /* Consume the `,' token. */
11450 cp_lexer_consume_token (parser
->lexer
);
11453 /* Perform semantic analysis. */
11454 if (DECL_CONSTRUCTOR_P (current_function_decl
))
11455 finish_mem_initializers (mem_initializer_list
);
11458 /* Parse a mem-initializer.
11461 mem-initializer-id ( expression-list [opt] )
11462 mem-initializer-id braced-init-list
11467 ( expression-list [opt] )
11469 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11470 class) or FIELD_DECL (for a non-static data member) to initialize;
11471 the TREE_VALUE is the expression-list. An empty initialization
11472 list is represented by void_list_node. */
11475 cp_parser_mem_initializer (cp_parser
* parser
)
11477 tree mem_initializer_id
;
11478 tree expression_list
;
11480 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11482 /* Find out what is being initialized. */
11483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
11485 permerror (token
->location
,
11486 "anachronistic old-style base class initializer");
11487 mem_initializer_id
= NULL_TREE
;
11491 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
11492 if (mem_initializer_id
== error_mark_node
)
11493 return mem_initializer_id
;
11495 member
= expand_member_init (mem_initializer_id
);
11496 if (member
&& !DECL_P (member
))
11497 in_base_initializer
= 1;
11499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11501 bool expr_non_constant_p
;
11502 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11503 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11504 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
11505 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
11510 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
11512 /*allow_expansion_p=*/true,
11513 /*non_constant_p=*/NULL
);
11515 return error_mark_node
;
11516 expression_list
= build_tree_list_vec (vec
);
11517 release_tree_vector (vec
);
11520 if (expression_list
== error_mark_node
)
11521 return error_mark_node
;
11522 if (!expression_list
)
11523 expression_list
= void_type_node
;
11525 in_base_initializer
= 0;
11527 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
11530 /* Parse a mem-initializer-id.
11532 mem-initializer-id:
11533 :: [opt] nested-name-specifier [opt] class-name
11536 Returns a TYPE indicating the class to be initializer for the first
11537 production. Returns an IDENTIFIER_NODE indicating the data member
11538 to be initialized for the second production. */
11541 cp_parser_mem_initializer_id (cp_parser
* parser
)
11543 bool global_scope_p
;
11544 bool nested_name_specifier_p
;
11545 bool template_p
= false;
11548 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11550 /* `typename' is not allowed in this context ([temp.res]). */
11551 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
11553 error_at (token
->location
,
11554 "keyword %<typename%> not allowed in this context (a qualified "
11555 "member initializer is implicitly a type)");
11556 cp_lexer_consume_token (parser
->lexer
);
11558 /* Look for the optional `::' operator. */
11560 = (cp_parser_global_scope_opt (parser
,
11561 /*current_scope_valid_p=*/false)
11563 /* Look for the optional nested-name-specifier. The simplest way to
11568 The keyword `typename' is not permitted in a base-specifier or
11569 mem-initializer; in these contexts a qualified name that
11570 depends on a template-parameter is implicitly assumed to be a
11573 is to assume that we have seen the `typename' keyword at this
11575 nested_name_specifier_p
11576 = (cp_parser_nested_name_specifier_opt (parser
,
11577 /*typename_keyword_p=*/true,
11578 /*check_dependency_p=*/true,
11580 /*is_declaration=*/true)
11582 if (nested_name_specifier_p
)
11583 template_p
= cp_parser_optional_template_keyword (parser
);
11584 /* If there is a `::' operator or a nested-name-specifier, then we
11585 are definitely looking for a class-name. */
11586 if (global_scope_p
|| nested_name_specifier_p
)
11587 return cp_parser_class_name (parser
,
11588 /*typename_keyword_p=*/true,
11589 /*template_keyword_p=*/template_p
,
11591 /*check_dependency_p=*/true,
11592 /*class_head_p=*/false,
11593 /*is_declaration=*/true);
11594 /* Otherwise, we could also be looking for an ordinary identifier. */
11595 cp_parser_parse_tentatively (parser
);
11596 /* Try a class-name. */
11597 id
= cp_parser_class_name (parser
,
11598 /*typename_keyword_p=*/true,
11599 /*template_keyword_p=*/false,
11601 /*check_dependency_p=*/true,
11602 /*class_head_p=*/false,
11603 /*is_declaration=*/true);
11604 /* If we found one, we're done. */
11605 if (cp_parser_parse_definitely (parser
))
11607 /* Otherwise, look for an ordinary identifier. */
11608 return cp_parser_identifier (parser
);
11611 /* Overloading [gram.over] */
11613 /* Parse an operator-function-id.
11615 operator-function-id:
11618 Returns an IDENTIFIER_NODE for the operator which is a
11619 human-readable spelling of the identifier, e.g., `operator +'. */
11622 cp_parser_operator_function_id (cp_parser
* parser
)
11624 /* Look for the `operator' keyword. */
11625 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
11626 return error_mark_node
;
11627 /* And then the name of the operator itself. */
11628 return cp_parser_operator (parser
);
11631 /* Return an identifier node for a user-defined literal operator.
11632 The suffix identifier is chained to the operator name identifier. */
11635 cp_literal_operator_id (const char* name
)
11638 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
11639 + strlen (name
) + 10);
11640 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
11641 identifier
= get_identifier (buffer
);
11642 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11647 /* Parse an operator.
11650 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11651 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11652 || ++ -- , ->* -> () []
11659 Returns an IDENTIFIER_NODE for the operator which is a
11660 human-readable spelling of the identifier, e.g., `operator +'. */
11663 cp_parser_operator (cp_parser
* parser
)
11665 tree id
= NULL_TREE
;
11668 /* Peek at the next token. */
11669 token
= cp_lexer_peek_token (parser
->lexer
);
11670 /* Figure out which operator we have. */
11671 switch (token
->type
)
11677 /* The keyword should be either `new' or `delete'. */
11678 if (token
->keyword
== RID_NEW
)
11680 else if (token
->keyword
== RID_DELETE
)
11685 /* Consume the `new' or `delete' token. */
11686 cp_lexer_consume_token (parser
->lexer
);
11688 /* Peek at the next token. */
11689 token
= cp_lexer_peek_token (parser
->lexer
);
11690 /* If it's a `[' token then this is the array variant of the
11692 if (token
->type
== CPP_OPEN_SQUARE
)
11694 /* Consume the `[' token. */
11695 cp_lexer_consume_token (parser
->lexer
);
11696 /* Look for the `]' token. */
11697 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
11698 id
= ansi_opname (op
== NEW_EXPR
11699 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
11701 /* Otherwise, we have the non-array variant. */
11703 id
= ansi_opname (op
);
11709 id
= ansi_opname (PLUS_EXPR
);
11713 id
= ansi_opname (MINUS_EXPR
);
11717 id
= ansi_opname (MULT_EXPR
);
11721 id
= ansi_opname (TRUNC_DIV_EXPR
);
11725 id
= ansi_opname (TRUNC_MOD_EXPR
);
11729 id
= ansi_opname (BIT_XOR_EXPR
);
11733 id
= ansi_opname (BIT_AND_EXPR
);
11737 id
= ansi_opname (BIT_IOR_EXPR
);
11741 id
= ansi_opname (BIT_NOT_EXPR
);
11745 id
= ansi_opname (TRUTH_NOT_EXPR
);
11749 id
= ansi_assopname (NOP_EXPR
);
11753 id
= ansi_opname (LT_EXPR
);
11757 id
= ansi_opname (GT_EXPR
);
11761 id
= ansi_assopname (PLUS_EXPR
);
11765 id
= ansi_assopname (MINUS_EXPR
);
11769 id
= ansi_assopname (MULT_EXPR
);
11773 id
= ansi_assopname (TRUNC_DIV_EXPR
);
11777 id
= ansi_assopname (TRUNC_MOD_EXPR
);
11781 id
= ansi_assopname (BIT_XOR_EXPR
);
11785 id
= ansi_assopname (BIT_AND_EXPR
);
11789 id
= ansi_assopname (BIT_IOR_EXPR
);
11793 id
= ansi_opname (LSHIFT_EXPR
);
11797 id
= ansi_opname (RSHIFT_EXPR
);
11800 case CPP_LSHIFT_EQ
:
11801 id
= ansi_assopname (LSHIFT_EXPR
);
11804 case CPP_RSHIFT_EQ
:
11805 id
= ansi_assopname (RSHIFT_EXPR
);
11809 id
= ansi_opname (EQ_EXPR
);
11813 id
= ansi_opname (NE_EXPR
);
11817 id
= ansi_opname (LE_EXPR
);
11820 case CPP_GREATER_EQ
:
11821 id
= ansi_opname (GE_EXPR
);
11825 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
11829 id
= ansi_opname (TRUTH_ORIF_EXPR
);
11832 case CPP_PLUS_PLUS
:
11833 id
= ansi_opname (POSTINCREMENT_EXPR
);
11836 case CPP_MINUS_MINUS
:
11837 id
= ansi_opname (PREDECREMENT_EXPR
);
11841 id
= ansi_opname (COMPOUND_EXPR
);
11844 case CPP_DEREF_STAR
:
11845 id
= ansi_opname (MEMBER_REF
);
11849 id
= ansi_opname (COMPONENT_REF
);
11852 case CPP_OPEN_PAREN
:
11853 /* Consume the `('. */
11854 cp_lexer_consume_token (parser
->lexer
);
11855 /* Look for the matching `)'. */
11856 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11857 return ansi_opname (CALL_EXPR
);
11859 case CPP_OPEN_SQUARE
:
11860 /* Consume the `['. */
11861 cp_lexer_consume_token (parser
->lexer
);
11862 /* Look for the matching `]'. */
11863 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
11864 return ansi_opname (ARRAY_REF
);
11867 if (cxx_dialect
== cxx98
)
11868 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
11869 if (TREE_STRING_LENGTH (token
->u
.value
) > 2)
11871 error ("expected empty string after %<operator%> keyword");
11872 return error_mark_node
;
11874 /* Consume the string. */
11875 cp_lexer_consume_token (parser
->lexer
);
11876 /* Look for the suffix identifier. */
11877 token
= cp_lexer_peek_token (parser
->lexer
);
11878 if (token
->type
== CPP_NAME
)
11880 id
= cp_parser_identifier (parser
);
11881 if (id
!= error_mark_node
)
11883 const char *name
= IDENTIFIER_POINTER (id
);
11884 return cp_literal_operator_id (name
);
11889 error ("expected suffix identifier");
11890 return error_mark_node
;
11893 case CPP_STRING_USERDEF
:
11894 error ("missing space between %<\"\"%> and suffix identifier");
11895 return error_mark_node
;
11898 /* Anything else is an error. */
11902 /* If we have selected an identifier, we need to consume the
11905 cp_lexer_consume_token (parser
->lexer
);
11906 /* Otherwise, no valid operator name was present. */
11909 cp_parser_error (parser
, "expected operator");
11910 id
= error_mark_node
;
11916 /* Parse a template-declaration.
11918 template-declaration:
11919 export [opt] template < template-parameter-list > declaration
11921 If MEMBER_P is TRUE, this template-declaration occurs within a
11924 The grammar rule given by the standard isn't correct. What
11925 is really meant is:
11927 template-declaration:
11928 export [opt] template-parameter-list-seq
11929 decl-specifier-seq [opt] init-declarator [opt] ;
11930 export [opt] template-parameter-list-seq
11931 function-definition
11933 template-parameter-list-seq:
11934 template-parameter-list-seq [opt]
11935 template < template-parameter-list > */
11938 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
11940 /* Check for `export'. */
11941 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
11943 /* Consume the `export' token. */
11944 cp_lexer_consume_token (parser
->lexer
);
11945 /* Warn that we do not support `export'. */
11946 warning (0, "keyword %<export%> not implemented, and will be ignored");
11949 cp_parser_template_declaration_after_export (parser
, member_p
);
11952 /* Parse a template-parameter-list.
11954 template-parameter-list:
11956 template-parameter-list , template-parameter
11958 Returns a TREE_LIST. Each node represents a template parameter.
11959 The nodes are connected via their TREE_CHAINs. */
11962 cp_parser_template_parameter_list (cp_parser
* parser
)
11964 tree parameter_list
= NULL_TREE
;
11966 begin_template_parm_list ();
11968 /* The loop below parses the template parms. We first need to know
11969 the total number of template parms to be able to compute proper
11970 canonical types of each dependent type. So after the loop, when
11971 we know the total number of template parms,
11972 end_template_parm_list computes the proper canonical types and
11973 fixes up the dependent types accordingly. */
11978 bool is_parameter_pack
;
11979 location_t parm_loc
;
11981 /* Parse the template-parameter. */
11982 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11983 parameter
= cp_parser_template_parameter (parser
,
11985 &is_parameter_pack
);
11986 /* Add it to the list. */
11987 if (parameter
!= error_mark_node
)
11988 parameter_list
= process_template_parm (parameter_list
,
11992 is_parameter_pack
);
11995 tree err_parm
= build_tree_list (parameter
, parameter
);
11996 parameter_list
= chainon (parameter_list
, err_parm
);
11999 /* If the next token is not a `,', we're done. */
12000 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12002 /* Otherwise, consume the `,' token. */
12003 cp_lexer_consume_token (parser
->lexer
);
12006 return end_template_parm_list (parameter_list
);
12009 /* Parse a template-parameter.
12011 template-parameter:
12013 parameter-declaration
12015 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12016 the parameter. The TREE_PURPOSE is the default value, if any.
12017 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12018 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12019 set to true iff this parameter is a parameter pack. */
12022 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
12023 bool *is_parameter_pack
)
12026 cp_parameter_declarator
*parameter_declarator
;
12027 cp_declarator
*id_declarator
;
12030 /* Assume it is a type parameter or a template parameter. */
12031 *is_non_type
= false;
12032 /* Assume it not a parameter pack. */
12033 *is_parameter_pack
= false;
12034 /* Peek at the next token. */
12035 token
= cp_lexer_peek_token (parser
->lexer
);
12036 /* If it is `class' or `template', we have a type-parameter. */
12037 if (token
->keyword
== RID_TEMPLATE
)
12038 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12039 /* If it is `class' or `typename' we do not know yet whether it is a
12040 type parameter or a non-type parameter. Consider:
12042 template <typename T, typename T::X X> ...
12046 template <class C, class D*> ...
12048 Here, the first parameter is a type parameter, and the second is
12049 a non-type parameter. We can tell by looking at the token after
12050 the identifier -- if it is a `,', `=', or `>' then we have a type
12052 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
12054 /* Peek at the token after `class' or `typename'. */
12055 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12056 /* If it's an ellipsis, we have a template type parameter
12058 if (token
->type
== CPP_ELLIPSIS
)
12059 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12060 /* If it's an identifier, skip it. */
12061 if (token
->type
== CPP_NAME
)
12062 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
12063 /* Now, see if the token looks like the end of a template
12065 if (token
->type
== CPP_COMMA
12066 || token
->type
== CPP_EQ
12067 || token
->type
== CPP_GREATER
)
12068 return cp_parser_type_parameter (parser
, is_parameter_pack
);
12071 /* Otherwise, it is a non-type parameter.
12075 When parsing a default template-argument for a non-type
12076 template-parameter, the first non-nested `>' is taken as the end
12077 of the template parameter-list rather than a greater-than
12079 *is_non_type
= true;
12080 parameter_declarator
12081 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
12082 /*parenthesized_p=*/NULL
);
12084 /* If the parameter declaration is marked as a parameter pack, set
12085 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12086 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12088 if (parameter_declarator
12089 && parameter_declarator
->declarator
12090 && parameter_declarator
->declarator
->parameter_pack_p
)
12092 *is_parameter_pack
= true;
12093 parameter_declarator
->declarator
->parameter_pack_p
= false;
12096 /* If the next token is an ellipsis, and we don't already have it
12097 marked as a parameter pack, then we have a parameter pack (that
12098 has no declarator). */
12099 if (!*is_parameter_pack
12100 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
12101 && declarator_can_be_parameter_pack (parameter_declarator
->declarator
))
12103 /* Consume the `...'. */
12104 cp_lexer_consume_token (parser
->lexer
);
12105 maybe_warn_variadic_templates ();
12107 *is_parameter_pack
= true;
12109 /* We might end up with a pack expansion as the type of the non-type
12110 template parameter, in which case this is a non-type template
12112 else if (parameter_declarator
12113 && parameter_declarator
->decl_specifiers
.type
12114 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
12116 *is_parameter_pack
= true;
12117 parameter_declarator
->decl_specifiers
.type
=
12118 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
12121 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12123 /* Parameter packs cannot have default arguments. However, a
12124 user may try to do so, so we'll parse them and give an
12125 appropriate diagnostic here. */
12127 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12129 /* Find the name of the parameter pack. */
12130 id_declarator
= parameter_declarator
->declarator
;
12131 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
12132 id_declarator
= id_declarator
->declarator
;
12134 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
12135 error_at (start_token
->location
,
12136 "template parameter pack %qD cannot have a default argument",
12137 id_declarator
->u
.id
.unqualified_name
);
12139 error_at (start_token
->location
,
12140 "template parameter pack cannot have a default argument");
12142 /* Parse the default argument, but throw away the result. */
12143 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
12146 parm
= grokdeclarator (parameter_declarator
->declarator
,
12147 ¶meter_declarator
->decl_specifiers
,
12148 TPARM
, /*initialized=*/0,
12149 /*attrlist=*/NULL
);
12150 if (parm
== error_mark_node
)
12151 return error_mark_node
;
12153 return build_tree_list (parameter_declarator
->default_argument
, parm
);
12156 /* Parse a type-parameter.
12159 class identifier [opt]
12160 class identifier [opt] = type-id
12161 typename identifier [opt]
12162 typename identifier [opt] = type-id
12163 template < template-parameter-list > class identifier [opt]
12164 template < template-parameter-list > class identifier [opt]
12167 GNU Extension (variadic templates):
12170 class ... identifier [opt]
12171 typename ... identifier [opt]
12173 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12174 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12175 the declaration of the parameter.
12177 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12180 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
12185 /* Look for a keyword to tell us what kind of parameter this is. */
12186 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
12188 return error_mark_node
;
12190 switch (token
->keyword
)
12196 tree default_argument
;
12198 /* If the next token is an ellipsis, we have a template
12200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12202 /* Consume the `...' token. */
12203 cp_lexer_consume_token (parser
->lexer
);
12204 maybe_warn_variadic_templates ();
12206 *is_parameter_pack
= true;
12209 /* If the next token is an identifier, then it names the
12211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12212 identifier
= cp_parser_identifier (parser
);
12214 identifier
= NULL_TREE
;
12216 /* Create the parameter. */
12217 parameter
= finish_template_type_parm (class_type_node
, identifier
);
12219 /* If the next token is an `=', we have a default argument. */
12220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12222 /* Consume the `=' token. */
12223 cp_lexer_consume_token (parser
->lexer
);
12224 /* Parse the default-argument. */
12225 push_deferring_access_checks (dk_no_deferred
);
12226 default_argument
= cp_parser_type_id (parser
);
12228 /* Template parameter packs cannot have default
12230 if (*is_parameter_pack
)
12233 error_at (token
->location
,
12234 "template parameter pack %qD cannot have a "
12235 "default argument", identifier
);
12237 error_at (token
->location
,
12238 "template parameter packs cannot have "
12239 "default arguments");
12240 default_argument
= NULL_TREE
;
12242 pop_deferring_access_checks ();
12245 default_argument
= NULL_TREE
;
12247 /* Create the combined representation of the parameter and the
12248 default argument. */
12249 parameter
= build_tree_list (default_argument
, parameter
);
12256 tree default_argument
;
12258 /* Look for the `<'. */
12259 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
12260 /* Parse the template-parameter-list. */
12261 cp_parser_template_parameter_list (parser
);
12262 /* Look for the `>'. */
12263 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
12264 /* Look for the `class' keyword. */
12265 cp_parser_require_keyword (parser
, RID_CLASS
, RT_CLASS
);
12266 /* If the next token is an ellipsis, we have a template
12268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12270 /* Consume the `...' token. */
12271 cp_lexer_consume_token (parser
->lexer
);
12272 maybe_warn_variadic_templates ();
12274 *is_parameter_pack
= true;
12276 /* If the next token is an `=', then there is a
12277 default-argument. If the next token is a `>', we are at
12278 the end of the parameter-list. If the next token is a `,',
12279 then we are at the end of this parameter. */
12280 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12281 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
12282 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12284 identifier
= cp_parser_identifier (parser
);
12285 /* Treat invalid names as if the parameter were nameless. */
12286 if (identifier
== error_mark_node
)
12287 identifier
= NULL_TREE
;
12290 identifier
= NULL_TREE
;
12292 /* Create the template parameter. */
12293 parameter
= finish_template_template_parm (class_type_node
,
12296 /* If the next token is an `=', then there is a
12297 default-argument. */
12298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12302 /* Consume the `='. */
12303 cp_lexer_consume_token (parser
->lexer
);
12304 /* Parse the id-expression. */
12305 push_deferring_access_checks (dk_no_deferred
);
12306 /* save token before parsing the id-expression, for error
12308 token
= cp_lexer_peek_token (parser
->lexer
);
12310 = cp_parser_id_expression (parser
,
12311 /*template_keyword_p=*/false,
12312 /*check_dependency_p=*/true,
12313 /*template_p=*/&is_template
,
12314 /*declarator_p=*/false,
12315 /*optional_p=*/false);
12316 if (TREE_CODE (default_argument
) == TYPE_DECL
)
12317 /* If the id-expression was a template-id that refers to
12318 a template-class, we already have the declaration here,
12319 so no further lookup is needed. */
12322 /* Look up the name. */
12324 = cp_parser_lookup_name (parser
, default_argument
,
12326 /*is_template=*/is_template
,
12327 /*is_namespace=*/false,
12328 /*check_dependency=*/true,
12329 /*ambiguous_decls=*/NULL
,
12331 /* See if the default argument is valid. */
12333 = check_template_template_default_arg (default_argument
);
12335 /* Template parameter packs cannot have default
12337 if (*is_parameter_pack
)
12340 error_at (token
->location
,
12341 "template parameter pack %qD cannot "
12342 "have a default argument",
12345 error_at (token
->location
, "template parameter packs cannot "
12346 "have default arguments");
12347 default_argument
= NULL_TREE
;
12349 pop_deferring_access_checks ();
12352 default_argument
= NULL_TREE
;
12354 /* Create the combined representation of the parameter and the
12355 default argument. */
12356 parameter
= build_tree_list (default_argument
, parameter
);
12361 gcc_unreachable ();
12368 /* Parse a template-id.
12371 template-name < template-argument-list [opt] >
12373 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12374 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12375 returned. Otherwise, if the template-name names a function, or set
12376 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12377 names a class, returns a TYPE_DECL for the specialization.
12379 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12380 uninstantiated templates. */
12383 cp_parser_template_id (cp_parser
*parser
,
12384 bool template_keyword_p
,
12385 bool check_dependency_p
,
12386 enum tag_types tag_type
,
12387 bool is_declaration
)
12393 cp_token_position start_of_id
= 0;
12394 deferred_access_check
*chk
;
12395 VEC (deferred_access_check
,gc
) *access_check
;
12396 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
12397 bool is_identifier
;
12399 /* If the next token corresponds to a template-id, there is no need
12401 next_token
= cp_lexer_peek_token (parser
->lexer
);
12402 if (next_token
->type
== CPP_TEMPLATE_ID
)
12404 struct tree_check
*check_value
;
12406 /* Get the stored value. */
12407 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
12408 /* Perform any access checks that were deferred. */
12409 access_check
= check_value
->checks
;
12412 FOR_EACH_VEC_ELT (deferred_access_check
, access_check
, i
, chk
)
12413 perform_or_defer_access_check (chk
->binfo
,
12416 tf_warning_or_error
);
12418 /* Return the stored value. */
12419 return check_value
->value
;
12422 /* Avoid performing name lookup if there is no possibility of
12423 finding a template-id. */
12424 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
12425 || (next_token
->type
== CPP_NAME
12426 && !cp_parser_nth_token_starts_template_argument_list_p
12429 cp_parser_error (parser
, "expected template-id");
12430 return error_mark_node
;
12433 /* Remember where the template-id starts. */
12434 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
12435 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
12437 push_deferring_access_checks (dk_deferred
);
12439 /* Parse the template-name. */
12440 is_identifier
= false;
12441 templ
= cp_parser_template_name (parser
, template_keyword_p
,
12442 check_dependency_p
,
12446 if (templ
== error_mark_node
|| is_identifier
)
12448 pop_deferring_access_checks ();
12452 /* If we find the sequence `[:' after a template-name, it's probably
12453 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12454 parse correctly the argument list. */
12455 next_token
= cp_lexer_peek_token (parser
->lexer
);
12456 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12457 if (next_token
->type
== CPP_OPEN_SQUARE
12458 && next_token
->flags
& DIGRAPH
12459 && next_token_2
->type
== CPP_COLON
12460 && !(next_token_2
->flags
& PREV_WHITE
))
12462 cp_parser_parse_tentatively (parser
);
12463 /* Change `:' into `::'. */
12464 next_token_2
->type
= CPP_SCOPE
;
12465 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12467 cp_lexer_consume_token (parser
->lexer
);
12469 /* Parse the arguments. */
12470 arguments
= cp_parser_enclosed_template_argument_list (parser
);
12471 if (!cp_parser_parse_definitely (parser
))
12473 /* If we couldn't parse an argument list, then we revert our changes
12474 and return simply an error. Maybe this is not a template-id
12476 next_token_2
->type
= CPP_COLON
;
12477 cp_parser_error (parser
, "expected %<<%>");
12478 pop_deferring_access_checks ();
12479 return error_mark_node
;
12481 /* Otherwise, emit an error about the invalid digraph, but continue
12482 parsing because we got our argument list. */
12483 if (permerror (next_token
->location
,
12484 "%<<::%> cannot begin a template-argument list"))
12486 static bool hint
= false;
12487 inform (next_token
->location
,
12488 "%<<:%> is an alternate spelling for %<[%>."
12489 " Insert whitespace between %<<%> and %<::%>");
12490 if (!hint
&& !flag_permissive
)
12492 inform (next_token
->location
, "(if you use %<-fpermissive%>"
12493 " G++ will accept your code)");
12500 /* Look for the `<' that starts the template-argument-list. */
12501 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
12503 pop_deferring_access_checks ();
12504 return error_mark_node
;
12506 /* Parse the arguments. */
12507 arguments
= cp_parser_enclosed_template_argument_list (parser
);
12510 /* Build a representation of the specialization. */
12511 if (TREE_CODE (templ
) == IDENTIFIER_NODE
)
12512 template_id
= build_min_nt_loc (next_token
->location
,
12515 else if (DECL_TYPE_TEMPLATE_P (templ
)
12516 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
12518 bool entering_scope
;
12519 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12520 template (rather than some instantiation thereof) only if
12521 is not nested within some other construct. For example, in
12522 "template <typename T> void f(T) { A<T>::", A<T> is just an
12523 instantiation of A. */
12524 entering_scope
= (template_parm_scope_p ()
12525 && cp_lexer_next_token_is (parser
->lexer
,
12528 = finish_template_type (templ
, arguments
, entering_scope
);
12532 /* If it's not a class-template or a template-template, it should be
12533 a function-template. */
12534 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
12535 || TREE_CODE (templ
) == OVERLOAD
12536 || BASELINK_P (templ
)));
12538 template_id
= lookup_template_function (templ
, arguments
);
12541 /* If parsing tentatively, replace the sequence of tokens that makes
12542 up the template-id with a CPP_TEMPLATE_ID token. That way,
12543 should we re-parse the token stream, we will not have to repeat
12544 the effort required to do the parse, nor will we issue duplicate
12545 error messages about problems during instantiation of the
12549 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
12551 /* Reset the contents of the START_OF_ID token. */
12552 token
->type
= CPP_TEMPLATE_ID
;
12553 /* Retrieve any deferred checks. Do not pop this access checks yet
12554 so the memory will not be reclaimed during token replacing below. */
12555 token
->u
.tree_check_value
= ggc_alloc_cleared_tree_check ();
12556 token
->u
.tree_check_value
->value
= template_id
;
12557 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
12558 token
->keyword
= RID_MAX
;
12560 /* Purge all subsequent tokens. */
12561 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
12563 /* ??? Can we actually assume that, if template_id ==
12564 error_mark_node, we will have issued a diagnostic to the
12565 user, as opposed to simply marking the tentative parse as
12567 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
12568 error_at (token
->location
, "parse error in template argument list");
12571 pop_deferring_access_checks ();
12572 return template_id
;
12575 /* Parse a template-name.
12580 The standard should actually say:
12584 operator-function-id
12586 A defect report has been filed about this issue.
12588 A conversion-function-id cannot be a template name because they cannot
12589 be part of a template-id. In fact, looking at this code:
12591 a.operator K<int>()
12593 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12594 It is impossible to call a templated conversion-function-id with an
12595 explicit argument list, since the only allowed template parameter is
12596 the type to which it is converting.
12598 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12599 `template' keyword, in a construction like:
12603 In that case `f' is taken to be a template-name, even though there
12604 is no way of knowing for sure.
12606 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12607 name refers to a set of overloaded functions, at least one of which
12608 is a template, or an IDENTIFIER_NODE with the name of the template,
12609 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12610 names are looked up inside uninstantiated templates. */
12613 cp_parser_template_name (cp_parser
* parser
,
12614 bool template_keyword_p
,
12615 bool check_dependency_p
,
12616 bool is_declaration
,
12617 enum tag_types tag_type
,
12618 bool *is_identifier
)
12623 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12625 /* If the next token is `operator', then we have either an
12626 operator-function-id or a conversion-function-id. */
12627 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
12629 /* We don't know whether we're looking at an
12630 operator-function-id or a conversion-function-id. */
12631 cp_parser_parse_tentatively (parser
);
12632 /* Try an operator-function-id. */
12633 identifier
= cp_parser_operator_function_id (parser
);
12634 /* If that didn't work, try a conversion-function-id. */
12635 if (!cp_parser_parse_definitely (parser
))
12637 cp_parser_error (parser
, "expected template-name");
12638 return error_mark_node
;
12641 /* Look for the identifier. */
12643 identifier
= cp_parser_identifier (parser
);
12645 /* If we didn't find an identifier, we don't have a template-id. */
12646 if (identifier
== error_mark_node
)
12647 return error_mark_node
;
12649 /* If the name immediately followed the `template' keyword, then it
12650 is a template-name. However, if the next token is not `<', then
12651 we do not treat it as a template-name, since it is not being used
12652 as part of a template-id. This enables us to handle constructs
12655 template <typename T> struct S { S(); };
12656 template <typename T> S<T>::S();
12658 correctly. We would treat `S' as a template -- if it were `S<T>'
12659 -- but we do not if there is no `<'. */
12661 if (processing_template_decl
12662 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
12664 /* In a declaration, in a dependent context, we pretend that the
12665 "template" keyword was present in order to improve error
12666 recovery. For example, given:
12668 template <typename T> void f(T::X<int>);
12670 we want to treat "X<int>" as a template-id. */
12672 && !template_keyword_p
12673 && parser
->scope
&& TYPE_P (parser
->scope
)
12674 && check_dependency_p
12675 && dependent_scope_p (parser
->scope
)
12676 /* Do not do this for dtors (or ctors), since they never
12677 need the template keyword before their name. */
12678 && !constructor_name_p (identifier
, parser
->scope
))
12680 cp_token_position start
= 0;
12682 /* Explain what went wrong. */
12683 error_at (token
->location
, "non-template %qD used as template",
12685 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
12686 parser
->scope
, identifier
);
12687 /* If parsing tentatively, find the location of the "<" token. */
12688 if (cp_parser_simulate_error (parser
))
12689 start
= cp_lexer_token_position (parser
->lexer
, true);
12690 /* Parse the template arguments so that we can issue error
12691 messages about them. */
12692 cp_lexer_consume_token (parser
->lexer
);
12693 cp_parser_enclosed_template_argument_list (parser
);
12694 /* Skip tokens until we find a good place from which to
12695 continue parsing. */
12696 cp_parser_skip_to_closing_parenthesis (parser
,
12697 /*recovering=*/true,
12699 /*consume_paren=*/false);
12700 /* If parsing tentatively, permanently remove the
12701 template argument list. That will prevent duplicate
12702 error messages from being issued about the missing
12703 "template" keyword. */
12705 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
12707 *is_identifier
= true;
12711 /* If the "template" keyword is present, then there is generally
12712 no point in doing name-lookup, so we just return IDENTIFIER.
12713 But, if the qualifying scope is non-dependent then we can
12714 (and must) do name-lookup normally. */
12715 if (template_keyword_p
12717 || (TYPE_P (parser
->scope
)
12718 && dependent_type_p (parser
->scope
))))
12722 /* Look up the name. */
12723 decl
= cp_parser_lookup_name (parser
, identifier
,
12725 /*is_template=*/true,
12726 /*is_namespace=*/false,
12727 check_dependency_p
,
12728 /*ambiguous_decls=*/NULL
,
12731 /* If DECL is a template, then the name was a template-name. */
12732 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
12736 tree fn
= NULL_TREE
;
12738 /* The standard does not explicitly indicate whether a name that
12739 names a set of overloaded declarations, some of which are
12740 templates, is a template-name. However, such a name should
12741 be a template-name; otherwise, there is no way to form a
12742 template-id for the overloaded templates. */
12743 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
12744 if (TREE_CODE (fns
) == OVERLOAD
)
12745 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
12746 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
12751 /* The name does not name a template. */
12752 cp_parser_error (parser
, "expected template-name");
12753 return error_mark_node
;
12757 /* If DECL is dependent, and refers to a function, then just return
12758 its name; we will look it up again during template instantiation. */
12759 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
12761 tree scope
= ovl_scope (decl
);
12762 if (TYPE_P (scope
) && dependent_type_p (scope
))
12769 /* Parse a template-argument-list.
12771 template-argument-list:
12772 template-argument ... [opt]
12773 template-argument-list , template-argument ... [opt]
12775 Returns a TREE_VEC containing the arguments. */
12778 cp_parser_template_argument_list (cp_parser
* parser
)
12780 tree fixed_args
[10];
12781 unsigned n_args
= 0;
12782 unsigned alloced
= 10;
12783 tree
*arg_ary
= fixed_args
;
12785 bool saved_in_template_argument_list_p
;
12787 bool saved_non_ice_p
;
12789 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
12790 parser
->in_template_argument_list_p
= true;
12791 /* Even if the template-id appears in an integral
12792 constant-expression, the contents of the argument list do
12794 saved_ice_p
= parser
->integral_constant_expression_p
;
12795 parser
->integral_constant_expression_p
= false;
12796 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
12797 parser
->non_integral_constant_expression_p
= false;
12799 /* Parse the arguments. */
12805 /* Consume the comma. */
12806 cp_lexer_consume_token (parser
->lexer
);
12808 /* Parse the template-argument. */
12809 argument
= cp_parser_template_argument (parser
);
12811 /* If the next token is an ellipsis, we're expanding a template
12813 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12815 if (argument
== error_mark_node
)
12817 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12818 error_at (token
->location
,
12819 "expected parameter pack before %<...%>");
12821 /* Consume the `...' token. */
12822 cp_lexer_consume_token (parser
->lexer
);
12824 /* Make the argument into a TYPE_PACK_EXPANSION or
12825 EXPR_PACK_EXPANSION. */
12826 argument
= make_pack_expansion (argument
);
12829 if (n_args
== alloced
)
12833 if (arg_ary
== fixed_args
)
12835 arg_ary
= XNEWVEC (tree
, alloced
);
12836 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
12839 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
12841 arg_ary
[n_args
++] = argument
;
12843 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
12845 vec
= make_tree_vec (n_args
);
12848 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
12850 if (arg_ary
!= fixed_args
)
12852 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
12853 parser
->integral_constant_expression_p
= saved_ice_p
;
12854 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
12855 #ifdef ENABLE_CHECKING
12856 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
12861 /* Parse a template-argument.
12864 assignment-expression
12868 The representation is that of an assignment-expression, type-id, or
12869 id-expression -- except that the qualified id-expression is
12870 evaluated, so that the value returned is either a DECL or an
12873 Although the standard says "assignment-expression", it forbids
12874 throw-expressions or assignments in the template argument.
12875 Therefore, we use "conditional-expression" instead. */
12878 cp_parser_template_argument (cp_parser
* parser
)
12883 bool maybe_type_id
= false;
12884 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
12885 location_t loc
= 0;
12888 /* There's really no way to know what we're looking at, so we just
12889 try each alternative in order.
12893 In a template-argument, an ambiguity between a type-id and an
12894 expression is resolved to a type-id, regardless of the form of
12895 the corresponding template-parameter.
12897 Therefore, we try a type-id first. */
12898 cp_parser_parse_tentatively (parser
);
12899 argument
= cp_parser_template_type_arg (parser
);
12900 /* If there was no error parsing the type-id but the next token is a
12901 '>>', our behavior depends on which dialect of C++ we're
12902 parsing. In C++98, we probably found a typo for '> >'. But there
12903 are type-id which are also valid expressions. For instance:
12905 struct X { int operator >> (int); };
12906 template <int V> struct Foo {};
12909 Here 'X()' is a valid type-id of a function type, but the user just
12910 wanted to write the expression "X() >> 5". Thus, we remember that we
12911 found a valid type-id, but we still try to parse the argument as an
12912 expression to see what happens.
12914 In C++0x, the '>>' will be considered two separate '>'
12916 if (!cp_parser_error_occurred (parser
)
12917 && cxx_dialect
== cxx98
12918 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
12920 maybe_type_id
= true;
12921 cp_parser_abort_tentative_parse (parser
);
12925 /* If the next token isn't a `,' or a `>', then this argument wasn't
12926 really finished. This means that the argument is not a valid
12928 if (!cp_parser_next_token_ends_template_argument_p (parser
))
12929 cp_parser_error (parser
, "expected template-argument");
12930 /* If that worked, we're done. */
12931 if (cp_parser_parse_definitely (parser
))
12934 /* We're still not sure what the argument will be. */
12935 cp_parser_parse_tentatively (parser
);
12936 /* Try a template. */
12937 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
12938 argument
= cp_parser_id_expression (parser
,
12939 /*template_keyword_p=*/false,
12940 /*check_dependency_p=*/true,
12942 /*declarator_p=*/false,
12943 /*optional_p=*/false);
12944 /* If the next token isn't a `,' or a `>', then this argument wasn't
12945 really finished. */
12946 if (!cp_parser_next_token_ends_template_argument_p (parser
))
12947 cp_parser_error (parser
, "expected template-argument");
12948 if (!cp_parser_error_occurred (parser
))
12950 /* Figure out what is being referred to. If the id-expression
12951 was for a class template specialization, then we will have a
12952 TYPE_DECL at this point. There is no need to do name lookup
12953 at this point in that case. */
12954 if (TREE_CODE (argument
) != TYPE_DECL
)
12955 argument
= cp_parser_lookup_name (parser
, argument
,
12957 /*is_template=*/template_p
,
12958 /*is_namespace=*/false,
12959 /*check_dependency=*/true,
12960 /*ambiguous_decls=*/NULL
,
12961 argument_start_token
->location
);
12962 if (TREE_CODE (argument
) != TEMPLATE_DECL
12963 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
12964 cp_parser_error (parser
, "expected template-name");
12966 if (cp_parser_parse_definitely (parser
))
12968 /* It must be a non-type argument. There permitted cases are given
12969 in [temp.arg.nontype]:
12971 -- an integral constant-expression of integral or enumeration
12974 -- the name of a non-type template-parameter; or
12976 -- the name of an object or function with external linkage...
12978 -- the address of an object or function with external linkage...
12980 -- a pointer to member... */
12981 /* Look for a non-type template parameter. */
12982 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
12984 cp_parser_parse_tentatively (parser
);
12985 argument
= cp_parser_primary_expression (parser
,
12986 /*address_p=*/false,
12988 /*template_arg_p=*/true,
12990 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
12991 || !cp_parser_next_token_ends_template_argument_p (parser
))
12992 cp_parser_simulate_error (parser
);
12993 if (cp_parser_parse_definitely (parser
))
12997 /* If the next token is "&", the argument must be the address of an
12998 object or function with external linkage. */
12999 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
13002 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13003 cp_lexer_consume_token (parser
->lexer
);
13005 /* See if we might have an id-expression. */
13006 token
= cp_lexer_peek_token (parser
->lexer
);
13007 if (token
->type
== CPP_NAME
13008 || token
->keyword
== RID_OPERATOR
13009 || token
->type
== CPP_SCOPE
13010 || token
->type
== CPP_TEMPLATE_ID
13011 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
13013 cp_parser_parse_tentatively (parser
);
13014 argument
= cp_parser_primary_expression (parser
,
13017 /*template_arg_p=*/true,
13019 if (cp_parser_error_occurred (parser
)
13020 || !cp_parser_next_token_ends_template_argument_p (parser
))
13021 cp_parser_abort_tentative_parse (parser
);
13026 if (TREE_CODE (argument
) == INDIRECT_REF
)
13028 gcc_assert (REFERENCE_REF_P (argument
));
13029 argument
= TREE_OPERAND (argument
, 0);
13032 /* If we're in a template, we represent a qualified-id referring
13033 to a static data member as a SCOPE_REF even if the scope isn't
13034 dependent so that we can check access control later. */
13036 if (TREE_CODE (probe
) == SCOPE_REF
)
13037 probe
= TREE_OPERAND (probe
, 1);
13038 if (TREE_CODE (probe
) == VAR_DECL
)
13040 /* A variable without external linkage might still be a
13041 valid constant-expression, so no error is issued here
13042 if the external-linkage check fails. */
13043 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
13044 cp_parser_simulate_error (parser
);
13046 else if (is_overloaded_fn (argument
))
13047 /* All overloaded functions are allowed; if the external
13048 linkage test does not pass, an error will be issued
13052 && (TREE_CODE (argument
) == OFFSET_REF
13053 || TREE_CODE (argument
) == SCOPE_REF
))
13054 /* A pointer-to-member. */
13056 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
13059 cp_parser_simulate_error (parser
);
13061 if (cp_parser_parse_definitely (parser
))
13064 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
13065 tf_warning_or_error
);
13070 /* If the argument started with "&", there are no other valid
13071 alternatives at this point. */
13074 cp_parser_error (parser
, "invalid non-type template argument");
13075 return error_mark_node
;
13078 /* If the argument wasn't successfully parsed as a type-id followed
13079 by '>>', the argument can only be a constant expression now.
13080 Otherwise, we try parsing the constant-expression tentatively,
13081 because the argument could really be a type-id. */
13083 cp_parser_parse_tentatively (parser
);
13084 argument
= cp_parser_constant_expression (parser
,
13085 /*allow_non_constant_p=*/false,
13086 /*non_constant_p=*/NULL
);
13087 argument
= fold_non_dependent_expr (argument
);
13088 if (!maybe_type_id
)
13090 if (!cp_parser_next_token_ends_template_argument_p (parser
))
13091 cp_parser_error (parser
, "expected template-argument");
13092 if (cp_parser_parse_definitely (parser
))
13094 /* We did our best to parse the argument as a non type-id, but that
13095 was the only alternative that matched (albeit with a '>' after
13096 it). We can assume it's just a typo from the user, and a
13097 diagnostic will then be issued. */
13098 return cp_parser_template_type_arg (parser
);
13101 /* Parse an explicit-instantiation.
13103 explicit-instantiation:
13104 template declaration
13106 Although the standard says `declaration', what it really means is:
13108 explicit-instantiation:
13109 template decl-specifier-seq [opt] declarator [opt] ;
13111 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13112 supposed to be allowed. A defect report has been filed about this
13117 explicit-instantiation:
13118 storage-class-specifier template
13119 decl-specifier-seq [opt] declarator [opt] ;
13120 function-specifier template
13121 decl-specifier-seq [opt] declarator [opt] ; */
13124 cp_parser_explicit_instantiation (cp_parser
* parser
)
13126 int declares_class_or_enum
;
13127 cp_decl_specifier_seq decl_specifiers
;
13128 tree extension_specifier
= NULL_TREE
;
13130 timevar_push (TV_TEMPLATE_INST
);
13132 /* Look for an (optional) storage-class-specifier or
13133 function-specifier. */
13134 if (cp_parser_allow_gnu_extensions_p (parser
))
13136 extension_specifier
13137 = cp_parser_storage_class_specifier_opt (parser
);
13138 if (!extension_specifier
)
13139 extension_specifier
13140 = cp_parser_function_specifier_opt (parser
,
13141 /*decl_specs=*/NULL
);
13144 /* Look for the `template' keyword. */
13145 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
13146 /* Let the front end know that we are processing an explicit
13148 begin_explicit_instantiation ();
13149 /* [temp.explicit] says that we are supposed to ignore access
13150 control while processing explicit instantiation directives. */
13151 push_deferring_access_checks (dk_no_check
);
13152 /* Parse a decl-specifier-seq. */
13153 cp_parser_decl_specifier_seq (parser
,
13154 CP_PARSER_FLAGS_OPTIONAL
,
13156 &declares_class_or_enum
);
13157 /* If there was exactly one decl-specifier, and it declared a class,
13158 and there's no declarator, then we have an explicit type
13160 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
13164 type
= check_tag_decl (&decl_specifiers
);
13165 /* Turn access control back on for names used during
13166 template instantiation. */
13167 pop_deferring_access_checks ();
13169 do_type_instantiation (type
, extension_specifier
,
13170 /*complain=*/tf_error
);
13174 cp_declarator
*declarator
;
13177 /* Parse the declarator. */
13179 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
13180 /*ctor_dtor_or_conv_p=*/NULL
,
13181 /*parenthesized_p=*/NULL
,
13182 /*member_p=*/false);
13183 if (declares_class_or_enum
& 2)
13184 cp_parser_check_for_definition_in_return_type (declarator
,
13185 decl_specifiers
.type
,
13186 decl_specifiers
.locations
[ds_type_spec
]);
13187 if (declarator
!= cp_error_declarator
)
13189 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
13190 permerror (decl_specifiers
.locations
[ds_inline
],
13191 "explicit instantiation shall not use"
13192 " %<inline%> specifier");
13193 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
13194 permerror (decl_specifiers
.locations
[ds_constexpr
],
13195 "explicit instantiation shall not use"
13196 " %<constexpr%> specifier");
13198 decl
= grokdeclarator (declarator
, &decl_specifiers
,
13199 NORMAL
, 0, &decl_specifiers
.attributes
);
13200 /* Turn access control back on for names used during
13201 template instantiation. */
13202 pop_deferring_access_checks ();
13203 /* Do the explicit instantiation. */
13204 do_decl_instantiation (decl
, extension_specifier
);
13208 pop_deferring_access_checks ();
13209 /* Skip the body of the explicit instantiation. */
13210 cp_parser_skip_to_end_of_statement (parser
);
13213 /* We're done with the instantiation. */
13214 end_explicit_instantiation ();
13216 cp_parser_consume_semicolon_at_end_of_statement (parser
);
13218 timevar_pop (TV_TEMPLATE_INST
);
13221 /* Parse an explicit-specialization.
13223 explicit-specialization:
13224 template < > declaration
13226 Although the standard says `declaration', what it really means is:
13228 explicit-specialization:
13229 template <> decl-specifier [opt] init-declarator [opt] ;
13230 template <> function-definition
13231 template <> explicit-specialization
13232 template <> template-declaration */
13235 cp_parser_explicit_specialization (cp_parser
* parser
)
13237 bool need_lang_pop
;
13238 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13240 /* Look for the `template' keyword. */
13241 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
13242 /* Look for the `<'. */
13243 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13244 /* Look for the `>'. */
13245 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13246 /* We have processed another parameter list. */
13247 ++parser
->num_template_parameter_lists
;
13250 A template ... explicit specialization ... shall not have C
13252 if (current_lang_name
== lang_name_c
)
13254 error_at (token
->location
, "template specialization with C linkage");
13255 /* Give it C++ linkage to avoid confusing other parts of the
13257 push_lang_context (lang_name_cplusplus
);
13258 need_lang_pop
= true;
13261 need_lang_pop
= false;
13262 /* Let the front end know that we are beginning a specialization. */
13263 if (!begin_specialization ())
13265 end_specialization ();
13269 /* If the next keyword is `template', we need to figure out whether
13270 or not we're looking a template-declaration. */
13271 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
13273 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
13274 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
13275 cp_parser_template_declaration_after_export (parser
,
13276 /*member_p=*/false);
13278 cp_parser_explicit_specialization (parser
);
13281 /* Parse the dependent declaration. */
13282 cp_parser_single_declaration (parser
,
13284 /*member_p=*/false,
13285 /*explicit_specialization_p=*/true,
13286 /*friend_p=*/NULL
);
13287 /* We're done with the specialization. */
13288 end_specialization ();
13289 /* For the erroneous case of a template with C linkage, we pushed an
13290 implicit C++ linkage scope; exit that scope now. */
13292 pop_lang_context ();
13293 /* We're done with this parameter list. */
13294 --parser
->num_template_parameter_lists
;
13297 /* Parse a type-specifier.
13300 simple-type-specifier
13303 elaborated-type-specifier
13311 Returns a representation of the type-specifier. For a
13312 class-specifier, enum-specifier, or elaborated-type-specifier, a
13313 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13315 The parser flags FLAGS is used to control type-specifier parsing.
13317 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13318 in a decl-specifier-seq.
13320 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13321 class-specifier, enum-specifier, or elaborated-type-specifier, then
13322 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13323 if a type is declared; 2 if it is defined. Otherwise, it is set to
13326 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13327 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13328 is set to FALSE. */
13331 cp_parser_type_specifier (cp_parser
* parser
,
13332 cp_parser_flags flags
,
13333 cp_decl_specifier_seq
*decl_specs
,
13334 bool is_declaration
,
13335 int* declares_class_or_enum
,
13336 bool* is_cv_qualifier
)
13338 tree type_spec
= NULL_TREE
;
13341 cp_decl_spec ds
= ds_last
;
13343 /* Assume this type-specifier does not declare a new type. */
13344 if (declares_class_or_enum
)
13345 *declares_class_or_enum
= 0;
13346 /* And that it does not specify a cv-qualifier. */
13347 if (is_cv_qualifier
)
13348 *is_cv_qualifier
= false;
13349 /* Peek at the next token. */
13350 token
= cp_lexer_peek_token (parser
->lexer
);
13352 /* If we're looking at a keyword, we can use that to guide the
13353 production we choose. */
13354 keyword
= token
->keyword
;
13358 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
13359 goto elaborated_type_specifier
;
13361 /* Look for the enum-specifier. */
13362 type_spec
= cp_parser_enum_specifier (parser
);
13363 /* If that worked, we're done. */
13366 if (declares_class_or_enum
)
13367 *declares_class_or_enum
= 2;
13369 cp_parser_set_decl_spec_type (decl_specs
,
13372 /*type_definition_p=*/true);
13376 goto elaborated_type_specifier
;
13378 /* Any of these indicate either a class-specifier, or an
13379 elaborated-type-specifier. */
13383 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
13384 goto elaborated_type_specifier
;
13386 /* Parse tentatively so that we can back up if we don't find a
13387 class-specifier. */
13388 cp_parser_parse_tentatively (parser
);
13389 /* Look for the class-specifier. */
13390 type_spec
= cp_parser_class_specifier (parser
);
13391 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
13392 /* If that worked, we're done. */
13393 if (cp_parser_parse_definitely (parser
))
13395 if (declares_class_or_enum
)
13396 *declares_class_or_enum
= 2;
13398 cp_parser_set_decl_spec_type (decl_specs
,
13401 /*type_definition_p=*/true);
13405 /* Fall through. */
13406 elaborated_type_specifier
:
13407 /* We're declaring (not defining) a class or enum. */
13408 if (declares_class_or_enum
)
13409 *declares_class_or_enum
= 1;
13411 /* Fall through. */
13413 /* Look for an elaborated-type-specifier. */
13415 = (cp_parser_elaborated_type_specifier
13417 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
13420 cp_parser_set_decl_spec_type (decl_specs
,
13423 /*type_definition_p=*/false);
13428 if (is_cv_qualifier
)
13429 *is_cv_qualifier
= true;
13434 if (is_cv_qualifier
)
13435 *is_cv_qualifier
= true;
13440 if (is_cv_qualifier
)
13441 *is_cv_qualifier
= true;
13445 /* The `__complex__' keyword is a GNU extension. */
13453 /* Handle simple keywords. */
13458 set_and_check_decl_spec_loc (decl_specs
, ds
, token
->location
);
13459 decl_specs
->any_specifiers_p
= true;
13461 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13464 /* If we do not already have a type-specifier, assume we are looking
13465 at a simple-type-specifier. */
13466 type_spec
= cp_parser_simple_type_specifier (parser
,
13470 /* If we didn't find a type-specifier, and a type-specifier was not
13471 optional in this context, issue an error message. */
13472 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
13474 cp_parser_error (parser
, "expected type specifier");
13475 return error_mark_node
;
13481 /* Parse a simple-type-specifier.
13483 simple-type-specifier:
13484 :: [opt] nested-name-specifier [opt] type-name
13485 :: [opt] nested-name-specifier template template-id
13500 simple-type-specifier:
13502 decltype ( expression )
13505 __underlying_type ( type-id )
13509 simple-type-specifier:
13511 __typeof__ unary-expression
13512 __typeof__ ( type-id )
13514 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13515 appropriately updated. */
13518 cp_parser_simple_type_specifier (cp_parser
* parser
,
13519 cp_decl_specifier_seq
*decl_specs
,
13520 cp_parser_flags flags
)
13522 tree type
= NULL_TREE
;
13525 /* Peek at the next token. */
13526 token
= cp_lexer_peek_token (parser
->lexer
);
13528 /* If we're looking at a keyword, things are easy. */
13529 switch (token
->keyword
)
13533 decl_specs
->explicit_char_p
= true;
13534 type
= char_type_node
;
13537 type
= char16_type_node
;
13540 type
= char32_type_node
;
13543 type
= wchar_type_node
;
13546 type
= boolean_type_node
;
13549 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
->location
);
13550 type
= short_integer_type_node
;
13554 decl_specs
->explicit_int_p
= true;
13555 type
= integer_type_node
;
13558 if (!int128_integer_type_node
)
13561 decl_specs
->explicit_int128_p
= true;
13562 type
= int128_integer_type_node
;
13566 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
->location
);
13567 type
= long_integer_type_node
;
13570 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
->location
);
13571 type
= integer_type_node
;
13574 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
->location
);
13575 type
= unsigned_type_node
;
13578 type
= float_type_node
;
13581 type
= double_type_node
;
13584 type
= void_type_node
;
13588 maybe_warn_cpp0x (CPP0X_AUTO
);
13589 type
= make_auto ();
13593 /* Since DR 743, decltype can either be a simple-type-specifier by
13594 itself or begin a nested-name-specifier. Parsing it will replace
13595 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13596 handling below decide what to do. */
13597 cp_parser_decltype (parser
);
13598 cp_lexer_set_token_position (parser
->lexer
, token
);
13602 /* Consume the `typeof' token. */
13603 cp_lexer_consume_token (parser
->lexer
);
13604 /* Parse the operand to `typeof'. */
13605 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
13606 /* If it is not already a TYPE, take its type. */
13607 if (!TYPE_P (type
))
13608 type
= finish_typeof (type
);
13611 cp_parser_set_decl_spec_type (decl_specs
, type
,
13613 /*type_definition_p=*/false);
13617 case RID_UNDERLYING_TYPE
:
13618 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
13620 cp_parser_set_decl_spec_type (decl_specs
, type
,
13622 /*type_definition_p=*/false);
13627 case RID_DIRECT_BASES
:
13628 type
= cp_parser_trait_expr (parser
, token
->keyword
);
13630 cp_parser_set_decl_spec_type (decl_specs
, type
,
13632 /*type_definition_p=*/false);
13638 /* If token is an already-parsed decltype not followed by ::,
13639 it's a simple-type-specifier. */
13640 if (token
->type
== CPP_DECLTYPE
13641 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
13643 type
= token
->u
.value
;
13645 cp_parser_set_decl_spec_type (decl_specs
, type
,
13647 /*type_definition_p=*/false);
13648 cp_lexer_consume_token (parser
->lexer
);
13652 /* If the type-specifier was for a built-in type, we're done. */
13655 /* Record the type. */
13657 && (token
->keyword
!= RID_SIGNED
13658 && token
->keyword
!= RID_UNSIGNED
13659 && token
->keyword
!= RID_SHORT
13660 && token
->keyword
!= RID_LONG
))
13661 cp_parser_set_decl_spec_type (decl_specs
,
13664 /*type_definition_p=*/false);
13666 decl_specs
->any_specifiers_p
= true;
13668 /* Consume the token. */
13669 cp_lexer_consume_token (parser
->lexer
);
13671 /* There is no valid C++ program where a non-template type is
13672 followed by a "<". That usually indicates that the user thought
13673 that the type was a template. */
13674 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
13677 return TYPE_NAME (type
);
13680 /* The type-specifier must be a user-defined type. */
13681 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
13686 /* Don't gobble tokens or issue error messages if this is an
13687 optional type-specifier. */
13688 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
13689 cp_parser_parse_tentatively (parser
);
13691 /* Look for the optional `::' operator. */
13693 = (cp_parser_global_scope_opt (parser
,
13694 /*current_scope_valid_p=*/false)
13696 /* Look for the nested-name specifier. */
13698 = (cp_parser_nested_name_specifier_opt (parser
,
13699 /*typename_keyword_p=*/false,
13700 /*check_dependency_p=*/true,
13702 /*is_declaration=*/false)
13704 token
= cp_lexer_peek_token (parser
->lexer
);
13705 /* If we have seen a nested-name-specifier, and the next token
13706 is `template', then we are using the template-id production. */
13708 && cp_parser_optional_template_keyword (parser
))
13710 /* Look for the template-id. */
13711 type
= cp_parser_template_id (parser
,
13712 /*template_keyword_p=*/true,
13713 /*check_dependency_p=*/true,
13715 /*is_declaration=*/false);
13716 /* If the template-id did not name a type, we are out of
13718 if (TREE_CODE (type
) != TYPE_DECL
)
13720 cp_parser_error (parser
, "expected template-id for type");
13724 /* Otherwise, look for a type-name. */
13726 type
= cp_parser_type_name (parser
);
13727 /* Keep track of all name-lookups performed in class scopes. */
13731 && TREE_CODE (type
) == TYPE_DECL
13732 && TREE_CODE (DECL_NAME (type
)) == IDENTIFIER_NODE
)
13733 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
13734 /* If it didn't work out, we don't have a TYPE. */
13735 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
13736 && !cp_parser_parse_definitely (parser
))
13738 if (type
&& decl_specs
)
13739 cp_parser_set_decl_spec_type (decl_specs
, type
,
13741 /*type_definition_p=*/false);
13744 /* If we didn't get a type-name, issue an error message. */
13745 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
13747 cp_parser_error (parser
, "expected type-name");
13748 return error_mark_node
;
13751 if (type
&& type
!= error_mark_node
)
13753 /* See if TYPE is an Objective-C type, and if so, parse and
13754 accept any protocol references following it. Do this before
13755 the cp_parser_check_for_invalid_template_id() call, because
13756 Objective-C types can be followed by '<...>' which would
13757 enclose protocol names rather than template arguments, and so
13758 everything is fine. */
13759 if (c_dialect_objc () && !parser
->scope
13760 && (objc_is_id (type
) || objc_is_class_name (type
)))
13762 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
13763 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
13765 /* Clobber the "unqualified" type previously entered into
13766 DECL_SPECS with the new, improved protocol-qualified version. */
13768 decl_specs
->type
= qual_type
;
13773 /* There is no valid C++ program where a non-template type is
13774 followed by a "<". That usually indicates that the user
13775 thought that the type was a template. */
13776 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
13784 /* Parse a type-name.
13790 simple-template-id [in c++0x]
13798 Returns a TYPE_DECL for the type. */
13801 cp_parser_type_name (cp_parser
* parser
)
13805 /* We can't know yet whether it is a class-name or not. */
13806 cp_parser_parse_tentatively (parser
);
13807 /* Try a class-name. */
13808 type_decl
= cp_parser_class_name (parser
,
13809 /*typename_keyword_p=*/false,
13810 /*template_keyword_p=*/false,
13812 /*check_dependency_p=*/true,
13813 /*class_head_p=*/false,
13814 /*is_declaration=*/false);
13815 /* If it's not a class-name, keep looking. */
13816 if (!cp_parser_parse_definitely (parser
))
13818 if (cxx_dialect
< cxx0x
)
13819 /* It must be a typedef-name or an enum-name. */
13820 return cp_parser_nonclass_name (parser
);
13822 cp_parser_parse_tentatively (parser
);
13823 /* It is either a simple-template-id representing an
13824 instantiation of an alias template... */
13825 type_decl
= cp_parser_template_id (parser
,
13826 /*template_keyword_p=*/false,
13827 /*check_dependency_p=*/false,
13829 /*is_declaration=*/false);
13830 /* Note that this must be an instantiation of an alias template
13831 because [temp.names]/6 says:
13833 A template-id that names an alias template specialization
13836 Whereas [temp.names]/7 says:
13838 A simple-template-id that names a class template
13839 specialization is a class-name. */
13840 if (type_decl
!= NULL_TREE
13841 && TREE_CODE (type_decl
) == TYPE_DECL
13842 && TYPE_DECL_ALIAS_P (type_decl
))
13843 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
13845 cp_parser_simulate_error (parser
);
13847 if (!cp_parser_parse_definitely (parser
))
13848 /* ... Or a typedef-name or an enum-name. */
13849 return cp_parser_nonclass_name (parser
);
13855 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13863 Returns a TYPE_DECL for the type. */
13866 cp_parser_nonclass_name (cp_parser
* parser
)
13871 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13872 identifier
= cp_parser_identifier (parser
);
13873 if (identifier
== error_mark_node
)
13874 return error_mark_node
;
13876 /* Look up the type-name. */
13877 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
13879 if (TREE_CODE (type_decl
) == USING_DECL
)
13881 if (!DECL_DEPENDENT_P (type_decl
))
13882 type_decl
= strip_using_decl (type_decl
);
13883 else if (USING_DECL_TYPENAME_P (type_decl
))
13885 /* We have found a type introduced by a using
13886 declaration at class scope that refers to a dependent
13889 using typename :: [opt] nested-name-specifier unqualified-id ;
13891 type_decl
= make_typename_type (TREE_TYPE (type_decl
),
13892 DECL_NAME (type_decl
),
13893 typename_type
, tf_error
);
13894 if (type_decl
!= error_mark_node
)
13895 type_decl
= TYPE_NAME (type_decl
);
13899 if (TREE_CODE (type_decl
) != TYPE_DECL
13900 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
13902 /* See if this is an Objective-C type. */
13903 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
13904 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
13906 type_decl
= TYPE_NAME (type
);
13909 /* Issue an error if we did not find a type-name. */
13910 if (TREE_CODE (type_decl
) != TYPE_DECL
13911 /* In Objective-C, we have the complication that class names are
13912 normally type names and start declarations (eg, the
13913 "NSObject" in "NSObject *object;"), but can be used in an
13914 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13915 is an expression. So, a classname followed by a dot is not a
13916 valid type-name. */
13917 || (objc_is_class_name (TREE_TYPE (type_decl
))
13918 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
13920 if (!cp_parser_simulate_error (parser
))
13921 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
13922 NLE_TYPE
, token
->location
);
13923 return error_mark_node
;
13925 /* Remember that the name was used in the definition of the
13926 current class so that we can check later to see if the
13927 meaning would have been different after the class was
13928 entirely defined. */
13929 else if (type_decl
!= error_mark_node
13931 maybe_note_name_used_in_class (identifier
, type_decl
);
13936 /* Parse an elaborated-type-specifier. Note that the grammar given
13937 here incorporates the resolution to DR68.
13939 elaborated-type-specifier:
13940 class-key :: [opt] nested-name-specifier [opt] identifier
13941 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13942 enum-key :: [opt] nested-name-specifier [opt] identifier
13943 typename :: [opt] nested-name-specifier identifier
13944 typename :: [opt] nested-name-specifier template [opt]
13949 elaborated-type-specifier:
13950 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13951 class-key attributes :: [opt] nested-name-specifier [opt]
13952 template [opt] template-id
13953 enum attributes :: [opt] nested-name-specifier [opt] identifier
13955 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13956 declared `friend'. If IS_DECLARATION is TRUE, then this
13957 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13958 something is being declared.
13960 Returns the TYPE specified. */
13963 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
13965 bool is_declaration
)
13967 enum tag_types tag_type
;
13969 tree type
= NULL_TREE
;
13970 tree attributes
= NULL_TREE
;
13972 cp_token
*token
= NULL
;
13974 /* See if we're looking at the `enum' keyword. */
13975 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
13977 /* Consume the `enum' token. */
13978 cp_lexer_consume_token (parser
->lexer
);
13979 /* Remember that it's an enumeration type. */
13980 tag_type
= enum_type
;
13981 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13982 enums) is used here. */
13983 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
13984 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
13986 pedwarn (input_location
, 0, "elaborated-type-specifier "
13987 "for a scoped enum must not use the %<%D%> keyword",
13988 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
13989 /* Consume the `struct' or `class' and parse it anyway. */
13990 cp_lexer_consume_token (parser
->lexer
);
13992 /* Parse the attributes. */
13993 attributes
= cp_parser_attributes_opt (parser
);
13995 /* Or, it might be `typename'. */
13996 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
13999 /* Consume the `typename' token. */
14000 cp_lexer_consume_token (parser
->lexer
);
14001 /* Remember that it's a `typename' type. */
14002 tag_type
= typename_type
;
14004 /* Otherwise it must be a class-key. */
14007 tag_type
= cp_parser_class_key (parser
);
14008 if (tag_type
== none_type
)
14009 return error_mark_node
;
14010 /* Parse the attributes. */
14011 attributes
= cp_parser_attributes_opt (parser
);
14014 /* Look for the `::' operator. */
14015 globalscope
= cp_parser_global_scope_opt (parser
,
14016 /*current_scope_valid_p=*/false);
14017 /* Look for the nested-name-specifier. */
14018 if (tag_type
== typename_type
&& !globalscope
)
14020 if (!cp_parser_nested_name_specifier (parser
,
14021 /*typename_keyword_p=*/true,
14022 /*check_dependency_p=*/true,
14025 return error_mark_node
;
14028 /* Even though `typename' is not present, the proposed resolution
14029 to Core Issue 180 says that in `class A<T>::B', `B' should be
14030 considered a type-name, even if `A<T>' is dependent. */
14031 cp_parser_nested_name_specifier_opt (parser
,
14032 /*typename_keyword_p=*/true,
14033 /*check_dependency_p=*/true,
14036 /* For everything but enumeration types, consider a template-id.
14037 For an enumeration type, consider only a plain identifier. */
14038 if (tag_type
!= enum_type
)
14040 bool template_p
= false;
14043 /* Allow the `template' keyword. */
14044 template_p
= cp_parser_optional_template_keyword (parser
);
14045 /* If we didn't see `template', we don't know if there's a
14046 template-id or not. */
14048 cp_parser_parse_tentatively (parser
);
14049 /* Parse the template-id. */
14050 token
= cp_lexer_peek_token (parser
->lexer
);
14051 decl
= cp_parser_template_id (parser
, template_p
,
14052 /*check_dependency_p=*/true,
14055 /* If we didn't find a template-id, look for an ordinary
14057 if (!template_p
&& !cp_parser_parse_definitely (parser
))
14059 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14060 in effect, then we must assume that, upon instantiation, the
14061 template will correspond to a class. */
14062 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
14063 && tag_type
== typename_type
)
14064 type
= make_typename_type (parser
->scope
, decl
,
14066 /*complain=*/tf_error
);
14067 /* If the `typename' keyword is in effect and DECL is not a type
14068 decl. Then type is non existant. */
14069 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
14072 type
= check_elaborated_type_specifier (tag_type
, decl
,
14073 /*allow_template_p=*/true);
14078 token
= cp_lexer_peek_token (parser
->lexer
);
14079 identifier
= cp_parser_identifier (parser
);
14081 if (identifier
== error_mark_node
)
14083 parser
->scope
= NULL_TREE
;
14084 return error_mark_node
;
14087 /* For a `typename', we needn't call xref_tag. */
14088 if (tag_type
== typename_type
14089 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
14090 return cp_parser_make_typename_type (parser
, parser
->scope
,
14093 /* Look up a qualified name in the usual way. */
14097 tree ambiguous_decls
;
14099 decl
= cp_parser_lookup_name (parser
, identifier
,
14101 /*is_template=*/false,
14102 /*is_namespace=*/false,
14103 /*check_dependency=*/true,
14107 /* If the lookup was ambiguous, an error will already have been
14109 if (ambiguous_decls
)
14110 return error_mark_node
;
14112 /* If we are parsing friend declaration, DECL may be a
14113 TEMPLATE_DECL tree node here. However, we need to check
14114 whether this TEMPLATE_DECL results in valid code. Consider
14115 the following example:
14118 template <class T> class C {};
14121 template <class T> friend class N::C; // #1, valid code
14123 template <class T> class Y {
14124 friend class N::C; // #2, invalid code
14127 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14128 name lookup of `N::C'. We see that friend declaration must
14129 be template for the code to be valid. Note that
14130 processing_template_decl does not work here since it is
14131 always 1 for the above two cases. */
14133 decl
= (cp_parser_maybe_treat_template_as_class
14134 (decl
, /*tag_name_p=*/is_friend
14135 && parser
->num_template_parameter_lists
));
14137 if (TREE_CODE (decl
) != TYPE_DECL
)
14139 cp_parser_diagnose_invalid_type_name (parser
,
14143 return error_mark_node
;
14146 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
14148 bool allow_template
= (parser
->num_template_parameter_lists
14149 || DECL_SELF_REFERENCE_P (decl
));
14150 type
= check_elaborated_type_specifier (tag_type
, decl
,
14153 if (type
== error_mark_node
)
14154 return error_mark_node
;
14157 /* Forward declarations of nested types, such as
14162 are invalid unless all components preceding the final '::'
14163 are complete. If all enclosing types are complete, these
14164 declarations become merely pointless.
14166 Invalid forward declarations of nested types are errors
14167 caught elsewhere in parsing. Those that are pointless arrive
14170 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
14171 && !is_friend
&& !processing_explicit_instantiation
)
14172 warning (0, "declaration %qD does not declare anything", decl
);
14174 type
= TREE_TYPE (decl
);
14178 /* An elaborated-type-specifier sometimes introduces a new type and
14179 sometimes names an existing type. Normally, the rule is that it
14180 introduces a new type only if there is not an existing type of
14181 the same name already in scope. For example, given:
14184 void f() { struct S s; }
14186 the `struct S' in the body of `f' is the same `struct S' as in
14187 the global scope; the existing definition is used. However, if
14188 there were no global declaration, this would introduce a new
14189 local class named `S'.
14191 An exception to this rule applies to the following code:
14193 namespace N { struct S; }
14195 Here, the elaborated-type-specifier names a new type
14196 unconditionally; even if there is already an `S' in the
14197 containing scope this declaration names a new type.
14198 This exception only applies if the elaborated-type-specifier
14199 forms the complete declaration:
14203 A declaration consisting solely of `class-key identifier ;' is
14204 either a redeclaration of the name in the current scope or a
14205 forward declaration of the identifier as a class name. It
14206 introduces the name into the current scope.
14208 We are in this situation precisely when the next token is a `;'.
14210 An exception to the exception is that a `friend' declaration does
14211 *not* name a new type; i.e., given:
14213 struct S { friend struct T; };
14215 `T' is not a new type in the scope of `S'.
14217 Also, `new struct S' or `sizeof (struct S)' never results in the
14218 definition of a new type; a new type can only be declared in a
14219 declaration context. */
14225 /* Friends have special name lookup rules. */
14226 ts
= ts_within_enclosing_non_class
;
14227 else if (is_declaration
14228 && cp_lexer_next_token_is (parser
->lexer
,
14230 /* This is a `class-key identifier ;' */
14236 (parser
->num_template_parameter_lists
14237 && (cp_parser_next_token_starts_class_definition_p (parser
)
14238 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
14239 /* An unqualified name was used to reference this type, so
14240 there were no qualifying templates. */
14241 if (!cp_parser_check_template_parameters (parser
,
14242 /*num_templates=*/0,
14244 /*declarator=*/NULL
))
14245 return error_mark_node
;
14246 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
14250 if (type
== error_mark_node
)
14251 return error_mark_node
;
14253 /* Allow attributes on forward declarations of classes. */
14256 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14257 warning (OPT_Wattributes
,
14258 "attributes ignored on uninstantiated type");
14259 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
14260 && ! processing_explicit_instantiation
)
14261 warning (OPT_Wattributes
,
14262 "attributes ignored on template instantiation");
14263 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
14264 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
14266 warning (OPT_Wattributes
,
14267 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14270 if (tag_type
!= enum_type
)
14272 /* Indicate whether this class was declared as a `class' or as a
14274 if (TREE_CODE (type
) == RECORD_TYPE
)
14275 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
14276 cp_parser_check_class_key (tag_type
, type
);
14279 /* A "<" cannot follow an elaborated type specifier. If that
14280 happens, the user was probably trying to form a template-id. */
14281 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
14287 /* Parse an enum-specifier.
14290 enum-head { enumerator-list [opt] }
14291 enum-head { enumerator-list , } [C++0x]
14294 enum-key identifier [opt] enum-base [opt]
14295 enum-key nested-name-specifier identifier enum-base [opt]
14300 enum struct [C++0x]
14303 : type-specifier-seq
14305 opaque-enum-specifier:
14306 enum-key identifier enum-base [opt] ;
14309 enum-key attributes[opt] identifier [opt] enum-base [opt]
14310 { enumerator-list [opt] }attributes[opt]
14311 enum-key attributes[opt] identifier [opt] enum-base [opt]
14312 { enumerator-list, }attributes[opt] [C++0x]
14314 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14315 if the token stream isn't an enum-specifier after all. */
14318 cp_parser_enum_specifier (cp_parser
* parser
)
14321 tree type
= NULL_TREE
;
14323 tree nested_name_specifier
= NULL_TREE
;
14325 bool scoped_enum_p
= false;
14326 bool has_underlying_type
= false;
14327 bool nested_being_defined
= false;
14328 bool new_value_list
= false;
14329 bool is_new_type
= false;
14330 bool is_anonymous
= false;
14331 tree underlying_type
= NULL_TREE
;
14332 cp_token
*type_start_token
= NULL
;
14333 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
14335 parser
->colon_corrects_to_scope_p
= false;
14337 /* Parse tentatively so that we can back up if we don't find a
14339 cp_parser_parse_tentatively (parser
);
14341 /* Caller guarantees that the current token is 'enum', an identifier
14342 possibly follows, and the token after that is an opening brace.
14343 If we don't have an identifier, fabricate an anonymous name for
14344 the enumeration being defined. */
14345 cp_lexer_consume_token (parser
->lexer
);
14347 /* Parse the "class" or "struct", which indicates a scoped
14348 enumeration type in C++0x. */
14349 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
14350 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
14352 if (cxx_dialect
< cxx0x
)
14353 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
14355 /* Consume the `struct' or `class' token. */
14356 cp_lexer_consume_token (parser
->lexer
);
14358 scoped_enum_p
= true;
14361 attributes
= cp_parser_attributes_opt (parser
);
14363 /* Clear the qualification. */
14364 parser
->scope
= NULL_TREE
;
14365 parser
->qualifying_scope
= NULL_TREE
;
14366 parser
->object_scope
= NULL_TREE
;
14368 /* Figure out in what scope the declaration is being placed. */
14369 prev_scope
= current_scope ();
14371 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
14373 push_deferring_access_checks (dk_no_check
);
14374 nested_name_specifier
14375 = cp_parser_nested_name_specifier_opt (parser
,
14376 /*typename_keyword_p=*/true,
14377 /*check_dependency_p=*/false,
14379 /*is_declaration=*/false);
14381 if (nested_name_specifier
)
14385 identifier
= cp_parser_identifier (parser
);
14386 name
= cp_parser_lookup_name (parser
, identifier
,
14388 /*is_template=*/false,
14389 /*is_namespace=*/false,
14390 /*check_dependency=*/true,
14391 /*ambiguous_decls=*/NULL
,
14395 type
= TREE_TYPE (name
);
14396 if (TREE_CODE (type
) == TYPENAME_TYPE
)
14398 /* Are template enums allowed in ISO? */
14399 if (template_parm_scope_p ())
14400 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
14401 "%qD is an enumeration template", name
);
14402 /* ignore a typename reference, for it will be solved by name
14408 error_at (type_start_token
->location
,
14409 "%qD is not an enumerator-name", identifier
);
14413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14414 identifier
= cp_parser_identifier (parser
);
14417 identifier
= make_anon_name ();
14418 is_anonymous
= true;
14421 pop_deferring_access_checks ();
14423 /* Check for the `:' that denotes a specified underlying type in C++0x.
14424 Note that a ':' could also indicate a bitfield width, however. */
14425 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14427 cp_decl_specifier_seq type_specifiers
;
14429 /* Consume the `:'. */
14430 cp_lexer_consume_token (parser
->lexer
);
14432 /* Parse the type-specifier-seq. */
14433 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14434 /*is_trailing_return=*/false,
14437 /* At this point this is surely not elaborated type specifier. */
14438 if (!cp_parser_parse_definitely (parser
))
14441 if (cxx_dialect
< cxx0x
)
14442 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
14444 has_underlying_type
= true;
14446 /* If that didn't work, stop. */
14447 if (type_specifiers
.type
!= error_mark_node
)
14449 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
14450 /*initialized=*/0, NULL
);
14451 if (underlying_type
== error_mark_node
)
14452 underlying_type
= NULL_TREE
;
14456 /* Look for the `{' but don't consume it yet. */
14457 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14459 if (cxx_dialect
< cxx0x
|| (!scoped_enum_p
&& !underlying_type
))
14461 cp_parser_error (parser
, "expected %<{%>");
14462 if (has_underlying_type
)
14468 /* An opaque-enum-specifier must have a ';' here. */
14469 if ((scoped_enum_p
|| underlying_type
)
14470 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
14472 cp_parser_error (parser
, "expected %<;%> or %<{%>");
14473 if (has_underlying_type
)
14481 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
14484 if (nested_name_specifier
)
14486 if (CLASS_TYPE_P (nested_name_specifier
))
14488 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
14489 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
14490 push_scope (nested_name_specifier
);
14492 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
14494 push_nested_namespace (nested_name_specifier
);
14498 /* Issue an error message if type-definitions are forbidden here. */
14499 if (!cp_parser_check_type_definition (parser
))
14500 type
= error_mark_node
;
14502 /* Create the new type. We do this before consuming the opening
14503 brace so the enum will be recorded as being on the line of its
14504 tag (or the 'enum' keyword, if there is no tag). */
14505 type
= start_enum (identifier
, type
, underlying_type
,
14506 scoped_enum_p
, &is_new_type
);
14508 /* If the next token is not '{' it is an opaque-enum-specifier or an
14509 elaborated-type-specifier. */
14510 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14512 timevar_push (TV_PARSE_ENUM
);
14513 if (nested_name_specifier
)
14515 /* The following catches invalid code such as:
14516 enum class S<int>::E { A, B, C }; */
14517 if (!processing_specialization
14518 && CLASS_TYPE_P (nested_name_specifier
)
14519 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
14520 error_at (type_start_token
->location
, "cannot add an enumerator "
14521 "list to a template instantiation");
14523 /* If that scope does not contain the scope in which the
14524 class was originally declared, the program is invalid. */
14525 if (prev_scope
&& !is_ancestor (prev_scope
, nested_name_specifier
))
14527 if (at_namespace_scope_p ())
14528 error_at (type_start_token
->location
,
14529 "declaration of %qD in namespace %qD which does not "
14531 type
, prev_scope
, nested_name_specifier
);
14533 error_at (type_start_token
->location
,
14534 "declaration of %qD in %qD which does not enclose %qD",
14535 type
, prev_scope
, nested_name_specifier
);
14536 type
= error_mark_node
;
14541 begin_scope (sk_scoped_enum
, type
);
14543 /* Consume the opening brace. */
14544 cp_lexer_consume_token (parser
->lexer
);
14546 if (type
== error_mark_node
)
14547 ; /* Nothing to add */
14548 else if (OPAQUE_ENUM_P (type
)
14549 || (cxx_dialect
> cxx98
&& processing_specialization
))
14551 new_value_list
= true;
14552 SET_OPAQUE_ENUM_P (type
, false);
14553 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
14557 error_at (type_start_token
->location
, "multiple definition of %q#T", type
);
14558 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
14559 "previous definition here");
14560 type
= error_mark_node
;
14563 if (type
== error_mark_node
)
14564 cp_parser_skip_to_end_of_block_or_statement (parser
);
14565 /* If the next token is not '}', then there are some enumerators. */
14566 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
14567 cp_parser_enumerator_list (parser
, type
);
14569 /* Consume the final '}'. */
14570 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
14574 timevar_pop (TV_PARSE_ENUM
);
14578 /* If a ';' follows, then it is an opaque-enum-specifier
14579 and additional restrictions apply. */
14580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14583 error_at (type_start_token
->location
,
14584 "opaque-enum-specifier without name");
14585 else if (nested_name_specifier
)
14586 error_at (type_start_token
->location
,
14587 "opaque-enum-specifier must use a simple identifier");
14591 /* Look for trailing attributes to apply to this enumeration, and
14592 apply them if appropriate. */
14593 if (cp_parser_allow_gnu_extensions_p (parser
))
14595 tree trailing_attr
= cp_parser_attributes_opt (parser
);
14596 trailing_attr
= chainon (trailing_attr
, attributes
);
14597 cplus_decl_attributes (&type
,
14599 (int) ATTR_FLAG_TYPE_IN_PLACE
);
14602 /* Finish up the enumeration. */
14603 if (type
!= error_mark_node
)
14605 if (new_value_list
)
14606 finish_enum_value_list (type
);
14608 finish_enum (type
);
14611 if (nested_name_specifier
)
14613 if (CLASS_TYPE_P (nested_name_specifier
))
14615 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
14616 pop_scope (nested_name_specifier
);
14618 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
14620 pop_nested_namespace (nested_name_specifier
);
14624 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
14628 /* Parse an enumerator-list. The enumerators all have the indicated
14632 enumerator-definition
14633 enumerator-list , enumerator-definition */
14636 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
14640 /* Parse an enumerator-definition. */
14641 cp_parser_enumerator_definition (parser
, type
);
14643 /* If the next token is not a ',', we've reached the end of
14645 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14647 /* Otherwise, consume the `,' and keep going. */
14648 cp_lexer_consume_token (parser
->lexer
);
14649 /* If the next token is a `}', there is a trailing comma. */
14650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
14652 if (cxx_dialect
< cxx0x
&& !in_system_header
)
14653 pedwarn (input_location
, OPT_Wpedantic
,
14654 "comma at end of enumerator list");
14660 /* Parse an enumerator-definition. The enumerator has the indicated
14663 enumerator-definition:
14665 enumerator = constant-expression
14671 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
14677 /* Save the input location because we are interested in the location
14678 of the identifier and not the location of the explicit value. */
14679 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14681 /* Look for the identifier. */
14682 identifier
= cp_parser_identifier (parser
);
14683 if (identifier
== error_mark_node
)
14686 /* If the next token is an '=', then there is an explicit value. */
14687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14689 /* Consume the `=' token. */
14690 cp_lexer_consume_token (parser
->lexer
);
14691 /* Parse the value. */
14692 value
= cp_parser_constant_expression (parser
,
14693 /*allow_non_constant_p=*/false,
14699 /* If we are processing a template, make sure the initializer of the
14700 enumerator doesn't contain any bare template parameter pack. */
14701 if (check_for_bare_parameter_packs (value
))
14702 value
= error_mark_node
;
14704 /* integral_constant_value will pull out this expression, so make sure
14705 it's folded as appropriate. */
14706 value
= fold_non_dependent_expr (value
);
14708 /* Create the enumerator. */
14709 build_enumerator (identifier
, value
, type
, loc
);
14712 /* Parse a namespace-name.
14715 original-namespace-name
14718 Returns the NAMESPACE_DECL for the namespace. */
14721 cp_parser_namespace_name (cp_parser
* parser
)
14724 tree namespace_decl
;
14726 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14728 /* Get the name of the namespace. */
14729 identifier
= cp_parser_identifier (parser
);
14730 if (identifier
== error_mark_node
)
14731 return error_mark_node
;
14733 /* Look up the identifier in the currently active scope. Look only
14734 for namespaces, due to:
14736 [basic.lookup.udir]
14738 When looking up a namespace-name in a using-directive or alias
14739 definition, only namespace names are considered.
14743 [basic.lookup.qual]
14745 During the lookup of a name preceding the :: scope resolution
14746 operator, object, function, and enumerator names are ignored.
14748 (Note that cp_parser_qualifying_entity only calls this
14749 function if the token after the name is the scope resolution
14751 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
14753 /*is_template=*/false,
14754 /*is_namespace=*/true,
14755 /*check_dependency=*/true,
14756 /*ambiguous_decls=*/NULL
,
14758 /* If it's not a namespace, issue an error. */
14759 if (namespace_decl
== error_mark_node
14760 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
14762 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
14763 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
14764 cp_parser_error (parser
, "expected namespace-name");
14765 namespace_decl
= error_mark_node
;
14768 return namespace_decl
;
14771 /* Parse a namespace-definition.
14773 namespace-definition:
14774 named-namespace-definition
14775 unnamed-namespace-definition
14777 named-namespace-definition:
14778 original-namespace-definition
14779 extension-namespace-definition
14781 original-namespace-definition:
14782 namespace identifier { namespace-body }
14784 extension-namespace-definition:
14785 namespace original-namespace-name { namespace-body }
14787 unnamed-namespace-definition:
14788 namespace { namespace-body } */
14791 cp_parser_namespace_definition (cp_parser
* parser
)
14793 tree identifier
, attribs
;
14794 bool has_visibility
;
14797 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
14799 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
14801 cp_lexer_consume_token (parser
->lexer
);
14806 /* Look for the `namespace' keyword. */
14807 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
14809 /* Get the name of the namespace. We do not attempt to distinguish
14810 between an original-namespace-definition and an
14811 extension-namespace-definition at this point. The semantic
14812 analysis routines are responsible for that. */
14813 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14814 identifier
= cp_parser_identifier (parser
);
14816 identifier
= NULL_TREE
;
14818 /* Parse any specified attributes. */
14819 attribs
= cp_parser_attributes_opt (parser
);
14821 /* Look for the `{' to start the namespace. */
14822 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
14823 /* Start the namespace. */
14824 push_namespace (identifier
);
14826 /* "inline namespace" is equivalent to a stub namespace definition
14827 followed by a strong using directive. */
14830 tree name_space
= current_namespace
;
14831 /* Set up namespace association. */
14832 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
14833 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
14834 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
14835 /* Import the contents of the inline namespace. */
14837 do_using_directive (name_space
);
14838 push_namespace (identifier
);
14841 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
14843 /* Parse the body of the namespace. */
14844 cp_parser_namespace_body (parser
);
14846 if (has_visibility
)
14847 pop_visibility (1);
14849 /* Finish the namespace. */
14851 /* Look for the final `}'. */
14852 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
14855 /* Parse a namespace-body.
14858 declaration-seq [opt] */
14861 cp_parser_namespace_body (cp_parser
* parser
)
14863 cp_parser_declaration_seq_opt (parser
);
14866 /* Parse a namespace-alias-definition.
14868 namespace-alias-definition:
14869 namespace identifier = qualified-namespace-specifier ; */
14872 cp_parser_namespace_alias_definition (cp_parser
* parser
)
14875 tree namespace_specifier
;
14877 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14879 /* Look for the `namespace' keyword. */
14880 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
14881 /* Look for the identifier. */
14882 identifier
= cp_parser_identifier (parser
);
14883 if (identifier
== error_mark_node
)
14885 /* Look for the `=' token. */
14886 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
14887 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14889 error_at (token
->location
, "%<namespace%> definition is not allowed here");
14890 /* Skip the definition. */
14891 cp_lexer_consume_token (parser
->lexer
);
14892 if (cp_parser_skip_to_closing_brace (parser
))
14893 cp_lexer_consume_token (parser
->lexer
);
14896 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
14897 /* Look for the qualified-namespace-specifier. */
14898 namespace_specifier
14899 = cp_parser_qualified_namespace_specifier (parser
);
14900 /* Look for the `;' token. */
14901 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14903 /* Register the alias in the symbol table. */
14904 do_namespace_alias (identifier
, namespace_specifier
);
14907 /* Parse a qualified-namespace-specifier.
14909 qualified-namespace-specifier:
14910 :: [opt] nested-name-specifier [opt] namespace-name
14912 Returns a NAMESPACE_DECL corresponding to the specified
14916 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
14918 /* Look for the optional `::'. */
14919 cp_parser_global_scope_opt (parser
,
14920 /*current_scope_valid_p=*/false);
14922 /* Look for the optional nested-name-specifier. */
14923 cp_parser_nested_name_specifier_opt (parser
,
14924 /*typename_keyword_p=*/false,
14925 /*check_dependency_p=*/true,
14927 /*is_declaration=*/true);
14929 return cp_parser_namespace_name (parser
);
14932 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14933 access declaration.
14936 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14937 using :: unqualified-id ;
14939 access-declaration:
14945 cp_parser_using_declaration (cp_parser
* parser
,
14946 bool access_declaration_p
)
14949 bool typename_p
= false;
14950 bool global_scope_p
;
14954 int oldcount
= errorcount
;
14955 cp_token
*diag_token
= NULL
;
14957 if (access_declaration_p
)
14959 diag_token
= cp_lexer_peek_token (parser
->lexer
);
14960 cp_parser_parse_tentatively (parser
);
14964 /* Look for the `using' keyword. */
14965 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
14967 /* Peek at the next token. */
14968 token
= cp_lexer_peek_token (parser
->lexer
);
14969 /* See if it's `typename'. */
14970 if (token
->keyword
== RID_TYPENAME
)
14972 /* Remember that we've seen it. */
14974 /* Consume the `typename' token. */
14975 cp_lexer_consume_token (parser
->lexer
);
14979 /* Look for the optional global scope qualification. */
14981 = (cp_parser_global_scope_opt (parser
,
14982 /*current_scope_valid_p=*/false)
14985 /* If we saw `typename', or didn't see `::', then there must be a
14986 nested-name-specifier present. */
14987 if (typename_p
|| !global_scope_p
)
14988 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
14989 /*check_dependency_p=*/true,
14991 /*is_declaration=*/true);
14992 /* Otherwise, we could be in either of the two productions. In that
14993 case, treat the nested-name-specifier as optional. */
14995 qscope
= cp_parser_nested_name_specifier_opt (parser
,
14996 /*typename_keyword_p=*/false,
14997 /*check_dependency_p=*/true,
14999 /*is_declaration=*/true);
15001 qscope
= global_namespace
;
15003 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
15004 /* Something has already gone wrong; there's no need to parse
15005 further. Since an error has occurred, the return value of
15006 cp_parser_parse_definitely will be false, as required. */
15007 return cp_parser_parse_definitely (parser
);
15009 token
= cp_lexer_peek_token (parser
->lexer
);
15010 /* Parse the unqualified-id. */
15011 identifier
= cp_parser_unqualified_id (parser
,
15012 /*template_keyword_p=*/false,
15013 /*check_dependency_p=*/true,
15014 /*declarator_p=*/true,
15015 /*optional_p=*/false);
15017 if (access_declaration_p
)
15019 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15020 cp_parser_simulate_error (parser
);
15021 if (!cp_parser_parse_definitely (parser
))
15025 /* The function we call to handle a using-declaration is different
15026 depending on what scope we are in. */
15027 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
15029 else if (TREE_CODE (identifier
) != IDENTIFIER_NODE
15030 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
15031 /* [namespace.udecl]
15033 A using declaration shall not name a template-id. */
15034 error_at (token
->location
,
15035 "a template-id may not appear in a using-declaration");
15038 if (at_class_scope_p ())
15040 /* Create the USING_DECL. */
15041 decl
= do_class_using_decl (parser
->scope
, identifier
);
15043 if (decl
&& typename_p
)
15044 USING_DECL_TYPENAME_P (decl
) = 1;
15046 if (check_for_bare_parameter_packs (decl
))
15049 /* Add it to the list of members in this class. */
15050 finish_member_declaration (decl
);
15054 decl
= cp_parser_lookup_name_simple (parser
,
15057 if (decl
== error_mark_node
)
15058 cp_parser_name_lookup_error (parser
, identifier
,
15061 else if (check_for_bare_parameter_packs (decl
))
15063 else if (!at_namespace_scope_p ())
15064 do_local_using_decl (decl
, qscope
, identifier
);
15066 do_toplevel_using_decl (decl
, qscope
, identifier
);
15070 /* Look for the final `;'. */
15071 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15073 if (access_declaration_p
&& errorcount
== oldcount
)
15074 warning_at (diag_token
->location
, OPT_Wdeprecated
,
15075 "access declarations are deprecated "
15076 "in favour of using-declarations; "
15077 "suggestion: add the %<using%> keyword");
15082 /* Parse an alias-declaration.
15085 using identifier attribute-specifier-seq [opt] = type-id */
15088 cp_parser_alias_declaration (cp_parser
* parser
)
15090 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
15091 location_t id_location
, using_location
, attrs_location
= 0;
15092 cp_declarator
*declarator
;
15093 cp_decl_specifier_seq decl_specs
;
15095 const char *saved_message
= NULL
;
15097 /* Look for the `using' keyword. */
15098 using_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
15099 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15100 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
15101 id
= cp_parser_identifier (parser
);
15102 if (id
== error_mark_node
)
15103 return error_mark_node
;
15105 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
15106 attributes
= cp_parser_attributes_opt (parser
);
15107 if (attributes
== error_mark_node
)
15108 return error_mark_node
;
15110 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
15112 if (cp_parser_error_occurred (parser
))
15113 return error_mark_node
;
15115 /* Now we are going to parse the type-id of the declaration. */
15120 "A type-specifier-seq shall not define a class or enumeration
15121 unless it appears in the type-id of an alias-declaration (7.1.3) that
15122 is not the declaration of a template-declaration."
15124 In other words, if we currently are in an alias template, the
15125 type-id should not define a type.
15127 So let's set parser->type_definition_forbidden_message in that
15128 case; cp_parser_check_type_definition (called by
15129 cp_parser_class_specifier) will then emit an error if a type is
15130 defined in the type-id. */
15131 if (parser
->num_template_parameter_lists
)
15133 saved_message
= parser
->type_definition_forbidden_message
;
15134 parser
->type_definition_forbidden_message
=
15135 G_("types may not be defined in alias template declarations");
15138 type
= cp_parser_type_id (parser
);
15140 /* Restore the error message if need be. */
15141 if (parser
->num_template_parameter_lists
)
15142 parser
->type_definition_forbidden_message
= saved_message
;
15144 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15146 if (cp_parser_error_occurred (parser
))
15147 return error_mark_node
;
15149 /* A typedef-name can also be introduced by an alias-declaration. The
15150 identifier following the using keyword becomes a typedef-name. It has
15151 the same semantics as if it were introduced by the typedef
15152 specifier. In particular, it does not define a new type and it shall
15153 not appear in the type-id. */
15155 clear_decl_specs (&decl_specs
);
15156 decl_specs
.type
= type
;
15157 if (attributes
!= NULL_TREE
)
15159 decl_specs
.attributes
= attributes
;
15160 set_and_check_decl_spec_loc (&decl_specs
,
15164 set_and_check_decl_spec_loc (&decl_specs
,
15167 set_and_check_decl_spec_loc (&decl_specs
,
15171 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
15172 declarator
->id_loc
= id_location
;
15174 member_p
= at_class_scope_p ();
15176 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
15177 NULL_TREE
, attributes
);
15179 decl
= start_decl (declarator
, &decl_specs
, 0,
15180 attributes
, NULL_TREE
, &pushed_scope
);
15181 if (decl
== error_mark_node
)
15184 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
15187 pop_scope (pushed_scope
);
15189 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15190 added into the symbol table; otherwise, return the TYPE_DECL. */
15191 if (DECL_LANG_SPECIFIC (decl
)
15192 && DECL_TEMPLATE_INFO (decl
)
15193 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
15195 decl
= DECL_TI_TEMPLATE (decl
);
15197 check_member_template (decl
);
15203 /* Parse a using-directive.
15206 using namespace :: [opt] nested-name-specifier [opt]
15207 namespace-name ; */
15210 cp_parser_using_directive (cp_parser
* parser
)
15212 tree namespace_decl
;
15215 /* Look for the `using' keyword. */
15216 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
15217 /* And the `namespace' keyword. */
15218 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
15219 /* Look for the optional `::' operator. */
15220 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
15221 /* And the optional nested-name-specifier. */
15222 cp_parser_nested_name_specifier_opt (parser
,
15223 /*typename_keyword_p=*/false,
15224 /*check_dependency_p=*/true,
15226 /*is_declaration=*/true);
15227 /* Get the namespace being used. */
15228 namespace_decl
= cp_parser_namespace_name (parser
);
15229 /* And any specified attributes. */
15230 attribs
= cp_parser_attributes_opt (parser
);
15231 /* Update the symbol table. */
15232 parse_using_directive (namespace_decl
, attribs
);
15233 /* Look for the final `;'. */
15234 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15237 /* Parse an asm-definition.
15240 asm ( string-literal ) ;
15245 asm volatile [opt] ( string-literal ) ;
15246 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15247 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15248 : asm-operand-list [opt] ) ;
15249 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15250 : asm-operand-list [opt]
15251 : asm-clobber-list [opt] ) ;
15252 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15253 : asm-clobber-list [opt]
15254 : asm-goto-list ) ; */
15257 cp_parser_asm_definition (cp_parser
* parser
)
15260 tree outputs
= NULL_TREE
;
15261 tree inputs
= NULL_TREE
;
15262 tree clobbers
= NULL_TREE
;
15263 tree labels
= NULL_TREE
;
15265 bool volatile_p
= false;
15266 bool extended_p
= false;
15267 bool invalid_inputs_p
= false;
15268 bool invalid_outputs_p
= false;
15269 bool goto_p
= false;
15270 required_token missing
= RT_NONE
;
15272 /* Look for the `asm' keyword. */
15273 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
15274 /* See if the next token is `volatile'. */
15275 if (cp_parser_allow_gnu_extensions_p (parser
)
15276 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
15278 /* Remember that we saw the `volatile' keyword. */
15280 /* Consume the token. */
15281 cp_lexer_consume_token (parser
->lexer
);
15283 if (cp_parser_allow_gnu_extensions_p (parser
)
15284 && parser
->in_function_body
15285 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
15287 /* Remember that we saw the `goto' keyword. */
15289 /* Consume the token. */
15290 cp_lexer_consume_token (parser
->lexer
);
15292 /* Look for the opening `('. */
15293 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
15295 /* Look for the string. */
15296 string
= cp_parser_string_literal (parser
, false, false);
15297 if (string
== error_mark_node
)
15299 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15300 /*consume_paren=*/true);
15304 /* If we're allowing GNU extensions, check for the extended assembly
15305 syntax. Unfortunately, the `:' tokens need not be separated by
15306 a space in C, and so, for compatibility, we tolerate that here
15307 too. Doing that means that we have to treat the `::' operator as
15309 if (cp_parser_allow_gnu_extensions_p (parser
)
15310 && parser
->in_function_body
15311 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
15312 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
15314 bool inputs_p
= false;
15315 bool clobbers_p
= false;
15316 bool labels_p
= false;
15318 /* The extended syntax was used. */
15321 /* Look for outputs. */
15322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15324 /* Consume the `:'. */
15325 cp_lexer_consume_token (parser
->lexer
);
15326 /* Parse the output-operands. */
15327 if (cp_lexer_next_token_is_not (parser
->lexer
,
15329 && cp_lexer_next_token_is_not (parser
->lexer
,
15331 && cp_lexer_next_token_is_not (parser
->lexer
,
15334 outputs
= cp_parser_asm_operand_list (parser
);
15336 if (outputs
== error_mark_node
)
15337 invalid_outputs_p
= true;
15339 /* If the next token is `::', there are no outputs, and the
15340 next token is the beginning of the inputs. */
15341 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15342 /* The inputs are coming next. */
15345 /* Look for inputs. */
15347 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15349 /* Consume the `:' or `::'. */
15350 cp_lexer_consume_token (parser
->lexer
);
15351 /* Parse the output-operands. */
15352 if (cp_lexer_next_token_is_not (parser
->lexer
,
15354 && cp_lexer_next_token_is_not (parser
->lexer
,
15356 && cp_lexer_next_token_is_not (parser
->lexer
,
15358 inputs
= cp_parser_asm_operand_list (parser
);
15360 if (inputs
== error_mark_node
)
15361 invalid_inputs_p
= true;
15363 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15364 /* The clobbers are coming next. */
15367 /* Look for clobbers. */
15369 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15372 /* Consume the `:' or `::'. */
15373 cp_lexer_consume_token (parser
->lexer
);
15374 /* Parse the clobbers. */
15375 if (cp_lexer_next_token_is_not (parser
->lexer
,
15377 && cp_lexer_next_token_is_not (parser
->lexer
,
15379 clobbers
= cp_parser_asm_clobber_list (parser
);
15382 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
15383 /* The labels are coming next. */
15386 /* Look for labels. */
15388 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
15391 /* Consume the `:' or `::'. */
15392 cp_lexer_consume_token (parser
->lexer
);
15393 /* Parse the labels. */
15394 labels
= cp_parser_asm_label_list (parser
);
15397 if (goto_p
&& !labels_p
)
15398 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
15401 missing
= RT_COLON_SCOPE
;
15403 /* Look for the closing `)'. */
15404 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
15405 missing
? missing
: RT_CLOSE_PAREN
))
15406 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15407 /*consume_paren=*/true);
15408 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15410 if (!invalid_inputs_p
&& !invalid_outputs_p
)
15412 /* Create the ASM_EXPR. */
15413 if (parser
->in_function_body
)
15415 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
15416 inputs
, clobbers
, labels
);
15417 /* If the extended syntax was not used, mark the ASM_EXPR. */
15420 tree temp
= asm_stmt
;
15421 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
15422 temp
= TREE_OPERAND (temp
, 0);
15424 ASM_INPUT_P (temp
) = 1;
15428 add_asm_node (string
);
15432 /* Declarators [gram.dcl.decl] */
15434 /* Parse an init-declarator.
15437 declarator initializer [opt]
15442 declarator asm-specification [opt] attributes [opt] initializer [opt]
15444 function-definition:
15445 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15447 decl-specifier-seq [opt] declarator function-try-block
15451 function-definition:
15452 __extension__ function-definition
15456 function-definition:
15457 decl-specifier-seq [opt] declarator function-transaction-block
15459 The DECL_SPECIFIERS apply to this declarator. Returns a
15460 representation of the entity declared. If MEMBER_P is TRUE, then
15461 this declarator appears in a class scope. The new DECL created by
15462 this declarator is returned.
15464 The CHECKS are access checks that should be performed once we know
15465 what entity is being declared (and, therefore, what classes have
15468 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15469 for a function-definition here as well. If the declarator is a
15470 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15471 be TRUE upon return. By that point, the function-definition will
15472 have been completely parsed.
15474 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15477 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15478 parsed declaration if it is an uninitialized single declarator not followed
15479 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15480 if present, will not be consumed. If returned, this declarator will be
15481 created with SD_INITIALIZED but will not call cp_finish_decl. */
15484 cp_parser_init_declarator (cp_parser
* parser
,
15485 cp_decl_specifier_seq
*decl_specifiers
,
15486 VEC (deferred_access_check
,gc
)* checks
,
15487 bool function_definition_allowed_p
,
15489 int declares_class_or_enum
,
15490 bool* function_definition_p
,
15491 tree
* maybe_range_for_decl
)
15493 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
15494 *attributes_start_token
= NULL
;
15495 cp_declarator
*declarator
;
15496 tree prefix_attributes
;
15498 tree asm_specification
;
15500 tree decl
= NULL_TREE
;
15502 int is_initialized
;
15503 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15504 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15506 enum cpp_ttype initialization_kind
;
15507 bool is_direct_init
= false;
15508 bool is_non_constant_init
;
15509 int ctor_dtor_or_conv_p
;
15511 tree pushed_scope
= NULL_TREE
;
15512 bool range_for_decl_p
= false;
15514 /* Gather the attributes that were provided with the
15515 decl-specifiers. */
15516 prefix_attributes
= decl_specifiers
->attributes
;
15518 /* Assume that this is not the declarator for a function
15520 if (function_definition_p
)
15521 *function_definition_p
= false;
15523 /* Defer access checks while parsing the declarator; we cannot know
15524 what names are accessible until we know what is being
15526 resume_deferring_access_checks ();
15528 /* Parse the declarator. */
15529 token
= cp_lexer_peek_token (parser
->lexer
);
15531 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
15532 &ctor_dtor_or_conv_p
,
15533 /*parenthesized_p=*/NULL
,
15535 /* Gather up the deferred checks. */
15536 stop_deferring_access_checks ();
15538 /* If the DECLARATOR was erroneous, there's no need to go
15540 if (declarator
== cp_error_declarator
)
15541 return error_mark_node
;
15543 /* Check that the number of template-parameter-lists is OK. */
15544 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
15546 return error_mark_node
;
15548 if (declares_class_or_enum
& 2)
15549 cp_parser_check_for_definition_in_return_type (declarator
,
15550 decl_specifiers
->type
,
15551 decl_specifiers
->locations
[ds_type_spec
]);
15553 /* Figure out what scope the entity declared by the DECLARATOR is
15554 located in. `grokdeclarator' sometimes changes the scope, so
15555 we compute it now. */
15556 scope
= get_scope_of_declarator (declarator
);
15558 /* Perform any lookups in the declared type which were thought to be
15559 dependent, but are not in the scope of the declarator. */
15560 decl_specifiers
->type
15561 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
15563 /* If we're allowing GNU extensions, look for an asm-specification
15565 if (cp_parser_allow_gnu_extensions_p (parser
))
15567 /* Look for an asm-specification. */
15568 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
15569 asm_specification
= cp_parser_asm_specification_opt (parser
);
15570 /* And attributes. */
15571 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
15572 attributes
= cp_parser_attributes_opt (parser
);
15576 asm_specification
= NULL_TREE
;
15577 attributes
= NULL_TREE
;
15580 /* Peek at the next token. */
15581 token
= cp_lexer_peek_token (parser
->lexer
);
15582 /* Check to see if the token indicates the start of a
15583 function-definition. */
15584 if (function_declarator_p (declarator
)
15585 && cp_parser_token_starts_function_definition_p (token
))
15587 if (!function_definition_allowed_p
)
15589 /* If a function-definition should not appear here, issue an
15591 cp_parser_error (parser
,
15592 "a function-definition is not allowed here");
15593 return error_mark_node
;
15597 location_t func_brace_location
15598 = cp_lexer_peek_token (parser
->lexer
)->location
;
15600 /* Neither attributes nor an asm-specification are allowed
15601 on a function-definition. */
15602 if (asm_specification
)
15603 error_at (asm_spec_start_token
->location
,
15604 "an asm-specification is not allowed "
15605 "on a function-definition");
15607 error_at (attributes_start_token
->location
,
15608 "attributes are not allowed on a function-definition");
15609 /* This is a function-definition. */
15610 *function_definition_p
= true;
15612 /* Parse the function definition. */
15614 decl
= cp_parser_save_member_function_body (parser
,
15617 prefix_attributes
);
15620 = (cp_parser_function_definition_from_specifiers_and_declarator
15621 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
15623 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
15625 /* This is where the prologue starts... */
15626 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
15627 = func_brace_location
;
15636 Only in function declarations for constructors, destructors, and
15637 type conversions can the decl-specifier-seq be omitted.
15639 We explicitly postpone this check past the point where we handle
15640 function-definitions because we tolerate function-definitions
15641 that are missing their return types in some modes. */
15642 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
15644 cp_parser_error (parser
,
15645 "expected constructor, destructor, or type conversion");
15646 return error_mark_node
;
15649 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15650 if (token
->type
== CPP_EQ
15651 || token
->type
== CPP_OPEN_PAREN
15652 || token
->type
== CPP_OPEN_BRACE
)
15654 is_initialized
= SD_INITIALIZED
;
15655 initialization_kind
= token
->type
;
15656 if (maybe_range_for_decl
)
15657 *maybe_range_for_decl
= error_mark_node
;
15659 if (token
->type
== CPP_EQ
15660 && function_declarator_p (declarator
))
15662 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15663 if (t2
->keyword
== RID_DEFAULT
)
15664 is_initialized
= SD_DEFAULTED
;
15665 else if (t2
->keyword
== RID_DELETE
)
15666 is_initialized
= SD_DELETED
;
15671 /* If the init-declarator isn't initialized and isn't followed by a
15672 `,' or `;', it's not a valid init-declarator. */
15673 if (token
->type
!= CPP_COMMA
15674 && token
->type
!= CPP_SEMICOLON
)
15676 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
15677 range_for_decl_p
= true;
15680 cp_parser_error (parser
, "expected initializer");
15681 return error_mark_node
;
15684 is_initialized
= SD_UNINITIALIZED
;
15685 initialization_kind
= CPP_EOF
;
15688 /* Because start_decl has side-effects, we should only call it if we
15689 know we're going ahead. By this point, we know that we cannot
15690 possibly be looking at any other construct. */
15691 cp_parser_commit_to_tentative_parse (parser
);
15693 /* If the decl specifiers were bad, issue an error now that we're
15694 sure this was intended to be a declarator. Then continue
15695 declaring the variable(s), as int, to try to cut down on further
15697 if (decl_specifiers
->any_specifiers_p
15698 && decl_specifiers
->type
== error_mark_node
)
15700 cp_parser_error (parser
, "invalid type in declaration");
15701 decl_specifiers
->type
= integer_type_node
;
15704 /* Check to see whether or not this declaration is a friend. */
15705 friend_p
= cp_parser_friend_p (decl_specifiers
);
15707 /* Enter the newly declared entry in the symbol table. If we're
15708 processing a declaration in a class-specifier, we wait until
15709 after processing the initializer. */
15712 if (parser
->in_unbraced_linkage_specification_p
)
15713 decl_specifiers
->storage_class
= sc_extern
;
15714 decl
= start_decl (declarator
, decl_specifiers
,
15715 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
15716 attributes
, prefix_attributes
,
15718 /* Adjust location of decl if declarator->id_loc is more appropriate:
15719 set, and decl wasn't merged with another decl, in which case its
15720 location would be different from input_location, and more accurate. */
15722 && declarator
->id_loc
!= UNKNOWN_LOCATION
15723 && DECL_SOURCE_LOCATION (decl
) == input_location
)
15724 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
15727 /* Enter the SCOPE. That way unqualified names appearing in the
15728 initializer will be looked up in SCOPE. */
15729 pushed_scope
= push_scope (scope
);
15731 /* Perform deferred access control checks, now that we know in which
15732 SCOPE the declared entity resides. */
15733 if (!member_p
&& decl
)
15735 tree saved_current_function_decl
= NULL_TREE
;
15737 /* If the entity being declared is a function, pretend that we
15738 are in its scope. If it is a `friend', it may have access to
15739 things that would not otherwise be accessible. */
15740 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15742 saved_current_function_decl
= current_function_decl
;
15743 current_function_decl
= decl
;
15746 /* Perform access checks for template parameters. */
15747 cp_parser_perform_template_parameter_access_checks (checks
);
15749 /* Perform the access control checks for the declarator and the
15750 decl-specifiers. */
15751 perform_deferred_access_checks (tf_warning_or_error
);
15753 /* Restore the saved value. */
15754 if (TREE_CODE (decl
) == FUNCTION_DECL
)
15755 current_function_decl
= saved_current_function_decl
;
15758 /* Parse the initializer. */
15759 initializer
= NULL_TREE
;
15760 is_direct_init
= false;
15761 is_non_constant_init
= true;
15762 if (is_initialized
)
15764 if (function_declarator_p (declarator
))
15766 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
15767 if (initialization_kind
== CPP_EQ
)
15768 initializer
= cp_parser_pure_specifier (parser
);
15771 /* If the declaration was erroneous, we don't really
15772 know what the user intended, so just silently
15773 consume the initializer. */
15774 if (decl
!= error_mark_node
)
15775 error_at (initializer_start_token
->location
,
15776 "initializer provided for function");
15777 cp_parser_skip_to_closing_parenthesis (parser
,
15778 /*recovering=*/true,
15779 /*or_comma=*/false,
15780 /*consume_paren=*/true);
15785 /* We want to record the extra mangling scope for in-class
15786 initializers of class members and initializers of static data
15787 member templates. The former involves deferring
15788 parsing of the initializer until end of class as with default
15789 arguments. So right here we only handle the latter. */
15790 if (!member_p
&& processing_template_decl
)
15791 start_lambda_scope (decl
);
15792 initializer
= cp_parser_initializer (parser
,
15794 &is_non_constant_init
);
15795 if (!member_p
&& processing_template_decl
)
15796 finish_lambda_scope ();
15797 if (initializer
== error_mark_node
)
15798 cp_parser_skip_to_end_of_statement (parser
);
15802 /* The old parser allows attributes to appear after a parenthesized
15803 initializer. Mark Mitchell proposed removing this functionality
15804 on the GCC mailing lists on 2002-08-13. This parser accepts the
15805 attributes -- but ignores them. */
15806 if (cp_parser_allow_gnu_extensions_p (parser
)
15807 && initialization_kind
== CPP_OPEN_PAREN
)
15808 if (cp_parser_attributes_opt (parser
))
15809 warning (OPT_Wattributes
,
15810 "attributes after parenthesized initializer ignored");
15812 /* For an in-class declaration, use `grokfield' to create the
15818 pop_scope (pushed_scope
);
15819 pushed_scope
= NULL_TREE
;
15821 decl
= grokfield (declarator
, decl_specifiers
,
15822 initializer
, !is_non_constant_init
,
15823 /*asmspec=*/NULL_TREE
,
15824 prefix_attributes
);
15825 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
15826 cp_parser_save_default_args (parser
, decl
);
15829 /* Finish processing the declaration. But, skip member
15831 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
15833 cp_finish_decl (decl
,
15834 initializer
, !is_non_constant_init
,
15836 /* If the initializer is in parentheses, then this is
15837 a direct-initialization, which means that an
15838 `explicit' constructor is OK. Otherwise, an
15839 `explicit' constructor cannot be used. */
15840 ((is_direct_init
|| !is_initialized
)
15841 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
15843 else if ((cxx_dialect
!= cxx98
) && friend_p
15844 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
15845 /* Core issue #226 (C++0x only): A default template-argument
15846 shall not be specified in a friend class template
15848 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
15849 /*is_partial=*/false, /*is_friend_decl=*/1);
15851 if (!friend_p
&& pushed_scope
)
15852 pop_scope (pushed_scope
);
15857 /* Parse a declarator.
15861 ptr-operator declarator
15863 abstract-declarator:
15864 ptr-operator abstract-declarator [opt]
15865 direct-abstract-declarator
15870 attributes [opt] direct-declarator
15871 attributes [opt] ptr-operator declarator
15873 abstract-declarator:
15874 attributes [opt] ptr-operator abstract-declarator [opt]
15875 attributes [opt] direct-abstract-declarator
15877 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15878 detect constructor, destructor or conversion operators. It is set
15879 to -1 if the declarator is a name, and +1 if it is a
15880 function. Otherwise it is set to zero. Usually you just want to
15881 test for >0, but internally the negative value is used.
15883 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15884 a decl-specifier-seq unless it declares a constructor, destructor,
15885 or conversion. It might seem that we could check this condition in
15886 semantic analysis, rather than parsing, but that makes it difficult
15887 to handle something like `f()'. We want to notice that there are
15888 no decl-specifiers, and therefore realize that this is an
15889 expression, not a declaration.)
15891 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15892 the declarator is a direct-declarator of the form "(...)".
15894 MEMBER_P is true iff this declarator is a member-declarator. */
15896 static cp_declarator
*
15897 cp_parser_declarator (cp_parser
* parser
,
15898 cp_parser_declarator_kind dcl_kind
,
15899 int* ctor_dtor_or_conv_p
,
15900 bool* parenthesized_p
,
15903 cp_declarator
*declarator
;
15904 enum tree_code code
;
15905 cp_cv_quals cv_quals
;
15907 tree attributes
= NULL_TREE
;
15909 /* Assume this is not a constructor, destructor, or type-conversion
15911 if (ctor_dtor_or_conv_p
)
15912 *ctor_dtor_or_conv_p
= 0;
15914 if (cp_parser_allow_gnu_extensions_p (parser
))
15915 attributes
= cp_parser_attributes_opt (parser
);
15917 /* Check for the ptr-operator production. */
15918 cp_parser_parse_tentatively (parser
);
15919 /* Parse the ptr-operator. */
15920 code
= cp_parser_ptr_operator (parser
,
15923 /* If that worked, then we have a ptr-operator. */
15924 if (cp_parser_parse_definitely (parser
))
15926 /* If a ptr-operator was found, then this declarator was not
15928 if (parenthesized_p
)
15929 *parenthesized_p
= true;
15930 /* The dependent declarator is optional if we are parsing an
15931 abstract-declarator. */
15932 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
15933 cp_parser_parse_tentatively (parser
);
15935 /* Parse the dependent declarator. */
15936 declarator
= cp_parser_declarator (parser
, dcl_kind
,
15937 /*ctor_dtor_or_conv_p=*/NULL
,
15938 /*parenthesized_p=*/NULL
,
15939 /*member_p=*/false);
15941 /* If we are parsing an abstract-declarator, we must handle the
15942 case where the dependent declarator is absent. */
15943 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
15944 && !cp_parser_parse_definitely (parser
))
15947 declarator
= cp_parser_make_indirect_declarator
15948 (code
, class_type
, cv_quals
, declarator
);
15950 /* Everything else is a direct-declarator. */
15953 if (parenthesized_p
)
15954 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
15956 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
15957 ctor_dtor_or_conv_p
,
15961 if (attributes
&& declarator
&& declarator
!= cp_error_declarator
)
15962 declarator
->attributes
= attributes
;
15967 /* Parse a direct-declarator or direct-abstract-declarator.
15971 direct-declarator ( parameter-declaration-clause )
15972 cv-qualifier-seq [opt]
15973 exception-specification [opt]
15974 direct-declarator [ constant-expression [opt] ]
15977 direct-abstract-declarator:
15978 direct-abstract-declarator [opt]
15979 ( parameter-declaration-clause )
15980 cv-qualifier-seq [opt]
15981 exception-specification [opt]
15982 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15983 ( abstract-declarator )
15985 Returns a representation of the declarator. DCL_KIND is
15986 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15987 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15988 we are parsing a direct-declarator. It is
15989 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15990 of ambiguity we prefer an abstract declarator, as per
15991 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15992 cp_parser_declarator. */
15994 static cp_declarator
*
15995 cp_parser_direct_declarator (cp_parser
* parser
,
15996 cp_parser_declarator_kind dcl_kind
,
15997 int* ctor_dtor_or_conv_p
,
16001 cp_declarator
*declarator
= NULL
;
16002 tree scope
= NULL_TREE
;
16003 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16004 bool saved_in_declarator_p
= parser
->in_declarator_p
;
16006 tree pushed_scope
= NULL_TREE
;
16010 /* Peek at the next token. */
16011 token
= cp_lexer_peek_token (parser
->lexer
);
16012 if (token
->type
== CPP_OPEN_PAREN
)
16014 /* This is either a parameter-declaration-clause, or a
16015 parenthesized declarator. When we know we are parsing a
16016 named declarator, it must be a parenthesized declarator
16017 if FIRST is true. For instance, `(int)' is a
16018 parameter-declaration-clause, with an omitted
16019 direct-abstract-declarator. But `((*))', is a
16020 parenthesized abstract declarator. Finally, when T is a
16021 template parameter `(T)' is a
16022 parameter-declaration-clause, and not a parenthesized
16025 We first try and parse a parameter-declaration-clause,
16026 and then try a nested declarator (if FIRST is true).
16028 It is not an error for it not to be a
16029 parameter-declaration-clause, even when FIRST is
16035 The first is the declaration of a function while the
16036 second is the definition of a variable, including its
16039 Having seen only the parenthesis, we cannot know which of
16040 these two alternatives should be selected. Even more
16041 complex are examples like:
16046 The former is a function-declaration; the latter is a
16047 variable initialization.
16049 Thus again, we try a parameter-declaration-clause, and if
16050 that fails, we back out and return. */
16052 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16055 unsigned saved_num_template_parameter_lists
;
16056 bool is_declarator
= false;
16059 /* In a member-declarator, the only valid interpretation
16060 of a parenthesis is the start of a
16061 parameter-declaration-clause. (It is invalid to
16062 initialize a static data member with a parenthesized
16063 initializer; only the "=" form of initialization is
16066 cp_parser_parse_tentatively (parser
);
16068 /* Consume the `('. */
16069 cp_lexer_consume_token (parser
->lexer
);
16072 /* If this is going to be an abstract declarator, we're
16073 in a declarator and we can't have default args. */
16074 parser
->default_arg_ok_p
= false;
16075 parser
->in_declarator_p
= true;
16078 /* Inside the function parameter list, surrounding
16079 template-parameter-lists do not apply. */
16080 saved_num_template_parameter_lists
16081 = parser
->num_template_parameter_lists
;
16082 parser
->num_template_parameter_lists
= 0;
16084 begin_scope (sk_function_parms
, NULL_TREE
);
16086 /* Parse the parameter-declaration-clause. */
16087 params
= cp_parser_parameter_declaration_clause (parser
);
16089 parser
->num_template_parameter_lists
16090 = saved_num_template_parameter_lists
;
16092 /* Consume the `)'. */
16093 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
16095 /* If all went well, parse the cv-qualifier-seq and the
16096 exception-specification. */
16097 if (member_p
|| cp_parser_parse_definitely (parser
))
16099 cp_cv_quals cv_quals
;
16100 cp_virt_specifiers virt_specifiers
;
16101 tree exception_specification
;
16104 is_declarator
= true;
16106 if (ctor_dtor_or_conv_p
)
16107 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
16110 /* Parse the cv-qualifier-seq. */
16111 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
16112 /* And the exception-specification. */
16113 exception_specification
16114 = cp_parser_exception_specification_opt (parser
);
16116 late_return
= (cp_parser_late_return_type_opt
16117 (parser
, member_p
? cv_quals
: -1));
16119 /* Parse the virt-specifier-seq. */
16120 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
16122 /* Create the function-declarator. */
16123 declarator
= make_call_declarator (declarator
,
16127 exception_specification
,
16129 /* Any subsequent parameter lists are to do with
16130 return type, so are not those of the declared
16132 parser
->default_arg_ok_p
= false;
16135 /* Remove the function parms from scope. */
16136 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
16137 pop_binding (DECL_NAME (t
), t
);
16141 /* Repeat the main loop. */
16145 /* If this is the first, we can try a parenthesized
16149 bool saved_in_type_id_in_expr_p
;
16151 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16152 parser
->in_declarator_p
= saved_in_declarator_p
;
16154 /* Consume the `('. */
16155 cp_lexer_consume_token (parser
->lexer
);
16156 /* Parse the nested declarator. */
16157 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
16158 parser
->in_type_id_in_expr_p
= true;
16160 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
16161 /*parenthesized_p=*/NULL
,
16163 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
16165 /* Expect a `)'. */
16166 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
16167 declarator
= cp_error_declarator
;
16168 if (declarator
== cp_error_declarator
)
16171 goto handle_declarator
;
16173 /* Otherwise, we must be done. */
16177 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
16178 && token
->type
== CPP_OPEN_SQUARE
)
16180 /* Parse an array-declarator. */
16183 if (ctor_dtor_or_conv_p
)
16184 *ctor_dtor_or_conv_p
= 0;
16187 parser
->default_arg_ok_p
= false;
16188 parser
->in_declarator_p
= true;
16189 /* Consume the `['. */
16190 cp_lexer_consume_token (parser
->lexer
);
16191 /* Peek at the next token. */
16192 token
= cp_lexer_peek_token (parser
->lexer
);
16193 /* If the next token is `]', then there is no
16194 constant-expression. */
16195 if (token
->type
!= CPP_CLOSE_SQUARE
)
16197 bool non_constant_p
;
16200 = cp_parser_constant_expression (parser
,
16201 /*allow_non_constant=*/true,
16203 if (!non_constant_p
)
16205 else if (error_operand_p (bounds
))
16206 /* Already gave an error. */;
16207 else if (!parser
->in_function_body
16208 || current_binding_level
->kind
== sk_function_parms
)
16210 /* Normally, the array bound must be an integral constant
16211 expression. However, as an extension, we allow VLAs
16212 in function scopes as long as they aren't part of a
16213 parameter declaration. */
16214 cp_parser_error (parser
,
16215 "array bound is not an integer constant");
16216 bounds
= error_mark_node
;
16218 else if (processing_template_decl
)
16220 /* Remember this wasn't a constant-expression. */
16221 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
16222 TREE_SIDE_EFFECTS (bounds
) = 1;
16226 bounds
= NULL_TREE
;
16227 /* Look for the closing `]'. */
16228 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
16230 declarator
= cp_error_declarator
;
16234 declarator
= make_array_declarator (declarator
, bounds
);
16236 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
16239 tree qualifying_scope
;
16240 tree unqualified_name
;
16241 special_function_kind sfk
;
16243 bool pack_expansion_p
= false;
16244 cp_token
*declarator_id_start_token
;
16246 /* Parse a declarator-id */
16247 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
16250 cp_parser_parse_tentatively (parser
);
16252 /* If we see an ellipsis, we should be looking at a
16254 if (token
->type
== CPP_ELLIPSIS
)
16256 /* Consume the `...' */
16257 cp_lexer_consume_token (parser
->lexer
);
16259 pack_expansion_p
= true;
16263 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
16265 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
16266 qualifying_scope
= parser
->scope
;
16271 if (!unqualified_name
&& pack_expansion_p
)
16273 /* Check whether an error occurred. */
16274 okay
= !cp_parser_error_occurred (parser
);
16276 /* We already consumed the ellipsis to mark a
16277 parameter pack, but we have no way to report it,
16278 so abort the tentative parse. We will be exiting
16279 immediately anyway. */
16280 cp_parser_abort_tentative_parse (parser
);
16283 okay
= cp_parser_parse_definitely (parser
);
16286 unqualified_name
= error_mark_node
;
16287 else if (unqualified_name
16288 && (qualifying_scope
16289 || (TREE_CODE (unqualified_name
)
16290 != IDENTIFIER_NODE
)))
16292 cp_parser_error (parser
, "expected unqualified-id");
16293 unqualified_name
= error_mark_node
;
16297 if (!unqualified_name
)
16299 if (unqualified_name
== error_mark_node
)
16301 declarator
= cp_error_declarator
;
16302 pack_expansion_p
= false;
16303 declarator
->parameter_pack_p
= false;
16307 if (qualifying_scope
&& at_namespace_scope_p ()
16308 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
16310 /* In the declaration of a member of a template class
16311 outside of the class itself, the SCOPE will sometimes
16312 be a TYPENAME_TYPE. For example, given:
16314 template <typename T>
16315 int S<T>::R::i = 3;
16317 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16318 this context, we must resolve S<T>::R to an ordinary
16319 type, rather than a typename type.
16321 The reason we normally avoid resolving TYPENAME_TYPEs
16322 is that a specialization of `S' might render
16323 `S<T>::R' not a type. However, if `S' is
16324 specialized, then this `i' will not be used, so there
16325 is no harm in resolving the types here. */
16328 /* Resolve the TYPENAME_TYPE. */
16329 type
= resolve_typename_type (qualifying_scope
,
16330 /*only_current_p=*/false);
16331 /* If that failed, the declarator is invalid. */
16332 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16334 if (typedef_variant_p (type
))
16335 error_at (declarator_id_start_token
->location
,
16336 "cannot define member of dependent typedef "
16339 error_at (declarator_id_start_token
->location
,
16340 "%<%T::%E%> is not a type",
16341 TYPE_CONTEXT (qualifying_scope
),
16342 TYPE_IDENTIFIER (qualifying_scope
));
16344 qualifying_scope
= type
;
16349 if (unqualified_name
)
16353 if (qualifying_scope
16354 && CLASS_TYPE_P (qualifying_scope
))
16355 class_type
= qualifying_scope
;
16357 class_type
= current_class_type
;
16359 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
16361 tree name_type
= TREE_TYPE (unqualified_name
);
16362 if (class_type
&& same_type_p (name_type
, class_type
))
16364 if (qualifying_scope
16365 && CLASSTYPE_USE_TEMPLATE (name_type
))
16367 error_at (declarator_id_start_token
->location
,
16368 "invalid use of constructor as a template");
16369 inform (declarator_id_start_token
->location
,
16370 "use %<%T::%D%> instead of %<%T::%D%> to "
16371 "name the constructor in a qualified name",
16373 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
16374 class_type
, name_type
);
16375 declarator
= cp_error_declarator
;
16379 unqualified_name
= constructor_name (class_type
);
16383 /* We do not attempt to print the declarator
16384 here because we do not have enough
16385 information about its original syntactic
16387 cp_parser_error (parser
, "invalid declarator");
16388 declarator
= cp_error_declarator
;
16395 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
16396 sfk
= sfk_destructor
;
16397 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
16398 sfk
= sfk_conversion
;
16399 else if (/* There's no way to declare a constructor
16400 for an anonymous type, even if the type
16401 got a name for linkage purposes. */
16402 !TYPE_WAS_ANONYMOUS (class_type
)
16403 && constructor_name_p (unqualified_name
,
16406 unqualified_name
= constructor_name (class_type
);
16407 sfk
= sfk_constructor
;
16409 else if (is_overloaded_fn (unqualified_name
)
16410 && DECL_CONSTRUCTOR_P (get_first_fn
16411 (unqualified_name
)))
16412 sfk
= sfk_constructor
;
16414 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
16415 *ctor_dtor_or_conv_p
= -1;
16418 declarator
= make_id_declarator (qualifying_scope
,
16421 declarator
->id_loc
= token
->location
;
16422 declarator
->parameter_pack_p
= pack_expansion_p
;
16424 if (pack_expansion_p
)
16425 maybe_warn_variadic_templates ();
16428 handle_declarator
:;
16429 scope
= get_scope_of_declarator (declarator
);
16431 /* Any names that appear after the declarator-id for a
16432 member are looked up in the containing scope. */
16433 pushed_scope
= push_scope (scope
);
16434 parser
->in_declarator_p
= true;
16435 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
16436 || (declarator
&& declarator
->kind
== cdk_id
))
16437 /* Default args are only allowed on function
16439 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16441 parser
->default_arg_ok_p
= false;
16450 /* For an abstract declarator, we might wind up with nothing at this
16451 point. That's an error; the declarator is not optional. */
16453 cp_parser_error (parser
, "expected declarator");
16455 /* If we entered a scope, we must exit it now. */
16457 pop_scope (pushed_scope
);
16459 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16460 parser
->in_declarator_p
= saved_in_declarator_p
;
16465 /* Parse a ptr-operator.
16468 * cv-qualifier-seq [opt]
16470 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16475 & cv-qualifier-seq [opt]
16477 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16478 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16479 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16480 filled in with the TYPE containing the member. *CV_QUALS is
16481 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16482 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16483 Note that the tree codes returned by this function have nothing
16484 to do with the types of trees that will be eventually be created
16485 to represent the pointer or reference type being parsed. They are
16486 just constants with suggestive names. */
16487 static enum tree_code
16488 cp_parser_ptr_operator (cp_parser
* parser
,
16490 cp_cv_quals
*cv_quals
)
16492 enum tree_code code
= ERROR_MARK
;
16495 /* Assume that it's not a pointer-to-member. */
16497 /* And that there are no cv-qualifiers. */
16498 *cv_quals
= TYPE_UNQUALIFIED
;
16500 /* Peek at the next token. */
16501 token
= cp_lexer_peek_token (parser
->lexer
);
16503 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16504 if (token
->type
== CPP_MULT
)
16505 code
= INDIRECT_REF
;
16506 else if (token
->type
== CPP_AND
)
16508 else if ((cxx_dialect
!= cxx98
) &&
16509 token
->type
== CPP_AND_AND
) /* C++0x only */
16510 code
= NON_LVALUE_EXPR
;
16512 if (code
!= ERROR_MARK
)
16514 /* Consume the `*', `&' or `&&'. */
16515 cp_lexer_consume_token (parser
->lexer
);
16517 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16518 `&', if we are allowing GNU extensions. (The only qualifier
16519 that can legally appear after `&' is `restrict', but that is
16520 enforced during semantic analysis. */
16521 if (code
== INDIRECT_REF
16522 || cp_parser_allow_gnu_extensions_p (parser
))
16523 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
16527 /* Try the pointer-to-member case. */
16528 cp_parser_parse_tentatively (parser
);
16529 /* Look for the optional `::' operator. */
16530 cp_parser_global_scope_opt (parser
,
16531 /*current_scope_valid_p=*/false);
16532 /* Look for the nested-name specifier. */
16533 token
= cp_lexer_peek_token (parser
->lexer
);
16534 cp_parser_nested_name_specifier (parser
,
16535 /*typename_keyword_p=*/false,
16536 /*check_dependency_p=*/true,
16538 /*is_declaration=*/false);
16539 /* If we found it, and the next token is a `*', then we are
16540 indeed looking at a pointer-to-member operator. */
16541 if (!cp_parser_error_occurred (parser
)
16542 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
16544 /* Indicate that the `*' operator was used. */
16545 code
= INDIRECT_REF
;
16547 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
16548 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
16549 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
16550 error_at (token
->location
, "cannot form pointer to member of "
16551 "non-class %q#T", parser
->scope
);
16554 /* The type of which the member is a member is given by the
16556 *type
= parser
->scope
;
16557 /* The next name will not be qualified. */
16558 parser
->scope
= NULL_TREE
;
16559 parser
->qualifying_scope
= NULL_TREE
;
16560 parser
->object_scope
= NULL_TREE
;
16561 /* Look for the optional cv-qualifier-seq. */
16562 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
16565 /* If that didn't work we don't have a ptr-operator. */
16566 if (!cp_parser_parse_definitely (parser
))
16567 cp_parser_error (parser
, "expected ptr-operator");
16573 /* Parse an (optional) cv-qualifier-seq.
16576 cv-qualifier cv-qualifier-seq [opt]
16587 Returns a bitmask representing the cv-qualifiers. */
16590 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
16592 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
16597 cp_cv_quals cv_qualifier
;
16599 /* Peek at the next token. */
16600 token
= cp_lexer_peek_token (parser
->lexer
);
16601 /* See if it's a cv-qualifier. */
16602 switch (token
->keyword
)
16605 cv_qualifier
= TYPE_QUAL_CONST
;
16609 cv_qualifier
= TYPE_QUAL_VOLATILE
;
16613 cv_qualifier
= TYPE_QUAL_RESTRICT
;
16617 cv_qualifier
= TYPE_UNQUALIFIED
;
16624 if (cv_quals
& cv_qualifier
)
16626 error_at (token
->location
, "duplicate cv-qualifier");
16627 cp_lexer_purge_token (parser
->lexer
);
16631 cp_lexer_consume_token (parser
->lexer
);
16632 cv_quals
|= cv_qualifier
;
16639 /* Parse an (optional) virt-specifier-seq.
16641 virt-specifier-seq:
16642 virt-specifier virt-specifier-seq [opt]
16648 Returns a bitmask representing the virt-specifiers. */
16650 static cp_virt_specifiers
16651 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
16653 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
16658 cp_virt_specifiers virt_specifier
;
16660 /* Peek at the next token. */
16661 token
= cp_lexer_peek_token (parser
->lexer
);
16662 /* See if it's a virt-specifier-qualifier. */
16663 if (token
->type
!= CPP_NAME
)
16665 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
16667 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
16668 virt_specifier
= VIRT_SPEC_OVERRIDE
;
16670 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
16672 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
16673 virt_specifier
= VIRT_SPEC_FINAL
;
16675 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
16677 virt_specifier
= VIRT_SPEC_FINAL
;
16682 if (virt_specifiers
& virt_specifier
)
16684 error_at (token
->location
, "duplicate virt-specifier");
16685 cp_lexer_purge_token (parser
->lexer
);
16689 cp_lexer_consume_token (parser
->lexer
);
16690 virt_specifiers
|= virt_specifier
;
16693 return virt_specifiers
;
16696 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16697 is in scope even though it isn't real. */
16700 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
16704 if (current_class_ptr
)
16706 /* We don't clear this between NSDMIs. Is it already what we want? */
16707 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
16708 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
16709 && cp_type_quals (type
) == quals
)
16713 this_parm
= build_this_parm (ctype
, quals
);
16714 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16715 current_class_ptr
= NULL_TREE
;
16717 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
16718 current_class_ptr
= this_parm
;
16721 /* Parse a late-specified return type, if any. This is not a separate
16722 non-terminal, but part of a function declarator, which looks like
16724 -> trailing-type-specifier-seq abstract-declarator(opt)
16726 Returns the type indicated by the type-id.
16728 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16732 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_cv_quals quals
)
16737 /* Peek at the next token. */
16738 token
= cp_lexer_peek_token (parser
->lexer
);
16739 /* A late-specified return type is indicated by an initial '->'. */
16740 if (token
->type
!= CPP_DEREF
)
16743 /* Consume the ->. */
16744 cp_lexer_consume_token (parser
->lexer
);
16748 /* DR 1207: 'this' is in scope in the trailing return type. */
16749 gcc_assert (current_class_ptr
== NULL_TREE
);
16750 inject_this_parameter (current_class_type
, quals
);
16753 type
= cp_parser_trailing_type_id (parser
);
16756 current_class_ptr
= current_class_ref
= NULL_TREE
;
16761 /* Parse a declarator-id.
16765 :: [opt] nested-name-specifier [opt] type-name
16767 In the `id-expression' case, the value returned is as for
16768 cp_parser_id_expression if the id-expression was an unqualified-id.
16769 If the id-expression was a qualified-id, then a SCOPE_REF is
16770 returned. The first operand is the scope (either a NAMESPACE_DECL
16771 or TREE_TYPE), but the second is still just a representation of an
16775 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
16778 /* The expression must be an id-expression. Assume that qualified
16779 names are the names of types so that:
16782 int S<T>::R::i = 3;
16784 will work; we must treat `S<T>::R' as the name of a type.
16785 Similarly, assume that qualified names are templates, where
16789 int S<T>::R<T>::i = 3;
16792 id
= cp_parser_id_expression (parser
,
16793 /*template_keyword_p=*/false,
16794 /*check_dependency_p=*/false,
16795 /*template_p=*/NULL
,
16796 /*declarator_p=*/true,
16798 if (id
&& BASELINK_P (id
))
16799 id
= BASELINK_FUNCTIONS (id
);
16803 /* Parse a type-id.
16806 type-specifier-seq abstract-declarator [opt]
16808 Returns the TYPE specified. */
16811 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
16812 bool is_trailing_return
)
16814 cp_decl_specifier_seq type_specifier_seq
;
16815 cp_declarator
*abstract_declarator
;
16817 /* Parse the type-specifier-seq. */
16818 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
16819 is_trailing_return
,
16820 &type_specifier_seq
);
16821 if (type_specifier_seq
.type
== error_mark_node
)
16822 return error_mark_node
;
16824 /* There might or might not be an abstract declarator. */
16825 cp_parser_parse_tentatively (parser
);
16826 /* Look for the declarator. */
16827 abstract_declarator
16828 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
16829 /*parenthesized_p=*/NULL
,
16830 /*member_p=*/false);
16831 /* Check to see if there really was a declarator. */
16832 if (!cp_parser_parse_definitely (parser
))
16833 abstract_declarator
= NULL
;
16835 if (type_specifier_seq
.type
16836 && type_uses_auto (type_specifier_seq
.type
))
16838 /* A type-id with type 'auto' is only ok if the abstract declarator
16839 is a function declarator with a late-specified return type. */
16840 if (abstract_declarator
16841 && abstract_declarator
->kind
== cdk_function
16842 && abstract_declarator
->u
.function
.late_return_type
)
16846 error ("invalid use of %<auto%>");
16847 return error_mark_node
;
16851 return groktypename (&type_specifier_seq
, abstract_declarator
,
16855 static tree
cp_parser_type_id (cp_parser
*parser
)
16857 return cp_parser_type_id_1 (parser
, false, false);
16860 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
16863 const char *saved_message
= parser
->type_definition_forbidden_message
;
16864 parser
->type_definition_forbidden_message
16865 = G_("types may not be defined in template arguments");
16866 r
= cp_parser_type_id_1 (parser
, true, false);
16867 parser
->type_definition_forbidden_message
= saved_message
;
16871 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
16873 return cp_parser_type_id_1 (parser
, false, true);
16876 /* Parse a type-specifier-seq.
16878 type-specifier-seq:
16879 type-specifier type-specifier-seq [opt]
16883 type-specifier-seq:
16884 attributes type-specifier-seq [opt]
16886 If IS_DECLARATION is true, we are at the start of a "condition" or
16887 exception-declaration, so we might be followed by a declarator-id.
16889 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16890 i.e. we've just seen "->".
16892 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16895 cp_parser_type_specifier_seq (cp_parser
* parser
,
16896 bool is_declaration
,
16897 bool is_trailing_return
,
16898 cp_decl_specifier_seq
*type_specifier_seq
)
16900 bool seen_type_specifier
= false;
16901 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
16902 cp_token
*start_token
= NULL
;
16904 /* Clear the TYPE_SPECIFIER_SEQ. */
16905 clear_decl_specs (type_specifier_seq
);
16907 /* In the context of a trailing return type, enum E { } is an
16908 elaborated-type-specifier followed by a function-body, not an
16910 if (is_trailing_return
)
16911 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
16913 /* Parse the type-specifiers and attributes. */
16916 tree type_specifier
;
16917 bool is_cv_qualifier
;
16919 /* Check for attributes first. */
16920 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
16922 type_specifier_seq
->attributes
=
16923 chainon (type_specifier_seq
->attributes
,
16924 cp_parser_attributes_opt (parser
));
16928 /* record the token of the beginning of the type specifier seq,
16929 for error reporting purposes*/
16931 start_token
= cp_lexer_peek_token (parser
->lexer
);
16933 /* Look for the type-specifier. */
16934 type_specifier
= cp_parser_type_specifier (parser
,
16936 type_specifier_seq
,
16937 /*is_declaration=*/false,
16940 if (!type_specifier
)
16942 /* If the first type-specifier could not be found, this is not a
16943 type-specifier-seq at all. */
16944 if (!seen_type_specifier
)
16946 cp_parser_error (parser
, "expected type-specifier");
16947 type_specifier_seq
->type
= error_mark_node
;
16950 /* If subsequent type-specifiers could not be found, the
16951 type-specifier-seq is complete. */
16955 seen_type_specifier
= true;
16956 /* The standard says that a condition can be:
16958 type-specifier-seq declarator = assignment-expression
16965 we should treat the "S" as a declarator, not as a
16966 type-specifier. The standard doesn't say that explicitly for
16967 type-specifier-seq, but it does say that for
16968 decl-specifier-seq in an ordinary declaration. Perhaps it
16969 would be clearer just to allow a decl-specifier-seq here, and
16970 then add a semantic restriction that if any decl-specifiers
16971 that are not type-specifiers appear, the program is invalid. */
16972 if (is_declaration
&& !is_cv_qualifier
)
16973 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
16977 /* Parse a parameter-declaration-clause.
16979 parameter-declaration-clause:
16980 parameter-declaration-list [opt] ... [opt]
16981 parameter-declaration-list , ...
16983 Returns a representation for the parameter declarations. A return
16984 value of NULL indicates a parameter-declaration-clause consisting
16985 only of an ellipsis. */
16988 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
16995 /* Peek at the next token. */
16996 token
= cp_lexer_peek_token (parser
->lexer
);
16997 /* Check for trivial parameter-declaration-clauses. */
16998 if (token
->type
== CPP_ELLIPSIS
)
17000 /* Consume the `...' token. */
17001 cp_lexer_consume_token (parser
->lexer
);
17004 else if (token
->type
== CPP_CLOSE_PAREN
)
17005 /* There are no parameters. */
17007 #ifndef NO_IMPLICIT_EXTERN_C
17008 if (in_system_header
&& current_class_type
== NULL
17009 && current_lang_name
== lang_name_c
)
17013 return void_list_node
;
17015 /* Check for `(void)', too, which is a special case. */
17016 else if (token
->keyword
== RID_VOID
17017 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
17018 == CPP_CLOSE_PAREN
))
17020 /* Consume the `void' token. */
17021 cp_lexer_consume_token (parser
->lexer
);
17022 /* There are no parameters. */
17023 return void_list_node
;
17026 /* Parse the parameter-declaration-list. */
17027 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
17028 /* If a parse error occurred while parsing the
17029 parameter-declaration-list, then the entire
17030 parameter-declaration-clause is erroneous. */
17034 /* Peek at the next token. */
17035 token
= cp_lexer_peek_token (parser
->lexer
);
17036 /* If it's a `,', the clause should terminate with an ellipsis. */
17037 if (token
->type
== CPP_COMMA
)
17039 /* Consume the `,'. */
17040 cp_lexer_consume_token (parser
->lexer
);
17041 /* Expect an ellipsis. */
17043 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
17045 /* It might also be `...' if the optional trailing `,' was
17047 else if (token
->type
== CPP_ELLIPSIS
)
17049 /* Consume the `...' token. */
17050 cp_lexer_consume_token (parser
->lexer
);
17051 /* And remember that we saw it. */
17055 ellipsis_p
= false;
17057 /* Finish the parameter list. */
17059 parameters
= chainon (parameters
, void_list_node
);
17064 /* Parse a parameter-declaration-list.
17066 parameter-declaration-list:
17067 parameter-declaration
17068 parameter-declaration-list , parameter-declaration
17070 Returns a representation of the parameter-declaration-list, as for
17071 cp_parser_parameter_declaration_clause. However, the
17072 `void_list_node' is never appended to the list. Upon return,
17073 *IS_ERROR will be true iff an error occurred. */
17076 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
17078 tree parameters
= NULL_TREE
;
17079 tree
*tail
= ¶meters
;
17080 bool saved_in_unbraced_linkage_specification_p
;
17083 /* Assume all will go well. */
17085 /* The special considerations that apply to a function within an
17086 unbraced linkage specifications do not apply to the parameters
17087 to the function. */
17088 saved_in_unbraced_linkage_specification_p
17089 = parser
->in_unbraced_linkage_specification_p
;
17090 parser
->in_unbraced_linkage_specification_p
= false;
17092 /* Look for more parameters. */
17095 cp_parameter_declarator
*parameter
;
17096 tree decl
= error_mark_node
;
17097 bool parenthesized_p
= false;
17098 /* Parse the parameter. */
17100 = cp_parser_parameter_declaration (parser
,
17101 /*template_parm_p=*/false,
17104 /* We don't know yet if the enclosing context is deprecated, so wait
17105 and warn in grokparms if appropriate. */
17106 deprecated_state
= DEPRECATED_SUPPRESS
;
17109 decl
= grokdeclarator (parameter
->declarator
,
17110 ¶meter
->decl_specifiers
,
17112 parameter
->default_argument
!= NULL_TREE
,
17113 ¶meter
->decl_specifiers
.attributes
);
17115 deprecated_state
= DEPRECATED_NORMAL
;
17117 /* If a parse error occurred parsing the parameter declaration,
17118 then the entire parameter-declaration-list is erroneous. */
17119 if (decl
== error_mark_node
)
17122 parameters
= error_mark_node
;
17126 if (parameter
->decl_specifiers
.attributes
)
17127 cplus_decl_attributes (&decl
,
17128 parameter
->decl_specifiers
.attributes
,
17130 if (DECL_NAME (decl
))
17131 decl
= pushdecl (decl
);
17133 if (decl
!= error_mark_node
)
17135 retrofit_lang_decl (decl
);
17136 DECL_PARM_INDEX (decl
) = ++index
;
17137 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
17140 /* Add the new parameter to the list. */
17141 *tail
= build_tree_list (parameter
->default_argument
, decl
);
17142 tail
= &TREE_CHAIN (*tail
);
17144 /* Peek at the next token. */
17145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
17146 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
17147 /* These are for Objective-C++ */
17148 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17149 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17150 /* The parameter-declaration-list is complete. */
17152 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17156 /* Peek at the next token. */
17157 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17158 /* If it's an ellipsis, then the list is complete. */
17159 if (token
->type
== CPP_ELLIPSIS
)
17161 /* Otherwise, there must be more parameters. Consume the
17163 cp_lexer_consume_token (parser
->lexer
);
17164 /* When parsing something like:
17166 int i(float f, double d)
17168 we can tell after seeing the declaration for "f" that we
17169 are not looking at an initialization of a variable "i",
17170 but rather at the declaration of a function "i".
17172 Due to the fact that the parsing of template arguments
17173 (as specified to a template-id) requires backtracking we
17174 cannot use this technique when inside a template argument
17176 if (!parser
->in_template_argument_list_p
17177 && !parser
->in_type_id_in_expr_p
17178 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
17179 /* However, a parameter-declaration of the form
17180 "foat(f)" (which is a valid declaration of a
17181 parameter "f") can also be interpreted as an
17182 expression (the conversion of "f" to "float"). */
17183 && !parenthesized_p
)
17184 cp_parser_commit_to_tentative_parse (parser
);
17188 cp_parser_error (parser
, "expected %<,%> or %<...%>");
17189 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
17190 cp_parser_skip_to_closing_parenthesis (parser
,
17191 /*recovering=*/true,
17192 /*or_comma=*/false,
17193 /*consume_paren=*/false);
17198 parser
->in_unbraced_linkage_specification_p
17199 = saved_in_unbraced_linkage_specification_p
;
17204 /* Parse a parameter declaration.
17206 parameter-declaration:
17207 decl-specifier-seq ... [opt] declarator
17208 decl-specifier-seq declarator = assignment-expression
17209 decl-specifier-seq ... [opt] abstract-declarator [opt]
17210 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17212 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17213 declares a template parameter. (In that case, a non-nested `>'
17214 token encountered during the parsing of the assignment-expression
17215 is not interpreted as a greater-than operator.)
17217 Returns a representation of the parameter, or NULL if an error
17218 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17219 true iff the declarator is of the form "(p)". */
17221 static cp_parameter_declarator
*
17222 cp_parser_parameter_declaration (cp_parser
*parser
,
17223 bool template_parm_p
,
17224 bool *parenthesized_p
)
17226 int declares_class_or_enum
;
17227 cp_decl_specifier_seq decl_specifiers
;
17228 cp_declarator
*declarator
;
17229 tree default_argument
;
17230 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
17231 const char *saved_message
;
17233 /* In a template parameter, `>' is not an operator.
17237 When parsing a default template-argument for a non-type
17238 template-parameter, the first non-nested `>' is taken as the end
17239 of the template parameter-list rather than a greater-than
17242 /* Type definitions may not appear in parameter types. */
17243 saved_message
= parser
->type_definition_forbidden_message
;
17244 parser
->type_definition_forbidden_message
17245 = G_("types may not be defined in parameter types");
17247 /* Parse the declaration-specifiers. */
17248 cp_parser_decl_specifier_seq (parser
,
17249 CP_PARSER_FLAGS_NONE
,
17251 &declares_class_or_enum
);
17253 /* Complain about missing 'typename' or other invalid type names. */
17254 if (!decl_specifiers
.any_type_specifiers_p
)
17255 cp_parser_parse_and_diagnose_invalid_type_name (parser
);
17257 /* If an error occurred, there's no reason to attempt to parse the
17258 rest of the declaration. */
17259 if (cp_parser_error_occurred (parser
))
17261 parser
->type_definition_forbidden_message
= saved_message
;
17265 /* Peek at the next token. */
17266 token
= cp_lexer_peek_token (parser
->lexer
);
17268 /* If the next token is a `)', `,', `=', `>', or `...', then there
17269 is no declarator. However, when variadic templates are enabled,
17270 there may be a declarator following `...'. */
17271 if (token
->type
== CPP_CLOSE_PAREN
17272 || token
->type
== CPP_COMMA
17273 || token
->type
== CPP_EQ
17274 || token
->type
== CPP_GREATER
)
17277 if (parenthesized_p
)
17278 *parenthesized_p
= false;
17280 /* Otherwise, there should be a declarator. */
17283 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17284 parser
->default_arg_ok_p
= false;
17286 /* After seeing a decl-specifier-seq, if the next token is not a
17287 "(", there is no possibility that the code is a valid
17288 expression. Therefore, if parsing tentatively, we commit at
17290 if (!parser
->in_template_argument_list_p
17291 /* In an expression context, having seen:
17295 we cannot be sure whether we are looking at a
17296 function-type (taking a "char" as a parameter) or a cast
17297 of some object of type "char" to "int". */
17298 && !parser
->in_type_id_in_expr_p
17299 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
17300 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
17301 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
17302 cp_parser_commit_to_tentative_parse (parser
);
17303 /* Parse the declarator. */
17304 declarator_token_start
= token
;
17305 declarator
= cp_parser_declarator (parser
,
17306 CP_PARSER_DECLARATOR_EITHER
,
17307 /*ctor_dtor_or_conv_p=*/NULL
,
17309 /*member_p=*/false);
17310 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17311 /* After the declarator, allow more attributes. */
17312 decl_specifiers
.attributes
17313 = chainon (decl_specifiers
.attributes
,
17314 cp_parser_attributes_opt (parser
));
17317 /* If the next token is an ellipsis, and we have not seen a
17318 declarator name, and the type of the declarator contains parameter
17319 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17320 a parameter pack expansion expression. Otherwise, leave the
17321 ellipsis for a C-style variadic function. */
17322 token
= cp_lexer_peek_token (parser
->lexer
);
17323 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17325 tree type
= decl_specifiers
.type
;
17327 if (type
&& DECL_P (type
))
17328 type
= TREE_TYPE (type
);
17331 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
17332 && declarator_can_be_parameter_pack (declarator
)
17333 && (!declarator
|| !declarator
->parameter_pack_p
)
17334 && uses_parameter_packs (type
))
17336 /* Consume the `...'. */
17337 cp_lexer_consume_token (parser
->lexer
);
17338 maybe_warn_variadic_templates ();
17340 /* Build a pack expansion type */
17342 declarator
->parameter_pack_p
= true;
17344 decl_specifiers
.type
= make_pack_expansion (type
);
17348 /* The restriction on defining new types applies only to the type
17349 of the parameter, not to the default argument. */
17350 parser
->type_definition_forbidden_message
= saved_message
;
17352 /* If the next token is `=', then process a default argument. */
17353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17355 token
= cp_lexer_peek_token (parser
->lexer
);
17356 /* If we are defining a class, then the tokens that make up the
17357 default argument must be saved and processed later. */
17358 if (!template_parm_p
&& at_class_scope_p ()
17359 && TYPE_BEING_DEFINED (current_class_type
)
17360 && !LAMBDA_TYPE_P (current_class_type
))
17361 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
17362 /* Outside of a class definition, we can just parse the
17363 assignment-expression. */
17366 = cp_parser_default_argument (parser
, template_parm_p
);
17368 if (!parser
->default_arg_ok_p
)
17370 if (flag_permissive
)
17371 warning (0, "deprecated use of default argument for parameter of non-function");
17374 error_at (token
->location
,
17375 "default arguments are only "
17376 "permitted for function parameters");
17377 default_argument
= NULL_TREE
;
17380 else if ((declarator
&& declarator
->parameter_pack_p
)
17381 || (decl_specifiers
.type
17382 && PACK_EXPANSION_P (decl_specifiers
.type
)))
17384 /* Find the name of the parameter pack. */
17385 cp_declarator
*id_declarator
= declarator
;
17386 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
17387 id_declarator
= id_declarator
->declarator
;
17389 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
17390 error_at (declarator_token_start
->location
,
17392 ? G_("template parameter pack %qD "
17393 "cannot have a default argument")
17394 : G_("parameter pack %qD cannot have "
17395 "a default argument"),
17396 id_declarator
->u
.id
.unqualified_name
);
17398 error_at (declarator_token_start
->location
,
17400 ? G_("template parameter pack cannot have "
17401 "a default argument")
17402 : G_("parameter pack cannot have a "
17403 "default argument"));
17405 default_argument
= NULL_TREE
;
17409 default_argument
= NULL_TREE
;
17411 return make_parameter_declarator (&decl_specifiers
,
17416 /* Parse a default argument and return it.
17418 TEMPLATE_PARM_P is true if this is a default argument for a
17419 non-type template parameter. */
17421 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
17423 tree default_argument
= NULL_TREE
;
17424 bool saved_greater_than_is_operator_p
;
17425 bool saved_local_variables_forbidden_p
;
17426 bool non_constant_p
, is_direct_init
;
17428 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17430 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
17431 parser
->greater_than_is_operator_p
= !template_parm_p
;
17432 /* Local variable names (and the `this' keyword) may not
17433 appear in a default argument. */
17434 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
17435 parser
->local_variables_forbidden_p
= true;
17436 /* Parse the assignment-expression. */
17437 if (template_parm_p
)
17438 push_deferring_access_checks (dk_no_deferred
);
17440 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
17441 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
17442 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
17443 if (template_parm_p
)
17444 pop_deferring_access_checks ();
17445 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
17446 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
17448 return default_argument
;
17451 /* Parse a function-body.
17454 compound_statement */
17457 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
17459 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
17462 /* Parse a ctor-initializer-opt followed by a function-body. Return
17463 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17464 is true we are parsing a function-try-block. */
17467 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
17468 bool in_function_try_block
)
17471 bool ctor_initializer_p
;
17472 const bool check_body_p
=
17473 DECL_CONSTRUCTOR_P (current_function_decl
)
17474 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
17477 /* Begin the function body. */
17478 body
= begin_function_body ();
17479 /* Parse the optional ctor-initializer. */
17480 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
17482 /* If we're parsing a constexpr constructor definition, we need
17483 to check that the constructor body is indeed empty. However,
17484 before we get to cp_parser_function_body lot of junk has been
17485 generated, so we can't just check that we have an empty block.
17486 Rather we take a snapshot of the outermost block, and check whether
17487 cp_parser_function_body changed its state. */
17490 list
= cur_stmt_list
;
17491 if (STATEMENT_LIST_TAIL (list
))
17492 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
17494 /* Parse the function-body. */
17495 cp_parser_function_body (parser
, in_function_try_block
);
17497 check_constexpr_ctor_body (last
, list
);
17498 /* Finish the function body. */
17499 finish_function_body (body
);
17501 return ctor_initializer_p
;
17504 /* Parse an initializer.
17507 = initializer-clause
17508 ( expression-list )
17510 Returns an expression representing the initializer. If no
17511 initializer is present, NULL_TREE is returned.
17513 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17514 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17515 set to TRUE if there is no initializer present. If there is an
17516 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17517 is set to true; otherwise it is set to false. */
17520 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
17521 bool* non_constant_p
)
17526 /* Peek at the next token. */
17527 token
= cp_lexer_peek_token (parser
->lexer
);
17529 /* Let our caller know whether or not this initializer was
17531 *is_direct_init
= (token
->type
!= CPP_EQ
);
17532 /* Assume that the initializer is constant. */
17533 *non_constant_p
= false;
17535 if (token
->type
== CPP_EQ
)
17537 /* Consume the `='. */
17538 cp_lexer_consume_token (parser
->lexer
);
17539 /* Parse the initializer-clause. */
17540 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
17542 else if (token
->type
== CPP_OPEN_PAREN
)
17545 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
17547 /*allow_expansion_p=*/true,
17550 return error_mark_node
;
17551 init
= build_tree_list_vec (vec
);
17552 release_tree_vector (vec
);
17554 else if (token
->type
== CPP_OPEN_BRACE
)
17556 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
17557 init
= cp_parser_braced_list (parser
, non_constant_p
);
17558 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
17562 /* Anything else is an error. */
17563 cp_parser_error (parser
, "expected initializer");
17564 init
= error_mark_node
;
17570 /* Parse an initializer-clause.
17572 initializer-clause:
17573 assignment-expression
17576 Returns an expression representing the initializer.
17578 If the `assignment-expression' production is used the value
17579 returned is simply a representation for the expression.
17581 Otherwise, calls cp_parser_braced_list. */
17584 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
17588 /* Assume the expression is constant. */
17589 *non_constant_p
= false;
17591 /* If it is not a `{', then we are looking at an
17592 assignment-expression. */
17593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
17596 = cp_parser_constant_expression (parser
,
17597 /*allow_non_constant_p=*/true,
17601 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
17603 return initializer
;
17606 /* Parse a brace-enclosed initializer list.
17609 { initializer-list , [opt] }
17612 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17613 the elements of the initializer-list (or NULL, if the last
17614 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17615 NULL_TREE. There is no way to detect whether or not the optional
17616 trailing `,' was provided. NON_CONSTANT_P is as for
17617 cp_parser_initializer. */
17620 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
17624 /* Consume the `{' token. */
17625 cp_lexer_consume_token (parser
->lexer
);
17626 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17627 initializer
= make_node (CONSTRUCTOR
);
17628 /* If it's not a `}', then there is a non-trivial initializer. */
17629 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
17631 /* Parse the initializer list. */
17632 CONSTRUCTOR_ELTS (initializer
)
17633 = cp_parser_initializer_list (parser
, non_constant_p
);
17634 /* A trailing `,' token is allowed. */
17635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
17636 cp_lexer_consume_token (parser
->lexer
);
17638 /* Now, there should be a trailing `}'. */
17639 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17640 TREE_TYPE (initializer
) = init_list_type_node
;
17641 return initializer
;
17644 /* Parse an initializer-list.
17647 initializer-clause ... [opt]
17648 initializer-list , initializer-clause ... [opt]
17653 designation initializer-clause ...[opt]
17654 initializer-list , designation initializer-clause ...[opt]
17659 [ constant-expression ] =
17661 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17662 for the initializer. If the INDEX of the elt is non-NULL, it is the
17663 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17664 as for cp_parser_initializer. */
17666 static VEC(constructor_elt
,gc
) *
17667 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
17669 VEC(constructor_elt
,gc
) *v
= NULL
;
17671 /* Assume all of the expressions are constant. */
17672 *non_constant_p
= false;
17674 /* Parse the rest of the list. */
17680 bool clause_non_constant_p
;
17682 /* If the next token is an identifier and the following one is a
17683 colon, we are looking at the GNU designated-initializer
17685 if (cp_parser_allow_gnu_extensions_p (parser
)
17686 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
17687 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
17689 /* Warn the user that they are using an extension. */
17690 pedwarn (input_location
, OPT_Wpedantic
,
17691 "ISO C++ does not allow designated initializers");
17692 /* Consume the identifier. */
17693 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17694 /* Consume the `:'. */
17695 cp_lexer_consume_token (parser
->lexer
);
17697 /* Also handle the C99 syntax, '. id ='. */
17698 else if (cp_parser_allow_gnu_extensions_p (parser
)
17699 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
17700 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
17701 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
17703 /* Warn the user that they are using an extension. */
17704 pedwarn (input_location
, OPT_Wpedantic
,
17705 "ISO C++ does not allow C99 designated initializers");
17706 /* Consume the `.'. */
17707 cp_lexer_consume_token (parser
->lexer
);
17708 /* Consume the identifier. */
17709 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17710 /* Consume the `='. */
17711 cp_lexer_consume_token (parser
->lexer
);
17713 /* Also handle C99 array designators, '[ const ] ='. */
17714 else if (cp_parser_allow_gnu_extensions_p (parser
)
17715 && !c_dialect_objc ()
17716 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
17718 /* In C++11, [ could start a lambda-introducer. */
17719 cp_parser_parse_tentatively (parser
);
17720 cp_lexer_consume_token (parser
->lexer
);
17721 designator
= cp_parser_constant_expression (parser
, false, NULL
);
17722 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
17723 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
17724 if (!cp_parser_parse_definitely (parser
))
17725 designator
= NULL_TREE
;
17728 designator
= NULL_TREE
;
17730 /* Parse the initializer. */
17731 initializer
= cp_parser_initializer_clause (parser
,
17732 &clause_non_constant_p
);
17733 /* If any clause is non-constant, so is the entire initializer. */
17734 if (clause_non_constant_p
)
17735 *non_constant_p
= true;
17737 /* If we have an ellipsis, this is an initializer pack
17739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17741 /* Consume the `...'. */
17742 cp_lexer_consume_token (parser
->lexer
);
17744 /* Turn the initializer into an initializer expansion. */
17745 initializer
= make_pack_expansion (initializer
);
17748 /* Add it to the vector. */
17749 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
17751 /* If the next token is not a comma, we have reached the end of
17753 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17756 /* Peek at the next token. */
17757 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17758 /* If the next token is a `}', then we're still done. An
17759 initializer-clause can have a trailing `,' after the
17760 initializer-list and before the closing `}'. */
17761 if (token
->type
== CPP_CLOSE_BRACE
)
17764 /* Consume the `,' token. */
17765 cp_lexer_consume_token (parser
->lexer
);
17771 /* Classes [gram.class] */
17773 /* Parse a class-name.
17779 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17780 to indicate that names looked up in dependent types should be
17781 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17782 keyword has been used to indicate that the name that appears next
17783 is a template. TAG_TYPE indicates the explicit tag given before
17784 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17785 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17786 is the class being defined in a class-head.
17788 Returns the TYPE_DECL representing the class. */
17791 cp_parser_class_name (cp_parser
*parser
,
17792 bool typename_keyword_p
,
17793 bool template_keyword_p
,
17794 enum tag_types tag_type
,
17795 bool check_dependency_p
,
17797 bool is_declaration
)
17803 tree identifier
= NULL_TREE
;
17805 /* All class-names start with an identifier. */
17806 token
= cp_lexer_peek_token (parser
->lexer
);
17807 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
17809 cp_parser_error (parser
, "expected class-name");
17810 return error_mark_node
;
17813 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17814 to a template-id, so we save it here. */
17815 scope
= parser
->scope
;
17816 if (scope
== error_mark_node
)
17817 return error_mark_node
;
17819 /* Any name names a type if we're following the `typename' keyword
17820 in a qualified name where the enclosing scope is type-dependent. */
17821 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
17822 && dependent_type_p (scope
));
17823 /* Handle the common case (an identifier, but not a template-id)
17825 if (token
->type
== CPP_NAME
17826 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
17828 cp_token
*identifier_token
;
17831 /* Look for the identifier. */
17832 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
17833 ambiguous_p
= identifier_token
->ambiguous_p
;
17834 identifier
= cp_parser_identifier (parser
);
17835 /* If the next token isn't an identifier, we are certainly not
17836 looking at a class-name. */
17837 if (identifier
== error_mark_node
)
17838 decl
= error_mark_node
;
17839 /* If we know this is a type-name, there's no need to look it
17841 else if (typename_p
)
17845 tree ambiguous_decls
;
17846 /* If we already know that this lookup is ambiguous, then
17847 we've already issued an error message; there's no reason
17851 cp_parser_simulate_error (parser
);
17852 return error_mark_node
;
17854 /* If the next token is a `::', then the name must be a type
17857 [basic.lookup.qual]
17859 During the lookup for a name preceding the :: scope
17860 resolution operator, object, function, and enumerator
17861 names are ignored. */
17862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17863 tag_type
= typename_type
;
17864 /* Look up the name. */
17865 decl
= cp_parser_lookup_name (parser
, identifier
,
17867 /*is_template=*/false,
17868 /*is_namespace=*/false,
17869 check_dependency_p
,
17871 identifier_token
->location
);
17872 if (ambiguous_decls
)
17874 if (cp_parser_parsing_tentatively (parser
))
17875 cp_parser_simulate_error (parser
);
17876 return error_mark_node
;
17882 /* Try a template-id. */
17883 decl
= cp_parser_template_id (parser
, template_keyword_p
,
17884 check_dependency_p
,
17887 if (decl
== error_mark_node
)
17888 return error_mark_node
;
17891 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
17893 /* If this is a typename, create a TYPENAME_TYPE. */
17894 if (typename_p
&& decl
!= error_mark_node
)
17896 decl
= make_typename_type (scope
, decl
, typename_type
,
17897 /*complain=*/tf_error
);
17898 if (decl
!= error_mark_node
)
17899 decl
= TYPE_NAME (decl
);
17902 decl
= strip_using_decl (decl
);
17904 /* Check to see that it is really the name of a class. */
17905 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17906 && TREE_CODE (TREE_OPERAND (decl
, 0)) == IDENTIFIER_NODE
17907 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17908 /* Situations like this:
17910 template <typename T> struct A {
17911 typename T::template X<int>::I i;
17914 are problematic. Is `T::template X<int>' a class-name? The
17915 standard does not seem to be definitive, but there is no other
17916 valid interpretation of the following `::'. Therefore, those
17917 names are considered class-names. */
17919 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
17920 if (decl
!= error_mark_node
)
17921 decl
= TYPE_NAME (decl
);
17923 else if (TREE_CODE (decl
) != TYPE_DECL
17924 || TREE_TYPE (decl
) == error_mark_node
17925 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
17926 /* In Objective-C 2.0, a classname followed by '.' starts a
17927 dot-syntax expression, and it's not a type-name. */
17928 || (c_dialect_objc ()
17929 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
17930 && objc_is_class_name (decl
)))
17931 decl
= error_mark_node
;
17933 if (decl
== error_mark_node
)
17934 cp_parser_error (parser
, "expected class-name");
17935 else if (identifier
&& !parser
->scope
)
17936 maybe_note_name_used_in_class (identifier
, decl
);
17941 /* Parse a class-specifier.
17944 class-head { member-specification [opt] }
17946 Returns the TREE_TYPE representing the class. */
17949 cp_parser_class_specifier_1 (cp_parser
* parser
)
17952 tree attributes
= NULL_TREE
;
17953 bool nested_name_specifier_p
;
17954 unsigned saved_num_template_parameter_lists
;
17955 bool saved_in_function_body
;
17956 unsigned char in_statement
;
17957 bool in_switch_statement_p
;
17958 bool saved_in_unbraced_linkage_specification_p
;
17959 tree old_scope
= NULL_TREE
;
17960 tree scope
= NULL_TREE
;
17961 cp_token
*closing_brace
;
17963 push_deferring_access_checks (dk_no_deferred
);
17965 /* Parse the class-head. */
17966 type
= cp_parser_class_head (parser
,
17967 &nested_name_specifier_p
);
17968 /* If the class-head was a semantic disaster, skip the entire body
17972 cp_parser_skip_to_end_of_block_or_statement (parser
);
17973 pop_deferring_access_checks ();
17974 return error_mark_node
;
17977 /* Look for the `{'. */
17978 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
17980 pop_deferring_access_checks ();
17981 return error_mark_node
;
17984 /* Issue an error message if type-definitions are forbidden here. */
17985 cp_parser_check_type_definition (parser
);
17986 /* Remember that we are defining one more class. */
17987 ++parser
->num_classes_being_defined
;
17988 /* Inside the class, surrounding template-parameter-lists do not
17990 saved_num_template_parameter_lists
17991 = parser
->num_template_parameter_lists
;
17992 parser
->num_template_parameter_lists
= 0;
17993 /* We are not in a function body. */
17994 saved_in_function_body
= parser
->in_function_body
;
17995 parser
->in_function_body
= false;
17996 /* Or in a loop. */
17997 in_statement
= parser
->in_statement
;
17998 parser
->in_statement
= 0;
17999 /* Or in a switch. */
18000 in_switch_statement_p
= parser
->in_switch_statement_p
;
18001 parser
->in_switch_statement_p
= false;
18002 /* We are not immediately inside an extern "lang" block. */
18003 saved_in_unbraced_linkage_specification_p
18004 = parser
->in_unbraced_linkage_specification_p
;
18005 parser
->in_unbraced_linkage_specification_p
= false;
18007 /* Start the class. */
18008 if (nested_name_specifier_p
)
18010 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
18011 old_scope
= push_inner_scope (scope
);
18013 type
= begin_class_definition (type
);
18015 if (type
== error_mark_node
)
18016 /* If the type is erroneous, skip the entire body of the class. */
18017 cp_parser_skip_to_closing_brace (parser
);
18019 /* Parse the member-specification. */
18020 cp_parser_member_specification_opt (parser
);
18022 /* Look for the trailing `}'. */
18023 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
18024 /* Look for trailing attributes to apply to this class. */
18025 if (cp_parser_allow_gnu_extensions_p (parser
))
18026 attributes
= cp_parser_attributes_opt (parser
);
18027 if (type
!= error_mark_node
)
18028 type
= finish_struct (type
, attributes
);
18029 if (nested_name_specifier_p
)
18030 pop_inner_scope (old_scope
, scope
);
18032 /* We've finished a type definition. Check for the common syntax
18033 error of forgetting a semicolon after the definition. We need to
18034 be careful, as we can't just check for not-a-semicolon and be done
18035 with it; the user might have typed:
18037 class X { } c = ...;
18038 class X { } *p = ...;
18040 and so forth. Instead, enumerate all the possible tokens that
18041 might follow this production; if we don't see one of them, then
18042 complain and silently insert the semicolon. */
18044 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18045 bool want_semicolon
= true;
18047 switch (token
->type
)
18050 case CPP_SEMICOLON
:
18053 case CPP_OPEN_PAREN
:
18054 case CPP_CLOSE_PAREN
:
18056 want_semicolon
= false;
18059 /* While it's legal for type qualifiers and storage class
18060 specifiers to follow type definitions in the grammar, only
18061 compiler testsuites contain code like that. Assume that if
18062 we see such code, then what we're really seeing is a case
18066 const <type> var = ...;
18071 static <type> func (...) ...
18073 i.e. the qualifier or specifier applies to the next
18074 declaration. To do so, however, we need to look ahead one
18075 more token to see if *that* token is a type specifier.
18077 This code could be improved to handle:
18080 static const <type> var = ...; */
18082 if (keyword_is_decl_specifier (token
->keyword
))
18084 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18086 /* Handling user-defined types here would be nice, but very
18089 = (lookahead
->type
== CPP_KEYWORD
18090 && keyword_begins_type_specifier (lookahead
->keyword
));
18097 /* If we don't have a type, then something is very wrong and we
18098 shouldn't try to do anything clever. Likewise for not seeing the
18100 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
18102 cp_token_position prev
18103 = cp_lexer_previous_token_position (parser
->lexer
);
18104 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
18105 location_t loc
= prev_token
->location
;
18107 if (CLASSTYPE_DECLARED_CLASS (type
))
18108 error_at (loc
, "expected %<;%> after class definition");
18109 else if (TREE_CODE (type
) == RECORD_TYPE
)
18110 error_at (loc
, "expected %<;%> after struct definition");
18111 else if (TREE_CODE (type
) == UNION_TYPE
)
18112 error_at (loc
, "expected %<;%> after union definition");
18114 gcc_unreachable ();
18116 /* Unget one token and smash it to look as though we encountered
18117 a semicolon in the input stream. */
18118 cp_lexer_set_token_position (parser
->lexer
, prev
);
18119 token
= cp_lexer_peek_token (parser
->lexer
);
18120 token
->type
= CPP_SEMICOLON
;
18121 token
->keyword
= RID_MAX
;
18125 /* If this class is not itself within the scope of another class,
18126 then we need to parse the bodies of all of the queued function
18127 definitions. Note that the queued functions defined in a class
18128 are not always processed immediately following the
18129 class-specifier for that class. Consider:
18132 struct B { void f() { sizeof (A); } };
18135 If `f' were processed before the processing of `A' were
18136 completed, there would be no way to compute the size of `A'.
18137 Note that the nesting we are interested in here is lexical --
18138 not the semantic nesting given by TYPE_CONTEXT. In particular,
18141 struct A { struct B; };
18142 struct A::B { void f() { } };
18144 there is no need to delay the parsing of `A::B::f'. */
18145 if (--parser
->num_classes_being_defined
== 0)
18148 tree class_type
= NULL_TREE
;
18149 tree pushed_scope
= NULL_TREE
;
18151 cp_default_arg_entry
*e
;
18152 tree save_ccp
, save_ccr
;
18154 /* In a first pass, parse default arguments to the functions.
18155 Then, in a second pass, parse the bodies of the functions.
18156 This two-phased approach handles cases like:
18164 FOR_EACH_VEC_ELT (cp_default_arg_entry
, unparsed_funs_with_default_args
,
18168 /* If there are default arguments that have not yet been processed,
18169 take care of them now. */
18170 if (class_type
!= e
->class_type
)
18173 pop_scope (pushed_scope
);
18174 class_type
= e
->class_type
;
18175 pushed_scope
= push_scope (class_type
);
18177 /* Make sure that any template parameters are in scope. */
18178 maybe_begin_member_template_processing (decl
);
18179 /* Parse the default argument expressions. */
18180 cp_parser_late_parsing_default_args (parser
, decl
);
18181 /* Remove any template parameters from the symbol table. */
18182 maybe_end_member_template_processing ();
18184 VEC_truncate (cp_default_arg_entry
, unparsed_funs_with_default_args
, 0);
18185 /* Now parse any NSDMIs. */
18186 save_ccp
= current_class_ptr
;
18187 save_ccr
= current_class_ref
;
18188 FOR_EACH_VEC_ELT (tree
, unparsed_nsdmis
, ix
, decl
)
18190 if (class_type
!= DECL_CONTEXT (decl
))
18193 pop_scope (pushed_scope
);
18194 class_type
= DECL_CONTEXT (decl
);
18195 pushed_scope
= push_scope (class_type
);
18197 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
18198 cp_parser_late_parsing_nsdmi (parser
, decl
);
18200 VEC_truncate (tree
, unparsed_nsdmis
, 0);
18201 current_class_ptr
= save_ccp
;
18202 current_class_ref
= save_ccr
;
18204 pop_scope (pushed_scope
);
18205 /* Now parse the body of the functions. */
18206 FOR_EACH_VEC_ELT (tree
, unparsed_funs_with_definitions
, ix
, decl
)
18207 cp_parser_late_parsing_for_member (parser
, decl
);
18208 VEC_truncate (tree
, unparsed_funs_with_definitions
, 0);
18211 /* Put back any saved access checks. */
18212 pop_deferring_access_checks ();
18214 /* Restore saved state. */
18215 parser
->in_switch_statement_p
= in_switch_statement_p
;
18216 parser
->in_statement
= in_statement
;
18217 parser
->in_function_body
= saved_in_function_body
;
18218 parser
->num_template_parameter_lists
18219 = saved_num_template_parameter_lists
;
18220 parser
->in_unbraced_linkage_specification_p
18221 = saved_in_unbraced_linkage_specification_p
;
18227 cp_parser_class_specifier (cp_parser
* parser
)
18230 timevar_push (TV_PARSE_STRUCT
);
18231 ret
= cp_parser_class_specifier_1 (parser
);
18232 timevar_pop (TV_PARSE_STRUCT
);
18236 /* Parse a class-head.
18239 class-key identifier [opt] base-clause [opt]
18240 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18241 class-key nested-name-specifier [opt] template-id
18244 class-virt-specifier:
18248 class-key attributes identifier [opt] base-clause [opt]
18249 class-key attributes nested-name-specifier identifier base-clause [opt]
18250 class-key attributes nested-name-specifier [opt] template-id
18253 Upon return BASES is initialized to the list of base classes (or
18254 NULL, if there are none) in the same form returned by
18255 cp_parser_base_clause.
18257 Returns the TYPE of the indicated class. Sets
18258 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18259 involving a nested-name-specifier was used, and FALSE otherwise.
18261 Returns error_mark_node if this is not a class-head.
18263 Returns NULL_TREE if the class-head is syntactically valid, but
18264 semantically invalid in a way that means we should skip the entire
18265 body of the class. */
18268 cp_parser_class_head (cp_parser
* parser
,
18269 bool* nested_name_specifier_p
)
18271 tree nested_name_specifier
;
18272 enum tag_types class_key
;
18273 tree id
= NULL_TREE
;
18274 tree type
= NULL_TREE
;
18277 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18278 bool template_id_p
= false;
18279 bool qualified_p
= false;
18280 bool invalid_nested_name_p
= false;
18281 bool invalid_explicit_specialization_p
= false;
18282 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18283 tree pushed_scope
= NULL_TREE
;
18284 unsigned num_templates
;
18285 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
18286 /* Assume no nested-name-specifier will be present. */
18287 *nested_name_specifier_p
= false;
18288 /* Assume no template parameter lists will be used in defining the
18291 parser
->colon_corrects_to_scope_p
= false;
18293 /* Look for the class-key. */
18294 class_key
= cp_parser_class_key (parser
);
18295 if (class_key
== none_type
)
18296 return error_mark_node
;
18298 /* Parse the attributes. */
18299 attributes
= cp_parser_attributes_opt (parser
);
18301 /* If the next token is `::', that is invalid -- but sometimes
18302 people do try to write:
18306 Handle this gracefully by accepting the extra qualifier, and then
18307 issuing an error about it later if this really is a
18308 class-head. If it turns out just to be an elaborated type
18309 specifier, remain silent. */
18310 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
18311 qualified_p
= true;
18313 push_deferring_access_checks (dk_no_check
);
18315 /* Determine the name of the class. Begin by looking for an
18316 optional nested-name-specifier. */
18317 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
18318 nested_name_specifier
18319 = cp_parser_nested_name_specifier_opt (parser
,
18320 /*typename_keyword_p=*/false,
18321 /*check_dependency_p=*/false,
18323 /*is_declaration=*/false);
18324 /* If there was a nested-name-specifier, then there *must* be an
18326 if (nested_name_specifier
)
18328 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18329 /* Although the grammar says `identifier', it really means
18330 `class-name' or `template-name'. You are only allowed to
18331 define a class that has already been declared with this
18334 The proposed resolution for Core Issue 180 says that wherever
18335 you see `class T::X' you should treat `X' as a type-name.
18337 It is OK to define an inaccessible class; for example:
18339 class A { class B; };
18342 We do not know if we will see a class-name, or a
18343 template-name. We look for a class-name first, in case the
18344 class-name is a template-id; if we looked for the
18345 template-name first we would stop after the template-name. */
18346 cp_parser_parse_tentatively (parser
);
18347 type
= cp_parser_class_name (parser
,
18348 /*typename_keyword_p=*/false,
18349 /*template_keyword_p=*/false,
18351 /*check_dependency_p=*/false,
18352 /*class_head_p=*/true,
18353 /*is_declaration=*/false);
18354 /* If that didn't work, ignore the nested-name-specifier. */
18355 if (!cp_parser_parse_definitely (parser
))
18357 invalid_nested_name_p
= true;
18358 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18359 id
= cp_parser_identifier (parser
);
18360 if (id
== error_mark_node
)
18363 /* If we could not find a corresponding TYPE, treat this
18364 declaration like an unqualified declaration. */
18365 if (type
== error_mark_node
)
18366 nested_name_specifier
= NULL_TREE
;
18367 /* Otherwise, count the number of templates used in TYPE and its
18368 containing scopes. */
18373 for (scope
= TREE_TYPE (type
);
18374 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
18375 scope
= (TYPE_P (scope
)
18376 ? TYPE_CONTEXT (scope
)
18377 : DECL_CONTEXT (scope
)))
18379 && CLASS_TYPE_P (scope
)
18380 && CLASSTYPE_TEMPLATE_INFO (scope
)
18381 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
18382 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
))
18386 /* Otherwise, the identifier is optional. */
18389 /* We don't know whether what comes next is a template-id,
18390 an identifier, or nothing at all. */
18391 cp_parser_parse_tentatively (parser
);
18392 /* Check for a template-id. */
18393 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18394 id
= cp_parser_template_id (parser
,
18395 /*template_keyword_p=*/false,
18396 /*check_dependency_p=*/true,
18398 /*is_declaration=*/true);
18399 /* If that didn't work, it could still be an identifier. */
18400 if (!cp_parser_parse_definitely (parser
))
18402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18404 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18405 id
= cp_parser_identifier (parser
);
18412 template_id_p
= true;
18417 pop_deferring_access_checks ();
18421 cp_parser_check_for_invalid_template_id (parser
, id
,
18423 type_start_token
->location
);
18425 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
18427 /* If it's not a `:' or a `{' then we can't really be looking at a
18428 class-head, since a class-head only appears as part of a
18429 class-specifier. We have to detect this situation before calling
18430 xref_tag, since that has irreversible side-effects. */
18431 if (!cp_parser_next_token_starts_class_definition_p (parser
))
18433 cp_parser_error (parser
, "expected %<{%> or %<:%>");
18434 type
= error_mark_node
;
18438 /* At this point, we're going ahead with the class-specifier, even
18439 if some other problem occurs. */
18440 cp_parser_commit_to_tentative_parse (parser
);
18441 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
18443 cp_parser_error (parser
,
18444 "cannot specify %<override%> for a class");
18445 type
= error_mark_node
;
18448 /* Issue the error about the overly-qualified name now. */
18451 cp_parser_error (parser
,
18452 "global qualification of class name is invalid");
18453 type
= error_mark_node
;
18456 else if (invalid_nested_name_p
)
18458 cp_parser_error (parser
,
18459 "qualified name does not name a class");
18460 type
= error_mark_node
;
18463 else if (nested_name_specifier
)
18467 /* Reject typedef-names in class heads. */
18468 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
18470 error_at (type_start_token
->location
,
18471 "invalid class name in declaration of %qD",
18477 /* Figure out in what scope the declaration is being placed. */
18478 scope
= current_scope ();
18479 /* If that scope does not contain the scope in which the
18480 class was originally declared, the program is invalid. */
18481 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
18483 if (at_namespace_scope_p ())
18484 error_at (type_start_token
->location
,
18485 "declaration of %qD in namespace %qD which does not "
18487 type
, scope
, nested_name_specifier
);
18489 error_at (type_start_token
->location
,
18490 "declaration of %qD in %qD which does not enclose %qD",
18491 type
, scope
, nested_name_specifier
);
18497 A declarator-id shall not be qualified except for the
18498 definition of a ... nested class outside of its class
18499 ... [or] the definition or explicit instantiation of a
18500 class member of a namespace outside of its namespace. */
18501 if (scope
== nested_name_specifier
)
18503 permerror (nested_name_specifier_token_start
->location
,
18504 "extra qualification not allowed");
18505 nested_name_specifier
= NULL_TREE
;
18509 /* An explicit-specialization must be preceded by "template <>". If
18510 it is not, try to recover gracefully. */
18511 if (at_namespace_scope_p ()
18512 && parser
->num_template_parameter_lists
== 0
18515 error_at (type_start_token
->location
,
18516 "an explicit specialization must be preceded by %<template <>%>");
18517 invalid_explicit_specialization_p
= true;
18518 /* Take the same action that would have been taken by
18519 cp_parser_explicit_specialization. */
18520 ++parser
->num_template_parameter_lists
;
18521 begin_specialization ();
18523 /* There must be no "return" statements between this point and the
18524 end of this function; set "type "to the correct return value and
18525 use "goto done;" to return. */
18526 /* Make sure that the right number of template parameters were
18528 if (!cp_parser_check_template_parameters (parser
, num_templates
,
18529 type_start_token
->location
,
18530 /*declarator=*/NULL
))
18532 /* If something went wrong, there is no point in even trying to
18533 process the class-definition. */
18538 /* Look up the type. */
18541 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
18542 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
18543 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
18545 error_at (type_start_token
->location
,
18546 "function template %qD redeclared as a class template", id
);
18547 type
= error_mark_node
;
18551 type
= TREE_TYPE (id
);
18552 type
= maybe_process_partial_specialization (type
);
18554 if (nested_name_specifier
)
18555 pushed_scope
= push_scope (nested_name_specifier
);
18557 else if (nested_name_specifier
)
18563 template <typename T> struct S { struct T };
18564 template <typename T> struct S<T>::T { };
18566 we will get a TYPENAME_TYPE when processing the definition of
18567 `S::T'. We need to resolve it to the actual type before we
18568 try to define it. */
18569 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
18571 class_type
= resolve_typename_type (TREE_TYPE (type
),
18572 /*only_current_p=*/false);
18573 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
18574 type
= TYPE_NAME (class_type
);
18577 cp_parser_error (parser
, "could not resolve typename type");
18578 type
= error_mark_node
;
18582 if (maybe_process_partial_specialization (TREE_TYPE (type
))
18583 == error_mark_node
)
18589 class_type
= current_class_type
;
18590 /* Enter the scope indicated by the nested-name-specifier. */
18591 pushed_scope
= push_scope (nested_name_specifier
);
18592 /* Get the canonical version of this type. */
18593 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
18594 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18595 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
18597 type
= push_template_decl (type
);
18598 if (type
== error_mark_node
)
18605 type
= TREE_TYPE (type
);
18606 *nested_name_specifier_p
= true;
18608 else /* The name is not a nested name. */
18610 /* If the class was unnamed, create a dummy name. */
18612 id
= make_anon_name ();
18613 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
18614 parser
->num_template_parameter_lists
);
18617 /* Indicate whether this class was declared as a `class' or as a
18619 if (TREE_CODE (type
) == RECORD_TYPE
)
18620 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
18621 cp_parser_check_class_key (class_key
, type
);
18623 /* If this type was already complete, and we see another definition,
18624 that's an error. */
18625 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
18627 error_at (type_start_token
->location
, "redefinition of %q#T",
18629 error_at (type_start_token
->location
, "previous definition of %q+#T",
18634 else if (type
== error_mark_node
)
18639 /* Apply attributes now, before any use of the class as a template
18640 argument in its base list. */
18641 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
18642 fixup_attribute_variants (type
);
18645 /* We will have entered the scope containing the class; the names of
18646 base classes should be looked up in that context. For example:
18648 struct A { struct B {}; struct C; };
18649 struct A::C : B {};
18653 /* Get the list of base-classes, if there is one. */
18654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18655 bases
= cp_parser_base_clause (parser
);
18659 /* If we're really defining a class, process the base classes.
18660 If they're invalid, fail. */
18661 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
18662 && !xref_basetypes (type
, bases
))
18666 /* Leave the scope given by the nested-name-specifier. We will
18667 enter the class scope itself while processing the members. */
18669 pop_scope (pushed_scope
);
18671 if (invalid_explicit_specialization_p
)
18673 end_specialization ();
18674 --parser
->num_template_parameter_lists
;
18678 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18679 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
18680 CLASSTYPE_FINAL (type
) = 1;
18682 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18686 /* Parse a class-key.
18693 Returns the kind of class-key specified, or none_type to indicate
18696 static enum tag_types
18697 cp_parser_class_key (cp_parser
* parser
)
18700 enum tag_types tag_type
;
18702 /* Look for the class-key. */
18703 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
18707 /* Check to see if the TOKEN is a class-key. */
18708 tag_type
= cp_parser_token_is_class_key (token
);
18710 cp_parser_error (parser
, "expected class-key");
18714 /* Parse an (optional) member-specification.
18716 member-specification:
18717 member-declaration member-specification [opt]
18718 access-specifier : member-specification [opt] */
18721 cp_parser_member_specification_opt (cp_parser
* parser
)
18728 /* Peek at the next token. */
18729 token
= cp_lexer_peek_token (parser
->lexer
);
18730 /* If it's a `}', or EOF then we've seen all the members. */
18731 if (token
->type
== CPP_CLOSE_BRACE
18732 || token
->type
== CPP_EOF
18733 || token
->type
== CPP_PRAGMA_EOL
)
18736 /* See if this token is a keyword. */
18737 keyword
= token
->keyword
;
18741 case RID_PROTECTED
:
18743 /* Consume the access-specifier. */
18744 cp_lexer_consume_token (parser
->lexer
);
18745 /* Remember which access-specifier is active. */
18746 current_access_specifier
= token
->u
.value
;
18747 /* Look for the `:'. */
18748 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
18752 /* Accept #pragmas at class scope. */
18753 if (token
->type
== CPP_PRAGMA
)
18755 cp_parser_pragma (parser
, pragma_external
);
18759 /* Otherwise, the next construction must be a
18760 member-declaration. */
18761 cp_parser_member_declaration (parser
);
18766 /* Parse a member-declaration.
18768 member-declaration:
18769 decl-specifier-seq [opt] member-declarator-list [opt] ;
18770 function-definition ; [opt]
18771 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18773 template-declaration
18776 member-declarator-list:
18778 member-declarator-list , member-declarator
18781 declarator pure-specifier [opt]
18782 declarator constant-initializer [opt]
18783 identifier [opt] : constant-expression
18787 member-declaration:
18788 __extension__ member-declaration
18791 declarator attributes [opt] pure-specifier [opt]
18792 declarator attributes [opt] constant-initializer [opt]
18793 identifier [opt] attributes [opt] : constant-expression
18797 member-declaration:
18798 static_assert-declaration */
18801 cp_parser_member_declaration (cp_parser
* parser
)
18803 cp_decl_specifier_seq decl_specifiers
;
18804 tree prefix_attributes
;
18806 int declares_class_or_enum
;
18808 cp_token
*token
= NULL
;
18809 cp_token
*decl_spec_token_start
= NULL
;
18810 cp_token
*initializer_token_start
= NULL
;
18811 int saved_pedantic
;
18812 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18814 /* Check for the `__extension__' keyword. */
18815 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
18818 cp_parser_member_declaration (parser
);
18819 /* Restore the old value of the PEDANTIC flag. */
18820 pedantic
= saved_pedantic
;
18825 /* Check for a template-declaration. */
18826 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
18828 /* An explicit specialization here is an error condition, and we
18829 expect the specialization handler to detect and report this. */
18830 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
18831 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
18832 cp_parser_explicit_specialization (parser
);
18834 cp_parser_template_declaration (parser
, /*member_p=*/true);
18839 /* Check for a using-declaration. */
18840 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
18842 if (cxx_dialect
< cxx0x
)
18844 /* Parse the using-declaration. */
18845 cp_parser_using_declaration (parser
,
18846 /*access_declaration_p=*/false);
18852 cp_parser_parse_tentatively (parser
);
18853 decl
= cp_parser_alias_declaration (parser
);
18854 if (cp_parser_parse_definitely (parser
))
18855 finish_member_declaration (decl
);
18857 cp_parser_using_declaration (parser
,
18858 /*access_declaration_p=*/false);
18863 /* Check for @defs. */
18864 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
18867 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
18868 ivar
= ivar_chains
;
18872 ivar
= TREE_CHAIN (member
);
18873 TREE_CHAIN (member
) = NULL_TREE
;
18874 finish_member_declaration (member
);
18879 /* If the next token is `static_assert' we have a static assertion. */
18880 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
18882 cp_parser_static_assert (parser
, /*member_p=*/true);
18886 parser
->colon_corrects_to_scope_p
= false;
18888 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
18891 /* Parse the decl-specifier-seq. */
18892 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
18893 cp_parser_decl_specifier_seq (parser
,
18894 CP_PARSER_FLAGS_OPTIONAL
,
18896 &declares_class_or_enum
);
18897 prefix_attributes
= decl_specifiers
.attributes
;
18898 decl_specifiers
.attributes
= NULL_TREE
;
18899 /* Check for an invalid type-name. */
18900 if (!decl_specifiers
.any_type_specifiers_p
18901 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18903 /* If there is no declarator, then the decl-specifier-seq should
18905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18907 /* If there was no decl-specifier-seq, and the next token is a
18908 `;', then we have something like:
18914 Each member-declaration shall declare at least one member
18915 name of the class. */
18916 if (!decl_specifiers
.any_specifiers_p
)
18918 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18919 if (!in_system_header_at (token
->location
))
18920 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
18926 /* See if this declaration is a friend. */
18927 friend_p
= cp_parser_friend_p (&decl_specifiers
);
18928 /* If there were decl-specifiers, check to see if there was
18929 a class-declaration. */
18930 type
= check_tag_decl (&decl_specifiers
);
18931 /* Nested classes have already been added to the class, but
18932 a `friend' needs to be explicitly registered. */
18935 /* If the `friend' keyword was present, the friend must
18936 be introduced with a class-key. */
18937 if (!declares_class_or_enum
&& cxx_dialect
< cxx0x
)
18938 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
18939 "in C++03 a class-key must be used "
18940 "when declaring a friend");
18943 template <typename T> struct A {
18944 friend struct A<T>::B;
18947 A<T>::B will be represented by a TYPENAME_TYPE, and
18948 therefore not recognized by check_tag_decl. */
18951 type
= decl_specifiers
.type
;
18952 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
18953 type
= TREE_TYPE (type
);
18955 if (!type
|| !TYPE_P (type
))
18956 error_at (decl_spec_token_start
->location
,
18957 "friend declaration does not name a class or "
18960 make_friend_class (current_class_type
, type
,
18961 /*complain=*/true);
18963 /* If there is no TYPE, an error message will already have
18965 else if (!type
|| type
== error_mark_node
)
18967 /* An anonymous aggregate has to be handled specially; such
18968 a declaration really declares a data member (with a
18969 particular type), as opposed to a nested class. */
18970 else if (ANON_AGGR_TYPE_P (type
))
18973 if (decl_specifiers
.storage_class
!= sc_none
)
18974 error_at (decl_spec_token_start
->location
,
18975 "a storage class on an anonymous aggregate "
18976 "in class scope is not allowed");
18978 /* Remove constructors and such from TYPE, now that we
18979 know it is an anonymous aggregate. */
18980 fixup_anonymous_aggr (type
);
18981 /* And make the corresponding data member. */
18982 decl
= build_decl (decl_spec_token_start
->location
,
18983 FIELD_DECL
, NULL_TREE
, type
);
18984 /* Add it to the class. */
18985 finish_member_declaration (decl
);
18988 cp_parser_check_access_in_redeclaration
18990 decl_spec_token_start
->location
);
18995 bool assume_semicolon
= false;
18997 /* See if these declarations will be friends. */
18998 friend_p
= cp_parser_friend_p (&decl_specifiers
);
19000 /* Keep going until we hit the `;' at the end of the
19002 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19004 tree attributes
= NULL_TREE
;
19005 tree first_attribute
;
19007 /* Peek at the next token. */
19008 token
= cp_lexer_peek_token (parser
->lexer
);
19010 /* Check for a bitfield declaration. */
19011 if (token
->type
== CPP_COLON
19012 || (token
->type
== CPP_NAME
19013 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
19019 /* Get the name of the bitfield. Note that we cannot just
19020 check TOKEN here because it may have been invalidated by
19021 the call to cp_lexer_peek_nth_token above. */
19022 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
19023 identifier
= cp_parser_identifier (parser
);
19025 identifier
= NULL_TREE
;
19027 /* Consume the `:' token. */
19028 cp_lexer_consume_token (parser
->lexer
);
19029 /* Get the width of the bitfield. */
19031 = cp_parser_constant_expression (parser
,
19032 /*allow_non_constant=*/false,
19035 /* Look for attributes that apply to the bitfield. */
19036 attributes
= cp_parser_attributes_opt (parser
);
19037 /* Remember which attributes are prefix attributes and
19039 first_attribute
= attributes
;
19040 /* Combine the attributes. */
19041 attributes
= chainon (prefix_attributes
, attributes
);
19043 /* Create the bitfield declaration. */
19044 decl
= grokbitfield (identifier
19045 ? make_id_declarator (NULL_TREE
,
19055 cp_declarator
*declarator
;
19057 tree asm_specification
;
19058 int ctor_dtor_or_conv_p
;
19060 /* Parse the declarator. */
19062 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19063 &ctor_dtor_or_conv_p
,
19064 /*parenthesized_p=*/NULL
,
19065 /*member_p=*/true);
19067 /* If something went wrong parsing the declarator, make sure
19068 that we at least consume some tokens. */
19069 if (declarator
== cp_error_declarator
)
19071 /* Skip to the end of the statement. */
19072 cp_parser_skip_to_end_of_statement (parser
);
19073 /* If the next token is not a semicolon, that is
19074 probably because we just skipped over the body of
19075 a function. So, we consume a semicolon if
19076 present, but do not issue an error message if it
19078 if (cp_lexer_next_token_is (parser
->lexer
,
19080 cp_lexer_consume_token (parser
->lexer
);
19084 if (declares_class_or_enum
& 2)
19085 cp_parser_check_for_definition_in_return_type
19086 (declarator
, decl_specifiers
.type
,
19087 decl_specifiers
.locations
[ds_type_spec
]);
19089 /* Look for an asm-specification. */
19090 asm_specification
= cp_parser_asm_specification_opt (parser
);
19091 /* Look for attributes that apply to the declaration. */
19092 attributes
= cp_parser_attributes_opt (parser
);
19093 /* Remember which attributes are prefix attributes and
19095 first_attribute
= attributes
;
19096 /* Combine the attributes. */
19097 attributes
= chainon (prefix_attributes
, attributes
);
19099 /* If it's an `=', then we have a constant-initializer or a
19100 pure-specifier. It is not correct to parse the
19101 initializer before registering the member declaration
19102 since the member declaration should be in scope while
19103 its initializer is processed. However, the rest of the
19104 front end does not yet provide an interface that allows
19105 us to handle this correctly. */
19106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19110 A pure-specifier shall be used only in the declaration of
19111 a virtual function.
19113 A member-declarator can contain a constant-initializer
19114 only if it declares a static member of integral or
19117 Therefore, if the DECLARATOR is for a function, we look
19118 for a pure-specifier; otherwise, we look for a
19119 constant-initializer. When we call `grokfield', it will
19120 perform more stringent semantics checks. */
19121 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
19122 if (function_declarator_p (declarator
)
19123 || (decl_specifiers
.type
19124 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
19125 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
19126 == FUNCTION_TYPE
)))
19127 initializer
= cp_parser_pure_specifier (parser
);
19128 else if (decl_specifiers
.storage_class
!= sc_static
)
19129 initializer
= cp_parser_save_nsdmi (parser
);
19130 else if (cxx_dialect
>= cxx0x
)
19133 /* Don't require a constant rvalue in C++11, since we
19134 might want a reference constant. We'll enforce
19135 constancy later. */
19136 cp_lexer_consume_token (parser
->lexer
);
19137 /* Parse the initializer. */
19138 initializer
= cp_parser_initializer_clause (parser
,
19142 /* Parse the initializer. */
19143 initializer
= cp_parser_constant_initializer (parser
);
19145 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
19146 && !function_declarator_p (declarator
))
19149 if (decl_specifiers
.storage_class
!= sc_static
)
19150 initializer
= cp_parser_save_nsdmi (parser
);
19152 initializer
= cp_parser_initializer (parser
, &x
, &x
);
19154 /* Otherwise, there is no initializer. */
19156 initializer
= NULL_TREE
;
19158 /* See if we are probably looking at a function
19159 definition. We are certainly not looking at a
19160 member-declarator. Calling `grokfield' has
19161 side-effects, so we must not do it unless we are sure
19162 that we are looking at a member-declarator. */
19163 if (cp_parser_token_starts_function_definition_p
19164 (cp_lexer_peek_token (parser
->lexer
)))
19166 /* The grammar does not allow a pure-specifier to be
19167 used when a member function is defined. (It is
19168 possible that this fact is an oversight in the
19169 standard, since a pure function may be defined
19170 outside of the class-specifier. */
19171 if (initializer
&& initializer_token_start
)
19172 error_at (initializer_token_start
->location
,
19173 "pure-specifier on function-definition");
19174 decl
= cp_parser_save_member_function_body (parser
,
19178 /* If the member was not a friend, declare it here. */
19180 finish_member_declaration (decl
);
19181 /* Peek at the next token. */
19182 token
= cp_lexer_peek_token (parser
->lexer
);
19183 /* If the next token is a semicolon, consume it. */
19184 if (token
->type
== CPP_SEMICOLON
)
19185 cp_lexer_consume_token (parser
->lexer
);
19189 if (declarator
->kind
== cdk_function
)
19190 declarator
->id_loc
= token
->location
;
19191 /* Create the declaration. */
19192 decl
= grokfield (declarator
, &decl_specifiers
,
19193 initializer
, /*init_const_expr_p=*/true,
19198 /* Reset PREFIX_ATTRIBUTES. */
19199 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
19200 attributes
= TREE_CHAIN (attributes
);
19202 TREE_CHAIN (attributes
) = NULL_TREE
;
19204 /* If there is any qualification still in effect, clear it
19205 now; we will be starting fresh with the next declarator. */
19206 parser
->scope
= NULL_TREE
;
19207 parser
->qualifying_scope
= NULL_TREE
;
19208 parser
->object_scope
= NULL_TREE
;
19209 /* If it's a `,', then there are more declarators. */
19210 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19211 cp_lexer_consume_token (parser
->lexer
);
19212 /* If the next token isn't a `;', then we have a parse error. */
19213 else if (cp_lexer_next_token_is_not (parser
->lexer
,
19216 /* The next token might be a ways away from where the
19217 actual semicolon is missing. Find the previous token
19218 and use that for our error position. */
19219 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
19220 error_at (token
->location
,
19221 "expected %<;%> at end of member declaration");
19223 /* Assume that the user meant to provide a semicolon. If
19224 we were to cp_parser_skip_to_end_of_statement, we might
19225 skip to a semicolon inside a member function definition
19226 and issue nonsensical error messages. */
19227 assume_semicolon
= true;
19232 /* Add DECL to the list of members. */
19234 finish_member_declaration (decl
);
19236 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19237 cp_parser_save_default_args (parser
, decl
);
19238 else if (TREE_CODE (decl
) == FIELD_DECL
19239 && !DECL_C_BIT_FIELD (decl
)
19240 && DECL_INITIAL (decl
))
19241 /* Add DECL to the queue of NSDMI to be parsed later. */
19242 VEC_safe_push (tree
, gc
, unparsed_nsdmis
, decl
);
19245 if (assume_semicolon
)
19250 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19252 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19255 /* Parse a pure-specifier.
19260 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19261 Otherwise, ERROR_MARK_NODE is returned. */
19264 cp_parser_pure_specifier (cp_parser
* parser
)
19268 /* Look for the `=' token. */
19269 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
19270 return error_mark_node
;
19271 /* Look for the `0' token. */
19272 token
= cp_lexer_peek_token (parser
->lexer
);
19274 if (token
->type
== CPP_EOF
19275 || token
->type
== CPP_PRAGMA_EOL
)
19276 return error_mark_node
;
19278 cp_lexer_consume_token (parser
->lexer
);
19280 /* Accept = default or = delete in c++0x mode. */
19281 if (token
->keyword
== RID_DEFAULT
19282 || token
->keyword
== RID_DELETE
)
19284 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
19285 return token
->u
.value
;
19288 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19289 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
19291 cp_parser_error (parser
,
19292 "invalid pure specifier (only %<= 0%> is allowed)");
19293 cp_parser_skip_to_end_of_statement (parser
);
19294 return error_mark_node
;
19296 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19298 error_at (token
->location
, "templates may not be %<virtual%>");
19299 return error_mark_node
;
19302 return integer_zero_node
;
19305 /* Parse a constant-initializer.
19307 constant-initializer:
19308 = constant-expression
19310 Returns a representation of the constant-expression. */
19313 cp_parser_constant_initializer (cp_parser
* parser
)
19315 /* Look for the `=' token. */
19316 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
19317 return error_mark_node
;
19319 /* It is invalid to write:
19321 struct S { static const int i = { 7 }; };
19324 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19326 cp_parser_error (parser
,
19327 "a brace-enclosed initializer is not allowed here");
19328 /* Consume the opening brace. */
19329 cp_lexer_consume_token (parser
->lexer
);
19330 /* Skip the initializer. */
19331 cp_parser_skip_to_closing_brace (parser
);
19332 /* Look for the trailing `}'. */
19333 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19335 return error_mark_node
;
19338 return cp_parser_constant_expression (parser
,
19339 /*allow_non_constant=*/false,
19343 /* Derived classes [gram.class.derived] */
19345 /* Parse a base-clause.
19348 : base-specifier-list
19350 base-specifier-list:
19351 base-specifier ... [opt]
19352 base-specifier-list , base-specifier ... [opt]
19354 Returns a TREE_LIST representing the base-classes, in the order in
19355 which they were declared. The representation of each node is as
19356 described by cp_parser_base_specifier.
19358 In the case that no bases are specified, this function will return
19359 NULL_TREE, not ERROR_MARK_NODE. */
19362 cp_parser_base_clause (cp_parser
* parser
)
19364 tree bases
= NULL_TREE
;
19366 /* Look for the `:' that begins the list. */
19367 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
19369 /* Scan the base-specifier-list. */
19374 bool pack_expansion_p
= false;
19376 /* Look for the base-specifier. */
19377 base
= cp_parser_base_specifier (parser
);
19378 /* Look for the (optional) ellipsis. */
19379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19381 /* Consume the `...'. */
19382 cp_lexer_consume_token (parser
->lexer
);
19384 pack_expansion_p
= true;
19387 /* Add BASE to the front of the list. */
19388 if (base
&& base
!= error_mark_node
)
19390 if (pack_expansion_p
)
19391 /* Make this a pack expansion type. */
19392 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
19394 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
19396 TREE_CHAIN (base
) = bases
;
19400 /* Peek at the next token. */
19401 token
= cp_lexer_peek_token (parser
->lexer
);
19402 /* If it's not a comma, then the list is complete. */
19403 if (token
->type
!= CPP_COMMA
)
19405 /* Consume the `,'. */
19406 cp_lexer_consume_token (parser
->lexer
);
19409 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19410 base class had a qualified name. However, the next name that
19411 appears is certainly not qualified. */
19412 parser
->scope
= NULL_TREE
;
19413 parser
->qualifying_scope
= NULL_TREE
;
19414 parser
->object_scope
= NULL_TREE
;
19416 return nreverse (bases
);
19419 /* Parse a base-specifier.
19422 :: [opt] nested-name-specifier [opt] class-name
19423 virtual access-specifier [opt] :: [opt] nested-name-specifier
19425 access-specifier virtual [opt] :: [opt] nested-name-specifier
19428 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19429 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19430 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19431 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19434 cp_parser_base_specifier (cp_parser
* parser
)
19438 bool virtual_p
= false;
19439 bool duplicate_virtual_error_issued_p
= false;
19440 bool duplicate_access_error_issued_p
= false;
19441 bool class_scope_p
, template_p
;
19442 tree access
= access_default_node
;
19445 /* Process the optional `virtual' and `access-specifier'. */
19448 /* Peek at the next token. */
19449 token
= cp_lexer_peek_token (parser
->lexer
);
19450 /* Process `virtual'. */
19451 switch (token
->keyword
)
19454 /* If `virtual' appears more than once, issue an error. */
19455 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
19457 cp_parser_error (parser
,
19458 "%<virtual%> specified more than once in base-specified");
19459 duplicate_virtual_error_issued_p
= true;
19464 /* Consume the `virtual' token. */
19465 cp_lexer_consume_token (parser
->lexer
);
19470 case RID_PROTECTED
:
19472 /* If more than one access specifier appears, issue an
19474 if (access
!= access_default_node
19475 && !duplicate_access_error_issued_p
)
19477 cp_parser_error (parser
,
19478 "more than one access specifier in base-specified");
19479 duplicate_access_error_issued_p
= true;
19482 access
= ridpointers
[(int) token
->keyword
];
19484 /* Consume the access-specifier. */
19485 cp_lexer_consume_token (parser
->lexer
);
19494 /* It is not uncommon to see programs mechanically, erroneously, use
19495 the 'typename' keyword to denote (dependent) qualified types
19496 as base classes. */
19497 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
19499 token
= cp_lexer_peek_token (parser
->lexer
);
19500 if (!processing_template_decl
)
19501 error_at (token
->location
,
19502 "keyword %<typename%> not allowed outside of templates");
19504 error_at (token
->location
,
19505 "keyword %<typename%> not allowed in this context "
19506 "(the base class is implicitly a type)");
19507 cp_lexer_consume_token (parser
->lexer
);
19510 /* Look for the optional `::' operator. */
19511 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19512 /* Look for the nested-name-specifier. The simplest way to
19517 The keyword `typename' is not permitted in a base-specifier or
19518 mem-initializer; in these contexts a qualified name that
19519 depends on a template-parameter is implicitly assumed to be a
19522 is to pretend that we have seen the `typename' keyword at this
19524 cp_parser_nested_name_specifier_opt (parser
,
19525 /*typename_keyword_p=*/true,
19526 /*check_dependency_p=*/true,
19528 /*is_declaration=*/true);
19529 /* If the base class is given by a qualified name, assume that names
19530 we see are type names or templates, as appropriate. */
19531 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
19532 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
19535 && cp_lexer_next_token_is_decltype (parser
->lexer
))
19536 /* DR 950 allows decltype as a base-specifier. */
19537 type
= cp_parser_decltype (parser
);
19540 /* Otherwise, look for the class-name. */
19541 type
= cp_parser_class_name (parser
,
19545 /*check_dependency_p=*/true,
19546 /*class_head_p=*/false,
19547 /*is_declaration=*/true);
19548 type
= TREE_TYPE (type
);
19551 if (type
== error_mark_node
)
19552 return error_mark_node
;
19554 return finish_base_specifier (type
, access
, virtual_p
);
19557 /* Exception handling [gram.exception] */
19559 /* Parse an (optional) noexcept-specification.
19561 noexcept-specification:
19562 noexcept ( constant-expression ) [opt]
19564 If no noexcept-specification is present, returns NULL_TREE.
19565 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19566 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19567 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19568 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19569 in which case a boolean condition is returned instead. */
19572 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
19573 bool require_constexpr
,
19574 bool* consumed_expr
,
19578 const char *saved_message
;
19580 /* Peek at the next token. */
19581 token
= cp_lexer_peek_token (parser
->lexer
);
19583 /* Is it a noexcept-specification? */
19584 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
19587 cp_lexer_consume_token (parser
->lexer
);
19589 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
19591 cp_lexer_consume_token (parser
->lexer
);
19593 if (require_constexpr
)
19595 /* Types may not be defined in an exception-specification. */
19596 saved_message
= parser
->type_definition_forbidden_message
;
19597 parser
->type_definition_forbidden_message
19598 = G_("types may not be defined in an exception-specification");
19600 expr
= cp_parser_constant_expression (parser
, false, NULL
);
19602 /* Restore the saved message. */
19603 parser
->type_definition_forbidden_message
= saved_message
;
19607 expr
= cp_parser_expression (parser
, false, NULL
);
19608 *consumed_expr
= true;
19611 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19615 expr
= boolean_true_node
;
19616 if (!require_constexpr
)
19617 *consumed_expr
= false;
19620 /* We cannot build a noexcept-spec right away because this will check
19621 that expr is a constexpr. */
19623 return build_noexcept_spec (expr
, tf_warning_or_error
);
19631 /* Parse an (optional) exception-specification.
19633 exception-specification:
19634 throw ( type-id-list [opt] )
19636 Returns a TREE_LIST representing the exception-specification. The
19637 TREE_VALUE of each node is a type. */
19640 cp_parser_exception_specification_opt (cp_parser
* parser
)
19644 const char *saved_message
;
19646 /* Peek at the next token. */
19647 token
= cp_lexer_peek_token (parser
->lexer
);
19649 /* Is it a noexcept-specification? */
19650 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
19652 if (type_id_list
!= NULL_TREE
)
19653 return type_id_list
;
19655 /* If it's not `throw', then there's no exception-specification. */
19656 if (!cp_parser_is_keyword (token
, RID_THROW
))
19660 /* Enable this once a lot of code has transitioned to noexcept? */
19661 if (cxx_dialect
>= cxx0x
&& !in_system_header
)
19662 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
19663 "deprecated in C++0x; use %<noexcept%> instead");
19666 /* Consume the `throw'. */
19667 cp_lexer_consume_token (parser
->lexer
);
19669 /* Look for the `('. */
19670 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
19672 /* Peek at the next token. */
19673 token
= cp_lexer_peek_token (parser
->lexer
);
19674 /* If it's not a `)', then there is a type-id-list. */
19675 if (token
->type
!= CPP_CLOSE_PAREN
)
19677 /* Types may not be defined in an exception-specification. */
19678 saved_message
= parser
->type_definition_forbidden_message
;
19679 parser
->type_definition_forbidden_message
19680 = G_("types may not be defined in an exception-specification");
19681 /* Parse the type-id-list. */
19682 type_id_list
= cp_parser_type_id_list (parser
);
19683 /* Restore the saved message. */
19684 parser
->type_definition_forbidden_message
= saved_message
;
19687 type_id_list
= empty_except_spec
;
19689 /* Look for the `)'. */
19690 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19692 return type_id_list
;
19695 /* Parse an (optional) type-id-list.
19699 type-id-list , type-id ... [opt]
19701 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19702 in the order that the types were presented. */
19705 cp_parser_type_id_list (cp_parser
* parser
)
19707 tree types
= NULL_TREE
;
19714 /* Get the next type-id. */
19715 type
= cp_parser_type_id (parser
);
19716 /* Parse the optional ellipsis. */
19717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19719 /* Consume the `...'. */
19720 cp_lexer_consume_token (parser
->lexer
);
19722 /* Turn the type into a pack expansion expression. */
19723 type
= make_pack_expansion (type
);
19725 /* Add it to the list. */
19726 types
= add_exception_specifier (types
, type
, /*complain=*/1);
19727 /* Peek at the next token. */
19728 token
= cp_lexer_peek_token (parser
->lexer
);
19729 /* If it is not a `,', we are done. */
19730 if (token
->type
!= CPP_COMMA
)
19732 /* Consume the `,'. */
19733 cp_lexer_consume_token (parser
->lexer
);
19736 return nreverse (types
);
19739 /* Parse a try-block.
19742 try compound-statement handler-seq */
19745 cp_parser_try_block (cp_parser
* parser
)
19749 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
19750 try_block
= begin_try_block ();
19751 cp_parser_compound_statement (parser
, NULL
, true, false);
19752 finish_try_block (try_block
);
19753 cp_parser_handler_seq (parser
);
19754 finish_handler_sequence (try_block
);
19759 /* Parse a function-try-block.
19761 function-try-block:
19762 try ctor-initializer [opt] function-body handler-seq */
19765 cp_parser_function_try_block (cp_parser
* parser
)
19767 tree compound_stmt
;
19769 bool ctor_initializer_p
;
19771 /* Look for the `try' keyword. */
19772 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
19774 /* Let the rest of the front end know where we are. */
19775 try_block
= begin_function_try_block (&compound_stmt
);
19776 /* Parse the function-body. */
19777 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
19778 (parser
, /*in_function_try_block=*/true);
19779 /* We're done with the `try' part. */
19780 finish_function_try_block (try_block
);
19781 /* Parse the handlers. */
19782 cp_parser_handler_seq (parser
);
19783 /* We're done with the handlers. */
19784 finish_function_handler_sequence (try_block
, compound_stmt
);
19786 return ctor_initializer_p
;
19789 /* Parse a handler-seq.
19792 handler handler-seq [opt] */
19795 cp_parser_handler_seq (cp_parser
* parser
)
19801 /* Parse the handler. */
19802 cp_parser_handler (parser
);
19803 /* Peek at the next token. */
19804 token
= cp_lexer_peek_token (parser
->lexer
);
19805 /* If it's not `catch' then there are no more handlers. */
19806 if (!cp_parser_is_keyword (token
, RID_CATCH
))
19811 /* Parse a handler.
19814 catch ( exception-declaration ) compound-statement */
19817 cp_parser_handler (cp_parser
* parser
)
19822 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
19823 handler
= begin_handler ();
19824 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
19825 declaration
= cp_parser_exception_declaration (parser
);
19826 finish_handler_parms (declaration
, handler
);
19827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19828 cp_parser_compound_statement (parser
, NULL
, false, false);
19829 finish_handler (handler
);
19832 /* Parse an exception-declaration.
19834 exception-declaration:
19835 type-specifier-seq declarator
19836 type-specifier-seq abstract-declarator
19840 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19841 ellipsis variant is used. */
19844 cp_parser_exception_declaration (cp_parser
* parser
)
19846 cp_decl_specifier_seq type_specifiers
;
19847 cp_declarator
*declarator
;
19848 const char *saved_message
;
19850 /* If it's an ellipsis, it's easy to handle. */
19851 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19853 /* Consume the `...' token. */
19854 cp_lexer_consume_token (parser
->lexer
);
19858 /* Types may not be defined in exception-declarations. */
19859 saved_message
= parser
->type_definition_forbidden_message
;
19860 parser
->type_definition_forbidden_message
19861 = G_("types may not be defined in exception-declarations");
19863 /* Parse the type-specifier-seq. */
19864 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
19865 /*is_trailing_return=*/false,
19867 /* If it's a `)', then there is no declarator. */
19868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
19871 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
19872 /*ctor_dtor_or_conv_p=*/NULL
,
19873 /*parenthesized_p=*/NULL
,
19874 /*member_p=*/false);
19876 /* Restore the saved message. */
19877 parser
->type_definition_forbidden_message
= saved_message
;
19879 if (!type_specifiers
.any_specifiers_p
)
19880 return error_mark_node
;
19882 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
19885 /* Parse a throw-expression.
19888 throw assignment-expression [opt]
19890 Returns a THROW_EXPR representing the throw-expression. */
19893 cp_parser_throw_expression (cp_parser
* parser
)
19898 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
19899 token
= cp_lexer_peek_token (parser
->lexer
);
19900 /* Figure out whether or not there is an assignment-expression
19901 following the "throw" keyword. */
19902 if (token
->type
== CPP_COMMA
19903 || token
->type
== CPP_SEMICOLON
19904 || token
->type
== CPP_CLOSE_PAREN
19905 || token
->type
== CPP_CLOSE_SQUARE
19906 || token
->type
== CPP_CLOSE_BRACE
19907 || token
->type
== CPP_COLON
)
19908 expression
= NULL_TREE
;
19910 expression
= cp_parser_assignment_expression (parser
,
19911 /*cast_p=*/false, NULL
);
19913 return build_throw (expression
);
19916 /* GNU Extensions */
19918 /* Parse an (optional) asm-specification.
19921 asm ( string-literal )
19923 If the asm-specification is present, returns a STRING_CST
19924 corresponding to the string-literal. Otherwise, returns
19928 cp_parser_asm_specification_opt (cp_parser
* parser
)
19931 tree asm_specification
;
19933 /* Peek at the next token. */
19934 token
= cp_lexer_peek_token (parser
->lexer
);
19935 /* If the next token isn't the `asm' keyword, then there's no
19936 asm-specification. */
19937 if (!cp_parser_is_keyword (token
, RID_ASM
))
19940 /* Consume the `asm' token. */
19941 cp_lexer_consume_token (parser
->lexer
);
19942 /* Look for the `('. */
19943 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
19945 /* Look for the string-literal. */
19946 asm_specification
= cp_parser_string_literal (parser
, false, false);
19948 /* Look for the `)'. */
19949 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
19951 return asm_specification
;
19954 /* Parse an asm-operand-list.
19958 asm-operand-list , asm-operand
19961 string-literal ( expression )
19962 [ string-literal ] string-literal ( expression )
19964 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19965 each node is the expression. The TREE_PURPOSE is itself a
19966 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19967 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19968 is a STRING_CST for the string literal before the parenthesis. Returns
19969 ERROR_MARK_NODE if any of the operands are invalid. */
19972 cp_parser_asm_operand_list (cp_parser
* parser
)
19974 tree asm_operands
= NULL_TREE
;
19975 bool invalid_operands
= false;
19979 tree string_literal
;
19983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19985 /* Consume the `[' token. */
19986 cp_lexer_consume_token (parser
->lexer
);
19987 /* Read the operand name. */
19988 name
= cp_parser_identifier (parser
);
19989 if (name
!= error_mark_node
)
19990 name
= build_string (IDENTIFIER_LENGTH (name
),
19991 IDENTIFIER_POINTER (name
));
19992 /* Look for the closing `]'. */
19993 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19997 /* Look for the string-literal. */
19998 string_literal
= cp_parser_string_literal (parser
, false, false);
20000 /* Look for the `('. */
20001 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20002 /* Parse the expression. */
20003 expression
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
20004 /* Look for the `)'. */
20005 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
20007 if (name
== error_mark_node
20008 || string_literal
== error_mark_node
20009 || expression
== error_mark_node
)
20010 invalid_operands
= true;
20012 /* Add this operand to the list. */
20013 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
20016 /* If the next token is not a `,', there are no more
20018 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20020 /* Consume the `,'. */
20021 cp_lexer_consume_token (parser
->lexer
);
20024 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
20027 /* Parse an asm-clobber-list.
20031 asm-clobber-list , string-literal
20033 Returns a TREE_LIST, indicating the clobbers in the order that they
20034 appeared. The TREE_VALUE of each node is a STRING_CST. */
20037 cp_parser_asm_clobber_list (cp_parser
* parser
)
20039 tree clobbers
= NULL_TREE
;
20043 tree string_literal
;
20045 /* Look for the string literal. */
20046 string_literal
= cp_parser_string_literal (parser
, false, false);
20047 /* Add it to the list. */
20048 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
20049 /* If the next token is not a `,', then the list is
20051 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20053 /* Consume the `,' token. */
20054 cp_lexer_consume_token (parser
->lexer
);
20060 /* Parse an asm-label-list.
20064 asm-label-list , identifier
20066 Returns a TREE_LIST, indicating the labels in the order that they
20067 appeared. The TREE_VALUE of each node is a label. */
20070 cp_parser_asm_label_list (cp_parser
* parser
)
20072 tree labels
= NULL_TREE
;
20076 tree identifier
, label
, name
;
20078 /* Look for the identifier. */
20079 identifier
= cp_parser_identifier (parser
);
20080 if (!error_operand_p (identifier
))
20082 label
= lookup_label (identifier
);
20083 if (TREE_CODE (label
) == LABEL_DECL
)
20085 TREE_USED (label
) = 1;
20086 check_goto (label
);
20087 name
= build_string (IDENTIFIER_LENGTH (identifier
),
20088 IDENTIFIER_POINTER (identifier
));
20089 labels
= tree_cons (name
, label
, labels
);
20092 /* If the next token is not a `,', then the list is
20094 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20096 /* Consume the `,' token. */
20097 cp_lexer_consume_token (parser
->lexer
);
20100 return nreverse (labels
);
20103 /* Parse an (optional) series of attributes.
20106 attributes attribute
20109 __attribute__ (( attribute-list [opt] ))
20111 The return value is as for cp_parser_attribute_list. */
20114 cp_parser_attributes_opt (cp_parser
* parser
)
20116 tree attributes
= NULL_TREE
;
20121 tree attribute_list
;
20124 /* Peek at the next token. */
20125 token
= cp_lexer_peek_token (parser
->lexer
);
20126 /* If it's not `__attribute__', then we're done. */
20127 if (token
->keyword
!= RID_ATTRIBUTE
)
20130 /* Consume the `__attribute__' keyword. */
20131 cp_lexer_consume_token (parser
->lexer
);
20132 /* Look for the two `(' tokens. */
20133 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20134 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
20136 /* Peek at the next token. */
20137 token
= cp_lexer_peek_token (parser
->lexer
);
20138 if (token
->type
!= CPP_CLOSE_PAREN
)
20139 /* Parse the attribute-list. */
20140 attribute_list
= cp_parser_attribute_list (parser
);
20142 /* If the next token is a `)', then there is no attribute
20144 attribute_list
= NULL
;
20146 /* Look for the two `)' tokens. */
20147 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
20149 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
20152 cp_parser_skip_to_end_of_statement (parser
);
20154 /* Add these new attributes to the list. */
20155 attributes
= chainon (attributes
, attribute_list
);
20161 /* Parse an attribute-list.
20165 attribute-list , attribute
20169 identifier ( identifier )
20170 identifier ( identifier , expression-list )
20171 identifier ( expression-list )
20173 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20174 to an attribute. The TREE_PURPOSE of each node is the identifier
20175 indicating which attribute is in use. The TREE_VALUE represents
20176 the arguments, if any. */
20179 cp_parser_attribute_list (cp_parser
* parser
)
20181 tree attribute_list
= NULL_TREE
;
20182 bool save_translate_strings_p
= parser
->translate_strings_p
;
20184 parser
->translate_strings_p
= false;
20191 /* Look for the identifier. We also allow keywords here; for
20192 example `__attribute__ ((const))' is legal. */
20193 token
= cp_lexer_peek_token (parser
->lexer
);
20194 if (token
->type
== CPP_NAME
20195 || token
->type
== CPP_KEYWORD
)
20197 tree arguments
= NULL_TREE
;
20199 /* Consume the token. */
20200 token
= cp_lexer_consume_token (parser
->lexer
);
20202 /* Save away the identifier that indicates which attribute
20204 identifier
= (token
->type
== CPP_KEYWORD
)
20205 /* For keywords, use the canonical spelling, not the
20206 parsed identifier. */
20207 ? ridpointers
[(int) token
->keyword
]
20210 attribute
= build_tree_list (identifier
, NULL_TREE
);
20212 /* Peek at the next token. */
20213 token
= cp_lexer_peek_token (parser
->lexer
);
20214 /* If it's an `(', then parse the attribute arguments. */
20215 if (token
->type
== CPP_OPEN_PAREN
)
20218 int attr_flag
= (attribute_takes_identifier_p (identifier
)
20219 ? id_attr
: normal_attr
);
20220 vec
= cp_parser_parenthesized_expression_list
20221 (parser
, attr_flag
, /*cast_p=*/false,
20222 /*allow_expansion_p=*/false,
20223 /*non_constant_p=*/NULL
);
20225 arguments
= error_mark_node
;
20228 arguments
= build_tree_list_vec (vec
);
20229 release_tree_vector (vec
);
20231 /* Save the arguments away. */
20232 TREE_VALUE (attribute
) = arguments
;
20235 if (arguments
!= error_mark_node
)
20237 /* Add this attribute to the list. */
20238 TREE_CHAIN (attribute
) = attribute_list
;
20239 attribute_list
= attribute
;
20242 token
= cp_lexer_peek_token (parser
->lexer
);
20244 /* Now, look for more attributes. If the next token isn't a
20245 `,', we're done. */
20246 if (token
->type
!= CPP_COMMA
)
20249 /* Consume the comma and keep going. */
20250 cp_lexer_consume_token (parser
->lexer
);
20252 parser
->translate_strings_p
= save_translate_strings_p
;
20254 /* We built up the list in reverse order. */
20255 return nreverse (attribute_list
);
20258 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20259 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20260 current value of the PEDANTIC flag, regardless of whether or not
20261 the `__extension__' keyword is present. The caller is responsible
20262 for restoring the value of the PEDANTIC flag. */
20265 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
20267 /* Save the old value of the PEDANTIC flag. */
20268 *saved_pedantic
= pedantic
;
20270 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
20272 /* Consume the `__extension__' token. */
20273 cp_lexer_consume_token (parser
->lexer
);
20274 /* We're not being pedantic while the `__extension__' keyword is
20284 /* Parse a label declaration.
20287 __label__ label-declarator-seq ;
20289 label-declarator-seq:
20290 identifier , label-declarator-seq
20294 cp_parser_label_declaration (cp_parser
* parser
)
20296 /* Look for the `__label__' keyword. */
20297 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
20303 /* Look for an identifier. */
20304 identifier
= cp_parser_identifier (parser
);
20305 /* If we failed, stop. */
20306 if (identifier
== error_mark_node
)
20308 /* Declare it as a label. */
20309 finish_label_decl (identifier
);
20310 /* If the next token is a `;', stop. */
20311 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20313 /* Look for the `,' separating the label declarations. */
20314 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
20317 /* Look for the final `;'. */
20318 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20321 /* Support Functions */
20323 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20324 NAME should have one of the representations used for an
20325 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20326 is returned. If PARSER->SCOPE is a dependent type, then a
20327 SCOPE_REF is returned.
20329 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20330 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20331 was formed. Abstractly, such entities should not be passed to this
20332 function, because they do not need to be looked up, but it is
20333 simpler to check for this special case here, rather than at the
20336 In cases not explicitly covered above, this function returns a
20337 DECL, OVERLOAD, or baselink representing the result of the lookup.
20338 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20341 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20342 (e.g., "struct") that was used. In that case bindings that do not
20343 refer to types are ignored.
20345 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20348 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20351 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20354 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20355 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20356 NULL_TREE otherwise. */
20359 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
20360 enum tag_types tag_type
,
20363 bool check_dependency
,
20364 tree
*ambiguous_decls
,
20365 location_t name_location
)
20368 tree object_type
= parser
->context
->object_type
;
20370 /* Assume that the lookup will be unambiguous. */
20371 if (ambiguous_decls
)
20372 *ambiguous_decls
= NULL_TREE
;
20374 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20375 no longer valid. Note that if we are parsing tentatively, and
20376 the parse fails, OBJECT_TYPE will be automatically restored. */
20377 parser
->context
->object_type
= NULL_TREE
;
20379 if (name
== error_mark_node
)
20380 return error_mark_node
;
20382 /* A template-id has already been resolved; there is no lookup to
20384 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
20386 if (BASELINK_P (name
))
20388 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
20389 == TEMPLATE_ID_EXPR
);
20393 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20394 it should already have been checked to make sure that the name
20395 used matches the type being destroyed. */
20396 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
20400 /* Figure out to which type this destructor applies. */
20402 type
= parser
->scope
;
20403 else if (object_type
)
20404 type
= object_type
;
20406 type
= current_class_type
;
20407 /* If that's not a class type, there is no destructor. */
20408 if (!type
|| !CLASS_TYPE_P (type
))
20409 return error_mark_node
;
20410 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
20411 lazily_declare_fn (sfk_destructor
, type
);
20412 if (!CLASSTYPE_DESTRUCTORS (type
))
20413 return error_mark_node
;
20414 /* If it was a class type, return the destructor. */
20415 return CLASSTYPE_DESTRUCTORS (type
);
20418 /* By this point, the NAME should be an ordinary identifier. If
20419 the id-expression was a qualified name, the qualifying scope is
20420 stored in PARSER->SCOPE at this point. */
20421 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
20423 /* Perform the lookup. */
20428 if (parser
->scope
== error_mark_node
)
20429 return error_mark_node
;
20431 /* If the SCOPE is dependent, the lookup must be deferred until
20432 the template is instantiated -- unless we are explicitly
20433 looking up names in uninstantiated templates. Even then, we
20434 cannot look up the name if the scope is not a class type; it
20435 might, for example, be a template type parameter. */
20436 dependent_p
= (TYPE_P (parser
->scope
)
20437 && dependent_scope_p (parser
->scope
));
20438 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
20440 /* Defer lookup. */
20441 decl
= error_mark_node
;
20444 tree pushed_scope
= NULL_TREE
;
20446 /* If PARSER->SCOPE is a dependent type, then it must be a
20447 class type, and we must not be checking dependencies;
20448 otherwise, we would have processed this lookup above. So
20449 that PARSER->SCOPE is not considered a dependent base by
20450 lookup_member, we must enter the scope here. */
20452 pushed_scope
= push_scope (parser
->scope
);
20454 /* If the PARSER->SCOPE is a template specialization, it
20455 may be instantiated during name lookup. In that case,
20456 errors may be issued. Even if we rollback the current
20457 tentative parse, those errors are valid. */
20458 decl
= lookup_qualified_name (parser
->scope
, name
,
20459 tag_type
!= none_type
,
20460 /*complain=*/true);
20462 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20463 lookup result and the nested-name-specifier nominates a class C:
20464 * if the name specified after the nested-name-specifier, when
20465 looked up in C, is the injected-class-name of C (Clause 9), or
20466 * if the name specified after the nested-name-specifier is the
20467 same as the identifier or the simple-template-id's template-
20468 name in the last component of the nested-name-specifier,
20469 the name is instead considered to name the constructor of
20470 class C. [ Note: for example, the constructor is not an
20471 acceptable lookup result in an elaborated-type-specifier so
20472 the constructor would not be used in place of the
20473 injected-class-name. --end note ] Such a constructor name
20474 shall be used only in the declarator-id of a declaration that
20475 names a constructor or in a using-declaration. */
20476 if (tag_type
== none_type
20477 && DECL_SELF_REFERENCE_P (decl
)
20478 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
20479 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
20480 tag_type
!= none_type
,
20481 /*complain=*/true);
20483 /* If we have a single function from a using decl, pull it out. */
20484 if (TREE_CODE (decl
) == OVERLOAD
20485 && !really_overloaded_fn (decl
))
20486 decl
= OVL_FUNCTION (decl
);
20489 pop_scope (pushed_scope
);
20492 /* If the scope is a dependent type and either we deferred lookup or
20493 we did lookup but didn't find the name, rememeber the name. */
20494 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
20495 && dependent_type_p (parser
->scope
))
20501 /* The resolution to Core Issue 180 says that `struct
20502 A::B' should be considered a type-name, even if `A'
20504 type
= make_typename_type (parser
->scope
, name
, tag_type
,
20505 /*complain=*/tf_error
);
20506 decl
= TYPE_NAME (type
);
20508 else if (is_template
20509 && (cp_parser_next_token_ends_template_argument_p (parser
)
20510 || cp_lexer_next_token_is (parser
->lexer
,
20512 decl
= make_unbound_class_template (parser
->scope
,
20514 /*complain=*/tf_error
);
20516 decl
= build_qualified_name (/*type=*/NULL_TREE
,
20517 parser
->scope
, name
,
20520 parser
->qualifying_scope
= parser
->scope
;
20521 parser
->object_scope
= NULL_TREE
;
20523 else if (object_type
)
20525 tree object_decl
= NULL_TREE
;
20526 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20527 OBJECT_TYPE is not a class. */
20528 if (CLASS_TYPE_P (object_type
))
20529 /* If the OBJECT_TYPE is a template specialization, it may
20530 be instantiated during name lookup. In that case, errors
20531 may be issued. Even if we rollback the current tentative
20532 parse, those errors are valid. */
20533 object_decl
= lookup_member (object_type
,
20536 tag_type
!= none_type
,
20537 tf_warning_or_error
);
20538 /* Look it up in the enclosing context, too. */
20539 decl
= lookup_name_real (name
, tag_type
!= none_type
,
20541 /*block_p=*/true, is_namespace
, 0);
20542 parser
->object_scope
= object_type
;
20543 parser
->qualifying_scope
= NULL_TREE
;
20545 decl
= object_decl
;
20549 decl
= lookup_name_real (name
, tag_type
!= none_type
,
20551 /*block_p=*/true, is_namespace
, 0);
20552 parser
->qualifying_scope
= NULL_TREE
;
20553 parser
->object_scope
= NULL_TREE
;
20556 /* If the lookup failed, let our caller know. */
20557 if (!decl
|| decl
== error_mark_node
)
20558 return error_mark_node
;
20560 /* Pull out the template from an injected-class-name (or multiple). */
20562 decl
= maybe_get_template_decl_from_type_decl (decl
);
20564 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20565 if (TREE_CODE (decl
) == TREE_LIST
)
20567 if (ambiguous_decls
)
20568 *ambiguous_decls
= decl
;
20569 /* The error message we have to print is too complicated for
20570 cp_parser_error, so we incorporate its actions directly. */
20571 if (!cp_parser_simulate_error (parser
))
20573 error_at (name_location
, "reference to %qD is ambiguous",
20575 print_candidates (decl
);
20577 return error_mark_node
;
20580 gcc_assert (DECL_P (decl
)
20581 || TREE_CODE (decl
) == OVERLOAD
20582 || TREE_CODE (decl
) == SCOPE_REF
20583 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
20584 || BASELINK_P (decl
));
20586 /* If we have resolved the name of a member declaration, check to
20587 see if the declaration is accessible. When the name resolves to
20588 set of overloaded functions, accessibility is checked when
20589 overload resolution is done.
20591 During an explicit instantiation, access is not checked at all,
20592 as per [temp.explicit]. */
20594 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
20596 maybe_record_typedef_use (decl
);
20601 /* Like cp_parser_lookup_name, but for use in the typical case where
20602 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20603 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20606 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
20608 return cp_parser_lookup_name (parser
, name
,
20610 /*is_template=*/false,
20611 /*is_namespace=*/false,
20612 /*check_dependency=*/true,
20613 /*ambiguous_decls=*/NULL
,
20617 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20618 the current context, return the TYPE_DECL. If TAG_NAME_P is
20619 true, the DECL indicates the class being defined in a class-head,
20620 or declared in an elaborated-type-specifier.
20622 Otherwise, return DECL. */
20625 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
20627 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20628 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20631 template <typename T> struct B;
20634 template <typename T> struct A::B {};
20636 Similarly, in an elaborated-type-specifier:
20638 namespace N { struct X{}; }
20641 template <typename T> friend struct N::X;
20644 However, if the DECL refers to a class type, and we are in
20645 the scope of the class, then the name lookup automatically
20646 finds the TYPE_DECL created by build_self_reference rather
20647 than a TEMPLATE_DECL. For example, in:
20649 template <class T> struct S {
20653 there is no need to handle such case. */
20655 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
20656 return DECL_TEMPLATE_RESULT (decl
);
20661 /* If too many, or too few, template-parameter lists apply to the
20662 declarator, issue an error message. Returns TRUE if all went well,
20663 and FALSE otherwise. */
20666 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
20667 cp_declarator
*declarator
,
20668 location_t declarator_location
)
20670 switch (declarator
->kind
)
20674 unsigned num_templates
= 0;
20675 tree scope
= declarator
->u
.id
.qualifying_scope
;
20678 num_templates
= num_template_headers_for_class (scope
);
20679 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
20680 == TEMPLATE_ID_EXPR
)
20681 /* If the DECLARATOR has the form `X<y>' then it uses one
20682 additional level of template parameters. */
20685 return cp_parser_check_template_parameters
20686 (parser
, num_templates
, declarator_location
, declarator
);
20692 case cdk_reference
:
20694 return (cp_parser_check_declarator_template_parameters
20695 (parser
, declarator
->declarator
, declarator_location
));
20701 gcc_unreachable ();
20706 /* NUM_TEMPLATES were used in the current declaration. If that is
20707 invalid, return FALSE and issue an error messages. Otherwise,
20708 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20709 declarator and we can print more accurate diagnostics. */
20712 cp_parser_check_template_parameters (cp_parser
* parser
,
20713 unsigned num_templates
,
20714 location_t location
,
20715 cp_declarator
*declarator
)
20717 /* If there are the same number of template classes and parameter
20718 lists, that's OK. */
20719 if (parser
->num_template_parameter_lists
== num_templates
)
20721 /* If there are more, but only one more, then we are referring to a
20722 member template. That's OK too. */
20723 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
20725 /* If there are more template classes than parameter lists, we have
20728 template <class T> void S<T>::R<T>::f (); */
20729 if (parser
->num_template_parameter_lists
< num_templates
)
20731 if (declarator
&& !current_function_decl
)
20732 error_at (location
, "specializing member %<%T::%E%> "
20733 "requires %<template<>%> syntax",
20734 declarator
->u
.id
.qualifying_scope
,
20735 declarator
->u
.id
.unqualified_name
);
20736 else if (declarator
)
20737 error_at (location
, "invalid declaration of %<%T::%E%>",
20738 declarator
->u
.id
.qualifying_scope
,
20739 declarator
->u
.id
.unqualified_name
);
20741 error_at (location
, "too few template-parameter-lists");
20744 /* Otherwise, there are too many template parameter lists. We have
20747 template <class T> template <class U> void S::f(); */
20748 error_at (location
, "too many template-parameter-lists");
20752 /* Parse an optional `::' token indicating that the following name is
20753 from the global namespace. If so, PARSER->SCOPE is set to the
20754 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20755 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20756 Returns the new value of PARSER->SCOPE, if the `::' token is
20757 present, and NULL_TREE otherwise. */
20760 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
20764 /* Peek at the next token. */
20765 token
= cp_lexer_peek_token (parser
->lexer
);
20766 /* If we're looking at a `::' token then we're starting from the
20767 global namespace, not our current location. */
20768 if (token
->type
== CPP_SCOPE
)
20770 /* Consume the `::' token. */
20771 cp_lexer_consume_token (parser
->lexer
);
20772 /* Set the SCOPE so that we know where to start the lookup. */
20773 parser
->scope
= global_namespace
;
20774 parser
->qualifying_scope
= global_namespace
;
20775 parser
->object_scope
= NULL_TREE
;
20777 return parser
->scope
;
20779 else if (!current_scope_valid_p
)
20781 parser
->scope
= NULL_TREE
;
20782 parser
->qualifying_scope
= NULL_TREE
;
20783 parser
->object_scope
= NULL_TREE
;
20789 /* Returns TRUE if the upcoming token sequence is the start of a
20790 constructor declarator. If FRIEND_P is true, the declarator is
20791 preceded by the `friend' specifier. */
20794 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
20796 bool constructor_p
;
20797 tree nested_name_specifier
;
20798 cp_token
*next_token
;
20800 /* The common case is that this is not a constructor declarator, so
20801 try to avoid doing lots of work if at all possible. It's not
20802 valid declare a constructor at function scope. */
20803 if (parser
->in_function_body
)
20805 /* And only certain tokens can begin a constructor declarator. */
20806 next_token
= cp_lexer_peek_token (parser
->lexer
);
20807 if (next_token
->type
!= CPP_NAME
20808 && next_token
->type
!= CPP_SCOPE
20809 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
20810 && next_token
->type
!= CPP_TEMPLATE_ID
)
20813 /* Parse tentatively; we are going to roll back all of the tokens
20815 cp_parser_parse_tentatively (parser
);
20816 /* Assume that we are looking at a constructor declarator. */
20817 constructor_p
= true;
20819 /* Look for the optional `::' operator. */
20820 cp_parser_global_scope_opt (parser
,
20821 /*current_scope_valid_p=*/false);
20822 /* Look for the nested-name-specifier. */
20823 nested_name_specifier
20824 = (cp_parser_nested_name_specifier_opt (parser
,
20825 /*typename_keyword_p=*/false,
20826 /*check_dependency_p=*/false,
20828 /*is_declaration=*/false));
20829 /* Outside of a class-specifier, there must be a
20830 nested-name-specifier. */
20831 if (!nested_name_specifier
&&
20832 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type
)
20834 constructor_p
= false;
20835 else if (nested_name_specifier
== error_mark_node
)
20836 constructor_p
= false;
20838 /* If we have a class scope, this is easy; DR 147 says that S::S always
20839 names the constructor, and no other qualified name could. */
20840 if (constructor_p
&& nested_name_specifier
20841 && CLASS_TYPE_P (nested_name_specifier
))
20843 tree id
= cp_parser_unqualified_id (parser
,
20844 /*template_keyword_p=*/false,
20845 /*check_dependency_p=*/false,
20846 /*declarator_p=*/true,
20847 /*optional_p=*/false);
20848 if (is_overloaded_fn (id
))
20849 id
= DECL_NAME (get_first_fn (id
));
20850 if (!constructor_name_p (id
, nested_name_specifier
))
20851 constructor_p
= false;
20853 /* If we still think that this might be a constructor-declarator,
20854 look for a class-name. */
20855 else if (constructor_p
)
20859 template <typename T> struct S {
20863 we must recognize that the nested `S' names a class. */
20865 type_decl
= cp_parser_class_name (parser
,
20866 /*typename_keyword_p=*/false,
20867 /*template_keyword_p=*/false,
20869 /*check_dependency_p=*/false,
20870 /*class_head_p=*/false,
20871 /*is_declaration=*/false);
20872 /* If there was no class-name, then this is not a constructor. */
20873 constructor_p
= !cp_parser_error_occurred (parser
);
20875 /* If we're still considering a constructor, we have to see a `(',
20876 to begin the parameter-declaration-clause, followed by either a
20877 `)', an `...', or a decl-specifier. We need to check for a
20878 type-specifier to avoid being fooled into thinking that:
20882 is a constructor. (It is actually a function named `f' that
20883 takes one parameter (of type `int') and returns a value of type
20886 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
20887 constructor_p
= false;
20890 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
20891 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
20892 /* A parameter declaration begins with a decl-specifier,
20893 which is either the "attribute" keyword, a storage class
20894 specifier, or (usually) a type-specifier. */
20895 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
20898 tree pushed_scope
= NULL_TREE
;
20899 unsigned saved_num_template_parameter_lists
;
20901 /* Names appearing in the type-specifier should be looked up
20902 in the scope of the class. */
20903 if (current_class_type
)
20907 type
= TREE_TYPE (type_decl
);
20908 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20910 type
= resolve_typename_type (type
,
20911 /*only_current_p=*/false);
20912 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20914 cp_parser_abort_tentative_parse (parser
);
20918 pushed_scope
= push_scope (type
);
20921 /* Inside the constructor parameter list, surrounding
20922 template-parameter-lists do not apply. */
20923 saved_num_template_parameter_lists
20924 = parser
->num_template_parameter_lists
;
20925 parser
->num_template_parameter_lists
= 0;
20927 /* Look for the type-specifier. */
20928 cp_parser_type_specifier (parser
,
20929 CP_PARSER_FLAGS_NONE
,
20930 /*decl_specs=*/NULL
,
20931 /*is_declarator=*/true,
20932 /*declares_class_or_enum=*/NULL
,
20933 /*is_cv_qualifier=*/NULL
);
20935 parser
->num_template_parameter_lists
20936 = saved_num_template_parameter_lists
;
20938 /* Leave the scope of the class. */
20940 pop_scope (pushed_scope
);
20942 constructor_p
= !cp_parser_error_occurred (parser
);
20946 /* We did not really want to consume any tokens. */
20947 cp_parser_abort_tentative_parse (parser
);
20949 return constructor_p
;
20952 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20953 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20954 they must be performed once we are in the scope of the function.
20956 Returns the function defined. */
20959 cp_parser_function_definition_from_specifiers_and_declarator
20960 (cp_parser
* parser
,
20961 cp_decl_specifier_seq
*decl_specifiers
,
20963 const cp_declarator
*declarator
)
20968 /* Begin the function-definition. */
20969 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
20971 /* The things we're about to see are not directly qualified by any
20972 template headers we've seen thus far. */
20973 reset_specialization ();
20975 /* If there were names looked up in the decl-specifier-seq that we
20976 did not check, check them now. We must wait until we are in the
20977 scope of the function to perform the checks, since the function
20978 might be a friend. */
20979 perform_deferred_access_checks (tf_warning_or_error
);
20983 /* Skip the entire function. */
20984 cp_parser_skip_to_end_of_block_or_statement (parser
);
20985 fn
= error_mark_node
;
20987 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
20989 /* Seen already, skip it. An error message has already been output. */
20990 cp_parser_skip_to_end_of_block_or_statement (parser
);
20991 fn
= current_function_decl
;
20992 current_function_decl
= NULL_TREE
;
20993 /* If this is a function from a class, pop the nested class. */
20994 if (current_class_name
)
20995 pop_nested_class ();
21000 if (DECL_DECLARED_INLINE_P (current_function_decl
))
21001 tv
= TV_PARSE_INLINE
;
21003 tv
= TV_PARSE_FUNC
;
21005 fn
= cp_parser_function_definition_after_declarator (parser
,
21006 /*inline_p=*/false);
21013 /* Parse the part of a function-definition that follows the
21014 declarator. INLINE_P is TRUE iff this function is an inline
21015 function defined within a class-specifier.
21017 Returns the function defined. */
21020 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
21024 bool ctor_initializer_p
= false;
21025 bool saved_in_unbraced_linkage_specification_p
;
21026 bool saved_in_function_body
;
21027 unsigned saved_num_template_parameter_lists
;
21030 saved_in_function_body
= parser
->in_function_body
;
21031 parser
->in_function_body
= true;
21032 /* If the next token is `return', then the code may be trying to
21033 make use of the "named return value" extension that G++ used to
21035 token
= cp_lexer_peek_token (parser
->lexer
);
21036 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
21038 /* Consume the `return' keyword. */
21039 cp_lexer_consume_token (parser
->lexer
);
21040 /* Look for the identifier that indicates what value is to be
21042 cp_parser_identifier (parser
);
21043 /* Issue an error message. */
21044 error_at (token
->location
,
21045 "named return values are no longer supported");
21046 /* Skip tokens until we reach the start of the function body. */
21049 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21050 if (token
->type
== CPP_OPEN_BRACE
21051 || token
->type
== CPP_EOF
21052 || token
->type
== CPP_PRAGMA_EOL
)
21054 cp_lexer_consume_token (parser
->lexer
);
21057 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21058 anything declared inside `f'. */
21059 saved_in_unbraced_linkage_specification_p
21060 = parser
->in_unbraced_linkage_specification_p
;
21061 parser
->in_unbraced_linkage_specification_p
= false;
21062 /* Inside the function, surrounding template-parameter-lists do not
21064 saved_num_template_parameter_lists
21065 = parser
->num_template_parameter_lists
;
21066 parser
->num_template_parameter_lists
= 0;
21068 start_lambda_scope (current_function_decl
);
21070 /* If the next token is `try', `__transaction_atomic', or
21071 `__transaction_relaxed`, then we are looking at either function-try-block
21072 or function-transaction-block. Note that all of these include the
21074 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
21075 ctor_initializer_p
= cp_parser_function_transaction (parser
,
21076 RID_TRANSACTION_ATOMIC
);
21077 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
21078 RID_TRANSACTION_RELAXED
))
21079 ctor_initializer_p
= cp_parser_function_transaction (parser
,
21080 RID_TRANSACTION_RELAXED
);
21081 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
21082 ctor_initializer_p
= cp_parser_function_try_block (parser
);
21084 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21085 (parser
, /*in_function_try_block=*/false);
21087 finish_lambda_scope ();
21089 /* Finish the function. */
21090 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
21091 (inline_p
? 2 : 0));
21092 /* Generate code for it, if necessary. */
21093 expand_or_defer_fn (fn
);
21094 /* Restore the saved values. */
21095 parser
->in_unbraced_linkage_specification_p
21096 = saved_in_unbraced_linkage_specification_p
;
21097 parser
->num_template_parameter_lists
21098 = saved_num_template_parameter_lists
;
21099 parser
->in_function_body
= saved_in_function_body
;
21104 /* Parse a template-declaration, assuming that the `export' (and
21105 `extern') keywords, if present, has already been scanned. MEMBER_P
21106 is as for cp_parser_template_declaration. */
21109 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
21111 tree decl
= NULL_TREE
;
21112 VEC (deferred_access_check
,gc
) *checks
;
21113 tree parameter_list
;
21114 bool friend_p
= false;
21115 bool need_lang_pop
;
21118 /* Look for the `template' keyword. */
21119 token
= cp_lexer_peek_token (parser
->lexer
);
21120 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
21124 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
21126 if (at_class_scope_p () && current_function_decl
)
21128 /* 14.5.2.2 [temp.mem]
21130 A local class shall not have member templates. */
21131 error_at (token
->location
,
21132 "invalid declaration of member template in local class");
21133 cp_parser_skip_to_end_of_block_or_statement (parser
);
21138 A template ... shall not have C linkage. */
21139 if (current_lang_name
== lang_name_c
)
21141 error_at (token
->location
, "template with C linkage");
21142 /* Give it C++ linkage to avoid confusing other parts of the
21144 push_lang_context (lang_name_cplusplus
);
21145 need_lang_pop
= true;
21148 need_lang_pop
= false;
21150 /* We cannot perform access checks on the template parameter
21151 declarations until we know what is being declared, just as we
21152 cannot check the decl-specifier list. */
21153 push_deferring_access_checks (dk_deferred
);
21155 /* If the next token is `>', then we have an invalid
21156 specialization. Rather than complain about an invalid template
21157 parameter, issue an error message here. */
21158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
21160 cp_parser_error (parser
, "invalid explicit specialization");
21161 begin_specialization ();
21162 parameter_list
= NULL_TREE
;
21166 /* Parse the template parameters. */
21167 parameter_list
= cp_parser_template_parameter_list (parser
);
21170 /* Get the deferred access checks from the parameter list. These
21171 will be checked once we know what is being declared, as for a
21172 member template the checks must be performed in the scope of the
21173 class containing the member. */
21174 checks
= get_deferred_access_checks ();
21176 /* Look for the `>'. */
21177 cp_parser_skip_to_end_of_template_parameter_list (parser
);
21178 /* We just processed one more parameter list. */
21179 ++parser
->num_template_parameter_lists
;
21180 /* If the next token is `template', there are more template
21182 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
21184 cp_parser_template_declaration_after_export (parser
, member_p
);
21185 else if (cxx_dialect
>= cxx0x
21186 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
21187 decl
= cp_parser_alias_declaration (parser
);
21190 /* There are no access checks when parsing a template, as we do not
21191 know if a specialization will be a friend. */
21192 push_deferring_access_checks (dk_no_check
);
21193 token
= cp_lexer_peek_token (parser
->lexer
);
21194 decl
= cp_parser_single_declaration (parser
,
21197 /*explicit_specialization_p=*/false,
21199 pop_deferring_access_checks ();
21201 /* If this is a member template declaration, let the front
21203 if (member_p
&& !friend_p
&& decl
)
21205 if (TREE_CODE (decl
) == TYPE_DECL
)
21206 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
21208 decl
= finish_member_template_decl (decl
);
21210 else if (friend_p
&& decl
21211 && (TREE_CODE (decl
) == TYPE_DECL
21212 || DECL_TYPE_TEMPLATE_P (decl
)))
21213 make_friend_class (current_class_type
, TREE_TYPE (decl
),
21214 /*complain=*/true);
21216 /* We are done with the current parameter list. */
21217 --parser
->num_template_parameter_lists
;
21219 pop_deferring_access_checks ();
21222 finish_template_decl (parameter_list
);
21224 /* Check the template arguments for a literal operator template. */
21226 && (TREE_CODE (decl
) == FUNCTION_DECL
|| DECL_FUNCTION_TEMPLATE_P (decl
))
21227 && UDLIT_OPER_P (DECL_NAME (decl
)))
21230 if (parameter_list
== NULL_TREE
)
21234 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
21235 if (num_parms
!= 1)
21239 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
21240 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
21241 if (TREE_TYPE (parm
) != char_type_node
21242 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
21247 error ("literal operator template %qD has invalid parameter list."
21248 " Expected non-type template argument pack <char...>",
21251 /* Register member declarations. */
21252 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
21253 finish_member_declaration (decl
);
21254 /* For the erroneous case of a template with C linkage, we pushed an
21255 implicit C++ linkage scope; exit that scope now. */
21257 pop_lang_context ();
21258 /* If DECL is a function template, we must return to parse it later.
21259 (Even though there is no definition, there might be default
21260 arguments that need handling.) */
21261 if (member_p
&& decl
21262 && (TREE_CODE (decl
) == FUNCTION_DECL
21263 || DECL_FUNCTION_TEMPLATE_P (decl
)))
21264 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, decl
);
21267 /* Perform the deferred access checks from a template-parameter-list.
21268 CHECKS is a TREE_LIST of access checks, as returned by
21269 get_deferred_access_checks. */
21272 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check
,gc
)* checks
)
21274 ++processing_template_parmlist
;
21275 perform_access_checks (checks
, tf_warning_or_error
);
21276 --processing_template_parmlist
;
21279 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21280 `function-definition' sequence that follows a template header.
21281 If MEMBER_P is true, this declaration appears in a class scope.
21283 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21284 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21287 cp_parser_single_declaration (cp_parser
* parser
,
21288 VEC (deferred_access_check
,gc
)* checks
,
21290 bool explicit_specialization_p
,
21293 int declares_class_or_enum
;
21294 tree decl
= NULL_TREE
;
21295 cp_decl_specifier_seq decl_specifiers
;
21296 bool function_definition_p
= false;
21297 cp_token
*decl_spec_token_start
;
21299 /* This function is only used when processing a template
21301 gcc_assert (innermost_scope_kind () == sk_template_parms
21302 || innermost_scope_kind () == sk_template_spec
);
21304 /* Defer access checks until we know what is being declared. */
21305 push_deferring_access_checks (dk_deferred
);
21307 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21309 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21310 cp_parser_decl_specifier_seq (parser
,
21311 CP_PARSER_FLAGS_OPTIONAL
,
21313 &declares_class_or_enum
);
21315 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
21317 /* There are no template typedefs. */
21318 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
21320 error_at (decl_spec_token_start
->location
,
21321 "template declaration of %<typedef%>");
21322 decl
= error_mark_node
;
21325 /* Gather up the access checks that occurred the
21326 decl-specifier-seq. */
21327 stop_deferring_access_checks ();
21329 /* Check for the declaration of a template class. */
21330 if (declares_class_or_enum
)
21332 if (cp_parser_declares_only_class_p (parser
))
21334 decl
= shadow_tag (&decl_specifiers
);
21339 friend template <typename T> struct A<T>::B;
21342 A<T>::B will be represented by a TYPENAME_TYPE, and
21343 therefore not recognized by shadow_tag. */
21344 if (friend_p
&& *friend_p
21346 && decl_specifiers
.type
21347 && TYPE_P (decl_specifiers
.type
))
21348 decl
= decl_specifiers
.type
;
21350 if (decl
&& decl
!= error_mark_node
)
21351 decl
= TYPE_NAME (decl
);
21353 decl
= error_mark_node
;
21355 /* Perform access checks for template parameters. */
21356 cp_parser_perform_template_parameter_access_checks (checks
);
21360 /* Complain about missing 'typename' or other invalid type names. */
21361 if (!decl_specifiers
.any_type_specifiers_p
21362 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21364 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21365 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21366 the rest of this declaration. */
21367 decl
= error_mark_node
;
21371 /* If it's not a template class, try for a template function. If
21372 the next token is a `;', then this declaration does not declare
21373 anything. But, if there were errors in the decl-specifiers, then
21374 the error might well have come from an attempted class-specifier.
21375 In that case, there's no need to warn about a missing declarator. */
21377 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
21378 || decl_specifiers
.type
!= error_mark_node
))
21380 decl
= cp_parser_init_declarator (parser
,
21383 /*function_definition_allowed_p=*/true,
21385 declares_class_or_enum
,
21386 &function_definition_p
,
21389 /* 7.1.1-1 [dcl.stc]
21391 A storage-class-specifier shall not be specified in an explicit
21392 specialization... */
21394 && explicit_specialization_p
21395 && decl_specifiers
.storage_class
!= sc_none
)
21397 error_at (decl_spec_token_start
->location
,
21398 "explicit template specialization cannot have a storage class");
21399 decl
= error_mark_node
;
21402 if (decl
&& TREE_CODE (decl
) == VAR_DECL
)
21403 check_template_variable (decl
);
21406 /* Look for a trailing `;' after the declaration. */
21407 if (!function_definition_p
21408 && (decl
== error_mark_node
21409 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
21410 cp_parser_skip_to_end_of_block_or_statement (parser
);
21413 pop_deferring_access_checks ();
21415 /* Clear any current qualification; whatever comes next is the start
21416 of something new. */
21417 parser
->scope
= NULL_TREE
;
21418 parser
->qualifying_scope
= NULL_TREE
;
21419 parser
->object_scope
= NULL_TREE
;
21424 /* Parse a cast-expression that is not the operand of a unary "&". */
21427 cp_parser_simple_cast_expression (cp_parser
*parser
)
21429 return cp_parser_cast_expression (parser
, /*address_p=*/false,
21430 /*cast_p=*/false, NULL
);
21433 /* Parse a functional cast to TYPE. Returns an expression
21434 representing the cast. */
21437 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
21440 tree expression_list
;
21444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21446 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21447 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
21448 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
21449 if (TREE_CODE (type
) == TYPE_DECL
)
21450 type
= TREE_TYPE (type
);
21451 return finish_compound_literal (type
, expression_list
,
21452 tf_warning_or_error
);
21456 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21458 /*allow_expansion_p=*/true,
21459 /*non_constant_p=*/NULL
);
21461 expression_list
= error_mark_node
;
21464 expression_list
= build_tree_list_vec (vec
);
21465 release_tree_vector (vec
);
21468 cast
= build_functional_cast (type
, expression_list
,
21469 tf_warning_or_error
);
21470 /* [expr.const]/1: In an integral constant expression "only type
21471 conversions to integral or enumeration type can be used". */
21472 if (TREE_CODE (type
) == TYPE_DECL
)
21473 type
= TREE_TYPE (type
);
21474 if (cast
!= error_mark_node
21475 && !cast_valid_in_integral_constant_expression_p (type
)
21476 && cp_parser_non_integral_constant_expression (parser
,
21478 return error_mark_node
;
21482 /* Save the tokens that make up the body of a member function defined
21483 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21484 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21485 specifiers applied to the declaration. Returns the FUNCTION_DECL
21486 for the member function. */
21489 cp_parser_save_member_function_body (cp_parser
* parser
,
21490 cp_decl_specifier_seq
*decl_specifiers
,
21491 cp_declarator
*declarator
,
21498 /* Create the FUNCTION_DECL. */
21499 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
21500 /* If something went badly wrong, bail out now. */
21501 if (fn
== error_mark_node
)
21503 /* If there's a function-body, skip it. */
21504 if (cp_parser_token_starts_function_definition_p
21505 (cp_lexer_peek_token (parser
->lexer
)))
21506 cp_parser_skip_to_end_of_block_or_statement (parser
);
21507 return error_mark_node
;
21510 /* Remember it, if there default args to post process. */
21511 cp_parser_save_default_args (parser
, fn
);
21513 /* Save away the tokens that make up the body of the
21515 first
= parser
->lexer
->next_token
;
21516 /* We can have braced-init-list mem-initializers before the fn body. */
21517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
21519 cp_lexer_consume_token (parser
->lexer
);
21520 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21521 && cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_TRY
))
21523 /* cache_group will stop after an un-nested { } pair, too. */
21524 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
21527 /* variadic mem-inits have ... after the ')'. */
21528 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21529 cp_lexer_consume_token (parser
->lexer
);
21532 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
21533 /* Handle function try blocks. */
21534 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
21535 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
21536 last
= parser
->lexer
->next_token
;
21538 /* Save away the inline definition; we will process it when the
21539 class is complete. */
21540 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
21541 DECL_PENDING_INLINE_P (fn
) = 1;
21543 /* We need to know that this was defined in the class, so that
21544 friend templates are handled correctly. */
21545 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
21547 /* Add FN to the queue of functions to be parsed later. */
21548 VEC_safe_push (tree
, gc
, unparsed_funs_with_definitions
, fn
);
21553 /* Save the tokens that make up the in-class initializer for a non-static
21554 data member. Returns a DEFAULT_ARG. */
21557 cp_parser_save_nsdmi (cp_parser
* parser
)
21559 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
21562 /* Parse a template-argument-list, as well as the trailing ">" (but
21563 not the opening "<"). See cp_parser_template_argument_list for the
21567 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
21571 tree saved_qualifying_scope
;
21572 tree saved_object_scope
;
21573 bool saved_greater_than_is_operator_p
;
21574 int saved_unevaluated_operand
;
21575 int saved_inhibit_evaluation_warnings
;
21579 When parsing a template-id, the first non-nested `>' is taken as
21580 the end of the template-argument-list rather than a greater-than
21582 saved_greater_than_is_operator_p
21583 = parser
->greater_than_is_operator_p
;
21584 parser
->greater_than_is_operator_p
= false;
21585 /* Parsing the argument list may modify SCOPE, so we save it
21587 saved_scope
= parser
->scope
;
21588 saved_qualifying_scope
= parser
->qualifying_scope
;
21589 saved_object_scope
= parser
->object_scope
;
21590 /* We need to evaluate the template arguments, even though this
21591 template-id may be nested within a "sizeof". */
21592 saved_unevaluated_operand
= cp_unevaluated_operand
;
21593 cp_unevaluated_operand
= 0;
21594 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
21595 c_inhibit_evaluation_warnings
= 0;
21596 /* Parse the template-argument-list itself. */
21597 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
21598 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
21599 arguments
= NULL_TREE
;
21601 arguments
= cp_parser_template_argument_list (parser
);
21602 /* Look for the `>' that ends the template-argument-list. If we find
21603 a '>>' instead, it's probably just a typo. */
21604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
21606 if (cxx_dialect
!= cxx98
)
21608 /* In C++0x, a `>>' in a template argument list or cast
21609 expression is considered to be two separate `>'
21610 tokens. So, change the current token to a `>', but don't
21611 consume it: it will be consumed later when the outer
21612 template argument list (or cast expression) is parsed.
21613 Note that this replacement of `>' for `>>' is necessary
21614 even if we are parsing tentatively: in the tentative
21615 case, after calling
21616 cp_parser_enclosed_template_argument_list we will always
21617 throw away all of the template arguments and the first
21618 closing `>', either because the template argument list
21619 was erroneous or because we are replacing those tokens
21620 with a CPP_TEMPLATE_ID token. The second `>' (which will
21621 not have been thrown away) is needed either to close an
21622 outer template argument list or to complete a new-style
21624 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21625 token
->type
= CPP_GREATER
;
21627 else if (!saved_greater_than_is_operator_p
)
21629 /* If we're in a nested template argument list, the '>>' has
21630 to be a typo for '> >'. We emit the error message, but we
21631 continue parsing and we push a '>' as next token, so that
21632 the argument list will be parsed correctly. Note that the
21633 global source location is still on the token before the
21634 '>>', so we need to say explicitly where we want it. */
21635 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21636 error_at (token
->location
, "%<>>%> should be %<> >%> "
21637 "within a nested template argument list");
21639 token
->type
= CPP_GREATER
;
21643 /* If this is not a nested template argument list, the '>>'
21644 is a typo for '>'. Emit an error message and continue.
21645 Same deal about the token location, but here we can get it
21646 right by consuming the '>>' before issuing the diagnostic. */
21647 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
21648 error_at (token
->location
,
21649 "spurious %<>>%>, use %<>%> to terminate "
21650 "a template argument list");
21654 cp_parser_skip_to_end_of_template_parameter_list (parser
);
21655 /* The `>' token might be a greater-than operator again now. */
21656 parser
->greater_than_is_operator_p
21657 = saved_greater_than_is_operator_p
;
21658 /* Restore the SAVED_SCOPE. */
21659 parser
->scope
= saved_scope
;
21660 parser
->qualifying_scope
= saved_qualifying_scope
;
21661 parser
->object_scope
= saved_object_scope
;
21662 cp_unevaluated_operand
= saved_unevaluated_operand
;
21663 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
21668 /* MEMBER_FUNCTION is a member function, or a friend. If default
21669 arguments, or the body of the function have not yet been parsed,
21673 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
21675 timevar_push (TV_PARSE_INMETH
);
21676 /* If this member is a template, get the underlying
21678 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
21679 member_function
= DECL_TEMPLATE_RESULT (member_function
);
21681 /* There should not be any class definitions in progress at this
21682 point; the bodies of members are only parsed outside of all class
21684 gcc_assert (parser
->num_classes_being_defined
== 0);
21685 /* While we're parsing the member functions we might encounter more
21686 classes. We want to handle them right away, but we don't want
21687 them getting mixed up with functions that are currently in the
21689 push_unparsed_function_queues (parser
);
21691 /* Make sure that any template parameters are in scope. */
21692 maybe_begin_member_template_processing (member_function
);
21694 /* If the body of the function has not yet been parsed, parse it
21696 if (DECL_PENDING_INLINE_P (member_function
))
21698 tree function_scope
;
21699 cp_token_cache
*tokens
;
21701 /* The function is no longer pending; we are processing it. */
21702 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
21703 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
21704 DECL_PENDING_INLINE_P (member_function
) = 0;
21706 /* If this is a local class, enter the scope of the containing
21708 function_scope
= current_function_decl
;
21709 if (function_scope
)
21710 push_function_context ();
21712 /* Push the body of the function onto the lexer stack. */
21713 cp_parser_push_lexer_for_tokens (parser
, tokens
);
21715 /* Let the front end know that we going to be defining this
21717 start_preparsed_function (member_function
, NULL_TREE
,
21718 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
21720 /* Don't do access checking if it is a templated function. */
21721 if (processing_template_decl
)
21722 push_deferring_access_checks (dk_no_check
);
21724 /* Now, parse the body of the function. */
21725 cp_parser_function_definition_after_declarator (parser
,
21726 /*inline_p=*/true);
21728 if (processing_template_decl
)
21729 pop_deferring_access_checks ();
21731 /* Leave the scope of the containing function. */
21732 if (function_scope
)
21733 pop_function_context ();
21734 cp_parser_pop_lexer (parser
);
21737 /* Remove any template parameters from the symbol table. */
21738 maybe_end_member_template_processing ();
21740 /* Restore the queue. */
21741 pop_unparsed_function_queues (parser
);
21742 timevar_pop (TV_PARSE_INMETH
);
21745 /* If DECL contains any default args, remember it on the unparsed
21746 functions queue. */
21749 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
21753 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
21755 probe
= TREE_CHAIN (probe
))
21756 if (TREE_PURPOSE (probe
))
21758 cp_default_arg_entry entry
= {current_class_type
, decl
};
21759 VEC_safe_push (cp_default_arg_entry
, gc
,
21760 unparsed_funs_with_default_args
, entry
);
21765 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21766 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21767 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21768 from the parameter-type-list. */
21771 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
21772 tree default_arg
, tree parmtype
)
21774 cp_token_cache
*tokens
;
21778 if (default_arg
== error_mark_node
)
21779 return error_mark_node
;
21781 /* Push the saved tokens for the default argument onto the parser's
21783 tokens
= DEFARG_TOKENS (default_arg
);
21784 cp_parser_push_lexer_for_tokens (parser
, tokens
);
21786 start_lambda_scope (decl
);
21788 /* Parse the default argument. */
21789 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
21790 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
21791 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21793 finish_lambda_scope ();
21795 if (parsed_arg
== error_mark_node
)
21796 cp_parser_skip_to_end_of_statement (parser
);
21798 if (!processing_template_decl
)
21800 /* In a non-template class, check conversions now. In a template,
21801 we'll wait and instantiate these as needed. */
21802 if (TREE_CODE (decl
) == PARM_DECL
)
21803 parsed_arg
= check_default_argument (parmtype
, parsed_arg
);
21806 int flags
= LOOKUP_IMPLICIT
;
21807 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
)
21808 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg
))
21809 flags
= LOOKUP_NORMAL
;
21810 parsed_arg
= digest_init_flags (TREE_TYPE (decl
), parsed_arg
, flags
);
21814 /* If the token stream has not been completely used up, then
21815 there was extra junk after the end of the default
21817 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
21819 if (TREE_CODE (decl
) == PARM_DECL
)
21820 cp_parser_error (parser
, "expected %<,%>");
21822 cp_parser_error (parser
, "expected %<;%>");
21825 /* Revert to the main lexer. */
21826 cp_parser_pop_lexer (parser
);
21831 /* FIELD is a non-static data member with an initializer which we saved for
21832 later; parse it now. */
21835 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
21839 push_unparsed_function_queues (parser
);
21840 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
21841 DECL_INITIAL (field
),
21843 pop_unparsed_function_queues (parser
);
21845 DECL_INITIAL (field
) = def
;
21848 /* FN is a FUNCTION_DECL which may contains a parameter with an
21849 unparsed DEFAULT_ARG. Parse the default args now. This function
21850 assumes that the current scope is the scope in which the default
21851 argument should be processed. */
21854 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
21856 bool saved_local_variables_forbidden_p
;
21857 tree parm
, parmdecl
;
21859 /* While we're parsing the default args, we might (due to the
21860 statement expression extension) encounter more classes. We want
21861 to handle them right away, but we don't want them getting mixed
21862 up with default args that are currently in the queue. */
21863 push_unparsed_function_queues (parser
);
21865 /* Local variable names (and the `this' keyword) may not appear
21866 in a default argument. */
21867 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21868 parser
->local_variables_forbidden_p
= true;
21870 push_defarg_context (fn
);
21872 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
21873 parmdecl
= DECL_ARGUMENTS (fn
);
21874 parm
&& parm
!= void_list_node
;
21875 parm
= TREE_CHAIN (parm
),
21876 parmdecl
= DECL_CHAIN (parmdecl
))
21878 tree default_arg
= TREE_PURPOSE (parm
);
21880 VEC(tree
,gc
) *insts
;
21887 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
21888 /* This can happen for a friend declaration for a function
21889 already declared with default arguments. */
21893 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
21895 TREE_VALUE (parm
));
21896 if (parsed_arg
== error_mark_node
)
21901 TREE_PURPOSE (parm
) = parsed_arg
;
21903 /* Update any instantiations we've already created. */
21904 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
21905 VEC_iterate (tree
, insts
, ix
, copy
); ix
++)
21906 TREE_PURPOSE (copy
) = parsed_arg
;
21909 pop_defarg_context ();
21911 /* Make sure no default arg is missing. */
21912 check_default_args (fn
);
21914 /* Restore the state of local_variables_forbidden_p. */
21915 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21917 /* Restore the queue. */
21918 pop_unparsed_function_queues (parser
);
21921 /* Parse the operand of `sizeof' (or a similar operator). Returns
21922 either a TYPE or an expression, depending on the form of the
21923 input. The KEYWORD indicates which kind of expression we have
21927 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
21929 tree expr
= NULL_TREE
;
21930 const char *saved_message
;
21932 bool saved_integral_constant_expression_p
;
21933 bool saved_non_integral_constant_expression_p
;
21934 bool pack_expansion_p
= false;
21936 /* Types cannot be defined in a `sizeof' expression. Save away the
21938 saved_message
= parser
->type_definition_forbidden_message
;
21939 /* And create the new one. */
21940 tmp
= concat ("types may not be defined in %<",
21941 IDENTIFIER_POINTER (ridpointers
[keyword
]),
21942 "%> expressions", NULL
);
21943 parser
->type_definition_forbidden_message
= tmp
;
21945 /* The restrictions on constant-expressions do not apply inside
21946 sizeof expressions. */
21947 saved_integral_constant_expression_p
21948 = parser
->integral_constant_expression_p
;
21949 saved_non_integral_constant_expression_p
21950 = parser
->non_integral_constant_expression_p
;
21951 parser
->integral_constant_expression_p
= false;
21953 /* If it's a `...', then we are computing the length of a parameter
21955 if (keyword
== RID_SIZEOF
21956 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21958 /* Consume the `...'. */
21959 cp_lexer_consume_token (parser
->lexer
);
21960 maybe_warn_variadic_templates ();
21962 /* Note that this is an expansion. */
21963 pack_expansion_p
= true;
21966 /* Do not actually evaluate the expression. */
21967 ++cp_unevaluated_operand
;
21968 ++c_inhibit_evaluation_warnings
;
21969 /* If it's a `(', then we might be looking at the type-id
21971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21974 bool saved_in_type_id_in_expr_p
;
21976 /* We can't be sure yet whether we're looking at a type-id or an
21978 cp_parser_parse_tentatively (parser
);
21979 /* Consume the `('. */
21980 cp_lexer_consume_token (parser
->lexer
);
21981 /* Parse the type-id. */
21982 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
21983 parser
->in_type_id_in_expr_p
= true;
21984 type
= cp_parser_type_id (parser
);
21985 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
21986 /* Now, look for the trailing `)'. */
21987 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21988 /* If all went well, then we're done. */
21989 if (cp_parser_parse_definitely (parser
))
21991 cp_decl_specifier_seq decl_specs
;
21993 /* Build a trivial decl-specifier-seq. */
21994 clear_decl_specs (&decl_specs
);
21995 decl_specs
.type
= type
;
21997 /* Call grokdeclarator to figure out what type this is. */
21998 expr
= grokdeclarator (NULL
,
22002 /*attrlist=*/NULL
);
22005 else if (pack_expansion_p
)
22006 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
22007 "%<sizeof...%> argument must be surrounded by parentheses");
22009 /* If the type-id production did not work out, then we must be
22010 looking at the unary-expression production. */
22012 expr
= cp_parser_unary_expression (parser
, /*address_p=*/false,
22013 /*cast_p=*/false, NULL
);
22015 if (pack_expansion_p
)
22016 /* Build a pack expansion. */
22017 expr
= make_pack_expansion (expr
);
22019 /* Go back to evaluating expressions. */
22020 --cp_unevaluated_operand
;
22021 --c_inhibit_evaluation_warnings
;
22023 /* Free the message we created. */
22025 /* And restore the old one. */
22026 parser
->type_definition_forbidden_message
= saved_message
;
22027 parser
->integral_constant_expression_p
22028 = saved_integral_constant_expression_p
;
22029 parser
->non_integral_constant_expression_p
22030 = saved_non_integral_constant_expression_p
;
22035 /* If the current declaration has no declarator, return true. */
22038 cp_parser_declares_only_class_p (cp_parser
*parser
)
22040 /* If the next token is a `;' or a `,' then there is no
22042 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22043 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
22046 /* Update the DECL_SPECS to reflect the storage class indicated by
22050 cp_parser_set_storage_class (cp_parser
*parser
,
22051 cp_decl_specifier_seq
*decl_specs
,
22053 location_t location
)
22055 cp_storage_class storage_class
;
22057 if (parser
->in_unbraced_linkage_specification_p
)
22059 error_at (location
, "invalid use of %qD in linkage specification",
22060 ridpointers
[keyword
]);
22063 else if (decl_specs
->storage_class
!= sc_none
)
22065 decl_specs
->conflicting_specifiers_p
= true;
22069 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
22070 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
))
22072 error_at (decl_specs
->locations
[ds_thread
],
22073 "%<__thread%> before %qD", ridpointers
[keyword
]);
22074 decl_specs
->locations
[ds_thread
] = 0;
22080 storage_class
= sc_auto
;
22083 storage_class
= sc_register
;
22086 storage_class
= sc_static
;
22089 storage_class
= sc_extern
;
22092 storage_class
= sc_mutable
;
22095 gcc_unreachable ();
22097 decl_specs
->storage_class
= storage_class
;
22098 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, location
);
22100 /* A storage class specifier cannot be applied alongside a typedef
22101 specifier. If there is a typedef specifier present then set
22102 conflicting_specifiers_p which will trigger an error later
22103 on in grokdeclarator. */
22104 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
22105 decl_specs
->conflicting_specifiers_p
= true;
22108 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22109 is true, the type is a class or enum definition. */
22112 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
22114 location_t location
,
22115 bool type_definition_p
)
22117 decl_specs
->any_specifiers_p
= true;
22119 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22120 (with, for example, in "typedef int wchar_t;") we remember that
22121 this is what happened. In system headers, we ignore these
22122 declarations so that G++ can work with system headers that are not
22124 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
22125 && !type_definition_p
22126 && (type_spec
== boolean_type_node
22127 || type_spec
== char16_type_node
22128 || type_spec
== char32_type_node
22129 || type_spec
== wchar_type_node
)
22130 && (decl_specs
->type
22131 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
22132 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
22133 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
22134 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
22136 decl_specs
->redefined_builtin_type
= type_spec
;
22137 set_and_check_decl_spec_loc (decl_specs
,
22138 ds_redefined_builtin_type_spec
,
22140 if (!decl_specs
->type
)
22142 decl_specs
->type
= type_spec
;
22143 decl_specs
->type_definition_p
= false;
22144 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, location
);
22147 else if (decl_specs
->type
)
22148 decl_specs
->multiple_types_p
= true;
22151 decl_specs
->type
= type_spec
;
22152 decl_specs
->type_definition_p
= type_definition_p
;
22153 decl_specs
->redefined_builtin_type
= NULL_TREE
;
22154 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, location
);
22158 /* Set the location for a declarator specifier and check if it is
22161 DECL_SPECS is the sequence of declarator specifiers onto which to
22164 DS is the single declarator specifier to set which location is to
22165 be set onto the existing sequence of declarators.
22167 LOCATION is the location for the declarator specifier to
22171 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
22172 cp_decl_spec ds
, source_location location
)
22174 gcc_assert (ds
< ds_last
);
22176 if (decl_specs
== NULL
)
22179 if (decl_specs
->locations
[ds
] == 0)
22180 decl_specs
->locations
[ds
] = location
;
22185 if (decl_specs
->locations
[ds_long_long
] != 0)
22186 error_at (location
,
22187 "%<long long long%> is too long for GCC");
22190 decl_specs
->locations
[ds_long_long
] = location
;
22191 pedwarn_cxx98 (location
,
22193 "ISO C++ 1998 does not support %<long long%>");
22198 static const char *const decl_spec_names
[] = {
22216 error_at (location
,
22217 "duplicate %qs", decl_spec_names
[ds
]);
22222 /* Return true iff the declarator specifier DS is present in the
22223 sequence of declarator specifiers DECL_SPECS. */
22226 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
22229 gcc_assert (ds
< ds_last
);
22231 if (decl_specs
== NULL
)
22234 return decl_specs
->locations
[ds
] != 0;
22237 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22238 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22241 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
22243 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
22246 /* Issue an error message indicating that TOKEN_DESC was expected.
22247 If KEYWORD is true, it indicated this function is called by
22248 cp_parser_require_keword and the required token can only be
22249 a indicated keyword. */
22252 cp_parser_required_error (cp_parser
*parser
,
22253 required_token token_desc
,
22256 switch (token_desc
)
22259 cp_parser_error (parser
, "expected %<new%>");
22262 cp_parser_error (parser
, "expected %<delete%>");
22265 cp_parser_error (parser
, "expected %<return%>");
22268 cp_parser_error (parser
, "expected %<while%>");
22271 cp_parser_error (parser
, "expected %<extern%>");
22273 case RT_STATIC_ASSERT
:
22274 cp_parser_error (parser
, "expected %<static_assert%>");
22277 cp_parser_error (parser
, "expected %<decltype%>");
22280 cp_parser_error (parser
, "expected %<operator%>");
22283 cp_parser_error (parser
, "expected %<class%>");
22286 cp_parser_error (parser
, "expected %<template%>");
22289 cp_parser_error (parser
, "expected %<namespace%>");
22292 cp_parser_error (parser
, "expected %<using%>");
22295 cp_parser_error (parser
, "expected %<asm%>");
22298 cp_parser_error (parser
, "expected %<try%>");
22301 cp_parser_error (parser
, "expected %<catch%>");
22304 cp_parser_error (parser
, "expected %<throw%>");
22307 cp_parser_error (parser
, "expected %<__label__%>");
22310 cp_parser_error (parser
, "expected %<@try%>");
22312 case RT_AT_SYNCHRONIZED
:
22313 cp_parser_error (parser
, "expected %<@synchronized%>");
22316 cp_parser_error (parser
, "expected %<@throw%>");
22318 case RT_TRANSACTION_ATOMIC
:
22319 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
22321 case RT_TRANSACTION_RELAXED
:
22322 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
22329 switch (token_desc
)
22332 cp_parser_error (parser
, "expected %<;%>");
22334 case RT_OPEN_PAREN
:
22335 cp_parser_error (parser
, "expected %<(%>");
22337 case RT_CLOSE_BRACE
:
22338 cp_parser_error (parser
, "expected %<}%>");
22340 case RT_OPEN_BRACE
:
22341 cp_parser_error (parser
, "expected %<{%>");
22343 case RT_CLOSE_SQUARE
:
22344 cp_parser_error (parser
, "expected %<]%>");
22346 case RT_OPEN_SQUARE
:
22347 cp_parser_error (parser
, "expected %<[%>");
22350 cp_parser_error (parser
, "expected %<,%>");
22353 cp_parser_error (parser
, "expected %<::%>");
22356 cp_parser_error (parser
, "expected %<<%>");
22359 cp_parser_error (parser
, "expected %<>%>");
22362 cp_parser_error (parser
, "expected %<=%>");
22365 cp_parser_error (parser
, "expected %<...%>");
22368 cp_parser_error (parser
, "expected %<*%>");
22371 cp_parser_error (parser
, "expected %<~%>");
22374 cp_parser_error (parser
, "expected %<:%>");
22376 case RT_COLON_SCOPE
:
22377 cp_parser_error (parser
, "expected %<:%> or %<::%>");
22379 case RT_CLOSE_PAREN
:
22380 cp_parser_error (parser
, "expected %<)%>");
22382 case RT_COMMA_CLOSE_PAREN
:
22383 cp_parser_error (parser
, "expected %<,%> or %<)%>");
22385 case RT_PRAGMA_EOL
:
22386 cp_parser_error (parser
, "expected end of line");
22389 cp_parser_error (parser
, "expected identifier");
22392 cp_parser_error (parser
, "expected selection-statement");
22394 case RT_INTERATION
:
22395 cp_parser_error (parser
, "expected iteration-statement");
22398 cp_parser_error (parser
, "expected jump-statement");
22401 cp_parser_error (parser
, "expected class-key");
22403 case RT_CLASS_TYPENAME_TEMPLATE
:
22404 cp_parser_error (parser
,
22405 "expected %<class%>, %<typename%>, or %<template%>");
22408 gcc_unreachable ();
22412 gcc_unreachable ();
22417 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22418 issue an error message indicating that TOKEN_DESC was expected.
22420 Returns the token consumed, if the token had the appropriate type.
22421 Otherwise, returns NULL. */
22424 cp_parser_require (cp_parser
* parser
,
22425 enum cpp_ttype type
,
22426 required_token token_desc
)
22428 if (cp_lexer_next_token_is (parser
->lexer
, type
))
22429 return cp_lexer_consume_token (parser
->lexer
);
22432 /* Output the MESSAGE -- unless we're parsing tentatively. */
22433 if (!cp_parser_simulate_error (parser
))
22434 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
22439 /* An error message is produced if the next token is not '>'.
22440 All further tokens are skipped until the desired token is
22441 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22444 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
22446 /* Current level of '< ... >'. */
22447 unsigned level
= 0;
22448 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22449 unsigned nesting_depth
= 0;
22451 /* Are we ready, yet? If not, issue error message. */
22452 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
22455 /* Skip tokens until the desired token is found. */
22458 /* Peek at the next token. */
22459 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
22462 if (!nesting_depth
)
22467 if (cxx_dialect
== cxx98
)
22468 /* C++0x views the `>>' operator as two `>' tokens, but
22471 else if (!nesting_depth
&& level
-- == 0)
22473 /* We've hit a `>>' where the first `>' closes the
22474 template argument list, and the second `>' is
22475 spurious. Just consume the `>>' and stop; we've
22476 already produced at least one error. */
22477 cp_lexer_consume_token (parser
->lexer
);
22480 /* Fall through for C++0x, so we handle the second `>' in
22484 if (!nesting_depth
&& level
-- == 0)
22486 /* We've reached the token we want, consume it and stop. */
22487 cp_lexer_consume_token (parser
->lexer
);
22492 case CPP_OPEN_PAREN
:
22493 case CPP_OPEN_SQUARE
:
22497 case CPP_CLOSE_PAREN
:
22498 case CPP_CLOSE_SQUARE
:
22499 if (nesting_depth
-- == 0)
22504 case CPP_PRAGMA_EOL
:
22505 case CPP_SEMICOLON
:
22506 case CPP_OPEN_BRACE
:
22507 case CPP_CLOSE_BRACE
:
22508 /* The '>' was probably forgotten, don't look further. */
22515 /* Consume this token. */
22516 cp_lexer_consume_token (parser
->lexer
);
22520 /* If the next token is the indicated keyword, consume it. Otherwise,
22521 issue an error message indicating that TOKEN_DESC was expected.
22523 Returns the token consumed, if the token had the appropriate type.
22524 Otherwise, returns NULL. */
22527 cp_parser_require_keyword (cp_parser
* parser
,
22529 required_token token_desc
)
22531 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
22533 if (token
&& token
->keyword
!= keyword
)
22535 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
22542 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22543 function-definition. */
22546 cp_parser_token_starts_function_definition_p (cp_token
* token
)
22548 return (/* An ordinary function-body begins with an `{'. */
22549 token
->type
== CPP_OPEN_BRACE
22550 /* A ctor-initializer begins with a `:'. */
22551 || token
->type
== CPP_COLON
22552 /* A function-try-block begins with `try'. */
22553 || token
->keyword
== RID_TRY
22554 /* A function-transaction-block begins with `__transaction_atomic'
22555 or `__transaction_relaxed'. */
22556 || token
->keyword
== RID_TRANSACTION_ATOMIC
22557 || token
->keyword
== RID_TRANSACTION_RELAXED
22558 /* The named return value extension begins with `return'. */
22559 || token
->keyword
== RID_RETURN
);
22562 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22566 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
22570 token
= cp_lexer_peek_token (parser
->lexer
);
22571 return (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_COLON
);
22574 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22575 C++0x) ending a template-argument. */
22578 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
22582 token
= cp_lexer_peek_token (parser
->lexer
);
22583 return (token
->type
== CPP_COMMA
22584 || token
->type
== CPP_GREATER
22585 || token
->type
== CPP_ELLIPSIS
22586 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
22589 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22590 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22593 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
22598 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22599 if (token
->type
== CPP_LESS
)
22601 /* Check for the sequence `<::' in the original code. It would be lexed as
22602 `[:', where `[' is a digraph, and there is no whitespace before
22604 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
22607 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
22608 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
22614 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22615 or none_type otherwise. */
22617 static enum tag_types
22618 cp_parser_token_is_class_key (cp_token
* token
)
22620 switch (token
->keyword
)
22625 return record_type
;
22634 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22637 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
22639 if (type
== error_mark_node
)
22641 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
22643 permerror (input_location
, "%qs tag used in naming %q#T",
22644 class_key
== union_type
? "union"
22645 : class_key
== record_type
? "struct" : "class",
22647 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
22648 "%q#T was previously declared here", type
);
22652 /* Issue an error message if DECL is redeclared with different
22653 access than its original declaration [class.access.spec/3].
22654 This applies to nested classes and nested class templates.
22658 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
22660 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
22663 if ((TREE_PRIVATE (decl
)
22664 != (current_access_specifier
== access_private_node
))
22665 || (TREE_PROTECTED (decl
)
22666 != (current_access_specifier
== access_protected_node
)))
22667 error_at (location
, "%qD redeclared with different access", decl
);
22670 /* Look for the `template' keyword, as a syntactic disambiguator.
22671 Return TRUE iff it is present, in which case it will be
22675 cp_parser_optional_template_keyword (cp_parser
*parser
)
22677 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22679 /* The `template' keyword can only be used within templates;
22680 outside templates the parser can always figure out what is a
22681 template and what is not. */
22682 if (!processing_template_decl
)
22684 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22685 error_at (token
->location
,
22686 "%<template%> (as a disambiguator) is only allowed "
22687 "within templates");
22688 /* If this part of the token stream is rescanned, the same
22689 error message would be generated. So, we purge the token
22690 from the stream. */
22691 cp_lexer_purge_token (parser
->lexer
);
22696 /* Consume the `template' keyword. */
22697 cp_lexer_consume_token (parser
->lexer
);
22705 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22706 set PARSER->SCOPE, and perform other related actions. */
22709 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
22712 struct tree_check
*check_value
;
22713 deferred_access_check
*chk
;
22714 VEC (deferred_access_check
,gc
) *checks
;
22716 /* Get the stored value. */
22717 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
22718 /* Perform any access checks that were deferred. */
22719 checks
= check_value
->checks
;
22722 FOR_EACH_VEC_ELT (deferred_access_check
, checks
, i
, chk
)
22723 perform_or_defer_access_check (chk
->binfo
,
22725 chk
->diag_decl
, tf_warning_or_error
);
22727 /* Set the scope from the stored value. */
22728 parser
->scope
= check_value
->value
;
22729 parser
->qualifying_scope
= check_value
->qualifying_scope
;
22730 parser
->object_scope
= NULL_TREE
;
22733 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22734 encounter the end of a block before what we were looking for. */
22737 cp_parser_cache_group (cp_parser
*parser
,
22738 enum cpp_ttype end
,
22743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22745 /* Abort a parenthesized expression if we encounter a semicolon. */
22746 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
22747 && token
->type
== CPP_SEMICOLON
)
22749 /* If we've reached the end of the file, stop. */
22750 if (token
->type
== CPP_EOF
22751 || (end
!= CPP_PRAGMA_EOL
22752 && token
->type
== CPP_PRAGMA_EOL
))
22754 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
22755 /* We've hit the end of an enclosing block, so there's been some
22756 kind of syntax error. */
22759 /* Consume the token. */
22760 cp_lexer_consume_token (parser
->lexer
);
22761 /* See if it starts a new group. */
22762 if (token
->type
== CPP_OPEN_BRACE
)
22764 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
22765 /* In theory this should probably check end == '}', but
22766 cp_parser_save_member_function_body needs it to exit
22767 after either '}' or ')' when called with ')'. */
22771 else if (token
->type
== CPP_OPEN_PAREN
)
22773 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
22774 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
22777 else if (token
->type
== CPP_PRAGMA
)
22778 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
22779 else if (token
->type
== end
)
22784 /* Like above, for caching a default argument or NSDMI. Both of these are
22785 terminated by a non-nested comma, but it can be unclear whether or not a
22786 comma is nested in a template argument list unless we do more parsing.
22787 In order to handle this ambiguity, when we encounter a ',' after a '<'
22788 we try to parse what follows as a parameter-declaration-list (in the
22789 case of a default argument) or a member-declarator (in the case of an
22790 NSDMI). If that succeeds, then we stop caching. */
22793 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
22795 unsigned depth
= 0;
22796 int maybe_template_id
= 0;
22797 cp_token
*first_token
;
22799 tree default_argument
;
22801 /* Add tokens until we have processed the entire default
22802 argument. We add the range [first_token, token). */
22803 first_token
= cp_lexer_peek_token (parser
->lexer
);
22804 if (first_token
->type
== CPP_OPEN_BRACE
)
22806 /* For list-initialization, this is straightforward. */
22807 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
22808 token
= cp_lexer_peek_token (parser
->lexer
);
22814 /* Peek at the next token. */
22815 token
= cp_lexer_peek_token (parser
->lexer
);
22816 /* What we do depends on what token we have. */
22817 switch (token
->type
)
22819 /* In valid code, a default argument must be
22820 immediately followed by a `,' `)', or `...'. */
22822 if (depth
== 0 && maybe_template_id
)
22824 /* If we've seen a '<', we might be in a
22825 template-argument-list. Until Core issue 325 is
22826 resolved, we don't know how this situation ought
22827 to be handled, so try to DTRT. We check whether
22828 what comes after the comma is a valid parameter
22829 declaration list. If it is, then the comma ends
22830 the default argument; otherwise the default
22831 argument continues. */
22832 bool error
= false;
22835 /* Set ITALP so cp_parser_parameter_declaration_list
22836 doesn't decide to commit to this parse. */
22837 bool saved_italp
= parser
->in_template_argument_list_p
;
22838 parser
->in_template_argument_list_p
= true;
22840 cp_parser_parse_tentatively (parser
);
22841 cp_lexer_consume_token (parser
->lexer
);
22845 int ctor_dtor_or_conv_p
;
22846 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22847 &ctor_dtor_or_conv_p
,
22848 /*parenthesized_p=*/NULL
,
22849 /*member_p=*/true);
22853 begin_scope (sk_function_parms
, NULL_TREE
);
22854 cp_parser_parameter_declaration_list (parser
, &error
);
22855 for (t
= current_binding_level
->names
; t
; t
= DECL_CHAIN (t
))
22856 pop_binding (DECL_NAME (t
), t
);
22859 if (!cp_parser_error_occurred (parser
) && !error
)
22861 cp_parser_abort_tentative_parse (parser
);
22863 parser
->in_template_argument_list_p
= saved_italp
;
22866 case CPP_CLOSE_PAREN
:
22868 /* If we run into a non-nested `;', `}', or `]',
22869 then the code is invalid -- but the default
22870 argument is certainly over. */
22871 case CPP_SEMICOLON
:
22872 case CPP_CLOSE_BRACE
:
22873 case CPP_CLOSE_SQUARE
:
22876 /* Update DEPTH, if necessary. */
22877 else if (token
->type
== CPP_CLOSE_PAREN
22878 || token
->type
== CPP_CLOSE_BRACE
22879 || token
->type
== CPP_CLOSE_SQUARE
)
22883 case CPP_OPEN_PAREN
:
22884 case CPP_OPEN_SQUARE
:
22885 case CPP_OPEN_BRACE
:
22891 /* This might be the comparison operator, or it might
22892 start a template argument list. */
22893 ++maybe_template_id
;
22897 if (cxx_dialect
== cxx98
)
22899 /* Fall through for C++0x, which treats the `>>'
22900 operator like two `>' tokens in certain
22906 /* This might be an operator, or it might close a
22907 template argument list. But if a previous '<'
22908 started a template argument list, this will have
22909 closed it, so we can't be in one anymore. */
22910 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
22911 if (maybe_template_id
< 0)
22912 maybe_template_id
= 0;
22916 /* If we run out of tokens, issue an error message. */
22918 case CPP_PRAGMA_EOL
:
22919 error_at (token
->location
, "file ends in default argument");
22925 /* In these cases, we should look for template-ids.
22926 For example, if the default argument is
22927 `X<int, double>()', we need to do name lookup to
22928 figure out whether or not `X' is a template; if
22929 so, the `,' does not end the default argument.
22931 That is not yet done. */
22938 /* If we've reached the end, stop. */
22942 /* Add the token to the token block. */
22943 token
= cp_lexer_consume_token (parser
->lexer
);
22946 /* Create a DEFAULT_ARG to represent the unparsed default
22948 default_argument
= make_node (DEFAULT_ARG
);
22949 DEFARG_TOKENS (default_argument
)
22950 = cp_token_cache_new (first_token
, token
);
22951 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
22953 return default_argument
;
22956 /* Begin parsing tentatively. We always save tokens while parsing
22957 tentatively so that if the tentative parsing fails we can restore the
22961 cp_parser_parse_tentatively (cp_parser
* parser
)
22963 /* Enter a new parsing context. */
22964 parser
->context
= cp_parser_context_new (parser
->context
);
22965 /* Begin saving tokens. */
22966 cp_lexer_save_tokens (parser
->lexer
);
22967 /* In order to avoid repetitive access control error messages,
22968 access checks are queued up until we are no longer parsing
22970 push_deferring_access_checks (dk_deferred
);
22973 /* Commit to the currently active tentative parse. */
22976 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
22978 cp_parser_context
*context
;
22981 /* Mark all of the levels as committed. */
22982 lexer
= parser
->lexer
;
22983 for (context
= parser
->context
; context
->next
; context
= context
->next
)
22985 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
22987 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
22988 while (!cp_lexer_saving_tokens (lexer
))
22989 lexer
= lexer
->next
;
22990 cp_lexer_commit_tokens (lexer
);
22994 /* Abort the currently active tentative parse. All consumed tokens
22995 will be rolled back, and no diagnostics will be issued. */
22998 cp_parser_abort_tentative_parse (cp_parser
* parser
)
23000 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
23001 || errorcount
> 0);
23002 cp_parser_simulate_error (parser
);
23003 /* Now, pretend that we want to see if the construct was
23004 successfully parsed. */
23005 cp_parser_parse_definitely (parser
);
23008 /* Stop parsing tentatively. If a parse error has occurred, restore the
23009 token stream. Otherwise, commit to the tokens we have consumed.
23010 Returns true if no error occurred; false otherwise. */
23013 cp_parser_parse_definitely (cp_parser
* parser
)
23015 bool error_occurred
;
23016 cp_parser_context
*context
;
23018 /* Remember whether or not an error occurred, since we are about to
23019 destroy that information. */
23020 error_occurred
= cp_parser_error_occurred (parser
);
23021 /* Remove the topmost context from the stack. */
23022 context
= parser
->context
;
23023 parser
->context
= context
->next
;
23024 /* If no parse errors occurred, commit to the tentative parse. */
23025 if (!error_occurred
)
23027 /* Commit to the tokens read tentatively, unless that was
23029 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
23030 cp_lexer_commit_tokens (parser
->lexer
);
23032 pop_to_parent_deferring_access_checks ();
23034 /* Otherwise, if errors occurred, roll back our state so that things
23035 are just as they were before we began the tentative parse. */
23038 cp_lexer_rollback_tokens (parser
->lexer
);
23039 pop_deferring_access_checks ();
23041 /* Add the context to the front of the free list. */
23042 context
->next
= cp_parser_context_free_list
;
23043 cp_parser_context_free_list
= context
;
23045 return !error_occurred
;
23048 /* Returns true if we are parsing tentatively and are not committed to
23049 this tentative parse. */
23052 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
23054 return (cp_parser_parsing_tentatively (parser
)
23055 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
23058 /* Returns nonzero iff an error has occurred during the most recent
23059 tentative parse. */
23062 cp_parser_error_occurred (cp_parser
* parser
)
23064 return (cp_parser_parsing_tentatively (parser
)
23065 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
23068 /* Returns nonzero if GNU extensions are allowed. */
23071 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
23073 return parser
->allow_gnu_extensions_p
;
23076 /* Objective-C++ Productions */
23079 /* Parse an Objective-C expression, which feeds into a primary-expression
23083 objc-message-expression
23084 objc-string-literal
23085 objc-encode-expression
23086 objc-protocol-expression
23087 objc-selector-expression
23089 Returns a tree representation of the expression. */
23092 cp_parser_objc_expression (cp_parser
* parser
)
23094 /* Try to figure out what kind of declaration is present. */
23095 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
23099 case CPP_OPEN_SQUARE
:
23100 return cp_parser_objc_message_expression (parser
);
23102 case CPP_OBJC_STRING
:
23103 kwd
= cp_lexer_consume_token (parser
->lexer
);
23104 return objc_build_string_object (kwd
->u
.value
);
23107 switch (kwd
->keyword
)
23109 case RID_AT_ENCODE
:
23110 return cp_parser_objc_encode_expression (parser
);
23112 case RID_AT_PROTOCOL
:
23113 return cp_parser_objc_protocol_expression (parser
);
23115 case RID_AT_SELECTOR
:
23116 return cp_parser_objc_selector_expression (parser
);
23122 error_at (kwd
->location
,
23123 "misplaced %<@%D%> Objective-C++ construct",
23125 cp_parser_skip_to_end_of_block_or_statement (parser
);
23128 return error_mark_node
;
23131 /* Parse an Objective-C message expression.
23133 objc-message-expression:
23134 [ objc-message-receiver objc-message-args ]
23136 Returns a representation of an Objective-C message. */
23139 cp_parser_objc_message_expression (cp_parser
* parser
)
23141 tree receiver
, messageargs
;
23143 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
23144 receiver
= cp_parser_objc_message_receiver (parser
);
23145 messageargs
= cp_parser_objc_message_args (parser
);
23146 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23148 return objc_build_message_expr (receiver
, messageargs
);
23151 /* Parse an objc-message-receiver.
23153 objc-message-receiver:
23155 simple-type-specifier
23157 Returns a representation of the type or expression. */
23160 cp_parser_objc_message_receiver (cp_parser
* parser
)
23164 /* An Objective-C message receiver may be either (1) a type
23165 or (2) an expression. */
23166 cp_parser_parse_tentatively (parser
);
23167 rcv
= cp_parser_expression (parser
, false, NULL
);
23169 if (cp_parser_parse_definitely (parser
))
23172 rcv
= cp_parser_simple_type_specifier (parser
,
23173 /*decl_specs=*/NULL
,
23174 CP_PARSER_FLAGS_NONE
);
23176 return objc_get_class_reference (rcv
);
23179 /* Parse the arguments and selectors comprising an Objective-C message.
23184 objc-selector-args , objc-comma-args
23186 objc-selector-args:
23187 objc-selector [opt] : assignment-expression
23188 objc-selector-args objc-selector [opt] : assignment-expression
23191 assignment-expression
23192 objc-comma-args , assignment-expression
23194 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23195 selector arguments and TREE_VALUE containing a list of comma
23199 cp_parser_objc_message_args (cp_parser
* parser
)
23201 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
23202 bool maybe_unary_selector_p
= true;
23203 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23205 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
23207 tree selector
= NULL_TREE
, arg
;
23209 if (token
->type
!= CPP_COLON
)
23210 selector
= cp_parser_objc_selector (parser
);
23212 /* Detect if we have a unary selector. */
23213 if (maybe_unary_selector_p
23214 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
23215 return build_tree_list (selector
, NULL_TREE
);
23217 maybe_unary_selector_p
= false;
23218 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23219 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
23222 = chainon (sel_args
,
23223 build_tree_list (selector
, arg
));
23225 token
= cp_lexer_peek_token (parser
->lexer
);
23228 /* Handle non-selector arguments, if any. */
23229 while (token
->type
== CPP_COMMA
)
23233 cp_lexer_consume_token (parser
->lexer
);
23234 arg
= cp_parser_assignment_expression (parser
, false, NULL
);
23237 = chainon (addl_args
,
23238 build_tree_list (NULL_TREE
, arg
));
23240 token
= cp_lexer_peek_token (parser
->lexer
);
23243 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
23245 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
23246 return build_tree_list (error_mark_node
, error_mark_node
);
23249 return build_tree_list (sel_args
, addl_args
);
23252 /* Parse an Objective-C encode expression.
23254 objc-encode-expression:
23255 @encode objc-typename
23257 Returns an encoded representation of the type argument. */
23260 cp_parser_objc_encode_expression (cp_parser
* parser
)
23265 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
23266 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23267 token
= cp_lexer_peek_token (parser
->lexer
);
23268 type
= complete_type (cp_parser_type_id (parser
));
23269 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23273 error_at (token
->location
,
23274 "%<@encode%> must specify a type as an argument");
23275 return error_mark_node
;
23278 /* This happens if we find @encode(T) (where T is a template
23279 typename or something dependent on a template typename) when
23280 parsing a template. In that case, we can't compile it
23281 immediately, but we rather create an AT_ENCODE_EXPR which will
23282 need to be instantiated when the template is used.
23284 if (dependent_type_p (type
))
23286 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
23287 TREE_READONLY (value
) = 1;
23291 return objc_build_encode_expr (type
);
23294 /* Parse an Objective-C @defs expression. */
23297 cp_parser_objc_defs_expression (cp_parser
*parser
)
23301 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
23302 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23303 name
= cp_parser_identifier (parser
);
23304 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23306 return objc_get_class_ivars (name
);
23309 /* Parse an Objective-C protocol expression.
23311 objc-protocol-expression:
23312 @protocol ( identifier )
23314 Returns a representation of the protocol expression. */
23317 cp_parser_objc_protocol_expression (cp_parser
* parser
)
23321 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
23322 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23323 proto
= cp_parser_identifier (parser
);
23324 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23326 return objc_build_protocol_expr (proto
);
23329 /* Parse an Objective-C selector expression.
23331 objc-selector-expression:
23332 @selector ( objc-method-signature )
23334 objc-method-signature:
23340 objc-selector-seq objc-selector :
23342 Returns a representation of the method selector. */
23345 cp_parser_objc_selector_expression (cp_parser
* parser
)
23347 tree sel_seq
= NULL_TREE
;
23348 bool maybe_unary_selector_p
= true;
23350 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23352 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
23353 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23354 token
= cp_lexer_peek_token (parser
->lexer
);
23356 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
23357 || token
->type
== CPP_SCOPE
)
23359 tree selector
= NULL_TREE
;
23361 if (token
->type
!= CPP_COLON
23362 || token
->type
== CPP_SCOPE
)
23363 selector
= cp_parser_objc_selector (parser
);
23365 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
23366 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
23368 /* Detect if we have a unary selector. */
23369 if (maybe_unary_selector_p
)
23371 sel_seq
= selector
;
23372 goto finish_selector
;
23376 cp_parser_error (parser
, "expected %<:%>");
23379 maybe_unary_selector_p
= false;
23380 token
= cp_lexer_consume_token (parser
->lexer
);
23382 if (token
->type
== CPP_SCOPE
)
23385 = chainon (sel_seq
,
23386 build_tree_list (selector
, NULL_TREE
));
23388 = chainon (sel_seq
,
23389 build_tree_list (NULL_TREE
, NULL_TREE
));
23393 = chainon (sel_seq
,
23394 build_tree_list (selector
, NULL_TREE
));
23396 token
= cp_lexer_peek_token (parser
->lexer
);
23400 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23402 return objc_build_selector_expr (loc
, sel_seq
);
23405 /* Parse a list of identifiers.
23407 objc-identifier-list:
23409 objc-identifier-list , identifier
23411 Returns a TREE_LIST of identifier nodes. */
23414 cp_parser_objc_identifier_list (cp_parser
* parser
)
23420 identifier
= cp_parser_identifier (parser
);
23421 if (identifier
== error_mark_node
)
23422 return error_mark_node
;
23424 list
= build_tree_list (NULL_TREE
, identifier
);
23425 sep
= cp_lexer_peek_token (parser
->lexer
);
23427 while (sep
->type
== CPP_COMMA
)
23429 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
23430 identifier
= cp_parser_identifier (parser
);
23431 if (identifier
== error_mark_node
)
23434 list
= chainon (list
, build_tree_list (NULL_TREE
,
23436 sep
= cp_lexer_peek_token (parser
->lexer
);
23442 /* Parse an Objective-C alias declaration.
23444 objc-alias-declaration:
23445 @compatibility_alias identifier identifier ;
23447 This function registers the alias mapping with the Objective-C front end.
23448 It returns nothing. */
23451 cp_parser_objc_alias_declaration (cp_parser
* parser
)
23455 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
23456 alias
= cp_parser_identifier (parser
);
23457 orig
= cp_parser_identifier (parser
);
23458 objc_declare_alias (alias
, orig
);
23459 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23462 /* Parse an Objective-C class forward-declaration.
23464 objc-class-declaration:
23465 @class objc-identifier-list ;
23467 The function registers the forward declarations with the Objective-C
23468 front end. It returns nothing. */
23471 cp_parser_objc_class_declaration (cp_parser
* parser
)
23473 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
23478 id
= cp_parser_identifier (parser
);
23479 if (id
== error_mark_node
)
23482 objc_declare_class (id
);
23484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23485 cp_lexer_consume_token (parser
->lexer
);
23489 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23492 /* Parse a list of Objective-C protocol references.
23494 objc-protocol-refs-opt:
23495 objc-protocol-refs [opt]
23497 objc-protocol-refs:
23498 < objc-identifier-list >
23500 Returns a TREE_LIST of identifiers, if any. */
23503 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
23505 tree protorefs
= NULL_TREE
;
23507 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
23509 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
23510 protorefs
= cp_parser_objc_identifier_list (parser
);
23511 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
23517 /* Parse a Objective-C visibility specification. */
23520 cp_parser_objc_visibility_spec (cp_parser
* parser
)
23522 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
23524 switch (vis
->keyword
)
23526 case RID_AT_PRIVATE
:
23527 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
23529 case RID_AT_PROTECTED
:
23530 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
23532 case RID_AT_PUBLIC
:
23533 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
23535 case RID_AT_PACKAGE
:
23536 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
23542 /* Eat '@private'/'@protected'/'@public'. */
23543 cp_lexer_consume_token (parser
->lexer
);
23546 /* Parse an Objective-C method type. Return 'true' if it is a class
23547 (+) method, and 'false' if it is an instance (-) method. */
23550 cp_parser_objc_method_type (cp_parser
* parser
)
23552 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
23558 /* Parse an Objective-C protocol qualifier. */
23561 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
23563 tree quals
= NULL_TREE
, node
;
23564 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23566 node
= token
->u
.value
;
23568 while (node
&& TREE_CODE (node
) == IDENTIFIER_NODE
23569 && (node
== ridpointers
[(int) RID_IN
]
23570 || node
== ridpointers
[(int) RID_OUT
]
23571 || node
== ridpointers
[(int) RID_INOUT
]
23572 || node
== ridpointers
[(int) RID_BYCOPY
]
23573 || node
== ridpointers
[(int) RID_BYREF
]
23574 || node
== ridpointers
[(int) RID_ONEWAY
]))
23576 quals
= tree_cons (NULL_TREE
, node
, quals
);
23577 cp_lexer_consume_token (parser
->lexer
);
23578 token
= cp_lexer_peek_token (parser
->lexer
);
23579 node
= token
->u
.value
;
23585 /* Parse an Objective-C typename. */
23588 cp_parser_objc_typename (cp_parser
* parser
)
23590 tree type_name
= NULL_TREE
;
23592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23594 tree proto_quals
, cp_type
= NULL_TREE
;
23596 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
23597 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
23599 /* An ObjC type name may consist of just protocol qualifiers, in which
23600 case the type shall default to 'id'. */
23601 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
23603 cp_type
= cp_parser_type_id (parser
);
23605 /* If the type could not be parsed, an error has already
23606 been produced. For error recovery, behave as if it had
23607 not been specified, which will use the default type
23609 if (cp_type
== error_mark_node
)
23611 cp_type
= NULL_TREE
;
23612 /* We need to skip to the closing parenthesis as
23613 cp_parser_type_id() does not seem to do it for
23615 cp_parser_skip_to_closing_parenthesis (parser
,
23616 /*recovering=*/true,
23617 /*or_comma=*/false,
23618 /*consume_paren=*/false);
23622 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23623 type_name
= build_tree_list (proto_quals
, cp_type
);
23629 /* Check to see if TYPE refers to an Objective-C selector name. */
23632 cp_parser_objc_selector_p (enum cpp_ttype type
)
23634 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
23635 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
23636 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
23637 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
23638 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
23641 /* Parse an Objective-C selector. */
23644 cp_parser_objc_selector (cp_parser
* parser
)
23646 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
23648 if (!cp_parser_objc_selector_p (token
->type
))
23650 error_at (token
->location
, "invalid Objective-C++ selector name");
23651 return error_mark_node
;
23654 /* C++ operator names are allowed to appear in ObjC selectors. */
23655 switch (token
->type
)
23657 case CPP_AND_AND
: return get_identifier ("and");
23658 case CPP_AND_EQ
: return get_identifier ("and_eq");
23659 case CPP_AND
: return get_identifier ("bitand");
23660 case CPP_OR
: return get_identifier ("bitor");
23661 case CPP_COMPL
: return get_identifier ("compl");
23662 case CPP_NOT
: return get_identifier ("not");
23663 case CPP_NOT_EQ
: return get_identifier ("not_eq");
23664 case CPP_OR_OR
: return get_identifier ("or");
23665 case CPP_OR_EQ
: return get_identifier ("or_eq");
23666 case CPP_XOR
: return get_identifier ("xor");
23667 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
23668 default: return token
->u
.value
;
23672 /* Parse an Objective-C params list. */
23675 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
23677 tree params
= NULL_TREE
;
23678 bool maybe_unary_selector_p
= true;
23679 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23681 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
23683 tree selector
= NULL_TREE
, type_name
, identifier
;
23684 tree parm_attr
= NULL_TREE
;
23686 if (token
->keyword
== RID_ATTRIBUTE
)
23689 if (token
->type
!= CPP_COLON
)
23690 selector
= cp_parser_objc_selector (parser
);
23692 /* Detect if we have a unary selector. */
23693 if (maybe_unary_selector_p
23694 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
23696 params
= selector
; /* Might be followed by attributes. */
23700 maybe_unary_selector_p
= false;
23701 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
23703 /* Something went quite wrong. There should be a colon
23704 here, but there is not. Stop parsing parameters. */
23707 type_name
= cp_parser_objc_typename (parser
);
23708 /* New ObjC allows attributes on parameters too. */
23709 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
23710 parm_attr
= cp_parser_attributes_opt (parser
);
23711 identifier
= cp_parser_identifier (parser
);
23715 objc_build_keyword_decl (selector
,
23720 token
= cp_lexer_peek_token (parser
->lexer
);
23723 if (params
== NULL_TREE
)
23725 cp_parser_error (parser
, "objective-c++ method declaration is expected");
23726 return error_mark_node
;
23729 /* We allow tail attributes for the method. */
23730 if (token
->keyword
== RID_ATTRIBUTE
)
23732 *attributes
= cp_parser_attributes_opt (parser
);
23733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
23734 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23736 cp_parser_error (parser
,
23737 "method attributes must be specified at the end");
23738 return error_mark_node
;
23741 if (params
== NULL_TREE
)
23743 cp_parser_error (parser
, "objective-c++ method declaration is expected");
23744 return error_mark_node
;
23749 /* Parse the non-keyword Objective-C params. */
23752 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
23755 tree params
= make_node (TREE_LIST
);
23756 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23757 *ellipsisp
= false; /* Initially, assume no ellipsis. */
23759 while (token
->type
== CPP_COMMA
)
23761 cp_parameter_declarator
*parmdecl
;
23764 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
23765 token
= cp_lexer_peek_token (parser
->lexer
);
23767 if (token
->type
== CPP_ELLIPSIS
)
23769 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
23771 token
= cp_lexer_peek_token (parser
->lexer
);
23775 /* TODO: parse attributes for tail parameters. */
23776 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
23777 parm
= grokdeclarator (parmdecl
->declarator
,
23778 &parmdecl
->decl_specifiers
,
23779 PARM
, /*initialized=*/0,
23780 /*attrlist=*/NULL
);
23782 chainon (params
, build_tree_list (NULL_TREE
, parm
));
23783 token
= cp_lexer_peek_token (parser
->lexer
);
23786 /* We allow tail attributes for the method. */
23787 if (token
->keyword
== RID_ATTRIBUTE
)
23789 if (*attributes
== NULL_TREE
)
23791 *attributes
= cp_parser_attributes_opt (parser
);
23792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
23793 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23797 /* We have an error, but parse the attributes, so that we can
23799 *attributes
= cp_parser_attributes_opt (parser
);
23801 cp_parser_error (parser
,
23802 "method attributes must be specified at the end");
23803 return error_mark_node
;
23809 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23812 cp_parser_objc_interstitial_code (cp_parser
* parser
)
23814 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23816 /* If the next token is `extern' and the following token is a string
23817 literal, then we have a linkage specification. */
23818 if (token
->keyword
== RID_EXTERN
23819 && cp_parser_is_pure_string_literal
23820 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
23821 cp_parser_linkage_specification (parser
);
23822 /* Handle #pragma, if any. */
23823 else if (token
->type
== CPP_PRAGMA
)
23824 cp_parser_pragma (parser
, pragma_external
);
23825 /* Allow stray semicolons. */
23826 else if (token
->type
== CPP_SEMICOLON
)
23827 cp_lexer_consume_token (parser
->lexer
);
23828 /* Mark methods as optional or required, when building protocols. */
23829 else if (token
->keyword
== RID_AT_OPTIONAL
)
23831 cp_lexer_consume_token (parser
->lexer
);
23832 objc_set_method_opt (true);
23834 else if (token
->keyword
== RID_AT_REQUIRED
)
23836 cp_lexer_consume_token (parser
->lexer
);
23837 objc_set_method_opt (false);
23839 else if (token
->keyword
== RID_NAMESPACE
)
23840 cp_parser_namespace_definition (parser
);
23841 /* Other stray characters must generate errors. */
23842 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
23844 cp_lexer_consume_token (parser
->lexer
);
23845 error ("stray %qs between Objective-C++ methods",
23846 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
23848 /* Finally, try to parse a block-declaration, or a function-definition. */
23850 cp_parser_block_declaration (parser
, /*statement_p=*/false);
23853 /* Parse a method signature. */
23856 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
23858 tree rettype
, kwdparms
, optparms
;
23859 bool ellipsis
= false;
23860 bool is_class_method
;
23862 is_class_method
= cp_parser_objc_method_type (parser
);
23863 rettype
= cp_parser_objc_typename (parser
);
23864 *attributes
= NULL_TREE
;
23865 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
23866 if (kwdparms
== error_mark_node
)
23867 return error_mark_node
;
23868 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
23869 if (optparms
== error_mark_node
)
23870 return error_mark_node
;
23872 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
23876 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
23879 cp_lexer_save_tokens (parser
->lexer
);
23880 tattr
= cp_parser_attributes_opt (parser
);
23881 gcc_assert (tattr
) ;
23883 /* If the attributes are followed by a method introducer, this is not allowed.
23884 Dump the attributes and flag the situation. */
23885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
23886 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
23889 /* Otherwise, the attributes introduce some interstitial code, possibly so
23890 rewind to allow that check. */
23891 cp_lexer_rollback_tokens (parser
->lexer
);
23895 /* Parse an Objective-C method prototype list. */
23898 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
23900 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23902 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
23904 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
23906 tree attributes
, sig
;
23907 bool is_class_method
;
23908 if (token
->type
== CPP_PLUS
)
23909 is_class_method
= true;
23911 is_class_method
= false;
23912 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
23913 if (sig
== error_mark_node
)
23915 cp_parser_skip_to_end_of_block_or_statement (parser
);
23916 token
= cp_lexer_peek_token (parser
->lexer
);
23919 objc_add_method_declaration (is_class_method
, sig
, attributes
);
23920 cp_parser_consume_semicolon_at_end_of_statement (parser
);
23922 else if (token
->keyword
== RID_AT_PROPERTY
)
23923 cp_parser_objc_at_property_declaration (parser
);
23924 else if (token
->keyword
== RID_ATTRIBUTE
23925 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
23926 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
23928 "prefix attributes are ignored for methods");
23930 /* Allow for interspersed non-ObjC++ code. */
23931 cp_parser_objc_interstitial_code (parser
);
23933 token
= cp_lexer_peek_token (parser
->lexer
);
23936 if (token
->type
!= CPP_EOF
)
23937 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
23939 cp_parser_error (parser
, "expected %<@end%>");
23941 objc_finish_interface ();
23944 /* Parse an Objective-C method definition list. */
23947 cp_parser_objc_method_definition_list (cp_parser
* parser
)
23949 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23951 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
23955 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
23958 tree sig
, attribute
;
23959 bool is_class_method
;
23960 if (token
->type
== CPP_PLUS
)
23961 is_class_method
= true;
23963 is_class_method
= false;
23964 push_deferring_access_checks (dk_deferred
);
23965 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
23966 if (sig
== error_mark_node
)
23968 cp_parser_skip_to_end_of_block_or_statement (parser
);
23969 token
= cp_lexer_peek_token (parser
->lexer
);
23972 objc_start_method_definition (is_class_method
, sig
, attribute
,
23975 /* For historical reasons, we accept an optional semicolon. */
23976 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23977 cp_lexer_consume_token (parser
->lexer
);
23979 ptk
= cp_lexer_peek_token (parser
->lexer
);
23980 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
23981 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
23983 perform_deferred_access_checks (tf_warning_or_error
);
23984 stop_deferring_access_checks ();
23985 meth
= cp_parser_function_definition_after_declarator (parser
,
23987 pop_deferring_access_checks ();
23988 objc_finish_method_definition (meth
);
23991 /* The following case will be removed once @synthesize is
23992 completely implemented. */
23993 else if (token
->keyword
== RID_AT_PROPERTY
)
23994 cp_parser_objc_at_property_declaration (parser
);
23995 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
23996 cp_parser_objc_at_synthesize_declaration (parser
);
23997 else if (token
->keyword
== RID_AT_DYNAMIC
)
23998 cp_parser_objc_at_dynamic_declaration (parser
);
23999 else if (token
->keyword
== RID_ATTRIBUTE
24000 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
24001 warning_at (token
->location
, OPT_Wattributes
,
24002 "prefix attributes are ignored for methods");
24004 /* Allow for interspersed non-ObjC++ code. */
24005 cp_parser_objc_interstitial_code (parser
);
24007 token
= cp_lexer_peek_token (parser
->lexer
);
24010 if (token
->type
!= CPP_EOF
)
24011 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
24013 cp_parser_error (parser
, "expected %<@end%>");
24015 objc_finish_implementation ();
24018 /* Parse Objective-C ivars. */
24021 cp_parser_objc_class_ivars (cp_parser
* parser
)
24023 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24025 if (token
->type
!= CPP_OPEN_BRACE
)
24026 return; /* No ivars specified. */
24028 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
24029 token
= cp_lexer_peek_token (parser
->lexer
);
24031 while (token
->type
!= CPP_CLOSE_BRACE
24032 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
24034 cp_decl_specifier_seq declspecs
;
24035 int decl_class_or_enum_p
;
24036 tree prefix_attributes
;
24038 cp_parser_objc_visibility_spec (parser
);
24040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
24043 cp_parser_decl_specifier_seq (parser
,
24044 CP_PARSER_FLAGS_OPTIONAL
,
24046 &decl_class_or_enum_p
);
24048 /* auto, register, static, extern, mutable. */
24049 if (declspecs
.storage_class
!= sc_none
)
24051 cp_parser_error (parser
, "invalid type for instance variable");
24052 declspecs
.storage_class
= sc_none
;
24056 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
24058 cp_parser_error (parser
, "invalid type for instance variable");
24059 declspecs
.locations
[ds_thread
] = 0;
24063 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
24065 cp_parser_error (parser
, "invalid type for instance variable");
24066 declspecs
.locations
[ds_thread
] = 0;
24069 prefix_attributes
= declspecs
.attributes
;
24070 declspecs
.attributes
= NULL_TREE
;
24072 /* Keep going until we hit the `;' at the end of the
24074 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24076 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
24077 cp_declarator
*declarator
= NULL
;
24078 int ctor_dtor_or_conv_p
;
24080 /* Check for a (possibly unnamed) bitfield declaration. */
24081 token
= cp_lexer_peek_token (parser
->lexer
);
24082 if (token
->type
== CPP_COLON
)
24085 if (token
->type
== CPP_NAME
24086 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
24089 /* Get the name of the bitfield. */
24090 declarator
= make_id_declarator (NULL_TREE
,
24091 cp_parser_identifier (parser
),
24095 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
24096 /* Get the width of the bitfield. */
24098 = cp_parser_constant_expression (parser
,
24099 /*allow_non_constant=*/false,
24104 /* Parse the declarator. */
24106 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24107 &ctor_dtor_or_conv_p
,
24108 /*parenthesized_p=*/NULL
,
24109 /*member_p=*/false);
24112 /* Look for attributes that apply to the ivar. */
24113 attributes
= cp_parser_attributes_opt (parser
);
24114 /* Remember which attributes are prefix attributes and
24116 first_attribute
= attributes
;
24117 /* Combine the attributes. */
24118 attributes
= chainon (prefix_attributes
, attributes
);
24121 /* Create the bitfield declaration. */
24122 decl
= grokbitfield (declarator
, &declspecs
,
24126 decl
= grokfield (declarator
, &declspecs
,
24127 NULL_TREE
, /*init_const_expr_p=*/false,
24128 NULL_TREE
, attributes
);
24130 /* Add the instance variable. */
24131 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
24132 objc_add_instance_variable (decl
);
24134 /* Reset PREFIX_ATTRIBUTES. */
24135 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24136 attributes
= TREE_CHAIN (attributes
);
24138 TREE_CHAIN (attributes
) = NULL_TREE
;
24140 token
= cp_lexer_peek_token (parser
->lexer
);
24142 if (token
->type
== CPP_COMMA
)
24144 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
24150 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24151 token
= cp_lexer_peek_token (parser
->lexer
);
24154 if (token
->keyword
== RID_AT_END
)
24155 cp_parser_error (parser
, "expected %<}%>");
24157 /* Do not consume the RID_AT_END, so it will be read again as terminating
24158 the @interface of @implementation. */
24159 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
24160 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
24162 /* For historical reasons, we accept an optional semicolon. */
24163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24164 cp_lexer_consume_token (parser
->lexer
);
24167 /* Parse an Objective-C protocol declaration. */
24170 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
24172 tree proto
, protorefs
;
24175 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
24176 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
24178 tok
= cp_lexer_peek_token (parser
->lexer
);
24179 error_at (tok
->location
, "identifier expected after %<@protocol%>");
24180 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24184 /* See if we have a forward declaration or a definition. */
24185 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24187 /* Try a forward declaration first. */
24188 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
24194 id
= cp_parser_identifier (parser
);
24195 if (id
== error_mark_node
)
24198 objc_declare_protocol (id
, attributes
);
24200 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24201 cp_lexer_consume_token (parser
->lexer
);
24205 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24208 /* Ok, we got a full-fledged definition (or at least should). */
24211 proto
= cp_parser_identifier (parser
);
24212 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
24213 objc_start_protocol (proto
, protorefs
, attributes
);
24214 cp_parser_objc_method_prototype_list (parser
);
24218 /* Parse an Objective-C superclass or category. */
24221 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
24224 tree
*categ
, bool *is_class_extension
)
24226 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
24228 *super
= *categ
= NULL_TREE
;
24229 *is_class_extension
= false;
24230 if (next
->type
== CPP_COLON
)
24232 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
24233 *super
= cp_parser_identifier (parser
);
24235 else if (next
->type
== CPP_OPEN_PAREN
)
24237 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
24239 /* If there is no category name, and this is an @interface, we
24240 have a class extension. */
24241 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24243 *categ
= NULL_TREE
;
24244 *is_class_extension
= true;
24247 *categ
= cp_parser_identifier (parser
);
24249 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24253 /* Parse an Objective-C class interface. */
24256 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
24258 tree name
, super
, categ
, protos
;
24259 bool is_class_extension
;
24261 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
24262 name
= cp_parser_identifier (parser
);
24263 if (name
== error_mark_node
)
24265 /* It's hard to recover because even if valid @interface stuff
24266 is to follow, we can't compile it (or validate it) if we
24267 don't even know which class it refers to. Let's assume this
24268 was a stray '@interface' token in the stream and skip it.
24272 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
24273 &is_class_extension
);
24274 protos
= cp_parser_objc_protocol_refs_opt (parser
);
24276 /* We have either a class or a category on our hands. */
24277 if (categ
|| is_class_extension
)
24278 objc_start_category_interface (name
, categ
, protos
, attributes
);
24281 objc_start_class_interface (name
, super
, protos
, attributes
);
24282 /* Handle instance variable declarations, if any. */
24283 cp_parser_objc_class_ivars (parser
);
24284 objc_continue_interface ();
24287 cp_parser_objc_method_prototype_list (parser
);
24290 /* Parse an Objective-C class implementation. */
24293 cp_parser_objc_class_implementation (cp_parser
* parser
)
24295 tree name
, super
, categ
;
24296 bool is_class_extension
;
24298 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
24299 name
= cp_parser_identifier (parser
);
24300 if (name
== error_mark_node
)
24302 /* It's hard to recover because even if valid @implementation
24303 stuff is to follow, we can't compile it (or validate it) if
24304 we don't even know which class it refers to. Let's assume
24305 this was a stray '@implementation' token in the stream and
24310 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
24311 &is_class_extension
);
24313 /* We have either a class or a category on our hands. */
24315 objc_start_category_implementation (name
, categ
);
24318 objc_start_class_implementation (name
, super
);
24319 /* Handle instance variable declarations, if any. */
24320 cp_parser_objc_class_ivars (parser
);
24321 objc_continue_implementation ();
24324 cp_parser_objc_method_definition_list (parser
);
24327 /* Consume the @end token and finish off the implementation. */
24330 cp_parser_objc_end_implementation (cp_parser
* parser
)
24332 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
24333 objc_finish_implementation ();
24336 /* Parse an Objective-C declaration. */
24339 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
24341 /* Try to figure out what kind of declaration is present. */
24342 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
24345 switch (kwd
->keyword
)
24350 error_at (kwd
->location
, "attributes may not be specified before"
24351 " the %<@%D%> Objective-C++ keyword",
24355 case RID_AT_IMPLEMENTATION
:
24356 warning_at (kwd
->location
, OPT_Wattributes
,
24357 "prefix attributes are ignored before %<@%D%>",
24364 switch (kwd
->keyword
)
24367 cp_parser_objc_alias_declaration (parser
);
24370 cp_parser_objc_class_declaration (parser
);
24372 case RID_AT_PROTOCOL
:
24373 cp_parser_objc_protocol_declaration (parser
, attributes
);
24375 case RID_AT_INTERFACE
:
24376 cp_parser_objc_class_interface (parser
, attributes
);
24378 case RID_AT_IMPLEMENTATION
:
24379 cp_parser_objc_class_implementation (parser
);
24382 cp_parser_objc_end_implementation (parser
);
24385 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
24387 cp_parser_skip_to_end_of_block_or_statement (parser
);
24391 /* Parse an Objective-C try-catch-finally statement.
24393 objc-try-catch-finally-stmt:
24394 @try compound-statement objc-catch-clause-seq [opt]
24395 objc-finally-clause [opt]
24397 objc-catch-clause-seq:
24398 objc-catch-clause objc-catch-clause-seq [opt]
24401 @catch ( objc-exception-declaration ) compound-statement
24403 objc-finally-clause:
24404 @finally compound-statement
24406 objc-exception-declaration:
24407 parameter-declaration
24410 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24414 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24415 for C. Keep them in sync. */
24418 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
24420 location_t location
;
24423 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
24424 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
24425 objc_maybe_warn_exceptions (location
);
24426 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24427 node, lest it get absorbed into the surrounding block. */
24428 stmt
= push_stmt_list ();
24429 cp_parser_compound_statement (parser
, NULL
, false, false);
24430 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
24432 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
24434 cp_parameter_declarator
*parm
;
24435 tree parameter_declaration
= error_mark_node
;
24436 bool seen_open_paren
= false;
24438 cp_lexer_consume_token (parser
->lexer
);
24439 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
24440 seen_open_paren
= true;
24441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24443 /* We have "@catch (...)" (where the '...' are literally
24444 what is in the code). Skip the '...'.
24445 parameter_declaration is set to NULL_TREE, and
24446 objc_being_catch_clauses() knows that that means
24448 cp_lexer_consume_token (parser
->lexer
);
24449 parameter_declaration
= NULL_TREE
;
24453 /* We have "@catch (NSException *exception)" or something
24454 like that. Parse the parameter declaration. */
24455 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
24457 parameter_declaration
= error_mark_node
;
24459 parameter_declaration
= grokdeclarator (parm
->declarator
,
24460 &parm
->decl_specifiers
,
24461 PARM
, /*initialized=*/0,
24462 /*attrlist=*/NULL
);
24464 if (seen_open_paren
)
24465 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24468 /* If there was no open parenthesis, we are recovering from
24469 an error, and we are trying to figure out what mistake
24470 the user has made. */
24472 /* If there is an immediate closing parenthesis, the user
24473 probably forgot the opening one (ie, they typed "@catch
24474 NSException *e)". Parse the closing parenthesis and keep
24476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24477 cp_lexer_consume_token (parser
->lexer
);
24479 /* If these is no immediate closing parenthesis, the user
24480 probably doesn't know that parenthesis are required at
24481 all (ie, they typed "@catch NSException *e"). So, just
24482 forget about the closing parenthesis and keep going. */
24484 objc_begin_catch_clause (parameter_declaration
);
24485 cp_parser_compound_statement (parser
, NULL
, false, false);
24486 objc_finish_catch_clause ();
24488 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
24490 cp_lexer_consume_token (parser
->lexer
);
24491 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
24492 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24493 node, lest it get absorbed into the surrounding block. */
24494 stmt
= push_stmt_list ();
24495 cp_parser_compound_statement (parser
, NULL
, false, false);
24496 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
24499 return objc_finish_try_stmt ();
24502 /* Parse an Objective-C synchronized statement.
24504 objc-synchronized-stmt:
24505 @synchronized ( expression ) compound-statement
24507 Returns NULL_TREE. */
24510 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
24512 location_t location
;
24515 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
24517 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
24518 objc_maybe_warn_exceptions (location
);
24519 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
24520 lock
= cp_parser_expression (parser
, false, NULL
);
24521 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24523 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24524 node, lest it get absorbed into the surrounding block. */
24525 stmt
= push_stmt_list ();
24526 cp_parser_compound_statement (parser
, NULL
, false, false);
24528 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
24531 /* Parse an Objective-C throw statement.
24534 @throw assignment-expression [opt] ;
24536 Returns a constructed '@throw' statement. */
24539 cp_parser_objc_throw_statement (cp_parser
*parser
)
24541 tree expr
= NULL_TREE
;
24542 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24544 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
24546 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24547 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
24549 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24551 return objc_build_throw_stmt (loc
, expr
);
24554 /* Parse an Objective-C statement. */
24557 cp_parser_objc_statement (cp_parser
* parser
)
24559 /* Try to figure out what kind of declaration is present. */
24560 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
24562 switch (kwd
->keyword
)
24565 return cp_parser_objc_try_catch_finally_statement (parser
);
24566 case RID_AT_SYNCHRONIZED
:
24567 return cp_parser_objc_synchronized_statement (parser
);
24569 return cp_parser_objc_throw_statement (parser
);
24571 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
24573 cp_parser_skip_to_end_of_block_or_statement (parser
);
24576 return error_mark_node
;
24579 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24580 look ahead to see if an objc keyword follows the attributes. This
24581 is to detect the use of prefix attributes on ObjC @interface and
24585 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
24587 cp_lexer_save_tokens (parser
->lexer
);
24588 *attrib
= cp_parser_attributes_opt (parser
);
24589 gcc_assert (*attrib
);
24590 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
24592 cp_lexer_commit_tokens (parser
->lexer
);
24595 cp_lexer_rollback_tokens (parser
->lexer
);
24599 /* This routine is a minimal replacement for
24600 c_parser_struct_declaration () used when parsing the list of
24601 types/names or ObjC++ properties. For example, when parsing the
24604 @property (readonly) int a, b, c;
24606 this function is responsible for parsing "int a, int b, int c" and
24607 returning the declarations as CHAIN of DECLs.
24609 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24610 similar parsing. */
24612 cp_parser_objc_struct_declaration (cp_parser
*parser
)
24614 tree decls
= NULL_TREE
;
24615 cp_decl_specifier_seq declspecs
;
24616 int decl_class_or_enum_p
;
24617 tree prefix_attributes
;
24619 cp_parser_decl_specifier_seq (parser
,
24620 CP_PARSER_FLAGS_NONE
,
24622 &decl_class_or_enum_p
);
24624 if (declspecs
.type
== error_mark_node
)
24625 return error_mark_node
;
24627 /* auto, register, static, extern, mutable. */
24628 if (declspecs
.storage_class
!= sc_none
)
24630 cp_parser_error (parser
, "invalid type for property");
24631 declspecs
.storage_class
= sc_none
;
24635 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
24637 cp_parser_error (parser
, "invalid type for property");
24638 declspecs
.locations
[ds_thread
] = 0;
24642 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
24644 cp_parser_error (parser
, "invalid type for property");
24645 declspecs
.locations
[ds_typedef
] = 0;
24648 prefix_attributes
= declspecs
.attributes
;
24649 declspecs
.attributes
= NULL_TREE
;
24651 /* Keep going until we hit the `;' at the end of the declaration. */
24652 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24654 tree attributes
, first_attribute
, decl
;
24655 cp_declarator
*declarator
;
24658 /* Parse the declarator. */
24659 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24660 NULL
, NULL
, false);
24662 /* Look for attributes that apply to the ivar. */
24663 attributes
= cp_parser_attributes_opt (parser
);
24664 /* Remember which attributes are prefix attributes and
24666 first_attribute
= attributes
;
24667 /* Combine the attributes. */
24668 attributes
= chainon (prefix_attributes
, attributes
);
24670 decl
= grokfield (declarator
, &declspecs
,
24671 NULL_TREE
, /*init_const_expr_p=*/false,
24672 NULL_TREE
, attributes
);
24674 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
24675 return error_mark_node
;
24677 /* Reset PREFIX_ATTRIBUTES. */
24678 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24679 attributes
= TREE_CHAIN (attributes
);
24681 TREE_CHAIN (attributes
) = NULL_TREE
;
24683 DECL_CHAIN (decl
) = decls
;
24686 token
= cp_lexer_peek_token (parser
->lexer
);
24687 if (token
->type
== CPP_COMMA
)
24689 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
24698 /* Parse an Objective-C @property declaration. The syntax is:
24700 objc-property-declaration:
24701 '@property' objc-property-attributes[opt] struct-declaration ;
24703 objc-property-attributes:
24704 '(' objc-property-attribute-list ')'
24706 objc-property-attribute-list:
24707 objc-property-attribute
24708 objc-property-attribute-list, objc-property-attribute
24710 objc-property-attribute
24711 'getter' = identifier
24712 'setter' = identifier
24721 @property NSString *name;
24722 @property (readonly) id object;
24723 @property (retain, nonatomic, getter=getTheName) id name;
24724 @property int a, b, c;
24726 PS: This function is identical to
24727 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24729 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
24731 /* The following variables hold the attributes of the properties as
24732 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24733 seen. When we see an attribute, we set them to 'true' (if they
24734 are boolean properties) or to the identifier (if they have an
24735 argument, ie, for getter and setter). Note that here we only
24736 parse the list of attributes, check the syntax and accumulate the
24737 attributes that we find. objc_add_property_declaration() will
24738 then process the information. */
24739 bool property_assign
= false;
24740 bool property_copy
= false;
24741 tree property_getter_ident
= NULL_TREE
;
24742 bool property_nonatomic
= false;
24743 bool property_readonly
= false;
24744 bool property_readwrite
= false;
24745 bool property_retain
= false;
24746 tree property_setter_ident
= NULL_TREE
;
24748 /* 'properties' is the list of properties that we read. Usually a
24749 single one, but maybe more (eg, in "@property int a, b, c;" there
24754 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24756 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
24758 /* Parse the optional attribute list... */
24759 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24762 cp_lexer_consume_token (parser
->lexer
);
24766 bool syntax_error
= false;
24767 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24770 if (token
->type
!= CPP_NAME
)
24772 cp_parser_error (parser
, "expected identifier");
24775 keyword
= C_RID_CODE (token
->u
.value
);
24776 cp_lexer_consume_token (parser
->lexer
);
24779 case RID_ASSIGN
: property_assign
= true; break;
24780 case RID_COPY
: property_copy
= true; break;
24781 case RID_NONATOMIC
: property_nonatomic
= true; break;
24782 case RID_READONLY
: property_readonly
= true; break;
24783 case RID_READWRITE
: property_readwrite
= true; break;
24784 case RID_RETAIN
: property_retain
= true; break;
24788 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
24790 if (keyword
== RID_GETTER
)
24791 cp_parser_error (parser
,
24792 "missing %<=%> (after %<getter%> attribute)");
24794 cp_parser_error (parser
,
24795 "missing %<=%> (after %<setter%> attribute)");
24796 syntax_error
= true;
24799 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
24800 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
24802 cp_parser_error (parser
, "expected identifier");
24803 syntax_error
= true;
24806 if (keyword
== RID_SETTER
)
24808 if (property_setter_ident
!= NULL_TREE
)
24810 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
24811 cp_lexer_consume_token (parser
->lexer
);
24814 property_setter_ident
= cp_parser_objc_selector (parser
);
24815 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
24816 cp_parser_error (parser
, "setter name must terminate with %<:%>");
24818 cp_lexer_consume_token (parser
->lexer
);
24822 if (property_getter_ident
!= NULL_TREE
)
24824 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
24825 cp_lexer_consume_token (parser
->lexer
);
24828 property_getter_ident
= cp_parser_objc_selector (parser
);
24832 cp_parser_error (parser
, "unknown property attribute");
24833 syntax_error
= true;
24840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24841 cp_lexer_consume_token (parser
->lexer
);
24846 /* FIXME: "@property (setter, assign);" will generate a spurious
24847 "error: expected ‘)’ before ‘,’ token". This is because
24848 cp_parser_require, unlike the C counterpart, will produce an
24849 error even if we are in error recovery. */
24850 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24852 cp_parser_skip_to_closing_parenthesis (parser
,
24853 /*recovering=*/true,
24854 /*or_comma=*/false,
24855 /*consume_paren=*/true);
24859 /* ... and the property declaration(s). */
24860 properties
= cp_parser_objc_struct_declaration (parser
);
24862 if (properties
== error_mark_node
)
24864 cp_parser_skip_to_end_of_statement (parser
);
24865 /* If the next token is now a `;', consume it. */
24866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24867 cp_lexer_consume_token (parser
->lexer
);
24871 if (properties
== NULL_TREE
)
24872 cp_parser_error (parser
, "expected identifier");
24875 /* Comma-separated properties are chained together in
24876 reverse order; add them one by one. */
24877 properties
= nreverse (properties
);
24879 for (; properties
; properties
= TREE_CHAIN (properties
))
24880 objc_add_property_declaration (loc
, copy_node (properties
),
24881 property_readonly
, property_readwrite
,
24882 property_assign
, property_retain
,
24883 property_copy
, property_nonatomic
,
24884 property_getter_ident
, property_setter_ident
);
24887 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24890 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24892 objc-synthesize-declaration:
24893 @synthesize objc-synthesize-identifier-list ;
24895 objc-synthesize-identifier-list:
24896 objc-synthesize-identifier
24897 objc-synthesize-identifier-list, objc-synthesize-identifier
24899 objc-synthesize-identifier
24901 identifier = identifier
24904 @synthesize MyProperty;
24905 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24907 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24908 for C. Keep them in sync.
24911 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
24913 tree list
= NULL_TREE
;
24915 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24917 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
24920 tree property
, ivar
;
24921 property
= cp_parser_identifier (parser
);
24922 if (property
== error_mark_node
)
24924 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24929 cp_lexer_consume_token (parser
->lexer
);
24930 ivar
= cp_parser_identifier (parser
);
24931 if (ivar
== error_mark_node
)
24933 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24939 list
= chainon (list
, build_tree_list (ivar
, property
));
24940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24941 cp_lexer_consume_token (parser
->lexer
);
24945 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24946 objc_add_synthesize_declaration (loc
, list
);
24949 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24951 objc-dynamic-declaration:
24952 @dynamic identifier-list ;
24955 @dynamic MyProperty;
24956 @dynamic MyProperty, AnotherProperty;
24958 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24959 for C. Keep them in sync.
24962 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
24964 tree list
= NULL_TREE
;
24966 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24968 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
24972 property
= cp_parser_identifier (parser
);
24973 if (property
== error_mark_node
)
24975 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24978 list
= chainon (list
, build_tree_list (NULL
, property
));
24979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24980 cp_lexer_consume_token (parser
->lexer
);
24984 cp_parser_consume_semicolon_at_end_of_statement (parser
);
24985 objc_add_dynamic_declaration (loc
, list
);
24989 /* OpenMP 2.5 parsing routines. */
24991 /* Returns name of the next clause.
24992 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24993 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24994 returned and the token is consumed. */
24996 static pragma_omp_clause
24997 cp_parser_omp_clause_name (cp_parser
*parser
)
24999 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
25001 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
25002 result
= PRAGMA_OMP_CLAUSE_IF
;
25003 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
25004 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
25005 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
25006 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
25007 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
25009 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
25010 const char *p
= IDENTIFIER_POINTER (id
);
25015 if (!strcmp ("collapse", p
))
25016 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
25017 else if (!strcmp ("copyin", p
))
25018 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
25019 else if (!strcmp ("copyprivate", p
))
25020 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
25023 if (!strcmp ("final", p
))
25024 result
= PRAGMA_OMP_CLAUSE_FINAL
;
25025 else if (!strcmp ("firstprivate", p
))
25026 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
25029 if (!strcmp ("lastprivate", p
))
25030 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
25033 if (!strcmp ("mergeable", p
))
25034 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
25037 if (!strcmp ("nowait", p
))
25038 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
25039 else if (!strcmp ("num_threads", p
))
25040 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
25043 if (!strcmp ("ordered", p
))
25044 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
25047 if (!strcmp ("reduction", p
))
25048 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
25051 if (!strcmp ("schedule", p
))
25052 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
25053 else if (!strcmp ("shared", p
))
25054 result
= PRAGMA_OMP_CLAUSE_SHARED
;
25057 if (!strcmp ("untied", p
))
25058 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
25063 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
25064 cp_lexer_consume_token (parser
->lexer
);
25069 /* Validate that a clause of the given type does not already exist. */
25072 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
25073 const char *name
, location_t location
)
25077 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
25078 if (OMP_CLAUSE_CODE (c
) == code
)
25080 error_at (location
, "too many %qs clauses", name
);
25088 variable-list , identifier
25090 In addition, we match a closing parenthesis. An opening parenthesis
25091 will have been consumed by the caller.
25093 If KIND is nonzero, create the appropriate node and install the decl
25094 in OMP_CLAUSE_DECL and add the node to the head of the list.
25096 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25097 return the list created. */
25100 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
25108 token
= cp_lexer_peek_token (parser
->lexer
);
25109 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
25110 /*check_dependency_p=*/true,
25111 /*template_p=*/NULL
,
25112 /*declarator_p=*/false,
25113 /*optional_p=*/false);
25114 if (name
== error_mark_node
)
25117 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
25118 if (decl
== error_mark_node
)
25119 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
25121 else if (kind
!= 0)
25123 tree u
= build_omp_clause (token
->location
, kind
);
25124 OMP_CLAUSE_DECL (u
) = decl
;
25125 OMP_CLAUSE_CHAIN (u
) = list
;
25129 list
= tree_cons (decl
, NULL_TREE
, list
);
25132 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25134 cp_lexer_consume_token (parser
->lexer
);
25137 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25141 /* Try to resync to an unnested comma. Copied from
25142 cp_parser_parenthesized_expression_list. */
25144 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
25145 /*recovering=*/true,
25147 /*consume_paren=*/true);
25155 /* Similarly, but expect leading and trailing parenthesis. This is a very
25156 common case for omp clauses. */
25159 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
25161 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25162 return cp_parser_omp_var_list_no_open (parser
, kind
, list
);
25167 collapse ( constant-expression ) */
25170 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
25176 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25177 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25180 num
= cp_parser_constant_expression (parser
, false, NULL
);
25182 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25183 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25184 /*or_comma=*/false,
25185 /*consume_paren=*/true);
25187 if (num
== error_mark_node
)
25189 num
= fold_non_dependent_expr (num
);
25190 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
25191 || !host_integerp (num
, 0)
25192 || (n
= tree_low_cst (num
, 0)) <= 0
25195 error_at (loc
, "collapse argument needs positive constant integer expression");
25199 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
25200 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
25201 OMP_CLAUSE_CHAIN (c
) = list
;
25202 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
25208 default ( shared | none ) */
25211 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
25213 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
25216 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25218 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
25220 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
25221 const char *p
= IDENTIFIER_POINTER (id
);
25226 if (strcmp ("none", p
) != 0)
25228 kind
= OMP_CLAUSE_DEFAULT_NONE
;
25232 if (strcmp ("shared", p
) != 0)
25234 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
25241 cp_lexer_consume_token (parser
->lexer
);
25246 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
25249 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25250 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25251 /*or_comma=*/false,
25252 /*consume_paren=*/true);
25254 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
25257 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
25258 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
25259 OMP_CLAUSE_CHAIN (c
) = list
;
25260 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
25266 final ( expression ) */
25269 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
25273 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25276 t
= cp_parser_condition (parser
);
25278 if (t
== error_mark_node
25279 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25280 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25281 /*or_comma=*/false,
25282 /*consume_paren=*/true);
25284 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
25286 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
25287 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
25288 OMP_CLAUSE_CHAIN (c
) = list
;
25294 if ( expression ) */
25297 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
25301 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25304 t
= cp_parser_condition (parser
);
25306 if (t
== error_mark_node
25307 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25308 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25309 /*or_comma=*/false,
25310 /*consume_paren=*/true);
25312 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
25314 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
25315 OMP_CLAUSE_IF_EXPR (c
) = t
;
25316 OMP_CLAUSE_CHAIN (c
) = list
;
25325 cp_parser_omp_clause_mergeable (cp_parser
*parser ATTRIBUTE_UNUSED
,
25326 tree list
, location_t location
)
25330 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
25333 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
25334 OMP_CLAUSE_CHAIN (c
) = list
;
25342 cp_parser_omp_clause_nowait (cp_parser
*parser ATTRIBUTE_UNUSED
,
25343 tree list
, location_t location
)
25347 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
25349 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
25350 OMP_CLAUSE_CHAIN (c
) = list
;
25355 num_threads ( expression ) */
25358 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
25359 location_t location
)
25363 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25366 t
= cp_parser_expression (parser
, false, NULL
);
25368 if (t
== error_mark_node
25369 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25370 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25371 /*or_comma=*/false,
25372 /*consume_paren=*/true);
25374 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
25375 "num_threads", location
);
25377 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
25378 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
25379 OMP_CLAUSE_CHAIN (c
) = list
;
25388 cp_parser_omp_clause_ordered (cp_parser
*parser ATTRIBUTE_UNUSED
,
25389 tree list
, location_t location
)
25393 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
25394 "ordered", location
);
25396 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
25397 OMP_CLAUSE_CHAIN (c
) = list
;
25402 reduction ( reduction-operator : variable-list )
25404 reduction-operator:
25405 One of: + * - & ^ | && ||
25409 reduction-operator:
25410 One of: + * - & ^ | && || min max */
25413 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
25415 enum tree_code code
;
25418 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25421 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
25433 code
= BIT_AND_EXPR
;
25436 code
= BIT_XOR_EXPR
;
25439 code
= BIT_IOR_EXPR
;
25442 code
= TRUTH_ANDIF_EXPR
;
25445 code
= TRUTH_ORIF_EXPR
;
25449 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
25450 const char *p
= IDENTIFIER_POINTER (id
);
25452 if (strcmp (p
, "min") == 0)
25457 if (strcmp (p
, "max") == 0)
25465 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25466 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25468 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25469 /*or_comma=*/false,
25470 /*consume_paren=*/true);
25473 cp_lexer_consume_token (parser
->lexer
);
25475 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
25478 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
);
25479 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
25480 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
25486 schedule ( schedule-kind )
25487 schedule ( schedule-kind , expression )
25490 static | dynamic | guided | runtime | auto */
25493 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
25497 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25500 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
25502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
25504 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
25505 const char *p
= IDENTIFIER_POINTER (id
);
25510 if (strcmp ("dynamic", p
) != 0)
25512 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
25516 if (strcmp ("guided", p
) != 0)
25518 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
25522 if (strcmp ("runtime", p
) != 0)
25524 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
25531 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
25532 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
25533 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
25534 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
25537 cp_lexer_consume_token (parser
->lexer
);
25539 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25542 cp_lexer_consume_token (parser
->lexer
);
25544 token
= cp_lexer_peek_token (parser
->lexer
);
25545 t
= cp_parser_assignment_expression (parser
, false, NULL
);
25547 if (t
== error_mark_node
)
25549 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
25550 error_at (token
->location
, "schedule %<runtime%> does not take "
25551 "a %<chunk_size%> parameter");
25552 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
25553 error_at (token
->location
, "schedule %<auto%> does not take "
25554 "a %<chunk_size%> parameter");
25556 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
25558 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
25561 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
25564 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
25565 OMP_CLAUSE_CHAIN (c
) = list
;
25569 cp_parser_error (parser
, "invalid schedule kind");
25571 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
25572 /*or_comma=*/false,
25573 /*consume_paren=*/true);
25581 cp_parser_omp_clause_untied (cp_parser
*parser ATTRIBUTE_UNUSED
,
25582 tree list
, location_t location
)
25586 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
25588 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
25589 OMP_CLAUSE_CHAIN (c
) = list
;
25593 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25594 is a bitmask in MASK. Return the list of clauses found; the result
25595 of clause default goes in *pdefault. */
25598 cp_parser_omp_all_clauses (cp_parser
*parser
, unsigned int mask
,
25599 const char *where
, cp_token
*pragma_tok
)
25601 tree clauses
= NULL
;
25603 cp_token
*token
= NULL
;
25605 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
25607 pragma_omp_clause c_kind
;
25608 const char *c_name
;
25609 tree prev
= clauses
;
25611 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25612 cp_lexer_consume_token (parser
->lexer
);
25614 token
= cp_lexer_peek_token (parser
->lexer
);
25615 c_kind
= cp_parser_omp_clause_name (parser
);
25620 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
25621 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
25623 c_name
= "collapse";
25625 case PRAGMA_OMP_CLAUSE_COPYIN
:
25626 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
25629 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
25630 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
25632 c_name
= "copyprivate";
25634 case PRAGMA_OMP_CLAUSE_DEFAULT
:
25635 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
25637 c_name
= "default";
25639 case PRAGMA_OMP_CLAUSE_FINAL
:
25640 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
25643 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
25644 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
25646 c_name
= "firstprivate";
25648 case PRAGMA_OMP_CLAUSE_IF
:
25649 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
25652 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
25653 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
25655 c_name
= "lastprivate";
25657 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
25658 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
25660 c_name
= "mergeable";
25662 case PRAGMA_OMP_CLAUSE_NOWAIT
:
25663 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
25666 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
25667 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
25669 c_name
= "num_threads";
25671 case PRAGMA_OMP_CLAUSE_ORDERED
:
25672 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
25674 c_name
= "ordered";
25676 case PRAGMA_OMP_CLAUSE_PRIVATE
:
25677 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
25679 c_name
= "private";
25681 case PRAGMA_OMP_CLAUSE_REDUCTION
:
25682 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
25683 c_name
= "reduction";
25685 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
25686 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
25688 c_name
= "schedule";
25690 case PRAGMA_OMP_CLAUSE_SHARED
:
25691 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
25695 case PRAGMA_OMP_CLAUSE_UNTIED
:
25696 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
25701 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
25705 if (((mask
>> c_kind
) & 1) == 0)
25707 /* Remove the invalid clause(s) from the list to avoid
25708 confusing the rest of the compiler. */
25710 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
25714 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
25715 return finish_omp_clauses (clauses
);
25722 In practice, we're also interested in adding the statement to an
25723 outer node. So it is convenient if we work around the fact that
25724 cp_parser_statement calls add_stmt. */
25727 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
25729 unsigned save
= parser
->in_statement
;
25731 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25732 This preserves the "not within loop or switch" style error messages
25733 for nonsense cases like
25739 if (parser
->in_statement
)
25740 parser
->in_statement
= IN_OMP_BLOCK
;
25746 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
25748 parser
->in_statement
= save
;
25752 cp_parser_omp_structured_block (cp_parser
*parser
)
25754 tree stmt
= begin_omp_structured_block ();
25755 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
25757 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
25759 cp_parser_end_omp_structured_block (parser
, save
);
25760 return finish_omp_structured_block (stmt
);
25764 # pragma omp atomic new-line
25768 x binop= expr | x++ | ++x | x-- | --x
25770 +, *, -, /, &, ^, |, <<, >>
25772 where x is an lvalue expression with scalar type.
25775 # pragma omp atomic new-line
25778 # pragma omp atomic read new-line
25781 # pragma omp atomic write new-line
25784 # pragma omp atomic update new-line
25787 # pragma omp atomic capture new-line
25790 # pragma omp atomic capture new-line
25798 expression-stmt | x = x binop expr
25800 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25802 { v = x; update-stmt; } | { update-stmt; v = x; }
25804 where x and v are lvalue expressions with scalar type. */
25807 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
25809 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
25810 tree rhs1
= NULL_TREE
, orig_lhs
;
25811 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
25812 bool structured_block
= false;
25814 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
25816 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
25817 const char *p
= IDENTIFIER_POINTER (id
);
25819 if (!strcmp (p
, "read"))
25820 code
= OMP_ATOMIC_READ
;
25821 else if (!strcmp (p
, "write"))
25823 else if (!strcmp (p
, "update"))
25825 else if (!strcmp (p
, "capture"))
25826 code
= OMP_ATOMIC_CAPTURE_NEW
;
25830 cp_lexer_consume_token (parser
->lexer
);
25832 cp_parser_require_pragma_eol (parser
, pragma_tok
);
25836 case OMP_ATOMIC_READ
:
25837 case NOP_EXPR
: /* atomic write */
25838 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
25839 /*cast_p=*/false, NULL
);
25840 if (v
== error_mark_node
)
25842 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
25844 if (code
== NOP_EXPR
)
25845 lhs
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
25847 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
25848 /*cast_p=*/false, NULL
);
25849 if (lhs
== error_mark_node
)
25851 if (code
== NOP_EXPR
)
25853 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25861 case OMP_ATOMIC_CAPTURE_NEW
:
25862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25864 cp_lexer_consume_token (parser
->lexer
);
25865 structured_block
= true;
25869 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
25870 /*cast_p=*/false, NULL
);
25871 if (v
== error_mark_node
)
25873 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
25881 lhs
= cp_parser_unary_expression (parser
, /*address_p=*/false,
25882 /*cast_p=*/false, NULL
);
25884 switch (TREE_CODE (lhs
))
25889 case POSTINCREMENT_EXPR
:
25890 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
25891 code
= OMP_ATOMIC_CAPTURE_OLD
;
25893 case PREINCREMENT_EXPR
:
25894 lhs
= TREE_OPERAND (lhs
, 0);
25895 opcode
= PLUS_EXPR
;
25896 rhs
= integer_one_node
;
25899 case POSTDECREMENT_EXPR
:
25900 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
25901 code
= OMP_ATOMIC_CAPTURE_OLD
;
25903 case PREDECREMENT_EXPR
:
25904 lhs
= TREE_OPERAND (lhs
, 0);
25905 opcode
= MINUS_EXPR
;
25906 rhs
= integer_one_node
;
25909 case COMPOUND_EXPR
:
25910 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
25911 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
25912 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
25913 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
25914 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25915 (TREE_OPERAND (lhs
, 1), 0), 0)))
25917 /* Undo effects of boolean_increment for post {in,de}crement. */
25918 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
25921 if (TREE_CODE (lhs
) == MODIFY_EXPR
25922 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
25924 /* Undo effects of boolean_increment. */
25925 if (integer_onep (TREE_OPERAND (lhs
, 1)))
25927 /* This is pre or post increment. */
25928 rhs
= TREE_OPERAND (lhs
, 1);
25929 lhs
= TREE_OPERAND (lhs
, 0);
25931 if (code
== OMP_ATOMIC_CAPTURE_NEW
25932 && !structured_block
25933 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
25934 code
= OMP_ATOMIC_CAPTURE_OLD
;
25940 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
25943 opcode
= MULT_EXPR
;
25946 opcode
= TRUNC_DIV_EXPR
;
25949 opcode
= PLUS_EXPR
;
25952 opcode
= MINUS_EXPR
;
25954 case CPP_LSHIFT_EQ
:
25955 opcode
= LSHIFT_EXPR
;
25957 case CPP_RSHIFT_EQ
:
25958 opcode
= RSHIFT_EXPR
;
25961 opcode
= BIT_AND_EXPR
;
25964 opcode
= BIT_IOR_EXPR
;
25967 opcode
= BIT_XOR_EXPR
;
25970 if (structured_block
|| code
== OMP_ATOMIC
)
25972 enum cp_parser_prec oprec
;
25974 cp_lexer_consume_token (parser
->lexer
);
25975 rhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
25976 /*cast_p=*/false, NULL
);
25977 if (rhs1
== error_mark_node
)
25979 token
= cp_lexer_peek_token (parser
->lexer
);
25980 switch (token
->type
)
25982 case CPP_SEMICOLON
:
25983 if (code
== OMP_ATOMIC_CAPTURE_NEW
)
25985 code
= OMP_ATOMIC_CAPTURE_OLD
;
25990 cp_lexer_consume_token (parser
->lexer
);
25993 cp_parser_error (parser
,
25994 "invalid form of %<#pragma omp atomic%>");
25997 opcode
= MULT_EXPR
;
26000 opcode
= TRUNC_DIV_EXPR
;
26003 opcode
= PLUS_EXPR
;
26006 opcode
= MINUS_EXPR
;
26009 opcode
= LSHIFT_EXPR
;
26012 opcode
= RSHIFT_EXPR
;
26015 opcode
= BIT_AND_EXPR
;
26018 opcode
= BIT_IOR_EXPR
;
26021 opcode
= BIT_XOR_EXPR
;
26024 cp_parser_error (parser
,
26025 "invalid operator for %<#pragma omp atomic%>");
26028 oprec
= TOKEN_PRECEDENCE (token
);
26029 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
26030 if (commutative_tree_code (opcode
))
26031 oprec
= (enum cp_parser_prec
) (oprec
- 1);
26032 cp_lexer_consume_token (parser
->lexer
);
26033 rhs
= cp_parser_binary_expression (parser
, false, false,
26035 if (rhs
== error_mark_node
)
26041 cp_parser_error (parser
,
26042 "invalid operator for %<#pragma omp atomic%>");
26045 cp_lexer_consume_token (parser
->lexer
);
26047 rhs
= cp_parser_expression (parser
, false, NULL
);
26048 if (rhs
== error_mark_node
)
26053 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
26055 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26057 v
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26058 /*cast_p=*/false, NULL
);
26059 if (v
== error_mark_node
)
26061 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
26063 lhs1
= cp_parser_unary_expression (parser
, /*address_p=*/false,
26064 /*cast_p=*/false, NULL
);
26065 if (lhs1
== error_mark_node
)
26068 if (structured_block
)
26070 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26071 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
26074 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
26075 if (!structured_block
)
26076 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26080 cp_parser_skip_to_end_of_block_or_statement (parser
);
26081 if (structured_block
)
26083 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26084 cp_lexer_consume_token (parser
->lexer
);
26085 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
26087 cp_parser_skip_to_end_of_block_or_statement (parser
);
26088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26089 cp_lexer_consume_token (parser
->lexer
);
26096 # pragma omp barrier new-line */
26099 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
26101 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26102 finish_omp_barrier ();
26106 # pragma omp critical [(name)] new-line
26107 structured-block */
26110 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
26112 tree stmt
, name
= NULL
;
26114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26116 cp_lexer_consume_token (parser
->lexer
);
26118 name
= cp_parser_identifier (parser
);
26120 if (name
== error_mark_node
26121 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26122 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26123 /*or_comma=*/false,
26124 /*consume_paren=*/true);
26125 if (name
== error_mark_node
)
26128 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26130 stmt
= cp_parser_omp_structured_block (parser
);
26131 return c_finish_omp_critical (input_location
, stmt
, name
);
26135 # pragma omp flush flush-vars[opt] new-line
26138 ( variable-list ) */
26141 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
26143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26144 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
26145 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26147 finish_omp_flush ();
26150 /* Helper function, to parse omp for increment expression. */
26153 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
26155 tree cond
= cp_parser_binary_expression (parser
, false, true,
26156 PREC_NOT_OPERATOR
, NULL
);
26157 if (cond
== error_mark_node
26158 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26160 cp_parser_skip_to_end_of_statement (parser
);
26161 return error_mark_node
;
26164 switch (TREE_CODE (cond
))
26172 return error_mark_node
;
26175 /* If decl is an iterator, preserve LHS and RHS of the relational
26176 expr until finish_omp_for. */
26178 && (type_dependent_expression_p (decl
)
26179 || CLASS_TYPE_P (TREE_TYPE (decl
))))
26182 return build_x_binary_op (input_location
, TREE_CODE (cond
),
26183 TREE_OPERAND (cond
, 0), ERROR_MARK
,
26184 TREE_OPERAND (cond
, 1), ERROR_MARK
,
26185 /*overload=*/NULL
, tf_warning_or_error
);
26188 /* Helper function, to parse omp for increment expression. */
26191 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
26193 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26199 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
26201 op
= (token
->type
== CPP_PLUS_PLUS
26202 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
26203 cp_lexer_consume_token (parser
->lexer
);
26204 lhs
= cp_parser_cast_expression (parser
, false, false, NULL
);
26206 return error_mark_node
;
26207 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
26210 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
26212 return error_mark_node
;
26214 token
= cp_lexer_peek_token (parser
->lexer
);
26215 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
26217 op
= (token
->type
== CPP_PLUS_PLUS
26218 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
26219 cp_lexer_consume_token (parser
->lexer
);
26220 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
26223 op
= cp_parser_assignment_operator_opt (parser
);
26224 if (op
== ERROR_MARK
)
26225 return error_mark_node
;
26227 if (op
!= NOP_EXPR
)
26229 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
26230 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
26231 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
26234 lhs
= cp_parser_binary_expression (parser
, false, false,
26235 PREC_ADDITIVE_EXPRESSION
, NULL
);
26236 token
= cp_lexer_peek_token (parser
->lexer
);
26237 decl_first
= lhs
== decl
;
26240 if (token
->type
!= CPP_PLUS
26241 && token
->type
!= CPP_MINUS
)
26242 return error_mark_node
;
26246 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
26247 cp_lexer_consume_token (parser
->lexer
);
26248 rhs
= cp_parser_binary_expression (parser
, false, false,
26249 PREC_ADDITIVE_EXPRESSION
, NULL
);
26250 token
= cp_lexer_peek_token (parser
->lexer
);
26251 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
26253 if (lhs
== NULL_TREE
)
26255 if (op
== PLUS_EXPR
)
26258 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
26259 tf_warning_or_error
);
26262 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
26263 ERROR_MARK
, NULL
, tf_warning_or_error
);
26266 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
26270 if (rhs
!= decl
|| op
== MINUS_EXPR
)
26271 return error_mark_node
;
26272 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
26275 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
26277 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
26280 /* Parse the restricted form of the for statement allowed by OpenMP. */
26283 cp_parser_omp_for_loop (cp_parser
*parser
, tree clauses
, tree
*par_clauses
)
26285 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
26286 tree real_decl
, initv
, condv
, incrv
, declv
;
26287 tree this_pre_body
, cl
;
26288 location_t loc_first
;
26289 bool collapse_err
= false;
26290 int i
, collapse
= 1, nbraces
= 0;
26291 VEC(tree
,gc
) *for_block
= make_tree_vector ();
26293 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
26294 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
26295 collapse
= tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl
), 0);
26297 gcc_assert (collapse
>= 1);
26299 declv
= make_tree_vec (collapse
);
26300 initv
= make_tree_vec (collapse
);
26301 condv
= make_tree_vec (collapse
);
26302 incrv
= make_tree_vec (collapse
);
26304 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
26306 for (i
= 0; i
< collapse
; i
++)
26308 int bracecount
= 0;
26309 bool add_private_clause
= false;
26312 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
26314 cp_parser_error (parser
, "for statement expected");
26317 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26319 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26322 init
= decl
= real_decl
= NULL
;
26323 this_pre_body
= push_stmt_list ();
26324 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26326 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26330 integer-type var = lb
26331 random-access-iterator-type var = lb
26332 pointer-type var = lb
26334 cp_decl_specifier_seq type_specifiers
;
26336 /* First, try to parse as an initialized declaration. See
26337 cp_parser_condition, from whence the bulk of this is copied. */
26339 cp_parser_parse_tentatively (parser
);
26340 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
26341 /*is_trailing_return=*/false,
26343 if (cp_parser_parse_definitely (parser
))
26345 /* If parsing a type specifier seq succeeded, then this
26346 MUST be a initialized declaration. */
26347 tree asm_specification
, attributes
;
26348 cp_declarator
*declarator
;
26350 declarator
= cp_parser_declarator (parser
,
26351 CP_PARSER_DECLARATOR_NAMED
,
26352 /*ctor_dtor_or_conv_p=*/NULL
,
26353 /*parenthesized_p=*/NULL
,
26354 /*member_p=*/false);
26355 attributes
= cp_parser_attributes_opt (parser
);
26356 asm_specification
= cp_parser_asm_specification_opt (parser
);
26358 if (declarator
== cp_error_declarator
)
26359 cp_parser_skip_to_end_of_statement (parser
);
26363 tree pushed_scope
, auto_node
;
26365 decl
= start_decl (declarator
, &type_specifiers
,
26366 SD_INITIALIZED
, attributes
,
26367 /*prefix_attributes=*/NULL_TREE
,
26370 auto_node
= type_uses_auto (TREE_TYPE (decl
));
26371 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
26373 if (cp_lexer_next_token_is (parser
->lexer
,
26375 error ("parenthesized initialization is not allowed in "
26376 "OpenMP %<for%> loop");
26378 /* Trigger an error. */
26379 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
26381 init
= error_mark_node
;
26382 cp_parser_skip_to_end_of_statement (parser
);
26384 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
26385 || type_dependent_expression_p (decl
)
26388 bool is_direct_init
, is_non_constant_init
;
26390 init
= cp_parser_initializer (parser
,
26392 &is_non_constant_init
);
26397 = do_auto_deduction (TREE_TYPE (decl
), init
,
26400 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
26401 && !type_dependent_expression_p (decl
))
26405 cp_finish_decl (decl
, init
, !is_non_constant_init
,
26407 LOOKUP_ONLYCONVERTING
);
26408 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
26410 VEC_safe_push (tree
, gc
, for_block
, this_pre_body
);
26414 init
= pop_stmt_list (this_pre_body
);
26415 this_pre_body
= NULL_TREE
;
26420 cp_lexer_consume_token (parser
->lexer
);
26421 init
= cp_parser_assignment_expression (parser
, false, NULL
);
26424 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
26425 init
= error_mark_node
;
26427 cp_finish_decl (decl
, NULL_TREE
,
26428 /*init_const_expr_p=*/false,
26430 LOOKUP_ONLYCONVERTING
);
26434 pop_scope (pushed_scope
);
26440 /* If parsing a type specifier sequence failed, then
26441 this MUST be a simple expression. */
26442 cp_parser_parse_tentatively (parser
);
26443 decl
= cp_parser_primary_expression (parser
, false, false,
26445 if (!cp_parser_error_occurred (parser
)
26448 && CLASS_TYPE_P (TREE_TYPE (decl
)))
26452 cp_parser_parse_definitely (parser
);
26453 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
26454 rhs
= cp_parser_assignment_expression (parser
, false, NULL
);
26455 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
26458 tf_warning_or_error
));
26459 add_private_clause
= true;
26464 cp_parser_abort_tentative_parse (parser
);
26465 init
= cp_parser_expression (parser
, false, NULL
);
26468 if (TREE_CODE (init
) == MODIFY_EXPR
26469 || TREE_CODE (init
) == MODOP_EXPR
)
26470 real_decl
= TREE_OPERAND (init
, 0);
26475 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26478 this_pre_body
= pop_stmt_list (this_pre_body
);
26482 pre_body
= push_stmt_list ();
26484 add_stmt (this_pre_body
);
26485 pre_body
= pop_stmt_list (pre_body
);
26488 pre_body
= this_pre_body
;
26493 if (par_clauses
!= NULL
&& real_decl
!= NULL_TREE
)
26496 for (c
= par_clauses
; *c
; )
26497 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
26498 && OMP_CLAUSE_DECL (*c
) == real_decl
)
26500 error_at (loc
, "iteration variable %qD"
26501 " should not be firstprivate", real_decl
);
26502 *c
= OMP_CLAUSE_CHAIN (*c
);
26504 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
26505 && OMP_CLAUSE_DECL (*c
) == real_decl
)
26507 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26508 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26509 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
26510 OMP_CLAUSE_DECL (l
) = real_decl
;
26511 OMP_CLAUSE_CHAIN (l
) = clauses
;
26512 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
26514 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
26515 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
26516 add_private_clause
= false;
26520 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
26521 && OMP_CLAUSE_DECL (*c
) == real_decl
)
26522 add_private_clause
= false;
26523 c
= &OMP_CLAUSE_CHAIN (*c
);
26527 if (add_private_clause
)
26530 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
26532 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
26533 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
26534 && OMP_CLAUSE_DECL (c
) == decl
)
26536 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
26537 && OMP_CLAUSE_DECL (c
) == decl
)
26538 error_at (loc
, "iteration variable %qD "
26539 "should not be firstprivate",
26541 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
26542 && OMP_CLAUSE_DECL (c
) == decl
)
26543 error_at (loc
, "iteration variable %qD should not be reduction",
26548 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
26549 OMP_CLAUSE_DECL (c
) = decl
;
26550 c
= finish_omp_clauses (c
);
26553 OMP_CLAUSE_CHAIN (c
) = clauses
;
26560 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26561 cond
= cp_parser_omp_for_cond (parser
, decl
);
26562 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26565 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
26567 /* If decl is an iterator, preserve the operator on decl
26568 until finish_omp_for. */
26570 && ((processing_template_decl
26571 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
26572 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
26573 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
26575 incr
= cp_parser_expression (parser
, false, NULL
);
26576 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
26577 SET_EXPR_LOCATION (incr
, input_location
);
26580 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
26581 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
26582 /*or_comma=*/false,
26583 /*consume_paren=*/true);
26585 TREE_VEC_ELT (declv
, i
) = decl
;
26586 TREE_VEC_ELT (initv
, i
) = init
;
26587 TREE_VEC_ELT (condv
, i
) = cond
;
26588 TREE_VEC_ELT (incrv
, i
) = incr
;
26590 if (i
== collapse
- 1)
26593 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26594 in between the collapsed for loops to be still considered perfectly
26595 nested. Hopefully the final version clarifies this.
26596 For now handle (multiple) {'s and empty statements. */
26597 cp_parser_parse_tentatively (parser
);
26600 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
26602 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26604 cp_lexer_consume_token (parser
->lexer
);
26607 else if (bracecount
26608 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26609 cp_lexer_consume_token (parser
->lexer
);
26612 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26613 error_at (loc
, "not enough collapsed for loops");
26614 collapse_err
= true;
26615 cp_parser_abort_tentative_parse (parser
);
26624 cp_parser_parse_definitely (parser
);
26625 nbraces
+= bracecount
;
26629 /* Note that we saved the original contents of this flag when we entered
26630 the structured block, and so we don't need to re-save it here. */
26631 parser
->in_statement
= IN_OMP_FOR
;
26633 /* Note that the grammar doesn't call for a structured block here,
26634 though the loop as a whole is a structured block. */
26635 body
= push_stmt_list ();
26636 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
26637 body
= pop_stmt_list (body
);
26639 if (declv
== NULL_TREE
)
26642 ret
= finish_omp_for (loc_first
, declv
, initv
, condv
, incrv
, body
,
26643 pre_body
, clauses
);
26647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26649 cp_lexer_consume_token (parser
->lexer
);
26652 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26653 cp_lexer_consume_token (parser
->lexer
);
26658 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26659 "collapsed loops not perfectly nested");
26661 collapse_err
= true;
26662 cp_parser_statement_seq_opt (parser
, NULL
);
26663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
26668 while (!VEC_empty (tree
, for_block
))
26669 add_stmt (pop_stmt_list (VEC_pop (tree
, for_block
)));
26670 release_tree_vector (for_block
);
26676 #pragma omp for for-clause[optseq] new-line
26679 #define OMP_FOR_CLAUSE_MASK \
26680 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26681 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26682 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26683 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26684 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26685 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26686 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26687 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26690 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
)
26692 tree clauses
, sb
, ret
;
26695 clauses
= cp_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
26696 "#pragma omp for", pragma_tok
);
26698 sb
= begin_omp_structured_block ();
26699 save
= cp_parser_begin_omp_structured_block (parser
);
26701 ret
= cp_parser_omp_for_loop (parser
, clauses
, NULL
);
26703 cp_parser_end_omp_structured_block (parser
, save
);
26704 add_stmt (finish_omp_structured_block (sb
));
26710 # pragma omp master new-line
26711 structured-block */
26714 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
26716 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26717 return c_finish_omp_master (input_location
,
26718 cp_parser_omp_structured_block (parser
));
26722 # pragma omp ordered new-line
26723 structured-block */
26726 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
26728 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26729 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26730 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
26736 { section-sequence }
26739 section-directive[opt] structured-block
26740 section-sequence section-directive structured-block */
26743 cp_parser_omp_sections_scope (cp_parser
*parser
)
26745 tree stmt
, substmt
;
26746 bool error_suppress
= false;
26749 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
26752 stmt
= push_stmt_list ();
26754 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
26758 substmt
= begin_omp_structured_block ();
26759 save
= cp_parser_begin_omp_structured_block (parser
);
26763 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
26765 tok
= cp_lexer_peek_token (parser
->lexer
);
26766 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
26768 if (tok
->type
== CPP_CLOSE_BRACE
)
26770 if (tok
->type
== CPP_EOF
)
26774 cp_parser_end_omp_structured_block (parser
, save
);
26775 substmt
= finish_omp_structured_block (substmt
);
26776 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
26777 add_stmt (substmt
);
26782 tok
= cp_lexer_peek_token (parser
->lexer
);
26783 if (tok
->type
== CPP_CLOSE_BRACE
)
26785 if (tok
->type
== CPP_EOF
)
26788 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
26790 cp_lexer_consume_token (parser
->lexer
);
26791 cp_parser_require_pragma_eol (parser
, tok
);
26792 error_suppress
= false;
26794 else if (!error_suppress
)
26796 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
26797 error_suppress
= true;
26800 substmt
= cp_parser_omp_structured_block (parser
);
26801 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
26802 add_stmt (substmt
);
26804 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
26806 substmt
= pop_stmt_list (stmt
);
26808 stmt
= make_node (OMP_SECTIONS
);
26809 TREE_TYPE (stmt
) = void_type_node
;
26810 OMP_SECTIONS_BODY (stmt
) = substmt
;
26817 # pragma omp sections sections-clause[optseq] newline
26820 #define OMP_SECTIONS_CLAUSE_MASK \
26821 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26822 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26823 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26824 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26825 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26828 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
)
26832 clauses
= cp_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
26833 "#pragma omp sections", pragma_tok
);
26835 ret
= cp_parser_omp_sections_scope (parser
);
26837 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
26843 # pragma parallel parallel-clause new-line
26844 # pragma parallel for parallel-for-clause new-line
26845 # pragma parallel sections parallel-sections-clause new-line */
26847 #define OMP_PARALLEL_CLAUSE_MASK \
26848 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26849 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26850 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26851 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26852 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26853 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26854 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26855 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26858 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
26860 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
26861 const char *p_name
= "#pragma omp parallel";
26862 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
26863 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
26865 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26867 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
26869 cp_lexer_consume_token (parser
->lexer
);
26870 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
26871 p_name
= "#pragma omp parallel for";
26872 mask
|= OMP_FOR_CLAUSE_MASK
;
26873 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
26875 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
26877 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
26878 const char *p
= IDENTIFIER_POINTER (id
);
26879 if (strcmp (p
, "sections") == 0)
26881 cp_lexer_consume_token (parser
->lexer
);
26882 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
26883 p_name
= "#pragma omp parallel sections";
26884 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
26885 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
26889 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
26890 block
= begin_omp_parallel ();
26891 save
= cp_parser_begin_omp_structured_block (parser
);
26895 case PRAGMA_OMP_PARALLEL
:
26896 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
26897 par_clause
= clauses
;
26900 case PRAGMA_OMP_PARALLEL_FOR
:
26901 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
26902 cp_parser_omp_for_loop (parser
, ws_clause
, &par_clause
);
26905 case PRAGMA_OMP_PARALLEL_SECTIONS
:
26906 c_split_parallel_clauses (loc
, clauses
, &par_clause
, &ws_clause
);
26907 stmt
= cp_parser_omp_sections_scope (parser
);
26909 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
26913 gcc_unreachable ();
26916 cp_parser_end_omp_structured_block (parser
, save
);
26917 stmt
= finish_omp_parallel (par_clause
, block
);
26918 if (p_kind
!= PRAGMA_OMP_PARALLEL
)
26919 OMP_PARALLEL_COMBINED (stmt
) = 1;
26924 # pragma omp single single-clause[optseq] new-line
26925 structured-block */
26927 #define OMP_SINGLE_CLAUSE_MASK \
26928 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26929 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26930 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26931 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26934 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
26936 tree stmt
= make_node (OMP_SINGLE
);
26937 TREE_TYPE (stmt
) = void_type_node
;
26939 OMP_SINGLE_CLAUSES (stmt
)
26940 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
26941 "#pragma omp single", pragma_tok
);
26942 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
26944 return add_stmt (stmt
);
26948 # pragma omp task task-clause[optseq] new-line
26949 structured-block */
26951 #define OMP_TASK_CLAUSE_MASK \
26952 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26953 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26954 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26955 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26956 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26957 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26958 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26959 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26962 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
26964 tree clauses
, block
;
26967 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
26968 "#pragma omp task", pragma_tok
);
26969 block
= begin_omp_task ();
26970 save
= cp_parser_begin_omp_structured_block (parser
);
26971 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
26972 cp_parser_end_omp_structured_block (parser
, save
);
26973 return finish_omp_task (clauses
, block
);
26977 # pragma omp taskwait new-line */
26980 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
26982 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26983 finish_omp_taskwait ();
26987 # pragma omp taskyield new-line */
26990 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
26992 cp_parser_require_pragma_eol (parser
, pragma_tok
);
26993 finish_omp_taskyield ();
26997 # pragma omp threadprivate (variable-list) */
27000 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
27004 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
27005 cp_parser_require_pragma_eol (parser
, pragma_tok
);
27007 finish_omp_threadprivate (vars
);
27010 /* Main entry point to OpenMP statement pragmas. */
27013 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
27017 switch (pragma_tok
->pragma_kind
)
27019 case PRAGMA_OMP_ATOMIC
:
27020 cp_parser_omp_atomic (parser
, pragma_tok
);
27022 case PRAGMA_OMP_CRITICAL
:
27023 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
27025 case PRAGMA_OMP_FOR
:
27026 stmt
= cp_parser_omp_for (parser
, pragma_tok
);
27028 case PRAGMA_OMP_MASTER
:
27029 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
27031 case PRAGMA_OMP_ORDERED
:
27032 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
27034 case PRAGMA_OMP_PARALLEL
:
27035 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
);
27037 case PRAGMA_OMP_SECTIONS
:
27038 stmt
= cp_parser_omp_sections (parser
, pragma_tok
);
27040 case PRAGMA_OMP_SINGLE
:
27041 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
27043 case PRAGMA_OMP_TASK
:
27044 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
27047 gcc_unreachable ();
27051 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
27054 /* Transactional Memory parsing routines. */
27056 /* Parse a transaction attribute.
27062 ??? Simplify this when C++0x bracket attributes are
27063 implemented properly. */
27066 cp_parser_txn_attribute_opt (cp_parser
*parser
)
27069 tree attr_name
, attr
= NULL
;
27071 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
27072 return cp_parser_attributes_opt (parser
);
27074 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
27076 cp_lexer_consume_token (parser
->lexer
);
27077 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
27080 token
= cp_lexer_peek_token (parser
->lexer
);
27081 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
27083 token
= cp_lexer_consume_token (parser
->lexer
);
27085 attr_name
= (token
->type
== CPP_KEYWORD
27086 /* For keywords, use the canonical spelling,
27087 not the parsed identifier. */
27088 ? ridpointers
[(int) token
->keyword
]
27090 attr
= build_tree_list (attr_name
, NULL_TREE
);
27093 cp_parser_error (parser
, "expected identifier");
27095 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
27097 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
27101 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27103 transaction-statement:
27104 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27106 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27110 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
27112 unsigned char old_in
= parser
->in_transaction
;
27113 unsigned char this_in
= 1, new_in
;
27115 tree stmt
, attrs
, noex
;
27117 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
27118 || keyword
== RID_TRANSACTION_RELAXED
);
27119 token
= cp_parser_require_keyword (parser
, keyword
,
27120 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
27121 : RT_TRANSACTION_RELAXED
));
27122 gcc_assert (token
!= NULL
);
27124 if (keyword
== RID_TRANSACTION_RELAXED
)
27125 this_in
|= TM_STMT_ATTR_RELAXED
;
27128 attrs
= cp_parser_txn_attribute_opt (parser
);
27130 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
27133 /* Parse a noexcept specification. */
27134 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
27136 /* Keep track if we're in the lexical scope of an outer transaction. */
27137 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
27139 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
27141 parser
->in_transaction
= new_in
;
27142 cp_parser_compound_statement (parser
, NULL
, false, false);
27143 parser
->in_transaction
= old_in
;
27145 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
27150 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27152 transaction-expression:
27153 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27154 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27158 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
27160 unsigned char old_in
= parser
->in_transaction
;
27161 unsigned char this_in
= 1;
27166 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
27167 || keyword
== RID_TRANSACTION_RELAXED
);
27170 error (keyword
== RID_TRANSACTION_RELAXED
27171 ? G_("%<__transaction_relaxed%> without transactional memory "
27173 : G_("%<__transaction_atomic%> without transactional memory "
27174 "support enabled"));
27176 token
= cp_parser_require_keyword (parser
, keyword
,
27177 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
27178 : RT_TRANSACTION_RELAXED
));
27179 gcc_assert (token
!= NULL
);
27181 if (keyword
== RID_TRANSACTION_RELAXED
)
27182 this_in
|= TM_STMT_ATTR_RELAXED
;
27184 /* Set this early. This might mean that we allow transaction_cancel in
27185 an expression that we find out later actually has to be a constexpr.
27186 However, we expect that cxx_constant_value will be able to deal with
27187 this; also, if the noexcept has no constexpr, then what we parse next
27188 really is a transaction's body. */
27189 parser
->in_transaction
= this_in
;
27191 /* Parse a noexcept specification. */
27192 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
27195 if (!noex
|| !noex_expr
27196 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
27198 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27200 expr
= cp_parser_expression (parser
, /*cast_p=*/false, NULL
);
27201 finish_parenthesized_expr (expr
);
27203 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27207 /* The only expression that is available got parsed for the noexcept
27208 already. noexcept is true then. */
27210 noex
= boolean_true_node
;
27213 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
27214 parser
->in_transaction
= old_in
;
27216 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
27217 return error_mark_node
;
27219 return (flag_tm
? expr
: error_mark_node
);
27222 /* Parse a function-transaction-block.
27224 function-transaction-block:
27225 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27227 __transaction_atomic txn-attribute[opt] function-try-block
27228 __transaction_relaxed ctor-initializer[opt] function-body
27229 __transaction_relaxed function-try-block
27233 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
27235 unsigned char old_in
= parser
->in_transaction
;
27236 unsigned char new_in
= 1;
27237 tree compound_stmt
, stmt
, attrs
;
27238 bool ctor_initializer_p
;
27241 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
27242 || keyword
== RID_TRANSACTION_RELAXED
);
27243 token
= cp_parser_require_keyword (parser
, keyword
,
27244 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
27245 : RT_TRANSACTION_RELAXED
));
27246 gcc_assert (token
!= NULL
);
27248 if (keyword
== RID_TRANSACTION_RELAXED
)
27249 new_in
|= TM_STMT_ATTR_RELAXED
;
27252 attrs
= cp_parser_txn_attribute_opt (parser
);
27254 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
27257 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
27259 parser
->in_transaction
= new_in
;
27261 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27262 ctor_initializer_p
= cp_parser_function_try_block (parser
);
27264 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
27265 (parser
, /*in_function_try_block=*/false);
27267 parser
->in_transaction
= old_in
;
27269 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
27271 return ctor_initializer_p
;
27274 /* Parse a __transaction_cancel statement.
27277 __transaction_cancel txn-attribute[opt] ;
27278 __transaction_cancel txn-attribute[opt] throw-expression ;
27280 ??? Cancel and throw is not yet implemented. */
27283 cp_parser_transaction_cancel (cp_parser
*parser
)
27286 bool is_outer
= false;
27289 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
27290 RT_TRANSACTION_CANCEL
);
27291 gcc_assert (token
!= NULL
);
27293 attrs
= cp_parser_txn_attribute_opt (parser
);
27295 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
27297 /* ??? Parse cancel-and-throw here. */
27299 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27303 error_at (token
->location
, "%<__transaction_cancel%> without "
27304 "transactional memory support enabled");
27305 return error_mark_node
;
27307 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
27309 error_at (token
->location
, "%<__transaction_cancel%> within a "
27310 "%<__transaction_relaxed%>");
27311 return error_mark_node
;
27315 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
27316 && !is_tm_may_cancel_outer (current_function_decl
))
27318 error_at (token
->location
, "outer %<__transaction_cancel%> not "
27319 "within outer %<__transaction_atomic%>");
27320 error_at (token
->location
,
27321 " or a %<transaction_may_cancel_outer%> function");
27322 return error_mark_node
;
27325 else if (parser
->in_transaction
== 0)
27327 error_at (token
->location
, "%<__transaction_cancel%> not within "
27328 "%<__transaction_atomic%>");
27329 return error_mark_node
;
27332 stmt
= build_tm_abort_call (token
->location
, is_outer
);
27341 static GTY (()) cp_parser
*the_parser
;
27344 /* Special handling for the first token or line in the file. The first
27345 thing in the file might be #pragma GCC pch_preprocess, which loads a
27346 PCH file, which is a GC collection point. So we need to handle this
27347 first pragma without benefit of an existing lexer structure.
27349 Always returns one token to the caller in *FIRST_TOKEN. This is
27350 either the true first token of the file, or the first token after
27351 the initial pragma. */
27354 cp_parser_initial_pragma (cp_token
*first_token
)
27358 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27359 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
27362 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27363 if (first_token
->type
== CPP_STRING
)
27365 name
= first_token
->u
.value
;
27367 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27368 if (first_token
->type
!= CPP_PRAGMA_EOL
)
27369 error_at (first_token
->location
,
27370 "junk at end of %<#pragma GCC pch_preprocess%>");
27373 error_at (first_token
->location
, "expected string literal");
27375 /* Skip to the end of the pragma. */
27376 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
27377 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27379 /* Now actually load the PCH file. */
27381 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
27383 /* Read one more token to return to our caller. We have to do this
27384 after reading the PCH file in, since its pointers have to be
27386 cp_lexer_get_preprocessor_token (NULL
, first_token
);
27389 /* Normal parsing of a pragma token. Here we can (and must) use the
27393 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
27395 cp_token
*pragma_tok
;
27398 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
27399 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
27400 parser
->lexer
->in_pragma
= true;
27402 id
= pragma_tok
->pragma_kind
;
27405 case PRAGMA_GCC_PCH_PREPROCESS
:
27406 error_at (pragma_tok
->location
,
27407 "%<#pragma GCC pch_preprocess%> must be first");
27410 case PRAGMA_OMP_BARRIER
:
27413 case pragma_compound
:
27414 cp_parser_omp_barrier (parser
, pragma_tok
);
27417 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
27418 "used in compound statements");
27425 case PRAGMA_OMP_FLUSH
:
27428 case pragma_compound
:
27429 cp_parser_omp_flush (parser
, pragma_tok
);
27432 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
27433 "used in compound statements");
27440 case PRAGMA_OMP_TASKWAIT
:
27443 case pragma_compound
:
27444 cp_parser_omp_taskwait (parser
, pragma_tok
);
27447 error_at (pragma_tok
->location
,
27448 "%<#pragma omp taskwait%> may only be "
27449 "used in compound statements");
27456 case PRAGMA_OMP_TASKYIELD
:
27459 case pragma_compound
:
27460 cp_parser_omp_taskyield (parser
, pragma_tok
);
27463 error_at (pragma_tok
->location
,
27464 "%<#pragma omp taskyield%> may only be "
27465 "used in compound statements");
27472 case PRAGMA_OMP_THREADPRIVATE
:
27473 cp_parser_omp_threadprivate (parser
, pragma_tok
);
27476 case PRAGMA_OMP_ATOMIC
:
27477 case PRAGMA_OMP_CRITICAL
:
27478 case PRAGMA_OMP_FOR
:
27479 case PRAGMA_OMP_MASTER
:
27480 case PRAGMA_OMP_ORDERED
:
27481 case PRAGMA_OMP_PARALLEL
:
27482 case PRAGMA_OMP_SECTIONS
:
27483 case PRAGMA_OMP_SINGLE
:
27484 case PRAGMA_OMP_TASK
:
27485 if (context
== pragma_external
)
27487 cp_parser_omp_construct (parser
, pragma_tok
);
27490 case PRAGMA_OMP_SECTION
:
27491 error_at (pragma_tok
->location
,
27492 "%<#pragma omp section%> may only be used in "
27493 "%<#pragma omp sections%> construct");
27497 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
27498 c_invoke_pragma_handler (id
);
27502 cp_parser_error (parser
, "expected declaration specifiers");
27506 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
27510 /* The interface the pragma parsers have to the lexer. */
27513 pragma_lex (tree
*value
)
27516 enum cpp_ttype ret
;
27518 tok
= cp_lexer_peek_token (the_parser
->lexer
);
27521 *value
= tok
->u
.value
;
27523 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
27525 else if (ret
== CPP_STRING
)
27526 *value
= cp_parser_string_literal (the_parser
, false, false);
27529 cp_lexer_consume_token (the_parser
->lexer
);
27530 if (ret
== CPP_KEYWORD
)
27538 /* External interface. */
27540 /* Parse one entire translation unit. */
27543 c_parse_file (void)
27545 static bool already_called
= false;
27547 if (already_called
)
27549 sorry ("inter-module optimizations not implemented for C++");
27552 already_called
= true;
27554 the_parser
= cp_parser_new ();
27555 push_deferring_access_checks (flag_access_control
27556 ? dk_no_deferred
: dk_no_check
);
27557 cp_parser_translation_unit (the_parser
);
27561 #include "gt-cp-parser.h"