2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 static cp_token eof_token
=
57 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant
{
63 /* floating-point literal */
67 /* %<__FUNCTION__%> */
69 /* %<__PRETTY_FUNCTION__%> */
77 /* %<typeid%> operator */
79 /* non-constant compound literals */
87 /* an array reference */
93 /* the address of a label */
107 /* calls to overloaded operators */
111 /* a comma operator */
113 /* a call to a constructor */
115 /* a transaction expression */
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error
{
125 /* is not a class or namespace */
127 /* is not a class, namespace, or enumeration */
131 /* The various kinds of required token */
132 enum required_token
{
134 RT_SEMICOLON
, /* ';' */
135 RT_OPEN_PAREN
, /* '(' */
136 RT_CLOSE_BRACE
, /* '}' */
137 RT_OPEN_BRACE
, /* '{' */
138 RT_CLOSE_SQUARE
, /* ']' */
139 RT_OPEN_SQUARE
, /* '[' */
143 RT_GREATER
, /* '>' */
145 RT_ELLIPSIS
, /* '...' */
149 RT_COLON_SCOPE
, /* ':' or '::' */
150 RT_CLOSE_PAREN
, /* ')' */
151 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
152 RT_PRAGMA_EOL
, /* end of line */
153 RT_NAME
, /* identifier */
155 /* The type is CPP_KEYWORD */
157 RT_DELETE
, /* delete */
158 RT_RETURN
, /* return */
159 RT_WHILE
, /* while */
160 RT_EXTERN
, /* extern */
161 RT_STATIC_ASSERT
, /* static_assert */
162 RT_DECLTYPE
, /* decltype */
163 RT_OPERATOR
, /* operator */
164 RT_CLASS
, /* class */
165 RT_TEMPLATE
, /* template */
166 RT_NAMESPACE
, /* namespace */
167 RT_USING
, /* using */
170 RT_CATCH
, /* catch */
171 RT_THROW
, /* throw */
172 RT_LABEL
, /* __label__ */
173 RT_AT_TRY
, /* @try */
174 RT_AT_SYNCHRONIZED
, /* @synchronized */
175 RT_AT_THROW
, /* @throw */
177 RT_SELECT
, /* selection-statement */
178 RT_ITERATION
, /* iteration-statement */
179 RT_JUMP
, /* jump-statement */
180 RT_CLASS_KEY
, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
195 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
197 saved (parser
->in_type_id_in_expr_p
)
198 { parser
->in_type_id_in_expr_p
= set
; }
199 ~type_id_in_expr_sentinel ()
200 { parser
->in_type_id_in_expr_p
= saved
; }
205 static cp_lexer
*cp_lexer_new_main
207 static cp_lexer
*cp_lexer_new_from_tokens
208 (cp_token_cache
*tokens
);
209 static void cp_lexer_destroy
211 static int cp_lexer_saving_tokens
213 static cp_token
*cp_lexer_token_at
214 (cp_lexer
*, cp_token_position
);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer
*, cp_token
*);
217 static inline cp_token
*cp_lexer_peek_token
219 static cp_token
*cp_lexer_peek_nth_token
220 (cp_lexer
*, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer
*, enum cpp_ttype
);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer
*, enum cpp_ttype
);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer
*, enum rid
);
227 static cp_token
*cp_lexer_consume_token
229 static void cp_lexer_purge_token
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer
*, cp_token_position
);
233 static void cp_lexer_save_tokens
235 static void cp_lexer_commit_tokens
237 static void cp_lexer_rollback_tokens
239 static void cp_lexer_print_token
240 (FILE *, cp_token
*);
241 static inline bool cp_lexer_debugging_p
243 static void cp_lexer_start_debugging
244 (cp_lexer
*) ATTRIBUTE_UNUSED
;
245 static void cp_lexer_stop_debugging
246 (cp_lexer
*) ATTRIBUTE_UNUSED
;
248 static cp_token_cache
*cp_token_cache_new
249 (cp_token
*, cp_token
*);
251 static void cp_parser_initial_pragma
254 static bool cp_parser_omp_declare_reduction_exprs
256 static void cp_finalize_oacc_routine
257 (cp_parser
*, tree
, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream
;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand
;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
279 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
280 cp_token
*start_token
, unsigned num
,
281 cp_token
*curr_token
)
283 unsigned i
, nprinted
;
287 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
293 num
= buffer
->length ();
295 if (start_token
== NULL
)
296 start_token
= buffer
->address ();
298 if (start_token
> buffer
->address ())
300 cp_lexer_print_token (file
, &(*buffer
)[0]);
301 fprintf (file
, " ... ");
306 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
308 if (token
== start_token
)
315 if (token
== curr_token
)
316 fprintf (file
, "[[");
318 cp_lexer_print_token (file
, token
);
320 if (token
== curr_token
)
321 fprintf (file
, "]]");
327 case CPP_CLOSE_BRACE
:
337 if (i
== num
&& i
< buffer
->length ())
339 fprintf (file
, " ... ");
340 cp_lexer_print_token (file
, &buffer
->last ());
343 fprintf (file
, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
350 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
352 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
356 debug (vec
<cp_token
, va_gc
> &ref
)
358 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
362 debug (vec
<cp_token
, va_gc
> *ptr
)
367 fprintf (stderr
, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
375 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
379 fprintf (file
, "%s: ", desc
);
380 print_node_brief (file
, "", t
, 0);
385 /* Dump parser context C to FILE. */
388 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
390 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
392 print_node_brief (file
, "", c
->object_type
, 0);
393 fprintf (file
, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
403 cp_parser_context
*c
;
405 fprintf (file
, "Parsing context stack:\n");
406 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
408 fprintf (file
, "\t#%u: ", i
);
409 cp_debug_print_context (file
, c
);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
420 fprintf (file
, "%s: true\n", desc
);
424 /* Print an unparsed function entry UF to FILE. */
427 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
430 cp_default_arg_entry
*default_arg_fn
;
433 fprintf (file
, "\tFunctions with default args:\n");
435 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
438 fprintf (file
, "\t\tClass type: ");
439 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
440 fprintf (file
, "\t\tDeclaration: ");
441 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
442 fprintf (file
, "\n");
445 fprintf (file
, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
449 print_node_brief (file
, "", fn
, 0);
452 fprintf (file
, "\n");
454 fprintf (file
, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
458 print_node_brief (file
, "", fn
, 0);
461 fprintf (file
, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
468 cp_debug_print_unparsed_queues (FILE *file
,
469 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
472 cp_unparsed_functions_entry
*uf
;
474 fprintf (file
, "Unparsed functions\n");
475 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
477 fprintf (file
, "#%u:\n", i
);
478 cp_debug_print_unparsed_function (file
, uf
);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
489 cp_token
*next_token
, *first_token
, *start_token
;
494 next_token
= parser
->lexer
->next_token
;
495 first_token
= parser
->lexer
->buffer
->address ();
496 start_token
= (next_token
> first_token
+ window_size
/ 2)
497 ? next_token
- window_size
/ 2
499 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
508 cp_debug_parser (FILE *file
, cp_parser
*parser
)
510 const size_t window_size
= 20;
512 expanded_location eloc
;
517 fprintf (file
, "Parser state\n\n");
518 fprintf (file
, "Number of tokens: %u\n",
519 vec_safe_length (parser
->lexer
->buffer
));
520 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
521 cp_debug_print_tree_if_set (file
, "Object scope",
522 parser
->object_scope
);
523 cp_debug_print_tree_if_set (file
, "Qualifying scope",
524 parser
->qualifying_scope
);
525 cp_debug_print_context_stack (file
, parser
->context
);
526 cp_debug_print_flag (file
, "Allow GNU extensions",
527 parser
->allow_gnu_extensions_p
);
528 cp_debug_print_flag (file
, "'>' token is greater-than",
529 parser
->greater_than_is_operator_p
);
530 cp_debug_print_flag (file
, "Default args allowed in current "
531 "parameter list", parser
->default_arg_ok_p
);
532 cp_debug_print_flag (file
, "Parsing integral constant-expression",
533 parser
->integral_constant_expression_p
);
534 cp_debug_print_flag (file
, "Allow non-constant expression in current "
535 "constant-expression",
536 parser
->allow_non_integral_constant_expression_p
);
537 cp_debug_print_flag (file
, "Seen non-constant expression",
538 parser
->non_integral_constant_expression_p
);
539 cp_debug_print_flag (file
, "Local names forbidden in current context",
540 (parser
->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN
));
542 cp_debug_print_flag (file
, "'this' forbidden in current context",
543 (parser
->local_variables_forbidden_p
545 cp_debug_print_flag (file
, "In unbraced linkage specification",
546 parser
->in_unbraced_linkage_specification_p
);
547 cp_debug_print_flag (file
, "Parsing a declarator",
548 parser
->in_declarator_p
);
549 cp_debug_print_flag (file
, "In template argument list",
550 parser
->in_template_argument_list_p
);
551 cp_debug_print_flag (file
, "Parsing an iteration statement",
552 parser
->in_statement
& IN_ITERATION_STMT
);
553 cp_debug_print_flag (file
, "Parsing a switch statement",
554 parser
->in_statement
& IN_SWITCH_STMT
);
555 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
556 parser
->in_statement
& IN_OMP_BLOCK
);
557 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
558 parser
->in_statement
& IN_OMP_FOR
);
559 cp_debug_print_flag (file
, "Parsing an if statement",
560 parser
->in_statement
& IN_IF_STMT
);
561 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
562 "context", parser
->in_type_id_in_expr_p
);
563 cp_debug_print_flag (file
, "String expressions should be translated "
564 "to execution character set",
565 parser
->translate_strings_p
);
566 cp_debug_print_flag (file
, "Parsing function body outside of a "
567 "local class", parser
->in_function_body
);
568 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
569 parser
->colon_corrects_to_scope_p
);
570 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
571 parser
->colon_doesnt_start_class_def_p
);
572 if (parser
->type_definition_forbidden_message
)
573 fprintf (file
, "Error message for forbidden type definitions: %s %s\n",
574 parser
->type_definition_forbidden_message
,
575 parser
->type_definition_forbidden_message_arg
576 ? parser
->type_definition_forbidden_message_arg
: "<none>");
577 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
578 fprintf (file
, "Number of class definitions in progress: %u\n",
579 parser
->num_classes_being_defined
);
580 fprintf (file
, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser
->num_template_parameter_lists
);
582 cp_debug_parser_tokens (file
, parser
, window_size
);
583 token
= parser
->lexer
->next_token
;
584 fprintf (file
, "Next token to parse:\n");
585 fprintf (file
, "\tToken: ");
586 cp_lexer_print_token (file
, token
);
587 eloc
= expand_location (token
->location
);
588 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
589 fprintf (file
, "\tLine: %d\n", eloc
.line
);
590 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
594 debug (cp_parser
&ref
)
596 cp_debug_parser (stderr
, &ref
);
600 debug (cp_parser
*ptr
)
605 fprintf (stderr
, "<nil>\n");
608 /* Allocate memory for a new lexer object and return it. */
611 cp_lexer_alloc (void)
615 c_common_no_more_pch ();
617 /* Allocate the memory. */
618 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
620 /* Initially we are not debugging. */
621 lexer
->debugging_p
= false;
623 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
625 /* Create the buffer. */
626 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 cp_lexer_new_main (void)
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token
);
646 lexer
= cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 lexer
->buffer
->quick_push (token
);
651 /* Get the remaining tokens from the preprocessor. */
652 while (token
.type
!= CPP_EOF
)
654 cp_lexer_get_preprocessor_token (lexer
, &token
);
655 vec_safe_push (lexer
->buffer
, token
);
658 lexer
->last_token
= lexer
->buffer
->address ()
659 + lexer
->buffer
->length ()
661 lexer
->next_token
= lexer
->buffer
->length ()
662 ? lexer
->buffer
->address ()
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
669 gcc_assert (!lexer
->next_token
->purged_p
);
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
677 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
679 cp_token
*first
= cache
->first
;
680 cp_token
*last
= cache
->last
;
681 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
683 /* We do not own the buffer. */
684 lexer
->buffer
= NULL
;
685 lexer
->next_token
= first
== last
? &eof_token
: first
;
686 lexer
->last_token
= last
;
688 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
690 /* Initially we are not debugging. */
691 lexer
->debugging_p
= false;
693 gcc_assert (!lexer
->next_token
->purged_p
);
697 /* Frees all resources associated with LEXER. */
700 cp_lexer_destroy (cp_lexer
*lexer
)
702 vec_free (lexer
->buffer
);
703 lexer
->saved_tokens
.release ();
707 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
708 be used. The point of this flag is to help the compiler to fold away calls
709 to cp_lexer_debugging_p within this source file at compile time, when the
710 lexer is not being debugged. */
712 #define LEXER_DEBUGGING_ENABLED_P false
714 /* Returns nonzero if debugging information should be output. */
717 cp_lexer_debugging_p (cp_lexer
*lexer
)
719 if (!LEXER_DEBUGGING_ENABLED_P
)
722 return lexer
->debugging_p
;
726 static inline cp_token_position
727 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
729 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
731 return lexer
->next_token
- previous_p
;
734 static inline cp_token
*
735 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
741 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
743 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
746 static inline cp_token_position
747 cp_lexer_previous_token_position (cp_lexer
*lexer
)
749 if (lexer
->next_token
== &eof_token
)
750 return lexer
->last_token
- 1;
752 return cp_lexer_token_position (lexer
, true);
755 static inline cp_token
*
756 cp_lexer_previous_token (cp_lexer
*lexer
)
758 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
760 /* Skip past purged tokens. */
763 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
767 return cp_lexer_token_at (lexer
, tp
);
770 /* nonzero if we are presently saving tokens. */
773 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
775 return lexer
->saved_tokens
.length () != 0;
778 /* Store the next token from the preprocessor in *TOKEN. Return true
779 if we reach EOF. If LEXER is NULL, assume we are handling an
780 initial #pragma pch_preprocess, and thus want the lexer to return
781 processed strings. */
784 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
786 static int is_extern_c
= 0;
788 /* Get a new token from the preprocessor. */
790 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
791 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
792 token
->keyword
= RID_MAX
;
793 token
->purged_p
= false;
794 token
->error_reported
= false;
796 /* On some systems, some header files are surrounded by an
797 implicit extern "C" block. Set a flag in the token if it
798 comes from such a header. */
799 is_extern_c
+= pending_lang_change
;
800 pending_lang_change
= 0;
801 token
->implicit_extern_c
= is_extern_c
> 0;
803 /* Check to see if this token is a keyword. */
804 if (token
->type
== CPP_NAME
)
806 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
808 /* Mark this token as a keyword. */
809 token
->type
= CPP_KEYWORD
;
810 /* Record which keyword. */
811 token
->keyword
= C_RID_CODE (token
->u
.value
);
815 if (warn_cxx11_compat
816 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
817 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
819 /* Warn about the C++0x keyword (but still treat it as
821 warning (OPT_Wc__11_compat
,
822 "identifier %qE is a keyword in C++11",
825 /* Clear out the C_RID_CODE so we don't warn about this
826 particular identifier-turned-keyword again. */
827 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
830 token
->keyword
= RID_MAX
;
833 else if (token
->type
== CPP_AT_NAME
)
835 /* This only happens in Objective-C++; it must be a keyword. */
836 token
->type
= CPP_KEYWORD
;
837 switch (C_RID_CODE (token
->u
.value
))
839 /* Replace 'class' with '@class', 'private' with '@private',
840 etc. This prevents confusion with the C++ keyword
841 'class', and makes the tokens consistent with other
842 Objective-C 'AT' keywords. For example '@class' is
843 reported as RID_AT_CLASS which is consistent with
844 '@synchronized', which is reported as
847 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
848 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
849 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
850 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
851 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
852 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
853 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
854 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
855 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
860 /* Update the globals input_location and the input file stack from TOKEN. */
862 cp_lexer_set_source_position_from_token (cp_token
*token
)
864 if (token
->type
!= CPP_EOF
)
866 input_location
= token
->location
;
870 /* Update the globals input_location and the input file stack from LEXER. */
872 cp_lexer_set_source_position (cp_lexer
*lexer
)
874 cp_token
*token
= cp_lexer_peek_token (lexer
);
875 cp_lexer_set_source_position_from_token (token
);
878 /* Return a pointer to the next token in the token stream, but do not
881 static inline cp_token
*
882 cp_lexer_peek_token (cp_lexer
*lexer
)
884 if (cp_lexer_debugging_p (lexer
))
886 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
887 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
888 putc ('\n', cp_lexer_debug_stream
);
890 return lexer
->next_token
;
893 /* Return true if the next token has the indicated TYPE. */
896 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
898 return cp_lexer_peek_token (lexer
)->type
== type
;
901 /* Return true if the next token does not have the indicated TYPE. */
904 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
906 return !cp_lexer_next_token_is (lexer
, type
);
909 /* Return true if the next token is the indicated KEYWORD. */
912 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
914 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
918 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
920 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
924 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
926 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
929 /* Return true if KEYWORD can start a decl-specifier. */
932 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
936 /* auto specifier: storage-class-specifier in C++,
937 simple-type-specifier in C++0x. */
939 /* Storage classes. */
945 /* Elaborated type specifiers. */
951 /* Simple type specifiers. */
966 /* GNU extensions. */
969 /* C++0x extensions. */
971 case RID_UNDERLYING_TYPE
:
976 if (keyword
>= RID_FIRST_INT_N
977 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
978 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
984 /* Return true if the next token is a keyword for a decl-specifier. */
987 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
991 token
= cp_lexer_peek_token (lexer
);
992 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
995 /* Returns TRUE iff the token T begins a decltype type. */
998 token_is_decltype (cp_token
*t
)
1000 return (t
->keyword
== RID_DECLTYPE
1001 || t
->type
== CPP_DECLTYPE
);
1004 /* Returns TRUE iff the next token begins a decltype type. */
1007 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1009 cp_token
*t
= cp_lexer_peek_token (lexer
);
1010 return token_is_decltype (t
);
1013 /* Called when processing a token with tree_check_value; perform or defer the
1014 associated checks and return the value. */
1017 saved_checks_value (struct tree_check
*check_value
)
1019 /* Perform any access checks that were deferred. */
1020 vec
<deferred_access_check
, va_gc
> *checks
;
1021 deferred_access_check
*chk
;
1022 checks
= check_value
->checks
;
1026 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1027 perform_or_defer_access_check (chk
->binfo
,
1029 chk
->diag_decl
, tf_warning_or_error
);
1031 /* Return the stored value. */
1032 return check_value
->value
;
1035 /* Return a pointer to the Nth token in the token stream. If N is 1,
1036 then this is precisely equivalent to cp_lexer_peek_token (except
1037 that it is not inline). One would like to disallow that case, but
1038 there is one case (cp_parser_nth_token_starts_template_id) where
1039 the caller passes a variable for N and it might be 1. */
1042 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1046 /* N is 1-based, not zero-based. */
1049 if (cp_lexer_debugging_p (lexer
))
1050 fprintf (cp_lexer_debug_stream
,
1051 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1054 token
= lexer
->next_token
;
1055 gcc_assert (!n
|| token
!= &eof_token
);
1059 if (token
== lexer
->last_token
)
1065 if (!token
->purged_p
)
1069 if (cp_lexer_debugging_p (lexer
))
1071 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1072 putc ('\n', cp_lexer_debug_stream
);
1078 /* Return the next token, and advance the lexer's next_token pointer
1079 to point to the next non-purged token. */
1082 cp_lexer_consume_token (cp_lexer
* lexer
)
1084 cp_token
*token
= lexer
->next_token
;
1086 gcc_assert (token
!= &eof_token
);
1087 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1091 lexer
->next_token
++;
1092 if (lexer
->next_token
== lexer
->last_token
)
1094 lexer
->next_token
= &eof_token
;
1099 while (lexer
->next_token
->purged_p
);
1101 cp_lexer_set_source_position_from_token (token
);
1103 /* Provide debugging output. */
1104 if (cp_lexer_debugging_p (lexer
))
1106 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1107 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1108 putc ('\n', cp_lexer_debug_stream
);
1114 /* Permanently remove the next token from the token stream, and
1115 advance the next_token pointer to refer to the next non-purged
1119 cp_lexer_purge_token (cp_lexer
*lexer
)
1121 cp_token
*tok
= lexer
->next_token
;
1123 gcc_assert (tok
!= &eof_token
);
1124 tok
->purged_p
= true;
1125 tok
->location
= UNKNOWN_LOCATION
;
1126 tok
->u
.value
= NULL_TREE
;
1127 tok
->keyword
= RID_MAX
;
1132 if (tok
== lexer
->last_token
)
1138 while (tok
->purged_p
);
1139 lexer
->next_token
= tok
;
1142 /* Permanently remove all tokens after TOK, up to, but not
1143 including, the token that will be returned next by
1144 cp_lexer_peek_token. */
1147 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1149 cp_token
*peek
= lexer
->next_token
;
1151 if (peek
== &eof_token
)
1152 peek
= lexer
->last_token
;
1154 gcc_assert (tok
< peek
);
1156 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1158 tok
->purged_p
= true;
1159 tok
->location
= UNKNOWN_LOCATION
;
1160 tok
->u
.value
= NULL_TREE
;
1161 tok
->keyword
= RID_MAX
;
1165 /* Begin saving tokens. All tokens consumed after this point will be
1169 cp_lexer_save_tokens (cp_lexer
* lexer
)
1171 /* Provide debugging output. */
1172 if (cp_lexer_debugging_p (lexer
))
1173 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1175 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1178 /* Commit to the portion of the token stream most recently saved. */
1181 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer
))
1185 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1187 lexer
->saved_tokens
.pop ();
1190 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1191 to the token stream. Stop saving tokens. */
1194 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer
))
1198 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1200 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1203 /* RAII wrapper around the above functions, with sanity checking. Creating
1204 a variable saves tokens, which are committed when the variable is
1205 destroyed unless they are explicitly rolled back by calling the rollback
1208 struct saved_token_sentinel
1213 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1215 len
= lexer
->saved_tokens
.length ();
1216 cp_lexer_save_tokens (lexer
);
1220 cp_lexer_rollback_tokens (lexer
);
1223 ~saved_token_sentinel()
1226 cp_lexer_commit_tokens (lexer
);
1227 gcc_assert (lexer
->saved_tokens
.length () == len
);
1231 /* Print a representation of the TOKEN on the STREAM. */
1234 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1236 /* We don't use cpp_type2name here because the parser defines
1237 a few tokens of its own. */
1238 static const char *const token_names
[] = {
1239 /* cpplib-defined token types */
1240 #define OP(e, s) #e,
1241 #define TK(e, s) #e,
1245 /* C++ parser token types - see "Manifest constants", above. */
1248 "NESTED_NAME_SPECIFIER",
1251 /* For some tokens, print the associated data. */
1252 switch (token
->type
)
1255 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1256 For example, `struct' is mapped to an INTEGER_CST. */
1257 if (!identifier_p (token
->u
.value
))
1261 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1268 case CPP_UTF8STRING
:
1269 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1273 print_generic_expr (stream
, token
->u
.value
);
1277 /* If we have a name for the token, print it out. Otherwise, we
1278 simply give the numeric code. */
1279 if (token
->type
< ARRAY_SIZE(token_names
))
1280 fputs (token_names
[token
->type
], stream
);
1282 fprintf (stream
, "[%d]", token
->type
);
1288 debug (cp_token
&ref
)
1290 cp_lexer_print_token (stderr
, &ref
);
1291 fprintf (stderr
, "\n");
1295 debug (cp_token
*ptr
)
1300 fprintf (stderr
, "<nil>\n");
1304 /* Start emitting debugging information. */
1307 cp_lexer_start_debugging (cp_lexer
* lexer
)
1309 if (!LEXER_DEBUGGING_ENABLED_P
)
1310 fatal_error (input_location
,
1311 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1313 lexer
->debugging_p
= true;
1314 cp_lexer_debug_stream
= stderr
;
1317 /* Stop emitting debugging information. */
1320 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1322 if (!LEXER_DEBUGGING_ENABLED_P
)
1323 fatal_error (input_location
,
1324 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1326 lexer
->debugging_p
= false;
1327 cp_lexer_debug_stream
= NULL
;
1330 /* Create a new cp_token_cache, representing a range of tokens. */
1332 static cp_token_cache
*
1333 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1335 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1336 cache
->first
= first
;
1341 /* Diagnose if #pragma omp declare simd isn't followed immediately
1342 by function declaration or definition. */
1345 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1347 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1349 error ("%<#pragma omp declare simd%> not immediately followed by "
1350 "function declaration or definition");
1351 parser
->omp_declare_simd
= NULL
;
1355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1356 and put that into "omp declare simd" attribute. */
1359 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1361 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1363 if (fndecl
== error_mark_node
)
1365 parser
->omp_declare_simd
= NULL
;
1368 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1370 cp_ensure_no_omp_declare_simd (parser
);
1376 /* Diagnose if #pragma acc routine isn't followed immediately by function
1377 declaration or definition. */
1380 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1382 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1384 error_at (parser
->oacc_routine
->loc
,
1385 "%<#pragma acc routine%> not immediately followed by "
1386 "function declaration or definition");
1387 parser
->oacc_routine
= NULL
;
1391 /* Decl-specifiers. */
1393 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1396 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1398 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1403 /* Nothing other than the parser should be creating declarators;
1404 declarators are a semi-syntactic representation of C++ entities.
1405 Other parts of the front end that need to create entities (like
1406 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1408 static cp_declarator
*make_call_declarator
1409 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1410 static cp_declarator
*make_array_declarator
1411 (cp_declarator
*, tree
);
1412 static cp_declarator
*make_pointer_declarator
1413 (cp_cv_quals
, cp_declarator
*, tree
);
1414 static cp_declarator
*make_reference_declarator
1415 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1416 static cp_declarator
*make_ptrmem_declarator
1417 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1419 /* An erroneous declarator. */
1420 static cp_declarator
*cp_error_declarator
;
1422 /* The obstack on which declarators and related data structures are
1424 static struct obstack declarator_obstack
;
1426 /* Alloc BYTES from the declarator memory pool. */
1428 static inline void *
1429 alloc_declarator (size_t bytes
)
1431 return obstack_alloc (&declarator_obstack
, bytes
);
1434 /* Allocate a declarator of the indicated KIND. Clear fields that are
1435 common to all declarators. */
1437 static cp_declarator
*
1438 make_declarator (cp_declarator_kind kind
)
1440 cp_declarator
*declarator
;
1442 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1443 declarator
->kind
= kind
;
1444 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1445 declarator
->attributes
= NULL_TREE
;
1446 declarator
->std_attributes
= NULL_TREE
;
1447 declarator
->declarator
= NULL
;
1448 declarator
->parameter_pack_p
= false;
1449 declarator
->id_loc
= UNKNOWN_LOCATION
;
1454 /* Make a declarator for a generalized identifier. If
1455 QUALIFYING_SCOPE is non-NULL, the identifier is
1456 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1457 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1460 static cp_declarator
*
1461 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1462 special_function_kind sfk
, location_t id_location
)
1464 cp_declarator
*declarator
;
1466 /* It is valid to write:
1468 class C { void f(); };
1472 The standard is not clear about whether `typedef const C D' is
1473 legal; as of 2002-09-15 the committee is considering that
1474 question. EDG 3.0 allows that syntax. Therefore, we do as
1476 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1477 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1479 gcc_assert (identifier_p (unqualified_name
)
1480 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1481 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1483 declarator
= make_declarator (cdk_id
);
1484 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1485 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1486 declarator
->u
.id
.sfk
= sfk
;
1487 declarator
->id_loc
= id_location
;
1492 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1493 of modifiers such as const or volatile to apply to the pointer
1494 type, represented as identifiers. ATTRIBUTES represent the attributes that
1495 appertain to the pointer or reference. */
1498 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1501 cp_declarator
*declarator
;
1503 declarator
= make_declarator (cdk_pointer
);
1504 declarator
->declarator
= target
;
1505 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1506 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1509 declarator
->id_loc
= target
->id_loc
;
1510 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1511 target
->parameter_pack_p
= false;
1514 declarator
->parameter_pack_p
= false;
1516 declarator
->std_attributes
= attributes
;
1521 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1522 represent the attributes that appertain to the pointer or
1526 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1527 bool rvalue_ref
, tree attributes
)
1529 cp_declarator
*declarator
;
1531 declarator
= make_declarator (cdk_reference
);
1532 declarator
->declarator
= target
;
1533 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1534 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1537 declarator
->id_loc
= target
->id_loc
;
1538 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1539 target
->parameter_pack_p
= false;
1542 declarator
->parameter_pack_p
= false;
1544 declarator
->std_attributes
= attributes
;
1549 /* Like make_pointer_declarator -- but for a pointer to a non-static
1550 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1551 appertain to the pointer or reference. */
1554 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1555 cp_declarator
*pointee
,
1558 cp_declarator
*declarator
;
1560 declarator
= make_declarator (cdk_ptrmem
);
1561 declarator
->declarator
= pointee
;
1562 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1563 declarator
->u
.pointer
.class_type
= class_type
;
1567 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1568 pointee
->parameter_pack_p
= false;
1571 declarator
->parameter_pack_p
= false;
1573 declarator
->std_attributes
= attributes
;
1578 /* Make a declarator for the function given by TARGET, with the
1579 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1580 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1581 indicates what exceptions can be thrown. */
1584 make_call_declarator (cp_declarator
*target
,
1586 cp_cv_quals cv_qualifiers
,
1587 cp_virt_specifiers virt_specifiers
,
1588 cp_ref_qualifier ref_qualifier
,
1590 tree exception_specification
,
1591 tree late_return_type
,
1592 tree requires_clause
)
1594 cp_declarator
*declarator
;
1596 declarator
= make_declarator (cdk_function
);
1597 declarator
->declarator
= target
;
1598 declarator
->u
.function
.parameters
= parms
;
1599 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1600 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1601 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1602 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1603 declarator
->u
.function
.exception_specification
= exception_specification
;
1604 declarator
->u
.function
.late_return_type
= late_return_type
;
1605 declarator
->u
.function
.requires_clause
= requires_clause
;
1608 declarator
->id_loc
= target
->id_loc
;
1609 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1610 target
->parameter_pack_p
= false;
1613 declarator
->parameter_pack_p
= false;
1618 /* Make a declarator for an array of BOUNDS elements, each of which is
1619 defined by ELEMENT. */
1622 make_array_declarator (cp_declarator
*element
, tree bounds
)
1624 cp_declarator
*declarator
;
1626 declarator
= make_declarator (cdk_array
);
1627 declarator
->declarator
= element
;
1628 declarator
->u
.array
.bounds
= bounds
;
1631 declarator
->id_loc
= element
->id_loc
;
1632 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1633 element
->parameter_pack_p
= false;
1636 declarator
->parameter_pack_p
= false;
1641 /* Determine whether the declarator we've seen so far can be a
1642 parameter pack, when followed by an ellipsis. */
1644 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1646 if (declarator
&& declarator
->parameter_pack_p
)
1647 /* We already saw an ellipsis. */
1650 /* Search for a declarator name, or any other declarator that goes
1651 after the point where the ellipsis could appear in a parameter
1652 pack. If we find any of these, then this declarator cannot be
1653 made into a parameter pack. */
1655 while (declarator
&& !found
)
1657 switch ((int)declarator
->kind
)
1669 declarator
= declarator
->declarator
;
1677 cp_parameter_declarator
*no_parameters
;
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680 DECLARATOR and DEFAULT_ARGUMENT. */
1682 cp_parameter_declarator
*
1683 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1684 cp_declarator
*declarator
,
1685 tree default_argument
,
1687 bool template_parameter_pack_p
= false)
1689 cp_parameter_declarator
*parameter
;
1691 parameter
= ((cp_parameter_declarator
*)
1692 alloc_declarator (sizeof (cp_parameter_declarator
)));
1693 parameter
->next
= NULL
;
1694 if (decl_specifiers
)
1695 parameter
->decl_specifiers
= *decl_specifiers
;
1697 clear_decl_specs (¶meter
->decl_specifiers
);
1698 parameter
->declarator
= declarator
;
1699 parameter
->default_argument
= default_argument
;
1700 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1701 parameter
->loc
= loc
;
1706 /* Returns true iff DECLARATOR is a declaration for a function. */
1709 function_declarator_p (const cp_declarator
*declarator
)
1713 if (declarator
->kind
== cdk_function
1714 && declarator
->declarator
->kind
== cdk_id
)
1716 if (declarator
->kind
== cdk_id
1717 || declarator
->kind
== cdk_decomp
1718 || declarator
->kind
== cdk_error
)
1720 declarator
= declarator
->declarator
;
1730 A cp_parser parses the token stream as specified by the C++
1731 grammar. Its job is purely parsing, not semantic analysis. For
1732 example, the parser breaks the token stream into declarators,
1733 expressions, statements, and other similar syntactic constructs.
1734 It does not check that the types of the expressions on either side
1735 of an assignment-statement are compatible, or that a function is
1736 not declared with a parameter of type `void'.
1738 The parser invokes routines elsewhere in the compiler to perform
1739 semantic analysis and to build up the abstract syntax tree for the
1742 The parser (and the template instantiation code, which is, in a
1743 way, a close relative of parsing) are the only parts of the
1744 compiler that should be calling push_scope and pop_scope, or
1745 related functions. The parser (and template instantiation code)
1746 keeps track of what scope is presently active; everything else
1747 should simply honor that. (The code that generates static
1748 initializers may also need to set the scope, in order to check
1749 access control correctly when emitting the initializers.)
1754 The parser is of the standard recursive-descent variety. Upcoming
1755 tokens in the token stream are examined in order to determine which
1756 production to use when parsing a non-terminal. Some C++ constructs
1757 require arbitrary look ahead to disambiguate. For example, it is
1758 impossible, in the general case, to tell whether a statement is an
1759 expression or declaration without scanning the entire statement.
1760 Therefore, the parser is capable of "parsing tentatively." When the
1761 parser is not sure what construct comes next, it enters this mode.
1762 Then, while we attempt to parse the construct, the parser queues up
1763 error messages, rather than issuing them immediately, and saves the
1764 tokens it consumes. If the construct is parsed successfully, the
1765 parser "commits", i.e., it issues any queued error messages and
1766 the tokens that were being preserved are permanently discarded.
1767 If, however, the construct is not parsed successfully, the parser
1768 rolls back its state completely so that it can resume parsing using
1769 a different alternative.
1774 The performance of the parser could probably be improved substantially.
1775 We could often eliminate the need to parse tentatively by looking ahead
1776 a little bit. In some places, this approach might not entirely eliminate
1777 the need to parse tentatively, but it might still speed up the average
1780 /* Flags that are passed to some parsing functions. These values can
1781 be bitwise-ored together. */
1786 CP_PARSER_FLAGS_NONE
= 0x0,
1787 /* The construct is optional. If it is not present, then no error
1788 should be issued. */
1789 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1790 /* When parsing a type-specifier, treat user-defined type-names
1791 as non-type identifiers. */
1792 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1793 /* When parsing a type-specifier, do not try to parse a class-specifier
1794 or enum-specifier. */
1795 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1796 /* When parsing a decl-specifier-seq, only allow type-specifier or
1798 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1799 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1800 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1801 /* When parsing a decl-specifier-seq, allow missing typename. */
1802 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static void cp_parser_translation_unit (cp_parser
*);
2028 /* Expressions [gram.expr] */
2030 static cp_expr cp_parser_primary_expression
2031 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2032 static cp_expr cp_parser_id_expression
2033 (cp_parser
*, bool, bool, bool *, bool, bool);
2034 static cp_expr cp_parser_unqualified_id
2035 (cp_parser
*, bool, bool, bool, bool);
2036 static tree cp_parser_nested_name_specifier_opt
2037 (cp_parser
*, bool, bool, bool, bool, bool = false);
2038 static tree cp_parser_nested_name_specifier
2039 (cp_parser
*, bool, bool, bool, bool);
2040 static tree cp_parser_qualifying_entity
2041 (cp_parser
*, bool, bool, bool, bool, bool);
2042 static cp_expr cp_parser_postfix_expression
2043 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2044 static tree cp_parser_postfix_open_square_expression
2045 (cp_parser
*, tree
, bool, bool);
2046 static tree cp_parser_postfix_dot_deref_expression
2047 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2048 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2049 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2051 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2053 static void cp_parser_pseudo_destructor_name
2054 (cp_parser
*, tree
, tree
*, tree
*);
2055 static cp_expr cp_parser_unary_expression
2056 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2059 static tree cp_parser_has_attribute_expression
2061 static tree cp_parser_new_expression
2063 static vec
<tree
, va_gc
> *cp_parser_new_placement
2065 static tree cp_parser_new_type_id
2066 (cp_parser
*, tree
*);
2067 static cp_declarator
*cp_parser_new_declarator_opt
2069 static cp_declarator
*cp_parser_direct_new_declarator
2071 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2073 static tree cp_parser_delete_expression
2075 static cp_expr cp_parser_cast_expression
2076 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2077 static cp_expr cp_parser_binary_expression
2078 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2079 static tree cp_parser_question_colon_clause
2080 (cp_parser
*, cp_expr
);
2081 static cp_expr cp_parser_assignment_expression
2082 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2083 static enum tree_code cp_parser_assignment_operator_opt
2085 static cp_expr cp_parser_expression
2086 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2087 static cp_expr cp_parser_constant_expression
2088 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2089 static cp_expr cp_parser_builtin_offsetof
2091 static cp_expr cp_parser_lambda_expression
2093 static void cp_parser_lambda_introducer
2094 (cp_parser
*, tree
);
2095 static bool cp_parser_lambda_declarator_opt
2096 (cp_parser
*, tree
);
2097 static void cp_parser_lambda_body
2098 (cp_parser
*, tree
);
2100 /* Statements [gram.stmt.stmt] */
2102 static void cp_parser_statement
2103 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2104 static void cp_parser_label_for_labeled_statement
2105 (cp_parser
*, tree
);
2106 static tree cp_parser_expression_statement
2107 (cp_parser
*, tree
);
2108 static tree cp_parser_compound_statement
2109 (cp_parser
*, tree
, int, bool);
2110 static void cp_parser_statement_seq_opt
2111 (cp_parser
*, tree
);
2112 static tree cp_parser_selection_statement
2113 (cp_parser
*, bool *, vec
<tree
> *);
2114 static tree cp_parser_condition
2116 static tree cp_parser_iteration_statement
2117 (cp_parser
*, bool *, bool, unsigned short);
2118 static bool cp_parser_init_statement
2119 (cp_parser
*, tree
*decl
);
2120 static tree cp_parser_for
2121 (cp_parser
*, bool, unsigned short);
2122 static tree cp_parser_c_for
2123 (cp_parser
*, tree
, tree
, bool, unsigned short);
2124 static tree cp_parser_range_for
2125 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2126 static void do_range_for_auto_deduction
2128 static tree cp_parser_perform_range_for_lookup
2129 (tree
, tree
*, tree
*);
2130 static tree cp_parser_range_for_member_function
2132 static tree cp_parser_jump_statement
2134 static void cp_parser_declaration_statement
2137 static tree cp_parser_implicitly_scoped_statement
2138 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2139 static void cp_parser_already_scoped_statement
2140 (cp_parser
*, bool *, const token_indent_info
&);
2142 /* Declarations [gram.dcl.dcl] */
2144 static void cp_parser_declaration_seq_opt
2146 static void cp_parser_declaration
2148 static void cp_parser_toplevel_declaration
2150 static void cp_parser_block_declaration
2151 (cp_parser
*, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser
*, bool, tree
*);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser
*, cp_decl_specifier_seq
*);
2160 static tree cp_parser_type_specifier
2161 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2165 static tree cp_parser_type_name
2166 (cp_parser
*, bool);
2167 static tree cp_parser_nonclass_name
2168 (cp_parser
* parser
);
2169 static tree cp_parser_elaborated_type_specifier
2170 (cp_parser
*, bool, bool);
2171 static tree cp_parser_enum_specifier
2173 static void cp_parser_enumerator_list
2174 (cp_parser
*, tree
);
2175 static void cp_parser_enumerator_definition
2176 (cp_parser
*, tree
);
2177 static tree cp_parser_namespace_name
2179 static void cp_parser_namespace_definition
2181 static void cp_parser_namespace_body
2183 static tree cp_parser_qualified_namespace_specifier
2185 static void cp_parser_namespace_alias_definition
2187 static bool cp_parser_using_declaration
2188 (cp_parser
*, bool);
2189 static void cp_parser_using_directive
2191 static tree cp_parser_alias_declaration
2193 static void cp_parser_asm_definition
2195 static void cp_parser_linkage_specification
2197 static void cp_parser_static_assert
2198 (cp_parser
*, bool);
2199 static tree cp_parser_decltype
2201 static tree cp_parser_decomposition_declaration
2202 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2204 /* Declarators [gram.dcl.decl] */
2206 static tree cp_parser_init_declarator
2207 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2208 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2209 location_t
*, tree
*);
2210 static cp_declarator
*cp_parser_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2213 static cp_declarator
*cp_parser_direct_declarator
2214 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2216 static enum tree_code cp_parser_ptr_operator
2217 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2218 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2220 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2222 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2224 static tree cp_parser_tx_qualifier_opt
2226 static tree cp_parser_late_return_type_opt
2227 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2228 static tree cp_parser_declarator_id
2229 (cp_parser
*, bool);
2230 static tree cp_parser_type_id
2231 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2232 static tree cp_parser_template_type_arg
2234 static tree
cp_parser_trailing_type_id (cp_parser
*);
2235 static tree cp_parser_type_id_1
2236 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2237 static void cp_parser_type_specifier_seq
2238 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2239 static tree cp_parser_parameter_declaration_clause
2240 (cp_parser
*, cp_parser_flags
);
2241 static tree cp_parser_parameter_declaration_list
2242 (cp_parser
*, cp_parser_flags
);
2243 static cp_parameter_declarator
*cp_parser_parameter_declaration
2244 (cp_parser
*, cp_parser_flags
, bool, bool *);
2245 static tree cp_parser_default_argument
2246 (cp_parser
*, bool);
2247 static void cp_parser_function_body
2248 (cp_parser
*, bool);
2249 static tree cp_parser_initializer
2250 (cp_parser
*, bool *, bool *, bool = false);
2251 static cp_expr cp_parser_initializer_clause
2252 (cp_parser
*, bool *);
2253 static cp_expr cp_parser_braced_list
2254 (cp_parser
*, bool*);
2255 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2256 (cp_parser
*, bool *, bool *);
2258 static void cp_parser_ctor_initializer_opt_and_function_body
2259 (cp_parser
*, bool);
2261 static tree cp_parser_late_parsing_omp_declare_simd
2262 (cp_parser
*, tree
);
2264 static tree cp_parser_late_parsing_oacc_routine
2265 (cp_parser
*, tree
);
2267 static tree synthesize_implicit_template_parm
2268 (cp_parser
*, tree
);
2269 static tree finish_fully_implicit_template
2270 (cp_parser
*, tree
);
2271 static void abort_fully_implicit_template
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2280 static tree cp_parser_class_head
2281 (cp_parser
*, bool *);
2282 static enum tag_types cp_parser_class_key
2284 static void cp_parser_type_parameter_key
2285 (cp_parser
* parser
);
2286 static void cp_parser_member_specification_opt
2288 static void cp_parser_member_declaration
2290 static tree cp_parser_pure_specifier
2292 static tree cp_parser_constant_initializer
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2299 static tree cp_parser_base_specifier
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2306 static tree cp_parser_conversion_type_id
2308 static cp_declarator
*cp_parser_conversion_declarator_opt
2310 static void cp_parser_ctor_initializer_opt
2312 static void cp_parser_mem_initializer_list
2314 static tree cp_parser_mem_initializer
2316 static tree cp_parser_mem_initializer_id
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2323 static cp_expr cp_parser_operator
2324 (cp_parser
*, location_t
);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser
*, bool);
2330 static tree cp_parser_template_parameter_list
2332 static tree cp_parser_template_parameter
2333 (cp_parser
*, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser
*, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser
*, bool, bool, enum tag_types
, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2340 static tree cp_parser_template_argument_list
2342 static tree cp_parser_template_argument
2344 static void cp_parser_explicit_instantiation
2346 static void cp_parser_explicit_specialization
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2353 static void cp_parser_function_try_block
2355 static void cp_parser_handler_seq
2357 static void cp_parser_handler
2359 static tree cp_parser_exception_declaration
2361 static tree cp_parser_throw_expression
2363 static tree cp_parser_exception_specification_opt
2365 static tree cp_parser_type_id_list
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2372 static tree cp_parser_asm_operand_list
2374 static tree cp_parser_asm_clobber_list
2376 static tree cp_parser_asm_label_list
2378 static bool cp_next_tokens_can_be_attribute_p
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2382 static bool cp_next_tokens_can_be_std_attribute_p
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser
*, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser
*, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser
*, size_t);
2390 static tree cp_parser_attributes_opt
2392 static tree cp_parser_gnu_attributes_opt
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser
*, bool = false);
2396 static tree cp_parser_std_attribute
2397 (cp_parser
*, tree
);
2398 static tree cp_parser_std_attribute_spec
2400 static tree cp_parser_std_attribute_spec_seq
2402 static size_t cp_parser_skip_attributes_opt
2403 (cp_parser
*, size_t);
2404 static bool cp_parser_extension_opt
2405 (cp_parser
*, int *);
2406 static void cp_parser_label_declaration
2409 /* Concept Extensions */
2411 static tree cp_parser_requires_clause
2413 static tree cp_parser_requires_clause_opt
2415 static tree cp_parser_requires_expression
2417 static tree cp_parser_requirement_parameter_list
2419 static tree cp_parser_requirement_body
2421 static tree cp_parser_requirement_list
2423 static tree cp_parser_requirement
2425 static tree cp_parser_simple_requirement
2427 static tree cp_parser_compound_requirement
2429 static tree cp_parser_type_requirement
2431 static tree cp_parser_nested_requirement
2434 /* Transactional Memory Extensions */
2436 static tree cp_parser_transaction
2437 (cp_parser
*, cp_token
*);
2438 static tree cp_parser_transaction_expression
2439 (cp_parser
*, enum rid
);
2440 static void cp_parser_function_transaction
2441 (cp_parser
*, enum rid
);
2442 static tree cp_parser_transaction_cancel
2445 enum pragma_context
{
2452 static bool cp_parser_pragma
2453 (cp_parser
*, enum pragma_context
, bool *);
2455 /* Objective-C++ Productions */
2457 static tree cp_parser_objc_message_receiver
2459 static tree cp_parser_objc_message_args
2461 static tree cp_parser_objc_message_expression
2463 static cp_expr cp_parser_objc_encode_expression
2465 static tree cp_parser_objc_defs_expression
2467 static tree cp_parser_objc_protocol_expression
2469 static tree cp_parser_objc_selector_expression
2471 static cp_expr cp_parser_objc_expression
2473 static bool cp_parser_objc_selector_p
2475 static tree cp_parser_objc_selector
2477 static tree cp_parser_objc_protocol_refs_opt
2479 static void cp_parser_objc_declaration
2480 (cp_parser
*, tree
);
2481 static tree cp_parser_objc_statement
2483 static bool cp_parser_objc_valid_prefix_attributes
2484 (cp_parser
*, tree
*);
2485 static void cp_parser_objc_at_property_declaration
2487 static void cp_parser_objc_at_synthesize_declaration
2489 static void cp_parser_objc_at_dynamic_declaration
2491 static tree cp_parser_objc_struct_declaration
2494 /* Utility Routines */
2496 static cp_expr cp_parser_lookup_name
2497 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2498 static tree cp_parser_lookup_name_simple
2499 (cp_parser
*, tree
, location_t
);
2500 static tree cp_parser_maybe_treat_template_as_class
2502 static bool cp_parser_check_declarator_template_parameters
2503 (cp_parser
*, cp_declarator
*, location_t
);
2504 static bool cp_parser_check_template_parameters
2505 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2506 static cp_expr cp_parser_simple_cast_expression
2508 static tree cp_parser_global_scope_opt
2509 (cp_parser
*, bool);
2510 static bool cp_parser_constructor_declarator_p
2511 (cp_parser
*, cp_parser_flags
, bool);
2512 static tree cp_parser_function_definition_from_specifiers_and_declarator
2513 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2514 static tree cp_parser_function_definition_after_declarator
2515 (cp_parser
*, bool);
2516 static bool cp_parser_template_declaration_after_export
2517 (cp_parser
*, bool);
2518 static void cp_parser_perform_template_parameter_access_checks
2519 (vec
<deferred_access_check
, va_gc
> *);
2520 static tree cp_parser_single_declaration
2521 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2522 static cp_expr cp_parser_functional_cast
2523 (cp_parser
*, tree
);
2524 static tree cp_parser_save_member_function_body
2525 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2526 static tree cp_parser_save_nsdmi
2528 static tree cp_parser_enclosed_template_argument_list
2530 static void cp_parser_save_default_args
2531 (cp_parser
*, tree
);
2532 static void cp_parser_late_parsing_for_member
2533 (cp_parser
*, tree
);
2534 static tree cp_parser_late_parse_one_default_arg
2535 (cp_parser
*, tree
, tree
, tree
);
2536 static void cp_parser_late_parsing_nsdmi
2537 (cp_parser
*, tree
);
2538 static void cp_parser_late_parsing_default_args
2539 (cp_parser
*, tree
);
2540 static tree cp_parser_sizeof_operand
2541 (cp_parser
*, enum rid
);
2542 static cp_expr cp_parser_trait_expr
2543 (cp_parser
*, enum rid
);
2544 static bool cp_parser_declares_only_class_p
2546 static void cp_parser_set_storage_class
2547 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2548 static void cp_parser_set_decl_spec_type
2549 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2550 static void set_and_check_decl_spec_loc
2551 (cp_decl_specifier_seq
*decl_specs
,
2552 cp_decl_spec ds
, cp_token
*);
2553 static bool cp_parser_friend_p
2554 (const cp_decl_specifier_seq
*);
2555 static void cp_parser_required_error
2556 (cp_parser
*, required_token
, bool, location_t
);
2557 static cp_token
*cp_parser_require
2558 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2559 static cp_token
*cp_parser_require_keyword
2560 (cp_parser
*, enum rid
, required_token
);
2561 static bool cp_parser_token_starts_function_definition_p
2563 static bool cp_parser_next_token_starts_class_definition_p
2565 static bool cp_parser_next_token_ends_template_argument_p
2567 static bool cp_parser_nth_token_starts_template_argument_list_p
2568 (cp_parser
*, size_t);
2569 static enum tag_types cp_parser_token_is_class_key
2571 static enum tag_types cp_parser_token_is_type_parameter_key
2573 static void cp_parser_check_class_key
2574 (enum tag_types
, tree type
);
2575 static void cp_parser_check_access_in_redeclaration
2576 (tree type
, location_t location
);
2577 static bool cp_parser_optional_template_keyword
2579 static void cp_parser_pre_parsed_nested_name_specifier
2581 static bool cp_parser_cache_group
2582 (cp_parser
*, enum cpp_ttype
, unsigned);
2583 static tree cp_parser_cache_defarg
2584 (cp_parser
*parser
, bool nsdmi
);
2585 static void cp_parser_parse_tentatively
2587 static void cp_parser_commit_to_tentative_parse
2589 static void cp_parser_commit_to_topmost_tentative_parse
2591 static void cp_parser_abort_tentative_parse
2593 static bool cp_parser_parse_definitely
2595 static inline bool cp_parser_parsing_tentatively
2597 static bool cp_parser_uncommitted_to_tentative_parse_p
2599 static void cp_parser_error
2600 (cp_parser
*, const char *);
2601 static void cp_parser_name_lookup_error
2602 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2603 static bool cp_parser_simulate_error
2605 static bool cp_parser_check_type_definition
2607 static void cp_parser_check_for_definition_in_return_type
2608 (cp_declarator
*, tree
, location_t type_location
);
2609 static void cp_parser_check_for_invalid_template_id
2610 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2611 static bool cp_parser_non_integral_constant_expression
2612 (cp_parser
*, non_integral_constant
);
2613 static void cp_parser_diagnose_invalid_type_name
2614 (cp_parser
*, tree
, location_t
);
2615 static bool cp_parser_parse_and_diagnose_invalid_type_name
2617 static int cp_parser_skip_to_closing_parenthesis
2618 (cp_parser
*, bool, bool, bool);
2619 static void cp_parser_skip_to_end_of_statement
2621 static void cp_parser_consume_semicolon_at_end_of_statement
2623 static void cp_parser_skip_to_end_of_block_or_statement
2625 static bool cp_parser_skip_to_closing_brace
2627 static void cp_parser_skip_to_end_of_template_parameter_list
2629 static void cp_parser_skip_to_pragma_eol
2630 (cp_parser
*, cp_token
*);
2631 static bool cp_parser_error_occurred
2633 static bool cp_parser_allow_gnu_extensions_p
2635 static bool cp_parser_is_pure_string_literal
2637 static bool cp_parser_is_string_literal
2639 static bool cp_parser_is_keyword
2640 (cp_token
*, enum rid
);
2641 static tree cp_parser_make_typename_type
2642 (cp_parser
*, tree
, location_t location
);
2643 static cp_declarator
* cp_parser_make_indirect_declarator
2644 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2645 static bool cp_parser_compound_literal_p
2647 static bool cp_parser_array_designator_p
2649 static bool cp_parser_init_statement_p
2651 static bool cp_parser_skip_to_closing_square_bracket
2654 /* Concept-related syntactic transformations */
2656 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2657 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2659 // -------------------------------------------------------------------------- //
2660 // Unevaluated Operand Guard
2662 // Implementation of an RAII helper for unevaluated operand parsing.
2663 cp_unevaluated::cp_unevaluated ()
2665 ++cp_unevaluated_operand
;
2666 ++c_inhibit_evaluation_warnings
;
2669 cp_unevaluated::~cp_unevaluated ()
2671 --c_inhibit_evaluation_warnings
;
2672 --cp_unevaluated_operand
;
2675 // -------------------------------------------------------------------------- //
2676 // Tentative Parsing
2678 /* Returns nonzero if we are parsing tentatively. */
2681 cp_parser_parsing_tentatively (cp_parser
* parser
)
2683 return parser
->context
->next
!= NULL
;
2686 /* Returns nonzero if TOKEN is a string literal. */
2689 cp_parser_is_pure_string_literal (cp_token
* token
)
2691 return (token
->type
== CPP_STRING
||
2692 token
->type
== CPP_STRING16
||
2693 token
->type
== CPP_STRING32
||
2694 token
->type
== CPP_WSTRING
||
2695 token
->type
== CPP_UTF8STRING
);
2698 /* Returns nonzero if TOKEN is a string literal
2699 of a user-defined string literal. */
2702 cp_parser_is_string_literal (cp_token
* token
)
2704 return (cp_parser_is_pure_string_literal (token
) ||
2705 token
->type
== CPP_STRING_USERDEF
||
2706 token
->type
== CPP_STRING16_USERDEF
||
2707 token
->type
== CPP_STRING32_USERDEF
||
2708 token
->type
== CPP_WSTRING_USERDEF
||
2709 token
->type
== CPP_UTF8STRING_USERDEF
);
2712 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2715 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2717 return token
->keyword
== keyword
;
2720 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2723 static enum pragma_kind
2724 cp_parser_pragma_kind (cp_token
*token
)
2726 if (token
->type
!= CPP_PRAGMA
)
2728 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2729 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2732 /* Helper function for cp_parser_error.
2733 Having peeked a token of kind TOK1_KIND that might signify
2734 a conflict marker, peek successor tokens to determine
2735 if we actually do have a conflict marker.
2736 Specifically, we consider a run of 7 '<', '=' or '>' characters
2737 at the start of a line as a conflict marker.
2738 These come through the lexer as three pairs and a single,
2739 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2740 If it returns true, *OUT_LOC is written to with the location/range
2744 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2745 location_t
*out_loc
)
2747 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2748 if (token2
->type
!= tok1_kind
)
2750 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2751 if (token3
->type
!= tok1_kind
)
2753 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2754 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2757 /* It must be at the start of the line. */
2758 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2759 if (LOCATION_COLUMN (start_loc
) != 1)
2762 /* We have a conflict marker. Construct a location of the form:
2765 with start == caret, finishing at the end of the marker. */
2766 location_t finish_loc
= get_finish (token4
->location
);
2767 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2772 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2776 get_matching_symbol (required_token token_desc
)
2783 case RT_CLOSE_BRACE
:
2785 case RT_CLOSE_PAREN
:
2790 /* Attempt to convert TOKEN_DESC from a required_token to an
2791 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2793 static enum cpp_ttype
2794 get_required_cpp_ttype (required_token token_desc
)
2799 return CPP_SEMICOLON
;
2801 return CPP_OPEN_PAREN
;
2802 case RT_CLOSE_BRACE
:
2803 return CPP_CLOSE_BRACE
;
2805 return CPP_OPEN_BRACE
;
2806 case RT_CLOSE_SQUARE
:
2807 return CPP_CLOSE_SQUARE
;
2808 case RT_OPEN_SQUARE
:
2809 return CPP_OPEN_SQUARE
;
2814 case RT_CLOSE_PAREN
:
2815 return CPP_CLOSE_PAREN
;
2818 /* Use CPP_EOF as a "no completions possible" code. */
2824 /* Subroutine of cp_parser_error and cp_parser_required_error.
2826 Issue a diagnostic of the form
2827 FILE:LINE: MESSAGE before TOKEN
2828 where TOKEN is the next token in the input stream. MESSAGE
2829 (specified by the caller) is usually of the form "expected
2832 This bypasses the check for tentative passing, and potentially
2833 adds material needed by cp_parser_required_error.
2835 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2836 suggesting insertion of the missing token.
2838 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2839 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2843 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2844 required_token missing_token_desc
,
2845 location_t matching_location
)
2847 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2848 /* This diagnostic makes more sense if it is tagged to the line
2849 of the token we just peeked at. */
2850 cp_lexer_set_source_position_from_token (token
);
2852 if (token
->type
== CPP_PRAGMA
)
2854 error_at (token
->location
,
2855 "%<#pragma%> is not allowed here");
2856 cp_parser_skip_to_pragma_eol (parser
, token
);
2860 /* If this is actually a conflict marker, report it as such. */
2861 if (token
->type
== CPP_LSHIFT
2862 || token
->type
== CPP_RSHIFT
2863 || token
->type
== CPP_EQ_EQ
)
2866 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2868 error_at (loc
, "version control conflict marker in file");
2869 expanded_location token_exploc
= expand_location (token
->location
);
2870 /* Consume tokens until the end of the source line. */
2873 cp_lexer_consume_token (parser
->lexer
);
2874 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2877 expanded_location next_exploc
= expand_location (next
->location
);
2878 if (next_exploc
.file
!= token_exploc
.file
)
2880 if (next_exploc
.line
!= token_exploc
.line
)
2887 gcc_rich_location
richloc (input_location
);
2889 bool added_matching_location
= false;
2891 if (missing_token_desc
!= RT_NONE
)
2893 /* Potentially supply a fix-it hint, suggesting to add the
2894 missing token immediately after the *previous* token.
2895 This may move the primary location within richloc. */
2896 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2897 location_t prev_token_loc
2898 = cp_lexer_previous_token (parser
->lexer
)->location
;
2899 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2901 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2902 Attempt to consolidate diagnostics by printing it as a
2903 secondary range within the main diagnostic. */
2904 if (matching_location
!= UNKNOWN_LOCATION
)
2905 added_matching_location
2906 = richloc
.add_location_if_nearby (matching_location
);
2909 /* Actually emit the error. */
2910 c_parse_error (gmsgid
,
2911 /* Because c_parser_error does not understand
2912 CPP_KEYWORD, keywords are treated like
2914 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2915 token
->u
.value
, token
->flags
, &richloc
);
2917 if (missing_token_desc
!= RT_NONE
)
2919 /* If we weren't able to consolidate matching_location, then
2920 print it as a secondary diagnostic. */
2921 if (matching_location
!= UNKNOWN_LOCATION
2922 && !added_matching_location
)
2923 inform (matching_location
, "to match this %qs",
2924 get_matching_symbol (missing_token_desc
));
2928 /* If not parsing tentatively, issue a diagnostic of the form
2929 FILE:LINE: MESSAGE before TOKEN
2930 where TOKEN is the next token in the input stream. MESSAGE
2931 (specified by the caller) is usually of the form "expected
2935 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2937 if (!cp_parser_simulate_error (parser
))
2938 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2941 /* Issue an error about name-lookup failing. NAME is the
2942 IDENTIFIER_NODE DECL is the result of
2943 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2944 the thing that we hoped to find. */
2947 cp_parser_name_lookup_error (cp_parser
* parser
,
2950 name_lookup_error desired
,
2951 location_t location
)
2953 /* If name lookup completely failed, tell the user that NAME was not
2955 if (decl
== error_mark_node
)
2957 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2958 error_at (location
, "%<%E::%E%> has not been declared",
2959 parser
->scope
, name
);
2960 else if (parser
->scope
== global_namespace
)
2961 error_at (location
, "%<::%E%> has not been declared", name
);
2962 else if (parser
->object_scope
2963 && !CLASS_TYPE_P (parser
->object_scope
))
2964 error_at (location
, "request for member %qE in non-class type %qT",
2965 name
, parser
->object_scope
);
2966 else if (parser
->object_scope
)
2967 error_at (location
, "%<%T::%E%> has not been declared",
2968 parser
->object_scope
, name
);
2970 error_at (location
, "%qE has not been declared", name
);
2972 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2977 error_at (location
, "%<%E::%E%> is not a type",
2978 parser
->scope
, name
);
2981 error_at (location
, "%<%E::%E%> is not a class or namespace",
2982 parser
->scope
, name
);
2986 "%<%E::%E%> is not a class, namespace, or enumeration",
2987 parser
->scope
, name
);
2994 else if (parser
->scope
== global_namespace
)
2999 error_at (location
, "%<::%E%> is not a type", name
);
3002 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3006 "%<::%E%> is not a class, namespace, or enumeration",
3018 error_at (location
, "%qE is not a type", name
);
3021 error_at (location
, "%qE is not a class or namespace", name
);
3025 "%qE is not a class, namespace, or enumeration", name
);
3033 /* If we are parsing tentatively, remember that an error has occurred
3034 during this tentative parse. Returns true if the error was
3035 simulated; false if a message should be issued by the caller. */
3038 cp_parser_simulate_error (cp_parser
* parser
)
3040 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3042 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3048 /* This function is called when a type is defined. If type
3049 definitions are forbidden at this point, an error message is
3053 cp_parser_check_type_definition (cp_parser
* parser
)
3055 /* If types are forbidden here, issue a message. */
3056 if (parser
->type_definition_forbidden_message
)
3058 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3059 or %qs in the message need to be interpreted. */
3060 error (parser
->type_definition_forbidden_message
,
3061 parser
->type_definition_forbidden_message_arg
);
3067 /* This function is called when the DECLARATOR is processed. The TYPE
3068 was a type defined in the decl-specifiers. If it is invalid to
3069 define a type in the decl-specifiers for DECLARATOR, an error is
3070 issued. TYPE_LOCATION is the location of TYPE and is used
3071 for error reporting. */
3074 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3075 tree type
, location_t type_location
)
3077 /* [dcl.fct] forbids type definitions in return types.
3078 Unfortunately, it's not easy to know whether or not we are
3079 processing a return type until after the fact. */
3081 && (declarator
->kind
== cdk_pointer
3082 || declarator
->kind
== cdk_reference
3083 || declarator
->kind
== cdk_ptrmem
))
3084 declarator
= declarator
->declarator
;
3086 && declarator
->kind
== cdk_function
)
3088 error_at (type_location
,
3089 "new types may not be defined in a return type");
3090 inform (type_location
,
3091 "(perhaps a semicolon is missing after the definition of %qT)",
3096 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3097 "<" in any valid C++ program. If the next token is indeed "<",
3098 issue a message warning the user about what appears to be an
3099 invalid attempt to form a template-id. LOCATION is the location
3100 of the type-specifier (TYPE) */
3103 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3105 enum tag_types tag_type
,
3106 location_t location
)
3108 cp_token_position start
= 0;
3110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3112 if (TREE_CODE (type
) == TYPE_DECL
)
3113 type
= TREE_TYPE (type
);
3114 if (TYPE_P (type
) && !template_placeholder_p (type
))
3115 error_at (location
, "%qT is not a template", type
);
3116 else if (identifier_p (type
))
3118 if (tag_type
!= none_type
)
3119 error_at (location
, "%qE is not a class template", type
);
3121 error_at (location
, "%qE is not a template", type
);
3124 error_at (location
, "invalid template-id");
3125 /* Remember the location of the invalid "<". */
3126 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3127 start
= cp_lexer_token_position (parser
->lexer
, true);
3128 /* Consume the "<". */
3129 cp_lexer_consume_token (parser
->lexer
);
3130 /* Parse the template arguments. */
3131 cp_parser_enclosed_template_argument_list (parser
);
3132 /* Permanently remove the invalid template arguments so that
3133 this error message is not issued again. */
3135 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3139 /* If parsing an integral constant-expression, issue an error message
3140 about the fact that THING appeared and return true. Otherwise,
3141 return false. In either case, set
3142 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3145 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3146 non_integral_constant thing
)
3148 parser
->non_integral_constant_expression_p
= true;
3149 if (parser
->integral_constant_expression_p
)
3151 if (!parser
->allow_non_integral_constant_expression_p
)
3153 const char *msg
= NULL
;
3157 pedwarn (input_location
, OPT_Wpedantic
,
3158 "ISO C++ forbids using a floating-point literal "
3159 "in a constant-expression");
3162 error ("a cast to a type other than an integral or "
3163 "enumeration type cannot appear in a "
3164 "constant-expression");
3167 error ("%<typeid%> operator "
3168 "cannot appear in a constant-expression");
3171 error ("non-constant compound literals "
3172 "cannot appear in a constant-expression");
3175 error ("a function call "
3176 "cannot appear in a constant-expression");
3179 error ("an increment "
3180 "cannot appear in a constant-expression");
3183 error ("an decrement "
3184 "cannot appear in a constant-expression");
3187 error ("an array reference "
3188 "cannot appear in a constant-expression");
3190 case NIC_ADDR_LABEL
:
3191 error ("the address of a label "
3192 "cannot appear in a constant-expression");
3194 case NIC_OVERLOADED
:
3195 error ("calls to overloaded operators "
3196 "cannot appear in a constant-expression");
3198 case NIC_ASSIGNMENT
:
3199 error ("an assignment cannot appear in a constant-expression");
3202 error ("a comma operator "
3203 "cannot appear in a constant-expression");
3205 case NIC_CONSTRUCTOR
:
3206 error ("a call to a constructor "
3207 "cannot appear in a constant-expression");
3209 case NIC_TRANSACTION
:
3210 error ("a transaction expression "
3211 "cannot appear in a constant-expression");
3217 msg
= "__FUNCTION__";
3219 case NIC_PRETTY_FUNC
:
3220 msg
= "__PRETTY_FUNCTION__";
3240 case NIC_PREINCREMENT
:
3243 case NIC_PREDECREMENT
:
3256 error ("%qs cannot appear in a constant-expression", msg
);
3263 /* Emit a diagnostic for an invalid type name. This function commits
3264 to the current active tentative parse, if any. (Otherwise, the
3265 problematic construct might be encountered again later, resulting
3266 in duplicate error messages.) LOCATION is the location of ID. */
3269 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3270 location_t location
)
3272 tree decl
, ambiguous_decls
;
3273 cp_parser_commit_to_tentative_parse (parser
);
3274 /* Try to lookup the identifier. */
3275 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3276 /*is_template=*/false,
3277 /*is_namespace=*/false,
3278 /*check_dependency=*/true,
3279 &ambiguous_decls
, location
);
3280 if (ambiguous_decls
)
3281 /* If the lookup was ambiguous, an error will already have
3284 /* If the lookup found a template-name, it means that the user forgot
3285 to specify an argument list. Emit a useful error message. */
3286 if (DECL_TYPE_TEMPLATE_P (decl
))
3288 auto_diagnostic_group d
;
3290 "invalid use of template-name %qE without an argument list",
3292 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3293 inform (location
, "class template argument deduction is only available "
3294 "with %<-std=c++17%> or %<-std=gnu++17%>");
3295 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3297 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3298 error_at (location
, "invalid use of destructor %qD as a type", id
);
3299 else if (TREE_CODE (decl
) == TYPE_DECL
)
3300 /* Something like 'unsigned A a;' */
3301 error_at (location
, "invalid combination of multiple type-specifiers");
3302 else if (!parser
->scope
)
3304 /* Issue an error message. */
3305 auto_diagnostic_group d
;
3307 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3308 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3309 if (const char *suggestion
= hint
.suggestion ())
3311 gcc_rich_location
richloc (location
);
3312 richloc
.add_fixit_replace (suggestion
);
3314 "%qE does not name a type; did you mean %qs?",
3318 error_at (location
, "%qE does not name a type", id
);
3319 /* If we're in a template class, it's possible that the user was
3320 referring to a type from a base class. For example:
3322 template <typename T> struct A { typedef T X; };
3323 template <typename T> struct B : public A<T> { X x; };
3325 The user should have said "typename A<T>::X". */
3326 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3327 inform (location
, "C++11 %<constexpr%> only available with "
3328 "%<-std=c++11%> or %<-std=gnu++11%>");
3329 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3330 inform (location
, "C++11 %<noexcept%> only available with "
3331 "%<-std=c++11%> or %<-std=gnu++11%>");
3332 else if (cxx_dialect
< cxx11
3333 && TREE_CODE (id
) == IDENTIFIER_NODE
3334 && id_equal (id
, "thread_local"))
3335 inform (location
, "C++11 %<thread_local%> only available with "
3336 "%<-std=c++11%> or %<-std=gnu++11%>");
3337 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3338 inform (location
, "%<concept%> only available with %<-fconcepts%>");
3339 else if (processing_template_decl
&& current_class_type
3340 && TYPE_BINFO (current_class_type
))
3344 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3348 tree base_type
= BINFO_TYPE (b
);
3349 if (CLASS_TYPE_P (base_type
)
3350 && dependent_type_p (base_type
))
3353 /* Go from a particular instantiation of the
3354 template (which will have an empty TYPE_FIELDs),
3355 to the main version. */
3356 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3357 for (field
= TYPE_FIELDS (base_type
);
3359 field
= DECL_CHAIN (field
))
3360 if (TREE_CODE (field
) == TYPE_DECL
3361 && DECL_NAME (field
) == id
)
3364 "(perhaps %<typename %T::%E%> was intended)",
3365 BINFO_TYPE (b
), id
);
3374 /* Here we diagnose qualified-ids where the scope is actually correct,
3375 but the identifier does not resolve to a valid type name. */
3376 else if (parser
->scope
!= error_mark_node
)
3378 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3380 auto_diagnostic_group d
;
3382 if (decl
== error_mark_node
)
3383 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3385 const char *suggestion
= hint
.suggestion ();
3386 gcc_rich_location
richloc (location_of (id
));
3388 richloc
.add_fixit_replace (suggestion
);
3389 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3393 "%qE in namespace %qE does not name a template"
3394 " type; did you mean %qs?",
3395 id
, parser
->scope
, suggestion
);
3398 "%qE in namespace %qE does not name a template type",
3401 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3405 "%qE in namespace %qE does not name a template"
3406 " type; did you mean %qs?",
3407 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3410 "%qE in namespace %qE does not name a template"
3412 TREE_OPERAND (id
, 0), parser
->scope
);
3418 "%qE in namespace %qE does not name a type"
3419 "; did you mean %qs?",
3420 id
, parser
->scope
, suggestion
);
3423 "%qE in namespace %qE does not name a type",
3427 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3429 else if (CLASS_TYPE_P (parser
->scope
)
3430 && constructor_name_p (id
, parser
->scope
))
3433 auto_diagnostic_group d
;
3434 error_at (location
, "%<%T::%E%> names the constructor, not"
3435 " the type", parser
->scope
, id
);
3436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3437 error_at (location
, "and %qT has no template constructors",
3440 else if (TYPE_P (parser
->scope
)
3441 && dependent_scope_p (parser
->scope
))
3443 gcc_rich_location
richloc (location
);
3444 richloc
.add_fixit_insert_before ("typename ");
3445 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3447 "need %<typename%> before %<%T::%D::%E%> because "
3448 "%<%T::%D%> is a dependent scope",
3449 TYPE_CONTEXT (parser
->scope
),
3450 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3452 TYPE_CONTEXT (parser
->scope
),
3453 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3455 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3456 "%qT is a dependent scope",
3457 parser
->scope
, id
, parser
->scope
);
3459 else if (TYPE_P (parser
->scope
))
3461 auto_diagnostic_group d
;
3462 if (!COMPLETE_TYPE_P (parser
->scope
))
3463 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3465 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3466 error_at (location_of (id
),
3467 "%qE in %q#T does not name a template type",
3469 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3470 error_at (location_of (id
),
3471 "%qE in %q#T does not name a template type",
3472 TREE_OPERAND (id
, 0), parser
->scope
);
3474 error_at (location_of (id
),
3475 "%qE in %q#T does not name a type",
3478 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3485 /* Check for a common situation where a type-name should be present,
3486 but is not, and issue a sensible error message. Returns true if an
3487 invalid type-name was detected.
3489 The situation handled by this function are variable declarations of the
3490 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3491 Usually, `ID' should name a type, but if we got here it means that it
3492 does not. We try to emit the best possible error message depending on
3493 how exactly the id-expression looks like. */
3496 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3499 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3501 /* Avoid duplicate error about ambiguous lookup. */
3502 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3504 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3505 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3509 cp_parser_parse_tentatively (parser
);
3510 id
= cp_parser_id_expression (parser
,
3511 /*template_keyword_p=*/false,
3512 /*check_dependency_p=*/true,
3513 /*template_p=*/NULL
,
3514 /*declarator_p=*/false,
3515 /*optional_p=*/false);
3516 /* If the next token is a (, this is a function with no explicit return
3517 type, i.e. constructor, destructor or conversion op. */
3518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3519 || TREE_CODE (id
) == TYPE_DECL
)
3521 cp_parser_abort_tentative_parse (parser
);
3524 if (!cp_parser_parse_definitely (parser
))
3527 /* Emit a diagnostic for the invalid type. */
3528 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3530 /* If we aren't in the middle of a declarator (i.e. in a
3531 parameter-declaration-clause), skip to the end of the declaration;
3532 there's no point in trying to process it. */
3533 if (!parser
->in_declarator_p
)
3534 cp_parser_skip_to_end_of_block_or_statement (parser
);
3538 /* Consume tokens up to, and including, the next non-nested closing `)'.
3539 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3540 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3541 found an unnested token of that type. */
3544 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3549 unsigned paren_depth
= 0;
3550 unsigned brace_depth
= 0;
3551 unsigned square_depth
= 0;
3552 unsigned condop_depth
= 0;
3554 if (recovering
&& or_ttype
== CPP_EOF
3555 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3560 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3562 /* Have we found what we're looking for before the closing paren? */
3563 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3564 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3567 switch (token
->type
)
3569 case CPP_PRAGMA_EOL
:
3570 if (!parser
->lexer
->in_pragma
)
3574 /* If we've run out of tokens, then there is no closing `)'. */
3577 /* This is good for lambda expression capture-lists. */
3578 case CPP_OPEN_SQUARE
:
3581 case CPP_CLOSE_SQUARE
:
3582 if (!square_depth
--)
3587 /* This matches the processing in skip_to_end_of_statement. */
3592 case CPP_OPEN_BRACE
:
3595 case CPP_CLOSE_BRACE
:
3600 case CPP_OPEN_PAREN
:
3605 case CPP_CLOSE_PAREN
:
3606 if (!brace_depth
&& !paren_depth
--)
3609 cp_lexer_consume_token (parser
->lexer
);
3615 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3620 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser
->lexer
);
3633 /* Consume tokens up to, and including, the next non-nested closing `)'.
3634 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3635 are doing error recovery. Returns -1 if OR_COMMA is true and we
3636 found an unnested token of that type. */
3639 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3644 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3645 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3646 ttype
, consume_paren
);
3649 /* Consume tokens until we reach the end of the current statement.
3650 Normally, that will be just before consuming a `;'. However, if a
3651 non-nested `}' comes first, then we stop before consuming that. */
3654 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3656 unsigned nesting_depth
= 0;
3658 /* Unwind generic function template scope if necessary. */
3659 if (parser
->fully_implicit_function_template_p
)
3660 abort_fully_implicit_template (parser
);
3664 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3666 switch (token
->type
)
3668 case CPP_PRAGMA_EOL
:
3669 if (!parser
->lexer
->in_pragma
)
3673 /* If we've run out of tokens, stop. */
3677 /* If the next token is a `;', we have reached the end of the
3683 case CPP_CLOSE_BRACE
:
3684 /* If this is a non-nested '}', stop before consuming it.
3685 That way, when confronted with something like:
3689 we stop before consuming the closing '}', even though we
3690 have not yet reached a `;'. */
3691 if (nesting_depth
== 0)
3694 /* If it is the closing '}' for a block that we have
3695 scanned, stop -- but only after consuming the token.
3701 we will stop after the body of the erroneously declared
3702 function, but before consuming the following `typedef'
3704 if (--nesting_depth
== 0)
3706 cp_lexer_consume_token (parser
->lexer
);
3711 case CPP_OPEN_BRACE
:
3719 /* Consume the token. */
3720 cp_lexer_consume_token (parser
->lexer
);
3724 /* This function is called at the end of a statement or declaration.
3725 If the next token is a semicolon, it is consumed; otherwise, error
3726 recovery is attempted. */
3729 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3731 /* Look for the trailing `;'. */
3732 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3734 /* If there is additional (erroneous) input, skip to the end of
3736 cp_parser_skip_to_end_of_statement (parser
);
3737 /* If the next token is now a `;', consume it. */
3738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3739 cp_lexer_consume_token (parser
->lexer
);
3743 /* Skip tokens until we have consumed an entire block, or until we
3744 have consumed a non-nested `;'. */
3747 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3749 int nesting_depth
= 0;
3751 /* Unwind generic function template scope if necessary. */
3752 if (parser
->fully_implicit_function_template_p
)
3753 abort_fully_implicit_template (parser
);
3755 while (nesting_depth
>= 0)
3757 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3759 switch (token
->type
)
3761 case CPP_PRAGMA_EOL
:
3762 if (!parser
->lexer
->in_pragma
)
3766 /* If we've run out of tokens, stop. */
3770 /* Stop if this is an unnested ';'. */
3775 case CPP_CLOSE_BRACE
:
3776 /* Stop if this is an unnested '}', or closes the outermost
3779 if (nesting_depth
< 0)
3785 case CPP_OPEN_BRACE
:
3794 /* Consume the token. */
3795 cp_lexer_consume_token (parser
->lexer
);
3799 /* Skip tokens until a non-nested closing curly brace is the next
3800 token, or there are no more tokens. Return true in the first case,
3804 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3806 unsigned nesting_depth
= 0;
3810 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3812 switch (token
->type
)
3814 case CPP_PRAGMA_EOL
:
3815 if (!parser
->lexer
->in_pragma
)
3819 /* If we've run out of tokens, stop. */
3822 case CPP_CLOSE_BRACE
:
3823 /* If the next token is a non-nested `}', then we have reached
3824 the end of the current block. */
3825 if (nesting_depth
-- == 0)
3829 case CPP_OPEN_BRACE
:
3830 /* If it the next token is a `{', then we are entering a new
3831 block. Consume the entire block. */
3839 /* Consume the token. */
3840 cp_lexer_consume_token (parser
->lexer
);
3844 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3845 parameter is the PRAGMA token, allowing us to purge the entire pragma
3849 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3853 parser
->lexer
->in_pragma
= false;
3856 token
= cp_lexer_consume_token (parser
->lexer
);
3857 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3859 /* Ensure that the pragma is not parsed again. */
3860 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3863 /* Require pragma end of line, resyncing with it as necessary. The
3864 arguments are as for cp_parser_skip_to_pragma_eol. */
3867 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3869 parser
->lexer
->in_pragma
= false;
3870 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3871 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3874 /* This is a simple wrapper around make_typename_type. When the id is
3875 an unresolved identifier node, we can provide a superior diagnostic
3876 using cp_parser_diagnose_invalid_type_name. */
3879 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3880 location_t id_location
)
3883 if (identifier_p (id
))
3885 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3886 /*complain=*/tf_none
);
3887 if (result
== error_mark_node
)
3888 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3891 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3894 /* This is a wrapper around the
3895 make_{pointer,ptrmem,reference}_declarator functions that decides
3896 which one to call based on the CODE and CLASS_TYPE arguments. The
3897 CODE argument should be one of the values returned by
3898 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3899 appertain to the pointer or reference. */
3901 static cp_declarator
*
3902 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3903 cp_cv_quals cv_qualifiers
,
3904 cp_declarator
*target
,
3907 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3908 return cp_error_declarator
;
3910 if (code
== INDIRECT_REF
)
3911 if (class_type
== NULL_TREE
)
3912 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3914 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3915 target
, attributes
);
3916 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3917 return make_reference_declarator (cv_qualifiers
, target
,
3919 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3920 return make_reference_declarator (cv_qualifiers
, target
,
3925 /* Create a new C++ parser. */
3928 cp_parser_new (void)
3934 /* cp_lexer_new_main is called before doing GC allocation because
3935 cp_lexer_new_main might load a PCH file. */
3936 lexer
= cp_lexer_new_main ();
3938 /* Initialize the binops_by_token so that we can get the tree
3939 directly from the token. */
3940 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3941 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3943 parser
= ggc_cleared_alloc
<cp_parser
> ();
3944 parser
->lexer
= lexer
;
3945 parser
->context
= cp_parser_context_new (NULL
);
3947 /* For now, we always accept GNU extensions. */
3948 parser
->allow_gnu_extensions_p
= 1;
3950 /* The `>' token is a greater-than operator, not the end of a
3952 parser
->greater_than_is_operator_p
= true;
3954 parser
->default_arg_ok_p
= true;
3956 /* We are not parsing a constant-expression. */
3957 parser
->integral_constant_expression_p
= false;
3958 parser
->allow_non_integral_constant_expression_p
= false;
3959 parser
->non_integral_constant_expression_p
= false;
3961 /* Local variable names are not forbidden. */
3962 parser
->local_variables_forbidden_p
= 0;
3964 /* We are not processing an `extern "C"' declaration. */
3965 parser
->in_unbraced_linkage_specification_p
= false;
3967 /* We are not processing a declarator. */
3968 parser
->in_declarator_p
= false;
3970 /* We are not processing a template-argument-list. */
3971 parser
->in_template_argument_list_p
= false;
3973 /* We are not in an iteration statement. */
3974 parser
->in_statement
= 0;
3976 /* We are not in a switch statement. */
3977 parser
->in_switch_statement_p
= false;
3979 /* We are not parsing a type-id inside an expression. */
3980 parser
->in_type_id_in_expr_p
= false;
3982 /* String literals should be translated to the execution character set. */
3983 parser
->translate_strings_p
= true;
3985 /* We are not parsing a function body. */
3986 parser
->in_function_body
= false;
3988 /* We can correct until told otherwise. */
3989 parser
->colon_corrects_to_scope_p
= true;
3991 /* The unparsed function queue is empty. */
3992 push_unparsed_function_queues (parser
);
3994 /* There are no classes being defined. */
3995 parser
->num_classes_being_defined
= 0;
3997 /* No template parameters apply. */
3998 parser
->num_template_parameter_lists
= 0;
4000 /* Special parsing data structures. */
4001 parser
->omp_declare_simd
= NULL
;
4002 parser
->oacc_routine
= NULL
;
4004 /* Not declaring an implicit function template. */
4005 parser
->auto_is_implicit_function_template_parm_p
= false;
4006 parser
->fully_implicit_function_template_p
= false;
4007 parser
->implicit_template_parms
= 0;
4008 parser
->implicit_template_scope
= 0;
4010 /* Allow constrained-type-specifiers. */
4011 parser
->prevent_constrained_type_specifiers
= 0;
4013 /* We haven't yet seen an 'extern "C"'. */
4014 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4019 /* Create a cp_lexer structure which will emit the tokens in CACHE
4020 and push it onto the parser's lexer stack. This is used for delayed
4021 parsing of in-class method bodies and default arguments, and should
4022 not be confused with tentative parsing. */
4024 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4026 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4027 lexer
->next
= parser
->lexer
;
4028 parser
->lexer
= lexer
;
4030 /* Move the current source position to that of the first token in the
4032 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4035 /* Pop the top lexer off the parser stack. This is never used for the
4036 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4038 cp_parser_pop_lexer (cp_parser
*parser
)
4040 cp_lexer
*lexer
= parser
->lexer
;
4041 parser
->lexer
= lexer
->next
;
4042 cp_lexer_destroy (lexer
);
4044 /* Put the current source position back where it was before this
4045 lexer was pushed. */
4046 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4049 /* Lexical conventions [gram.lex] */
4051 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4055 cp_parser_identifier (cp_parser
* parser
)
4059 /* Look for the identifier. */
4060 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4061 /* Return the value. */
4063 return cp_expr (token
->u
.value
, token
->location
);
4065 return error_mark_node
;
4068 /* Parse a sequence of adjacent string constants. Returns a
4069 TREE_STRING representing the combined, nul-terminated string
4070 constant. If TRANSLATE is true, translate the string to the
4071 execution character set. If WIDE_OK is true, a wide string is
4074 C++98 [lex.string] says that if a narrow string literal token is
4075 adjacent to a wide string literal token, the behavior is undefined.
4076 However, C99 6.4.5p4 says that this results in a wide string literal.
4077 We follow C99 here, for consistency with the C front end.
4079 This code is largely lifted from lex_string() in c-lex.c.
4081 FUTURE: ObjC++ will need to handle @-strings here. */
4083 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4084 bool lookup_udlit
= true)
4088 struct obstack str_ob
;
4089 struct obstack loc_ob
;
4090 cpp_string str
, istr
, *strs
;
4092 enum cpp_ttype type
, curr_type
;
4093 int have_suffix_p
= 0;
4095 tree suffix_id
= NULL_TREE
;
4096 bool curr_tok_is_userdef_p
= false;
4098 tok
= cp_lexer_peek_token (parser
->lexer
);
4099 if (!cp_parser_is_string_literal (tok
))
4101 cp_parser_error (parser
, "expected string-literal");
4102 return error_mark_node
;
4105 location_t loc
= tok
->location
;
4107 if (cpp_userdef_string_p (tok
->type
))
4109 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4110 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4111 curr_tok_is_userdef_p
= true;
4115 string_tree
= tok
->u
.value
;
4116 curr_type
= tok
->type
;
4120 /* Try to avoid the overhead of creating and destroying an obstack
4121 for the common case of just one string. */
4122 if (!cp_parser_is_string_literal
4123 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4125 cp_lexer_consume_token (parser
->lexer
);
4127 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4128 str
.len
= TREE_STRING_LENGTH (string_tree
);
4131 if (curr_tok_is_userdef_p
)
4133 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4135 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4138 curr_type
= tok
->type
;
4144 location_t last_tok_loc
= tok
->location
;
4145 gcc_obstack_init (&str_ob
);
4146 gcc_obstack_init (&loc_ob
);
4151 cp_lexer_consume_token (parser
->lexer
);
4153 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4154 str
.len
= TREE_STRING_LENGTH (string_tree
);
4156 if (curr_tok_is_userdef_p
)
4158 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4159 if (have_suffix_p
== 0)
4161 suffix_id
= curr_suffix_id
;
4164 else if (have_suffix_p
== 1
4165 && curr_suffix_id
!= suffix_id
)
4167 error ("inconsistent user-defined literal suffixes"
4168 " %qD and %qD in string literal",
4169 suffix_id
, curr_suffix_id
);
4172 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4175 curr_type
= tok
->type
;
4177 if (type
!= curr_type
)
4179 if (type
== CPP_STRING
)
4181 else if (curr_type
!= CPP_STRING
)
4183 rich_location
rich_loc (line_table
, tok
->location
);
4184 rich_loc
.add_range (last_tok_loc
);
4185 error_at (&rich_loc
,
4186 "unsupported non-standard concatenation "
4187 "of string literals");
4191 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4192 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4194 last_tok_loc
= tok
->location
;
4196 tok
= cp_lexer_peek_token (parser
->lexer
);
4197 if (cpp_userdef_string_p (tok
->type
))
4199 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4200 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4201 curr_tok_is_userdef_p
= true;
4205 string_tree
= tok
->u
.value
;
4206 curr_type
= tok
->type
;
4207 curr_tok_is_userdef_p
= false;
4210 while (cp_parser_is_string_literal (tok
));
4212 /* A string literal built by concatenation has its caret=start at
4213 the start of the initial string, and its finish at the finish of
4214 the final string literal. */
4215 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4217 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4220 if (type
!= CPP_STRING
&& !wide_ok
)
4222 cp_parser_error (parser
, "a wide string is invalid in this context");
4226 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4227 (parse_in
, strs
, count
, &istr
, type
))
4229 value
= build_string (istr
.len
, (const char *)istr
.text
);
4230 free (CONST_CAST (unsigned char *, istr
.text
));
4233 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4234 gcc_assert (g_string_concat_db
);
4235 g_string_concat_db
->record_string_concatenation (count
, locs
);
4242 TREE_TYPE (value
) = char_array_type_node
;
4244 case CPP_UTF8STRING
:
4246 TREE_TYPE (value
) = char8_array_type_node
;
4248 TREE_TYPE (value
) = char_array_type_node
;
4251 TREE_TYPE (value
) = char16_array_type_node
;
4254 TREE_TYPE (value
) = char32_array_type_node
;
4257 TREE_TYPE (value
) = wchar_array_type_node
;
4261 value
= fix_string_type (value
);
4265 tree literal
= build_userdef_literal (suffix_id
, value
,
4266 OT_NONE
, NULL_TREE
);
4268 value
= cp_parser_userdef_string_literal (literal
);
4274 /* cpp_interpret_string has issued an error. */
4275 value
= error_mark_node
;
4279 obstack_free (&str_ob
, 0);
4280 obstack_free (&loc_ob
, 0);
4283 return cp_expr (value
, loc
);
4286 /* Look up a literal operator with the name and the exact arguments. */
4289 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4291 tree decl
= lookup_name (name
);
4292 if (!decl
|| !is_overloaded_fn (decl
))
4293 return error_mark_node
;
4295 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4299 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4305 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4306 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4308 tree tparm
= TREE_VALUE (parmtypes
);
4309 tree targ
= TREE_TYPE ((*args
)[ix
]);
4310 bool ptr
= TYPE_PTR_P (tparm
);
4311 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4312 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4314 || !same_type_p (TREE_TYPE (tparm
),
4320 && ix
== vec_safe_length (args
)
4321 /* May be this should be sufficient_parms_p instead,
4322 depending on how exactly should user-defined literals
4323 work in presence of default arguments on the literal
4324 operator parameters. */
4325 && parmtypes
== void_list_node
)
4330 return error_mark_node
;
4333 /* Parse a user-defined char constant. Returns a call to a user-defined
4334 literal operator taking the character as an argument. */
4337 cp_parser_userdef_char_literal (cp_parser
*parser
)
4339 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4340 tree literal
= token
->u
.value
;
4341 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4342 tree value
= USERDEF_LITERAL_VALUE (literal
);
4343 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4346 /* Build up a call to the user-defined operator */
4347 /* Lookup the name we got back from the id-expression. */
4349 vec_safe_push (args
, value
);
4350 decl
= lookup_literal_operator (name
, args
);
4351 if (!decl
|| decl
== error_mark_node
)
4353 error ("unable to find character literal operator %qD with %qT argument",
4354 name
, TREE_TYPE (value
));
4355 return error_mark_node
;
4357 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4361 /* A subroutine of cp_parser_userdef_numeric_literal to
4362 create a char... template parameter pack from a string node. */
4365 make_char_string_pack (tree value
)
4368 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4369 const char *str
= TREE_STRING_POINTER (value
);
4370 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4371 tree argvec
= make_tree_vec (1);
4373 /* Fill in CHARVEC with all of the parameters. */
4374 charvec
= make_tree_vec (len
);
4375 for (i
= 0; i
< len
; ++i
)
4377 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4378 cpp_string in
= { 3, s
};
4379 cpp_string out
= { 0, 0 };
4380 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4382 gcc_assert (out
.len
== 2);
4383 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4387 /* Build the argument packs. */
4388 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4390 TREE_VEC_ELT (argvec
, 0) = argpack
;
4395 /* A subroutine of cp_parser_userdef_numeric_literal to
4396 create a char... template parameter pack from a string node. */
4399 make_string_pack (tree value
)
4402 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4403 const unsigned char *str
4404 = (const unsigned char *) TREE_STRING_POINTER (value
);
4405 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4406 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4407 tree argvec
= make_tree_vec (2);
4409 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4410 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4412 /* First template parm is character type. */
4413 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4415 /* Fill in CHARVEC with all of the parameters. */
4416 charvec
= make_tree_vec (len
);
4417 for (int i
= 0; i
< len
; ++i
)
4418 TREE_VEC_ELT (charvec
, i
)
4419 = double_int_to_tree (str_char_type_node
,
4420 double_int::from_buffer (str
+ i
* sz
, sz
));
4422 /* Build the argument packs. */
4423 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4425 TREE_VEC_ELT (argvec
, 1) = argpack
;
4430 /* Parse a user-defined numeric constant. returns a call to a user-defined
4431 literal operator. */
4434 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4436 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4437 tree literal
= token
->u
.value
;
4438 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4439 tree value
= USERDEF_LITERAL_VALUE (literal
);
4440 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4441 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4442 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4445 /* Look for a literal operator taking the exact type of numeric argument
4446 as the literal value. */
4448 vec_safe_push (args
, value
);
4449 decl
= lookup_literal_operator (name
, args
);
4450 if (decl
&& decl
!= error_mark_node
)
4452 result
= finish_call_expr (decl
, &args
, false, true,
4453 tf_warning_or_error
);
4455 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4457 warning_at (token
->location
, OPT_Woverflow
,
4458 "integer literal exceeds range of %qT type",
4459 long_long_unsigned_type_node
);
4464 warning_at (token
->location
, OPT_Woverflow
,
4465 "floating literal exceeds range of %qT type",
4466 long_double_type_node
);
4467 else if (overflow
< 0)
4468 warning_at (token
->location
, OPT_Woverflow
,
4469 "floating literal truncated to zero");
4475 /* If the numeric argument didn't work, look for a raw literal
4476 operator taking a const char* argument consisting of the number
4477 in string format. */
4479 vec_safe_push (args
, num_string
);
4480 decl
= lookup_literal_operator (name
, args
);
4481 if (decl
&& decl
!= error_mark_node
)
4483 result
= finish_call_expr (decl
, &args
, false, true,
4484 tf_warning_or_error
);
4488 /* If the raw literal didn't work, look for a non-type template
4489 function with parameter pack char.... Call the function with
4490 template parameter characters representing the number. */
4492 decl
= lookup_literal_operator (name
, args
);
4493 if (decl
&& decl
!= error_mark_node
)
4495 tree tmpl_args
= make_char_string_pack (num_string
);
4496 if (tmpl_args
== NULL_TREE
)
4498 error ("failed to translate literal to execution character set %qT",
4500 return error_mark_node
;
4502 decl
= lookup_template_function (decl
, tmpl_args
);
4503 result
= finish_call_expr (decl
, &args
, false, true,
4504 tf_warning_or_error
);
4508 /* In C++14 the standard library defines complex number suffixes that
4509 conflict with GNU extensions. Prefer them if <complex> is #included. */
4510 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4511 bool i14
= (cxx_dialect
> cxx11
4512 && (id_equal (suffix_id
, "i")
4513 || id_equal (suffix_id
, "if")
4514 || id_equal (suffix_id
, "il")));
4515 diagnostic_t kind
= DK_ERROR
;
4520 tree cxlit
= lookup_qualified_name (std_node
,
4521 get_identifier ("complex_literals"),
4523 if (cxlit
== error_mark_node
)
4525 /* No <complex>, so pedwarn and use GNU semantics. */
4527 opt
= OPT_Wpedantic
;
4532 = emit_diagnostic (kind
, input_location
, opt
,
4533 "unable to find numeric literal operator %qD", name
);
4536 /* Don't inform either. */;
4539 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4540 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4543 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4544 "GNU built-in suffix");
4547 inform (token
->location
, "use %<-fext-numeric-literals%> "
4548 "to enable more built-in suffixes");
4550 if (kind
== DK_ERROR
)
4551 value
= error_mark_node
;
4554 /* Use the built-in semantics. */
4556 if (id_equal (suffix_id
, "i"))
4558 if (TREE_CODE (value
) == INTEGER_CST
)
4559 type
= integer_type_node
;
4561 type
= double_type_node
;
4563 else if (id_equal (suffix_id
, "if"))
4564 type
= float_type_node
;
4565 else /* if (id_equal (suffix_id, "il")) */
4566 type
= long_double_type_node
;
4568 value
= build_complex (build_complex_type (type
),
4569 fold_convert (type
, integer_zero_node
),
4570 fold_convert (type
, value
));
4573 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4574 /* Avoid repeated diagnostics. */
4575 token
->u
.value
= value
;
4579 /* Parse a user-defined string constant. Returns a call to a user-defined
4580 literal operator taking a character pointer and the length of the string
4584 cp_parser_userdef_string_literal (tree literal
)
4586 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4587 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4588 tree value
= USERDEF_LITERAL_VALUE (literal
);
4589 int len
= TREE_STRING_LENGTH (value
)
4590 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4593 /* Build up a call to the user-defined operator. */
4594 /* Lookup the name we got back from the id-expression. */
4596 vec_safe_push (args
, value
);
4597 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4598 decl
= lookup_literal_operator (name
, args
);
4600 if (decl
&& decl
!= error_mark_node
)
4601 return finish_call_expr (decl
, &args
, false, true,
4602 tf_warning_or_error
);
4604 /* Look for a suitable template function, either (C++20) with a single
4605 parameter of class type, or (N3599) with typename parameter CharT and
4606 parameter pack CharT... */
4608 decl
= lookup_literal_operator (name
, args
);
4609 if (decl
&& decl
!= error_mark_node
)
4611 /* Use resolve_nondeduced_context to try to choose one form of template
4613 tree tmpl_args
= make_tree_vec (1);
4614 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4615 decl
= lookup_template_function (decl
, tmpl_args
);
4616 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4621 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4622 res
= resolve_nondeduced_context (decl
, tf_none
);
4626 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4627 TREE_OPERAND (decl
, 1) = tmpl_args
;
4628 return finish_call_expr (decl
, &args
, false, true,
4629 tf_warning_or_error
);
4632 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4633 name
, TREE_TYPE (value
), size_type_node
);
4634 return error_mark_node
;
4638 /* Basic concepts [gram.basic] */
4640 /* Parse a translation-unit.
4643 declaration-seq [opt] */
4646 cp_parser_translation_unit (cp_parser
* parser
)
4648 gcc_checking_assert (!cp_error_declarator
);
4650 /* Create the declarator obstack. */
4651 gcc_obstack_init (&declarator_obstack
);
4652 /* Create the error declarator. */
4653 cp_error_declarator
= make_declarator (cdk_error
);
4654 /* Create the empty parameter list. */
4655 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4657 /* Remember where the base of the declarator obstack lies. */
4658 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4660 bool implicit_extern_c
= false;
4664 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4666 /* If we're entering or exiting a region that's implicitly
4667 extern "C", modify the lang context appropriately. */
4668 if (implicit_extern_c
4669 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4671 implicit_extern_c
= !implicit_extern_c
;
4672 if (implicit_extern_c
)
4673 push_lang_context (lang_name_c
);
4675 pop_lang_context ();
4678 if (token
->type
== CPP_EOF
)
4681 if (token
->type
== CPP_CLOSE_BRACE
)
4683 cp_parser_error (parser
, "expected declaration");
4684 cp_lexer_consume_token (parser
->lexer
);
4685 /* If the next token is now a `;', consume it. */
4686 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4687 cp_lexer_consume_token (parser
->lexer
);
4690 cp_parser_toplevel_declaration (parser
);
4693 /* Get rid of the token array; we don't need it any more. */
4694 cp_lexer_destroy (parser
->lexer
);
4695 parser
->lexer
= NULL
;
4697 /* The EOF should have reset this. */
4698 gcc_checking_assert (!implicit_extern_c
);
4700 /* Make sure the declarator obstack was fully cleaned up. */
4701 gcc_assert (obstack_next_free (&declarator_obstack
)
4702 == declarator_obstack_base
);
4705 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4706 decltype context. */
4708 static inline tsubst_flags_t
4709 complain_flags (bool decltype_p
)
4711 tsubst_flags_t complain
= tf_warning_or_error
;
4713 complain
|= tf_decltype
;
4717 /* We're about to parse a collection of statements. If we're currently
4718 parsing tentatively, set up a firewall so that any nested
4719 cp_parser_commit_to_tentative_parse won't affect the current context. */
4721 static cp_token_position
4722 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4724 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4727 cp_parser_parse_tentatively (parser
);
4728 cp_parser_commit_to_topmost_tentative_parse (parser
);
4729 return cp_lexer_token_position (parser
->lexer
, false);
4732 /* We've finished parsing the collection of statements. Wrap up the
4733 firewall and replace the relevant tokens with the parsed form. */
4736 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4742 /* Finish the firewall level. */
4743 cp_parser_parse_definitely (parser
);
4744 /* And remember the result of the parse for when we try again. */
4745 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4746 token
->type
= CPP_PREPARSED_EXPR
;
4747 token
->u
.value
= expr
;
4748 token
->keyword
= RID_MAX
;
4749 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4752 /* Like the above functions, but let the user modify the tokens. Used by
4753 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4754 later parses, so it makes sense to localize the effects of
4755 cp_parser_commit_to_tentative_parse. */
4757 struct tentative_firewall
4762 tentative_firewall (cp_parser
*p
): parser(p
)
4764 /* If we're currently parsing tentatively, start a committed level as a
4765 firewall and then an inner tentative parse. */
4766 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4768 cp_parser_parse_tentatively (parser
);
4769 cp_parser_commit_to_topmost_tentative_parse (parser
);
4770 cp_parser_parse_tentatively (parser
);
4774 ~tentative_firewall()
4778 /* Finish the inner tentative parse and the firewall, propagating any
4779 uncommitted error state to the outer tentative parse. */
4780 bool err
= cp_parser_error_occurred (parser
);
4781 cp_parser_parse_definitely (parser
);
4782 cp_parser_parse_definitely (parser
);
4784 cp_parser_simulate_error (parser
);
4789 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4790 This class is for tracking such a matching pair of symbols.
4791 In particular, it tracks the location of the first token,
4792 so that if the second token is missing, we can highlight the
4793 location of the first token when notifying the user about the
4796 template <typename traits_t
>
4800 /* token_pair's ctor. */
4801 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4803 /* If the next token is the opening symbol for this pair, consume it and
4805 Otherwise, issue an error and return false.
4806 In either case, record the location of the opening token. */
4808 bool require_open (cp_parser
*parser
)
4810 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4811 return cp_parser_require (parser
, traits_t::open_token_type
,
4812 traits_t::required_token_open
);
4815 /* Consume the next token from PARSER, recording its location as
4816 that of the opening token within the pair. */
4818 cp_token
* consume_open (cp_parser
*parser
)
4820 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4821 gcc_assert (tok
->type
== traits_t::open_token_type
);
4822 m_open_loc
= tok
->location
;
4826 /* If the next token is the closing symbol for this pair, consume it
4828 Otherwise, issue an error, highlighting the location of the
4829 corresponding opening token, and return NULL. */
4831 cp_token
*require_close (cp_parser
*parser
) const
4833 return cp_parser_require (parser
, traits_t::close_token_type
,
4834 traits_t::required_token_close
,
4839 location_t m_open_loc
;
4842 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4844 struct matching_paren_traits
4846 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4847 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4848 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4849 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4852 /* "matching_parens" is a token_pair<T> class for tracking matching
4853 pairs of parentheses. */
4855 typedef token_pair
<matching_paren_traits
> matching_parens
;
4857 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4859 struct matching_brace_traits
4861 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4862 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4863 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4864 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4867 /* "matching_braces" is a token_pair<T> class for tracking matching
4870 typedef token_pair
<matching_brace_traits
> matching_braces
;
4873 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4874 enclosing parentheses. */
4877 cp_parser_statement_expr (cp_parser
*parser
)
4879 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4881 /* Consume the '('. */
4882 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4883 matching_parens parens
;
4884 parens
.consume_open (parser
);
4885 /* Start the statement-expression. */
4886 tree expr
= begin_stmt_expr ();
4887 /* Parse the compound-statement. */
4888 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4890 expr
= finish_stmt_expr (expr
, false);
4891 /* Consume the ')'. */
4892 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4893 if (!parens
.require_close (parser
))
4894 cp_parser_skip_to_end_of_statement (parser
);
4896 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4897 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4898 return cp_expr (expr
, combined_loc
);
4901 /* Expressions [gram.expr] */
4903 /* Parse a fold-operator.
4906 - * / % ^ & | = < > << >>
4907 = -= *= /= %= ^= &= |= <<= >>=
4908 == != <= >= && || , .* ->*
4910 This returns the tree code corresponding to the matched operator
4911 as an int. When the current token matches a compound assignment
4912 opertor, the resulting tree code is the negative value of the
4913 non-assignment operator. */
4916 cp_parser_fold_operator (cp_token
*token
)
4918 switch (token
->type
)
4920 case CPP_PLUS
: return PLUS_EXPR
;
4921 case CPP_MINUS
: return MINUS_EXPR
;
4922 case CPP_MULT
: return MULT_EXPR
;
4923 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4924 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4925 case CPP_XOR
: return BIT_XOR_EXPR
;
4926 case CPP_AND
: return BIT_AND_EXPR
;
4927 case CPP_OR
: return BIT_IOR_EXPR
;
4928 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4929 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4931 case CPP_EQ
: return -NOP_EXPR
;
4932 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4933 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4934 case CPP_MULT_EQ
: return -MULT_EXPR
;
4935 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4936 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4937 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4938 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4939 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4940 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4941 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4943 case CPP_EQ_EQ
: return EQ_EXPR
;
4944 case CPP_NOT_EQ
: return NE_EXPR
;
4945 case CPP_LESS
: return LT_EXPR
;
4946 case CPP_GREATER
: return GT_EXPR
;
4947 case CPP_LESS_EQ
: return LE_EXPR
;
4948 case CPP_GREATER_EQ
: return GE_EXPR
;
4950 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4951 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4953 case CPP_COMMA
: return COMPOUND_EXPR
;
4955 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4956 case CPP_DEREF_STAR
: return MEMBER_REF
;
4958 default: return ERROR_MARK
;
4962 /* Returns true if CODE indicates a binary expression, which is not allowed in
4963 the LHS of a fold-expression. More codes will need to be added to use this
4964 function in other contexts. */
4967 is_binary_op (tree_code code
)
4972 case POINTER_PLUS_EXPR
:
4975 case TRUNC_DIV_EXPR
:
4976 case TRUNC_MOD_EXPR
:
4992 case TRUTH_ANDIF_EXPR
:
4993 case TRUTH_ORIF_EXPR
:
5006 /* If the next token is a suitable fold operator, consume it and return as
5007 the function above. */
5010 cp_parser_fold_operator (cp_parser
*parser
)
5012 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5013 int code
= cp_parser_fold_operator (token
);
5014 if (code
!= ERROR_MARK
)
5015 cp_lexer_consume_token (parser
->lexer
);
5019 /* Parse a fold-expression.
5022 ( ... folding-operator cast-expression)
5023 ( cast-expression folding-operator ... )
5024 ( cast-expression folding operator ... folding-operator cast-expression)
5026 Note that the '(' and ')' are matched in primary expression. */
5029 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5036 cp_lexer_consume_token (parser
->lexer
);
5037 int op
= cp_parser_fold_operator (parser
);
5038 if (op
== ERROR_MARK
)
5040 cp_parser_error (parser
, "expected binary operator");
5041 return error_mark_node
;
5044 tree expr
= cp_parser_cast_expression (parser
, false, false,
5046 if (expr
== error_mark_node
)
5047 return error_mark_node
;
5048 return finish_left_unary_fold_expr (expr
, op
);
5051 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5052 int op
= cp_parser_fold_operator (parser
);
5053 if (op
== ERROR_MARK
)
5055 cp_parser_error (parser
, "expected binary operator");
5056 return error_mark_node
;
5059 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5061 cp_parser_error (parser
, "expected ...");
5062 return error_mark_node
;
5064 cp_lexer_consume_token (parser
->lexer
);
5066 /* The operands of a fold-expression are cast-expressions, so binary or
5067 conditional expressions are not allowed. We check this here to avoid
5068 tentative parsing. */
5069 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5070 /* OK, the expression was parenthesized. */;
5071 else if (is_binary_op (TREE_CODE (expr1
)))
5072 error_at (location_of (expr1
),
5073 "binary expression in operand of fold-expression");
5074 else if (TREE_CODE (expr1
) == COND_EXPR
5075 || (REFERENCE_REF_P (expr1
)
5076 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5077 error_at (location_of (expr1
),
5078 "conditional expression in operand of fold-expression");
5081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5082 return finish_right_unary_fold_expr (expr1
, op
);
5084 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5086 cp_parser_error (parser
, "mismatched operator in fold-expression");
5087 return error_mark_node
;
5089 cp_lexer_consume_token (parser
->lexer
);
5091 // Binary left or right fold.
5092 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5093 if (expr2
== error_mark_node
)
5094 return error_mark_node
;
5095 return finish_binary_fold_expr (expr1
, expr2
, op
);
5098 /* Parse a primary-expression.
5105 lambda-expression (C++11)
5110 ( compound-statement )
5111 __builtin_va_arg ( assignment-expression , type-id )
5112 __builtin_offsetof ( type-id , offsetof-expression )
5115 __has_nothrow_assign ( type-id )
5116 __has_nothrow_constructor ( type-id )
5117 __has_nothrow_copy ( type-id )
5118 __has_trivial_assign ( type-id )
5119 __has_trivial_constructor ( type-id )
5120 __has_trivial_copy ( type-id )
5121 __has_trivial_destructor ( type-id )
5122 __has_virtual_destructor ( type-id )
5123 __is_abstract ( type-id )
5124 __is_base_of ( type-id , type-id )
5125 __is_class ( type-id )
5126 __is_empty ( type-id )
5127 __is_enum ( type-id )
5128 __is_final ( type-id )
5129 __is_literal_type ( type-id )
5130 __is_pod ( type-id )
5131 __is_polymorphic ( type-id )
5132 __is_std_layout ( type-id )
5133 __is_trivial ( type-id )
5134 __is_union ( type-id )
5136 Objective-C++ Extension:
5144 ADDRESS_P is true iff this expression was immediately preceded by
5145 "&" and therefore might denote a pointer-to-member. CAST_P is true
5146 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5147 true iff this expression is a template argument.
5149 Returns a representation of the expression. Upon return, *IDK
5150 indicates what kind of id-expression (if any) was present. */
5153 cp_parser_primary_expression (cp_parser
*parser
,
5156 bool template_arg_p
,
5160 cp_token
*token
= NULL
;
5162 /* Assume the primary expression is not an id-expression. */
5163 *idk
= CP_ID_KIND_NONE
;
5165 /* Peek at the next token. */
5166 token
= cp_lexer_peek_token (parser
->lexer
);
5167 switch ((int) token
->type
)
5176 user-defined-literal */
5183 case CPP_PREPARSED_EXPR
:
5184 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5185 return cp_parser_userdef_numeric_literal (parser
);
5186 token
= cp_lexer_consume_token (parser
->lexer
);
5187 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5189 error_at (token
->location
,
5190 "fixed-point types not supported in C++");
5191 return error_mark_node
;
5193 /* Floating-point literals are only allowed in an integral
5194 constant expression if they are cast to an integral or
5195 enumeration type. */
5196 if (TREE_CODE (token
->u
.value
) == REAL_CST
5197 && parser
->integral_constant_expression_p
5200 /* CAST_P will be set even in invalid code like "int(2.7 +
5201 ...)". Therefore, we have to check that the next token
5202 is sure to end the cast. */
5205 cp_token
*next_token
;
5207 next_token
= cp_lexer_peek_token (parser
->lexer
);
5208 if (/* The comma at the end of an
5209 enumerator-definition. */
5210 next_token
->type
!= CPP_COMMA
5211 /* The curly brace at the end of an enum-specifier. */
5212 && next_token
->type
!= CPP_CLOSE_BRACE
5213 /* The end of a statement. */
5214 && next_token
->type
!= CPP_SEMICOLON
5215 /* The end of the cast-expression. */
5216 && next_token
->type
!= CPP_CLOSE_PAREN
5217 /* The end of an array bound. */
5218 && next_token
->type
!= CPP_CLOSE_SQUARE
5219 /* The closing ">" in a template-argument-list. */
5220 && (next_token
->type
!= CPP_GREATER
5221 || parser
->greater_than_is_operator_p
)
5222 /* C++0x only: A ">>" treated like two ">" tokens,
5223 in a template-argument-list. */
5224 && (next_token
->type
!= CPP_RSHIFT
5225 || (cxx_dialect
== cxx98
)
5226 || parser
->greater_than_is_operator_p
))
5230 /* If we are within a cast, then the constraint that the
5231 cast is to an integral or enumeration type will be
5232 checked at that point. If we are not within a cast, then
5233 this code is invalid. */
5235 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5237 return (cp_expr (token
->u
.value
, token
->location
)
5238 .maybe_add_location_wrapper ());
5240 case CPP_CHAR_USERDEF
:
5241 case CPP_CHAR16_USERDEF
:
5242 case CPP_CHAR32_USERDEF
:
5243 case CPP_WCHAR_USERDEF
:
5244 case CPP_UTF8CHAR_USERDEF
:
5245 return cp_parser_userdef_char_literal (parser
);
5251 case CPP_UTF8STRING
:
5252 case CPP_STRING_USERDEF
:
5253 case CPP_STRING16_USERDEF
:
5254 case CPP_STRING32_USERDEF
:
5255 case CPP_WSTRING_USERDEF
:
5256 case CPP_UTF8STRING_USERDEF
:
5257 /* ??? Should wide strings be allowed when parser->translate_strings_p
5258 is false (i.e. in attributes)? If not, we can kill the third
5259 argument to cp_parser_string_literal. */
5260 return (cp_parser_string_literal (parser
,
5261 parser
->translate_strings_p
,
5263 .maybe_add_location_wrapper ());
5265 case CPP_OPEN_PAREN
:
5266 /* If we see `( { ' then we are looking at the beginning of
5267 a GNU statement-expression. */
5268 if (cp_parser_allow_gnu_extensions_p (parser
)
5269 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5271 /* Statement-expressions are not allowed by the standard. */
5272 pedwarn (token
->location
, OPT_Wpedantic
,
5273 "ISO C++ forbids braced-groups within expressions");
5275 /* And they're not allowed outside of a function-body; you
5276 cannot, for example, write:
5278 int i = ({ int j = 3; j + 1; });
5280 at class or namespace scope. */
5281 if (!parser
->in_function_body
5282 || parser
->in_template_argument_list_p
)
5284 error_at (token
->location
,
5285 "statement-expressions are not allowed outside "
5286 "functions nor in template-argument lists");
5287 cp_parser_skip_to_end_of_block_or_statement (parser
);
5288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5289 cp_lexer_consume_token (parser
->lexer
);
5290 return error_mark_node
;
5293 return cp_parser_statement_expr (parser
);
5295 /* Otherwise it's a normal parenthesized expression. */
5298 bool saved_greater_than_is_operator_p
;
5300 location_t open_paren_loc
= token
->location
;
5302 /* Consume the `('. */
5303 matching_parens parens
;
5304 parens
.consume_open (parser
);
5305 /* Within a parenthesized expression, a `>' token is always
5306 the greater-than operator. */
5307 saved_greater_than_is_operator_p
5308 = parser
->greater_than_is_operator_p
;
5309 parser
->greater_than_is_operator_p
= true;
5311 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5312 /* Left fold expression. */
5315 /* Parse the parenthesized expression. */
5316 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5318 token
= cp_lexer_peek_token (parser
->lexer
);
5319 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5321 expr
= cp_parser_fold_expression (parser
, expr
);
5322 if (expr
!= error_mark_node
5323 && cxx_dialect
< cxx17
5324 && !in_system_header_at (input_location
))
5325 pedwarn (input_location
, 0, "fold-expressions only available "
5326 "with %<-std=c++17%> or %<-std=gnu++17%>");
5329 /* Let the front end know that this expression was
5330 enclosed in parentheses. This matters in case, for
5331 example, the expression is of the form `A::B', since
5332 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5334 expr
= finish_parenthesized_expr (expr
);
5336 /* DR 705: Wrapping an unqualified name in parentheses
5337 suppresses arg-dependent lookup. We want to pass back
5338 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5339 (c++/37862), but none of the others. */
5340 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5341 *idk
= CP_ID_KIND_NONE
;
5343 /* The `>' token might be the end of a template-id or
5344 template-parameter-list now. */
5345 parser
->greater_than_is_operator_p
5346 = saved_greater_than_is_operator_p
;
5348 /* Consume the `)'. */
5349 token
= cp_lexer_peek_token (parser
->lexer
);
5350 location_t close_paren_loc
= token
->location
;
5351 expr
.set_range (open_paren_loc
, close_paren_loc
);
5352 if (!parens
.require_close (parser
)
5353 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5354 cp_parser_skip_to_end_of_statement (parser
);
5359 case CPP_OPEN_SQUARE
:
5361 if (c_dialect_objc ())
5363 /* We might have an Objective-C++ message. */
5364 cp_parser_parse_tentatively (parser
);
5365 tree msg
= cp_parser_objc_message_expression (parser
);
5366 /* If that works out, we're done ... */
5367 if (cp_parser_parse_definitely (parser
))
5369 /* ... else, fall though to see if it's a lambda. */
5371 cp_expr lam
= cp_parser_lambda_expression (parser
);
5372 /* Don't warn about a failed tentative parse. */
5373 if (cp_parser_error_occurred (parser
))
5374 return error_mark_node
;
5375 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5379 case CPP_OBJC_STRING
:
5380 if (c_dialect_objc ())
5381 /* We have an Objective-C++ string literal. */
5382 return cp_parser_objc_expression (parser
);
5383 cp_parser_error (parser
, "expected primary-expression");
5384 return error_mark_node
;
5387 switch (token
->keyword
)
5389 /* These two are the boolean literals. */
5391 cp_lexer_consume_token (parser
->lexer
);
5392 return cp_expr (boolean_true_node
, token
->location
);
5394 cp_lexer_consume_token (parser
->lexer
);
5395 return cp_expr (boolean_false_node
, token
->location
);
5397 /* The `__null' literal. */
5399 cp_lexer_consume_token (parser
->lexer
);
5400 return cp_expr (null_node
, token
->location
);
5402 /* The `nullptr' literal. */
5404 cp_lexer_consume_token (parser
->lexer
);
5405 return cp_expr (nullptr_node
, token
->location
);
5407 /* Recognize the `this' keyword. */
5409 cp_lexer_consume_token (parser
->lexer
);
5410 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5412 error_at (token
->location
,
5413 "%<this%> may not be used in this context");
5414 return error_mark_node
;
5416 /* Pointers cannot appear in constant-expressions. */
5417 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5418 return error_mark_node
;
5419 return cp_expr (finish_this_expr (), token
->location
);
5421 /* The `operator' keyword can be the beginning of an
5426 case RID_FUNCTION_NAME
:
5427 case RID_PRETTY_FUNCTION_NAME
:
5428 case RID_C99_FUNCTION_NAME
:
5430 non_integral_constant name
;
5432 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5433 __func__ are the names of variables -- but they are
5434 treated specially. Therefore, they are handled here,
5435 rather than relying on the generic id-expression logic
5436 below. Grammatically, these names are id-expressions.
5438 Consume the token. */
5439 token
= cp_lexer_consume_token (parser
->lexer
);
5441 switch (token
->keyword
)
5443 case RID_FUNCTION_NAME
:
5444 name
= NIC_FUNC_NAME
;
5446 case RID_PRETTY_FUNCTION_NAME
:
5447 name
= NIC_PRETTY_FUNC
;
5449 case RID_C99_FUNCTION_NAME
:
5450 name
= NIC_C99_FUNC
;
5456 if (cp_parser_non_integral_constant_expression (parser
, name
))
5457 return error_mark_node
;
5459 /* Look up the name. */
5460 return finish_fname (token
->u
.value
);
5467 location_t type_location
;
5468 location_t start_loc
5469 = cp_lexer_peek_token (parser
->lexer
)->location
;
5470 /* The `__builtin_va_arg' construct is used to handle
5471 `va_arg'. Consume the `__builtin_va_arg' token. */
5472 cp_lexer_consume_token (parser
->lexer
);
5473 /* Look for the opening `('. */
5474 matching_parens parens
;
5475 parens
.require_open (parser
);
5476 /* Now, parse the assignment-expression. */
5477 expression
= cp_parser_assignment_expression (parser
);
5478 /* Look for the `,'. */
5479 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5480 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5481 /* Parse the type-id. */
5483 type_id_in_expr_sentinel
s (parser
);
5484 type
= cp_parser_type_id (parser
);
5486 /* Look for the closing `)'. */
5487 location_t finish_loc
5488 = cp_lexer_peek_token (parser
->lexer
)->location
;
5489 parens
.require_close (parser
);
5490 /* Using `va_arg' in a constant-expression is not
5492 if (cp_parser_non_integral_constant_expression (parser
,
5494 return error_mark_node
;
5495 /* Construct a location of the form:
5496 __builtin_va_arg (v, int)
5497 ~~~~~~~~~~~~~~~~~~~~~^~~~
5498 with the caret at the type, ranging from the start of the
5499 "__builtin_va_arg" token to the close paren. */
5500 location_t combined_loc
5501 = make_location (type_location
, start_loc
, finish_loc
);
5502 return build_x_va_arg (combined_loc
, expression
, type
);
5506 return cp_parser_builtin_offsetof (parser
);
5508 case RID_HAS_NOTHROW_ASSIGN
:
5509 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5510 case RID_HAS_NOTHROW_COPY
:
5511 case RID_HAS_TRIVIAL_ASSIGN
:
5512 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5513 case RID_HAS_TRIVIAL_COPY
:
5514 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5515 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5516 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5517 case RID_IS_ABSTRACT
:
5518 case RID_IS_AGGREGATE
:
5519 case RID_IS_BASE_OF
:
5524 case RID_IS_LITERAL_TYPE
:
5526 case RID_IS_POLYMORPHIC
:
5527 case RID_IS_SAME_AS
:
5528 case RID_IS_STD_LAYOUT
:
5529 case RID_IS_TRIVIAL
:
5530 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5531 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5532 case RID_IS_TRIVIALLY_COPYABLE
:
5534 case RID_IS_ASSIGNABLE
:
5535 case RID_IS_CONSTRUCTIBLE
:
5536 return cp_parser_trait_expr (parser
, token
->keyword
);
5540 return cp_parser_requires_expression (parser
);
5542 /* Objective-C++ expressions. */
5544 case RID_AT_PROTOCOL
:
5545 case RID_AT_SELECTOR
:
5546 return cp_parser_objc_expression (parser
);
5549 if (parser
->in_function_body
5550 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5553 error_at (token
->location
,
5554 "a template declaration cannot appear at block scope");
5555 cp_parser_skip_to_end_of_block_or_statement (parser
);
5556 return error_mark_node
;
5560 cp_parser_error (parser
, "expected primary-expression");
5561 return error_mark_node
;
5564 /* An id-expression can start with either an identifier, a
5565 `::' as the beginning of a qualified-id, or the "operator"
5569 case CPP_TEMPLATE_ID
:
5570 case CPP_NESTED_NAME_SPECIFIER
:
5573 cp_expr id_expression
;
5575 const char *error_msg
;
5578 cp_token
*id_expr_token
;
5580 /* Parse the id-expression. */
5582 = cp_parser_id_expression (parser
,
5583 /*template_keyword_p=*/false,
5584 /*check_dependency_p=*/true,
5586 /*declarator_p=*/false,
5587 /*optional_p=*/false);
5588 if (id_expression
== error_mark_node
)
5589 return error_mark_node
;
5590 id_expr_token
= token
;
5591 token
= cp_lexer_peek_token (parser
->lexer
);
5592 done
= (token
->type
!= CPP_OPEN_SQUARE
5593 && token
->type
!= CPP_OPEN_PAREN
5594 && token
->type
!= CPP_DOT
5595 && token
->type
!= CPP_DEREF
5596 && token
->type
!= CPP_PLUS_PLUS
5597 && token
->type
!= CPP_MINUS_MINUS
);
5598 /* If we have a template-id, then no further lookup is
5599 required. If the template-id was for a template-class, we
5600 will sometimes have a TYPE_DECL at this point. */
5601 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5602 || TREE_CODE (id_expression
) == TYPE_DECL
)
5603 decl
= id_expression
;
5604 /* Look up the name. */
5607 tree ambiguous_decls
;
5609 /* If we already know that this lookup is ambiguous, then
5610 we've already issued an error message; there's no reason
5612 if (id_expr_token
->type
== CPP_NAME
5613 && id_expr_token
->error_reported
)
5615 cp_parser_simulate_error (parser
);
5616 return error_mark_node
;
5619 decl
= cp_parser_lookup_name (parser
, id_expression
,
5622 /*is_namespace=*/false,
5623 /*check_dependency=*/true,
5625 id_expression
.get_location ());
5626 /* If the lookup was ambiguous, an error will already have
5628 if (ambiguous_decls
)
5629 return error_mark_node
;
5631 /* In Objective-C++, we may have an Objective-C 2.0
5632 dot-syntax for classes here. */
5633 if (c_dialect_objc ()
5634 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5635 && TREE_CODE (decl
) == TYPE_DECL
5636 && objc_is_class_name (decl
))
5639 cp_lexer_consume_token (parser
->lexer
);
5640 component
= cp_parser_identifier (parser
);
5641 if (component
== error_mark_node
)
5642 return error_mark_node
;
5644 tree result
= objc_build_class_component_ref (id_expression
,
5646 /* Build a location of the form:
5649 with caret at the start of the component name (at
5650 input_location), ranging from the start of the id_expression
5651 to the end of the component name. */
5652 location_t combined_loc
5653 = make_location (input_location
, id_expression
.get_start (),
5654 get_finish (input_location
));
5655 protected_set_expr_location (result
, combined_loc
);
5659 /* In Objective-C++, an instance variable (ivar) may be preferred
5660 to whatever cp_parser_lookup_name() found.
5661 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5662 rest of c-family, we have to do a little extra work to preserve
5663 any location information in cp_expr "decl". Given that
5664 objc_lookup_ivar is implemented in "c-family" and "objc", we
5665 have a trip through the pure "tree" type, rather than cp_expr.
5666 Naively copying it back to "decl" would implicitly give the
5667 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5668 store an EXPR_LOCATION. Hence we only update "decl" (and
5669 hence its location_t) if we get back a different tree node. */
5670 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5672 if (decl_tree
!= decl
.get_value ())
5673 decl
= cp_expr (decl_tree
);
5675 /* If name lookup gives us a SCOPE_REF, then the
5676 qualifying scope was dependent. */
5677 if (TREE_CODE (decl
) == SCOPE_REF
)
5679 /* At this point, we do not know if DECL is a valid
5680 integral constant expression. We assume that it is
5681 in fact such an expression, so that code like:
5683 template <int N> struct A {
5687 is accepted. At template-instantiation time, we
5688 will check that B<N>::i is actually a constant. */
5691 /* Check to see if DECL is a local variable in a context
5692 where that is forbidden. */
5693 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5694 && local_variable_p (decl
))
5696 error_at (id_expression
.get_location (),
5697 "local variable %qD may not appear in this context",
5699 return error_mark_node
;
5703 decl
= (finish_id_expression
5704 (id_expression
, decl
, parser
->scope
,
5706 parser
->integral_constant_expression_p
,
5707 parser
->allow_non_integral_constant_expression_p
,
5708 &parser
->non_integral_constant_expression_p
,
5709 template_p
, done
, address_p
,
5712 id_expression
.get_location ()));
5714 cp_parser_error (parser
, error_msg
);
5715 /* Build a location for an id-expression of the form:
5721 i.e. from the start of the first token to the end of the final
5722 token, with the caret at the start of the unqualified-id. */
5723 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5724 location_t start_loc
= get_start (id_expr_token
->location
);
5725 location_t finish_loc
= get_finish (id_expression
.get_location ());
5726 location_t combined_loc
5727 = make_location (caret_loc
, start_loc
, finish_loc
);
5729 decl
.set_location (combined_loc
);
5733 /* Anything else is an error. */
5735 cp_parser_error (parser
, "expected primary-expression");
5736 return error_mark_node
;
5740 static inline cp_expr
5741 cp_parser_primary_expression (cp_parser
*parser
,
5744 bool template_arg_p
,
5747 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5748 /*decltype*/false, idk
);
5751 /* Parse an id-expression.
5758 :: [opt] nested-name-specifier template [opt] unqualified-id
5760 :: operator-function-id
5763 Return a representation of the unqualified portion of the
5764 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5765 a `::' or nested-name-specifier.
5767 Often, if the id-expression was a qualified-id, the caller will
5768 want to make a SCOPE_REF to represent the qualified-id. This
5769 function does not do this in order to avoid wastefully creating
5770 SCOPE_REFs when they are not required.
5772 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5775 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5776 uninstantiated templates.
5778 If *TEMPLATE_P is non-NULL, it is set to true iff the
5779 `template' keyword is used to explicitly indicate that the entity
5780 named is a template.
5782 If DECLARATOR_P is true, the id-expression is appearing as part of
5783 a declarator, rather than as part of an expression. */
5786 cp_parser_id_expression (cp_parser
*parser
,
5787 bool template_keyword_p
,
5788 bool check_dependency_p
,
5793 bool global_scope_p
;
5794 bool nested_name_specifier_p
;
5796 /* Assume the `template' keyword was not used. */
5798 *template_p
= template_keyword_p
;
5800 /* Look for the optional `::' operator. */
5802 = (!template_keyword_p
5803 && (cp_parser_global_scope_opt (parser
,
5804 /*current_scope_valid_p=*/false)
5807 /* Look for the optional nested-name-specifier. */
5808 nested_name_specifier_p
5809 = (cp_parser_nested_name_specifier_opt (parser
,
5810 /*typename_keyword_p=*/false,
5817 /* If there is a nested-name-specifier, then we are looking at
5818 the first qualified-id production. */
5819 if (nested_name_specifier_p
)
5822 tree saved_object_scope
;
5823 tree saved_qualifying_scope
;
5824 cp_expr unqualified_id
;
5827 /* See if the next token is the `template' keyword. */
5829 template_p
= &is_template
;
5830 *template_p
= cp_parser_optional_template_keyword (parser
);
5831 /* Name lookup we do during the processing of the
5832 unqualified-id might obliterate SCOPE. */
5833 saved_scope
= parser
->scope
;
5834 saved_object_scope
= parser
->object_scope
;
5835 saved_qualifying_scope
= parser
->qualifying_scope
;
5836 /* Process the final unqualified-id. */
5837 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5840 /*optional_p=*/false);
5841 /* Restore the SAVED_SCOPE for our caller. */
5842 parser
->scope
= saved_scope
;
5843 parser
->object_scope
= saved_object_scope
;
5844 parser
->qualifying_scope
= saved_qualifying_scope
;
5846 return unqualified_id
;
5848 /* Otherwise, if we are in global scope, then we are looking at one
5849 of the other qualified-id productions. */
5850 else if (global_scope_p
)
5855 /* Peek at the next token. */
5856 token
= cp_lexer_peek_token (parser
->lexer
);
5858 /* If it's an identifier, and the next token is not a "<", then
5859 we can avoid the template-id case. This is an optimization
5860 for this common case. */
5861 if (token
->type
== CPP_NAME
5862 && !cp_parser_nth_token_starts_template_argument_list_p
5864 return cp_parser_identifier (parser
);
5866 cp_parser_parse_tentatively (parser
);
5867 /* Try a template-id. */
5868 id
= cp_parser_template_id (parser
,
5869 /*template_keyword_p=*/false,
5870 /*check_dependency_p=*/true,
5873 /* If that worked, we're done. */
5874 if (cp_parser_parse_definitely (parser
))
5877 /* Peek at the next token. (Changes in the token buffer may
5878 have invalidated the pointer obtained above.) */
5879 token
= cp_lexer_peek_token (parser
->lexer
);
5881 switch (token
->type
)
5884 return cp_parser_identifier (parser
);
5887 if (token
->keyword
== RID_OPERATOR
)
5888 return cp_parser_operator_function_id (parser
);
5892 cp_parser_error (parser
, "expected id-expression");
5893 return error_mark_node
;
5897 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5898 /*check_dependency_p=*/true,
5903 /* Parse an unqualified-id.
5907 operator-function-id
5908 conversion-function-id
5912 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5913 keyword, in a construct like `A::template ...'.
5915 Returns a representation of unqualified-id. For the `identifier'
5916 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5917 production a BIT_NOT_EXPR is returned; the operand of the
5918 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5919 other productions, see the documentation accompanying the
5920 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5921 names are looked up in uninstantiated templates. If DECLARATOR_P
5922 is true, the unqualified-id is appearing as part of a declarator,
5923 rather than as part of an expression. */
5926 cp_parser_unqualified_id (cp_parser
* parser
,
5927 bool template_keyword_p
,
5928 bool check_dependency_p
,
5934 /* Peek at the next token. */
5935 token
= cp_lexer_peek_token (parser
->lexer
);
5937 switch ((int) token
->type
)
5943 /* We don't know yet whether or not this will be a
5945 cp_parser_parse_tentatively (parser
);
5946 /* Try a template-id. */
5947 id
= cp_parser_template_id (parser
, template_keyword_p
,
5951 /* If it worked, we're done. */
5952 if (cp_parser_parse_definitely (parser
))
5954 /* Otherwise, it's an ordinary identifier. */
5955 return cp_parser_identifier (parser
);
5958 case CPP_TEMPLATE_ID
:
5959 return cp_parser_template_id (parser
, template_keyword_p
,
5967 tree qualifying_scope
;
5971 location_t tilde_loc
= token
->location
;
5973 /* Consume the `~' token. */
5974 cp_lexer_consume_token (parser
->lexer
);
5975 /* Parse the class-name. The standard, as written, seems to
5978 template <typename T> struct S { ~S (); };
5979 template <typename T> S<T>::~S() {}
5981 is invalid, since `~' must be followed by a class-name, but
5982 `S<T>' is dependent, and so not known to be a class.
5983 That's not right; we need to look in uninstantiated
5984 templates. A further complication arises from:
5986 template <typename T> void f(T t) {
5990 Here, it is not possible to look up `T' in the scope of `T'
5991 itself. We must look in both the current scope, and the
5992 scope of the containing complete expression.
5994 Yet another issue is:
6003 The standard does not seem to say that the `S' in `~S'
6004 should refer to the type `S' and not the data member
6007 /* DR 244 says that we look up the name after the "~" in the
6008 same scope as we looked up the qualifying name. That idea
6009 isn't fully worked out; it's more complicated than that. */
6010 scope
= parser
->scope
;
6011 object_scope
= parser
->object_scope
;
6012 qualifying_scope
= parser
->qualifying_scope
;
6014 /* Check for invalid scopes. */
6015 if (scope
== error_mark_node
)
6017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6018 cp_lexer_consume_token (parser
->lexer
);
6019 return error_mark_node
;
6021 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6023 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6024 error_at (token
->location
,
6025 "scope %qT before %<~%> is not a class-name",
6027 cp_parser_simulate_error (parser
);
6028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6029 cp_lexer_consume_token (parser
->lexer
);
6030 return error_mark_node
;
6032 gcc_assert (!scope
|| TYPE_P (scope
));
6034 token
= cp_lexer_peek_token (parser
->lexer
);
6036 /* Create a location with caret == start at the tilde,
6037 finishing at the end of the peeked token, e.g:
6041 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6043 /* If the name is of the form "X::~X" it's OK even if X is a
6047 && token
->type
== CPP_NAME
6048 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6050 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6051 || (CLASS_TYPE_P (scope
)
6052 && constructor_name_p (token
->u
.value
, scope
))))
6054 cp_lexer_consume_token (parser
->lexer
);
6055 return cp_expr (build_nt (BIT_NOT_EXPR
, scope
), loc
);
6058 /* ~auto means the destructor of whatever the object is. */
6059 if (cp_parser_is_keyword (token
, RID_AUTO
))
6061 if (cxx_dialect
< cxx14
)
6063 "%<~auto%> only available with "
6064 "%<-std=c++14%> or %<-std=gnu++14%>");
6065 cp_lexer_consume_token (parser
->lexer
);
6066 return cp_expr (build_nt (BIT_NOT_EXPR
, make_auto (), loc
));
6069 /* If there was an explicit qualification (S::~T), first look
6070 in the scope given by the qualification (i.e., S).
6072 Note: in the calls to cp_parser_class_name below we pass
6073 typename_type so that lookup finds the injected-class-name
6074 rather than the constructor. */
6076 type_decl
= NULL_TREE
;
6079 cp_parser_parse_tentatively (parser
);
6080 type_decl
= cp_parser_class_name (parser
,
6081 /*typename_keyword_p=*/false,
6082 /*template_keyword_p=*/false,
6084 /*check_dependency=*/false,
6085 /*class_head_p=*/false,
6087 if (cp_parser_parse_definitely (parser
))
6090 /* In "N::S::~S", look in "N" as well. */
6091 if (!done
&& scope
&& qualifying_scope
)
6093 cp_parser_parse_tentatively (parser
);
6094 parser
->scope
= qualifying_scope
;
6095 parser
->object_scope
= NULL_TREE
;
6096 parser
->qualifying_scope
= NULL_TREE
;
6098 = cp_parser_class_name (parser
,
6099 /*typename_keyword_p=*/false,
6100 /*template_keyword_p=*/false,
6102 /*check_dependency=*/false,
6103 /*class_head_p=*/false,
6105 if (cp_parser_parse_definitely (parser
))
6108 /* In "p->S::~T", look in the scope given by "*p" as well. */
6109 else if (!done
&& object_scope
)
6111 cp_parser_parse_tentatively (parser
);
6112 parser
->scope
= object_scope
;
6113 parser
->object_scope
= NULL_TREE
;
6114 parser
->qualifying_scope
= NULL_TREE
;
6116 = cp_parser_class_name (parser
,
6117 /*typename_keyword_p=*/false,
6118 /*template_keyword_p=*/false,
6120 /*check_dependency=*/false,
6121 /*class_head_p=*/false,
6123 if (cp_parser_parse_definitely (parser
))
6126 /* Look in the surrounding context. */
6129 parser
->scope
= NULL_TREE
;
6130 parser
->object_scope
= NULL_TREE
;
6131 parser
->qualifying_scope
= NULL_TREE
;
6132 if (processing_template_decl
)
6133 cp_parser_parse_tentatively (parser
);
6135 = cp_parser_class_name (parser
,
6136 /*typename_keyword_p=*/false,
6137 /*template_keyword_p=*/false,
6139 /*check_dependency=*/false,
6140 /*class_head_p=*/false,
6142 if (processing_template_decl
6143 && ! cp_parser_parse_definitely (parser
))
6145 /* We couldn't find a type with this name. If we're parsing
6146 tentatively, fail and try something else. */
6147 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6149 cp_parser_simulate_error (parser
);
6150 return error_mark_node
;
6152 /* Otherwise, accept it and check for a match at instantiation
6154 type_decl
= cp_parser_identifier (parser
);
6155 if (type_decl
!= error_mark_node
)
6156 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6157 return cp_expr (type_decl
, loc
);
6160 /* If an error occurred, assume that the name of the
6161 destructor is the same as the name of the qualifying
6162 class. That allows us to keep parsing after running
6163 into ill-formed destructor names. */
6164 if (type_decl
== error_mark_node
&& scope
)
6165 return build_nt (BIT_NOT_EXPR
, scope
);
6166 else if (type_decl
== error_mark_node
)
6167 return error_mark_node
;
6169 /* Check that destructor name and scope match. */
6170 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6172 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6174 "declaration of %<~%T%> as member of %qT",
6176 cp_parser_simulate_error (parser
);
6177 return error_mark_node
;
6182 A typedef-name that names a class shall not be used as the
6183 identifier in the declarator for a destructor declaration. */
6185 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6186 && !DECL_SELF_REFERENCE_P (type_decl
)
6187 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6189 "typedef-name %qD used as destructor declarator",
6192 return cp_expr (build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
), loc
));
6196 if (token
->keyword
== RID_OPERATOR
)
6200 /* This could be a template-id, so we try that first. */
6201 cp_parser_parse_tentatively (parser
);
6202 /* Try a template-id. */
6203 id
= cp_parser_template_id (parser
, template_keyword_p
,
6204 /*check_dependency_p=*/true,
6207 /* If that worked, we're done. */
6208 if (cp_parser_parse_definitely (parser
))
6210 /* We still don't know whether we're looking at an
6211 operator-function-id or a conversion-function-id. */
6212 cp_parser_parse_tentatively (parser
);
6213 /* Try an operator-function-id. */
6214 id
= cp_parser_operator_function_id (parser
);
6215 /* If that didn't work, try a conversion-function-id. */
6216 if (!cp_parser_parse_definitely (parser
))
6217 id
= cp_parser_conversion_function_id (parser
);
6226 cp_parser_error (parser
, "expected unqualified-id");
6227 return error_mark_node
;
6231 /* Parse an (optional) nested-name-specifier.
6233 nested-name-specifier: [C++98]
6234 class-or-namespace-name :: nested-name-specifier [opt]
6235 class-or-namespace-name :: template nested-name-specifier [opt]
6237 nested-name-specifier: [C++0x]
6240 nested-name-specifier identifier ::
6241 nested-name-specifier template [opt] simple-template-id ::
6243 PARSER->SCOPE should be set appropriately before this function is
6244 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6245 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6248 Sets PARSER->SCOPE to the class (TYPE) or namespace
6249 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6250 it unchanged if there is no nested-name-specifier. Returns the new
6251 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6253 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6254 part of a declaration and/or decl-specifier. */
6257 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6258 bool typename_keyword_p
,
6259 bool check_dependency_p
,
6261 bool is_declaration
,
6262 bool template_keyword_p
/* = false */)
6264 bool success
= false;
6265 cp_token_position start
= 0;
6268 /* Remember where the nested-name-specifier starts. */
6269 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6271 start
= cp_lexer_token_position (parser
->lexer
, false);
6272 push_deferring_access_checks (dk_deferred
);
6279 tree saved_qualifying_scope
;
6281 /* Spot cases that cannot be the beginning of a
6282 nested-name-specifier. */
6283 token
= cp_lexer_peek_token (parser
->lexer
);
6285 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6286 the already parsed nested-name-specifier. */
6287 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6289 /* Grab the nested-name-specifier and continue the loop. */
6290 cp_parser_pre_parsed_nested_name_specifier (parser
);
6291 /* If we originally encountered this nested-name-specifier
6292 with IS_DECLARATION set to false, we will not have
6293 resolved TYPENAME_TYPEs, so we must do so here. */
6295 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6297 new_scope
= resolve_typename_type (parser
->scope
,
6298 /*only_current_p=*/false);
6299 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6300 parser
->scope
= new_scope
;
6306 /* Spot cases that cannot be the beginning of a
6307 nested-name-specifier. On the second and subsequent times
6308 through the loop, we look for the `template' keyword. */
6309 if (success
&& token
->keyword
== RID_TEMPLATE
)
6311 /* A template-id can start a nested-name-specifier. */
6312 else if (token
->type
== CPP_TEMPLATE_ID
)
6314 /* DR 743: decltype can be used in a nested-name-specifier. */
6315 else if (token_is_decltype (token
))
6319 /* If the next token is not an identifier, then it is
6320 definitely not a type-name or namespace-name. */
6321 if (token
->type
!= CPP_NAME
)
6323 /* If the following token is neither a `<' (to begin a
6324 template-id), nor a `::', then we are not looking at a
6325 nested-name-specifier. */
6326 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6328 if (token
->type
== CPP_COLON
6329 && parser
->colon_corrects_to_scope_p
6330 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6332 gcc_rich_location
richloc (token
->location
);
6333 richloc
.add_fixit_replace ("::");
6335 "found %<:%> in nested-name-specifier, "
6337 token
->type
= CPP_SCOPE
;
6340 if (token
->type
!= CPP_SCOPE
6341 && !cp_parser_nth_token_starts_template_argument_list_p
6346 /* The nested-name-specifier is optional, so we parse
6348 cp_parser_parse_tentatively (parser
);
6350 /* Look for the optional `template' keyword, if this isn't the
6351 first time through the loop. */
6353 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6355 /* Save the old scope since the name lookup we are about to do
6356 might destroy it. */
6357 old_scope
= parser
->scope
;
6358 saved_qualifying_scope
= parser
->qualifying_scope
;
6359 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6360 look up names in "X<T>::I" in order to determine that "Y" is
6361 a template. So, if we have a typename at this point, we make
6362 an effort to look through it. */
6364 && !typename_keyword_p
6366 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6367 parser
->scope
= resolve_typename_type (parser
->scope
,
6368 /*only_current_p=*/false);
6369 /* Parse the qualifying entity. */
6371 = cp_parser_qualifying_entity (parser
,
6377 /* Look for the `::' token. */
6378 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6380 /* If we found what we wanted, we keep going; otherwise, we're
6382 if (!cp_parser_parse_definitely (parser
))
6384 bool error_p
= false;
6386 /* Restore the OLD_SCOPE since it was valid before the
6387 failed attempt at finding the last
6388 class-or-namespace-name. */
6389 parser
->scope
= old_scope
;
6390 parser
->qualifying_scope
= saved_qualifying_scope
;
6392 /* If the next token is a decltype, and the one after that is a
6393 `::', then the decltype has failed to resolve to a class or
6394 enumeration type. Give this error even when parsing
6395 tentatively since it can't possibly be valid--and we're going
6396 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6397 won't get another chance.*/
6398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6399 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6402 token
= cp_lexer_consume_token (parser
->lexer
);
6403 error_at (token
->location
, "%<decltype%> evaluates to %qT, "
6404 "which is not a class or enumeration type",
6405 token
->u
.tree_check_value
->value
);
6406 parser
->scope
= error_mark_node
;
6410 cp_lexer_consume_token (parser
->lexer
);
6413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6414 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6416 /* If we have a non-type template-id followed by ::, it can't
6417 possibly be valid. */
6418 token
= cp_lexer_peek_token (parser
->lexer
);
6419 tree tid
= token
->u
.tree_check_value
->value
;
6420 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6421 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6423 tree tmpl
= NULL_TREE
;
6424 if (is_overloaded_fn (tid
))
6426 tree fns
= get_fns (tid
);
6427 if (OVL_SINGLE_P (fns
))
6428 tmpl
= OVL_FIRST (fns
);
6429 error_at (token
->location
, "function template-id %qD "
6430 "in nested-name-specifier", tid
);
6434 /* Variable template. */
6435 tmpl
= TREE_OPERAND (tid
, 0);
6436 gcc_assert (variable_template_p (tmpl
));
6437 error_at (token
->location
, "variable template-id %qD "
6438 "in nested-name-specifier", tid
);
6441 inform (DECL_SOURCE_LOCATION (tmpl
),
6442 "%qD declared here", tmpl
);
6444 parser
->scope
= error_mark_node
;
6448 cp_lexer_consume_token (parser
->lexer
);
6449 cp_lexer_consume_token (parser
->lexer
);
6453 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6455 /* If the next token is an identifier, and the one after
6456 that is a `::', then any valid interpretation would have
6457 found a class-or-namespace-name. */
6458 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6459 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6461 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6464 token
= cp_lexer_consume_token (parser
->lexer
);
6467 if (!token
->error_reported
)
6470 tree ambiguous_decls
;
6472 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6474 /*is_template=*/false,
6475 /*is_namespace=*/false,
6476 /*check_dependency=*/true,
6479 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6480 error_at (token
->location
,
6481 "%qD used without template arguments",
6483 else if (ambiguous_decls
)
6485 // cp_parser_lookup_name has the same diagnostic,
6486 // thus make sure to emit it at most once.
6487 if (cp_parser_uncommitted_to_tentative_parse_p
6490 error_at (token
->location
,
6491 "reference to %qD is ambiguous",
6493 print_candidates (ambiguous_decls
);
6495 decl
= error_mark_node
;
6499 if (cxx_dialect
!= cxx98
)
6500 cp_parser_name_lookup_error
6501 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6504 cp_parser_name_lookup_error
6505 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6509 parser
->scope
= error_mark_node
;
6511 /* Treat this as a successful nested-name-specifier
6516 If the name found is not a class-name (clause
6517 _class_) or namespace-name (_namespace.def_), the
6518 program is ill-formed. */
6521 cp_lexer_consume_token (parser
->lexer
);
6525 /* We've found one valid nested-name-specifier. */
6527 /* Name lookup always gives us a DECL. */
6528 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6529 new_scope
= TREE_TYPE (new_scope
);
6530 /* Uses of "template" must be followed by actual templates. */
6531 if (template_keyword_p
6532 && !(CLASS_TYPE_P (new_scope
)
6533 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6534 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6535 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6536 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6537 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6538 == TEMPLATE_ID_EXPR
)))
6539 permerror (input_location
, TYPE_P (new_scope
)
6540 ? G_("%qT is not a template")
6541 : G_("%qD is not a template"),
6543 /* If it is a class scope, try to complete it; we are about to
6544 be looking up names inside the class. */
6545 if (TYPE_P (new_scope
)
6546 /* Since checking types for dependency can be expensive,
6547 avoid doing it if the type is already complete. */
6548 && !COMPLETE_TYPE_P (new_scope
)
6549 /* Do not try to complete dependent types. */
6550 && !dependent_type_p (new_scope
))
6552 new_scope
= complete_type (new_scope
);
6553 /* If it is a typedef to current class, use the current
6554 class instead, as the typedef won't have any names inside
6556 if (!COMPLETE_TYPE_P (new_scope
)
6557 && currently_open_class (new_scope
))
6558 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6560 /* Make sure we look in the right scope the next time through
6562 parser
->scope
= new_scope
;
6565 /* If parsing tentatively, replace the sequence of tokens that makes
6566 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6567 token. That way, should we re-parse the token stream, we will
6568 not have to repeat the effort required to do the parse, nor will
6569 we issue duplicate error messages. */
6570 if (success
&& start
)
6574 token
= cp_lexer_token_at (parser
->lexer
, start
);
6575 /* Reset the contents of the START token. */
6576 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6577 /* Retrieve any deferred checks. Do not pop this access checks yet
6578 so the memory will not be reclaimed during token replacing below. */
6579 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6580 token
->u
.tree_check_value
->value
= parser
->scope
;
6581 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6582 token
->u
.tree_check_value
->qualifying_scope
=
6583 parser
->qualifying_scope
;
6584 token
->keyword
= RID_MAX
;
6586 /* Purge all subsequent tokens. */
6587 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6591 pop_to_parent_deferring_access_checks ();
6593 return success
? parser
->scope
: NULL_TREE
;
6596 /* Parse a nested-name-specifier. See
6597 cp_parser_nested_name_specifier_opt for details. This function
6598 behaves identically, except that it will an issue an error if no
6599 nested-name-specifier is present. */
6602 cp_parser_nested_name_specifier (cp_parser
*parser
,
6603 bool typename_keyword_p
,
6604 bool check_dependency_p
,
6606 bool is_declaration
)
6610 /* Look for the nested-name-specifier. */
6611 scope
= cp_parser_nested_name_specifier_opt (parser
,
6616 /* If it was not present, issue an error message. */
6619 cp_parser_error (parser
, "expected nested-name-specifier");
6620 parser
->scope
= NULL_TREE
;
6626 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6627 this is either a class-name or a namespace-name (which corresponds
6628 to the class-or-namespace-name production in the grammar). For
6629 C++0x, it can also be a type-name that refers to an enumeration
6630 type or a simple-template-id.
6632 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6633 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6634 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6635 TYPE_P is TRUE iff the next name should be taken as a class-name,
6636 even the same name is declared to be another entity in the same
6639 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6640 specified by the class-or-namespace-name. If neither is found the
6641 ERROR_MARK_NODE is returned. */
6644 cp_parser_qualifying_entity (cp_parser
*parser
,
6645 bool typename_keyword_p
,
6646 bool template_keyword_p
,
6647 bool check_dependency_p
,
6649 bool is_declaration
)
6652 tree saved_qualifying_scope
;
6653 tree saved_object_scope
;
6656 bool successful_parse_p
;
6658 /* DR 743: decltype can appear in a nested-name-specifier. */
6659 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6661 scope
= cp_parser_decltype (parser
);
6662 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6663 && !MAYBE_CLASS_TYPE_P (scope
))
6665 cp_parser_simulate_error (parser
);
6666 return error_mark_node
;
6668 if (TYPE_NAME (scope
))
6669 scope
= TYPE_NAME (scope
);
6673 /* Before we try to parse the class-name, we must save away the
6674 current PARSER->SCOPE since cp_parser_class_name will destroy
6676 saved_scope
= parser
->scope
;
6677 saved_qualifying_scope
= parser
->qualifying_scope
;
6678 saved_object_scope
= parser
->object_scope
;
6679 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6680 there is no need to look for a namespace-name. */
6681 only_class_p
= template_keyword_p
6682 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6684 cp_parser_parse_tentatively (parser
);
6685 scope
= cp_parser_class_name (parser
,
6688 type_p
? class_type
: none_type
,
6690 /*class_head_p=*/false,
6692 /*enum_ok=*/cxx_dialect
> cxx98
);
6693 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6694 /* If that didn't work, try for a namespace-name. */
6695 if (!only_class_p
&& !successful_parse_p
)
6697 /* Restore the saved scope. */
6698 parser
->scope
= saved_scope
;
6699 parser
->qualifying_scope
= saved_qualifying_scope
;
6700 parser
->object_scope
= saved_object_scope
;
6701 /* If we are not looking at an identifier followed by the scope
6702 resolution operator, then this is not part of a
6703 nested-name-specifier. (Note that this function is only used
6704 to parse the components of a nested-name-specifier.) */
6705 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6706 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6707 return error_mark_node
;
6708 scope
= cp_parser_namespace_name (parser
);
6714 /* Return true if we are looking at a compound-literal, false otherwise. */
6717 cp_parser_compound_literal_p (cp_parser
*parser
)
6719 cp_lexer_save_tokens (parser
->lexer
);
6721 /* Skip tokens until the next token is a closing parenthesis.
6722 If we find the closing `)', and the next token is a `{', then
6723 we are looking at a compound-literal. */
6724 bool compound_literal_p
6725 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6726 /*consume_paren=*/true)
6727 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6729 /* Roll back the tokens we skipped. */
6730 cp_lexer_rollback_tokens (parser
->lexer
);
6732 return compound_literal_p
;
6735 /* Return true if EXPR is the integer constant zero or a complex constant
6736 of zero, without any folding, but ignoring location wrappers. */
6739 literal_integer_zerop (const_tree expr
)
6741 return (location_wrapper_p (expr
)
6742 && integer_zerop (TREE_OPERAND (expr
, 0)));
6745 /* Parse a postfix-expression.
6749 postfix-expression [ expression ]
6750 postfix-expression ( expression-list [opt] )
6751 simple-type-specifier ( expression-list [opt] )
6752 typename :: [opt] nested-name-specifier identifier
6753 ( expression-list [opt] )
6754 typename :: [opt] nested-name-specifier template [opt] template-id
6755 ( expression-list [opt] )
6756 postfix-expression . template [opt] id-expression
6757 postfix-expression -> template [opt] id-expression
6758 postfix-expression . pseudo-destructor-name
6759 postfix-expression -> pseudo-destructor-name
6760 postfix-expression ++
6761 postfix-expression --
6762 dynamic_cast < type-id > ( expression )
6763 static_cast < type-id > ( expression )
6764 reinterpret_cast < type-id > ( expression )
6765 const_cast < type-id > ( expression )
6766 typeid ( expression )
6772 ( type-id ) { initializer-list , [opt] }
6774 This extension is a GNU version of the C99 compound-literal
6775 construct. (The C99 grammar uses `type-name' instead of `type-id',
6776 but they are essentially the same concept.)
6778 If ADDRESS_P is true, the postfix expression is the operand of the
6779 `&' operator. CAST_P is true if this expression is the target of a
6782 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6783 class member access expressions [expr.ref].
6785 Returns a representation of the expression. */
6788 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6789 bool member_access_only_p
, bool decltype_p
,
6790 cp_id_kind
* pidk_return
)
6795 cp_id_kind idk
= CP_ID_KIND_NONE
;
6796 cp_expr postfix_expression
= NULL_TREE
;
6797 bool is_member_access
= false;
6799 /* Peek at the next token. */
6800 token
= cp_lexer_peek_token (parser
->lexer
);
6801 loc
= token
->location
;
6802 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6804 /* Some of the productions are determined by keywords. */
6805 keyword
= token
->keyword
;
6815 const char *saved_message
;
6816 bool saved_in_type_id_in_expr_p
;
6818 /* All of these can be handled in the same way from the point
6819 of view of parsing. Begin by consuming the token
6820 identifying the cast. */
6821 cp_lexer_consume_token (parser
->lexer
);
6823 /* New types cannot be defined in the cast. */
6824 saved_message
= parser
->type_definition_forbidden_message
;
6825 parser
->type_definition_forbidden_message
6826 = G_("types may not be defined in casts");
6828 /* Look for the opening `<'. */
6829 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6830 /* Parse the type to which we are casting. */
6831 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6832 parser
->in_type_id_in_expr_p
= true;
6833 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
6835 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6836 /* Look for the closing `>'. */
6837 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6838 /* Restore the old message. */
6839 parser
->type_definition_forbidden_message
= saved_message
;
6841 bool saved_greater_than_is_operator_p
6842 = parser
->greater_than_is_operator_p
;
6843 parser
->greater_than_is_operator_p
= true;
6845 /* And the expression which is being cast. */
6846 matching_parens parens
;
6847 parens
.require_open (parser
);
6848 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6849 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6851 location_t end_loc
= close_paren
?
6852 close_paren
->location
: UNKNOWN_LOCATION
;
6854 parser
->greater_than_is_operator_p
6855 = saved_greater_than_is_operator_p
;
6857 /* Only type conversions to integral or enumeration types
6858 can be used in constant-expressions. */
6859 if (!cast_valid_in_integral_constant_expression_p (type
)
6860 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6862 postfix_expression
= error_mark_node
;
6870 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6874 = build_static_cast (type
, expression
, tf_warning_or_error
);
6878 = build_reinterpret_cast (type
, expression
,
6879 tf_warning_or_error
);
6883 = build_const_cast (type
, expression
, tf_warning_or_error
);
6889 /* Construct a location e.g. :
6890 reinterpret_cast <int *> (expr)
6891 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6892 ranging from the start of the "*_cast" token to the final closing
6893 paren, with the caret at the start. */
6894 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6895 postfix_expression
.set_location (cp_cast_loc
);
6902 const char *saved_message
;
6903 bool saved_in_type_id_in_expr_p
;
6905 /* Consume the `typeid' token. */
6906 cp_lexer_consume_token (parser
->lexer
);
6907 /* Look for the `(' token. */
6908 matching_parens parens
;
6909 parens
.require_open (parser
);
6910 /* Types cannot be defined in a `typeid' expression. */
6911 saved_message
= parser
->type_definition_forbidden_message
;
6912 parser
->type_definition_forbidden_message
6913 = G_("types may not be defined in a %<typeid%> expression");
6914 /* We can't be sure yet whether we're looking at a type-id or an
6916 cp_parser_parse_tentatively (parser
);
6917 /* Try a type-id first. */
6918 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6919 parser
->in_type_id_in_expr_p
= true;
6920 type
= cp_parser_type_id (parser
);
6921 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6922 /* Look for the `)' token. Otherwise, we can't be sure that
6923 we're not looking at an expression: consider `typeid (int
6924 (3))', for example. */
6925 cp_token
*close_paren
= parens
.require_close (parser
);
6926 /* If all went well, simply lookup the type-id. */
6927 if (cp_parser_parse_definitely (parser
))
6928 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6929 /* Otherwise, fall back to the expression variant. */
6934 /* Look for an expression. */
6935 expression
= cp_parser_expression (parser
, & idk
);
6936 /* Compute its typeid. */
6937 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6938 /* Look for the `)' token. */
6939 close_paren
= parens
.require_close (parser
);
6941 /* Restore the saved message. */
6942 parser
->type_definition_forbidden_message
= saved_message
;
6943 /* `typeid' may not appear in an integral constant expression. */
6944 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6945 postfix_expression
= error_mark_node
;
6947 /* Construct a location e.g. :
6950 ranging from the start of the "typeid" token to the final closing
6951 paren, with the caret at the start. */
6954 location_t typeid_loc
6955 = make_location (start_loc
, start_loc
, close_paren
->location
);
6956 postfix_expression
.set_location (typeid_loc
);
6957 postfix_expression
.maybe_add_location_wrapper ();
6965 /* The syntax permitted here is the same permitted for an
6966 elaborated-type-specifier. */
6967 ++parser
->prevent_constrained_type_specifiers
;
6968 type
= cp_parser_elaborated_type_specifier (parser
,
6969 /*is_friend=*/false,
6970 /*is_declaration=*/false);
6971 --parser
->prevent_constrained_type_specifiers
;
6972 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6977 case RID_BUILTIN_SHUFFLE
:
6978 case RID_BUILTIN_LAUNDER
:
6980 vec
<tree
, va_gc
> *vec
;
6984 cp_lexer_consume_token (parser
->lexer
);
6985 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6986 /*cast_p=*/false, /*allow_expansion_p=*/true,
6987 /*non_constant_p=*/NULL
);
6990 postfix_expression
= error_mark_node
;
6994 FOR_EACH_VEC_ELT (*vec
, i
, p
)
7000 if (vec
->length () == 1)
7002 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7005 error_at (loc
, "wrong number of arguments to "
7006 "%<__builtin_addressof%>");
7007 postfix_expression
= error_mark_node
;
7011 case RID_BUILTIN_LAUNDER
:
7012 if (vec
->length () == 1)
7013 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7014 tf_warning_or_error
);
7017 error_at (loc
, "wrong number of arguments to "
7018 "%<__builtin_launder%>");
7019 postfix_expression
= error_mark_node
;
7023 case RID_BUILTIN_SHUFFLE
:
7024 if (vec
->length () == 2)
7026 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7027 (*vec
)[1], tf_warning_or_error
);
7028 else if (vec
->length () == 3)
7030 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7031 (*vec
)[2], tf_warning_or_error
);
7034 error_at (loc
, "wrong number of arguments to "
7035 "%<__builtin_shuffle%>");
7036 postfix_expression
= error_mark_node
;
7046 case RID_BUILTIN_CONVERTVECTOR
:
7050 /* Consume the `__builtin_convertvector' token. */
7051 cp_lexer_consume_token (parser
->lexer
);
7052 /* Look for the opening `('. */
7053 matching_parens parens
;
7054 parens
.require_open (parser
);
7055 /* Now, parse the assignment-expression. */
7056 expression
= cp_parser_assignment_expression (parser
);
7057 /* Look for the `,'. */
7058 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7059 location_t type_location
7060 = cp_lexer_peek_token (parser
->lexer
)->location
;
7061 /* Parse the type-id. */
7063 type_id_in_expr_sentinel
s (parser
);
7064 type
= cp_parser_type_id (parser
);
7066 /* Look for the closing `)'. */
7067 parens
.require_close (parser
);
7068 return cp_build_vec_convert (expression
, type_location
, type
,
7069 tf_warning_or_error
);
7076 /* If the next thing is a simple-type-specifier, we may be
7077 looking at a functional cast. We could also be looking at
7078 an id-expression. So, we try the functional cast, and if
7079 that doesn't work we fall back to the primary-expression. */
7080 cp_parser_parse_tentatively (parser
);
7081 /* Look for the simple-type-specifier. */
7082 ++parser
->prevent_constrained_type_specifiers
;
7083 type
= cp_parser_simple_type_specifier (parser
,
7084 /*decl_specs=*/NULL
,
7085 CP_PARSER_FLAGS_NONE
);
7086 --parser
->prevent_constrained_type_specifiers
;
7087 /* Parse the cast itself. */
7088 if (!cp_parser_error_occurred (parser
))
7090 = cp_parser_functional_cast (parser
, type
);
7091 /* If that worked, we're done. */
7092 if (cp_parser_parse_definitely (parser
))
7095 /* If the functional-cast didn't work out, try a
7096 compound-literal. */
7097 if (cp_parser_allow_gnu_extensions_p (parser
)
7098 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7100 cp_expr initializer
= NULL_TREE
;
7102 cp_parser_parse_tentatively (parser
);
7104 matching_parens parens
;
7105 parens
.consume_open (parser
);
7107 /* Avoid calling cp_parser_type_id pointlessly, see comment
7108 in cp_parser_cast_expression about c++/29234. */
7109 if (!cp_parser_compound_literal_p (parser
))
7110 cp_parser_simulate_error (parser
);
7113 /* Parse the type. */
7114 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7115 parser
->in_type_id_in_expr_p
= true;
7116 type
= cp_parser_type_id (parser
);
7117 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7118 parens
.require_close (parser
);
7121 /* If things aren't going well, there's no need to
7123 if (!cp_parser_error_occurred (parser
))
7125 bool non_constant_p
;
7126 /* Parse the brace-enclosed initializer list. */
7127 initializer
= cp_parser_braced_list (parser
,
7130 /* If that worked, we're definitely looking at a
7131 compound-literal expression. */
7132 if (cp_parser_parse_definitely (parser
))
7134 /* Warn the user that a compound literal is not
7135 allowed in standard C++. */
7136 pedwarn (input_location
, OPT_Wpedantic
,
7137 "ISO C++ forbids compound-literals");
7138 /* For simplicity, we disallow compound literals in
7139 constant-expressions. We could
7140 allow compound literals of integer type, whose
7141 initializer was a constant, in constant
7142 expressions. Permitting that usage, as a further
7143 extension, would not change the meaning of any
7144 currently accepted programs. (Of course, as
7145 compound literals are not part of ISO C++, the
7146 standard has nothing to say.) */
7147 if (cp_parser_non_integral_constant_expression (parser
,
7150 postfix_expression
= error_mark_node
;
7153 /* Form the representation of the compound-literal. */
7155 = finish_compound_literal (type
, initializer
,
7156 tf_warning_or_error
, fcl_c99
);
7157 postfix_expression
.set_location (initializer
.get_location ());
7162 /* It must be a primary-expression. */
7164 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7165 /*template_arg_p=*/false,
7172 /* Note that we don't need to worry about calling build_cplus_new on a
7173 class-valued CALL_EXPR in decltype when it isn't the end of the
7174 postfix-expression; unary_complex_lvalue will take care of that for
7177 /* Keep looping until the postfix-expression is complete. */
7180 if (idk
== CP_ID_KIND_UNQUALIFIED
7181 && identifier_p (postfix_expression
)
7182 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7183 /* It is not a Koenig lookup function call. */
7185 = unqualified_name_lookup_error (postfix_expression
);
7187 /* Peek at the next token. */
7188 token
= cp_lexer_peek_token (parser
->lexer
);
7190 switch (token
->type
)
7192 case CPP_OPEN_SQUARE
:
7193 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7195 cp_parser_error (parser
,
7196 "two consecutive %<[%> shall "
7197 "only introduce an attribute");
7198 return error_mark_node
;
7201 = cp_parser_postfix_open_square_expression (parser
,
7205 postfix_expression
.set_range (start_loc
,
7206 postfix_expression
.get_location ());
7208 idk
= CP_ID_KIND_NONE
;
7209 is_member_access
= false;
7212 case CPP_OPEN_PAREN
:
7213 /* postfix-expression ( expression-list [opt] ) */
7216 bool is_builtin_constant_p
;
7217 bool saved_integral_constant_expression_p
= false;
7218 bool saved_non_integral_constant_expression_p
= false;
7219 tsubst_flags_t complain
= complain_flags (decltype_p
);
7220 vec
<tree
, va_gc
> *args
;
7221 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7223 is_member_access
= false;
7225 tree stripped_expression
7226 = tree_strip_any_location_wrapper (postfix_expression
);
7227 is_builtin_constant_p
7228 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7229 if (is_builtin_constant_p
)
7231 /* The whole point of __builtin_constant_p is to allow
7232 non-constant expressions to appear as arguments. */
7233 saved_integral_constant_expression_p
7234 = parser
->integral_constant_expression_p
;
7235 saved_non_integral_constant_expression_p
7236 = parser
->non_integral_constant_expression_p
;
7237 parser
->integral_constant_expression_p
= false;
7239 args
= (cp_parser_parenthesized_expression_list
7241 /*cast_p=*/false, /*allow_expansion_p=*/true,
7242 /*non_constant_p=*/NULL
,
7243 /*close_paren_loc=*/&close_paren_loc
,
7244 /*wrap_locations_p=*/true));
7245 if (is_builtin_constant_p
)
7247 parser
->integral_constant_expression_p
7248 = saved_integral_constant_expression_p
;
7249 parser
->non_integral_constant_expression_p
7250 = saved_non_integral_constant_expression_p
;
7255 postfix_expression
= error_mark_node
;
7259 /* Function calls are not permitted in
7260 constant-expressions. */
7261 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7262 && cp_parser_non_integral_constant_expression (parser
,
7265 postfix_expression
= error_mark_node
;
7266 release_tree_vector (args
);
7271 if (idk
== CP_ID_KIND_UNQUALIFIED
7272 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7274 if (identifier_p (postfix_expression
)
7275 /* In C++2A, we may need to perform ADL for a template
7277 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7278 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7280 if (!args
->is_empty ())
7283 if (!any_type_dependent_arguments_p (args
))
7285 = perform_koenig_lookup (postfix_expression
, args
,
7290 = unqualified_fn_lookup_error (postfix_expression
);
7292 /* We do not perform argument-dependent lookup if
7293 normal lookup finds a non-function, in accordance
7294 with the expected resolution of DR 218. */
7295 else if (!args
->is_empty ()
7296 && is_overloaded_fn (postfix_expression
))
7298 /* We only need to look at the first function,
7299 because all the fns share the attribute we're
7300 concerned with (all member fns or all local
7302 tree fn
= get_first_fn (postfix_expression
);
7303 fn
= STRIP_TEMPLATE (fn
);
7305 /* Do not do argument dependent lookup if regular
7306 lookup finds a member function or a block-scope
7307 function declaration. [basic.lookup.argdep]/3 */
7308 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7309 || DECL_FUNCTION_MEMBER_P (fn
)
7310 || DECL_LOCAL_FUNCTION_P (fn
)))
7313 if (!any_type_dependent_arguments_p (args
))
7315 = perform_koenig_lookup (postfix_expression
, args
,
7321 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7323 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7324 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7326 if (processing_template_decl
7327 && (type_dependent_object_expression_p (instance
)
7328 || (!BASELINK_P (fn
)
7329 && TREE_CODE (fn
) != FIELD_DECL
)
7330 || type_dependent_expression_p (fn
)
7331 || any_type_dependent_arguments_p (args
)))
7333 maybe_generic_this_capture (instance
, fn
);
7335 = build_min_nt_call_vec (postfix_expression
, args
);
7336 release_tree_vector (args
);
7340 if (BASELINK_P (fn
))
7343 = (build_new_method_call
7344 (instance
, fn
, &args
, NULL_TREE
,
7345 (idk
== CP_ID_KIND_QUALIFIED
7346 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7353 = finish_call_expr (postfix_expression
, &args
,
7354 /*disallow_virtual=*/false,
7358 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7359 || TREE_CODE (postfix_expression
) == MEMBER_REF
7360 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7361 postfix_expression
= (build_offset_ref_call_from_tree
7362 (postfix_expression
, &args
,
7364 else if (idk
== CP_ID_KIND_QUALIFIED
)
7365 /* A call to a static class member, or a namespace-scope
7368 = finish_call_expr (postfix_expression
, &args
,
7369 /*disallow_virtual=*/true,
7373 /* All other function calls. */
7375 = finish_call_expr (postfix_expression
, &args
,
7376 /*disallow_virtual=*/false,
7380 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7382 location_t combined_loc
= make_location (token
->location
,
7385 postfix_expression
.set_location (combined_loc
);
7388 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7389 idk
= CP_ID_KIND_NONE
;
7391 release_tree_vector (args
);
7397 /* postfix-expression . template [opt] id-expression
7398 postfix-expression . pseudo-destructor-name
7399 postfix-expression -> template [opt] id-expression
7400 postfix-expression -> pseudo-destructor-name */
7402 /* Consume the `.' or `->' operator. */
7403 cp_lexer_consume_token (parser
->lexer
);
7406 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7410 is_member_access
= true;
7414 /* postfix-expression ++ */
7415 /* Consume the `++' token. */
7416 cp_lexer_consume_token (parser
->lexer
);
7417 /* Generate a representation for the complete expression. */
7419 = finish_increment_expr (postfix_expression
,
7420 POSTINCREMENT_EXPR
);
7421 /* Increments may not appear in constant-expressions. */
7422 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7423 postfix_expression
= error_mark_node
;
7424 idk
= CP_ID_KIND_NONE
;
7425 is_member_access
= false;
7428 case CPP_MINUS_MINUS
:
7429 /* postfix-expression -- */
7430 /* Consume the `--' token. */
7431 cp_lexer_consume_token (parser
->lexer
);
7432 /* Generate a representation for the complete expression. */
7434 = finish_increment_expr (postfix_expression
,
7435 POSTDECREMENT_EXPR
);
7436 /* Decrements may not appear in constant-expressions. */
7437 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7438 postfix_expression
= error_mark_node
;
7439 idk
= CP_ID_KIND_NONE
;
7440 is_member_access
= false;
7444 if (pidk_return
!= NULL
)
7445 * pidk_return
= idk
;
7446 if (member_access_only_p
)
7447 return is_member_access
7448 ? postfix_expression
7449 : cp_expr (error_mark_node
);
7451 return postfix_expression
;
7455 /* We should never get here. */
7457 return error_mark_node
;
7460 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7461 by cp_parser_builtin_offsetof. We're looking for
7463 postfix-expression [ expression ]
7464 postfix-expression [ braced-init-list ] (C++11)
7466 FOR_OFFSETOF is set if we're being called in that context, which
7467 changes how we deal with integer constant expressions. */
7470 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7471 tree postfix_expression
,
7475 tree index
= NULL_TREE
;
7476 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7477 bool saved_greater_than_is_operator_p
;
7479 /* Consume the `[' token. */
7480 cp_lexer_consume_token (parser
->lexer
);
7482 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7483 parser
->greater_than_is_operator_p
= true;
7485 /* Parse the index expression. */
7486 /* ??? For offsetof, there is a question of what to allow here. If
7487 offsetof is not being used in an integral constant expression context,
7488 then we *could* get the right answer by computing the value at runtime.
7489 If we are in an integral constant expression context, then we might
7490 could accept any constant expression; hard to say without analysis.
7491 Rather than open the barn door too wide right away, allow only integer
7492 constant expressions here. */
7494 index
= cp_parser_constant_expression (parser
);
7497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7499 bool expr_nonconst_p
;
7500 cp_lexer_set_source_position (parser
->lexer
);
7501 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7502 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7505 index
= cp_parser_expression (parser
);
7508 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7510 /* Look for the closing `]'. */
7511 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7513 /* Build the ARRAY_REF. */
7514 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7517 /* When not doing offsetof, array references are not permitted in
7518 constant-expressions. */
7520 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7521 postfix_expression
= error_mark_node
;
7523 return postfix_expression
;
7526 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7527 dereference of incomplete type, returns true if error_mark_node should
7528 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7529 and *DEPENDENT_P. */
7532 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7535 /* In a template, be permissive by treating an object expression
7536 of incomplete type as dependent (after a pedwarn). */
7537 diagnostic_t kind
= (processing_template_decl
7538 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7540 switch (TREE_CODE (*postfix_expression
))
7543 case REINTERPRET_CAST_EXPR
:
7544 case CONST_CAST_EXPR
:
7545 case STATIC_CAST_EXPR
:
7546 case DYNAMIC_CAST_EXPR
:
7547 case IMPLICIT_CONV_EXPR
:
7548 case VIEW_CONVERT_EXPR
:
7549 case NON_LVALUE_EXPR
:
7553 /* Don't emit any diagnostic for OVERLOADs. */
7557 /* Avoid clobbering e.g. DECLs. */
7558 if (!EXPR_P (*postfix_expression
))
7563 if (kind
== DK_IGNORED
)
7566 location_t exploc
= location_of (*postfix_expression
);
7567 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7568 if (!MAYBE_CLASS_TYPE_P (*scope
))
7570 if (kind
== DK_ERROR
)
7571 *scope
= *postfix_expression
= error_mark_node
;
7572 else if (processing_template_decl
)
7574 *dependent_p
= true;
7575 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7580 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7581 by cp_parser_builtin_offsetof. We're looking for
7583 postfix-expression . template [opt] id-expression
7584 postfix-expression . pseudo-destructor-name
7585 postfix-expression -> template [opt] id-expression
7586 postfix-expression -> pseudo-destructor-name
7588 FOR_OFFSETOF is set if we're being called in that context. That sorta
7589 limits what of the above we'll actually accept, but nevermind.
7590 TOKEN_TYPE is the "." or "->" token, which will already have been
7591 removed from the stream. */
7594 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7595 enum cpp_ttype token_type
,
7596 cp_expr postfix_expression
,
7597 bool for_offsetof
, cp_id_kind
*idk
,
7598 location_t location
)
7602 bool pseudo_destructor_p
;
7603 tree scope
= NULL_TREE
;
7604 location_t start_loc
= postfix_expression
.get_start ();
7606 /* If this is a `->' operator, dereference the pointer. */
7607 if (token_type
== CPP_DEREF
)
7608 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7609 tf_warning_or_error
);
7610 /* Check to see whether or not the expression is type-dependent and
7611 not the current instantiation. */
7612 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7613 /* The identifier following the `->' or `.' is not qualified. */
7614 parser
->scope
= NULL_TREE
;
7615 parser
->qualifying_scope
= NULL_TREE
;
7616 parser
->object_scope
= NULL_TREE
;
7617 *idk
= CP_ID_KIND_NONE
;
7619 /* Enter the scope corresponding to the type of the object
7620 given by the POSTFIX_EXPRESSION. */
7623 scope
= TREE_TYPE (postfix_expression
);
7624 /* According to the standard, no expression should ever have
7625 reference type. Unfortunately, we do not currently match
7626 the standard in this respect in that our internal representation
7627 of an expression may have reference type even when the standard
7628 says it does not. Therefore, we have to manually obtain the
7629 underlying type here. */
7630 scope
= non_reference (scope
);
7631 /* The type of the POSTFIX_EXPRESSION must be complete. */
7632 /* Unlike the object expression in other contexts, *this is not
7633 required to be of complete type for purposes of class member
7634 access (5.2.5) outside the member function body. */
7635 if (postfix_expression
!= current_class_ref
7636 && scope
!= error_mark_node
7637 && !currently_open_class (scope
))
7639 scope
= complete_type (scope
);
7640 if (!COMPLETE_TYPE_P (scope
)
7641 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7643 return error_mark_node
;
7648 /* Let the name lookup machinery know that we are processing a
7649 class member access expression. */
7650 parser
->context
->object_type
= scope
;
7651 /* If something went wrong, we want to be able to discern that case,
7652 as opposed to the case where there was no SCOPE due to the type
7653 of expression being dependent. */
7655 scope
= error_mark_node
;
7656 /* If the SCOPE was erroneous, make the various semantic analysis
7657 functions exit quickly -- and without issuing additional error
7659 if (scope
== error_mark_node
)
7660 postfix_expression
= error_mark_node
;
7665 /* Tell cp_parser_lookup_name that there was an object, even though it's
7667 parser
->context
->object_type
= unknown_type_node
;
7669 /* Assume this expression is not a pseudo-destructor access. */
7670 pseudo_destructor_p
= false;
7672 /* If the SCOPE is a scalar type, then, if this is a valid program,
7673 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7674 is type dependent, it can be pseudo-destructor-name or something else.
7675 Try to parse it as pseudo-destructor-name first. */
7676 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7681 cp_parser_parse_tentatively (parser
);
7682 /* Parse the pseudo-destructor-name. */
7684 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7687 && (cp_parser_error_occurred (parser
)
7688 || !SCALAR_TYPE_P (type
)))
7689 cp_parser_abort_tentative_parse (parser
);
7690 else if (cp_parser_parse_definitely (parser
))
7692 pseudo_destructor_p
= true;
7694 = finish_pseudo_destructor_expr (postfix_expression
,
7699 if (!pseudo_destructor_p
)
7701 /* If the SCOPE is not a scalar type, we are looking at an
7702 ordinary class member access expression, rather than a
7703 pseudo-destructor-name. */
7705 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7706 /* Parse the id-expression. */
7707 name
= (cp_parser_id_expression
7709 cp_parser_optional_template_keyword (parser
),
7710 /*check_dependency_p=*/true,
7712 /*declarator_p=*/false,
7713 /*optional_p=*/false));
7714 /* In general, build a SCOPE_REF if the member name is qualified.
7715 However, if the name was not dependent and has already been
7716 resolved; there is no need to build the SCOPE_REF. For example;
7718 struct X { void f(); };
7719 template <typename T> void f(T* t) { t->X::f(); }
7721 Even though "t" is dependent, "X::f" is not and has been resolved
7722 to a BASELINK; there is no need to include scope information. */
7724 /* But we do need to remember that there was an explicit scope for
7725 virtual function calls. */
7727 *idk
= CP_ID_KIND_QUALIFIED
;
7729 /* If the name is a template-id that names a type, we will get a
7730 TYPE_DECL here. That is invalid code. */
7731 if (TREE_CODE (name
) == TYPE_DECL
)
7733 error_at (token
->location
, "invalid use of %qD", name
);
7734 postfix_expression
= error_mark_node
;
7738 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7740 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7742 error_at (token
->location
, "%<%D::%D%> is not a class member",
7743 parser
->scope
, name
);
7744 postfix_expression
= error_mark_node
;
7747 name
= build_qualified_name (/*type=*/NULL_TREE
,
7751 parser
->scope
= NULL_TREE
;
7752 parser
->qualifying_scope
= NULL_TREE
;
7753 parser
->object_scope
= NULL_TREE
;
7755 if (parser
->scope
&& name
&& BASELINK_P (name
))
7756 adjust_result_of_qualified_name_lookup
7757 (name
, parser
->scope
, scope
);
7759 = finish_class_member_access_expr (postfix_expression
, name
,
7761 tf_warning_or_error
);
7762 /* Build a location e.g.:
7765 where the caret is at the deref token, ranging from
7766 the start of postfix_expression to the end of the access expr. */
7768 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7769 location_t combined_loc
7770 = make_location (input_location
, start_loc
, end_loc
);
7771 protected_set_expr_location (postfix_expression
, combined_loc
);
7775 /* We no longer need to look up names in the scope of the object on
7776 the left-hand side of the `.' or `->' operator. */
7777 parser
->context
->object_type
= NULL_TREE
;
7779 /* Outside of offsetof, these operators may not appear in
7780 constant-expressions. */
7782 && (cp_parser_non_integral_constant_expression
7783 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7784 postfix_expression
= error_mark_node
;
7786 return postfix_expression
;
7789 /* Parse a parenthesized expression-list.
7792 assignment-expression
7793 expression-list, assignment-expression
7798 identifier, expression-list
7800 CAST_P is true if this expression is the target of a cast.
7802 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7805 WRAP_LOCATIONS_P is true if expressions within this list for which
7806 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7807 their source locations.
7809 Returns a vector of trees. Each element is a representation of an
7810 assignment-expression. NULL is returned if the ( and or ) are
7811 missing. An empty, but allocated, vector is returned on no
7812 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7813 if we are parsing an attribute list for an attribute that wants a
7814 plain identifier argument, normal_attr for an attribute that wants
7815 an expression, or non_attr if we aren't parsing an attribute list. If
7816 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7817 not all of the expressions in the list were constant.
7818 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7819 will be written to with the location of the closing parenthesis. If
7820 an error occurs, it may or may not be written to. */
7822 static vec
<tree
, va_gc
> *
7823 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7824 int is_attribute_list
,
7826 bool allow_expansion_p
,
7827 bool *non_constant_p
,
7828 location_t
*close_paren_loc
,
7829 bool wrap_locations_p
)
7831 vec
<tree
, va_gc
> *expression_list
;
7832 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7833 tree identifier
= NULL_TREE
;
7834 bool saved_greater_than_is_operator_p
;
7836 /* Assume all the expressions will be constant. */
7838 *non_constant_p
= false;
7840 matching_parens parens
;
7841 if (!parens
.require_open (parser
))
7844 expression_list
= make_tree_vector ();
7846 /* Within a parenthesized expression, a `>' token is always
7847 the greater-than operator. */
7848 saved_greater_than_is_operator_p
7849 = parser
->greater_than_is_operator_p
;
7850 parser
->greater_than_is_operator_p
= true;
7852 cp_expr
expr (NULL_TREE
);
7854 /* Consume expressions until there are no more. */
7855 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7858 /* At the beginning of attribute lists, check to see if the
7859 next token is an identifier. */
7860 if (is_attribute_list
== id_attr
7861 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7865 /* Consume the identifier. */
7866 token
= cp_lexer_consume_token (parser
->lexer
);
7867 /* Save the identifier. */
7868 identifier
= token
->u
.value
;
7872 bool expr_non_constant_p
;
7874 /* Parse the next assignment-expression. */
7875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7877 /* A braced-init-list. */
7878 cp_lexer_set_source_position (parser
->lexer
);
7879 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7880 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7881 if (non_constant_p
&& expr_non_constant_p
)
7882 *non_constant_p
= true;
7884 else if (non_constant_p
)
7886 expr
= (cp_parser_constant_expression
7887 (parser
, /*allow_non_constant_p=*/true,
7888 &expr_non_constant_p
));
7889 if (expr_non_constant_p
)
7890 *non_constant_p
= true;
7893 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7897 expr
= instantiate_non_dependent_expr (expr
);
7899 /* If we have an ellipsis, then this is an expression
7901 if (allow_expansion_p
7902 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7904 /* Consume the `...'. */
7905 cp_lexer_consume_token (parser
->lexer
);
7907 /* Build the argument pack. */
7908 expr
= make_pack_expansion (expr
);
7911 if (wrap_locations_p
)
7912 expr
.maybe_add_location_wrapper ();
7914 /* Add it to the list. We add error_mark_node
7915 expressions to the list, so that we can still tell if
7916 the correct form for a parenthesized expression-list
7917 is found. That gives better errors. */
7918 vec_safe_push (expression_list
, expr
.get_value ());
7920 if (expr
== error_mark_node
)
7924 /* After the first item, attribute lists look the same as
7925 expression lists. */
7926 is_attribute_list
= non_attr
;
7929 /* If the next token isn't a `,', then we are done. */
7930 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7933 /* Otherwise, consume the `,' and keep going. */
7934 cp_lexer_consume_token (parser
->lexer
);
7937 if (close_paren_loc
)
7938 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7940 if (!parens
.require_close (parser
))
7945 /* We try and resync to an unnested comma, as that will give the
7946 user better diagnostics. */
7947 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7948 /*recovering=*/true,
7950 /*consume_paren=*/true);
7955 parser
->greater_than_is_operator_p
7956 = saved_greater_than_is_operator_p
;
7961 parser
->greater_than_is_operator_p
7962 = saved_greater_than_is_operator_p
;
7965 vec_safe_insert (expression_list
, 0, identifier
);
7967 return expression_list
;
7970 /* Parse a pseudo-destructor-name.
7972 pseudo-destructor-name:
7973 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7974 :: [opt] nested-name-specifier template template-id :: ~ type-name
7975 :: [opt] nested-name-specifier [opt] ~ type-name
7977 If either of the first two productions is used, sets *SCOPE to the
7978 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7979 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7980 or ERROR_MARK_NODE if the parse fails. */
7983 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7988 bool nested_name_specifier_p
;
7991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7992 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7993 && !type_dependent_expression_p (object
))
7995 if (cxx_dialect
< cxx14
)
7996 pedwarn (input_location
, 0,
7997 "%<~auto%> only available with "
7998 "%<-std=c++14%> or %<-std=gnu++14%>");
7999 cp_lexer_consume_token (parser
->lexer
);
8000 cp_lexer_consume_token (parser
->lexer
);
8002 *type
= TREE_TYPE (object
);
8006 /* Assume that things will not work out. */
8007 *type
= error_mark_node
;
8009 /* Look for the optional `::' operator. */
8010 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8011 /* Look for the optional nested-name-specifier. */
8012 nested_name_specifier_p
8013 = (cp_parser_nested_name_specifier_opt (parser
,
8014 /*typename_keyword_p=*/false,
8015 /*check_dependency_p=*/true,
8017 /*is_declaration=*/false)
8019 /* Now, if we saw a nested-name-specifier, we might be doing the
8020 second production. */
8021 if (nested_name_specifier_p
8022 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8024 /* Consume the `template' keyword. */
8025 cp_lexer_consume_token (parser
->lexer
);
8026 /* Parse the template-id. */
8027 cp_parser_template_id (parser
,
8028 /*template_keyword_p=*/true,
8029 /*check_dependency_p=*/false,
8031 /*is_declaration=*/true);
8032 /* Look for the `::' token. */
8033 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8035 /* If the next token is not a `~', then there might be some
8036 additional qualification. */
8037 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8039 /* At this point, we're looking for "type-name :: ~". The type-name
8040 must not be a class-name, since this is a pseudo-destructor. So,
8041 it must be either an enum-name, or a typedef-name -- both of which
8042 are just identifiers. So, we peek ahead to check that the "::"
8043 and "~" tokens are present; if they are not, then we can avoid
8044 calling type_name. */
8045 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8046 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8047 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8049 cp_parser_error (parser
, "non-scalar type");
8053 /* Look for the type-name. */
8054 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8055 if (*scope
== error_mark_node
)
8058 /* Look for the `::' token. */
8059 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8064 /* Look for the `~'. */
8065 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8067 /* Once we see the ~, this has to be a pseudo-destructor. */
8068 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8069 cp_parser_commit_to_topmost_tentative_parse (parser
);
8071 /* Look for the type-name again. We are not responsible for
8072 checking that it matches the first type-name. */
8073 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8076 /* Parse a unary-expression.
8082 unary-operator cast-expression
8083 sizeof unary-expression
8085 alignof ( type-id ) [C++0x]
8092 __extension__ cast-expression
8093 __alignof__ unary-expression
8094 __alignof__ ( type-id )
8095 alignof unary-expression [C++0x]
8096 __real__ cast-expression
8097 __imag__ cast-expression
8099 sizeof ( type-id ) { initializer-list , [opt] }
8100 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8101 __alignof__ ( type-id ) { initializer-list , [opt] }
8103 ADDRESS_P is true iff the unary-expression is appearing as the
8104 operand of the `&' operator. CAST_P is true if this expression is
8105 the target of a cast.
8107 Returns a representation of the expression. */
8110 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8111 bool address_p
, bool cast_p
, bool decltype_p
)
8114 enum tree_code unary_operator
;
8116 /* Peek at the next token. */
8117 token
= cp_lexer_peek_token (parser
->lexer
);
8118 /* Some keywords give away the kind of expression. */
8119 if (token
->type
== CPP_KEYWORD
)
8121 enum rid keyword
= token
->keyword
;
8130 location_t start_loc
= token
->location
;
8132 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8133 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8135 /* Consume the token. */
8136 cp_lexer_consume_token (parser
->lexer
);
8137 /* Parse the operand. */
8138 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8140 if (TYPE_P (operand
))
8141 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8145 /* ISO C++ defines alignof only with types, not with
8146 expressions. So pedwarn if alignof is used with a non-
8147 type expression. However, __alignof__ is ok. */
8149 pedwarn (token
->location
, OPT_Wpedantic
,
8150 "ISO C++ does not allow %<alignof%> "
8153 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8155 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8156 SIZEOF_EXPR with the original operand. */
8157 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8159 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8161 if (!processing_template_decl
&& TYPE_P (operand
))
8163 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8164 build1 (NOP_EXPR
, operand
,
8166 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8169 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8170 TREE_SIDE_EFFECTS (ret
) = 0;
8171 TREE_READONLY (ret
) = 1;
8175 /* Construct a location e.g. :
8178 with start == caret at the start of the "alignof"/"sizeof"
8179 token, with the endpoint at the final closing paren. */
8180 location_t finish_loc
8181 = cp_lexer_previous_token (parser
->lexer
)->location
;
8182 location_t compound_loc
8183 = make_location (start_loc
, start_loc
, finish_loc
);
8185 cp_expr
ret_expr (ret
);
8186 ret_expr
.set_location (compound_loc
);
8187 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8191 case RID_BUILTIN_HAS_ATTRIBUTE
:
8192 return cp_parser_has_attribute_expression (parser
);
8195 return cp_parser_new_expression (parser
);
8198 return cp_parser_delete_expression (parser
);
8202 /* The saved value of the PEDANTIC flag. */
8206 /* Save away the PEDANTIC flag. */
8207 cp_parser_extension_opt (parser
, &saved_pedantic
);
8208 /* Parse the cast-expression. */
8209 expr
= cp_parser_simple_cast_expression (parser
);
8210 /* Restore the PEDANTIC flag. */
8211 pedantic
= saved_pedantic
;
8221 /* Consume the `__real__' or `__imag__' token. */
8222 cp_lexer_consume_token (parser
->lexer
);
8223 /* Parse the cast-expression. */
8224 expression
= cp_parser_simple_cast_expression (parser
);
8225 /* Create the complete representation. */
8226 return build_x_unary_op (token
->location
,
8227 (keyword
== RID_REALPART
8228 ? REALPART_EXPR
: IMAGPART_EXPR
),
8230 tf_warning_or_error
);
8234 case RID_TRANSACTION_ATOMIC
:
8235 case RID_TRANSACTION_RELAXED
:
8236 return cp_parser_transaction_expression (parser
, keyword
);
8241 const char *saved_message
;
8242 bool saved_integral_constant_expression_p
;
8243 bool saved_non_integral_constant_expression_p
;
8244 bool saved_greater_than_is_operator_p
;
8246 location_t start_loc
= token
->location
;
8248 cp_lexer_consume_token (parser
->lexer
);
8249 matching_parens parens
;
8250 parens
.require_open (parser
);
8252 saved_message
= parser
->type_definition_forbidden_message
;
8253 parser
->type_definition_forbidden_message
8254 = G_("types may not be defined in %<noexcept%> expressions");
8256 saved_integral_constant_expression_p
8257 = parser
->integral_constant_expression_p
;
8258 saved_non_integral_constant_expression_p
8259 = parser
->non_integral_constant_expression_p
;
8260 parser
->integral_constant_expression_p
= false;
8262 saved_greater_than_is_operator_p
8263 = parser
->greater_than_is_operator_p
;
8264 parser
->greater_than_is_operator_p
= true;
8266 ++cp_unevaluated_operand
;
8267 ++c_inhibit_evaluation_warnings
;
8268 ++cp_noexcept_operand
;
8269 expr
= cp_parser_expression (parser
);
8270 --cp_noexcept_operand
;
8271 --c_inhibit_evaluation_warnings
;
8272 --cp_unevaluated_operand
;
8274 parser
->greater_than_is_operator_p
8275 = saved_greater_than_is_operator_p
;
8277 parser
->integral_constant_expression_p
8278 = saved_integral_constant_expression_p
;
8279 parser
->non_integral_constant_expression_p
8280 = saved_non_integral_constant_expression_p
;
8282 parser
->type_definition_forbidden_message
= saved_message
;
8284 location_t finish_loc
8285 = cp_lexer_peek_token (parser
->lexer
)->location
;
8286 parens
.require_close (parser
);
8288 /* Construct a location of the form:
8291 with start == caret, finishing at the close-paren. */
8292 location_t noexcept_loc
8293 = make_location (start_loc
, start_loc
, finish_loc
);
8295 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8304 /* Look for the `:: new' and `:: delete', which also signal the
8305 beginning of a new-expression, or delete-expression,
8306 respectively. If the next token is `::', then it might be one of
8308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8312 /* See if the token after the `::' is one of the keywords in
8313 which we're interested. */
8314 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8315 /* If it's `new', we have a new-expression. */
8316 if (keyword
== RID_NEW
)
8317 return cp_parser_new_expression (parser
);
8318 /* Similarly, for `delete'. */
8319 else if (keyword
== RID_DELETE
)
8320 return cp_parser_delete_expression (parser
);
8323 /* Look for a unary operator. */
8324 unary_operator
= cp_parser_unary_operator (token
);
8325 /* The `++' and `--' operators can be handled similarly, even though
8326 they are not technically unary-operators in the grammar. */
8327 if (unary_operator
== ERROR_MARK
)
8329 if (token
->type
== CPP_PLUS_PLUS
)
8330 unary_operator
= PREINCREMENT_EXPR
;
8331 else if (token
->type
== CPP_MINUS_MINUS
)
8332 unary_operator
= PREDECREMENT_EXPR
;
8333 /* Handle the GNU address-of-label extension. */
8334 else if (cp_parser_allow_gnu_extensions_p (parser
)
8335 && token
->type
== CPP_AND_AND
)
8339 location_t start_loc
= token
->location
;
8341 /* Consume the '&&' token. */
8342 cp_lexer_consume_token (parser
->lexer
);
8343 /* Look for the identifier. */
8344 location_t finish_loc
8345 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8346 identifier
= cp_parser_identifier (parser
);
8347 /* Construct a location of the form:
8350 with caret==start at the "&&", finish at the end of the label. */
8351 location_t combined_loc
8352 = make_location (start_loc
, start_loc
, finish_loc
);
8353 /* Create an expression representing the address. */
8354 expression
= finish_label_address_expr (identifier
, combined_loc
);
8355 if (cp_parser_non_integral_constant_expression (parser
,
8357 expression
= error_mark_node
;
8361 if (unary_operator
!= ERROR_MARK
)
8363 cp_expr cast_expression
;
8364 cp_expr expression
= error_mark_node
;
8365 non_integral_constant non_constant_p
= NIC_NONE
;
8366 location_t loc
= token
->location
;
8367 tsubst_flags_t complain
= complain_flags (decltype_p
);
8369 /* Consume the operator token. */
8370 token
= cp_lexer_consume_token (parser
->lexer
);
8371 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8373 /* Parse the cast-expression. */
8375 = cp_parser_cast_expression (parser
,
8376 unary_operator
== ADDR_EXPR
,
8382 OP_TOKEN CAST_EXPRESSION
8383 ^~~~~~~~~~~~~~~~~~~~~~~~~
8384 with start==caret at the operator token, and
8385 extending to the end of the cast_expression. */
8386 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8388 /* Now, build an appropriate representation. */
8389 switch (unary_operator
)
8392 non_constant_p
= NIC_STAR
;
8393 expression
= build_x_indirect_ref (loc
, cast_expression
,
8396 /* TODO: build_x_indirect_ref does not always honor the
8397 location, so ensure it is set. */
8398 expression
.set_location (loc
);
8402 non_constant_p
= NIC_ADDR
;
8405 expression
= build_x_unary_op (loc
, unary_operator
,
8408 /* TODO: build_x_unary_op does not always honor the location,
8409 so ensure it is set. */
8410 expression
.set_location (loc
);
8413 case PREINCREMENT_EXPR
:
8414 case PREDECREMENT_EXPR
:
8415 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8416 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8419 /* Immediately fold negation of a constant, unless the constant is 0
8420 (since -0 == 0) or it would overflow. */
8421 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8424 = tree_strip_any_location_wrapper (cast_expression
);
8425 if (CONSTANT_CLASS_P (stripped_expr
)
8426 && !integer_zerop (stripped_expr
)
8427 && !TREE_OVERFLOW (stripped_expr
))
8429 tree folded
= fold_build1 (unary_operator
,
8430 TREE_TYPE (stripped_expr
),
8432 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8434 expression
= maybe_wrap_with_location (folded
, loc
);
8440 case UNARY_PLUS_EXPR
:
8441 case TRUTH_NOT_EXPR
:
8442 expression
= finish_unary_op_expr (loc
, unary_operator
,
8443 cast_expression
, complain
);
8450 if (non_constant_p
!= NIC_NONE
8451 && cp_parser_non_integral_constant_expression (parser
,
8453 expression
= error_mark_node
;
8458 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8459 /*member_access_only_p=*/false,
8464 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8465 unary-operator, the corresponding tree code is returned. */
8467 static enum tree_code
8468 cp_parser_unary_operator (cp_token
* token
)
8470 switch (token
->type
)
8473 return INDIRECT_REF
;
8479 return UNARY_PLUS_EXPR
;
8485 return TRUTH_NOT_EXPR
;
8488 return BIT_NOT_EXPR
;
8495 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8496 Returns a representation of the expression. */
8499 cp_parser_has_attribute_expression (cp_parser
*parser
)
8501 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8503 /* Consume the __builtin_has_attribute token. */
8504 cp_lexer_consume_token (parser
->lexer
);
8506 matching_parens parens
;
8507 if (!parens
.require_open (parser
))
8508 return error_mark_node
;
8510 /* Types cannot be defined in a `sizeof' expression. Save away the
8512 const char *saved_message
= parser
->type_definition_forbidden_message
;
8513 const char *saved_message_arg
8514 = parser
->type_definition_forbidden_message_arg
;
8515 parser
->type_definition_forbidden_message
8516 = G_("types may not be defined in %qs expressions");
8517 parser
->type_definition_forbidden_message_arg
8518 = IDENTIFIER_POINTER (ridpointers
[RID_BUILTIN_HAS_ATTRIBUTE
]);
8520 /* The restrictions on constant-expressions do not apply inside
8521 sizeof expressions. */
8522 bool saved_integral_constant_expression_p
8523 = parser
->integral_constant_expression_p
;
8524 bool saved_non_integral_constant_expression_p
8525 = parser
->non_integral_constant_expression_p
;
8526 parser
->integral_constant_expression_p
= false;
8528 /* Do not actually evaluate the expression. */
8529 ++cp_unevaluated_operand
;
8530 ++c_inhibit_evaluation_warnings
;
8532 tree oper
= NULL_TREE
;
8534 /* We can't be sure yet whether we're looking at a type-id or an
8536 cp_parser_parse_tentatively (parser
);
8538 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8539 parser
->in_type_id_in_expr_p
= true;
8540 /* Look for the type-id. */
8541 oper
= cp_parser_type_id (parser
);
8542 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8544 cp_parser_parse_definitely (parser
);
8546 /* If the type-id production did not work out, then we must be
8547 looking at an expression. */
8548 if (!oper
|| oper
== error_mark_node
)
8549 oper
= cp_parser_assignment_expression (parser
);
8551 STRIP_ANY_LOCATION_WRAPPER (oper
);
8553 /* Go back to evaluating expressions. */
8554 --cp_unevaluated_operand
;
8555 --c_inhibit_evaluation_warnings
;
8557 /* And restore the old one. */
8558 parser
->type_definition_forbidden_message
= saved_message
;
8559 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
8560 parser
->integral_constant_expression_p
8561 = saved_integral_constant_expression_p
;
8562 parser
->non_integral_constant_expression_p
8563 = saved_non_integral_constant_expression_p
;
8565 /* Consume the comma if it's there. */
8566 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8568 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8569 /*consume_paren=*/true);
8570 return error_mark_node
;
8573 /* Parse the attribute specification. */
8575 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8576 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8578 if (oper
!= error_mark_node
)
8580 /* Fold constant expressions used in attributes first. */
8581 cp_check_const_attributes (attr
);
8583 /* Finally, see if OPER has been declared with ATTR. */
8584 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8587 parens
.require_close (parser
);
8591 error_at (atloc
, "expected identifier");
8592 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8595 /* Construct a location e.g. :
8596 __builtin_has_attribute (oper, attr)
8597 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8598 with start == caret at the start of the built-in token,
8599 and with the endpoint at the final closing paren. */
8600 location_t finish_loc
8601 = cp_lexer_previous_token (parser
->lexer
)->location
;
8602 location_t compound_loc
8603 = make_location (start_loc
, start_loc
, finish_loc
);
8605 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8606 ret_expr
.set_location (compound_loc
);
8607 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8611 /* Parse a new-expression.
8614 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8615 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8617 Returns a representation of the expression. */
8620 cp_parser_new_expression (cp_parser
* parser
)
8622 bool global_scope_p
;
8623 vec
<tree
, va_gc
> *placement
;
8625 vec
<tree
, va_gc
> *initializer
;
8626 tree nelts
= NULL_TREE
;
8629 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8631 /* Look for the optional `::' operator. */
8633 = (cp_parser_global_scope_opt (parser
,
8634 /*current_scope_valid_p=*/false)
8636 /* Look for the `new' operator. */
8637 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8638 /* There's no easy way to tell a new-placement from the
8639 `( type-id )' construct. */
8640 cp_parser_parse_tentatively (parser
);
8641 /* Look for a new-placement. */
8642 placement
= cp_parser_new_placement (parser
);
8643 /* If that didn't work out, there's no new-placement. */
8644 if (!cp_parser_parse_definitely (parser
))
8646 if (placement
!= NULL
)
8647 release_tree_vector (placement
);
8651 /* If the next token is a `(', then we have a parenthesized
8653 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8656 const char *saved_message
= parser
->type_definition_forbidden_message
;
8658 /* Consume the `('. */
8659 matching_parens parens
;
8660 parens
.consume_open (parser
);
8662 /* Parse the type-id. */
8663 parser
->type_definition_forbidden_message
8664 = G_("types may not be defined in a new-expression");
8666 type_id_in_expr_sentinel
s (parser
);
8667 type
= cp_parser_type_id (parser
);
8669 parser
->type_definition_forbidden_message
= saved_message
;
8671 /* Look for the closing `)'. */
8672 parens
.require_close (parser
);
8673 token
= cp_lexer_peek_token (parser
->lexer
);
8674 /* There should not be a direct-new-declarator in this production,
8675 but GCC used to allowed this, so we check and emit a sensible error
8676 message for this case. */
8677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8679 error_at (token
->location
,
8680 "array bound forbidden after parenthesized type-id");
8681 inform (token
->location
,
8682 "try removing the parentheses around the type-id");
8683 cp_parser_direct_new_declarator (parser
);
8686 /* Otherwise, there must be a new-type-id. */
8688 type
= cp_parser_new_type_id (parser
, &nelts
);
8690 /* If the next token is a `(' or '{', then we have a new-initializer. */
8691 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8692 if (token
->type
== CPP_OPEN_PAREN
8693 || token
->type
== CPP_OPEN_BRACE
)
8694 initializer
= cp_parser_new_initializer (parser
);
8698 /* A new-expression may not appear in an integral constant
8700 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8701 ret
= error_mark_node
;
8702 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8703 of a new-type-id or type-id of a new-expression, the new-expression shall
8704 contain a new-initializer of the form ( assignment-expression )".
8705 Additionally, consistently with the spirit of DR 1467, we want to accept
8706 'new auto { 2 }' too. */
8707 else if ((ret
= type_uses_auto (type
))
8708 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8709 && (vec_safe_length (initializer
) != 1
8710 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8711 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8713 error_at (token
->location
,
8714 "initialization of new-expression for type %<auto%> "
8715 "requires exactly one element");
8716 ret
= error_mark_node
;
8720 /* Construct a location e.g.:
8723 with caret == start at the start of the "new" token, and the end
8724 at the end of the final token we consumed. */
8725 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8726 location_t end_loc
= get_finish (end_tok
->location
);
8727 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8729 /* Create a representation of the new-expression. */
8730 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8731 tf_warning_or_error
);
8732 protected_set_expr_location (ret
, combined_loc
);
8735 if (placement
!= NULL
)
8736 release_tree_vector (placement
);
8737 if (initializer
!= NULL
)
8738 release_tree_vector (initializer
);
8743 /* Parse a new-placement.
8748 Returns the same representation as for an expression-list. */
8750 static vec
<tree
, va_gc
> *
8751 cp_parser_new_placement (cp_parser
* parser
)
8753 vec
<tree
, va_gc
> *expression_list
;
8755 /* Parse the expression-list. */
8756 expression_list
= (cp_parser_parenthesized_expression_list
8757 (parser
, non_attr
, /*cast_p=*/false,
8758 /*allow_expansion_p=*/true,
8759 /*non_constant_p=*/NULL
));
8761 if (expression_list
&& expression_list
->is_empty ())
8762 error ("expected expression-list or type-id");
8764 return expression_list
;
8767 /* Parse a new-type-id.
8770 type-specifier-seq new-declarator [opt]
8772 Returns the TYPE allocated. If the new-type-id indicates an array
8773 type, *NELTS is set to the number of elements in the last array
8774 bound; the TYPE will not include the last array bound. */
8777 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8779 cp_decl_specifier_seq type_specifier_seq
;
8780 cp_declarator
*new_declarator
;
8781 cp_declarator
*declarator
;
8782 cp_declarator
*outer_declarator
;
8783 const char *saved_message
;
8785 /* The type-specifier sequence must not contain type definitions.
8786 (It cannot contain declarations of new types either, but if they
8787 are not definitions we will catch that because they are not
8789 saved_message
= parser
->type_definition_forbidden_message
;
8790 parser
->type_definition_forbidden_message
8791 = G_("types may not be defined in a new-type-id");
8792 /* Parse the type-specifier-seq. */
8793 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8794 /*is_declaration=*/false,
8795 /*is_trailing_return=*/false,
8796 &type_specifier_seq
);
8797 /* Restore the old message. */
8798 parser
->type_definition_forbidden_message
= saved_message
;
8800 if (type_specifier_seq
.type
== error_mark_node
)
8801 return error_mark_node
;
8803 /* Parse the new-declarator. */
8804 new_declarator
= cp_parser_new_declarator_opt (parser
);
8806 /* Determine the number of elements in the last array dimension, if
8809 /* Skip down to the last array dimension. */
8810 declarator
= new_declarator
;
8811 outer_declarator
= NULL
;
8812 while (declarator
&& (declarator
->kind
== cdk_pointer
8813 || declarator
->kind
== cdk_ptrmem
))
8815 outer_declarator
= declarator
;
8816 declarator
= declarator
->declarator
;
8819 && declarator
->kind
== cdk_array
8820 && declarator
->declarator
8821 && declarator
->declarator
->kind
== cdk_array
)
8823 outer_declarator
= declarator
;
8824 declarator
= declarator
->declarator
;
8827 if (declarator
&& declarator
->kind
== cdk_array
)
8829 *nelts
= declarator
->u
.array
.bounds
;
8830 if (*nelts
== error_mark_node
)
8831 *nelts
= integer_one_node
;
8833 if (outer_declarator
)
8834 outer_declarator
->declarator
= declarator
->declarator
;
8836 new_declarator
= NULL
;
8839 return groktypename (&type_specifier_seq
, new_declarator
, false);
8842 /* Parse an (optional) new-declarator.
8845 ptr-operator new-declarator [opt]
8846 direct-new-declarator
8848 Returns the declarator. */
8850 static cp_declarator
*
8851 cp_parser_new_declarator_opt (cp_parser
* parser
)
8853 enum tree_code code
;
8854 tree type
, std_attributes
= NULL_TREE
;
8855 cp_cv_quals cv_quals
;
8857 /* We don't know if there's a ptr-operator next, or not. */
8858 cp_parser_parse_tentatively (parser
);
8859 /* Look for a ptr-operator. */
8860 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8861 /* If that worked, look for more new-declarators. */
8862 if (cp_parser_parse_definitely (parser
))
8864 cp_declarator
*declarator
;
8866 /* Parse another optional declarator. */
8867 declarator
= cp_parser_new_declarator_opt (parser
);
8869 declarator
= cp_parser_make_indirect_declarator
8870 (code
, type
, cv_quals
, declarator
, std_attributes
);
8875 /* If the next token is a `[', there is a direct-new-declarator. */
8876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8877 return cp_parser_direct_new_declarator (parser
);
8882 /* Parse a direct-new-declarator.
8884 direct-new-declarator:
8886 direct-new-declarator [constant-expression]
8890 static cp_declarator
*
8891 cp_parser_direct_new_declarator (cp_parser
* parser
)
8893 cp_declarator
*declarator
= NULL
;
8900 /* Look for the opening `['. */
8901 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8903 token
= cp_lexer_peek_token (parser
->lexer
);
8904 expression
= cp_parser_expression (parser
);
8905 /* The standard requires that the expression have integral
8906 type. DR 74 adds enumeration types. We believe that the
8907 real intent is that these expressions be handled like the
8908 expression in a `switch' condition, which also allows
8909 classes with a single conversion to integral or
8910 enumeration type. */
8911 if (!processing_template_decl
)
8914 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8919 error_at (token
->location
,
8920 "expression in new-declarator must have integral "
8921 "or enumeration type");
8922 expression
= error_mark_node
;
8926 /* Look for the closing `]'. */
8927 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8929 /* Add this bound to the declarator. */
8930 declarator
= make_array_declarator (declarator
, expression
);
8932 /* If the next token is not a `[', then there are no more
8934 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8941 /* Parse a new-initializer.
8944 ( expression-list [opt] )
8947 Returns a representation of the expression-list. */
8949 static vec
<tree
, va_gc
> *
8950 cp_parser_new_initializer (cp_parser
* parser
)
8952 vec
<tree
, va_gc
> *expression_list
;
8954 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8957 bool expr_non_constant_p
;
8958 cp_lexer_set_source_position (parser
->lexer
);
8959 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8960 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8961 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8962 expression_list
= make_tree_vector_single (t
);
8965 expression_list
= (cp_parser_parenthesized_expression_list
8966 (parser
, non_attr
, /*cast_p=*/false,
8967 /*allow_expansion_p=*/true,
8968 /*non_constant_p=*/NULL
));
8970 return expression_list
;
8973 /* Parse a delete-expression.
8976 :: [opt] delete cast-expression
8977 :: [opt] delete [ ] cast-expression
8979 Returns a representation of the expression. */
8982 cp_parser_delete_expression (cp_parser
* parser
)
8984 bool global_scope_p
;
8988 /* Look for the optional `::' operator. */
8990 = (cp_parser_global_scope_opt (parser
,
8991 /*current_scope_valid_p=*/false)
8993 /* Look for the `delete' keyword. */
8994 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8995 /* See if the array syntax is in use. */
8996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8998 /* Consume the `[' token. */
8999 cp_lexer_consume_token (parser
->lexer
);
9000 /* Look for the `]' token. */
9001 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9002 /* Remember that this is the `[]' construct. */
9008 /* Parse the cast-expression. */
9009 expression
= cp_parser_simple_cast_expression (parser
);
9011 /* A delete-expression may not appear in an integral constant
9013 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9014 return error_mark_node
;
9016 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
9017 tf_warning_or_error
);
9020 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9021 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9025 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9027 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9028 switch (token
->type
)
9034 case CPP_CLOSE_SQUARE
:
9035 case CPP_CLOSE_PAREN
:
9036 case CPP_CLOSE_BRACE
:
9037 case CPP_OPEN_BRACE
:
9041 case CPP_DEREF_STAR
:
9049 case CPP_GREATER_EQ
:
9070 case CPP_OPEN_PAREN
:
9071 /* In ((type ()) () the last () isn't a valid cast-expression,
9072 so the whole must be parsed as postfix-expression. */
9073 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9076 case CPP_OPEN_SQUARE
:
9077 /* '[' may start a primary-expression in obj-c++ and in C++11,
9078 as a lambda-expression, eg, '(void)[]{}'. */
9079 if (cxx_dialect
>= cxx11
)
9081 return c_dialect_objc ();
9084 case CPP_MINUS_MINUS
:
9085 /* '++' and '--' may or may not start a cast-expression:
9087 struct T { void operator++(int); };
9088 void f() { (T())++; }
9101 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9102 in the order: const_cast, static_cast, reinterpret_cast.
9104 Don't suggest dynamic_cast.
9106 Return the first legal cast kind found, or NULL otherwise. */
9109 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9113 /* Reuse the parser logic by attempting to build the various kinds of
9114 cast, with "complain" disabled.
9115 Identify the first such cast that is valid. */
9117 /* Don't attempt to run such logic within template processing. */
9118 if (processing_template_decl
)
9121 /* First try const_cast. */
9122 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9123 if (trial
!= error_mark_node
)
9124 return "const_cast";
9126 /* If that fails, try static_cast. */
9127 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9128 if (trial
!= error_mark_node
)
9129 return "static_cast";
9131 /* Finally, try reinterpret_cast. */
9132 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9133 if (trial
!= error_mark_node
)
9134 return "reinterpret_cast";
9136 /* No such cast possible. */
9140 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9141 suggesting how to convert a C-style cast of the form:
9145 to a C++-style cast.
9147 The primary range of RICHLOC is asssumed to be that of the original
9148 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9149 of the parens in the C-style cast. */
9152 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9153 location_t close_paren_loc
, tree orig_expr
,
9156 /* This function is non-trivial, so bail out now if the warning isn't
9157 going to be emitted. */
9158 if (!warn_old_style_cast
)
9161 /* Try to find a legal C++ cast, trying them in order:
9162 const_cast, static_cast, reinterpret_cast. */
9163 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9164 if (!cast_suggestion
)
9167 /* Replace the open paren with "CAST_SUGGESTION<". */
9169 pp_printf (&pp
, "%s<", cast_suggestion
);
9170 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9172 /* Replace the close paren with "> (". */
9173 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9175 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9176 rich_loc
->add_fixit_insert_after (")");
9180 /* Parse a cast-expression.
9184 ( type-id ) cast-expression
9186 ADDRESS_P is true iff the unary-expression is appearing as the
9187 operand of the `&' operator. CAST_P is true if this expression is
9188 the target of a cast.
9190 Returns a representation of the expression. */
9193 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9194 bool decltype_p
, cp_id_kind
* pidk
)
9196 /* If it's a `(', then we might be looking at a cast. */
9197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9199 tree type
= NULL_TREE
;
9200 cp_expr
expr (NULL_TREE
);
9201 int cast_expression
= 0;
9202 const char *saved_message
;
9204 /* There's no way to know yet whether or not this is a cast.
9205 For example, `(int (3))' is a unary-expression, while `(int)
9206 3' is a cast. So, we resort to parsing tentatively. */
9207 cp_parser_parse_tentatively (parser
);
9208 /* Types may not be defined in a cast. */
9209 saved_message
= parser
->type_definition_forbidden_message
;
9210 parser
->type_definition_forbidden_message
9211 = G_("types may not be defined in casts");
9212 /* Consume the `('. */
9213 matching_parens parens
;
9214 cp_token
*open_paren
= parens
.consume_open (parser
);
9215 location_t open_paren_loc
= open_paren
->location
;
9216 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9218 /* A very tricky bit is that `(struct S) { 3 }' is a
9219 compound-literal (which we permit in C++ as an extension).
9220 But, that construct is not a cast-expression -- it is a
9221 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9222 is legal; if the compound-literal were a cast-expression,
9223 you'd need an extra set of parentheses.) But, if we parse
9224 the type-id, and it happens to be a class-specifier, then we
9225 will commit to the parse at that point, because we cannot
9226 undo the action that is done when creating a new class. So,
9227 then we cannot back up and do a postfix-expression.
9229 Another tricky case is the following (c++/29234):
9231 struct S { void operator () (); };
9238 As a type-id we parse the parenthesized S()() as a function
9239 returning a function, groktypename complains and we cannot
9240 back up in this case either.
9242 Therefore, we scan ahead to the closing `)', and check to see
9243 if the tokens after the `)' can start a cast-expression. Otherwise
9244 we are dealing with an unary-expression, a postfix-expression
9247 Yet another tricky case, in C++11, is the following (c++/54891):
9251 The issue is that usually, besides the case of lambda-expressions,
9252 the parenthesized type-id cannot be followed by '[', and, eg, we
9253 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9254 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9255 we don't commit, we try a cast-expression, then an unary-expression.
9257 Save tokens so that we can put them back. */
9258 cp_lexer_save_tokens (parser
->lexer
);
9260 /* We may be looking at a cast-expression. */
9261 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9262 /*consume_paren=*/true))
9264 = cp_parser_tokens_start_cast_expression (parser
);
9266 /* Roll back the tokens we skipped. */
9267 cp_lexer_rollback_tokens (parser
->lexer
);
9268 /* If we aren't looking at a cast-expression, simulate an error so
9269 that the call to cp_parser_error_occurred below returns true. */
9270 if (!cast_expression
)
9271 cp_parser_simulate_error (parser
);
9274 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9275 parser
->in_type_id_in_expr_p
= true;
9276 /* Look for the type-id. */
9277 type
= cp_parser_type_id (parser
);
9278 /* Look for the closing `)'. */
9279 cp_token
*close_paren
= parens
.require_close (parser
);
9281 close_paren_loc
= close_paren
->location
;
9282 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9285 /* Restore the saved message. */
9286 parser
->type_definition_forbidden_message
= saved_message
;
9288 /* At this point this can only be either a cast or a
9289 parenthesized ctor such as `(T ())' that looks like a cast to
9290 function returning T. */
9291 if (!cp_parser_error_occurred (parser
))
9293 /* Only commit if the cast-expression doesn't start with
9294 '++', '--', or '[' in C++11. */
9295 if (cast_expression
> 0)
9296 cp_parser_commit_to_topmost_tentative_parse (parser
);
9298 expr
= cp_parser_cast_expression (parser
,
9299 /*address_p=*/false,
9301 /*decltype_p=*/false,
9304 if (cp_parser_parse_definitely (parser
))
9306 /* Warn about old-style casts, if so requested. */
9307 if (warn_old_style_cast
9308 && !in_system_header_at (input_location
)
9309 && !VOID_TYPE_P (type
)
9310 && current_lang_name
!= lang_name_c
)
9312 gcc_rich_location
rich_loc (input_location
);
9313 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9315 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9316 "use of old-style cast to %q#T", type
);
9319 /* Only type conversions to integral or enumeration types
9320 can be used in constant-expressions. */
9321 if (!cast_valid_in_integral_constant_expression_p (type
)
9322 && cp_parser_non_integral_constant_expression (parser
,
9324 return error_mark_node
;
9326 /* Perform the cast. */
9330 with start==caret at the open paren, extending to the
9332 location_t cast_loc
= make_location (open_paren_loc
,
9334 expr
.get_finish ());
9335 expr
= build_c_cast (cast_loc
, type
, expr
);
9340 cp_parser_abort_tentative_parse (parser
);
9343 /* If we get here, then it's not a cast, so it must be a
9344 unary-expression. */
9345 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9346 cast_p
, decltype_p
);
9349 /* Parse a binary expression of the general form:
9353 pm-expression .* cast-expression
9354 pm-expression ->* cast-expression
9356 multiplicative-expression:
9358 multiplicative-expression * pm-expression
9359 multiplicative-expression / pm-expression
9360 multiplicative-expression % pm-expression
9362 additive-expression:
9363 multiplicative-expression
9364 additive-expression + multiplicative-expression
9365 additive-expression - multiplicative-expression
9369 shift-expression << additive-expression
9370 shift-expression >> additive-expression
9372 relational-expression:
9374 relational-expression < shift-expression
9375 relational-expression > shift-expression
9376 relational-expression <= shift-expression
9377 relational-expression >= shift-expression
9381 relational-expression:
9382 relational-expression <? shift-expression
9383 relational-expression >? shift-expression
9385 equality-expression:
9386 relational-expression
9387 equality-expression == relational-expression
9388 equality-expression != relational-expression
9392 and-expression & equality-expression
9394 exclusive-or-expression:
9396 exclusive-or-expression ^ and-expression
9398 inclusive-or-expression:
9399 exclusive-or-expression
9400 inclusive-or-expression | exclusive-or-expression
9402 logical-and-expression:
9403 inclusive-or-expression
9404 logical-and-expression && inclusive-or-expression
9406 logical-or-expression:
9407 logical-and-expression
9408 logical-or-expression || logical-and-expression
9410 All these are implemented with a single function like:
9413 simple-cast-expression
9414 binary-expression <token> binary-expression
9416 CAST_P is true if this expression is the target of a cast.
9418 The binops_by_token map is used to get the tree codes for each <token> type.
9419 binary-expressions are associated according to a precedence table. */
9421 #define TOKEN_PRECEDENCE(token) \
9422 (((token->type == CPP_GREATER \
9423 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9424 && !parser->greater_than_is_operator_p) \
9425 ? PREC_NOT_OPERATOR \
9426 : binops_by_token[token->type].prec)
9429 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9430 bool no_toplevel_fold_p
,
9432 enum cp_parser_prec prec
,
9435 cp_parser_expression_stack stack
;
9436 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9437 cp_parser_expression_stack_entry
*disable_warnings_sp
= NULL
;
9438 cp_parser_expression_stack_entry current
;
9441 enum tree_code rhs_type
;
9442 enum cp_parser_prec new_prec
, lookahead_prec
;
9445 /* Parse the first expression. */
9446 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9447 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9448 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9449 cast_p
, decltype_p
, pidk
);
9450 current
.prec
= prec
;
9452 if (cp_parser_error_occurred (parser
))
9453 return error_mark_node
;
9457 /* Get an operator token. */
9458 token
= cp_lexer_peek_token (parser
->lexer
);
9460 if (warn_cxx11_compat
9461 && token
->type
== CPP_RSHIFT
9462 && !parser
->greater_than_is_operator_p
)
9464 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9465 "%<>>%> operator is treated"
9466 " as two right angle brackets in C++11"))
9467 inform (token
->location
,
9468 "suggest parentheses around %<>>%> expression");
9471 new_prec
= TOKEN_PRECEDENCE (token
);
9472 if (new_prec
!= PREC_NOT_OPERATOR
9473 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9474 /* This is a fold-expression; handle it later. */
9475 new_prec
= PREC_NOT_OPERATOR
;
9477 /* Popping an entry off the stack means we completed a subexpression:
9478 - either we found a token which is not an operator (`>' where it is not
9479 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9480 will happen repeatedly;
9481 - or, we found an operator which has lower priority. This is the case
9482 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9484 if (new_prec
<= current
.prec
)
9493 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9494 current
.loc
= token
->location
;
9496 /* We used the operator token. */
9497 cp_lexer_consume_token (parser
->lexer
);
9499 /* For "false && x" or "true || x", x will never be executed;
9500 disable warnings while evaluating it. */
9501 if ((current
.tree_type
== TRUTH_ANDIF_EXPR
9502 && cp_fully_fold (current
.lhs
) == truthvalue_false_node
)
9503 || (current
.tree_type
== TRUTH_ORIF_EXPR
9504 && cp_fully_fold (current
.lhs
) == truthvalue_true_node
))
9506 disable_warnings_sp
= sp
;
9507 ++c_inhibit_evaluation_warnings
;
9510 /* Extract another operand. It may be the RHS of this expression
9511 or the LHS of a new, higher priority expression. */
9512 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9513 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9514 rhs
= cp_parser_simple_cast_expression (parser
);
9516 /* Get another operator token. Look up its precedence to avoid
9517 building a useless (immediately popped) stack entry for common
9518 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9519 token
= cp_lexer_peek_token (parser
->lexer
);
9520 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9521 if (lookahead_prec
!= PREC_NOT_OPERATOR
9522 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9523 lookahead_prec
= PREC_NOT_OPERATOR
;
9524 if (lookahead_prec
> new_prec
)
9526 /* ... and prepare to parse the RHS of the new, higher priority
9527 expression. Since precedence levels on the stack are
9528 monotonically increasing, we do not have to care about
9533 current
.lhs_type
= rhs_type
;
9534 current
.prec
= new_prec
;
9535 new_prec
= lookahead_prec
;
9539 lookahead_prec
= new_prec
;
9540 /* If the stack is not empty, we have parsed into LHS the right side
9541 (`4' in the example above) of an expression we had suspended.
9542 We can use the information on the stack to recover the LHS (`3')
9543 from the stack together with the tree code (`MULT_EXPR'), and
9544 the precedence of the higher level subexpression
9545 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9546 which will be used to actually build the additive expression. */
9548 rhs_type
= current
.lhs_type
;
9553 /* Undo the disabling of warnings done above. */
9554 if (sp
== disable_warnings_sp
)
9556 disable_warnings_sp
= NULL
;
9557 --c_inhibit_evaluation_warnings
;
9560 if (warn_logical_not_paren
9561 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9562 && current
.lhs_type
== TRUTH_NOT_EXPR
9563 /* Avoid warning for !!x == y. */
9564 && (TREE_CODE (current
.lhs
) != NE_EXPR
9565 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9566 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9567 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9568 /* Avoid warning for !b == y where b is boolean. */
9569 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9570 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9572 /* Avoid warning for !!b == y where b is boolean. */
9573 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9574 || TREE_TYPE (current
.lhs
) == NULL_TREE
9575 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9576 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9577 current
.lhs
, maybe_constant_value (rhs
));
9581 location_t combined_loc
= make_location (current
.loc
,
9582 current
.lhs
.get_start (),
9585 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9586 ERROR_MARK for everything that is not a binary expression.
9587 This makes warn_about_parentheses miss some warnings that
9588 involve unary operators. For unary expressions we should
9589 pass the correct tree_code unless the unary expression was
9590 surrounded by parentheses.
9592 if (no_toplevel_fold_p
9593 && lookahead_prec
<= current
.prec
9596 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9597 current
.lhs
= error_mark_node
;
9601 = build_min (current
.tree_type
,
9602 TREE_CODE_CLASS (current
.tree_type
)
9604 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9605 current
.lhs
.get_value (), rhs
.get_value ());
9606 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9611 op_location_t
op_loc (current
.loc
, combined_loc
);
9612 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9613 current
.lhs
, current
.lhs_type
,
9614 rhs
, rhs_type
, &overload
,
9615 complain_flags (decltype_p
));
9616 /* TODO: build_x_binary_op doesn't always honor the location. */
9617 current
.lhs
.set_location (combined_loc
);
9619 current
.lhs_type
= current
.tree_type
;
9621 /* If the binary operator required the use of an overloaded operator,
9622 then this expression cannot be an integral constant-expression.
9623 An overloaded operator can be used even if both operands are
9624 otherwise permissible in an integral constant-expression if at
9625 least one of the operands is of enumeration type. */
9628 && cp_parser_non_integral_constant_expression (parser
,
9630 return error_mark_node
;
9637 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9638 bool no_toplevel_fold_p
,
9639 enum cp_parser_prec prec
,
9642 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9643 /*decltype*/false, prec
, pidk
);
9646 /* Parse the `? expression : assignment-expression' part of a
9647 conditional-expression. The LOGICAL_OR_EXPR is the
9648 logical-or-expression that started the conditional-expression.
9649 Returns a representation of the entire conditional-expression.
9651 This routine is used by cp_parser_assignment_expression.
9653 ? expression : assignment-expression
9657 ? : assignment-expression */
9660 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9662 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9663 cp_expr assignment_expr
;
9664 struct cp_token
*token
;
9665 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9667 /* Consume the `?' token. */
9668 cp_lexer_consume_token (parser
->lexer
);
9669 token
= cp_lexer_peek_token (parser
->lexer
);
9670 if (cp_parser_allow_gnu_extensions_p (parser
)
9671 && token
->type
== CPP_COLON
)
9673 pedwarn (token
->location
, OPT_Wpedantic
,
9674 "ISO C++ does not allow %<?:%> with omitted middle operand");
9675 /* Implicit true clause. */
9677 c_inhibit_evaluation_warnings
+=
9678 folded_logical_or_expr
== truthvalue_true_node
;
9679 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9683 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9684 parser
->colon_corrects_to_scope_p
= false;
9685 /* Parse the expression. */
9686 c_inhibit_evaluation_warnings
+=
9687 folded_logical_or_expr
== truthvalue_false_node
;
9688 expr
= cp_parser_expression (parser
);
9689 c_inhibit_evaluation_warnings
+=
9690 ((folded_logical_or_expr
== truthvalue_true_node
)
9691 - (folded_logical_or_expr
== truthvalue_false_node
));
9692 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9695 /* The next token should be a `:'. */
9696 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9697 /* Parse the assignment-expression. */
9698 assignment_expr
= cp_parser_assignment_expression (parser
);
9699 c_inhibit_evaluation_warnings
-=
9700 folded_logical_or_expr
== truthvalue_true_node
;
9703 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9704 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9705 with the caret at the "?", ranging from the start of
9706 the logical_or_expr to the end of the assignment_expr. */
9707 loc
= make_location (loc
,
9708 logical_or_expr
.get_start (),
9709 assignment_expr
.get_finish ());
9711 /* Build the conditional-expression. */
9712 return build_x_conditional_expr (loc
, logical_or_expr
,
9715 tf_warning_or_error
);
9718 /* Parse an assignment-expression.
9720 assignment-expression:
9721 conditional-expression
9722 logical-or-expression assignment-operator assignment_expression
9725 CAST_P is true if this expression is the target of a cast.
9726 DECLTYPE_P is true if this expression is the operand of decltype.
9728 Returns a representation for the expression. */
9731 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9732 bool cast_p
, bool decltype_p
)
9736 /* If the next token is the `throw' keyword, then we're looking at
9737 a throw-expression. */
9738 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9739 expr
= cp_parser_throw_expression (parser
);
9740 /* Otherwise, it must be that we are looking at a
9741 logical-or-expression. */
9744 /* Parse the binary expressions (logical-or-expression). */
9745 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9747 PREC_NOT_OPERATOR
, pidk
);
9748 /* If the next token is a `?' then we're actually looking at a
9749 conditional-expression. */
9750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9751 return cp_parser_question_colon_clause (parser
, expr
);
9754 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9756 /* If it's an assignment-operator, we're using the second
9758 enum tree_code assignment_operator
9759 = cp_parser_assignment_operator_opt (parser
);
9760 if (assignment_operator
!= ERROR_MARK
)
9762 bool non_constant_p
;
9764 /* Parse the right-hand side of the assignment. */
9765 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9768 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9769 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9771 /* An assignment may not appear in a
9772 constant-expression. */
9773 if (cp_parser_non_integral_constant_expression (parser
,
9775 return error_mark_node
;
9776 /* Build the assignment expression. Its default
9780 is the location of the '=' token as the
9781 caret, ranging from the start of the lhs to the
9783 loc
= make_location (loc
,
9786 expr
= build_x_modify_expr (loc
, expr
,
9787 assignment_operator
,
9789 complain_flags (decltype_p
));
9790 /* TODO: build_x_modify_expr doesn't honor the location,
9791 so we must set it here. */
9792 expr
.set_location (loc
);
9800 /* Parse an (optional) assignment-operator.
9802 assignment-operator: one of
9803 = *= /= %= += -= >>= <<= &= ^= |=
9807 assignment-operator: one of
9810 If the next token is an assignment operator, the corresponding tree
9811 code is returned, and the token is consumed. For example, for
9812 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9813 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9814 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9815 operator, ERROR_MARK is returned. */
9817 static enum tree_code
9818 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9823 /* Peek at the next token. */
9824 token
= cp_lexer_peek_token (parser
->lexer
);
9826 switch (token
->type
)
9837 op
= TRUNC_DIV_EXPR
;
9841 op
= TRUNC_MOD_EXPR
;
9873 /* Nothing else is an assignment operator. */
9877 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9878 if (op
!= ERROR_MARK
9879 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9882 /* If it was an assignment operator, consume it. */
9883 if (op
!= ERROR_MARK
)
9884 cp_lexer_consume_token (parser
->lexer
);
9889 /* Parse an expression.
9892 assignment-expression
9893 expression , assignment-expression
9895 CAST_P is true if this expression is the target of a cast.
9896 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9897 except possibly parenthesized or on the RHS of a comma (N3276).
9899 Returns a representation of the expression. */
9902 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9903 bool cast_p
, bool decltype_p
)
9905 cp_expr expression
= NULL_TREE
;
9906 location_t loc
= UNKNOWN_LOCATION
;
9910 cp_expr assignment_expression
;
9912 /* Parse the next assignment-expression. */
9913 assignment_expression
9914 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9916 /* We don't create a temporary for a call that is the immediate operand
9917 of decltype or on the RHS of a comma. But when we see a comma, we
9918 need to create a temporary for a call on the LHS. */
9919 if (decltype_p
&& !processing_template_decl
9920 && TREE_CODE (assignment_expression
) == CALL_EXPR
9921 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9922 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9923 assignment_expression
9924 = build_cplus_new (TREE_TYPE (assignment_expression
),
9925 assignment_expression
, tf_warning_or_error
);
9927 /* If this is the first assignment-expression, we can just
9930 expression
= assignment_expression
;
9933 /* Create a location with caret at the comma, ranging
9934 from the start of the LHS to the end of the RHS. */
9935 loc
= make_location (loc
,
9936 expression
.get_start (),
9937 assignment_expression
.get_finish ());
9938 expression
= build_x_compound_expr (loc
, expression
,
9939 assignment_expression
,
9940 complain_flags (decltype_p
));
9941 expression
.set_location (loc
);
9943 /* If the next token is not a comma, or we're in a fold-expression, then
9944 we are done with the expression. */
9945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9946 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9948 /* Consume the `,'. */
9949 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9950 cp_lexer_consume_token (parser
->lexer
);
9951 /* A comma operator cannot appear in a constant-expression. */
9952 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9953 expression
= error_mark_node
;
9959 /* Parse a constant-expression.
9961 constant-expression:
9962 conditional-expression
9964 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9965 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9966 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9967 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9968 only parse a conditional-expression, otherwise parse an
9969 assignment-expression. See below for rationale. */
9972 cp_parser_constant_expression (cp_parser
* parser
,
9973 bool allow_non_constant_p
,
9974 bool *non_constant_p
,
9977 bool saved_integral_constant_expression_p
;
9978 bool saved_allow_non_integral_constant_expression_p
;
9979 bool saved_non_integral_constant_expression_p
;
9982 /* It might seem that we could simply parse the
9983 conditional-expression, and then check to see if it were
9984 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9985 one that the compiler can figure out is constant, possibly after
9986 doing some simplifications or optimizations. The standard has a
9987 precise definition of constant-expression, and we must honor
9988 that, even though it is somewhat more restrictive.
9994 is not a legal declaration, because `(2, 3)' is not a
9995 constant-expression. The `,' operator is forbidden in a
9996 constant-expression. However, GCC's constant-folding machinery
9997 will fold this operation to an INTEGER_CST for `3'. */
9999 /* Save the old settings. */
10000 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10001 saved_allow_non_integral_constant_expression_p
10002 = parser
->allow_non_integral_constant_expression_p
;
10003 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10004 /* We are now parsing a constant-expression. */
10005 parser
->integral_constant_expression_p
= true;
10006 parser
->allow_non_integral_constant_expression_p
10007 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10008 parser
->non_integral_constant_expression_p
= false;
10009 /* Although the grammar says "conditional-expression", when not STRICT_P,
10010 we parse an "assignment-expression", which also permits
10011 "throw-expression" and the use of assignment operators. In the case
10012 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10013 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10014 actually essential that we look for an assignment-expression.
10015 For example, cp_parser_initializer_clauses uses this function to
10016 determine whether a particular assignment-expression is in fact
10020 /* Parse the binary expressions (logical-or-expression). */
10021 expression
= cp_parser_binary_expression (parser
, false, false, false,
10022 PREC_NOT_OPERATOR
, NULL
);
10023 /* If the next token is a `?' then we're actually looking at
10024 a conditional-expression; otherwise we're done. */
10025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10026 expression
= cp_parser_question_colon_clause (parser
, expression
);
10029 expression
= cp_parser_assignment_expression (parser
);
10030 /* Restore the old settings. */
10031 parser
->integral_constant_expression_p
10032 = saved_integral_constant_expression_p
;
10033 parser
->allow_non_integral_constant_expression_p
10034 = saved_allow_non_integral_constant_expression_p
;
10035 if (cxx_dialect
>= cxx11
)
10037 /* Require an rvalue constant expression here; that's what our
10038 callers expect. Reference constant expressions are handled
10039 separately in e.g. cp_parser_template_argument. */
10040 tree decay
= expression
;
10041 if (TREE_TYPE (expression
)
10042 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10043 decay
= build_address (expression
);
10044 bool is_const
= potential_rvalue_constant_expression (decay
);
10045 parser
->non_integral_constant_expression_p
= !is_const
;
10046 if (!is_const
&& !allow_non_constant_p
)
10047 require_potential_rvalue_constant_expression (decay
);
10049 if (allow_non_constant_p
)
10050 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10051 parser
->non_integral_constant_expression_p
10052 = saved_non_integral_constant_expression_p
;
10057 /* Parse __builtin_offsetof.
10059 offsetof-expression:
10060 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10062 offsetof-member-designator:
10064 | offsetof-member-designator "." id-expression
10065 | offsetof-member-designator "[" expression "]"
10066 | offsetof-member-designator "->" id-expression */
10069 cp_parser_builtin_offsetof (cp_parser
*parser
)
10071 int save_ice_p
, save_non_ice_p
;
10076 location_t finish_loc
;
10078 /* We're about to accept non-integral-constant things, but will
10079 definitely yield an integral constant expression. Save and
10080 restore these values around our local parsing. */
10081 save_ice_p
= parser
->integral_constant_expression_p
;
10082 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10084 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10086 /* Consume the "__builtin_offsetof" token. */
10087 cp_lexer_consume_token (parser
->lexer
);
10088 /* Consume the opening `('. */
10089 matching_parens parens
;
10090 parens
.require_open (parser
);
10091 /* Parse the type-id. */
10092 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10094 const char *saved_message
= parser
->type_definition_forbidden_message
;
10095 parser
->type_definition_forbidden_message
10096 = G_("types may not be defined within %<__builtin_offsetof%>");
10097 type
= cp_parser_type_id (parser
);
10098 parser
->type_definition_forbidden_message
= saved_message
;
10100 /* Look for the `,'. */
10101 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10102 token
= cp_lexer_peek_token (parser
->lexer
);
10104 /* Build the (type *)null that begins the traditional offsetof macro. */
10106 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10107 tf_warning_or_error
);
10109 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10110 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10111 true, &dummy
, token
->location
);
10114 token
= cp_lexer_peek_token (parser
->lexer
);
10115 switch (token
->type
)
10117 case CPP_OPEN_SQUARE
:
10118 /* offsetof-member-designator "[" expression "]" */
10119 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10124 /* offsetof-member-designator "->" identifier */
10125 expr
= grok_array_decl (token
->location
, expr
,
10126 integer_zero_node
, false);
10130 /* offsetof-member-designator "." identifier */
10131 cp_lexer_consume_token (parser
->lexer
);
10132 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10133 expr
, true, &dummy
,
10137 case CPP_CLOSE_PAREN
:
10138 /* Consume the ")" token. */
10139 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10140 cp_lexer_consume_token (parser
->lexer
);
10144 /* Error. We know the following require will fail, but
10145 that gives the proper error message. */
10146 parens
.require_close (parser
);
10147 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10148 expr
= error_mark_node
;
10154 /* Make a location of the form:
10155 __builtin_offsetof (struct s, f)
10156 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10157 with caret at the type-id, ranging from the start of the
10158 "_builtin_offsetof" token to the close paren. */
10159 loc
= make_location (loc
, start_loc
, finish_loc
);
10160 /* The result will be an INTEGER_CST, so we need to explicitly
10161 preserve the location. */
10162 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10165 parser
->integral_constant_expression_p
= save_ice_p
;
10166 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10168 expr
= expr
.maybe_add_location_wrapper ();
10172 /* Parse a trait expression.
10174 Returns a representation of the expression, the underlying type
10175 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10178 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10180 cp_trait_kind kind
;
10181 tree type1
, type2
= NULL_TREE
;
10182 bool binary
= false;
10183 bool variadic
= false;
10187 case RID_HAS_NOTHROW_ASSIGN
:
10188 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10190 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10191 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10193 case RID_HAS_NOTHROW_COPY
:
10194 kind
= CPTK_HAS_NOTHROW_COPY
;
10196 case RID_HAS_TRIVIAL_ASSIGN
:
10197 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10199 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10200 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10202 case RID_HAS_TRIVIAL_COPY
:
10203 kind
= CPTK_HAS_TRIVIAL_COPY
;
10205 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10206 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10208 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10209 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10211 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10212 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10214 case RID_IS_ABSTRACT
:
10215 kind
= CPTK_IS_ABSTRACT
;
10217 case RID_IS_AGGREGATE
:
10218 kind
= CPTK_IS_AGGREGATE
;
10220 case RID_IS_BASE_OF
:
10221 kind
= CPTK_IS_BASE_OF
;
10225 kind
= CPTK_IS_CLASS
;
10228 kind
= CPTK_IS_EMPTY
;
10231 kind
= CPTK_IS_ENUM
;
10234 kind
= CPTK_IS_FINAL
;
10236 case RID_IS_LITERAL_TYPE
:
10237 kind
= CPTK_IS_LITERAL_TYPE
;
10240 kind
= CPTK_IS_POD
;
10242 case RID_IS_POLYMORPHIC
:
10243 kind
= CPTK_IS_POLYMORPHIC
;
10245 case RID_IS_SAME_AS
:
10246 kind
= CPTK_IS_SAME_AS
;
10249 case RID_IS_STD_LAYOUT
:
10250 kind
= CPTK_IS_STD_LAYOUT
;
10252 case RID_IS_TRIVIAL
:
10253 kind
= CPTK_IS_TRIVIAL
;
10255 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10256 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10259 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10260 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10263 case RID_IS_TRIVIALLY_COPYABLE
:
10264 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10267 kind
= CPTK_IS_UNION
;
10269 case RID_UNDERLYING_TYPE
:
10270 kind
= CPTK_UNDERLYING_TYPE
;
10275 case RID_DIRECT_BASES
:
10276 kind
= CPTK_DIRECT_BASES
;
10278 case RID_IS_ASSIGNABLE
:
10279 kind
= CPTK_IS_ASSIGNABLE
;
10282 case RID_IS_CONSTRUCTIBLE
:
10283 kind
= CPTK_IS_CONSTRUCTIBLE
;
10287 gcc_unreachable ();
10290 /* Get location of initial token. */
10291 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10293 /* Consume the token. */
10294 cp_lexer_consume_token (parser
->lexer
);
10296 matching_parens parens
;
10297 parens
.require_open (parser
);
10300 type_id_in_expr_sentinel
s (parser
);
10301 type1
= cp_parser_type_id (parser
);
10304 if (type1
== error_mark_node
)
10305 return error_mark_node
;
10309 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10312 type_id_in_expr_sentinel
s (parser
);
10313 type2
= cp_parser_type_id (parser
);
10316 if (type2
== error_mark_node
)
10317 return error_mark_node
;
10321 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10323 cp_lexer_consume_token (parser
->lexer
);
10324 tree elt
= cp_parser_type_id (parser
);
10325 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10327 cp_lexer_consume_token (parser
->lexer
);
10328 elt
= make_pack_expansion (elt
);
10330 if (elt
== error_mark_node
)
10331 return error_mark_node
;
10332 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10336 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10337 parens
.require_close (parser
);
10339 /* Construct a location of the form:
10340 __is_trivially_copyable(_Tp)
10341 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10342 with start == caret, finishing at the close-paren. */
10343 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10345 /* Complete the trait expression, which may mean either processing
10346 the trait expr now or saving it for template instantiation. */
10349 case CPTK_UNDERLYING_TYPE
:
10350 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10352 return cp_expr (finish_bases (type1
, false), trait_loc
);
10353 case CPTK_DIRECT_BASES
:
10354 return cp_expr (finish_bases (type1
, true), trait_loc
);
10356 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10360 /* Parse a lambda expression.
10363 lambda-introducer lambda-declarator [opt] compound-statement
10365 Returns a representation of the expression. */
10368 cp_parser_lambda_expression (cp_parser
* parser
)
10370 tree lambda_expr
= build_lambda_expr ();
10373 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10374 cp_token_position start
= 0;
10376 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10378 if (cxx_dialect
>= cxx2a
)
10379 /* C++20 allows lambdas in unevaluated context. */;
10380 else if (cp_unevaluated_operand
)
10382 if (!token
->error_reported
)
10384 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10385 "lambda-expression in unevaluated context"
10386 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10387 token
->error_reported
= true;
10391 else if (parser
->in_template_argument_list_p
|| processing_template_parmlist
)
10393 if (!token
->error_reported
)
10395 error_at (token
->location
, "lambda-expression in template-argument"
10396 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10397 token
->error_reported
= true;
10402 /* We may be in the middle of deferred access check. Disable
10404 push_deferring_access_checks (dk_no_deferred
);
10406 cp_parser_lambda_introducer (parser
, lambda_expr
);
10407 if (cp_parser_error_occurred (parser
))
10408 return error_mark_node
;
10410 type
= begin_lambda_type (lambda_expr
);
10411 if (type
== error_mark_node
)
10412 return error_mark_node
;
10414 record_lambda_scope (lambda_expr
);
10416 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10417 determine_visibility (TYPE_NAME (type
));
10419 /* Now that we've started the type, add the capture fields for any
10420 explicit captures. */
10421 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10424 /* Inside the class, surrounding template-parameter-lists do not apply. */
10425 unsigned int saved_num_template_parameter_lists
10426 = parser
->num_template_parameter_lists
;
10427 unsigned char in_statement
= parser
->in_statement
;
10428 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10429 bool fully_implicit_function_template_p
10430 = parser
->fully_implicit_function_template_p
;
10431 tree implicit_template_parms
= parser
->implicit_template_parms
;
10432 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10433 bool auto_is_implicit_function_template_parm_p
10434 = parser
->auto_is_implicit_function_template_parm_p
;
10436 parser
->num_template_parameter_lists
= 0;
10437 parser
->in_statement
= 0;
10438 parser
->in_switch_statement_p
= false;
10439 parser
->fully_implicit_function_template_p
= false;
10440 parser
->implicit_template_parms
= 0;
10441 parser
->implicit_template_scope
= 0;
10442 parser
->auto_is_implicit_function_template_parm_p
= false;
10444 /* By virtue of defining a local class, a lambda expression has access to
10445 the private variables of enclosing classes. */
10447 if (cp_parser_start_tentative_firewall (parser
))
10450 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10452 if (ok
&& cp_parser_error_occurred (parser
))
10457 cp_parser_lambda_body (parser
, lambda_expr
);
10459 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10461 if (cp_parser_skip_to_closing_brace (parser
))
10462 cp_lexer_consume_token (parser
->lexer
);
10465 /* The capture list was built up in reverse order; fix that now. */
10466 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10467 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10470 maybe_add_lambda_conv_op (type
);
10472 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10474 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10475 parser
->in_statement
= in_statement
;
10476 parser
->in_switch_statement_p
= in_switch_statement_p
;
10477 parser
->fully_implicit_function_template_p
10478 = fully_implicit_function_template_p
;
10479 parser
->implicit_template_parms
= implicit_template_parms
;
10480 parser
->implicit_template_scope
= implicit_template_scope
;
10481 parser
->auto_is_implicit_function_template_parm_p
10482 = auto_is_implicit_function_template_parm_p
;
10485 /* This field is only used during parsing of the lambda. */
10486 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10488 /* This lambda shouldn't have any proxies left at this point. */
10489 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10490 /* And now that we're done, push proxies for an enclosing lambda. */
10491 insert_pending_capture_proxies ();
10493 /* Update the lambda expression to a range. */
10494 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10495 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10497 end_tok
->location
);
10500 lambda_expr
= build_lambda_object (lambda_expr
);
10502 lambda_expr
= error_mark_node
;
10504 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10506 pop_deferring_access_checks ();
10508 return lambda_expr
;
10511 /* Parse the beginning of a lambda expression.
10514 [ lambda-capture [opt] ]
10516 LAMBDA_EXPR is the current representation of the lambda expression. */
10519 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10521 /* Need commas after the first capture. */
10524 /* Eat the leading `['. */
10525 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10527 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10528 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10529 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10530 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10531 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10532 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10534 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10536 cp_lexer_consume_token (parser
->lexer
);
10539 if (!(at_function_scope_p () || parsing_nsdmi ()))
10540 error ("non-local lambda expression cannot have a capture-default");
10543 hash_set
<tree
, true> ids
;
10544 tree first_capture_id
= NULL_TREE
;
10545 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10547 cp_token
* capture_token
;
10549 tree capture_init_expr
;
10550 cp_id_kind idk
= CP_ID_KIND_NONE
;
10551 bool explicit_init_p
= false;
10553 enum capture_kind_type
10558 enum capture_kind_type capture_kind
= BY_COPY
;
10560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10562 error ("expected end of capture-list");
10569 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10571 /* Possibly capture `this'. */
10572 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10574 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10575 if (cxx_dialect
< cxx2a
10576 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10577 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10578 "with by-copy capture default");
10579 cp_lexer_consume_token (parser
->lexer
);
10580 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10581 pedwarn (input_location
, 0,
10582 "already captured %qD in lambda expression",
10585 add_capture (lambda_expr
, /*id=*/this_identifier
,
10586 /*initializer=*/finish_this_expr (),
10587 /*by_reference_p=*/true, explicit_init_p
);
10591 /* Possibly capture `*this'. */
10592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10593 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10595 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10596 if (cxx_dialect
< cxx17
)
10597 pedwarn (loc
, 0, "%<*this%> capture only available with "
10598 "%<-std=c++17%> or %<-std=gnu++17%>");
10599 cp_lexer_consume_token (parser
->lexer
);
10600 cp_lexer_consume_token (parser
->lexer
);
10601 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10602 pedwarn (input_location
, 0,
10603 "already captured %qD in lambda expression",
10606 add_capture (lambda_expr
, /*id=*/this_identifier
,
10607 /*initializer=*/finish_this_expr (),
10608 /*by_reference_p=*/false, explicit_init_p
);
10612 bool init_pack_expansion
= false;
10613 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
10614 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10616 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10617 if (cxx_dialect
< cxx2a
)
10618 pedwarn (ellipsis_loc
, 0, "pack init-capture only available with "
10619 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10620 cp_lexer_consume_token (parser
->lexer
);
10621 init_pack_expansion
= true;
10624 /* Remember whether we want to capture as a reference or not. */
10625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10627 capture_kind
= BY_REFERENCE
;
10628 cp_lexer_consume_token (parser
->lexer
);
10631 /* Get the identifier. */
10632 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10633 capture_id
= cp_parser_identifier (parser
);
10635 if (capture_id
== error_mark_node
)
10636 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10637 delimiters, but I modified this to stop on unnested ']' as well. It
10638 was already changed to stop on unnested '}', so the
10639 "closing_parenthesis" name is no more misleading with my change. */
10641 cp_parser_skip_to_closing_parenthesis (parser
,
10642 /*recovering=*/true,
10644 /*consume_paren=*/true);
10648 /* Find the initializer for this capture. */
10649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10650 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10651 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10653 bool direct
, non_constant
;
10654 /* An explicit initializer exists. */
10655 if (cxx_dialect
< cxx14
)
10656 pedwarn (input_location
, 0,
10657 "lambda capture initializers "
10658 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10659 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10660 &non_constant
, true);
10661 explicit_init_p
= true;
10662 if (capture_init_expr
== NULL_TREE
)
10664 error ("empty initializer for lambda init-capture");
10665 capture_init_expr
= error_mark_node
;
10667 if (init_pack_expansion
)
10668 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10672 const char* error_msg
;
10674 /* Turn the identifier into an id-expression. */
10676 = cp_parser_lookup_name_simple (parser
, capture_id
,
10677 capture_token
->location
);
10679 if (capture_init_expr
== error_mark_node
)
10681 unqualified_name_lookup_error (capture_id
);
10684 else if (!VAR_P (capture_init_expr
)
10685 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10687 error_at (capture_token
->location
,
10688 "capture of non-variable %qE",
10689 capture_init_expr
);
10690 if (DECL_P (capture_init_expr
))
10691 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10692 "%q#D declared here", capture_init_expr
);
10695 if (VAR_P (capture_init_expr
)
10696 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10698 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10699 "%qD with non-automatic storage duration",
10700 capture_init_expr
))
10701 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10702 "%q#D declared here", capture_init_expr
);
10707 = finish_id_expression
10712 /*integral_constant_expression_p=*/false,
10713 /*allow_non_integral_constant_expression_p=*/false,
10714 /*non_integral_constant_expression_p=*/NULL
,
10715 /*template_p=*/false,
10717 /*address_p=*/false,
10718 /*template_arg_p=*/false,
10720 capture_token
->location
);
10722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10724 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10725 cp_lexer_consume_token (parser
->lexer
);
10726 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10727 if (init_pack_expansion
)
10729 /* If what follows is an initializer, the second '...' is
10730 invalid. But for cases like [...xs...], the first one
10732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10733 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10734 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10735 ellipsis_loc
= loc
;
10736 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
10742 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10743 && !explicit_init_p
)
10745 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10746 && capture_kind
== BY_COPY
)
10747 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10748 "of %qD redundant with by-copy capture default",
10750 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10751 && capture_kind
== BY_REFERENCE
)
10752 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10753 "capture of %qD redundant with by-reference capture "
10754 "default", capture_id
);
10757 /* Check for duplicates.
10758 Optimize for the zero or one explicit captures cases and only create
10759 the hash_set after adding second capture. */
10760 bool found
= false;
10761 if (!ids
.is_empty ())
10762 found
= ids
.add (capture_id
);
10763 else if (first_capture_id
== NULL_TREE
)
10764 first_capture_id
= capture_id
;
10765 else if (capture_id
== first_capture_id
)
10769 ids
.add (first_capture_id
);
10770 ids
.add (capture_id
);
10773 pedwarn (input_location
, 0,
10774 "already captured %qD in lambda expression", capture_id
);
10776 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
10777 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10780 /* If there is any qualification still in effect, clear it
10781 now; we will be starting fresh with the next capture. */
10782 parser
->scope
= NULL_TREE
;
10783 parser
->qualifying_scope
= NULL_TREE
;
10784 parser
->object_scope
= NULL_TREE
;
10787 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10790 /* Parse the (optional) middle of a lambda expression.
10793 < template-parameter-list [opt] >
10794 ( parameter-declaration-clause [opt] )
10795 attribute-specifier [opt]
10796 decl-specifier-seq [opt]
10797 exception-specification [opt]
10798 lambda-return-type-clause [opt]
10800 LAMBDA_EXPR is the current representation of the lambda expression. */
10803 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10805 /* 5.1.1.4 of the standard says:
10806 If a lambda-expression does not include a lambda-declarator, it is as if
10807 the lambda-declarator were ().
10808 This means an empty parameter list, no attributes, and no exception
10810 tree param_list
= void_list_node
;
10811 tree std_attrs
= NULL_TREE
;
10812 tree gnu_attrs
= NULL_TREE
;
10813 tree exception_spec
= NULL_TREE
;
10814 tree template_param_list
= NULL_TREE
;
10815 tree tx_qual
= NULL_TREE
;
10816 tree return_type
= NULL_TREE
;
10817 cp_decl_specifier_seq lambda_specs
;
10818 clear_decl_specs (&lambda_specs
);
10820 /* The template-parameter-list is optional, but must begin with
10821 an opening angle if present. */
10822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10824 if (cxx_dialect
< cxx14
)
10825 pedwarn (parser
->lexer
->next_token
->location
, 0,
10826 "lambda templates are only available with "
10827 "%<-std=c++14%> or %<-std=gnu++14%>");
10828 else if (cxx_dialect
< cxx2a
)
10829 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10830 "lambda templates are only available with "
10831 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10833 cp_lexer_consume_token (parser
->lexer
);
10835 template_param_list
= cp_parser_template_parameter_list (parser
);
10837 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10839 /* We just processed one more parameter list. */
10840 ++parser
->num_template_parameter_lists
;
10843 /* The parameter-declaration-clause is optional (unless
10844 template-parameter-list was given), but must begin with an
10845 opening parenthesis if present. */
10846 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10848 matching_parens parens
;
10849 parens
.consume_open (parser
);
10851 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10853 /* Parse parameters. */
10855 = cp_parser_parameter_declaration_clause
10856 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
10858 /* Default arguments shall not be specified in the
10859 parameter-declaration-clause of a lambda-declarator. */
10860 if (cxx_dialect
< cxx14
)
10861 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10862 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10863 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10864 "default argument specified for lambda parameter");
10866 parens
.require_close (parser
);
10868 /* In the decl-specifier-seq of the lambda-declarator, each
10869 decl-specifier shall either be mutable or constexpr. */
10870 int declares_class_or_enum
;
10871 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
10872 && !cp_next_tokens_can_be_gnu_attribute_p (parser
))
10873 cp_parser_decl_specifier_seq (parser
,
10874 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10875 &lambda_specs
, &declares_class_or_enum
);
10876 if (lambda_specs
.storage_class
== sc_mutable
)
10878 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10879 if (lambda_specs
.conflicting_specifiers_p
)
10880 error_at (lambda_specs
.locations
[ds_storage_class
],
10881 "duplicate %<mutable%>");
10884 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10886 /* Parse optional exception specification. */
10887 exception_spec
= cp_parser_exception_specification_opt (parser
);
10889 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10891 /* Parse optional trailing return type. */
10892 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10894 cp_lexer_consume_token (parser
->lexer
);
10895 return_type
= cp_parser_trailing_type_id (parser
);
10898 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
10899 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
10901 /* The function parameters must be in scope all the way until after the
10902 trailing-return-type in case of decltype. */
10903 pop_bindings_and_leave_scope ();
10905 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10906 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10908 /* Create the function call operator.
10910 Messing with declarators like this is no uglier than building up the
10911 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10914 cp_decl_specifier_seq return_type_specs
;
10915 cp_declarator
* declarator
;
10920 clear_decl_specs (&return_type_specs
);
10921 return_type_specs
.type
= make_auto ();
10923 if (lambda_specs
.locations
[ds_constexpr
])
10925 if (cxx_dialect
>= cxx17
)
10926 return_type_specs
.locations
[ds_constexpr
]
10927 = lambda_specs
.locations
[ds_constexpr
];
10929 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10930 "lambda only available with %<-std=c++17%> or "
10931 "%<-std=gnu++17%>");
10934 p
= obstack_alloc (&declarator_obstack
, 0);
10936 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10937 LAMBDA_EXPR_LOCATION (lambda_expr
));
10939 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10940 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10941 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10942 VIRT_SPEC_UNSPECIFIED
,
10947 /*requires_clause*/NULL_TREE
);
10948 declarator
->std_attributes
= std_attrs
;
10950 fco
= grokmethod (&return_type_specs
,
10953 if (fco
!= error_mark_node
)
10955 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10956 DECL_ARTIFICIAL (fco
) = 1;
10957 /* Give the object parameter a different name. */
10958 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10959 DECL_LAMBDA_FUNCTION (fco
) = 1;
10961 if (template_param_list
)
10963 fco
= finish_member_template_decl (fco
);
10964 finish_template_decl (template_param_list
);
10965 --parser
->num_template_parameter_lists
;
10967 else if (parser
->fully_implicit_function_template_p
)
10968 fco
= finish_fully_implicit_template (parser
, fco
);
10970 finish_member_declaration (fco
);
10972 obstack_free (&declarator_obstack
, p
);
10974 return (fco
!= error_mark_node
);
10978 /* Parse the body of a lambda expression, which is simply
10982 but which requires special handling.
10983 LAMBDA_EXPR is the current representation of the lambda expression. */
10986 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10988 bool nested
= (current_function_decl
!= NULL_TREE
);
10989 unsigned char local_variables_forbidden_p
10990 = parser
->local_variables_forbidden_p
;
10991 bool in_function_body
= parser
->in_function_body
;
10993 /* The body of a lambda-expression is not a subexpression of the enclosing
10998 push_function_context ();
11000 /* Still increment function_depth so that we don't GC in the
11001 middle of an expression. */
11004 vec
<tree
> omp_privatization_save
;
11005 save_omp_privatization_clauses (omp_privatization_save
);
11006 /* Clear this in case we're in the middle of a default argument. */
11007 parser
->local_variables_forbidden_p
= 0;
11008 parser
->in_function_body
= true;
11011 local_specialization_stack
s (lss_copy
);
11012 tree fco
= lambda_function (lambda_expr
);
11013 tree body
= start_lambda_function (fco
, lambda_expr
);
11014 matching_braces braces
;
11016 if (braces
.require_open (parser
))
11018 tree compound_stmt
= begin_compound_stmt (0);
11020 /* Originally C++11 required us to peek for 'return expr'; and
11021 process it specially here to deduce the return type. N3638
11022 removed the need for that. */
11024 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11025 cp_parser_label_declaration (parser
);
11026 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11027 braces
.require_close (parser
);
11029 finish_compound_stmt (compound_stmt
);
11032 finish_lambda_function (body
);
11035 restore_omp_privatization_clauses (omp_privatization_save
);
11036 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11037 parser
->in_function_body
= in_function_body
;
11039 pop_function_context();
11044 /* Statements [gram.stmt.stmt] */
11046 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11049 add_debug_begin_stmt (location_t loc
)
11051 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11053 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11054 /* A concept is never expanded normally. */
11057 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11058 SET_EXPR_LOCATION (stmt
, loc
);
11062 /* Parse a statement.
11066 expression-statement
11068 selection-statement
11069 iteration-statement
11071 declaration-statement
11078 attribute-specifier-seq (opt) expression-statement
11079 attribute-specifier-seq (opt) compound-statement
11080 attribute-specifier-seq (opt) selection-statement
11081 attribute-specifier-seq (opt) iteration-statement
11082 attribute-specifier-seq (opt) jump-statement
11083 declaration-statement
11084 attribute-specifier-seq (opt) try-block
11087 expression-statement
11095 IN_COMPOUND is true when the statement is nested inside a
11096 cp_parser_compound_statement; this matters for certain pragmas.
11098 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11099 is a (possibly labeled) if statement which is not enclosed in braces
11100 and has an else clause. This is used to implement -Wparentheses.
11102 CHAIN is a vector of if-else-if conditions. */
11105 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11106 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11107 location_t
*loc_after_labels
)
11109 tree statement
, std_attrs
= NULL_TREE
;
11111 location_t statement_location
, attrs_loc
;
11116 /* There is no statement yet. */
11117 statement
= NULL_TREE
;
11119 saved_token_sentinel
saved_tokens (parser
->lexer
);
11120 attrs_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11121 if (c_dialect_objc ())
11122 /* In obj-c++, seeing '[[' might be the either the beginning of
11123 c++11 attributes, or a nested objc-message-expression. So
11124 let's parse the c++11 attributes tentatively. */
11125 cp_parser_parse_tentatively (parser
);
11126 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11130 = cp_lexer_previous_token (parser
->lexer
)->location
;
11131 attrs_loc
= make_location (attrs_loc
, attrs_loc
, end_loc
);
11133 if (c_dialect_objc ())
11135 if (!cp_parser_parse_definitely (parser
))
11136 std_attrs
= NULL_TREE
;
11139 /* Peek at the next token. */
11140 token
= cp_lexer_peek_token (parser
->lexer
);
11141 /* Remember the location of the first token in the statement. */
11142 cp_token
*statement_token
= token
;
11143 statement_location
= token
->location
;
11144 add_debug_begin_stmt (statement_location
);
11145 /* If this is a keyword, then that will often determine what kind of
11146 statement we have. */
11147 if (token
->type
== CPP_KEYWORD
)
11149 enum rid keyword
= token
->keyword
;
11155 /* Looks like a labeled-statement with a case label.
11156 Parse the label, and then use tail recursion to parse
11158 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11159 in_compound
= false;
11164 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11165 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11171 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11172 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11179 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11180 statement
= cp_parser_jump_statement (parser
);
11183 /* Objective-C++ exception-handling constructs. */
11186 case RID_AT_FINALLY
:
11187 case RID_AT_SYNCHRONIZED
:
11189 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11190 statement
= cp_parser_objc_statement (parser
);
11194 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11195 statement
= cp_parser_try_block (parser
);
11198 case RID_NAMESPACE
:
11199 /* This must be a namespace alias definition. */
11200 if (std_attrs
!= NULL_TREE
)
11202 /* Attributes should be parsed as part of the the
11203 declaration, so let's un-parse them. */
11204 saved_tokens
.rollback();
11205 std_attrs
= NULL_TREE
;
11207 cp_parser_declaration_statement (parser
);
11210 case RID_TRANSACTION_ATOMIC
:
11211 case RID_TRANSACTION_RELAXED
:
11212 case RID_SYNCHRONIZED
:
11213 case RID_ATOMIC_NOEXCEPT
:
11214 case RID_ATOMIC_CANCEL
:
11215 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11216 statement
= cp_parser_transaction (parser
, token
);
11218 case RID_TRANSACTION_CANCEL
:
11219 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11220 statement
= cp_parser_transaction_cancel (parser
);
11224 /* It might be a keyword like `int' that can start a
11225 declaration-statement. */
11229 else if (token
->type
== CPP_NAME
)
11231 /* If the next token is a `:', then we are looking at a
11232 labeled-statement. */
11233 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11234 if (token
->type
== CPP_COLON
)
11236 /* Looks like a labeled-statement with an ordinary label.
11237 Parse the label, and then use tail recursion to parse
11240 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11241 in_compound
= false;
11245 /* Anything that starts with a `{' must be a compound-statement. */
11246 else if (token
->type
== CPP_OPEN_BRACE
)
11247 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11248 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11249 a statement all its own. */
11250 else if (token
->type
== CPP_PRAGMA
)
11252 /* Only certain OpenMP pragmas are attached to statements, and thus
11253 are considered statements themselves. All others are not. In
11254 the context of a compound, accept the pragma as a "statement" and
11255 return so that we can check for a close brace. Otherwise we
11256 require a real statement and must go back and read one. */
11258 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11259 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11263 else if (token
->type
== CPP_EOF
)
11265 cp_parser_error (parser
, "expected statement");
11269 /* Everything else must be a declaration-statement or an
11270 expression-statement. Try for the declaration-statement
11271 first, unless we are looking at a `;', in which case we know that
11272 we have an expression-statement. */
11275 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11277 if (std_attrs
!= NULL_TREE
)
11278 /* Attributes should be parsed as part of the declaration,
11279 so let's un-parse them. */
11280 saved_tokens
.rollback();
11282 cp_parser_parse_tentatively (parser
);
11283 /* Try to parse the declaration-statement. */
11284 cp_parser_declaration_statement (parser
);
11285 /* If that worked, we're done. */
11286 if (cp_parser_parse_definitely (parser
))
11288 /* It didn't work, restore the post-attribute position. */
11290 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11292 /* All preceding labels have been parsed at this point. */
11293 if (loc_after_labels
!= NULL
)
11294 *loc_after_labels
= statement_location
;
11296 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11298 /* Look for an expression-statement instead. */
11299 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11301 /* Handle [[fallthrough]];. */
11302 if (attribute_fallthrough_p (std_attrs
))
11304 /* The next token after the fallthrough attribute is ';'. */
11305 if (statement
== NULL_TREE
)
11307 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11308 statement
= build_call_expr_internal_loc (statement_location
,
11310 void_type_node
, 0);
11311 finish_expr_stmt (statement
);
11314 warning_at (statement_location
, OPT_Wattributes
,
11315 "%<fallthrough%> attribute not followed by %<;%>");
11316 std_attrs
= NULL_TREE
;
11320 /* Set the line number for the statement. */
11321 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11322 SET_EXPR_LOCATION (statement
, statement_location
);
11324 /* Allow "[[fallthrough]];", but warn otherwise. */
11325 if (std_attrs
!= NULL_TREE
)
11326 warning_at (attrs_loc
,
11328 "attributes at the beginning of statement are ignored");
11331 /* Append ATTR to attribute list ATTRS. */
11334 attr_chainon (tree attrs
, tree attr
)
11336 if (attrs
== error_mark_node
)
11337 return error_mark_node
;
11338 if (attr
== error_mark_node
)
11339 return error_mark_node
;
11340 return chainon (attrs
, attr
);
11343 /* Parse the label for a labeled-statement, i.e.
11346 case constant-expression :
11350 case constant-expression ... constant-expression : statement
11352 When a label is parsed without errors, the label is added to the
11353 parse tree by the finish_* functions, so this function doesn't
11354 have to return the label. */
11357 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11360 tree label
= NULL_TREE
;
11361 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11363 /* The next token should be an identifier. */
11364 token
= cp_lexer_peek_token (parser
->lexer
);
11365 if (token
->type
!= CPP_NAME
11366 && token
->type
!= CPP_KEYWORD
)
11368 cp_parser_error (parser
, "expected labeled-statement");
11372 /* Remember whether this case or a user-defined label is allowed to fall
11374 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11376 parser
->colon_corrects_to_scope_p
= false;
11377 switch (token
->keyword
)
11381 tree expr
, expr_hi
;
11382 cp_token
*ellipsis
;
11384 /* Consume the `case' token. */
11385 cp_lexer_consume_token (parser
->lexer
);
11386 /* Parse the constant-expression. */
11387 expr
= cp_parser_constant_expression (parser
);
11388 if (check_for_bare_parameter_packs (expr
))
11389 expr
= error_mark_node
;
11391 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11392 if (ellipsis
->type
== CPP_ELLIPSIS
)
11394 /* Consume the `...' token. */
11395 cp_lexer_consume_token (parser
->lexer
);
11396 expr_hi
= cp_parser_constant_expression (parser
);
11397 if (check_for_bare_parameter_packs (expr_hi
))
11398 expr_hi
= error_mark_node
;
11400 /* We don't need to emit warnings here, as the common code
11401 will do this for us. */
11404 expr_hi
= NULL_TREE
;
11406 if (parser
->in_switch_statement_p
)
11408 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11409 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11411 label
= CASE_LABEL (l
);
11412 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11416 error_at (token
->location
,
11417 "case label %qE not within a switch statement",
11423 /* Consume the `default' token. */
11424 cp_lexer_consume_token (parser
->lexer
);
11426 if (parser
->in_switch_statement_p
)
11428 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11429 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11431 label
= CASE_LABEL (l
);
11432 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11436 error_at (token
->location
, "case label not within a switch statement");
11440 /* Anything else must be an ordinary label. */
11441 label
= finish_label_stmt (cp_parser_identifier (parser
));
11442 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11443 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11447 /* Require the `:' token. */
11448 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11450 /* An ordinary label may optionally be followed by attributes.
11451 However, this is only permitted if the attributes are then
11452 followed by a semicolon. This is because, for backward
11453 compatibility, when parsing
11454 lab: __attribute__ ((unused)) int i;
11455 we want the attribute to attach to "i", not "lab". */
11456 if (label
!= NULL_TREE
11457 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11460 cp_parser_parse_tentatively (parser
);
11461 attrs
= cp_parser_gnu_attributes_opt (parser
);
11462 if (attrs
== NULL_TREE
11463 /* And fallthrough always binds to the expression-statement. */
11464 || attribute_fallthrough_p (attrs
)
11465 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11466 cp_parser_abort_tentative_parse (parser
);
11467 else if (!cp_parser_parse_definitely (parser
))
11470 attributes
= attr_chainon (attributes
, attrs
);
11473 if (attributes
!= NULL_TREE
)
11474 cplus_decl_attributes (&label
, attributes
, 0);
11476 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11479 /* Parse an expression-statement.
11481 expression-statement:
11484 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11485 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11486 indicates whether this expression-statement is part of an
11487 expression statement. */
11490 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11492 tree statement
= NULL_TREE
;
11493 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11494 location_t loc
= token
->location
;
11496 /* There might be attribute fallthrough. */
11497 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11499 /* If the next token is a ';', then there is no expression
11501 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11503 statement
= cp_parser_expression (parser
);
11504 if (statement
== error_mark_node
11505 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11507 cp_parser_skip_to_end_of_block_or_statement (parser
);
11508 return error_mark_node
;
11512 /* Handle [[fallthrough]];. */
11513 if (attribute_fallthrough_p (attr
))
11515 /* The next token after the fallthrough attribute is ';'. */
11516 if (statement
== NULL_TREE
)
11517 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11518 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11519 void_type_node
, 0);
11521 warning_at (loc
, OPT_Wattributes
,
11522 "%<fallthrough%> attribute not followed by %<;%>");
11526 /* Allow "[[fallthrough]];", but warn otherwise. */
11527 if (attr
!= NULL_TREE
)
11528 warning_at (loc
, OPT_Wattributes
,
11529 "attributes at the beginning of statement are ignored");
11531 /* Give a helpful message for "A<T>::type t;" and the like. */
11532 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11533 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11535 if (TREE_CODE (statement
) == SCOPE_REF
)
11536 error_at (token
->location
, "need %<typename%> before %qE because "
11537 "%qT is a dependent scope",
11538 statement
, TREE_OPERAND (statement
, 0));
11539 else if (is_overloaded_fn (statement
)
11540 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11543 tree fn
= get_first_fn (statement
);
11544 error_at (token
->location
,
11545 "%<%T::%D%> names the constructor, not the type",
11546 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11550 /* Consume the final `;'. */
11551 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11553 if (in_statement_expr
11554 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11555 /* This is the final expression statement of a statement
11557 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11558 else if (statement
)
11559 statement
= finish_expr_stmt (statement
);
11564 /* Parse a compound-statement.
11566 compound-statement:
11567 { statement-seq [opt] }
11571 compound-statement:
11572 { label-declaration-seq [opt] statement-seq [opt] }
11574 label-declaration-seq:
11576 label-declaration-seq label-declaration
11578 Returns a tree representing the statement. */
11581 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11582 int bcs_flags
, bool function_body
)
11584 tree compound_stmt
;
11585 matching_braces braces
;
11587 /* Consume the `{'. */
11588 if (!braces
.require_open (parser
))
11589 return error_mark_node
;
11590 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11591 && !function_body
&& cxx_dialect
< cxx14
)
11592 pedwarn (input_location
, OPT_Wpedantic
,
11593 "compound-statement in %<constexpr%> function");
11594 /* Begin the compound-statement. */
11595 compound_stmt
= begin_compound_stmt (bcs_flags
);
11596 /* If the next keyword is `__label__' we have a label declaration. */
11597 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11598 cp_parser_label_declaration (parser
);
11599 /* Parse an (optional) statement-seq. */
11600 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11601 /* Finish the compound-statement. */
11602 finish_compound_stmt (compound_stmt
);
11603 /* Consume the `}'. */
11604 braces
.require_close (parser
);
11606 return compound_stmt
;
11609 /* Parse an (optional) statement-seq.
11613 statement-seq [opt] statement */
11616 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11618 /* Scan statements until there aren't any more. */
11621 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11623 /* If we are looking at a `}', then we have run out of
11624 statements; the same is true if we have reached the end
11625 of file, or have stumbled upon a stray '@end'. */
11626 if (token
->type
== CPP_CLOSE_BRACE
11627 || token
->type
== CPP_EOF
11628 || token
->type
== CPP_PRAGMA_EOL
11629 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11632 /* If we are in a compound statement and find 'else' then
11633 something went wrong. */
11634 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11636 if (parser
->in_statement
& IN_IF_STMT
)
11640 token
= cp_lexer_consume_token (parser
->lexer
);
11641 error_at (token
->location
, "%<else%> without a previous %<if%>");
11645 /* Parse the statement. */
11646 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11650 /* Return true if this is the C++20 version of range-based-for with
11654 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11658 /* Save tokens so that we can put them back. */
11659 cp_lexer_save_tokens (parser
->lexer
);
11661 /* There has to be an unnested ; followed by an unnested :. */
11662 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11663 /*recovering=*/false,
11665 /*consume_paren=*/false) != -1)
11668 /* We found the semicolon, eat it now. */
11669 cp_lexer_consume_token (parser
->lexer
);
11671 /* Now look for ':' that is not nested in () or {}. */
11672 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11673 /*recovering=*/false,
11675 /*consume_paren=*/false) == -1);
11678 /* Roll back the tokens we skipped. */
11679 cp_lexer_rollback_tokens (parser
->lexer
);
11684 /* Return true if we're looking at (init; cond), false otherwise. */
11687 cp_parser_init_statement_p (cp_parser
*parser
)
11689 /* Save tokens so that we can put them back. */
11690 cp_lexer_save_tokens (parser
->lexer
);
11692 /* Look for ';' that is not nested in () or {}. */
11693 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11694 /*recovering=*/false,
11696 /*consume_paren=*/false);
11698 /* Roll back the tokens we skipped. */
11699 cp_lexer_rollback_tokens (parser
->lexer
);
11704 /* Parse a selection-statement.
11706 selection-statement:
11707 if ( init-statement [opt] condition ) statement
11708 if ( init-statement [opt] condition ) statement else statement
11709 switch ( init-statement [opt] condition ) statement
11711 Returns the new IF_STMT or SWITCH_STMT.
11713 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11714 is a (possibly labeled) if statement which is not enclosed in
11715 braces and has an else clause. This is used to implement
11718 CHAIN is a vector of if-else-if conditions. This is used to implement
11719 -Wduplicated-cond. */
11722 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11727 token_indent_info guard_tinfo
;
11732 /* Peek at the next token. */
11733 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11734 guard_tinfo
= get_token_indent_info (token
);
11736 /* See what kind of keyword it is. */
11737 keyword
= token
->keyword
;
11747 if (keyword
== RID_IF
11748 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11752 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11753 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11754 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11755 "with %<-std=c++17%> or %<-std=gnu++17%>");
11758 /* Look for the `('. */
11759 matching_parens parens
;
11760 if (!parens
.require_open (parser
))
11762 cp_parser_skip_to_end_of_statement (parser
);
11763 return error_mark_node
;
11766 /* Begin the selection-statement. */
11767 if (keyword
== RID_IF
)
11769 statement
= begin_if_stmt ();
11770 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11773 statement
= begin_switch_stmt ();
11775 /* Parse the optional init-statement. */
11776 if (cp_parser_init_statement_p (parser
))
11779 if (cxx_dialect
< cxx17
)
11780 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11781 "init-statement in selection statements only available "
11782 "with %<-std=c++17%> or %<-std=gnu++17%>");
11783 cp_parser_init_statement (parser
, &decl
);
11786 /* Parse the condition. */
11787 condition
= cp_parser_condition (parser
);
11788 /* Look for the `)'. */
11789 if (!parens
.require_close (parser
))
11790 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11791 /*consume_paren=*/true);
11793 if (keyword
== RID_IF
)
11796 unsigned char in_statement
;
11798 /* Add the condition. */
11799 condition
= finish_if_stmt_cond (condition
, statement
);
11801 if (warn_duplicated_cond
)
11802 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11805 /* Parse the then-clause. */
11806 in_statement
= parser
->in_statement
;
11807 parser
->in_statement
|= IN_IF_STMT
;
11809 /* Outside a template, the non-selected branch of a constexpr
11810 if is a 'discarded statement', i.e. unevaluated. */
11811 bool was_discarded
= in_discarded_stmt
;
11812 bool discard_then
= (cx
&& !processing_template_decl
11813 && integer_zerop (condition
));
11816 in_discarded_stmt
= true;
11817 ++c_inhibit_evaluation_warnings
;
11820 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11823 parser
->in_statement
= in_statement
;
11825 finish_then_clause (statement
);
11829 THEN_CLAUSE (statement
) = NULL_TREE
;
11830 in_discarded_stmt
= was_discarded
;
11831 --c_inhibit_evaluation_warnings
;
11834 /* If the next token is `else', parse the else-clause. */
11835 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11838 bool discard_else
= (cx
&& !processing_template_decl
11839 && integer_nonzerop (condition
));
11842 in_discarded_stmt
= true;
11843 ++c_inhibit_evaluation_warnings
;
11847 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11848 /* Consume the `else' keyword. */
11849 cp_lexer_consume_token (parser
->lexer
);
11850 if (warn_duplicated_cond
)
11852 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11856 /* We've got "if (COND) else if (COND2)". Start
11857 the condition chain and add COND as the first
11859 chain
= new vec
<tree
> ();
11860 if (!CONSTANT_CLASS_P (condition
)
11861 && !TREE_SIDE_EFFECTS (condition
))
11863 /* Wrap it in a NOP_EXPR so that we can set the
11864 location of the condition. */
11865 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11867 SET_EXPR_LOCATION (e
, token
->location
);
11868 chain
->safe_push (e
);
11871 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11874 /* This is if-else without subsequent if. Zap the
11875 condition chain; we would have already warned at
11881 begin_else_clause (statement
);
11882 /* Parse the else-clause. */
11883 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11884 guard_tinfo
, chain
);
11886 finish_else_clause (statement
);
11888 /* If we are currently parsing a then-clause, then
11889 IF_P will not be NULL. We set it to true to
11890 indicate that this if statement has an else clause.
11891 This may trigger the Wparentheses warning below
11892 when we get back up to the parent if statement. */
11898 ELSE_CLAUSE (statement
) = NULL_TREE
;
11899 in_discarded_stmt
= was_discarded
;
11900 --c_inhibit_evaluation_warnings
;
11905 /* This if statement does not have an else clause. If
11906 NESTED_IF is true, then the then-clause has an if
11907 statement which does have an else clause. We warn
11908 about the potential ambiguity. */
11910 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11911 "suggest explicit braces to avoid ambiguous"
11913 if (warn_duplicated_cond
)
11915 /* We don't need the condition chain anymore. */
11921 /* Now we're all done with the if-statement. */
11922 finish_if_stmt (statement
);
11926 bool in_switch_statement_p
;
11927 unsigned char in_statement
;
11929 /* Add the condition. */
11930 finish_switch_cond (condition
, statement
);
11932 /* Parse the body of the switch-statement. */
11933 in_switch_statement_p
= parser
->in_switch_statement_p
;
11934 in_statement
= parser
->in_statement
;
11935 parser
->in_switch_statement_p
= true;
11936 parser
->in_statement
|= IN_SWITCH_STMT
;
11937 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11939 parser
->in_switch_statement_p
= in_switch_statement_p
;
11940 parser
->in_statement
= in_statement
;
11942 /* Now we're all done with the switch-statement. */
11943 finish_switch_stmt (statement
);
11951 cp_parser_error (parser
, "expected selection-statement");
11952 return error_mark_node
;
11956 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11957 If we have seen at least one decl-specifier, and the next token
11958 is not a parenthesis, then we must be looking at a declaration.
11959 (After "int (" we might be looking at a functional cast.) */
11962 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11963 bool any_specifiers_p
)
11965 if (any_specifiers_p
11966 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11967 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11968 && !cp_parser_error_occurred (parser
))
11969 cp_parser_commit_to_tentative_parse (parser
);
11972 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11973 The declarator shall not specify a function or an array. Returns
11974 TRUE if the declarator is valid, FALSE otherwise. */
11977 cp_parser_check_condition_declarator (cp_parser
* parser
,
11978 cp_declarator
*declarator
,
11981 if (declarator
== cp_error_declarator
11982 || function_declarator_p (declarator
)
11983 || declarator
->kind
== cdk_array
)
11985 if (declarator
== cp_error_declarator
)
11986 /* Already complained. */;
11987 else if (declarator
->kind
== cdk_array
)
11988 error_at (loc
, "condition declares an array");
11990 error_at (loc
, "condition declares a function");
11991 if (parser
->fully_implicit_function_template_p
)
11992 abort_fully_implicit_template (parser
);
11993 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11994 /*or_comma=*/false,
11995 /*consume_paren=*/false);
12002 /* Parse a condition.
12006 type-specifier-seq declarator = initializer-clause
12007 type-specifier-seq declarator braced-init-list
12012 type-specifier-seq declarator asm-specification [opt]
12013 attributes [opt] = assignment-expression
12015 Returns the expression that should be tested. */
12018 cp_parser_condition (cp_parser
* parser
)
12020 cp_decl_specifier_seq type_specifiers
;
12021 const char *saved_message
;
12022 int declares_class_or_enum
;
12024 /* Try the declaration first. */
12025 cp_parser_parse_tentatively (parser
);
12026 /* New types are not allowed in the type-specifier-seq for a
12028 saved_message
= parser
->type_definition_forbidden_message
;
12029 parser
->type_definition_forbidden_message
12030 = G_("types may not be defined in conditions");
12031 /* Parse the type-specifier-seq. */
12032 cp_parser_decl_specifier_seq (parser
,
12033 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
12035 &declares_class_or_enum
);
12036 /* Restore the saved message. */
12037 parser
->type_definition_forbidden_message
= saved_message
;
12039 cp_parser_maybe_commit_to_declaration (parser
,
12040 type_specifiers
.any_specifiers_p
);
12042 /* If all is well, we might be looking at a declaration. */
12043 if (!cp_parser_error_occurred (parser
))
12046 tree asm_specification
;
12048 cp_declarator
*declarator
;
12049 tree initializer
= NULL_TREE
;
12050 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12052 /* Parse the declarator. */
12053 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12054 CP_PARSER_FLAGS_NONE
,
12055 /*ctor_dtor_or_conv_p=*/NULL
,
12056 /*parenthesized_p=*/NULL
,
12057 /*member_p=*/false,
12058 /*friend_p=*/false,
12059 /*static_p=*/false);
12060 /* Parse the attributes. */
12061 attributes
= cp_parser_attributes_opt (parser
);
12062 /* Parse the asm-specification. */
12063 asm_specification
= cp_parser_asm_specification_opt (parser
);
12064 /* If the next token is not an `=' or '{', then we might still be
12065 looking at an expression. For example:
12069 looks like a decl-specifier-seq and a declarator -- but then
12070 there is no `=', so this is an expression. */
12071 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12072 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12073 cp_parser_simulate_error (parser
);
12075 /* If we did see an `=' or '{', then we are looking at a declaration
12077 if (cp_parser_parse_definitely (parser
))
12080 bool non_constant_p
= false;
12081 int flags
= LOOKUP_ONLYCONVERTING
;
12083 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12084 return error_mark_node
;
12086 /* Create the declaration. */
12087 decl
= start_decl (declarator
, &type_specifiers
,
12088 /*initialized_p=*/true,
12089 attributes
, /*prefix_attributes=*/NULL_TREE
,
12092 /* Parse the initializer. */
12093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12095 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12096 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12099 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12101 /* Consume the `='. */
12102 cp_lexer_consume_token (parser
->lexer
);
12103 initializer
= cp_parser_initializer_clause (parser
,
12108 cp_parser_error (parser
, "expected initializer");
12109 initializer
= error_mark_node
;
12111 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12112 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12114 /* Process the initializer. */
12115 cp_finish_decl (decl
,
12116 initializer
, !non_constant_p
,
12121 pop_scope (pushed_scope
);
12123 return convert_from_reference (decl
);
12126 /* If we didn't even get past the declarator successfully, we are
12127 definitely not looking at a declaration. */
12129 cp_parser_abort_tentative_parse (parser
);
12131 /* Otherwise, we are looking at an expression. */
12132 return cp_parser_expression (parser
);
12135 /* Parses a for-statement or range-for-statement until the closing ')',
12139 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12141 tree init
, scope
, decl
;
12144 /* Begin the for-statement. */
12145 scope
= begin_for_scope (&init
);
12147 /* Parse the initialization. */
12148 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12151 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12154 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12158 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12159 unsigned short unroll
)
12161 /* Normal for loop */
12162 tree condition
= NULL_TREE
;
12163 tree expression
= NULL_TREE
;
12166 stmt
= begin_for_stmt (scope
, init
);
12167 /* The init-statement has already been parsed in
12168 cp_parser_init_statement, so no work is needed here. */
12169 finish_init_stmt (stmt
);
12171 /* If there's a condition, process it. */
12172 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12173 condition
= cp_parser_condition (parser
);
12176 cp_parser_error (parser
, "missing loop condition in loop with "
12177 "%<GCC ivdep%> pragma");
12178 condition
= error_mark_node
;
12182 cp_parser_error (parser
, "missing loop condition in loop with "
12183 "%<GCC unroll%> pragma");
12184 condition
= error_mark_node
;
12186 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12187 /* Look for the `;'. */
12188 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12190 /* If there's an expression, process it. */
12191 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12192 expression
= cp_parser_expression (parser
);
12193 finish_for_expr (expression
, stmt
);
12198 /* Tries to parse a range-based for-statement:
12201 decl-specifier-seq declarator : expression
12203 The decl-specifier-seq declarator and the `:' are already parsed by
12204 cp_parser_init_statement. If processing_template_decl it returns a
12205 newly created RANGE_FOR_STMT; if not, it is converted to a
12206 regular FOR_STMT. */
12209 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12210 bool ivdep
, unsigned short unroll
, bool is_omp
)
12212 tree stmt
, range_expr
;
12213 auto_vec
<cxx_binding
*, 16> bindings
;
12214 auto_vec
<tree
, 16> names
;
12215 tree decomp_first_name
= NULL_TREE
;
12216 unsigned int decomp_cnt
= 0;
12218 /* Get the range declaration momentarily out of the way so that
12219 the range expression doesn't clash with it. */
12220 if (range_decl
!= error_mark_node
)
12222 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12224 tree v
= DECL_VALUE_EXPR (range_decl
);
12225 /* For decomposition declaration get all of the corresponding
12226 declarations out of the way. */
12227 if (TREE_CODE (v
) == ARRAY_REF
12228 && VAR_P (TREE_OPERAND (v
, 0))
12229 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12231 tree d
= range_decl
;
12232 range_decl
= TREE_OPERAND (v
, 0);
12233 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12234 decomp_first_name
= d
;
12235 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12237 tree name
= DECL_NAME (d
);
12238 names
.safe_push (name
);
12239 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12240 IDENTIFIER_BINDING (name
)
12241 = IDENTIFIER_BINDING (name
)->previous
;
12245 if (names
.is_empty ())
12247 tree name
= DECL_NAME (range_decl
);
12248 names
.safe_push (name
);
12249 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12250 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12256 bool expr_non_constant_p
;
12257 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12260 range_expr
= cp_parser_expression (parser
);
12262 /* Put the range declaration(s) back into scope. */
12263 for (unsigned int i
= 0; i
< names
.length (); i
++)
12265 cxx_binding
*binding
= bindings
[i
];
12266 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12267 IDENTIFIER_BINDING (names
[i
]) = binding
;
12270 /* finish_omp_for has its own code for the following, so just
12271 return the range_expr instead. */
12275 /* If in template, STMT is converted to a normal for-statement
12276 at instantiation. If not, it is done just ahead. */
12277 if (processing_template_decl
)
12279 if (check_for_bare_parameter_packs (range_expr
))
12280 range_expr
= error_mark_node
;
12281 stmt
= begin_range_for_stmt (scope
, init
);
12283 RANGE_FOR_IVDEP (stmt
) = 1;
12285 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12286 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12287 if (!type_dependent_expression_p (range_expr
)
12288 /* do_auto_deduction doesn't mess with template init-lists. */
12289 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12290 do_range_for_auto_deduction (range_decl
, range_expr
);
12294 stmt
= begin_for_stmt (scope
, init
);
12295 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12296 decomp_first_name
, decomp_cnt
, ivdep
,
12302 /* Subroutine of cp_convert_range_for: given the initializer expression,
12303 builds up the range temporary. */
12306 build_range_temp (tree range_expr
)
12308 tree range_type
, range_temp
;
12310 /* Find out the type deduced by the declaration
12311 `auto &&__range = range_expr'. */
12312 range_type
= cp_build_reference_type (make_auto (), true);
12313 range_type
= do_auto_deduction (range_type
, range_expr
,
12314 type_uses_auto (range_type
));
12316 /* Create the __range variable. */
12317 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12319 TREE_USED (range_temp
) = 1;
12320 DECL_ARTIFICIAL (range_temp
) = 1;
12325 /* Used by cp_parser_range_for in template context: we aren't going to
12326 do a full conversion yet, but we still need to resolve auto in the
12327 type of the for-range-declaration if present. This is basically
12328 a shortcut version of cp_convert_range_for. */
12331 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12333 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12336 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12337 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12338 iter_type
= (cp_parser_perform_range_for_lookup
12339 (range_temp
, &begin_dummy
, &end_dummy
));
12342 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12344 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12346 tf_warning_or_error
);
12347 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12348 iter_decl
, auto_node
);
12353 /* Converts a range-based for-statement into a normal
12354 for-statement, as per the definition.
12356 for (RANGE_DECL : RANGE_EXPR)
12359 should be equivalent to:
12362 auto &&__range = RANGE_EXPR;
12363 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12367 RANGE_DECL = *__begin;
12372 If RANGE_EXPR is an array:
12373 BEGIN_EXPR = __range
12374 END_EXPR = __range + ARRAY_SIZE(__range)
12375 Else if RANGE_EXPR has a member 'begin' or 'end':
12376 BEGIN_EXPR = __range.begin()
12377 END_EXPR = __range.end()
12379 BEGIN_EXPR = begin(__range)
12380 END_EXPR = end(__range);
12382 If __range has a member 'begin' but not 'end', or vice versa, we must
12383 still use the second alternative (it will surely fail, however).
12384 When calling begin()/end() in the third alternative we must use
12385 argument dependent lookup, but always considering 'std' as an associated
12389 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12390 tree decomp_first_name
, unsigned int decomp_cnt
,
12391 bool ivdep
, unsigned short unroll
)
12394 tree iter_type
, begin_expr
, end_expr
;
12395 tree condition
, expression
;
12397 range_expr
= mark_lvalue_use (range_expr
);
12399 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12400 /* If an error happened previously do nothing or else a lot of
12401 unhelpful errors would be issued. */
12402 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12407 if (VAR_P (range_expr
)
12408 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12409 /* Can't bind a reference to an array of runtime bound. */
12410 range_temp
= range_expr
;
12413 range_temp
= build_range_temp (range_expr
);
12414 pushdecl (range_temp
);
12415 cp_finish_decl (range_temp
, range_expr
,
12416 /*is_constant_init*/false, NULL_TREE
,
12417 LOOKUP_ONLYCONVERTING
);
12418 range_temp
= convert_from_reference (range_temp
);
12420 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12421 &begin_expr
, &end_expr
);
12424 /* The new for initialization statement. */
12425 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12427 TREE_USED (begin
) = 1;
12428 DECL_ARTIFICIAL (begin
) = 1;
12430 cp_finish_decl (begin
, begin_expr
,
12431 /*is_constant_init*/false, NULL_TREE
,
12432 LOOKUP_ONLYCONVERTING
);
12434 if (cxx_dialect
>= cxx17
)
12435 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12436 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12437 TREE_USED (end
) = 1;
12438 DECL_ARTIFICIAL (end
) = 1;
12440 cp_finish_decl (end
, end_expr
,
12441 /*is_constant_init*/false, NULL_TREE
,
12442 LOOKUP_ONLYCONVERTING
);
12444 finish_init_stmt (statement
);
12446 /* The new for condition. */
12447 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12450 NULL
, tf_warning_or_error
);
12451 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12453 /* The new increment expression. */
12454 expression
= finish_unary_op_expr (input_location
,
12455 PREINCREMENT_EXPR
, begin
,
12456 tf_warning_or_error
);
12457 finish_for_expr (expression
, statement
);
12459 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12460 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12462 /* The declaration is initialized with *__begin inside the loop body. */
12463 cp_finish_decl (range_decl
,
12464 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12465 tf_warning_or_error
),
12466 /*is_constant_init*/false, NULL_TREE
,
12467 LOOKUP_ONLYCONVERTING
);
12468 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12469 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12474 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12475 We need to solve both at the same time because the method used
12476 depends on the existence of members begin or end.
12477 Returns the type deduced for the iterator expression. */
12480 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12482 if (error_operand_p (range
))
12484 *begin
= *end
= error_mark_node
;
12485 return error_mark_node
;
12488 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12490 error ("range-based %<for%> expression of type %qT "
12491 "has incomplete type", TREE_TYPE (range
));
12492 *begin
= *end
= error_mark_node
;
12493 return error_mark_node
;
12495 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12497 /* If RANGE is an array, we will use pointer arithmetic. */
12498 *begin
= decay_conversion (range
, tf_warning_or_error
);
12499 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12501 array_type_nelts_top (TREE_TYPE (range
)),
12503 return TREE_TYPE (*begin
);
12507 /* If it is not an array, we must do a bit of magic. */
12508 tree id_begin
, id_end
;
12509 tree member_begin
, member_end
;
12511 *begin
= *end
= error_mark_node
;
12513 id_begin
= get_identifier ("begin");
12514 id_end
= get_identifier ("end");
12515 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12516 /*protect=*/2, /*want_type=*/false,
12517 tf_warning_or_error
);
12518 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12519 /*protect=*/2, /*want_type=*/false,
12520 tf_warning_or_error
);
12522 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12524 /* Use the member functions. */
12525 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12526 *end
= cp_parser_range_for_member_function (range
, id_end
);
12530 /* Use global functions with ADL. */
12533 vec_safe_push (vec
, range
);
12535 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12536 tf_warning_or_error
);
12537 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12538 tf_warning_or_error
);
12539 member_end
= perform_koenig_lookup (id_end
, vec
,
12540 tf_warning_or_error
);
12541 *end
= finish_call_expr (member_end
, &vec
, false, true,
12542 tf_warning_or_error
);
12545 /* Last common checks. */
12546 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12548 /* If one of the expressions is an error do no more checks. */
12549 *begin
= *end
= error_mark_node
;
12550 return error_mark_node
;
12552 else if (type_dependent_expression_p (*begin
)
12553 || type_dependent_expression_p (*end
))
12554 /* Can happen, when, eg, in a template context, Koenig lookup
12555 can't resolve begin/end (c++/58503). */
12559 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12560 /* The unqualified type of the __begin and __end temporaries should
12561 be the same, as required by the multiple auto declaration. */
12562 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12564 if (cxx_dialect
>= cxx17
12565 && (build_x_binary_op (input_location
, NE_EXPR
,
12566 *begin
, ERROR_MARK
,
12569 != error_mark_node
))
12570 /* P0184R0 allows __begin and __end to have different types,
12571 but make sure they are comparable so we can give a better
12574 error ("inconsistent begin/end types in range-based %<for%> "
12575 "statement: %qT and %qT",
12576 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12583 /* Helper function for cp_parser_perform_range_for_lookup.
12584 Builds a tree for RANGE.IDENTIFIER(). */
12587 cp_parser_range_for_member_function (tree range
, tree identifier
)
12591 member
= finish_class_member_access_expr (range
, identifier
,
12592 false, tf_warning_or_error
);
12593 if (member
== error_mark_node
)
12594 return error_mark_node
;
12597 res
= finish_call_expr (member
, &vec
,
12598 /*disallow_virtual=*/false,
12599 /*koenig_p=*/false,
12600 tf_warning_or_error
);
12604 /* Parse an iteration-statement.
12606 iteration-statement:
12607 while ( condition ) statement
12608 do statement while ( expression ) ;
12609 for ( init-statement condition [opt] ; expression [opt] )
12612 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12615 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12616 unsigned short unroll
)
12621 unsigned char in_statement
;
12622 token_indent_info guard_tinfo
;
12624 /* Peek at the next token. */
12625 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12627 return error_mark_node
;
12629 guard_tinfo
= get_token_indent_info (token
);
12631 /* Remember whether or not we are already within an iteration
12633 in_statement
= parser
->in_statement
;
12635 /* See what kind of keyword it is. */
12636 keyword
= token
->keyword
;
12643 /* Begin the while-statement. */
12644 statement
= begin_while_stmt ();
12645 /* Look for the `('. */
12646 matching_parens parens
;
12647 parens
.require_open (parser
);
12648 /* Parse the condition. */
12649 condition
= cp_parser_condition (parser
);
12650 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12651 /* Look for the `)'. */
12652 parens
.require_close (parser
);
12653 /* Parse the dependent statement. */
12654 parser
->in_statement
= IN_ITERATION_STMT
;
12655 bool prev
= note_iteration_stmt_body_start ();
12656 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12657 note_iteration_stmt_body_end (prev
);
12658 parser
->in_statement
= in_statement
;
12659 /* We're done with the while-statement. */
12660 finish_while_stmt (statement
);
12668 /* Begin the do-statement. */
12669 statement
= begin_do_stmt ();
12670 /* Parse the body of the do-statement. */
12671 parser
->in_statement
= IN_ITERATION_STMT
;
12672 bool prev
= note_iteration_stmt_body_start ();
12673 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12674 note_iteration_stmt_body_end (prev
);
12675 parser
->in_statement
= in_statement
;
12676 finish_do_body (statement
);
12677 /* Look for the `while' keyword. */
12678 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12679 /* Look for the `('. */
12680 matching_parens parens
;
12681 parens
.require_open (parser
);
12682 /* Parse the expression. */
12683 expression
= cp_parser_expression (parser
);
12684 /* We're done with the do-statement. */
12685 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12686 /* Look for the `)'. */
12687 parens
.require_close (parser
);
12688 /* Look for the `;'. */
12689 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12695 /* Look for the `('. */
12696 matching_parens parens
;
12697 parens
.require_open (parser
);
12699 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12701 /* Look for the `)'. */
12702 parens
.require_close (parser
);
12704 /* Parse the body of the for-statement. */
12705 parser
->in_statement
= IN_ITERATION_STMT
;
12706 bool prev
= note_iteration_stmt_body_start ();
12707 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12708 note_iteration_stmt_body_end (prev
);
12709 parser
->in_statement
= in_statement
;
12711 /* We're done with the for-statement. */
12712 finish_for_stmt (statement
);
12717 cp_parser_error (parser
, "expected iteration-statement");
12718 statement
= error_mark_node
;
12725 /* Parse a init-statement or the declarator of a range-based-for.
12726 Returns true if a range-based-for declaration is seen.
12729 expression-statement
12730 simple-declaration */
12733 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12735 /* If the next token is a `;', then we have an empty
12736 expression-statement. Grammatically, this is also a
12737 simple-declaration, but an invalid one, because it does not
12738 declare anything. Therefore, if we did not handle this case
12739 specially, we would issue an error message about an invalid
12741 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12743 bool is_range_for
= false;
12744 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12746 /* Try to parse the init-statement. */
12747 if (cp_parser_range_based_for_with_init_p (parser
))
12750 cp_parser_parse_tentatively (parser
);
12751 /* Parse the declaration. */
12752 cp_parser_simple_declaration (parser
,
12753 /*function_definition_allowed_p=*/false,
12755 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12756 if (!cp_parser_parse_definitely (parser
))
12757 /* That didn't work, try to parse it as an expression-statement. */
12758 cp_parser_expression_statement (parser
, NULL_TREE
);
12760 if (cxx_dialect
< cxx2a
)
12762 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12763 "range-based %<for%> loops with initializer only "
12764 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12765 *decl
= error_mark_node
;
12769 /* A colon is used in range-based for. */
12770 parser
->colon_corrects_to_scope_p
= false;
12772 /* We're going to speculatively look for a declaration, falling back
12773 to an expression, if necessary. */
12774 cp_parser_parse_tentatively (parser
);
12775 /* Parse the declaration. */
12776 cp_parser_simple_declaration (parser
,
12777 /*function_definition_allowed_p=*/false,
12779 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12780 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12782 /* It is a range-for, consume the ':'. */
12783 cp_lexer_consume_token (parser
->lexer
);
12784 is_range_for
= true;
12785 if (cxx_dialect
< cxx11
)
12786 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12787 "range-based %<for%> loops only available with "
12788 "%<-std=c++11%> or %<-std=gnu++11%>");
12791 /* The ';' is not consumed yet because we told
12792 cp_parser_simple_declaration not to. */
12793 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12795 if (cp_parser_parse_definitely (parser
))
12796 return is_range_for
;
12797 /* If the tentative parse failed, then we shall need to look for an
12798 expression-statement. */
12800 /* If we are here, it is an expression-statement. */
12801 cp_parser_expression_statement (parser
, NULL_TREE
);
12805 /* Parse a jump-statement.
12810 return expression [opt] ;
12811 return braced-init-list ;
12817 goto * expression ;
12819 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12822 cp_parser_jump_statement (cp_parser
* parser
)
12824 tree statement
= error_mark_node
;
12827 unsigned char in_statement
;
12829 /* Peek at the next token. */
12830 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12832 return error_mark_node
;
12834 /* See what kind of keyword it is. */
12835 keyword
= token
->keyword
;
12839 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12840 switch (in_statement
)
12843 error_at (token
->location
, "break statement not within loop or switch");
12846 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12847 || in_statement
== IN_ITERATION_STMT
);
12848 statement
= finish_break_stmt ();
12849 if (in_statement
== IN_ITERATION_STMT
)
12850 break_maybe_infinite_loop ();
12853 error_at (token
->location
, "invalid exit from OpenMP structured block");
12856 error_at (token
->location
, "break statement used with OpenMP for loop");
12859 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12863 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12866 error_at (token
->location
, "continue statement not within a loop");
12868 /* Fall through. */
12869 case IN_ITERATION_STMT
:
12871 statement
= finish_continue_stmt ();
12874 error_at (token
->location
, "invalid exit from OpenMP structured block");
12877 gcc_unreachable ();
12879 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12885 bool expr_non_constant_p
;
12887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12889 cp_lexer_set_source_position (parser
->lexer
);
12890 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12891 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12893 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12894 expr
= cp_parser_expression (parser
);
12896 /* If the next token is a `;', then there is no
12899 /* Build the return-statement. */
12900 if (FNDECL_USED_AUTO (current_function_decl
) && in_discarded_stmt
)
12901 /* Don't deduce from a discarded return statement. */;
12903 statement
= finish_return_stmt (expr
);
12904 /* Look for the final `;'. */
12905 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12910 if (parser
->in_function_body
12911 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12913 error ("%<goto%> in %<constexpr%> function");
12914 cp_function_chain
->invalid_constexpr
= true;
12917 /* Create the goto-statement. */
12918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12920 /* Issue a warning about this use of a GNU extension. */
12921 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12922 /* Consume the '*' token. */
12923 cp_lexer_consume_token (parser
->lexer
);
12924 /* Parse the dependent expression. */
12925 finish_goto_stmt (cp_parser_expression (parser
));
12928 finish_goto_stmt (cp_parser_identifier (parser
));
12929 /* Look for the final `;'. */
12930 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12934 cp_parser_error (parser
, "expected jump-statement");
12941 /* Parse a declaration-statement.
12943 declaration-statement:
12944 block-declaration */
12947 cp_parser_declaration_statement (cp_parser
* parser
)
12951 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12952 p
= obstack_alloc (&declarator_obstack
, 0);
12954 /* Parse the block-declaration. */
12955 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12957 /* Free any declarators allocated. */
12958 obstack_free (&declarator_obstack
, p
);
12961 /* Some dependent statements (like `if (cond) statement'), are
12962 implicitly in their own scope. In other words, if the statement is
12963 a single statement (as opposed to a compound-statement), it is
12964 none-the-less treated as if it were enclosed in braces. Any
12965 declarations appearing in the dependent statement are out of scope
12966 after control passes that point. This function parses a statement,
12967 but ensures that is in its own scope, even if it is not a
12968 compound-statement.
12970 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12971 is a (possibly labeled) if statement which is not enclosed in
12972 braces and has an else clause. This is used to implement
12975 CHAIN is a vector of if-else-if conditions. This is used to implement
12978 Returns the new statement. */
12981 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12982 const token_indent_info
&guard_tinfo
,
12986 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12987 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12988 token_indent_info body_tinfo
12989 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12994 /* Mark if () ; with a special NOP_EXPR. */
12995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12997 cp_lexer_consume_token (parser
->lexer
);
12998 statement
= add_stmt (build_empty_stmt (body_loc
));
13000 if (guard_tinfo
.keyword
== RID_IF
13001 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
13002 warning_at (body_loc
, OPT_Wempty_body
,
13003 "suggest braces around empty body in an %<if%> statement");
13004 else if (guard_tinfo
.keyword
== RID_ELSE
)
13005 warning_at (body_loc
, OPT_Wempty_body
,
13006 "suggest braces around empty body in an %<else%> statement");
13008 /* if a compound is opened, we simply parse the statement directly. */
13009 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13010 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
13011 /* If the token is not a `{', then we must take special action. */
13014 /* Create a compound-statement. */
13015 statement
= begin_compound_stmt (0);
13016 /* Parse the dependent-statement. */
13017 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
13018 &body_loc_after_labels
);
13019 /* Finish the dummy compound-statement. */
13020 finish_compound_stmt (statement
);
13023 token_indent_info next_tinfo
13024 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13025 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13027 if (body_loc_after_labels
!= UNKNOWN_LOCATION
13028 && next_tinfo
.type
!= CPP_SEMICOLON
)
13029 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
13030 guard_tinfo
.location
, guard_tinfo
.keyword
);
13032 /* Return the statement. */
13036 /* For some dependent statements (like `while (cond) statement'), we
13037 have already created a scope. Therefore, even if the dependent
13038 statement is a compound-statement, we do not want to create another
13042 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
13043 const token_indent_info
&guard_tinfo
)
13045 /* If the token is a `{', then we must take special action. */
13046 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13048 token_indent_info body_tinfo
13049 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13050 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13052 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13053 &loc_after_labels
);
13054 token_indent_info next_tinfo
13055 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13056 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13058 if (loc_after_labels
!= UNKNOWN_LOCATION
13059 && next_tinfo
.type
!= CPP_SEMICOLON
)
13060 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13061 guard_tinfo
.location
,
13062 guard_tinfo
.keyword
);
13066 /* Avoid calling cp_parser_compound_statement, so that we
13067 don't create a new scope. Do everything else by hand. */
13068 matching_braces braces
;
13069 braces
.require_open (parser
);
13070 /* If the next keyword is `__label__' we have a label declaration. */
13071 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13072 cp_parser_label_declaration (parser
);
13073 /* Parse an (optional) statement-seq. */
13074 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13075 braces
.require_close (parser
);
13079 /* Declarations [gram.dcl.dcl] */
13081 /* Parse an optional declaration-sequence.
13085 declaration-seq declaration */
13088 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13092 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13094 if (token
->type
== CPP_CLOSE_BRACE
13095 || token
->type
== CPP_EOF
)
13098 cp_parser_toplevel_declaration (parser
);
13102 /* Parse a declaration.
13106 function-definition
13107 template-declaration
13108 explicit-instantiation
13109 explicit-specialization
13110 linkage-specification
13111 namespace-definition
13119 __extension__ declaration */
13122 cp_parser_declaration (cp_parser
* parser
)
13126 int saved_pedantic
;
13128 tree attributes
= NULL_TREE
;
13130 /* Check for the `__extension__' keyword. */
13131 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13133 /* Parse the qualified declaration. */
13134 cp_parser_declaration (parser
);
13135 /* Restore the PEDANTIC flag. */
13136 pedantic
= saved_pedantic
;
13141 /* Try to figure out what kind of declaration is present. */
13142 token1
= *cp_lexer_peek_token (parser
->lexer
);
13144 if (token1
.type
!= CPP_EOF
)
13145 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13148 token2
.type
= CPP_EOF
;
13149 token2
.keyword
= RID_MAX
;
13152 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13153 p
= obstack_alloc (&declarator_obstack
, 0);
13155 /* If the next token is `extern' and the following token is a string
13156 literal, then we have a linkage specification. */
13157 if (token1
.keyword
== RID_EXTERN
13158 && cp_parser_is_pure_string_literal (&token2
))
13159 cp_parser_linkage_specification (parser
);
13160 /* If the next token is `template', then we have either a template
13161 declaration, an explicit instantiation, or an explicit
13163 else if (token1
.keyword
== RID_TEMPLATE
)
13165 /* `template <>' indicates a template specialization. */
13166 if (token2
.type
== CPP_LESS
13167 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13168 cp_parser_explicit_specialization (parser
);
13169 /* `template <' indicates a template declaration. */
13170 else if (token2
.type
== CPP_LESS
)
13171 cp_parser_template_declaration (parser
, /*member_p=*/false);
13172 /* Anything else must be an explicit instantiation. */
13174 cp_parser_explicit_instantiation (parser
);
13176 /* If the next token is `export', then we have a template
13178 else if (token1
.keyword
== RID_EXPORT
)
13179 cp_parser_template_declaration (parser
, /*member_p=*/false);
13180 /* If the next token is `extern', 'static' or 'inline' and the one
13181 after that is `template', we have a GNU extended explicit
13182 instantiation directive. */
13183 else if (cp_parser_allow_gnu_extensions_p (parser
)
13184 && (token1
.keyword
== RID_EXTERN
13185 || token1
.keyword
== RID_STATIC
13186 || token1
.keyword
== RID_INLINE
)
13187 && token2
.keyword
== RID_TEMPLATE
)
13188 cp_parser_explicit_instantiation (parser
);
13189 /* If the next token is `namespace', check for a named or unnamed
13190 namespace definition. */
13191 else if (token1
.keyword
== RID_NAMESPACE
13192 && (/* A named namespace definition. */
13193 (token2
.type
== CPP_NAME
13194 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13196 || (token2
.type
== CPP_OPEN_SQUARE
13197 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13198 == CPP_OPEN_SQUARE
)
13199 /* An unnamed namespace definition. */
13200 || token2
.type
== CPP_OPEN_BRACE
13201 || token2
.keyword
== RID_ATTRIBUTE
))
13202 cp_parser_namespace_definition (parser
);
13203 /* An inline (associated) namespace definition. */
13204 else if (token1
.keyword
== RID_INLINE
13205 && token2
.keyword
== RID_NAMESPACE
)
13206 cp_parser_namespace_definition (parser
);
13207 /* Objective-C++ declaration/definition. */
13208 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13209 cp_parser_objc_declaration (parser
, NULL_TREE
);
13210 else if (c_dialect_objc ()
13211 && token1
.keyword
== RID_ATTRIBUTE
13212 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13213 cp_parser_objc_declaration (parser
, attributes
);
13214 /* At this point we may have a template declared by a concept
13216 else if (flag_concepts
13217 && cp_parser_template_declaration_after_export (parser
,
13218 /*member_p=*/false))
13221 /* Try to parse a block-declaration, or a function-definition. */
13222 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13224 /* Free any declarators allocated. */
13225 obstack_free (&declarator_obstack
, p
);
13228 /* Parse a namespace-scope declaration. */
13231 cp_parser_toplevel_declaration (cp_parser
* parser
)
13233 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13235 if (token
->type
== CPP_PRAGMA
)
13236 /* A top-level declaration can consist solely of a #pragma. A
13237 nested declaration cannot, so this is done here and not in
13238 cp_parser_declaration. (A #pragma at block scope is
13239 handled in cp_parser_statement.) */
13240 cp_parser_pragma (parser
, pragma_external
, NULL
);
13241 else if (token
->type
== CPP_SEMICOLON
)
13243 /* A declaration consisting of a single semicolon is
13244 invalid. Allow it unless we're being pedantic. */
13245 cp_lexer_consume_token (parser
->lexer
);
13246 if (!in_system_header_at (input_location
))
13247 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13250 /* Parse the declaration itself. */
13251 cp_parser_declaration (parser
);
13254 /* Parse a block-declaration.
13259 namespace-alias-definition
13266 __extension__ block-declaration
13271 static_assert-declaration
13273 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13274 part of a declaration-statement. */
13277 cp_parser_block_declaration (cp_parser
*parser
,
13281 int saved_pedantic
;
13283 /* Check for the `__extension__' keyword. */
13284 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13286 /* Parse the qualified declaration. */
13287 cp_parser_block_declaration (parser
, statement_p
);
13288 /* Restore the PEDANTIC flag. */
13289 pedantic
= saved_pedantic
;
13294 /* Peek at the next token to figure out which kind of declaration is
13296 token1
= cp_lexer_peek_token (parser
->lexer
);
13298 /* If the next keyword is `asm', we have an asm-definition. */
13299 if (token1
->keyword
== RID_ASM
)
13302 cp_parser_commit_to_tentative_parse (parser
);
13303 cp_parser_asm_definition (parser
);
13305 /* If the next keyword is `namespace', we have a
13306 namespace-alias-definition. */
13307 else if (token1
->keyword
== RID_NAMESPACE
)
13308 cp_parser_namespace_alias_definition (parser
);
13309 /* If the next keyword is `using', we have a
13310 using-declaration, a using-directive, or an alias-declaration. */
13311 else if (token1
->keyword
== RID_USING
)
13316 cp_parser_commit_to_tentative_parse (parser
);
13317 /* If the token after `using' is `namespace', then we have a
13318 using-directive. */
13319 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13320 if (token2
->keyword
== RID_NAMESPACE
)
13321 cp_parser_using_directive (parser
);
13322 /* If the second token after 'using' is '=', then we have an
13323 alias-declaration. */
13324 else if (cxx_dialect
>= cxx11
13325 && token2
->type
== CPP_NAME
13326 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13327 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13328 cp_parser_alias_declaration (parser
);
13329 /* Otherwise, it's a using-declaration. */
13331 cp_parser_using_declaration (parser
,
13332 /*access_declaration_p=*/false);
13334 /* If the next keyword is `__label__' we have a misplaced label
13336 else if (token1
->keyword
== RID_LABEL
)
13338 cp_lexer_consume_token (parser
->lexer
);
13339 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13340 cp_parser_skip_to_end_of_statement (parser
);
13341 /* If the next token is now a `;', consume it. */
13342 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13343 cp_lexer_consume_token (parser
->lexer
);
13345 /* If the next token is `static_assert' we have a static assertion. */
13346 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13347 cp_parser_static_assert (parser
, /*member_p=*/false);
13348 /* Anything else must be a simple-declaration. */
13350 cp_parser_simple_declaration (parser
, !statement_p
,
13351 /*maybe_range_for_decl*/NULL
);
13354 /* Parse a simple-declaration.
13356 simple-declaration:
13357 decl-specifier-seq [opt] init-declarator-list [opt] ;
13358 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13359 brace-or-equal-initializer ;
13361 init-declarator-list:
13363 init-declarator-list , init-declarator
13365 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13366 function-definition as a simple-declaration.
13368 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13369 parsed declaration if it is an uninitialized single declarator not followed
13370 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13371 if present, will not be consumed. */
13374 cp_parser_simple_declaration (cp_parser
* parser
,
13375 bool function_definition_allowed_p
,
13376 tree
*maybe_range_for_decl
)
13378 cp_decl_specifier_seq decl_specifiers
;
13379 int declares_class_or_enum
;
13380 bool saw_declarator
;
13381 location_t comma_loc
= UNKNOWN_LOCATION
;
13382 location_t init_loc
= UNKNOWN_LOCATION
;
13384 if (maybe_range_for_decl
)
13385 *maybe_range_for_decl
= NULL_TREE
;
13387 /* Defer access checks until we know what is being declared; the
13388 checks for names appearing in the decl-specifier-seq should be
13389 done as if we were in the scope of the thing being declared. */
13390 push_deferring_access_checks (dk_deferred
);
13392 /* Parse the decl-specifier-seq. We have to keep track of whether
13393 or not the decl-specifier-seq declares a named class or
13394 enumeration type, since that is the only case in which the
13395 init-declarator-list is allowed to be empty.
13399 In a simple-declaration, the optional init-declarator-list can be
13400 omitted only when declaring a class or enumeration, that is when
13401 the decl-specifier-seq contains either a class-specifier, an
13402 elaborated-type-specifier, or an enum-specifier. */
13403 cp_parser_decl_specifier_seq (parser
,
13404 CP_PARSER_FLAGS_OPTIONAL
,
13406 &declares_class_or_enum
);
13407 /* We no longer need to defer access checks. */
13408 stop_deferring_access_checks ();
13410 /* In a block scope, a valid declaration must always have a
13411 decl-specifier-seq. By not trying to parse declarators, we can
13412 resolve the declaration/expression ambiguity more quickly. */
13413 if (!function_definition_allowed_p
13414 && !decl_specifiers
.any_specifiers_p
)
13416 cp_parser_error (parser
, "expected declaration");
13420 /* If the next two tokens are both identifiers, the code is
13421 erroneous. The usual cause of this situation is code like:
13425 where "T" should name a type -- but does not. */
13426 if (!decl_specifiers
.any_type_specifiers_p
13427 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13429 /* If parsing tentatively, we should commit; we really are
13430 looking at a declaration. */
13431 cp_parser_commit_to_tentative_parse (parser
);
13436 cp_parser_maybe_commit_to_declaration (parser
,
13437 decl_specifiers
.any_specifiers_p
);
13439 /* Look for C++17 decomposition declaration. */
13440 for (size_t n
= 1; ; n
++)
13441 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13442 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13444 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13445 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13446 && decl_specifiers
.any_specifiers_p
)
13449 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13450 maybe_range_for_decl
,
13453 /* The next token should be either a `,' or a `;'. */
13454 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13455 /* If it's a `;', we are done. */
13456 if (token
->type
== CPP_SEMICOLON
)
13458 else if (maybe_range_for_decl
)
13460 if (*maybe_range_for_decl
== NULL_TREE
)
13461 *maybe_range_for_decl
= error_mark_node
;
13464 /* Anything else is an error. */
13467 /* If we have already issued an error message we don't need
13468 to issue another one. */
13469 if ((decl
!= error_mark_node
13470 && DECL_INITIAL (decl
) != error_mark_node
)
13471 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13472 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13473 /* Skip tokens until we reach the end of the statement. */
13474 cp_parser_skip_to_end_of_statement (parser
);
13475 /* If the next token is now a `;', consume it. */
13476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13477 cp_lexer_consume_token (parser
->lexer
);
13485 bool auto_specifier_p
;
13486 /* NULL_TREE if both variable and function declaration are allowed,
13487 error_mark_node if function declaration are not allowed and
13488 a FUNCTION_DECL that should be diagnosed if it is followed by
13489 variable declarations. */
13490 tree auto_function_declaration
;
13492 last_type
= NULL_TREE
;
13494 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13495 auto_function_declaration
= NULL_TREE
;
13497 /* Keep going until we hit the `;' at the end of the simple
13499 saw_declarator
= false;
13500 while (cp_lexer_next_token_is_not (parser
->lexer
,
13504 bool function_definition_p
;
13506 tree auto_result
= NULL_TREE
;
13508 if (saw_declarator
)
13510 /* If we are processing next declarator, comma is expected */
13511 token
= cp_lexer_peek_token (parser
->lexer
);
13512 gcc_assert (token
->type
== CPP_COMMA
);
13513 cp_lexer_consume_token (parser
->lexer
);
13514 if (maybe_range_for_decl
)
13516 *maybe_range_for_decl
= error_mark_node
;
13517 if (comma_loc
== UNKNOWN_LOCATION
)
13518 comma_loc
= token
->location
;
13522 saw_declarator
= true;
13524 /* Parse the init-declarator. */
13525 decl
= cp_parser_init_declarator (parser
,
13526 CP_PARSER_FLAGS_NONE
,
13529 function_definition_allowed_p
,
13530 /*member_p=*/false,
13531 declares_class_or_enum
,
13532 &function_definition_p
,
13533 maybe_range_for_decl
,
13536 /* If an error occurred while parsing tentatively, exit quickly.
13537 (That usually happens when in the body of a function; each
13538 statement is treated as a declaration-statement until proven
13540 if (cp_parser_error_occurred (parser
))
13543 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13545 /* If the init-declarator-list contains more than one
13546 init-declarator, they shall all form declarations of
13548 if (auto_function_declaration
== NULL_TREE
)
13549 auto_function_declaration
13550 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13551 else if (TREE_CODE (decl
) == FUNCTION_DECL
13552 || auto_function_declaration
!= error_mark_node
)
13554 error_at (decl_specifiers
.locations
[ds_type_spec
],
13555 "non-variable %qD in declaration with more than one "
13556 "declarator with placeholder type",
13557 TREE_CODE (decl
) == FUNCTION_DECL
13558 ? decl
: auto_function_declaration
);
13559 auto_function_declaration
= error_mark_node
;
13564 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13567 && last_type
!= error_mark_node
13568 && !same_type_p (auto_result
, last_type
))
13570 /* If the list of declarators contains more than one declarator,
13571 the type of each declared variable is determined as described
13572 above. If the type deduced for the template parameter U is not
13573 the same in each deduction, the program is ill-formed. */
13574 error_at (decl_specifiers
.locations
[ds_type_spec
],
13575 "inconsistent deduction for %qT: %qT and then %qT",
13576 decl_specifiers
.type
, last_type
, auto_result
);
13577 last_type
= error_mark_node
;
13580 last_type
= auto_result
;
13583 /* Handle function definitions specially. */
13584 if (function_definition_p
)
13586 /* If the next token is a `,', then we are probably
13587 processing something like:
13591 which is erroneous. */
13592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13594 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13595 error_at (token
->location
,
13597 " declarations and function-definitions is forbidden");
13599 /* Otherwise, we're done with the list of declarators. */
13602 pop_deferring_access_checks ();
13606 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13607 *maybe_range_for_decl
= decl
;
13608 /* The next token should be either a `,' or a `;'. */
13609 token
= cp_lexer_peek_token (parser
->lexer
);
13610 /* If it's a `,', there are more declarators to come. */
13611 if (token
->type
== CPP_COMMA
)
13612 /* will be consumed next time around */;
13613 /* If it's a `;', we are done. */
13614 else if (token
->type
== CPP_SEMICOLON
)
13616 else if (maybe_range_for_decl
)
13618 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13619 permerror (decl_specifiers
.locations
[ds_type_spec
],
13620 "types may not be defined in a for-range-declaration");
13623 /* Anything else is an error. */
13626 /* If we have already issued an error message we don't need
13627 to issue another one. */
13628 if ((decl
!= error_mark_node
13629 && DECL_INITIAL (decl
) != error_mark_node
)
13630 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13631 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13632 /* Skip tokens until we reach the end of the statement. */
13633 cp_parser_skip_to_end_of_statement (parser
);
13634 /* If the next token is now a `;', consume it. */
13635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13636 cp_lexer_consume_token (parser
->lexer
);
13639 /* After the first time around, a function-definition is not
13640 allowed -- even if it was OK at first. For example:
13645 function_definition_allowed_p
= false;
13648 /* Issue an error message if no declarators are present, and the
13649 decl-specifier-seq does not itself declare a class or
13650 enumeration: [dcl.dcl]/3. */
13651 if (!saw_declarator
)
13653 if (cp_parser_declares_only_class_p (parser
))
13655 if (!declares_class_or_enum
13656 && decl_specifiers
.type
13657 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13658 /* Ensure an error is issued anyway when finish_decltype_type,
13659 called via cp_parser_decl_specifier_seq, returns a class or
13660 an enumeration (c++/51786). */
13661 decl_specifiers
.type
= NULL_TREE
;
13662 shadow_tag (&decl_specifiers
);
13664 /* Perform any deferred access checks. */
13665 perform_deferred_access_checks (tf_warning_or_error
);
13668 /* Consume the `;'. */
13670 if (!maybe_range_for_decl
)
13671 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13672 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13674 if (init_loc
!= UNKNOWN_LOCATION
)
13675 error_at (init_loc
, "initializer in range-based %<for%> loop");
13676 if (comma_loc
!= UNKNOWN_LOCATION
)
13677 error_at (comma_loc
,
13678 "multiple declarations in range-based %<for%> loop");
13682 pop_deferring_access_checks ();
13685 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13686 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13690 cp_parser_decomposition_declaration (cp_parser
*parser
,
13691 cp_decl_specifier_seq
*decl_specifiers
,
13692 tree
*maybe_range_for_decl
,
13693 location_t
*init_loc
)
13695 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13696 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13697 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13699 /* Parse the identifier-list. */
13700 auto_vec
<cp_expr
, 10> v
;
13701 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13704 cp_expr e
= cp_parser_identifier (parser
);
13705 if (e
.get_value () == error_mark_node
)
13708 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13710 cp_lexer_consume_token (parser
->lexer
);
13713 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13714 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13716 end_loc
= UNKNOWN_LOCATION
;
13717 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13719 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13720 cp_lexer_consume_token (parser
->lexer
);
13723 cp_parser_skip_to_end_of_statement (parser
);
13724 return error_mark_node
;
13728 if (cxx_dialect
< cxx17
)
13729 pedwarn (loc
, 0, "structured bindings only available with "
13730 "%<-std=c++17%> or %<-std=gnu++17%>");
13733 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13734 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13735 declarator
->id_loc
= loc
;
13736 if (ref_qual
!= REF_QUAL_NONE
)
13737 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13738 ref_qual
== REF_QUAL_RVALUE
,
13740 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13741 NULL_TREE
, decl_specifiers
->attributes
,
13743 tree orig_decl
= decl
;
13747 cp_decl_specifier_seq decl_specs
;
13748 clear_decl_specs (&decl_specs
);
13749 decl_specs
.type
= make_auto ();
13751 FOR_EACH_VEC_ELT (v
, i
, e
)
13754 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13755 sfk_none
, e
.get_location ());
13758 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13759 declarator
->id_loc
= e
.get_location ();
13761 tree elt_pushed_scope
;
13762 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13763 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13764 if (decl2
== error_mark_node
)
13765 decl
= error_mark_node
;
13766 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13768 /* Ensure we've diagnosed redeclaration if we aren't creating
13770 gcc_assert (errorcount
);
13771 decl
= error_mark_node
;
13775 if (elt_pushed_scope
)
13776 pop_scope (elt_pushed_scope
);
13781 error_at (loc
, "empty structured binding declaration");
13782 decl
= error_mark_node
;
13785 if (maybe_range_for_decl
== NULL
13786 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13788 bool non_constant_p
= false, is_direct_init
= false;
13789 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13790 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13792 if (initializer
== NULL_TREE
13793 || (TREE_CODE (initializer
) == TREE_LIST
13794 && TREE_CHAIN (initializer
))
13796 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13797 && CONSTRUCTOR_NELTS (initializer
) != 1))
13799 error_at (loc
, "invalid initializer for structured binding "
13801 initializer
= error_mark_node
;
13804 if (decl
!= error_mark_node
)
13806 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13807 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13808 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13809 cp_finish_decomp (decl
, prev
, v
.length ());
13812 else if (decl
!= error_mark_node
)
13814 *maybe_range_for_decl
= prev
;
13815 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13816 the underlying DECL. */
13817 cp_finish_decomp (decl
, prev
, v
.length ());
13821 pop_scope (pushed_scope
);
13823 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13825 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13826 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13832 /* Parse a decl-specifier-seq.
13834 decl-specifier-seq:
13835 decl-specifier-seq [opt] decl-specifier
13836 decl-specifier attribute-specifier-seq [opt] (C++11)
13839 storage-class-specifier
13850 Concepts Extension:
13855 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13857 The parser flags FLAGS is used to control type-specifier parsing.
13859 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13862 1: one of the decl-specifiers is an elaborated-type-specifier
13863 (i.e., a type declaration)
13864 2: one of the decl-specifiers is an enum-specifier or a
13865 class-specifier (i.e., a type definition)
13870 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13871 cp_parser_flags flags
,
13872 cp_decl_specifier_seq
*decl_specs
,
13873 int* declares_class_or_enum
)
13875 bool constructor_possible_p
= !parser
->in_declarator_p
;
13876 bool found_decl_spec
= false;
13877 cp_token
*start_token
= NULL
;
13880 /* Clear DECL_SPECS. */
13881 clear_decl_specs (decl_specs
);
13883 /* Assume no class or enumeration type is declared. */
13884 *declares_class_or_enum
= 0;
13886 /* Keep reading specifiers until there are no more to read. */
13889 bool constructor_p
;
13893 /* Peek at the next token. */
13894 token
= cp_lexer_peek_token (parser
->lexer
);
13896 /* Save the first token of the decl spec list for error
13899 start_token
= token
;
13900 /* Handle attributes. */
13901 if (cp_next_tokens_can_be_attribute_p (parser
))
13903 /* Parse the attributes. */
13904 tree attrs
= cp_parser_attributes_opt (parser
);
13906 /* In a sequence of declaration specifiers, c++11 attributes
13907 appertain to the type that precede them. In that case
13910 The attribute-specifier-seq affects the type only for
13911 the declaration it appears in, not other declarations
13912 involving the same type.
13914 But for now let's force the user to position the
13915 attribute either at the beginning of the declaration or
13916 after the declarator-id, which would clearly mean that it
13917 applies to the declarator. */
13918 if (cxx11_attribute_p (attrs
))
13920 if (!found_decl_spec
)
13921 /* The c++11 attribute is at the beginning of the
13922 declaration. It appertains to the entity being
13926 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13928 /* This is an attribute following a
13929 class-specifier. */
13930 if (decl_specs
->type_definition_p
)
13931 warn_misplaced_attr_for_class_type (token
->location
,
13937 decl_specs
->std_attributes
13938 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13939 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13940 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13946 decl_specs
->attributes
13947 = attr_chainon (decl_specs
->attributes
, attrs
);
13948 if (decl_specs
->locations
[ds_attribute
] == 0)
13949 decl_specs
->locations
[ds_attribute
] = token
->location
;
13952 /* Assume we will find a decl-specifier keyword. */
13953 found_decl_spec
= true;
13954 /* If the next token is an appropriate keyword, we can simply
13955 add it to the list. */
13956 switch (token
->keyword
)
13962 if (!at_class_scope_p ())
13964 gcc_rich_location
richloc (token
->location
);
13965 richloc
.add_fixit_remove ();
13966 error_at (&richloc
, "%<friend%> used outside of class");
13967 cp_lexer_purge_token (parser
->lexer
);
13972 /* Consume the token. */
13973 cp_lexer_consume_token (parser
->lexer
);
13977 case RID_CONSTEXPR
:
13979 cp_lexer_consume_token (parser
->lexer
);
13984 cp_lexer_consume_token (parser
->lexer
);
13985 /* In C++20 a concept definition is just 'concept name = expr;'
13986 Support that syntax by pretending we've seen 'bool'. */
13987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
13988 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
13990 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
13991 token
, /*type_definition*/false);
13992 decl_specs
->any_type_specifiers_p
= true;
13996 /* function-specifier:
14003 cp_parser_function_specifier_opt (parser
, decl_specs
);
14010 /* Consume the token. */
14011 cp_lexer_consume_token (parser
->lexer
);
14012 /* A constructor declarator cannot appear in a typedef. */
14013 constructor_possible_p
= false;
14014 /* The "typedef" keyword can only occur in a declaration; we
14015 may as well commit at this point. */
14016 cp_parser_commit_to_tentative_parse (parser
);
14018 if (decl_specs
->storage_class
!= sc_none
)
14019 decl_specs
->conflicting_specifiers_p
= true;
14022 /* storage-class-specifier:
14032 if (cxx_dialect
== cxx98
)
14034 /* Consume the token. */
14035 cp_lexer_consume_token (parser
->lexer
);
14037 /* Complain about `auto' as a storage specifier, if
14038 we're complaining about C++0x compatibility. */
14039 gcc_rich_location
richloc (token
->location
);
14040 richloc
.add_fixit_remove ();
14041 warning_at (&richloc
, OPT_Wc__11_compat
,
14042 "%<auto%> changes meaning in C++11; "
14043 "please remove it");
14045 /* Set the storage class anyway. */
14046 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
14050 /* C++0x auto type-specifier. */
14051 found_decl_spec
= false;
14058 /* Consume the token. */
14059 cp_lexer_consume_token (parser
->lexer
);
14060 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14064 /* Consume the token. */
14066 cp_lexer_consume_token (parser
->lexer
);
14070 /* We did not yet find a decl-specifier yet. */
14071 found_decl_spec
= false;
14075 if (found_decl_spec
14076 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14077 && token
->keyword
!= RID_CONSTEXPR
)
14078 error ("decl-specifier invalid in condition");
14080 if (found_decl_spec
14081 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14082 && token
->keyword
!= RID_MUTABLE
14083 && token
->keyword
!= RID_CONSTEXPR
)
14084 error_at (token
->location
, "%qD invalid in lambda",
14085 ridpointers
[token
->keyword
]);
14088 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14090 /* Constructors are a special case. The `S' in `S()' is not a
14091 decl-specifier; it is the beginning of the declarator. */
14093 = (!found_decl_spec
14094 && constructor_possible_p
14095 && (cp_parser_constructor_declarator_p
14096 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
14099 /* If we don't have a DECL_SPEC yet, then we must be looking at
14100 a type-specifier. */
14101 if (!found_decl_spec
&& !constructor_p
)
14103 int decl_spec_declares_class_or_enum
;
14104 bool is_cv_qualifier
;
14108 = cp_parser_type_specifier (parser
, flags
,
14110 /*is_declaration=*/true,
14111 &decl_spec_declares_class_or_enum
,
14113 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14115 /* If this type-specifier referenced a user-defined type
14116 (a typedef, class-name, etc.), then we can't allow any
14117 more such type-specifiers henceforth.
14121 The longest sequence of decl-specifiers that could
14122 possibly be a type name is taken as the
14123 decl-specifier-seq of a declaration. The sequence shall
14124 be self-consistent as described below.
14128 As a general rule, at most one type-specifier is allowed
14129 in the complete decl-specifier-seq of a declaration. The
14130 only exceptions are the following:
14132 -- const or volatile can be combined with any other
14135 -- signed or unsigned can be combined with char, long,
14143 void g (const int Pc);
14145 Here, Pc is *not* part of the decl-specifier seq; it's
14146 the declarator. Therefore, once we see a type-specifier
14147 (other than a cv-qualifier), we forbid any additional
14148 user-defined types. We *do* still allow things like `int
14149 int' to be considered a decl-specifier-seq, and issue the
14150 error message later. */
14151 if (type_spec
&& !is_cv_qualifier
)
14152 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14153 /* A constructor declarator cannot follow a type-specifier. */
14156 constructor_possible_p
= false;
14157 found_decl_spec
= true;
14158 if (!is_cv_qualifier
)
14159 decl_specs
->any_type_specifiers_p
= true;
14161 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14162 error_at (token
->location
, "type-specifier invalid in lambda");
14166 /* If we still do not have a DECL_SPEC, then there are no more
14167 decl-specifiers. */
14168 if (!found_decl_spec
)
14171 decl_specs
->any_specifiers_p
= true;
14172 /* After we see one decl-specifier, further decl-specifiers are
14173 always optional. */
14174 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14177 /* Don't allow a friend specifier with a class definition. */
14178 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14179 && (*declares_class_or_enum
& 2))
14180 error_at (decl_specs
->locations
[ds_friend
],
14181 "class definition may not be declared a friend");
14184 /* Parse an (optional) storage-class-specifier.
14186 storage-class-specifier:
14195 storage-class-specifier:
14198 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14201 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14203 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14206 if (cxx_dialect
!= cxx98
)
14208 /* Fall through for C++98. */
14209 gcc_fallthrough ();
14216 /* Consume the token. */
14217 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14224 /* Parse an (optional) function-specifier.
14226 function-specifier:
14232 explicit(constant-expression)
14234 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14235 Updates DECL_SPECS, if it is non-NULL. */
14238 cp_parser_function_specifier_opt (cp_parser
* parser
,
14239 cp_decl_specifier_seq
*decl_specs
)
14241 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14242 switch (token
->keyword
)
14245 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14249 /* 14.5.2.3 [temp.mem]
14251 A member function template shall not be virtual. */
14252 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14253 && current_class_type
)
14254 error_at (token
->location
, "templates may not be %<virtual%>");
14256 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14261 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14262 /* If we see '(', it's C++20 explicit(bool). */
14264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14266 matching_parens parens
;
14267 parens
.consume_open (parser
);
14269 /* New types are not allowed in an explicit-specifier. */
14270 const char *saved_message
14271 = parser
->type_definition_forbidden_message
;
14272 parser
->type_definition_forbidden_message
14273 = G_("types may not be defined in explicit-specifier");
14275 if (cxx_dialect
< cxx2a
)
14276 pedwarn (token
->location
, 0,
14277 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14278 "or %<-std=gnu++2a%>");
14280 /* Parse the constant-expression. */
14281 expr
= cp_parser_constant_expression (parser
);
14283 /* Restore the saved message. */
14284 parser
->type_definition_forbidden_message
= saved_message
;
14285 parens
.require_close (parser
);
14288 /* The explicit-specifier explicit without a constant-expression is
14289 equivalent to the explicit-specifier explicit(true). */
14290 expr
= boolean_true_node
;
14293 "the constant-expression, if supplied, shall be a contextually
14294 converted constant expression of type bool." */
14295 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14296 /* We could evaluate it -- mark the decl as appropriate. */
14297 if (expr
== boolean_true_node
)
14298 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14299 else if (expr
== boolean_false_node
)
14300 /* Don't mark the decl as explicit. */;
14301 else if (decl_specs
)
14302 /* The expression was value-dependent. Remember it so that we can
14303 substitute it later. */
14304 decl_specs
->explicit_specifier
= expr
;
14312 /* Consume the token. */
14313 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14316 /* Parse a linkage-specification.
14318 linkage-specification:
14319 extern string-literal { declaration-seq [opt] }
14320 extern string-literal declaration */
14323 cp_parser_linkage_specification (cp_parser
* parser
)
14327 /* Look for the `extern' keyword. */
14328 cp_token
*extern_token
14329 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14331 /* Look for the string-literal. */
14332 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14333 linkage
= cp_parser_string_literal (parser
, false, false);
14335 /* Transform the literal into an identifier. If the literal is a
14336 wide-character string, or contains embedded NULs, then we can't
14337 handle it as the user wants. */
14338 if (strlen (TREE_STRING_POINTER (linkage
))
14339 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14341 cp_parser_error (parser
, "invalid linkage-specification");
14342 /* Assume C++ linkage. */
14343 linkage
= lang_name_cplusplus
;
14346 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14348 /* We're now using the new linkage. */
14349 push_lang_context (linkage
);
14351 /* Preserve the location of the the innermost linkage specification,
14352 tracking the locations of nested specifications via a local. */
14353 location_t saved_location
14354 = parser
->innermost_linkage_specification_location
;
14355 /* Construct a location ranging from the start of the "extern" to
14356 the end of the string-literal, with the caret at the start, e.g.:
14360 parser
->innermost_linkage_specification_location
14361 = make_location (extern_token
->location
,
14362 extern_token
->location
,
14363 get_finish (string_token
->location
));
14365 /* If the next token is a `{', then we're using the first
14367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14369 cp_ensure_no_omp_declare_simd (parser
);
14370 cp_ensure_no_oacc_routine (parser
);
14372 /* Consume the `{' token. */
14373 matching_braces braces
;
14374 braces
.consume_open (parser
);
14375 /* Parse the declarations. */
14376 cp_parser_declaration_seq_opt (parser
);
14377 /* Look for the closing `}'. */
14378 braces
.require_close (parser
);
14380 /* Otherwise, there's just one declaration. */
14383 bool saved_in_unbraced_linkage_specification_p
;
14385 saved_in_unbraced_linkage_specification_p
14386 = parser
->in_unbraced_linkage_specification_p
;
14387 parser
->in_unbraced_linkage_specification_p
= true;
14388 cp_parser_declaration (parser
);
14389 parser
->in_unbraced_linkage_specification_p
14390 = saved_in_unbraced_linkage_specification_p
;
14393 /* We're done with the linkage-specification. */
14394 pop_lang_context ();
14396 /* Restore location of parent linkage specification, if any. */
14397 parser
->innermost_linkage_specification_location
= saved_location
;
14400 /* Parse a static_assert-declaration.
14402 static_assert-declaration:
14403 static_assert ( constant-expression , string-literal ) ;
14404 static_assert ( constant-expression ) ; (C++17)
14406 If MEMBER_P, this static_assert is a class member. */
14409 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14412 location_t token_loc
;
14416 /* Peek at the `static_assert' token so we can keep track of exactly
14417 where the static assertion started. */
14418 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14420 /* Look for the `static_assert' keyword. */
14421 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14425 /* We know we are in a static assertion; commit to any tentative
14427 if (cp_parser_parsing_tentatively (parser
))
14428 cp_parser_commit_to_tentative_parse (parser
);
14430 /* Parse the `(' starting the static assertion condition. */
14431 matching_parens parens
;
14432 parens
.require_open (parser
);
14434 /* Parse the constant-expression. Allow a non-constant expression
14435 here in order to give better diagnostics in finish_static_assert. */
14437 cp_parser_constant_expression (parser
,
14438 /*allow_non_constant_p=*/true,
14439 /*non_constant_p=*/&dummy
);
14441 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14443 if (cxx_dialect
< cxx17
)
14444 pedwarn (input_location
, OPT_Wpedantic
,
14445 "%<static_assert%> without a message "
14446 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14448 cp_lexer_consume_token (parser
->lexer
);
14449 message
= build_string (1, "");
14450 TREE_TYPE (message
) = char_array_type_node
;
14451 fix_string_type (message
);
14455 /* Parse the separating `,'. */
14456 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14458 /* Parse the string-literal message. */
14459 message
= cp_parser_string_literal (parser
,
14460 /*translate=*/false,
14463 /* A `)' completes the static assertion. */
14464 if (!parens
.require_close (parser
))
14465 cp_parser_skip_to_closing_parenthesis (parser
,
14466 /*recovering=*/true,
14467 /*or_comma=*/false,
14468 /*consume_paren=*/true);
14471 /* A semicolon terminates the declaration. */
14472 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14474 /* Get the location for the static assertion. Use that of the
14475 condition if available, otherwise, use that of the "static_assert"
14477 location_t assert_loc
= condition
.get_location ();
14478 if (assert_loc
== UNKNOWN_LOCATION
)
14479 assert_loc
= token_loc
;
14481 /* Complete the static assertion, which may mean either processing
14482 the static assert now or saving it for template instantiation. */
14483 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14486 /* Parse the expression in decltype ( expression ). */
14489 cp_parser_decltype_expr (cp_parser
*parser
,
14490 bool &id_expression_or_member_access_p
)
14492 cp_token
*id_expr_start_token
;
14495 /* Since we're going to preserve any side-effects from this parse, set up a
14496 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14497 in the expression. */
14498 tentative_firewall
firewall (parser
);
14500 /* First, try parsing an id-expression. */
14501 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14502 cp_parser_parse_tentatively (parser
);
14503 expr
= cp_parser_id_expression (parser
,
14504 /*template_keyword_p=*/false,
14505 /*check_dependency_p=*/true,
14506 /*template_p=*/NULL
,
14507 /*declarator_p=*/false,
14508 /*optional_p=*/false);
14510 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14512 bool non_integral_constant_expression_p
= false;
14513 tree id_expression
= expr
;
14515 const char *error_msg
;
14517 if (identifier_p (expr
))
14518 /* Lookup the name we got back from the id-expression. */
14519 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14520 id_expr_start_token
->location
);
14522 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14523 /* A template without args is not a complete id-expression. */
14524 expr
= error_mark_node
;
14527 && expr
!= error_mark_node
14528 && TREE_CODE (expr
) != TYPE_DECL
14529 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14530 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14531 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14533 /* Complete lookup of the id-expression. */
14534 expr
= (finish_id_expression
14535 (id_expression
, expr
, parser
->scope
, &idk
,
14536 /*integral_constant_expression_p=*/false,
14537 /*allow_non_integral_constant_expression_p=*/true,
14538 &non_integral_constant_expression_p
,
14539 /*template_p=*/false,
14541 /*address_p=*/false,
14542 /*template_arg_p=*/false,
14544 id_expr_start_token
->location
));
14546 if (expr
== error_mark_node
)
14547 /* We found an id-expression, but it was something that we
14548 should not have found. This is an error, not something
14549 we can recover from, so note that we found an
14550 id-expression and we'll recover as gracefully as
14552 id_expression_or_member_access_p
= true;
14556 && expr
!= error_mark_node
14557 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14558 /* We have an id-expression. */
14559 id_expression_or_member_access_p
= true;
14562 if (!id_expression_or_member_access_p
)
14564 /* Abort the id-expression parse. */
14565 cp_parser_abort_tentative_parse (parser
);
14567 /* Parsing tentatively, again. */
14568 cp_parser_parse_tentatively (parser
);
14570 /* Parse a class member access. */
14571 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14572 /*cast_p=*/false, /*decltype*/true,
14573 /*member_access_only_p=*/true, NULL
);
14576 && expr
!= error_mark_node
14577 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14578 /* We have an id-expression. */
14579 id_expression_or_member_access_p
= true;
14582 if (id_expression_or_member_access_p
)
14583 /* We have parsed the complete id-expression or member access. */
14584 cp_parser_parse_definitely (parser
);
14587 /* Abort our attempt to parse an id-expression or member access
14589 cp_parser_abort_tentative_parse (parser
);
14591 /* Commit to the tentative_firewall so we get syntax errors. */
14592 cp_parser_commit_to_tentative_parse (parser
);
14594 /* Parse a full expression. */
14595 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14596 /*decltype_p=*/true);
14602 /* Parse a `decltype' type. Returns the type.
14604 simple-type-specifier:
14605 decltype ( expression )
14607 decltype ( auto ) */
14610 cp_parser_decltype (cp_parser
*parser
)
14612 bool id_expression_or_member_access_p
= false;
14613 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14615 if (start_token
->type
== CPP_DECLTYPE
)
14617 /* Already parsed. */
14618 cp_lexer_consume_token (parser
->lexer
);
14619 return saved_checks_value (start_token
->u
.tree_check_value
);
14622 /* Look for the `decltype' token. */
14623 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14624 return error_mark_node
;
14626 /* Parse the opening `('. */
14627 matching_parens parens
;
14628 if (!parens
.require_open (parser
))
14629 return error_mark_node
;
14631 push_deferring_access_checks (dk_deferred
);
14633 tree expr
= NULL_TREE
;
14635 if (cxx_dialect
>= cxx14
14636 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14637 /* decltype (auto) */
14638 cp_lexer_consume_token (parser
->lexer
);
14641 /* decltype (expression) */
14643 /* Types cannot be defined in a `decltype' expression. Save away the
14644 old message and set the new one. */
14645 const char *saved_message
= parser
->type_definition_forbidden_message
;
14646 parser
->type_definition_forbidden_message
14647 = G_("types may not be defined in %<decltype%> expressions");
14649 /* The restrictions on constant-expressions do not apply inside
14650 decltype expressions. */
14651 bool saved_integral_constant_expression_p
14652 = parser
->integral_constant_expression_p
;
14653 bool saved_non_integral_constant_expression_p
14654 = parser
->non_integral_constant_expression_p
;
14655 parser
->integral_constant_expression_p
= false;
14657 /* Within a parenthesized expression, a `>' token is always
14658 the greater-than operator. */
14659 bool saved_greater_than_is_operator_p
14660 = parser
->greater_than_is_operator_p
;
14661 parser
->greater_than_is_operator_p
= true;
14663 /* Do not actually evaluate the expression. */
14664 ++cp_unevaluated_operand
;
14666 /* Do not warn about problems with the expression. */
14667 ++c_inhibit_evaluation_warnings
;
14669 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14670 STRIP_ANY_LOCATION_WRAPPER (expr
);
14672 /* Go back to evaluating expressions. */
14673 --cp_unevaluated_operand
;
14674 --c_inhibit_evaluation_warnings
;
14676 /* The `>' token might be the end of a template-id or
14677 template-parameter-list now. */
14678 parser
->greater_than_is_operator_p
14679 = saved_greater_than_is_operator_p
;
14681 /* Restore the old message and the integral constant expression
14683 parser
->type_definition_forbidden_message
= saved_message
;
14684 parser
->integral_constant_expression_p
14685 = saved_integral_constant_expression_p
;
14686 parser
->non_integral_constant_expression_p
14687 = saved_non_integral_constant_expression_p
;
14690 /* Parse to the closing `)'. */
14691 if (!parens
.require_close (parser
))
14693 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14694 /*consume_paren=*/true);
14695 pop_deferring_access_checks ();
14696 return error_mark_node
;
14702 expr
= make_decltype_auto ();
14703 AUTO_IS_DECLTYPE (expr
) = true;
14706 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14707 tf_warning_or_error
);
14709 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14711 start_token
->type
= CPP_DECLTYPE
;
14712 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14713 start_token
->u
.tree_check_value
->value
= expr
;
14714 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14715 start_token
->keyword
= RID_MAX
;
14716 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14718 pop_to_parent_deferring_access_checks ();
14723 /* Special member functions [gram.special] */
14725 /* Parse a conversion-function-id.
14727 conversion-function-id:
14728 operator conversion-type-id
14730 Returns an IDENTIFIER_NODE representing the operator. */
14733 cp_parser_conversion_function_id (cp_parser
* parser
)
14737 tree saved_qualifying_scope
;
14738 tree saved_object_scope
;
14739 tree pushed_scope
= NULL_TREE
;
14741 /* Look for the `operator' token. */
14742 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14743 return error_mark_node
;
14744 /* When we parse the conversion-type-id, the current scope will be
14745 reset. However, we need that information in able to look up the
14746 conversion function later, so we save it here. */
14747 saved_scope
= parser
->scope
;
14748 saved_qualifying_scope
= parser
->qualifying_scope
;
14749 saved_object_scope
= parser
->object_scope
;
14750 /* We must enter the scope of the class so that the names of
14751 entities declared within the class are available in the
14752 conversion-type-id. For example, consider:
14759 S::operator I() { ... }
14761 In order to see that `I' is a type-name in the definition, we
14762 must be in the scope of `S'. */
14764 pushed_scope
= push_scope (saved_scope
);
14765 /* Parse the conversion-type-id. */
14766 type
= cp_parser_conversion_type_id (parser
);
14767 /* Leave the scope of the class, if any. */
14769 pop_scope (pushed_scope
);
14770 /* Restore the saved scope. */
14771 parser
->scope
= saved_scope
;
14772 parser
->qualifying_scope
= saved_qualifying_scope
;
14773 parser
->object_scope
= saved_object_scope
;
14774 /* If the TYPE is invalid, indicate failure. */
14775 if (type
== error_mark_node
)
14776 return error_mark_node
;
14777 return make_conv_op_name (type
);
14780 /* Parse a conversion-type-id:
14782 conversion-type-id:
14783 type-specifier-seq conversion-declarator [opt]
14785 Returns the TYPE specified. */
14788 cp_parser_conversion_type_id (cp_parser
* parser
)
14791 cp_decl_specifier_seq type_specifiers
;
14792 cp_declarator
*declarator
;
14793 tree type_specified
;
14794 const char *saved_message
;
14796 /* Parse the attributes. */
14797 attributes
= cp_parser_attributes_opt (parser
);
14799 saved_message
= parser
->type_definition_forbidden_message
;
14800 parser
->type_definition_forbidden_message
14801 = G_("types may not be defined in a conversion-type-id");
14803 /* Parse the type-specifiers. */
14804 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
14805 /*is_declaration=*/false,
14806 /*is_trailing_return=*/false,
14809 parser
->type_definition_forbidden_message
= saved_message
;
14811 /* If that didn't work, stop. */
14812 if (type_specifiers
.type
== error_mark_node
)
14813 return error_mark_node
;
14814 /* Parse the conversion-declarator. */
14815 declarator
= cp_parser_conversion_declarator_opt (parser
);
14817 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14818 /*initialized=*/0, &attributes
);
14820 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14822 /* Don't give this error when parsing tentatively. This happens to
14823 work because we always parse this definitively once. */
14824 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14825 && type_uses_auto (type_specified
))
14827 if (cxx_dialect
< cxx14
)
14829 error ("invalid use of %<auto%> in conversion operator");
14830 return error_mark_node
;
14832 else if (template_parm_scope_p ())
14833 warning (0, "use of %<auto%> in member template "
14834 "conversion operator can never be deduced");
14837 return type_specified
;
14840 /* Parse an (optional) conversion-declarator.
14842 conversion-declarator:
14843 ptr-operator conversion-declarator [opt]
14847 static cp_declarator
*
14848 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14850 enum tree_code code
;
14851 tree class_type
, std_attributes
= NULL_TREE
;
14852 cp_cv_quals cv_quals
;
14854 /* We don't know if there's a ptr-operator next, or not. */
14855 cp_parser_parse_tentatively (parser
);
14856 /* Try the ptr-operator. */
14857 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14859 /* If it worked, look for more conversion-declarators. */
14860 if (cp_parser_parse_definitely (parser
))
14862 cp_declarator
*declarator
;
14864 /* Parse another optional declarator. */
14865 declarator
= cp_parser_conversion_declarator_opt (parser
);
14867 declarator
= cp_parser_make_indirect_declarator
14868 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14876 /* Parse an (optional) ctor-initializer.
14879 : mem-initializer-list */
14882 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14884 /* If the next token is not a `:', then there is no
14885 ctor-initializer. */
14886 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14888 /* Do default initialization of any bases and members. */
14889 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14890 finish_mem_initializers (NULL_TREE
);
14894 /* Consume the `:' token. */
14895 cp_lexer_consume_token (parser
->lexer
);
14896 /* And the mem-initializer-list. */
14897 cp_parser_mem_initializer_list (parser
);
14900 /* Parse a mem-initializer-list.
14902 mem-initializer-list:
14903 mem-initializer ... [opt]
14904 mem-initializer ... [opt] , mem-initializer-list */
14907 cp_parser_mem_initializer_list (cp_parser
* parser
)
14909 tree mem_initializer_list
= NULL_TREE
;
14910 tree target_ctor
= error_mark_node
;
14911 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14913 /* Let the semantic analysis code know that we are starting the
14914 mem-initializer-list. */
14915 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14916 error_at (token
->location
,
14917 "only constructors take member initializers");
14919 /* Loop through the list. */
14922 tree mem_initializer
;
14924 token
= cp_lexer_peek_token (parser
->lexer
);
14925 /* Parse the mem-initializer. */
14926 mem_initializer
= cp_parser_mem_initializer (parser
);
14927 /* If the next token is a `...', we're expanding member initializers. */
14928 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14930 || (mem_initializer
!= error_mark_node
14931 && check_for_bare_parameter_packs (TREE_PURPOSE
14932 (mem_initializer
))))
14934 /* Consume the `...'. */
14936 cp_lexer_consume_token (parser
->lexer
);
14938 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14939 can be expanded but members cannot. */
14940 if (mem_initializer
!= error_mark_node
14941 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14943 error_at (token
->location
,
14944 "cannot expand initializer for member %qD",
14945 TREE_PURPOSE (mem_initializer
));
14946 mem_initializer
= error_mark_node
;
14949 /* Construct the pack expansion type. */
14950 if (mem_initializer
!= error_mark_node
)
14951 mem_initializer
= make_pack_expansion (mem_initializer
);
14953 if (target_ctor
!= error_mark_node
14954 && mem_initializer
!= error_mark_node
)
14956 error ("mem-initializer for %qD follows constructor delegation",
14957 TREE_PURPOSE (mem_initializer
));
14958 mem_initializer
= error_mark_node
;
14960 /* Look for a target constructor. */
14961 if (mem_initializer
!= error_mark_node
14962 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14963 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14965 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14966 if (mem_initializer_list
)
14968 error ("constructor delegation follows mem-initializer for %qD",
14969 TREE_PURPOSE (mem_initializer_list
));
14970 mem_initializer
= error_mark_node
;
14972 target_ctor
= mem_initializer
;
14974 /* Add it to the list, unless it was erroneous. */
14975 if (mem_initializer
!= error_mark_node
)
14977 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14978 mem_initializer_list
= mem_initializer
;
14980 /* If the next token is not a `,', we're done. */
14981 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14983 /* Consume the `,' token. */
14984 cp_lexer_consume_token (parser
->lexer
);
14987 /* Perform semantic analysis. */
14988 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14989 finish_mem_initializers (mem_initializer_list
);
14992 /* Parse a mem-initializer.
14995 mem-initializer-id ( expression-list [opt] )
14996 mem-initializer-id braced-init-list
15001 ( expression-list [opt] )
15003 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15004 class) or FIELD_DECL (for a non-static data member) to initialize;
15005 the TREE_VALUE is the expression-list. An empty initialization
15006 list is represented by void_list_node. */
15009 cp_parser_mem_initializer (cp_parser
* parser
)
15011 tree mem_initializer_id
;
15012 tree expression_list
;
15014 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15016 /* Find out what is being initialized. */
15017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15019 permerror (token
->location
,
15020 "anachronistic old-style base class initializer");
15021 mem_initializer_id
= NULL_TREE
;
15025 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
15026 if (mem_initializer_id
== error_mark_node
)
15027 return mem_initializer_id
;
15029 member
= expand_member_init (mem_initializer_id
);
15030 if (member
&& !DECL_P (member
))
15031 in_base_initializer
= 1;
15033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15035 bool expr_non_constant_p
;
15036 cp_lexer_set_source_position (parser
->lexer
);
15037 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
15038 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
15039 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
15040 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
15044 vec
<tree
, va_gc
> *vec
;
15045 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
15047 /*allow_expansion_p=*/true,
15048 /*non_constant_p=*/NULL
,
15049 /*close_paren_loc=*/NULL
,
15050 /*wrap_locations_p=*/true);
15052 return error_mark_node
;
15053 expression_list
= build_tree_list_vec (vec
);
15054 release_tree_vector (vec
);
15057 if (expression_list
== error_mark_node
)
15058 return error_mark_node
;
15059 if (!expression_list
)
15060 expression_list
= void_type_node
;
15062 in_base_initializer
= 0;
15064 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
15067 /* Parse a mem-initializer-id.
15069 mem-initializer-id:
15070 :: [opt] nested-name-specifier [opt] class-name
15071 decltype-specifier (C++11)
15074 Returns a TYPE indicating the class to be initialized for the first
15075 production (and the second in C++11). Returns an IDENTIFIER_NODE
15076 indicating the data member to be initialized for the last production. */
15079 cp_parser_mem_initializer_id (cp_parser
* parser
)
15081 bool global_scope_p
;
15082 bool nested_name_specifier_p
;
15083 bool template_p
= false;
15086 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15088 /* `typename' is not allowed in this context ([temp.res]). */
15089 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15091 error_at (token
->location
,
15092 "keyword %<typename%> not allowed in this context (a qualified "
15093 "member initializer is implicitly a type)");
15094 cp_lexer_consume_token (parser
->lexer
);
15096 /* Look for the optional `::' operator. */
15098 = (cp_parser_global_scope_opt (parser
,
15099 /*current_scope_valid_p=*/false)
15101 /* Look for the optional nested-name-specifier. The simplest way to
15106 The keyword `typename' is not permitted in a base-specifier or
15107 mem-initializer; in these contexts a qualified name that
15108 depends on a template-parameter is implicitly assumed to be a
15111 is to assume that we have seen the `typename' keyword at this
15113 nested_name_specifier_p
15114 = (cp_parser_nested_name_specifier_opt (parser
,
15115 /*typename_keyword_p=*/true,
15116 /*check_dependency_p=*/true,
15118 /*is_declaration=*/true)
15120 if (nested_name_specifier_p
)
15121 template_p
= cp_parser_optional_template_keyword (parser
);
15122 /* If there is a `::' operator or a nested-name-specifier, then we
15123 are definitely looking for a class-name. */
15124 if (global_scope_p
|| nested_name_specifier_p
)
15125 return cp_parser_class_name (parser
,
15126 /*typename_keyword_p=*/true,
15127 /*template_keyword_p=*/template_p
,
15129 /*check_dependency_p=*/true,
15130 /*class_head_p=*/false,
15131 /*is_declaration=*/true);
15132 /* Otherwise, we could also be looking for an ordinary identifier. */
15133 cp_parser_parse_tentatively (parser
);
15134 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15135 /* Try a decltype-specifier. */
15136 id
= cp_parser_decltype (parser
);
15138 /* Otherwise, try a class-name. */
15139 id
= cp_parser_class_name (parser
,
15140 /*typename_keyword_p=*/true,
15141 /*template_keyword_p=*/false,
15143 /*check_dependency_p=*/true,
15144 /*class_head_p=*/false,
15145 /*is_declaration=*/true);
15146 /* If we found one, we're done. */
15147 if (cp_parser_parse_definitely (parser
))
15149 /* Otherwise, look for an ordinary identifier. */
15150 return cp_parser_identifier (parser
);
15153 /* Overloading [gram.over] */
15155 /* Parse an operator-function-id.
15157 operator-function-id:
15160 Returns an IDENTIFIER_NODE for the operator which is a
15161 human-readable spelling of the identifier, e.g., `operator +'. */
15164 cp_parser_operator_function_id (cp_parser
* parser
)
15166 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15167 /* Look for the `operator' keyword. */
15168 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15169 return error_mark_node
;
15170 /* And then the name of the operator itself. */
15171 return cp_parser_operator (parser
, start_loc
);
15174 /* Return an identifier node for a user-defined literal operator.
15175 The suffix identifier is chained to the operator name identifier. */
15178 cp_literal_operator_id (const char* name
)
15181 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15182 + strlen (name
) + 10);
15183 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15184 identifier
= get_identifier (buffer
);
15189 /* Parse an operator.
15192 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15193 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15194 || ++ -- , ->* -> () []
15201 Returns an IDENTIFIER_NODE for the operator which is a
15202 human-readable spelling of the identifier, e.g., `operator +'. */
15205 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15207 tree id
= NULL_TREE
;
15211 /* Peek at the next token. */
15212 token
= cp_lexer_peek_token (parser
->lexer
);
15214 location_t end_loc
= token
->location
;
15216 /* Figure out which operator we have. */
15217 enum tree_code op
= ERROR_MARK
;
15218 bool assop
= false;
15219 bool consumed
= false;
15220 switch (token
->type
)
15224 /* The keyword should be either `new' or `delete'. */
15225 if (token
->keyword
== RID_NEW
)
15227 else if (token
->keyword
== RID_DELETE
)
15232 /* Consume the `new' or `delete' token. */
15233 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15235 /* Peek at the next token. */
15236 token
= cp_lexer_peek_token (parser
->lexer
);
15237 /* If it's a `[' token then this is the array variant of the
15239 if (token
->type
== CPP_OPEN_SQUARE
)
15241 /* Consume the `[' token. */
15242 cp_lexer_consume_token (parser
->lexer
);
15243 /* Look for the `]' token. */
15244 if (cp_token
*close_token
15245 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15246 end_loc
= close_token
->location
;
15247 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15266 op
= TRUNC_DIV_EXPR
;
15270 op
= TRUNC_MOD_EXPR
;
15290 op
= TRUTH_NOT_EXPR
;
15323 op
= TRUNC_DIV_EXPR
;
15328 op
= TRUNC_MOD_EXPR
;
15354 case CPP_LSHIFT_EQ
:
15359 case CPP_RSHIFT_EQ
:
15376 case CPP_GREATER_EQ
:
15381 op
= TRUTH_ANDIF_EXPR
;
15385 op
= TRUTH_ORIF_EXPR
;
15388 case CPP_PLUS_PLUS
:
15389 op
= POSTINCREMENT_EXPR
;
15392 case CPP_MINUS_MINUS
:
15393 op
= PREDECREMENT_EXPR
;
15397 op
= COMPOUND_EXPR
;
15400 case CPP_DEREF_STAR
:
15405 op
= COMPONENT_REF
;
15408 case CPP_OPEN_PAREN
:
15410 /* Consume the `('. */
15411 matching_parens parens
;
15412 parens
.consume_open (parser
);
15413 /* Look for the matching `)'. */
15414 token
= parens
.require_close (parser
);
15416 end_loc
= token
->location
;
15422 case CPP_OPEN_SQUARE
:
15423 /* Consume the `['. */
15424 cp_lexer_consume_token (parser
->lexer
);
15425 /* Look for the matching `]'. */
15426 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15428 end_loc
= token
->location
;
15433 case CPP_UTF8STRING
:
15434 case CPP_UTF8STRING_USERDEF
:
15441 case CPP_STRING_USERDEF
:
15442 case CPP_WSTRING_USERDEF
:
15443 case CPP_STRING16_USERDEF
:
15444 case CPP_STRING32_USERDEF
:
15450 if (cxx_dialect
== cxx98
)
15451 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15453 /* Consume the string. */
15454 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15455 /*wide_ok=*/true, /*lookup_udlit=*/false);
15456 if (str
== error_mark_node
)
15457 return error_mark_node
;
15458 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15460 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15461 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15462 end_loc
= str
.get_location ();
15467 /* Look for the suffix identifier. */
15468 token
= cp_lexer_peek_token (parser
->lexer
);
15469 if (token
->type
== CPP_NAME
)
15471 id
= cp_parser_identifier (parser
);
15472 end_loc
= token
->location
;
15474 else if (token
->type
== CPP_KEYWORD
)
15476 error ("unexpected keyword;"
15477 " remove space between quotes and suffix identifier");
15478 return error_mark_node
;
15482 error ("expected suffix identifier");
15483 return error_mark_node
;
15486 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15487 (TREE_TYPE (TREE_TYPE (string_tree
))));
15488 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15491 error ("expected empty string after %<operator%> keyword");
15492 return error_mark_node
;
15494 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15497 error ("invalid encoding prefix in literal operator");
15498 return error_mark_node
;
15500 if (id
!= error_mark_node
)
15502 const char *name
= IDENTIFIER_POINTER (id
);
15503 id
= cp_literal_operator_id (name
);
15505 /* Generate a location of the form:
15506 "" _suffix_identifier
15507 ^~~~~~~~~~~~~~~~~~~~~
15508 with caret == start at the start token, finish at the end of the
15509 suffix identifier. */
15510 location_t finish_loc
15511 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15512 location_t combined_loc
15513 = make_location (start_loc
, start_loc
, finish_loc
);
15514 return cp_expr (id
, combined_loc
);
15518 /* Anything else is an error. */
15522 /* If we have selected an identifier, we need to consume the
15524 if (op
!= ERROR_MARK
)
15526 id
= ovl_op_identifier (assop
, op
);
15528 cp_lexer_consume_token (parser
->lexer
);
15530 /* Otherwise, no valid operator name was present. */
15533 cp_parser_error (parser
, "expected operator");
15534 id
= error_mark_node
;
15537 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15538 return cp_expr (id
, start_loc
);
15541 /* Parse a template-declaration.
15543 template-declaration:
15544 export [opt] template < template-parameter-list > declaration
15546 If MEMBER_P is TRUE, this template-declaration occurs within a
15549 The grammar rule given by the standard isn't correct. What
15550 is really meant is:
15552 template-declaration:
15553 export [opt] template-parameter-list-seq
15554 decl-specifier-seq [opt] init-declarator [opt] ;
15555 export [opt] template-parameter-list-seq
15556 function-definition
15558 template-parameter-list-seq:
15559 template-parameter-list-seq [opt]
15560 template < template-parameter-list >
15562 Concept Extensions:
15564 template-parameter-list-seq:
15565 template < template-parameter-list > requires-clause [opt]
15568 requires logical-or-expression */
15571 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15573 /* Check for `export'. */
15574 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15576 /* Consume the `export' token. */
15577 cp_lexer_consume_token (parser
->lexer
);
15578 /* Warn that we do not support `export'. */
15579 warning (0, "keyword %<export%> not implemented, and will be ignored");
15582 cp_parser_template_declaration_after_export (parser
, member_p
);
15585 /* Parse a template-parameter-list.
15587 template-parameter-list:
15589 template-parameter-list , template-parameter
15591 Returns a TREE_LIST. Each node represents a template parameter.
15592 The nodes are connected via their TREE_CHAINs. */
15595 cp_parser_template_parameter_list (cp_parser
* parser
)
15597 tree parameter_list
= NULL_TREE
;
15599 /* Don't create wrapper nodes within a template-parameter-list,
15600 since we don't want to have different types based on the
15601 spelling location of constants and decls within them. */
15602 auto_suppress_location_wrappers sentinel
;
15604 begin_template_parm_list ();
15606 /* The loop below parses the template parms. We first need to know
15607 the total number of template parms to be able to compute proper
15608 canonical types of each dependent type. So after the loop, when
15609 we know the total number of template parms,
15610 end_template_parm_list computes the proper canonical types and
15611 fixes up the dependent types accordingly. */
15616 bool is_parameter_pack
;
15617 location_t parm_loc
;
15619 /* Parse the template-parameter. */
15620 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15621 parameter
= cp_parser_template_parameter (parser
,
15623 &is_parameter_pack
);
15624 /* Add it to the list. */
15625 if (parameter
!= error_mark_node
)
15626 parameter_list
= process_template_parm (parameter_list
,
15630 is_parameter_pack
);
15633 tree err_parm
= build_tree_list (parameter
, parameter
);
15634 parameter_list
= chainon (parameter_list
, err_parm
);
15637 /* If the next token is not a `,', we're done. */
15638 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15640 /* Otherwise, consume the `,' token. */
15641 cp_lexer_consume_token (parser
->lexer
);
15644 return end_template_parm_list (parameter_list
);
15647 /* Parse a introduction-list.
15650 introduced-parameter
15651 introduction-list , introduced-parameter
15653 introduced-parameter:
15654 ...[opt] identifier
15656 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15657 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15658 WILDCARD_DECL will also have DECL_NAME set and token location in
15659 DECL_SOURCE_LOCATION. */
15662 cp_parser_introduction_list (cp_parser
*parser
)
15664 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15668 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15670 cp_lexer_consume_token (parser
->lexer
);
15672 tree identifier
= cp_parser_identifier (parser
);
15673 if (identifier
== error_mark_node
)
15676 /* Build placeholder. */
15677 tree parm
= build_nt (WILDCARD_DECL
);
15678 DECL_SOURCE_LOCATION (parm
)
15679 = cp_lexer_peek_token (parser
->lexer
)->location
;
15680 DECL_NAME (parm
) = identifier
;
15681 WILDCARD_PACK_P (parm
) = is_pack
;
15682 vec_safe_push (introduction_vec
, parm
);
15684 /* If the next token is not a `,', we're done. */
15685 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15687 /* Otherwise, consume the `,' token. */
15688 cp_lexer_consume_token (parser
->lexer
);
15691 /* Convert the vec into a TREE_VEC. */
15692 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15695 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15696 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15698 release_tree_vector (introduction_vec
);
15699 return introduction_list
;
15702 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15703 is an abstract declarator. */
15705 static inline cp_declarator
*
15706 get_id_declarator (cp_declarator
*declarator
)
15708 cp_declarator
*d
= declarator
;
15709 while (d
&& d
->kind
!= cdk_id
)
15714 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15715 is an abstract declarator. */
15718 get_unqualified_id (cp_declarator
*declarator
)
15720 declarator
= get_id_declarator (declarator
);
15722 return declarator
->u
.id
.unqualified_name
;
15727 /* Returns true if DECL represents a constrained-parameter. */
15730 is_constrained_parameter (tree decl
)
15733 && TREE_CODE (decl
) == TYPE_DECL
15734 && CONSTRAINED_PARM_CONCEPT (decl
)
15735 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15738 /* Returns true if PARM declares a constrained-parameter. */
15741 is_constrained_parameter (cp_parameter_declarator
*parm
)
15743 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15746 /* Check that the type parameter is only a declarator-id, and that its
15747 type is not cv-qualified. */
15750 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15751 cp_parameter_declarator
*parm
)
15753 if (!parm
->declarator
)
15756 if (parm
->declarator
->kind
!= cdk_id
)
15758 cp_parser_error (parser
, "invalid constrained type parameter");
15762 /* Don't allow cv-qualified type parameters. */
15763 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15764 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15766 cp_parser_error (parser
, "cv-qualified type parameter");
15773 /* Finish parsing/processing a template type parameter and checking
15774 various restrictions. */
15777 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15779 cp_parameter_declarator
* parmdecl
)
15781 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15782 return finish_template_type_parm (class_type_node
, id
);
15784 return error_mark_node
;
15788 finish_constrained_template_template_parm (tree proto
, tree id
)
15790 /* FIXME: This should probably be copied, and we may need to adjust
15791 the template parameter depths. */
15792 tree saved_parms
= current_template_parms
;
15793 begin_template_parm_list ();
15794 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15795 end_template_parm_list ();
15797 tree parm
= finish_template_template_parm (class_type_node
, id
);
15798 current_template_parms
= saved_parms
;
15803 /* Finish parsing/processing a template template parameter by borrowing
15804 the template parameter list from the prototype parameter. */
15807 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15810 cp_parameter_declarator
*parmdecl
)
15812 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15813 return error_mark_node
;
15814 return finish_constrained_template_template_parm (proto
, id
);
15817 /* Create a new non-type template parameter from the given PARM
15821 constrained_non_type_template_parm (bool *is_non_type
,
15822 cp_parameter_declarator
*parm
)
15824 *is_non_type
= true;
15825 cp_declarator
*decl
= parm
->declarator
;
15826 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15827 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15828 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15831 /* Build a constrained template parameter based on the PARMDECL
15832 declarator. The type of PARMDECL is the constrained type, which
15833 refers to the prototype template parameter that ultimately
15834 specifies the type of the declared parameter. */
15837 finish_constrained_parameter (cp_parser
*parser
,
15838 cp_parameter_declarator
*parmdecl
,
15840 bool *is_parameter_pack
)
15842 tree decl
= parmdecl
->decl_specifiers
.type
;
15843 tree id
= get_unqualified_id (parmdecl
->declarator
);
15844 tree def
= parmdecl
->default_argument
;
15845 tree proto
= DECL_INITIAL (decl
);
15847 /* A template parameter constrained by a variadic concept shall also
15848 be declared as a template parameter pack. */
15849 bool is_variadic
= template_parameter_pack_p (proto
);
15850 if (is_variadic
&& !*is_parameter_pack
)
15851 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15853 /* Build the parameter. Return an error if the declarator was invalid. */
15855 if (TREE_CODE (proto
) == TYPE_DECL
)
15856 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15857 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15858 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15861 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15862 if (parm
== error_mark_node
)
15863 return error_mark_node
;
15865 /* Finish the parameter decl and create a node attaching the
15866 default argument and constraint. */
15867 parm
= build_tree_list (def
, parm
);
15868 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15873 /* Returns true if the parsed type actually represents the declaration
15874 of a type template-parameter. */
15877 declares_constrained_type_template_parameter (tree type
)
15879 return (is_constrained_parameter (type
)
15880 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15884 /* Returns true if the parsed type actually represents the declaration of
15885 a template template-parameter. */
15888 declares_constrained_template_template_parameter (tree type
)
15890 return (is_constrained_parameter (type
)
15891 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15894 /* Parse a default argument for a type template-parameter.
15895 Note that diagnostics are handled in cp_parser_template_parameter. */
15898 cp_parser_default_type_template_argument (cp_parser
*parser
)
15900 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15902 /* Consume the `=' token. */
15903 cp_lexer_consume_token (parser
->lexer
);
15905 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15907 /* Parse the default-argument. */
15908 push_deferring_access_checks (dk_no_deferred
);
15909 tree default_argument
= cp_parser_type_id (parser
,
15910 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15912 pop_deferring_access_checks ();
15914 if (flag_concepts
&& type_uses_auto (default_argument
))
15916 error_at (token
->location
,
15917 "invalid use of %<auto%> in default template argument");
15918 return error_mark_node
;
15921 return default_argument
;
15924 /* Parse a default argument for a template template-parameter. */
15927 cp_parser_default_template_template_argument (cp_parser
*parser
)
15929 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15933 /* Consume the `='. */
15934 cp_lexer_consume_token (parser
->lexer
);
15935 /* Parse the id-expression. */
15936 push_deferring_access_checks (dk_no_deferred
);
15937 /* save token before parsing the id-expression, for error
15939 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15940 tree default_argument
15941 = cp_parser_id_expression (parser
,
15942 /*template_keyword_p=*/false,
15943 /*check_dependency_p=*/true,
15944 /*template_p=*/&is_template
,
15945 /*declarator_p=*/false,
15946 /*optional_p=*/false);
15947 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15948 /* If the id-expression was a template-id that refers to
15949 a template-class, we already have the declaration here,
15950 so no further lookup is needed. */
15953 /* Look up the name. */
15955 = cp_parser_lookup_name (parser
, default_argument
,
15957 /*is_template=*/is_template
,
15958 /*is_namespace=*/false,
15959 /*check_dependency=*/true,
15960 /*ambiguous_decls=*/NULL
,
15962 /* See if the default argument is valid. */
15963 default_argument
= check_template_template_default_arg (default_argument
);
15964 pop_deferring_access_checks ();
15965 return default_argument
;
15968 /* Parse a template-parameter.
15970 template-parameter:
15972 parameter-declaration
15974 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15975 the parameter. The TREE_PURPOSE is the default value, if any.
15976 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15977 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15978 set to true iff this parameter is a parameter pack. */
15981 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15982 bool *is_parameter_pack
)
15985 cp_parameter_declarator
*parameter_declarator
;
15988 /* Assume it is a type parameter or a template parameter. */
15989 *is_non_type
= false;
15990 /* Assume it not a parameter pack. */
15991 *is_parameter_pack
= false;
15992 /* Peek at the next token. */
15993 token
= cp_lexer_peek_token (parser
->lexer
);
15994 /* If it is `template', we have a type-parameter. */
15995 if (token
->keyword
== RID_TEMPLATE
)
15996 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15997 /* If it is `class' or `typename' we do not know yet whether it is a
15998 type parameter or a non-type parameter. Consider:
16000 template <typename T, typename T::X X> ...
16004 template <class C, class D*> ...
16006 Here, the first parameter is a type parameter, and the second is
16007 a non-type parameter. We can tell by looking at the token after
16008 the identifier -- if it is a `,', `=', or `>' then we have a type
16010 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
16012 /* Peek at the token after `class' or `typename'. */
16013 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16014 /* If it's an ellipsis, we have a template type parameter
16016 if (token
->type
== CPP_ELLIPSIS
)
16017 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16018 /* If it's an identifier, skip it. */
16019 if (token
->type
== CPP_NAME
)
16020 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
16021 /* Now, see if the token looks like the end of a template
16023 if (token
->type
== CPP_COMMA
16024 || token
->type
== CPP_EQ
16025 || token
->type
== CPP_GREATER
)
16026 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16029 /* Otherwise, it is a non-type parameter or a constrained parameter.
16033 When parsing a default template-argument for a non-type
16034 template-parameter, the first non-nested `>' is taken as the end
16035 of the template parameter-list rather than a greater-than
16037 parameter_declarator
16038 = cp_parser_parameter_declaration (parser
,
16039 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16040 /*template_parm_p=*/true,
16041 /*parenthesized_p=*/NULL
);
16043 if (!parameter_declarator
)
16044 return error_mark_node
;
16046 /* If the parameter declaration is marked as a parameter pack, set
16047 *IS_PARAMETER_PACK to notify the caller. */
16048 if (parameter_declarator
->template_parameter_pack_p
)
16049 *is_parameter_pack
= true;
16051 if (parameter_declarator
->default_argument
)
16053 /* Can happen in some cases of erroneous input (c++/34892). */
16054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16055 /* Consume the `...' for better error recovery. */
16056 cp_lexer_consume_token (parser
->lexer
);
16059 // The parameter may have been constrained.
16060 if (is_constrained_parameter (parameter_declarator
))
16061 return finish_constrained_parameter (parser
,
16062 parameter_declarator
,
16064 is_parameter_pack
);
16066 // Now we're sure that the parameter is a non-type parameter.
16067 *is_non_type
= true;
16069 parm
= grokdeclarator (parameter_declarator
->declarator
,
16070 ¶meter_declarator
->decl_specifiers
,
16071 TPARM
, /*initialized=*/0,
16072 /*attrlist=*/NULL
);
16073 if (parm
== error_mark_node
)
16074 return error_mark_node
;
16076 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16079 /* Parse a type-parameter.
16082 class identifier [opt]
16083 class identifier [opt] = type-id
16084 typename identifier [opt]
16085 typename identifier [opt] = type-id
16086 template < template-parameter-list > class identifier [opt]
16087 template < template-parameter-list > class identifier [opt]
16090 GNU Extension (variadic templates):
16093 class ... identifier [opt]
16094 typename ... identifier [opt]
16096 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16097 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16098 the declaration of the parameter.
16100 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16103 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16108 /* Look for a keyword to tell us what kind of parameter this is. */
16109 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16111 return error_mark_node
;
16113 switch (token
->keyword
)
16119 tree default_argument
;
16121 /* If the next token is an ellipsis, we have a template
16123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16125 /* Consume the `...' token. */
16126 cp_lexer_consume_token (parser
->lexer
);
16127 maybe_warn_variadic_templates ();
16129 *is_parameter_pack
= true;
16132 /* If the next token is an identifier, then it names the
16134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16135 identifier
= cp_parser_identifier (parser
);
16137 identifier
= NULL_TREE
;
16139 /* Create the parameter. */
16140 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16142 /* If the next token is an `=', we have a default argument. */
16143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16146 = cp_parser_default_type_template_argument (parser
);
16148 /* Template parameter packs cannot have default
16150 if (*is_parameter_pack
)
16153 error_at (token
->location
,
16154 "template parameter pack %qD cannot have a "
16155 "default argument", identifier
);
16157 error_at (token
->location
,
16158 "template parameter packs cannot have "
16159 "default arguments");
16160 default_argument
= NULL_TREE
;
16162 else if (check_for_bare_parameter_packs (default_argument
))
16163 default_argument
= error_mark_node
;
16166 default_argument
= NULL_TREE
;
16168 /* Create the combined representation of the parameter and the
16169 default argument. */
16170 parameter
= build_tree_list (default_argument
, parameter
);
16177 tree default_argument
;
16179 /* Look for the `<'. */
16180 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16181 /* Parse the template-parameter-list. */
16182 cp_parser_template_parameter_list (parser
);
16183 /* Look for the `>'. */
16184 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16186 // If template requirements are present, parse them.
16189 tree reqs
= get_shorthand_constraints (current_template_parms
);
16190 if (tree r
= cp_parser_requires_clause_opt (parser
))
16191 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16192 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16195 /* Look for the `class' or 'typename' keywords. */
16196 cp_parser_type_parameter_key (parser
);
16197 /* If the next token is an ellipsis, we have a template
16199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16201 /* Consume the `...' token. */
16202 cp_lexer_consume_token (parser
->lexer
);
16203 maybe_warn_variadic_templates ();
16205 *is_parameter_pack
= true;
16207 /* If the next token is an `=', then there is a
16208 default-argument. If the next token is a `>', we are at
16209 the end of the parameter-list. If the next token is a `,',
16210 then we are at the end of this parameter. */
16211 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16212 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16213 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16215 identifier
= cp_parser_identifier (parser
);
16216 /* Treat invalid names as if the parameter were nameless. */
16217 if (identifier
== error_mark_node
)
16218 identifier
= NULL_TREE
;
16221 identifier
= NULL_TREE
;
16223 /* Create the template parameter. */
16224 parameter
= finish_template_template_parm (class_type_node
,
16227 /* If the next token is an `=', then there is a
16228 default-argument. */
16229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16232 = cp_parser_default_template_template_argument (parser
);
16234 /* Template parameter packs cannot have default
16236 if (*is_parameter_pack
)
16239 error_at (token
->location
,
16240 "template parameter pack %qD cannot "
16241 "have a default argument",
16244 error_at (token
->location
, "template parameter packs cannot "
16245 "have default arguments");
16246 default_argument
= NULL_TREE
;
16250 default_argument
= NULL_TREE
;
16252 /* Create the combined representation of the parameter and the
16253 default argument. */
16254 parameter
= build_tree_list (default_argument
, parameter
);
16259 gcc_unreachable ();
16266 /* Parse a template-id.
16269 template-name < template-argument-list [opt] >
16271 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16272 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16273 returned. Otherwise, if the template-name names a function, or set
16274 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16275 names a class, returns a TYPE_DECL for the specialization.
16277 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16278 uninstantiated templates. */
16281 cp_parser_template_id (cp_parser
*parser
,
16282 bool template_keyword_p
,
16283 bool check_dependency_p
,
16284 enum tag_types tag_type
,
16285 bool is_declaration
)
16290 cp_token_position start_of_id
= 0;
16291 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16292 bool is_identifier
;
16294 /* If the next token corresponds to a template-id, there is no need
16296 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16297 if (token
->type
== CPP_TEMPLATE_ID
)
16299 cp_lexer_consume_token (parser
->lexer
);
16300 return saved_checks_value (token
->u
.tree_check_value
);
16303 /* Avoid performing name lookup if there is no possibility of
16304 finding a template-id. */
16305 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16306 || (token
->type
== CPP_NAME
16307 && !cp_parser_nth_token_starts_template_argument_list_p
16310 cp_parser_error (parser
, "expected template-id");
16311 return error_mark_node
;
16314 /* Remember where the template-id starts. */
16315 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16316 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16318 push_deferring_access_checks (dk_deferred
);
16320 /* Parse the template-name. */
16321 is_identifier
= false;
16322 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16323 check_dependency_p
,
16328 /* Push any access checks inside the firewall we're about to create. */
16329 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16330 pop_deferring_access_checks ();
16331 if (templ
== error_mark_node
|| is_identifier
)
16334 /* Since we're going to preserve any side-effects from this parse, set up a
16335 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16336 in the template arguments. */
16337 tentative_firewall
firewall (parser
);
16338 reopen_deferring_access_checks (checks
);
16340 /* If we find the sequence `[:' after a template-name, it's probably
16341 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16342 parse correctly the argument list. */
16343 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16344 == CPP_OPEN_SQUARE
)
16345 && next_token
->flags
& DIGRAPH
16346 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16348 && !(next_token_2
->flags
& PREV_WHITE
))
16350 cp_parser_parse_tentatively (parser
);
16351 /* Change `:' into `::'. */
16352 next_token_2
->type
= CPP_SCOPE
;
16353 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16355 cp_lexer_consume_token (parser
->lexer
);
16357 /* Parse the arguments. */
16358 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16359 if (!cp_parser_parse_definitely (parser
))
16361 /* If we couldn't parse an argument list, then we revert our changes
16362 and return simply an error. Maybe this is not a template-id
16364 next_token_2
->type
= CPP_COLON
;
16365 cp_parser_error (parser
, "expected %<<%>");
16366 pop_deferring_access_checks ();
16367 return error_mark_node
;
16369 /* Otherwise, emit an error about the invalid digraph, but continue
16370 parsing because we got our argument list. */
16371 if (permerror (next_token
->location
,
16372 "%<<::%> cannot begin a template-argument list"))
16374 static bool hint
= false;
16375 inform (next_token
->location
,
16376 "%<<:%> is an alternate spelling for %<[%>."
16377 " Insert whitespace between %<<%> and %<::%>");
16378 if (!hint
&& !flag_permissive
)
16380 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16381 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16382 "accept your code)");
16389 /* Look for the `<' that starts the template-argument-list. */
16390 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16392 pop_deferring_access_checks ();
16393 return error_mark_node
;
16395 /* Parse the arguments. */
16396 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16398 if ((cxx_dialect
> cxx17
)
16399 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16400 && !template_keyword_p
16401 && (cp_parser_error_occurred (parser
)
16402 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16404 /* This didn't go well. */
16405 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16407 /* C++2A says that "function-name < a;" is now ill-formed. */
16408 if (cp_parser_error_occurred (parser
))
16410 error_at (token
->location
, "invalid template-argument-list");
16411 inform (token
->location
, "function name as the left hand "
16412 "operand of %<<%> is ill-formed in C++2a; wrap the "
16413 "function name in %<()%>");
16416 /* We expect "f<targs>" to be followed by "(args)". */
16417 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16418 "expected %<(%> after template-argument-list");
16420 /* Purge all subsequent tokens. */
16421 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16424 cp_parser_simulate_error (parser
);
16425 pop_deferring_access_checks ();
16426 return error_mark_node
;
16430 /* Set the location to be of the form:
16431 template-name < template-argument-list [opt] >
16432 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16433 with caret == start at the start of the template-name,
16434 ranging until the closing '>'. */
16435 location_t finish_loc
16436 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16437 location_t combined_loc
16438 = make_location (token
->location
, token
->location
, finish_loc
);
16440 /* Check for concepts autos where they don't belong. We could
16441 identify types in some cases of idnetifier TEMPL, looking ahead
16442 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16443 types. We reject them in functions, but if what we have is an
16444 identifier, even with none_type we can't conclude it's NOT a
16445 type, we have to wait for template substitution. */
16446 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16447 template_id
= error_mark_node
;
16448 /* Build a representation of the specialization. */
16449 else if (identifier_p (templ
))
16450 template_id
= build_min_nt_loc (combined_loc
,
16453 else if (DECL_TYPE_TEMPLATE_P (templ
)
16454 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16456 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16457 template (rather than some instantiation thereof) only if
16458 is not nested within some other construct. For example, in
16459 "template <typename T> void f(T) { A<T>::", A<T> is just an
16460 instantiation of A. */
16461 bool entering_scope
16462 = (template_parm_scope_p ()
16463 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16465 = finish_template_type (templ
, arguments
, entering_scope
);
16467 /* A template-like identifier may be a partial concept id. */
16468 else if (flag_concepts
16469 && (template_id
= (cp_parser_maybe_partial_concept_id
16470 (parser
, templ
, arguments
))))
16471 return template_id
;
16472 else if (variable_template_p (templ
))
16474 template_id
= lookup_template_variable (templ
, arguments
);
16475 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16476 SET_EXPR_LOCATION (template_id
, combined_loc
);
16480 /* If it's not a class-template or a template-template, it should be
16481 a function-template. */
16482 gcc_assert (OVL_P (templ
) || BASELINK_P (templ
));
16484 template_id
= lookup_template_function (templ
, arguments
);
16485 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16486 SET_EXPR_LOCATION (template_id
, combined_loc
);
16489 /* If parsing tentatively, replace the sequence of tokens that makes
16490 up the template-id with a CPP_TEMPLATE_ID token. That way,
16491 should we re-parse the token stream, we will not have to repeat
16492 the effort required to do the parse, nor will we issue duplicate
16493 error messages about problems during instantiation of the
16496 /* Don't do this if we had a parse error in a declarator; re-parsing
16497 might succeed if a name changes meaning (60361). */
16498 && !(cp_parser_error_occurred (parser
)
16499 && cp_parser_parsing_tentatively (parser
)
16500 && parser
->in_declarator_p
))
16502 /* Reset the contents of the START_OF_ID token. */
16503 token
->type
= CPP_TEMPLATE_ID
;
16504 token
->location
= combined_loc
;
16506 /* Retrieve any deferred checks. Do not pop this access checks yet
16507 so the memory will not be reclaimed during token replacing below. */
16508 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16509 token
->u
.tree_check_value
->value
= template_id
;
16510 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16511 token
->keyword
= RID_MAX
;
16513 /* Purge all subsequent tokens. */
16514 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16516 /* ??? Can we actually assume that, if template_id ==
16517 error_mark_node, we will have issued a diagnostic to the
16518 user, as opposed to simply marking the tentative parse as
16520 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16521 error_at (token
->location
, "parse error in template argument list");
16524 pop_to_parent_deferring_access_checks ();
16525 return template_id
;
16528 /* Parse a template-name.
16533 The standard should actually say:
16537 operator-function-id
16539 A defect report has been filed about this issue.
16541 A conversion-function-id cannot be a template name because they cannot
16542 be part of a template-id. In fact, looking at this code:
16544 a.operator K<int>()
16546 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16547 It is impossible to call a templated conversion-function-id with an
16548 explicit argument list, since the only allowed template parameter is
16549 the type to which it is converting.
16551 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16552 `template' keyword, in a construction like:
16556 In that case `f' is taken to be a template-name, even though there
16557 is no way of knowing for sure.
16559 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16560 name refers to a set of overloaded functions, at least one of which
16561 is a template, or an IDENTIFIER_NODE with the name of the template,
16562 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16563 names are looked up inside uninstantiated templates. */
16566 cp_parser_template_name (cp_parser
* parser
,
16567 bool template_keyword_p
,
16568 bool check_dependency_p
,
16569 bool is_declaration
,
16570 enum tag_types tag_type
,
16571 bool *is_identifier
)
16575 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16577 /* If the next token is `operator', then we have either an
16578 operator-function-id or a conversion-function-id. */
16579 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16581 /* We don't know whether we're looking at an
16582 operator-function-id or a conversion-function-id. */
16583 cp_parser_parse_tentatively (parser
);
16584 /* Try an operator-function-id. */
16585 identifier
= cp_parser_operator_function_id (parser
);
16586 /* If that didn't work, try a conversion-function-id. */
16587 if (!cp_parser_parse_definitely (parser
))
16589 cp_parser_error (parser
, "expected template-name");
16590 return error_mark_node
;
16593 /* Look for the identifier. */
16595 identifier
= cp_parser_identifier (parser
);
16597 /* If we didn't find an identifier, we don't have a template-id. */
16598 if (identifier
== error_mark_node
)
16599 return error_mark_node
;
16601 /* If the name immediately followed the `template' keyword, then it
16602 is a template-name. However, if the next token is not `<', then
16603 we do not treat it as a template-name, since it is not being used
16604 as part of a template-id. This enables us to handle constructs
16607 template <typename T> struct S { S(); };
16608 template <typename T> S<T>::S();
16610 correctly. We would treat `S' as a template -- if it were `S<T>'
16611 -- but we do not if there is no `<'. */
16613 if (processing_template_decl
16614 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16616 /* In a declaration, in a dependent context, we pretend that the
16617 "template" keyword was present in order to improve error
16618 recovery. For example, given:
16620 template <typename T> void f(T::X<int>);
16622 we want to treat "X<int>" as a template-id. */
16624 && !template_keyword_p
16625 && parser
->scope
&& TYPE_P (parser
->scope
)
16626 && check_dependency_p
16627 && dependent_scope_p (parser
->scope
)
16628 /* Do not do this for dtors (or ctors), since they never
16629 need the template keyword before their name. */
16630 && !constructor_name_p (identifier
, parser
->scope
))
16632 cp_token_position start
= 0;
16634 /* Explain what went wrong. */
16635 error_at (token
->location
, "non-template %qD used as template",
16637 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16638 parser
->scope
, identifier
);
16639 /* If parsing tentatively, find the location of the "<" token. */
16640 if (cp_parser_simulate_error (parser
))
16641 start
= cp_lexer_token_position (parser
->lexer
, true);
16642 /* Parse the template arguments so that we can issue error
16643 messages about them. */
16644 cp_lexer_consume_token (parser
->lexer
);
16645 cp_parser_enclosed_template_argument_list (parser
);
16646 /* Skip tokens until we find a good place from which to
16647 continue parsing. */
16648 cp_parser_skip_to_closing_parenthesis (parser
,
16649 /*recovering=*/true,
16651 /*consume_paren=*/false);
16652 /* If parsing tentatively, permanently remove the
16653 template argument list. That will prevent duplicate
16654 error messages from being issued about the missing
16655 "template" keyword. */
16657 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16659 *is_identifier
= true;
16660 parser
->context
->object_type
= NULL_TREE
;
16664 /* If the "template" keyword is present, then there is generally
16665 no point in doing name-lookup, so we just return IDENTIFIER.
16666 But, if the qualifying scope is non-dependent then we can
16667 (and must) do name-lookup normally. */
16668 if (template_keyword_p
)
16670 tree scope
= (parser
->scope
? parser
->scope
16671 : parser
->context
->object_type
);
16672 if (scope
&& TYPE_P (scope
)
16673 && (!CLASS_TYPE_P (scope
)
16674 || (check_dependency_p
&& dependent_type_p (scope
))))
16676 /* We're optimizing away the call to cp_parser_lookup_name, but
16677 we still need to do this. */
16678 parser
->context
->object_type
= NULL_TREE
;
16684 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16685 const bool scoped_p
= ((parser
->scope
? parser
->scope
16686 : parser
->context
->object_type
) != NULL_TREE
);
16688 /* Look up the name. */
16689 decl
= cp_parser_lookup_name (parser
, identifier
,
16691 /*is_template=*/true,
16692 /*is_namespace=*/false,
16693 check_dependency_p
,
16694 /*ambiguous_decls=*/NULL
,
16697 decl
= strip_using_decl (decl
);
16699 /* If DECL is a template, then the name was a template-name. */
16700 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16702 if (TREE_DEPRECATED (decl
)
16703 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16704 warn_deprecated_use (decl
, NULL_TREE
);
16708 /* The standard does not explicitly indicate whether a name that
16709 names a set of overloaded declarations, some of which are
16710 templates, is a template-name. However, such a name should
16711 be a template-name; otherwise, there is no way to form a
16712 template-id for the overloaded templates. */
16713 bool found
= false;
16715 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16716 !found
&& iter
; ++iter
)
16717 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16721 && (cxx_dialect
> cxx17
)
16723 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
16724 && tag_type
== none_type
)
16726 /* [temp.names] says "A name is also considered to refer to a template
16727 if it is an unqualified-id followed by a < and name lookup finds
16728 either one or more functions or finds nothing." */
16730 /* The "more functions" case. Just use the OVERLOAD as normally.
16731 We don't use is_overloaded_fn here to avoid considering
16733 if (TREE_CODE (decl
) == OVERLOAD
16734 /* Name lookup found one function. */
16735 || TREE_CODE (decl
) == FUNCTION_DECL
)
16737 /* Name lookup found nothing. */
16738 else if (decl
== error_mark_node
)
16744 /* The name does not name a template. */
16745 cp_parser_error (parser
, "expected template-name");
16746 return error_mark_node
;
16753 /* Parse a template-argument-list.
16755 template-argument-list:
16756 template-argument ... [opt]
16757 template-argument-list , template-argument ... [opt]
16759 Returns a TREE_VEC containing the arguments. */
16762 cp_parser_template_argument_list (cp_parser
* parser
)
16764 tree fixed_args
[10];
16765 unsigned n_args
= 0;
16766 unsigned alloced
= 10;
16767 tree
*arg_ary
= fixed_args
;
16769 bool saved_in_template_argument_list_p
;
16771 bool saved_non_ice_p
;
16773 /* Don't create location wrapper nodes within a template-argument-list. */
16774 auto_suppress_location_wrappers sentinel
;
16776 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16777 parser
->in_template_argument_list_p
= true;
16778 /* Even if the template-id appears in an integral
16779 constant-expression, the contents of the argument list do
16781 saved_ice_p
= parser
->integral_constant_expression_p
;
16782 parser
->integral_constant_expression_p
= false;
16783 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16784 parser
->non_integral_constant_expression_p
= false;
16786 /* Parse the arguments. */
16792 /* Consume the comma. */
16793 cp_lexer_consume_token (parser
->lexer
);
16795 /* Parse the template-argument. */
16796 argument
= cp_parser_template_argument (parser
);
16798 /* If the next token is an ellipsis, we're expanding a template
16800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16802 if (argument
== error_mark_node
)
16804 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16805 error_at (token
->location
,
16806 "expected parameter pack before %<...%>");
16808 /* Consume the `...' token. */
16809 cp_lexer_consume_token (parser
->lexer
);
16811 /* Make the argument into a TYPE_PACK_EXPANSION or
16812 EXPR_PACK_EXPANSION. */
16813 argument
= make_pack_expansion (argument
);
16816 if (n_args
== alloced
)
16820 if (arg_ary
== fixed_args
)
16822 arg_ary
= XNEWVEC (tree
, alloced
);
16823 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16826 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16828 arg_ary
[n_args
++] = argument
;
16830 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16832 vec
= make_tree_vec (n_args
);
16835 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16837 if (arg_ary
!= fixed_args
)
16839 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16840 parser
->integral_constant_expression_p
= saved_ice_p
;
16841 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16843 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16847 /* Parse a template-argument.
16850 assignment-expression
16854 The representation is that of an assignment-expression, type-id, or
16855 id-expression -- except that the qualified id-expression is
16856 evaluated, so that the value returned is either a DECL or an
16859 Although the standard says "assignment-expression", it forbids
16860 throw-expressions or assignments in the template argument.
16861 Therefore, we use "conditional-expression" instead. */
16864 cp_parser_template_argument (cp_parser
* parser
)
16869 bool maybe_type_id
= false;
16870 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16871 location_t loc
= 0;
16874 /* There's really no way to know what we're looking at, so we just
16875 try each alternative in order.
16879 In a template-argument, an ambiguity between a type-id and an
16880 expression is resolved to a type-id, regardless of the form of
16881 the corresponding template-parameter.
16883 Therefore, we try a type-id first. */
16884 cp_parser_parse_tentatively (parser
);
16885 argument
= cp_parser_template_type_arg (parser
);
16886 /* If there was no error parsing the type-id but the next token is a
16887 '>>', our behavior depends on which dialect of C++ we're
16888 parsing. In C++98, we probably found a typo for '> >'. But there
16889 are type-id which are also valid expressions. For instance:
16891 struct X { int operator >> (int); };
16892 template <int V> struct Foo {};
16895 Here 'X()' is a valid type-id of a function type, but the user just
16896 wanted to write the expression "X() >> 5". Thus, we remember that we
16897 found a valid type-id, but we still try to parse the argument as an
16898 expression to see what happens.
16900 In C++0x, the '>>' will be considered two separate '>'
16902 if (!cp_parser_error_occurred (parser
)
16903 && cxx_dialect
== cxx98
16904 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16906 maybe_type_id
= true;
16907 cp_parser_abort_tentative_parse (parser
);
16911 /* If the next token isn't a `,' or a `>', then this argument wasn't
16912 really finished. This means that the argument is not a valid
16914 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16915 cp_parser_error (parser
, "expected template-argument");
16916 /* If that worked, we're done. */
16917 if (cp_parser_parse_definitely (parser
))
16920 /* We're still not sure what the argument will be. */
16921 cp_parser_parse_tentatively (parser
);
16922 /* Try a template. */
16923 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16924 argument
= cp_parser_id_expression (parser
,
16925 /*template_keyword_p=*/false,
16926 /*check_dependency_p=*/true,
16928 /*declarator_p=*/false,
16929 /*optional_p=*/false);
16930 /* If the next token isn't a `,' or a `>', then this argument wasn't
16931 really finished. */
16932 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16933 cp_parser_error (parser
, "expected template-argument");
16934 if (!cp_parser_error_occurred (parser
))
16936 /* Figure out what is being referred to. If the id-expression
16937 was for a class template specialization, then we will have a
16938 TYPE_DECL at this point. There is no need to do name lookup
16939 at this point in that case. */
16940 if (TREE_CODE (argument
) != TYPE_DECL
)
16941 argument
= cp_parser_lookup_name (parser
, argument
,
16943 /*is_template=*/template_p
,
16944 /*is_namespace=*/false,
16945 /*check_dependency=*/true,
16946 /*ambiguous_decls=*/NULL
,
16947 argument_start_token
->location
);
16948 /* Handle a constrained-type-specifier for a non-type template
16950 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16952 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16953 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16954 cp_parser_error (parser
, "expected template-name");
16956 if (cp_parser_parse_definitely (parser
))
16958 if (TREE_DEPRECATED (argument
))
16959 warn_deprecated_use (argument
, NULL_TREE
);
16962 /* It must be a non-type argument. In C++17 any constant-expression is
16964 if (cxx_dialect
> cxx14
)
16967 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16969 -- an integral constant-expression of integral or enumeration
16972 -- the name of a non-type template-parameter; or
16974 -- the name of an object or function with external linkage...
16976 -- the address of an object or function with external linkage...
16978 -- a pointer to member... */
16979 /* Look for a non-type template parameter. */
16980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16982 cp_parser_parse_tentatively (parser
);
16983 argument
= cp_parser_primary_expression (parser
,
16984 /*address_p=*/false,
16986 /*template_arg_p=*/true,
16988 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16989 || !cp_parser_next_token_ends_template_argument_p (parser
))
16990 cp_parser_simulate_error (parser
);
16991 if (cp_parser_parse_definitely (parser
))
16995 /* If the next token is "&", the argument must be the address of an
16996 object or function with external linkage. */
16997 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
17000 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17001 cp_lexer_consume_token (parser
->lexer
);
17003 /* See if we might have an id-expression. */
17004 token
= cp_lexer_peek_token (parser
->lexer
);
17005 if (token
->type
== CPP_NAME
17006 || token
->keyword
== RID_OPERATOR
17007 || token
->type
== CPP_SCOPE
17008 || token
->type
== CPP_TEMPLATE_ID
17009 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
17011 cp_parser_parse_tentatively (parser
);
17012 argument
= cp_parser_primary_expression (parser
,
17015 /*template_arg_p=*/true,
17017 if (cp_parser_error_occurred (parser
)
17018 || !cp_parser_next_token_ends_template_argument_p (parser
))
17019 cp_parser_abort_tentative_parse (parser
);
17024 if (INDIRECT_REF_P (argument
))
17026 /* Strip the dereference temporarily. */
17027 gcc_assert (REFERENCE_REF_P (argument
));
17028 argument
= TREE_OPERAND (argument
, 0);
17031 /* If we're in a template, we represent a qualified-id referring
17032 to a static data member as a SCOPE_REF even if the scope isn't
17033 dependent so that we can check access control later. */
17035 if (TREE_CODE (probe
) == SCOPE_REF
)
17036 probe
= TREE_OPERAND (probe
, 1);
17039 /* A variable without external linkage might still be a
17040 valid constant-expression, so no error is issued here
17041 if the external-linkage check fails. */
17042 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
17043 cp_parser_simulate_error (parser
);
17045 else if (is_overloaded_fn (argument
))
17046 /* All overloaded functions are allowed; if the external
17047 linkage test does not pass, an error will be issued
17051 && (TREE_CODE (argument
) == OFFSET_REF
17052 || TREE_CODE (argument
) == SCOPE_REF
))
17053 /* A pointer-to-member. */
17055 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
17058 cp_parser_simulate_error (parser
);
17060 if (cp_parser_parse_definitely (parser
))
17063 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17064 tf_warning_or_error
);
17066 argument
= convert_from_reference (argument
);
17071 /* If the argument started with "&", there are no other valid
17072 alternatives at this point. */
17075 cp_parser_error (parser
, "invalid non-type template argument");
17076 return error_mark_node
;
17080 /* If the argument wasn't successfully parsed as a type-id followed
17081 by '>>', the argument can only be a constant expression now.
17082 Otherwise, we try parsing the constant-expression tentatively,
17083 because the argument could really be a type-id. */
17085 cp_parser_parse_tentatively (parser
);
17087 if (cxx_dialect
<= cxx14
)
17088 argument
= cp_parser_constant_expression (parser
);
17091 /* In C++20, we can encounter a braced-init-list. */
17092 if (cxx_dialect
>= cxx2a
17093 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17095 bool expr_non_constant_p
;
17096 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17099 /* With C++17 generalized non-type template arguments we need to handle
17100 lvalue constant expressions, too. */
17101 argument
= cp_parser_assignment_expression (parser
);
17102 require_potential_constant_expression (argument
);
17105 if (!maybe_type_id
)
17107 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17108 cp_parser_error (parser
, "expected template-argument");
17109 if (cp_parser_parse_definitely (parser
))
17111 /* We did our best to parse the argument as a non type-id, but that
17112 was the only alternative that matched (albeit with a '>' after
17113 it). We can assume it's just a typo from the user, and a
17114 diagnostic will then be issued. */
17115 return cp_parser_template_type_arg (parser
);
17118 /* Parse an explicit-instantiation.
17120 explicit-instantiation:
17121 template declaration
17123 Although the standard says `declaration', what it really means is:
17125 explicit-instantiation:
17126 template decl-specifier-seq [opt] declarator [opt] ;
17128 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17129 supposed to be allowed. A defect report has been filed about this
17134 explicit-instantiation:
17135 storage-class-specifier template
17136 decl-specifier-seq [opt] declarator [opt] ;
17137 function-specifier template
17138 decl-specifier-seq [opt] declarator [opt] ; */
17141 cp_parser_explicit_instantiation (cp_parser
* parser
)
17143 int declares_class_or_enum
;
17144 cp_decl_specifier_seq decl_specifiers
;
17145 tree extension_specifier
= NULL_TREE
;
17147 timevar_push (TV_TEMPLATE_INST
);
17149 /* Look for an (optional) storage-class-specifier or
17150 function-specifier. */
17151 if (cp_parser_allow_gnu_extensions_p (parser
))
17153 extension_specifier
17154 = cp_parser_storage_class_specifier_opt (parser
);
17155 if (!extension_specifier
)
17156 extension_specifier
17157 = cp_parser_function_specifier_opt (parser
,
17158 /*decl_specs=*/NULL
);
17161 /* Look for the `template' keyword. */
17162 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17163 /* Let the front end know that we are processing an explicit
17165 begin_explicit_instantiation ();
17166 /* [temp.explicit] says that we are supposed to ignore access
17167 control while processing explicit instantiation directives. */
17168 push_deferring_access_checks (dk_no_check
);
17169 /* Parse a decl-specifier-seq. */
17170 cp_parser_decl_specifier_seq (parser
,
17171 CP_PARSER_FLAGS_OPTIONAL
,
17173 &declares_class_or_enum
);
17174 /* If there was exactly one decl-specifier, and it declared a class,
17175 and there's no declarator, then we have an explicit type
17177 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17181 type
= check_tag_decl (&decl_specifiers
,
17182 /*explicit_type_instantiation_p=*/true);
17183 /* Turn access control back on for names used during
17184 template instantiation. */
17185 pop_deferring_access_checks ();
17187 do_type_instantiation (type
, extension_specifier
,
17188 /*complain=*/tf_error
);
17192 cp_declarator
*declarator
;
17195 /* Parse the declarator. */
17197 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17198 CP_PARSER_FLAGS_NONE
,
17199 /*ctor_dtor_or_conv_p=*/NULL
,
17200 /*parenthesized_p=*/NULL
,
17201 /*member_p=*/false,
17202 /*friend_p=*/false,
17203 /*static_p=*/false);
17204 if (declares_class_or_enum
& 2)
17205 cp_parser_check_for_definition_in_return_type (declarator
,
17206 decl_specifiers
.type
,
17207 decl_specifiers
.locations
[ds_type_spec
]);
17208 if (declarator
!= cp_error_declarator
)
17210 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17211 permerror (decl_specifiers
.locations
[ds_inline
],
17212 "explicit instantiation shall not use"
17213 " %<inline%> specifier");
17214 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17215 permerror (decl_specifiers
.locations
[ds_constexpr
],
17216 "explicit instantiation shall not use"
17217 " %<constexpr%> specifier");
17219 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17220 NORMAL
, 0, &decl_specifiers
.attributes
);
17221 /* Turn access control back on for names used during
17222 template instantiation. */
17223 pop_deferring_access_checks ();
17224 /* Do the explicit instantiation. */
17225 do_decl_instantiation (decl
, extension_specifier
);
17229 pop_deferring_access_checks ();
17230 /* Skip the body of the explicit instantiation. */
17231 cp_parser_skip_to_end_of_statement (parser
);
17234 /* We're done with the instantiation. */
17235 end_explicit_instantiation ();
17237 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17239 timevar_pop (TV_TEMPLATE_INST
);
17242 /* Parse an explicit-specialization.
17244 explicit-specialization:
17245 template < > declaration
17247 Although the standard says `declaration', what it really means is:
17249 explicit-specialization:
17250 template <> decl-specifier [opt] init-declarator [opt] ;
17251 template <> function-definition
17252 template <> explicit-specialization
17253 template <> template-declaration */
17256 cp_parser_explicit_specialization (cp_parser
* parser
)
17258 bool need_lang_pop
;
17259 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17261 /* Look for the `template' keyword. */
17262 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17263 /* Look for the `<'. */
17264 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17265 /* Look for the `>'. */
17266 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17267 /* We have processed another parameter list. */
17268 ++parser
->num_template_parameter_lists
;
17271 A template ... explicit specialization ... shall not have C
17273 if (current_lang_name
== lang_name_c
)
17275 error_at (token
->location
, "template specialization with C linkage");
17276 maybe_show_extern_c_location ();
17277 /* Give it C++ linkage to avoid confusing other parts of the
17279 push_lang_context (lang_name_cplusplus
);
17280 need_lang_pop
= true;
17283 need_lang_pop
= false;
17284 /* Let the front end know that we are beginning a specialization. */
17285 if (!begin_specialization ())
17287 end_specialization ();
17291 /* If the next keyword is `template', we need to figure out whether
17292 or not we're looking a template-declaration. */
17293 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17295 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17296 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17297 cp_parser_template_declaration_after_export (parser
,
17298 /*member_p=*/false);
17300 cp_parser_explicit_specialization (parser
);
17303 /* Parse the dependent declaration. */
17304 cp_parser_single_declaration (parser
,
17306 /*member_p=*/false,
17307 /*explicit_specialization_p=*/true,
17308 /*friend_p=*/NULL
);
17309 /* We're done with the specialization. */
17310 end_specialization ();
17311 /* For the erroneous case of a template with C linkage, we pushed an
17312 implicit C++ linkage scope; exit that scope now. */
17314 pop_lang_context ();
17315 /* We're done with this parameter list. */
17316 --parser
->num_template_parameter_lists
;
17319 /* Parse a type-specifier.
17322 simple-type-specifier
17325 elaborated-type-specifier
17333 Returns a representation of the type-specifier. For a
17334 class-specifier, enum-specifier, or elaborated-type-specifier, a
17335 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17337 The parser flags FLAGS is used to control type-specifier parsing.
17339 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17340 in a decl-specifier-seq.
17342 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17343 class-specifier, enum-specifier, or elaborated-type-specifier, then
17344 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17345 if a type is declared; 2 if it is defined. Otherwise, it is set to
17348 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17349 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17350 is set to FALSE. */
17353 cp_parser_type_specifier (cp_parser
* parser
,
17354 cp_parser_flags flags
,
17355 cp_decl_specifier_seq
*decl_specs
,
17356 bool is_declaration
,
17357 int* declares_class_or_enum
,
17358 bool* is_cv_qualifier
)
17360 tree type_spec
= NULL_TREE
;
17363 cp_decl_spec ds
= ds_last
;
17365 /* Assume this type-specifier does not declare a new type. */
17366 if (declares_class_or_enum
)
17367 *declares_class_or_enum
= 0;
17368 /* And that it does not specify a cv-qualifier. */
17369 if (is_cv_qualifier
)
17370 *is_cv_qualifier
= false;
17371 /* Peek at the next token. */
17372 token
= cp_lexer_peek_token (parser
->lexer
);
17374 /* If we're looking at a keyword, we can use that to guide the
17375 production we choose. */
17376 keyword
= token
->keyword
;
17380 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17381 goto elaborated_type_specifier
;
17383 /* Look for the enum-specifier. */
17384 type_spec
= cp_parser_enum_specifier (parser
);
17385 /* If that worked, we're done. */
17388 if (declares_class_or_enum
)
17389 *declares_class_or_enum
= 2;
17391 cp_parser_set_decl_spec_type (decl_specs
,
17394 /*type_definition_p=*/true);
17398 goto elaborated_type_specifier
;
17400 /* Any of these indicate either a class-specifier, or an
17401 elaborated-type-specifier. */
17405 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17406 goto elaborated_type_specifier
;
17408 /* Parse tentatively so that we can back up if we don't find a
17409 class-specifier. */
17410 cp_parser_parse_tentatively (parser
);
17411 /* Look for the class-specifier. */
17412 type_spec
= cp_parser_class_specifier (parser
);
17413 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17414 /* If that worked, we're done. */
17415 if (cp_parser_parse_definitely (parser
))
17417 if (declares_class_or_enum
)
17418 *declares_class_or_enum
= 2;
17420 cp_parser_set_decl_spec_type (decl_specs
,
17423 /*type_definition_p=*/true);
17427 /* Fall through. */
17428 elaborated_type_specifier
:
17429 /* We're declaring (not defining) a class or enum. */
17430 if (declares_class_or_enum
)
17431 *declares_class_or_enum
= 1;
17433 /* Fall through. */
17435 /* Look for an elaborated-type-specifier. */
17437 = (cp_parser_elaborated_type_specifier
17439 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17442 cp_parser_set_decl_spec_type (decl_specs
,
17445 /*type_definition_p=*/false);
17450 if (is_cv_qualifier
)
17451 *is_cv_qualifier
= true;
17456 if (is_cv_qualifier
)
17457 *is_cv_qualifier
= true;
17462 if (is_cv_qualifier
)
17463 *is_cv_qualifier
= true;
17467 /* The `__complex__' keyword is a GNU extension. */
17475 /* Handle simple keywords. */
17480 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17481 decl_specs
->any_specifiers_p
= true;
17483 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17486 /* If we do not already have a type-specifier, assume we are looking
17487 at a simple-type-specifier. */
17488 type_spec
= cp_parser_simple_type_specifier (parser
,
17492 /* If we didn't find a type-specifier, and a type-specifier was not
17493 optional in this context, issue an error message. */
17494 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17496 cp_parser_error (parser
, "expected type specifier");
17497 return error_mark_node
;
17503 /* Parse a simple-type-specifier.
17505 simple-type-specifier:
17506 :: [opt] nested-name-specifier [opt] type-name
17507 :: [opt] nested-name-specifier template template-id
17522 simple-type-specifier:
17524 decltype ( expression )
17527 __underlying_type ( type-id )
17531 nested-name-specifier(opt) template-name
17535 simple-type-specifier:
17537 __typeof__ unary-expression
17538 __typeof__ ( type-id )
17539 __typeof__ ( type-id ) { initializer-list , [opt] }
17541 Concepts Extension:
17543 simple-type-specifier:
17544 constrained-type-specifier
17546 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17547 appropriately updated. */
17550 cp_parser_simple_type_specifier (cp_parser
* parser
,
17551 cp_decl_specifier_seq
*decl_specs
,
17552 cp_parser_flags flags
)
17554 tree type
= NULL_TREE
;
17558 /* Peek at the next token. */
17559 token
= cp_lexer_peek_token (parser
->lexer
);
17561 /* If we're looking at a keyword, things are easy. */
17562 switch (token
->keyword
)
17566 decl_specs
->explicit_char_p
= true;
17567 type
= char_type_node
;
17570 type
= char8_type_node
;
17573 type
= char16_type_node
;
17576 type
= char32_type_node
;
17579 type
= wchar_type_node
;
17582 type
= boolean_type_node
;
17585 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17586 type
= short_integer_type_node
;
17590 decl_specs
->explicit_int_p
= true;
17591 type
= integer_type_node
;
17597 idx
= token
->keyword
- RID_INT_N_0
;
17598 if (! int_n_enabled_p
[idx
])
17602 decl_specs
->explicit_intN_p
= true;
17603 decl_specs
->int_n_idx
= idx
;
17605 type
= int_n_trees
[idx
].signed_type
;
17609 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17610 type
= long_integer_type_node
;
17613 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17614 type
= integer_type_node
;
17617 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17618 type
= unsigned_type_node
;
17621 type
= float_type_node
;
17624 type
= double_type_node
;
17627 type
= void_type_node
;
17631 maybe_warn_cpp0x (CPP0X_AUTO
);
17632 if (parser
->auto_is_implicit_function_template_parm_p
)
17634 /* The 'auto' might be the placeholder return type for a function decl
17635 with trailing return type. */
17636 bool have_trailing_return_fn_decl
= false;
17638 cp_parser_parse_tentatively (parser
);
17639 cp_lexer_consume_token (parser
->lexer
);
17640 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17641 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17642 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17643 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17647 cp_lexer_consume_token (parser
->lexer
);
17648 cp_parser_skip_to_closing_parenthesis (parser
,
17649 /*recovering*/false,
17651 /*consume_paren*/true);
17655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17657 have_trailing_return_fn_decl
= true;
17661 cp_lexer_consume_token (parser
->lexer
);
17663 cp_parser_abort_tentative_parse (parser
);
17665 if (have_trailing_return_fn_decl
)
17667 type
= make_auto ();
17671 if (cxx_dialect
>= cxx14
)
17673 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17674 type
= TREE_TYPE (type
);
17677 type
= error_mark_node
;
17679 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17681 if (cxx_dialect
< cxx14
)
17682 error_at (token
->location
,
17683 "use of %<auto%> in lambda parameter declaration "
17684 "only available with "
17685 "%<-std=c++14%> or %<-std=gnu++14%>");
17687 else if (cxx_dialect
< cxx14
)
17688 error_at (token
->location
,
17689 "use of %<auto%> in parameter declaration "
17690 "only available with "
17691 "%<-std=c++14%> or %<-std=gnu++14%>");
17692 else if (!flag_concepts
)
17693 pedwarn (token
->location
, 0,
17694 "use of %<auto%> in parameter declaration "
17695 "only available with %<-fconcepts%>");
17698 type
= make_auto ();
17702 /* Since DR 743, decltype can either be a simple-type-specifier by
17703 itself or begin a nested-name-specifier. Parsing it will replace
17704 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17705 handling below decide what to do. */
17706 cp_parser_decltype (parser
);
17707 cp_lexer_set_token_position (parser
->lexer
, token
);
17711 /* Consume the `typeof' token. */
17712 cp_lexer_consume_token (parser
->lexer
);
17713 /* Parse the operand to `typeof'. */
17714 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17715 /* If it is not already a TYPE, take its type. */
17716 if (!TYPE_P (type
))
17717 type
= finish_typeof (type
);
17720 cp_parser_set_decl_spec_type (decl_specs
, type
,
17722 /*type_definition_p=*/false);
17726 case RID_UNDERLYING_TYPE
:
17727 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17729 cp_parser_set_decl_spec_type (decl_specs
, type
,
17731 /*type_definition_p=*/false);
17736 case RID_DIRECT_BASES
:
17737 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17739 cp_parser_set_decl_spec_type (decl_specs
, type
,
17741 /*type_definition_p=*/false);
17747 /* If token is an already-parsed decltype not followed by ::,
17748 it's a simple-type-specifier. */
17749 if (token
->type
== CPP_DECLTYPE
17750 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17752 type
= saved_checks_value (token
->u
.tree_check_value
);
17755 cp_parser_set_decl_spec_type (decl_specs
, type
,
17757 /*type_definition_p=*/false);
17758 /* Remember that we are handling a decltype in order to
17759 implement the resolution of DR 1510 when the argument
17760 isn't instantiation dependent. */
17761 decl_specs
->decltype_p
= true;
17763 cp_lexer_consume_token (parser
->lexer
);
17767 /* If the type-specifier was for a built-in type, we're done. */
17770 /* Record the type. */
17772 && (token
->keyword
!= RID_SIGNED
17773 && token
->keyword
!= RID_UNSIGNED
17774 && token
->keyword
!= RID_SHORT
17775 && token
->keyword
!= RID_LONG
))
17776 cp_parser_set_decl_spec_type (decl_specs
,
17779 /*type_definition_p=*/false);
17781 decl_specs
->any_specifiers_p
= true;
17783 /* Consume the token. */
17784 cp_lexer_consume_token (parser
->lexer
);
17786 if (type
== error_mark_node
)
17787 return error_mark_node
;
17789 /* There is no valid C++ program where a non-template type is
17790 followed by a "<". That usually indicates that the user thought
17791 that the type was a template. */
17792 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17795 return TYPE_NAME (type
);
17798 /* The type-specifier must be a user-defined type. */
17799 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17803 const bool typename_p
= (cxx_dialect
>= cxx2a
17804 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
17806 /* Don't gobble tokens or issue error messages if this is an
17807 optional type-specifier. */
17808 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17809 cp_parser_parse_tentatively (parser
);
17811 token
= cp_lexer_peek_token (parser
->lexer
);
17813 /* Look for the optional `::' operator. */
17815 = (cp_parser_global_scope_opt (parser
,
17816 /*current_scope_valid_p=*/false)
17818 /* Look for the nested-name specifier. */
17820 = (cp_parser_nested_name_specifier_opt (parser
,
17821 /*typename_keyword_p=*/false,
17822 /*check_dependency_p=*/true,
17824 /*is_declaration=*/false)
17826 /* If we have seen a nested-name-specifier, and the next token
17827 is `template', then we are using the template-id production. */
17829 && cp_parser_optional_template_keyword (parser
))
17831 /* Look for the template-id. */
17832 type
= cp_parser_template_id (parser
,
17833 /*template_keyword_p=*/true,
17834 /*check_dependency_p=*/true,
17836 /*is_declaration=*/false);
17837 /* If the template-id did not name a type, we are out of
17839 if (TREE_CODE (type
) != TYPE_DECL
)
17841 /* ...unless we pretend we have seen 'typename'. */
17843 type
= cp_parser_make_typename_type (parser
, type
,
17847 cp_parser_error (parser
, "expected template-id for type");
17852 /* Otherwise, look for a type-name. */
17854 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
17856 /* Keep track of all name-lookups performed in class scopes. */
17860 && TREE_CODE (type
) == TYPE_DECL
17861 && identifier_p (DECL_NAME (type
)))
17862 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17863 /* If it didn't work out, we don't have a TYPE. */
17864 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17865 && !cp_parser_parse_definitely (parser
))
17867 if (!type
&& cxx_dialect
>= cxx17
)
17869 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17870 cp_parser_parse_tentatively (parser
);
17872 cp_parser_global_scope_opt (parser
,
17873 /*current_scope_valid_p=*/false);
17874 cp_parser_nested_name_specifier_opt (parser
,
17875 /*typename_keyword_p=*/false,
17876 /*check_dependency_p=*/true,
17878 /*is_declaration=*/false);
17879 tree name
= cp_parser_identifier (parser
);
17880 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17881 && parser
->scope
!= error_mark_node
)
17883 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17885 /*is_template=*/false,
17886 /*is_namespace=*/false,
17887 /*check_dependency=*/true,
17888 /*ambiguous_decls=*/NULL
,
17890 if (tmpl
&& tmpl
!= error_mark_node
17891 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17892 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17893 type
= make_template_placeholder (tmpl
);
17896 type
= error_mark_node
;
17897 if (!cp_parser_simulate_error (parser
))
17898 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17899 NLE_TYPE
, token
->location
);
17903 type
= error_mark_node
;
17905 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17906 && !cp_parser_parse_definitely (parser
))
17909 if (type
&& decl_specs
)
17910 cp_parser_set_decl_spec_type (decl_specs
, type
,
17912 /*type_definition_p=*/false);
17915 /* If we didn't get a type-name, issue an error message. */
17916 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17918 cp_parser_error (parser
, "expected type-name");
17919 return error_mark_node
;
17922 if (type
&& type
!= error_mark_node
)
17924 /* See if TYPE is an Objective-C type, and if so, parse and
17925 accept any protocol references following it. Do this before
17926 the cp_parser_check_for_invalid_template_id() call, because
17927 Objective-C types can be followed by '<...>' which would
17928 enclose protocol names rather than template arguments, and so
17929 everything is fine. */
17930 if (c_dialect_objc () && !parser
->scope
17931 && (objc_is_id (type
) || objc_is_class_name (type
)))
17933 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17934 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17936 /* Clobber the "unqualified" type previously entered into
17937 DECL_SPECS with the new, improved protocol-qualified version. */
17939 decl_specs
->type
= qual_type
;
17944 /* There is no valid C++ program where a non-template type is
17945 followed by a "<". That usually indicates that the user
17946 thought that the type was a template. */
17947 cp_parser_check_for_invalid_template_id (parser
, type
,
17955 /* Parse a type-name.
17961 simple-template-id [in c++0x]
17978 Returns a TYPE_DECL for the type. */
17981 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17985 /* We can't know yet whether it is a class-name or not. */
17986 cp_parser_parse_tentatively (parser
);
17987 /* Try a class-name. */
17988 type_decl
= cp_parser_class_name (parser
,
17989 typename_keyword_p
,
17990 /*template_keyword_p=*/false,
17992 /*check_dependency_p=*/true,
17993 /*class_head_p=*/false,
17994 /*is_declaration=*/false);
17995 /* If it's not a class-name, keep looking. */
17996 if (!cp_parser_parse_definitely (parser
))
17998 if (cxx_dialect
< cxx11
)
17999 /* It must be a typedef-name or an enum-name. */
18000 return cp_parser_nonclass_name (parser
);
18002 cp_parser_parse_tentatively (parser
);
18003 /* It is either a simple-template-id representing an
18004 instantiation of an alias template... */
18005 type_decl
= cp_parser_template_id (parser
,
18006 /*template_keyword_p=*/false,
18007 /*check_dependency_p=*/true,
18009 /*is_declaration=*/false);
18010 /* Note that this must be an instantiation of an alias template
18011 because [temp.names]/6 says:
18013 A template-id that names an alias template specialization
18016 Whereas [temp.names]/7 says:
18018 A simple-template-id that names a class template
18019 specialization is a class-name.
18021 With concepts, this could also be a partial-concept-id that
18022 declares a non-type template parameter. */
18023 if (type_decl
!= NULL_TREE
18024 && TREE_CODE (type_decl
) == TYPE_DECL
18025 && TYPE_DECL_ALIAS_P (type_decl
))
18026 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
18027 else if (is_constrained_parameter (type_decl
))
18028 /* Don't do anything. */ ;
18030 cp_parser_simulate_error (parser
);
18032 if (!cp_parser_parse_definitely (parser
))
18033 /* ... Or a typedef-name or an enum-name. */
18034 return cp_parser_nonclass_name (parser
);
18040 /* Check if DECL and ARGS can form a constrained-type-specifier.
18041 If ARGS is non-null, we try to form a concept check of the
18042 form DECL<?, ARGS> where ? is a wildcard that matches any
18043 kind of template argument. If ARGS is NULL, then we try to
18044 form a concept check of the form DECL<?>. */
18047 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
18048 tree decl
, tree args
)
18050 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
18052 /* If we a constrained-type-specifier cannot be deduced. */
18053 if (parser
->prevent_constrained_type_specifiers
)
18056 /* A constrained type specifier can only be found in an
18057 overload set or as a reference to a template declaration.
18059 FIXME: This might be masking a bug. It's possible that
18060 that the deduction below is causing template specializations
18061 to be formed with the wildcard as an argument. */
18062 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
18065 /* Try to build a call expression that evaluates the
18066 concept. This can fail if the overload set refers
18067 only to non-templates. */
18068 tree placeholder
= build_nt (WILDCARD_DECL
);
18069 tree check
= build_concept_check (decl
, placeholder
, args
);
18070 if (check
== error_mark_node
)
18073 /* Deduce the checked constraint and the prototype parameter.
18075 FIXME: In certain cases, failure to deduce should be a
18076 diagnosable error. */
18079 if (!deduce_constrained_parameter (check
, conc
, proto
))
18082 /* In template parameter scope, this results in a constrained
18083 parameter. Return a descriptor of that parm. */
18084 if (processing_template_parmlist
)
18085 return build_constrained_parameter (conc
, proto
, args
);
18087 /* In a parameter-declaration-clause, constrained-type
18088 specifiers result in invented template parameters. */
18089 if (parser
->auto_is_implicit_function_template_parm_p
)
18091 tree x
= build_constrained_parameter (conc
, proto
, args
);
18092 return synthesize_implicit_template_parm (parser
, x
);
18096 /* Otherwise, we're in a context where the constrained
18097 type name is deduced and the constraint applies
18098 after deduction. */
18099 return make_constrained_auto (conc
, args
);
18105 /* If DECL refers to a concept, return a TYPE_DECL representing
18106 the result of using the constrained type specifier in the
18107 current context. DECL refers to a concept if
18109 - it is an overload set containing a function concept taking a single
18112 - it is a variable concept taking a single type argument. */
18115 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
18118 && (TREE_CODE (decl
) == OVERLOAD
18119 || BASELINK_P (decl
)
18120 || variable_concept_p (decl
)))
18121 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
18126 /* Check if DECL and ARGS form a partial-concept-id. If so,
18127 assign ID to the resulting constrained placeholder.
18129 Returns true if the partial-concept-id designates a placeholder
18130 and false otherwise. Note that *id is set to NULL_TREE in
18134 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
18136 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
18139 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18151 Returns a TYPE_DECL for the type. */
18154 cp_parser_nonclass_name (cp_parser
* parser
)
18159 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18160 identifier
= cp_parser_identifier (parser
);
18161 if (identifier
== error_mark_node
)
18162 return error_mark_node
;
18164 /* Look up the type-name. */
18165 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18167 type_decl
= strip_using_decl (type_decl
);
18169 /* If we found an overload set, then it may refer to a concept-name. */
18170 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
18173 if (TREE_CODE (type_decl
) != TYPE_DECL
18174 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18176 /* See if this is an Objective-C type. */
18177 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18178 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18180 type_decl
= TYPE_NAME (type
);
18183 /* Issue an error if we did not find a type-name. */
18184 if (TREE_CODE (type_decl
) != TYPE_DECL
18185 /* In Objective-C, we have the complication that class names are
18186 normally type names and start declarations (eg, the
18187 "NSObject" in "NSObject *object;"), but can be used in an
18188 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18189 is an expression. So, a classname followed by a dot is not a
18190 valid type-name. */
18191 || (objc_is_class_name (TREE_TYPE (type_decl
))
18192 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18194 if (!cp_parser_simulate_error (parser
))
18195 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18196 NLE_TYPE
, token
->location
);
18197 return error_mark_node
;
18199 /* Remember that the name was used in the definition of the
18200 current class so that we can check later to see if the
18201 meaning would have been different after the class was
18202 entirely defined. */
18203 else if (type_decl
!= error_mark_node
18205 maybe_note_name_used_in_class (identifier
, type_decl
);
18210 /* Parse an elaborated-type-specifier. Note that the grammar given
18211 here incorporates the resolution to DR68.
18213 elaborated-type-specifier:
18214 class-key :: [opt] nested-name-specifier [opt] identifier
18215 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18216 enum-key :: [opt] nested-name-specifier [opt] identifier
18217 typename :: [opt] nested-name-specifier identifier
18218 typename :: [opt] nested-name-specifier template [opt]
18223 elaborated-type-specifier:
18224 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18225 class-key attributes :: [opt] nested-name-specifier [opt]
18226 template [opt] template-id
18227 enum attributes :: [opt] nested-name-specifier [opt] identifier
18229 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18230 declared `friend'. If IS_DECLARATION is TRUE, then this
18231 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18232 something is being declared.
18234 Returns the TYPE specified. */
18237 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18239 bool is_declaration
)
18241 enum tag_types tag_type
;
18243 tree type
= NULL_TREE
;
18244 tree attributes
= NULL_TREE
;
18246 cp_token
*token
= NULL
;
18248 /* See if we're looking at the `enum' keyword. */
18249 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18251 /* Consume the `enum' token. */
18252 cp_lexer_consume_token (parser
->lexer
);
18253 /* Remember that it's an enumeration type. */
18254 tag_type
= enum_type
;
18255 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18256 enums) is used here. */
18257 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18258 if (cp_parser_is_keyword (token
, RID_CLASS
)
18259 || cp_parser_is_keyword (token
, RID_STRUCT
))
18261 gcc_rich_location
richloc (token
->location
);
18262 richloc
.add_range (input_location
);
18263 richloc
.add_fixit_remove ();
18264 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18265 "a scoped enum must not use the %qD keyword",
18267 /* Consume the `struct' or `class' and parse it anyway. */
18268 cp_lexer_consume_token (parser
->lexer
);
18270 /* Parse the attributes. */
18271 attributes
= cp_parser_attributes_opt (parser
);
18273 /* Or, it might be `typename'. */
18274 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18277 /* Consume the `typename' token. */
18278 cp_lexer_consume_token (parser
->lexer
);
18279 /* Remember that it's a `typename' type. */
18280 tag_type
= typename_type
;
18282 /* Otherwise it must be a class-key. */
18285 tag_type
= cp_parser_class_key (parser
);
18286 if (tag_type
== none_type
)
18287 return error_mark_node
;
18288 /* Parse the attributes. */
18289 attributes
= cp_parser_attributes_opt (parser
);
18292 /* Look for the `::' operator. */
18293 globalscope
= cp_parser_global_scope_opt (parser
,
18294 /*current_scope_valid_p=*/false);
18295 /* Look for the nested-name-specifier. */
18296 tree nested_name_specifier
;
18297 if (tag_type
== typename_type
&& !globalscope
)
18299 nested_name_specifier
18300 = cp_parser_nested_name_specifier (parser
,
18301 /*typename_keyword_p=*/true,
18302 /*check_dependency_p=*/true,
18305 if (!nested_name_specifier
)
18306 return error_mark_node
;
18309 /* Even though `typename' is not present, the proposed resolution
18310 to Core Issue 180 says that in `class A<T>::B', `B' should be
18311 considered a type-name, even if `A<T>' is dependent. */
18312 nested_name_specifier
18313 = cp_parser_nested_name_specifier_opt (parser
,
18314 /*typename_keyword_p=*/true,
18315 /*check_dependency_p=*/true,
18318 /* For everything but enumeration types, consider a template-id.
18319 For an enumeration type, consider only a plain identifier. */
18320 if (tag_type
!= enum_type
)
18322 bool template_p
= false;
18325 /* Allow the `template' keyword. */
18326 template_p
= cp_parser_optional_template_keyword (parser
);
18327 /* If we didn't see `template', we don't know if there's a
18328 template-id or not. */
18330 cp_parser_parse_tentatively (parser
);
18331 /* The `template' keyword must follow a nested-name-specifier. */
18332 else if (!nested_name_specifier
)
18334 cp_parser_error (parser
, "%<template%> must follow a nested-"
18336 return error_mark_node
;
18339 /* Parse the template-id. */
18340 token
= cp_lexer_peek_token (parser
->lexer
);
18341 decl
= cp_parser_template_id (parser
, template_p
,
18342 /*check_dependency_p=*/true,
18345 /* If we didn't find a template-id, look for an ordinary
18347 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18349 /* We can get here when cp_parser_template_id, called by
18350 cp_parser_class_name with tag_type == none_type, succeeds
18351 and caches a BASELINK. Then, when called again here,
18352 instead of failing and returning an error_mark_node
18353 returns it (see template/typename17.C in C++11).
18354 ??? Could we diagnose this earlier? */
18355 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18357 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18358 type
= error_mark_node
;
18360 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18361 in effect, then we must assume that, upon instantiation, the
18362 template will correspond to a class. */
18363 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18364 && tag_type
== typename_type
)
18365 type
= make_typename_type (parser
->scope
, decl
,
18367 /*complain=*/tf_error
);
18368 /* If the `typename' keyword is in effect and DECL is not a type
18369 decl, then type is non existent. */
18370 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18372 else if (TREE_CODE (decl
) == TYPE_DECL
)
18374 type
= check_elaborated_type_specifier (tag_type
, decl
,
18375 /*allow_template_p=*/true);
18377 /* If the next token is a semicolon, this must be a specialization,
18378 instantiation, or friend declaration. Check the scope while we
18379 still know whether or not we had a nested-name-specifier. */
18380 if (type
!= error_mark_node
18381 && !nested_name_specifier
&& !is_friend
18382 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18383 check_unqualified_spec_or_inst (type
, token
->location
);
18385 else if (decl
== error_mark_node
)
18386 type
= error_mark_node
;
18391 token
= cp_lexer_peek_token (parser
->lexer
);
18392 identifier
= cp_parser_identifier (parser
);
18394 if (identifier
== error_mark_node
)
18396 parser
->scope
= NULL_TREE
;
18397 return error_mark_node
;
18400 /* For a `typename', we needn't call xref_tag. */
18401 if (tag_type
== typename_type
18402 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18403 return cp_parser_make_typename_type (parser
, identifier
,
18406 /* Template parameter lists apply only if we are not within a
18407 function parameter list. */
18408 bool template_parm_lists_apply
18409 = parser
->num_template_parameter_lists
;
18410 if (template_parm_lists_apply
)
18411 for (cp_binding_level
*s
= current_binding_level
;
18412 s
&& s
->kind
!= sk_template_parms
;
18413 s
= s
->level_chain
)
18414 if (s
->kind
== sk_function_parms
)
18415 template_parm_lists_apply
= false;
18417 /* Look up a qualified name in the usual way. */
18421 tree ambiguous_decls
;
18423 decl
= cp_parser_lookup_name (parser
, identifier
,
18425 /*is_template=*/false,
18426 /*is_namespace=*/false,
18427 /*check_dependency=*/true,
18431 /* If the lookup was ambiguous, an error will already have been
18433 if (ambiguous_decls
)
18434 return error_mark_node
;
18436 /* If we are parsing friend declaration, DECL may be a
18437 TEMPLATE_DECL tree node here. However, we need to check
18438 whether this TEMPLATE_DECL results in valid code. Consider
18439 the following example:
18442 template <class T> class C {};
18445 template <class T> friend class N::C; // #1, valid code
18447 template <class T> class Y {
18448 friend class N::C; // #2, invalid code
18451 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18452 name lookup of `N::C'. We see that friend declaration must
18453 be template for the code to be valid. Note that
18454 processing_template_decl does not work here since it is
18455 always 1 for the above two cases. */
18457 decl
= (cp_parser_maybe_treat_template_as_class
18458 (decl
, /*tag_name_p=*/is_friend
18459 && template_parm_lists_apply
));
18461 if (TREE_CODE (decl
) != TYPE_DECL
)
18463 cp_parser_diagnose_invalid_type_name (parser
,
18466 return error_mark_node
;
18469 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18471 bool allow_template
= (template_parm_lists_apply
18472 || DECL_SELF_REFERENCE_P (decl
));
18473 type
= check_elaborated_type_specifier (tag_type
, decl
,
18476 if (type
== error_mark_node
)
18477 return error_mark_node
;
18480 /* Forward declarations of nested types, such as
18485 are invalid unless all components preceding the final '::'
18486 are complete. If all enclosing types are complete, these
18487 declarations become merely pointless.
18489 Invalid forward declarations of nested types are errors
18490 caught elsewhere in parsing. Those that are pointless arrive
18493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18494 && !is_friend
&& !processing_explicit_instantiation
)
18495 warning (0, "declaration %qD does not declare anything", decl
);
18497 type
= TREE_TYPE (decl
);
18501 /* An elaborated-type-specifier sometimes introduces a new type and
18502 sometimes names an existing type. Normally, the rule is that it
18503 introduces a new type only if there is not an existing type of
18504 the same name already in scope. For example, given:
18507 void f() { struct S s; }
18509 the `struct S' in the body of `f' is the same `struct S' as in
18510 the global scope; the existing definition is used. However, if
18511 there were no global declaration, this would introduce a new
18512 local class named `S'.
18514 An exception to this rule applies to the following code:
18516 namespace N { struct S; }
18518 Here, the elaborated-type-specifier names a new type
18519 unconditionally; even if there is already an `S' in the
18520 containing scope this declaration names a new type.
18521 This exception only applies if the elaborated-type-specifier
18522 forms the complete declaration:
18526 A declaration consisting solely of `class-key identifier ;' is
18527 either a redeclaration of the name in the current scope or a
18528 forward declaration of the identifier as a class name. It
18529 introduces the name into the current scope.
18531 We are in this situation precisely when the next token is a `;'.
18533 An exception to the exception is that a `friend' declaration does
18534 *not* name a new type; i.e., given:
18536 struct S { friend struct T; };
18538 `T' is not a new type in the scope of `S'.
18540 Also, `new struct S' or `sizeof (struct S)' never results in the
18541 definition of a new type; a new type can only be declared in a
18542 declaration context. */
18548 /* Friends have special name lookup rules. */
18549 ts
= ts_within_enclosing_non_class
;
18550 else if (is_declaration
18551 && cp_lexer_next_token_is (parser
->lexer
,
18553 /* This is a `class-key identifier ;' */
18559 (template_parm_lists_apply
18560 && (cp_parser_next_token_starts_class_definition_p (parser
)
18561 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18562 /* An unqualified name was used to reference this type, so
18563 there were no qualifying templates. */
18564 if (template_parm_lists_apply
18565 && !cp_parser_check_template_parameters (parser
,
18566 /*num_templates=*/0,
18567 /*template_id*/false,
18569 /*declarator=*/NULL
))
18570 return error_mark_node
;
18571 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18575 if (type
== error_mark_node
)
18576 return error_mark_node
;
18578 /* Allow attributes on forward declarations of classes. */
18581 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18582 warning (OPT_Wattributes
,
18583 "attributes ignored on uninstantiated type");
18584 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18585 && ! processing_explicit_instantiation
)
18586 warning (OPT_Wattributes
,
18587 "attributes ignored on template instantiation");
18588 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18589 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18591 warning (OPT_Wattributes
,
18592 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18595 if (tag_type
!= enum_type
)
18597 /* Indicate whether this class was declared as a `class' or as a
18599 if (CLASS_TYPE_P (type
))
18600 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18601 cp_parser_check_class_key (tag_type
, type
);
18604 /* A "<" cannot follow an elaborated type specifier. If that
18605 happens, the user was probably trying to form a template-id. */
18606 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18612 /* Parse an enum-specifier.
18615 enum-head { enumerator-list [opt] }
18616 enum-head { enumerator-list , } [C++0x]
18619 enum-key identifier [opt] enum-base [opt]
18620 enum-key nested-name-specifier identifier enum-base [opt]
18625 enum struct [C++0x]
18628 : type-specifier-seq
18630 opaque-enum-specifier:
18631 enum-key identifier enum-base [opt] ;
18634 enum-key attributes[opt] identifier [opt] enum-base [opt]
18635 { enumerator-list [opt] }attributes[opt]
18636 enum-key attributes[opt] identifier [opt] enum-base [opt]
18637 { enumerator-list, }attributes[opt] [C++0x]
18639 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18640 if the token stream isn't an enum-specifier after all. */
18643 cp_parser_enum_specifier (cp_parser
* parser
)
18646 tree type
= NULL_TREE
;
18648 tree nested_name_specifier
= NULL_TREE
;
18650 bool scoped_enum_p
= false;
18651 bool has_underlying_type
= false;
18652 bool nested_being_defined
= false;
18653 bool new_value_list
= false;
18654 bool is_new_type
= false;
18655 bool is_unnamed
= false;
18656 tree underlying_type
= NULL_TREE
;
18657 cp_token
*type_start_token
= NULL
;
18658 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18660 parser
->colon_corrects_to_scope_p
= false;
18662 /* Parse tentatively so that we can back up if we don't find a
18664 cp_parser_parse_tentatively (parser
);
18666 /* Caller guarantees that the current token is 'enum', an identifier
18667 possibly follows, and the token after that is an opening brace.
18668 If we don't have an identifier, fabricate an anonymous name for
18669 the enumeration being defined. */
18670 cp_lexer_consume_token (parser
->lexer
);
18672 /* Parse the "class" or "struct", which indicates a scoped
18673 enumeration type in C++0x. */
18674 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18675 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18677 if (cxx_dialect
< cxx11
)
18678 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18680 /* Consume the `struct' or `class' token. */
18681 cp_lexer_consume_token (parser
->lexer
);
18683 scoped_enum_p
= true;
18686 attributes
= cp_parser_attributes_opt (parser
);
18688 /* Clear the qualification. */
18689 parser
->scope
= NULL_TREE
;
18690 parser
->qualifying_scope
= NULL_TREE
;
18691 parser
->object_scope
= NULL_TREE
;
18693 /* Figure out in what scope the declaration is being placed. */
18694 prev_scope
= current_scope ();
18696 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18698 push_deferring_access_checks (dk_no_check
);
18699 nested_name_specifier
18700 = cp_parser_nested_name_specifier_opt (parser
,
18701 /*typename_keyword_p=*/true,
18702 /*check_dependency_p=*/false,
18704 /*is_declaration=*/false);
18706 if (nested_name_specifier
)
18710 identifier
= cp_parser_identifier (parser
);
18711 name
= cp_parser_lookup_name (parser
, identifier
,
18713 /*is_template=*/false,
18714 /*is_namespace=*/false,
18715 /*check_dependency=*/true,
18716 /*ambiguous_decls=*/NULL
,
18718 if (name
&& name
!= error_mark_node
)
18720 type
= TREE_TYPE (name
);
18721 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18723 /* Are template enums allowed in ISO? */
18724 if (template_parm_scope_p ())
18725 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18726 "%qD is an enumeration template", name
);
18727 /* ignore a typename reference, for it will be solved by name
18732 else if (nested_name_specifier
== error_mark_node
)
18733 /* We already issued an error. */;
18736 error_at (type_start_token
->location
,
18737 "%qD does not name an enumeration in %qT",
18738 identifier
, nested_name_specifier
);
18739 nested_name_specifier
= error_mark_node
;
18744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18745 identifier
= cp_parser_identifier (parser
);
18748 identifier
= make_anon_name ();
18751 error_at (type_start_token
->location
,
18752 "unnamed scoped enum is not allowed");
18755 pop_deferring_access_checks ();
18757 /* Check for the `:' that denotes a specified underlying type in C++0x.
18758 Note that a ':' could also indicate a bitfield width, however. */
18759 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18761 cp_decl_specifier_seq type_specifiers
;
18763 /* Consume the `:'. */
18764 cp_lexer_consume_token (parser
->lexer
);
18766 /* Parse the type-specifier-seq. */
18767 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
18768 /*is_declaration=*/false,
18769 /*is_trailing_return=*/false,
18772 /* At this point this is surely not elaborated type specifier. */
18773 if (!cp_parser_parse_definitely (parser
))
18776 if (cxx_dialect
< cxx11
)
18777 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18779 has_underlying_type
= true;
18781 /* If that didn't work, stop. */
18782 if (type_specifiers
.type
!= error_mark_node
)
18784 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18785 /*initialized=*/0, NULL
);
18786 if (underlying_type
== error_mark_node
18787 || check_for_bare_parameter_packs (underlying_type
))
18788 underlying_type
= NULL_TREE
;
18792 /* Look for the `{' but don't consume it yet. */
18793 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18795 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18797 cp_parser_error (parser
, "expected %<{%>");
18798 if (has_underlying_type
)
18804 /* An opaque-enum-specifier must have a ';' here. */
18805 if ((scoped_enum_p
|| underlying_type
)
18806 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18808 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18809 if (has_underlying_type
)
18817 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18820 if (nested_name_specifier
)
18822 if (CLASS_TYPE_P (nested_name_specifier
))
18824 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18825 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18826 push_scope (nested_name_specifier
);
18828 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18830 push_nested_namespace (nested_name_specifier
);
18834 /* Issue an error message if type-definitions are forbidden here. */
18835 if (!cp_parser_check_type_definition (parser
))
18836 type
= error_mark_node
;
18838 /* Create the new type. We do this before consuming the opening
18839 brace so the enum will be recorded as being on the line of its
18840 tag (or the 'enum' keyword, if there is no tag). */
18841 type
= start_enum (identifier
, type
, underlying_type
,
18842 attributes
, scoped_enum_p
, &is_new_type
);
18844 /* If the next token is not '{' it is an opaque-enum-specifier or an
18845 elaborated-type-specifier. */
18846 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18848 timevar_push (TV_PARSE_ENUM
);
18849 if (nested_name_specifier
18850 && nested_name_specifier
!= error_mark_node
)
18852 /* The following catches invalid code such as:
18853 enum class S<int>::E { A, B, C }; */
18854 if (!processing_specialization
18855 && CLASS_TYPE_P (nested_name_specifier
)
18856 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18857 error_at (type_start_token
->location
, "cannot add an enumerator "
18858 "list to a template instantiation");
18860 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18862 error_at (type_start_token
->location
,
18863 "%<%T::%E%> has not been declared",
18864 TYPE_CONTEXT (nested_name_specifier
),
18865 nested_name_specifier
);
18866 type
= error_mark_node
;
18868 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18869 && !CLASS_TYPE_P (nested_name_specifier
))
18871 error_at (type_start_token
->location
, "nested name specifier "
18872 "%qT for enum declaration does not name a class "
18873 "or namespace", nested_name_specifier
);
18874 type
= error_mark_node
;
18876 /* If that scope does not contain the scope in which the
18877 class was originally declared, the program is invalid. */
18878 else if (prev_scope
&& !is_ancestor (prev_scope
,
18879 nested_name_specifier
))
18881 if (at_namespace_scope_p ())
18882 error_at (type_start_token
->location
,
18883 "declaration of %qD in namespace %qD which does not "
18885 type
, prev_scope
, nested_name_specifier
);
18887 error_at (type_start_token
->location
,
18888 "declaration of %qD in %qD which does not "
18890 type
, prev_scope
, nested_name_specifier
);
18891 type
= error_mark_node
;
18893 /* If that scope is the scope where the declaration is being placed
18894 the program is invalid. */
18895 else if (CLASS_TYPE_P (nested_name_specifier
)
18896 && CLASS_TYPE_P (prev_scope
)
18897 && same_type_p (nested_name_specifier
, prev_scope
))
18899 permerror (type_start_token
->location
,
18900 "extra qualification not allowed");
18901 nested_name_specifier
= NULL_TREE
;
18906 begin_scope (sk_scoped_enum
, type
);
18908 /* Consume the opening brace. */
18909 matching_braces braces
;
18910 braces
.consume_open (parser
);
18912 if (type
== error_mark_node
)
18913 ; /* Nothing to add */
18914 else if (OPAQUE_ENUM_P (type
)
18915 || (cxx_dialect
> cxx98
&& processing_specialization
))
18917 new_value_list
= true;
18918 SET_OPAQUE_ENUM_P (type
, false);
18919 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18923 error_at (type_start_token
->location
,
18924 "multiple definition of %q#T", type
);
18925 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18926 "previous definition here");
18927 type
= error_mark_node
;
18930 if (type
== error_mark_node
)
18931 cp_parser_skip_to_end_of_block_or_statement (parser
);
18932 /* If the next token is not '}', then there are some enumerators. */
18933 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18935 if (is_unnamed
&& !scoped_enum_p
)
18936 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18937 "ISO C++ forbids empty unnamed enum");
18940 cp_parser_enumerator_list (parser
, type
);
18942 /* Consume the final '}'. */
18943 braces
.require_close (parser
);
18947 timevar_pop (TV_PARSE_ENUM
);
18951 /* If a ';' follows, then it is an opaque-enum-specifier
18952 and additional restrictions apply. */
18953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18956 error_at (type_start_token
->location
,
18957 "opaque-enum-specifier without name");
18958 else if (nested_name_specifier
)
18959 error_at (type_start_token
->location
,
18960 "opaque-enum-specifier must use a simple identifier");
18964 /* Look for trailing attributes to apply to this enumeration, and
18965 apply them if appropriate. */
18966 if (cp_parser_allow_gnu_extensions_p (parser
))
18968 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18969 cplus_decl_attributes (&type
,
18971 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18974 /* Finish up the enumeration. */
18975 if (type
!= error_mark_node
)
18977 if (new_value_list
)
18978 finish_enum_value_list (type
);
18980 finish_enum (type
);
18983 if (nested_name_specifier
)
18985 if (CLASS_TYPE_P (nested_name_specifier
))
18987 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18988 pop_scope (nested_name_specifier
);
18990 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18992 pop_nested_namespace (nested_name_specifier
);
18996 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19000 /* Parse an enumerator-list. The enumerators all have the indicated
19004 enumerator-definition
19005 enumerator-list , enumerator-definition */
19008 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
19012 /* Parse an enumerator-definition. */
19013 cp_parser_enumerator_definition (parser
, type
);
19015 /* If the next token is not a ',', we've reached the end of
19017 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19019 /* Otherwise, consume the `,' and keep going. */
19020 cp_lexer_consume_token (parser
->lexer
);
19021 /* If the next token is a `}', there is a trailing comma. */
19022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19024 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
19025 pedwarn (input_location
, OPT_Wpedantic
,
19026 "comma at end of enumerator list");
19032 /* Parse an enumerator-definition. The enumerator has the indicated
19035 enumerator-definition:
19037 enumerator = constant-expression
19044 enumerator-definition:
19045 enumerator attributes [opt]
19046 enumerator attributes [opt] = constant-expression */
19049 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
19055 /* Save the input location because we are interested in the location
19056 of the identifier and not the location of the explicit value. */
19057 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19059 /* Look for the identifier. */
19060 identifier
= cp_parser_identifier (parser
);
19061 if (identifier
== error_mark_node
)
19064 /* Parse any specified attributes. */
19065 tree attrs
= cp_parser_attributes_opt (parser
);
19067 /* If the next token is an '=', then there is an explicit value. */
19068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19070 /* Consume the `=' token. */
19071 cp_lexer_consume_token (parser
->lexer
);
19072 /* Parse the value. */
19073 value
= cp_parser_constant_expression (parser
);
19078 /* If we are processing a template, make sure the initializer of the
19079 enumerator doesn't contain any bare template parameter pack. */
19080 if (check_for_bare_parameter_packs (value
))
19081 value
= error_mark_node
;
19083 /* Create the enumerator. */
19084 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19087 /* Parse a namespace-name.
19090 original-namespace-name
19093 Returns the NAMESPACE_DECL for the namespace. */
19096 cp_parser_namespace_name (cp_parser
* parser
)
19099 tree namespace_decl
;
19101 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19103 /* Get the name of the namespace. */
19104 identifier
= cp_parser_identifier (parser
);
19105 if (identifier
== error_mark_node
)
19106 return error_mark_node
;
19108 /* Look up the identifier in the currently active scope. Look only
19109 for namespaces, due to:
19111 [basic.lookup.udir]
19113 When looking up a namespace-name in a using-directive or alias
19114 definition, only namespace names are considered.
19118 [basic.lookup.qual]
19120 During the lookup of a name preceding the :: scope resolution
19121 operator, object, function, and enumerator names are ignored.
19123 (Note that cp_parser_qualifying_entity only calls this
19124 function if the token after the name is the scope resolution
19126 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19128 /*is_template=*/false,
19129 /*is_namespace=*/true,
19130 /*check_dependency=*/true,
19131 /*ambiguous_decls=*/NULL
,
19133 /* If it's not a namespace, issue an error. */
19134 if (namespace_decl
== error_mark_node
19135 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19137 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19139 auto_diagnostic_group d
;
19141 if (namespace_decl
== error_mark_node
19142 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19143 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19146 if (const char *suggestion
= hint
.suggestion ())
19148 gcc_rich_location
richloc (token
->location
);
19149 richloc
.add_fixit_replace (suggestion
);
19150 error_at (&richloc
,
19151 "%qD is not a namespace-name; did you mean %qs?",
19152 identifier
, suggestion
);
19155 error_at (token
->location
, "%qD is not a namespace-name",
19159 cp_parser_error (parser
, "expected namespace-name");
19160 namespace_decl
= error_mark_node
;
19163 return namespace_decl
;
19166 /* Parse a namespace-definition.
19168 namespace-definition:
19169 named-namespace-definition
19170 unnamed-namespace-definition
19172 named-namespace-definition:
19173 original-namespace-definition
19174 extension-namespace-definition
19176 original-namespace-definition:
19177 namespace identifier { namespace-body }
19179 extension-namespace-definition:
19180 namespace original-namespace-name { namespace-body }
19182 unnamed-namespace-definition:
19183 namespace { namespace-body } */
19186 cp_parser_namespace_definition (cp_parser
* parser
)
19189 int nested_definition_count
= 0;
19191 cp_ensure_no_omp_declare_simd (parser
);
19192 cp_ensure_no_oacc_routine (parser
);
19194 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19195 const bool topmost_inline_p
= is_inline
;
19199 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19200 cp_lexer_consume_token (parser
->lexer
);
19203 /* Look for the `namespace' keyword. */
19205 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19207 /* Parse any specified attributes before the identifier. */
19208 tree attribs
= cp_parser_attributes_opt (parser
);
19212 identifier
= NULL_TREE
;
19214 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19216 if (nested_inline_p
&& nested_definition_count
!= 0)
19218 if (cxx_dialect
< cxx2a
)
19219 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19220 OPT_Wpedantic
, "nested inline namespace definitions only "
19221 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19222 cp_lexer_consume_token (parser
->lexer
);
19225 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19227 identifier
= cp_parser_identifier (parser
);
19229 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19230 pedwarn (input_location
, OPT_Wpedantic
,
19231 "standard attributes on namespaces must precede "
19232 "the namespace name");
19234 /* Parse any attributes specified after the identifier. */
19235 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19238 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19240 /* Don't forget that the innermost namespace might have been
19241 marked as inline. Use |= because we cannot overwrite
19242 IS_INLINE in case the outermost namespace is inline, but
19243 there are no nested inlines. */
19244 is_inline
|= nested_inline_p
;
19248 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19249 pedwarn (input_location
, OPT_Wpedantic
,
19250 "nested namespace definitions only available with "
19251 "%<-std=c++17%> or %<-std=gnu++17%>");
19253 /* Nested namespace names can create new namespaces (unlike
19254 other qualified-ids). */
19255 if (int count
= (identifier
19256 ? push_namespace (identifier
, nested_inline_p
)
19258 nested_definition_count
+= count
;
19260 cp_parser_error (parser
, "nested namespace name required");
19261 cp_lexer_consume_token (parser
->lexer
);
19264 if (nested_definition_count
&& !identifier
)
19265 cp_parser_error (parser
, "namespace name required");
19267 if (nested_definition_count
&& attribs
)
19268 error_at (token
->location
,
19269 "a nested namespace definition cannot have attributes");
19270 if (nested_definition_count
&& topmost_inline_p
)
19271 error_at (token
->location
,
19272 "a nested namespace definition cannot be inline");
19274 /* Start the namespace. */
19275 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19277 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19279 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19281 /* Look for the `{' to validate starting the namespace. */
19282 matching_braces braces
;
19283 if (braces
.require_open (parser
))
19285 /* Parse the body of the namespace. */
19286 cp_parser_namespace_body (parser
);
19288 /* Look for the final `}'. */
19289 braces
.require_close (parser
);
19292 if (has_visibility
)
19293 pop_visibility (1);
19295 /* Pop the nested namespace definitions. */
19296 while (nested_definition_count
--)
19300 /* Parse a namespace-body.
19303 declaration-seq [opt] */
19306 cp_parser_namespace_body (cp_parser
* parser
)
19308 cp_parser_declaration_seq_opt (parser
);
19311 /* Parse a namespace-alias-definition.
19313 namespace-alias-definition:
19314 namespace identifier = qualified-namespace-specifier ; */
19317 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19320 tree namespace_specifier
;
19322 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19324 /* Look for the `namespace' keyword. */
19325 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19326 /* Look for the identifier. */
19327 identifier
= cp_parser_identifier (parser
);
19328 if (identifier
== error_mark_node
)
19330 /* Look for the `=' token. */
19331 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19332 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19334 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19335 /* Skip the definition. */
19336 cp_lexer_consume_token (parser
->lexer
);
19337 if (cp_parser_skip_to_closing_brace (parser
))
19338 cp_lexer_consume_token (parser
->lexer
);
19341 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19342 /* Look for the qualified-namespace-specifier. */
19343 namespace_specifier
19344 = cp_parser_qualified_namespace_specifier (parser
);
19345 /* Look for the `;' token. */
19346 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19348 /* Register the alias in the symbol table. */
19349 do_namespace_alias (identifier
, namespace_specifier
);
19352 /* Parse a qualified-namespace-specifier.
19354 qualified-namespace-specifier:
19355 :: [opt] nested-name-specifier [opt] namespace-name
19357 Returns a NAMESPACE_DECL corresponding to the specified
19361 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19363 /* Look for the optional `::'. */
19364 cp_parser_global_scope_opt (parser
,
19365 /*current_scope_valid_p=*/false);
19367 /* Look for the optional nested-name-specifier. */
19368 cp_parser_nested_name_specifier_opt (parser
,
19369 /*typename_keyword_p=*/false,
19370 /*check_dependency_p=*/true,
19372 /*is_declaration=*/true);
19374 return cp_parser_namespace_name (parser
);
19377 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19378 access declaration.
19381 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19382 using :: unqualified-id ;
19384 access-declaration:
19390 cp_parser_using_declaration (cp_parser
* parser
,
19391 bool access_declaration_p
)
19394 bool typename_p
= false;
19395 bool global_scope_p
;
19399 int oldcount
= errorcount
;
19400 cp_token
*diag_token
= NULL
;
19402 if (access_declaration_p
)
19404 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19405 cp_parser_parse_tentatively (parser
);
19409 /* Look for the `using' keyword. */
19410 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19413 /* Peek at the next token. */
19414 token
= cp_lexer_peek_token (parser
->lexer
);
19415 /* See if it's `typename'. */
19416 if (token
->keyword
== RID_TYPENAME
)
19418 /* Remember that we've seen it. */
19420 /* Consume the `typename' token. */
19421 cp_lexer_consume_token (parser
->lexer
);
19425 /* Look for the optional global scope qualification. */
19427 = (cp_parser_global_scope_opt (parser
,
19428 /*current_scope_valid_p=*/false)
19431 /* If we saw `typename', or didn't see `::', then there must be a
19432 nested-name-specifier present. */
19433 if (typename_p
|| !global_scope_p
)
19435 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19436 /*check_dependency_p=*/true,
19438 /*is_declaration=*/true);
19439 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19441 cp_parser_skip_to_end_of_block_or_statement (parser
);
19445 /* Otherwise, we could be in either of the two productions. In that
19446 case, treat the nested-name-specifier as optional. */
19448 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19449 /*typename_keyword_p=*/false,
19450 /*check_dependency_p=*/true,
19452 /*is_declaration=*/true);
19454 qscope
= global_namespace
;
19455 else if (UNSCOPED_ENUM_P (qscope
)
19456 && !TYPE_FUNCTION_SCOPE_P (qscope
))
19457 qscope
= CP_TYPE_CONTEXT (qscope
);
19459 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19460 /* Something has already gone wrong; there's no need to parse
19461 further. Since an error has occurred, the return value of
19462 cp_parser_parse_definitely will be false, as required. */
19463 return cp_parser_parse_definitely (parser
);
19465 token
= cp_lexer_peek_token (parser
->lexer
);
19466 /* Parse the unqualified-id. */
19467 identifier
= cp_parser_unqualified_id (parser
,
19468 /*template_keyword_p=*/false,
19469 /*check_dependency_p=*/true,
19470 /*declarator_p=*/true,
19471 /*optional_p=*/false);
19473 if (access_declaration_p
)
19475 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19476 cp_parser_simulate_error (parser
);
19477 if (!cp_parser_parse_definitely (parser
))
19480 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19482 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19483 if (cxx_dialect
< cxx17
19484 && !in_system_header_at (ell
->location
))
19485 pedwarn (ell
->location
, 0,
19486 "pack expansion in using-declaration only available "
19487 "with %<-std=c++17%> or %<-std=gnu++17%>");
19488 qscope
= make_pack_expansion (qscope
);
19491 /* The function we call to handle a using-declaration is different
19492 depending on what scope we are in. */
19493 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19495 else if (!identifier_p (identifier
)
19496 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19497 /* [namespace.udecl]
19499 A using declaration shall not name a template-id. */
19500 error_at (token
->location
,
19501 "a template-id may not appear in a using-declaration");
19504 if (at_class_scope_p ())
19506 /* Create the USING_DECL. */
19507 decl
= do_class_using_decl (qscope
, identifier
);
19509 if (decl
&& typename_p
)
19510 USING_DECL_TYPENAME_P (decl
) = 1;
19512 if (check_for_bare_parameter_packs (decl
))
19514 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19518 /* Add it to the list of members in this class. */
19519 finish_member_declaration (decl
);
19522 finish_nonmember_using_decl (qscope
, identifier
);
19525 if (!access_declaration_p
19526 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19528 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19529 if (cxx_dialect
< cxx17
)
19530 pedwarn (comma
->location
, 0,
19531 "comma-separated list in using-declaration only available "
19532 "with %<-std=c++17%> or %<-std=gnu++17%>");
19536 /* Look for the final `;'. */
19537 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19539 if (access_declaration_p
&& errorcount
== oldcount
)
19540 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19541 "access declarations are deprecated "
19542 "in favour of using-declarations; "
19543 "suggestion: add the %<using%> keyword");
19548 /* Parse an alias-declaration.
19551 using identifier attribute-specifier-seq [opt] = type-id */
19554 cp_parser_alias_declaration (cp_parser
* parser
)
19556 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19557 location_t id_location
, type_location
;
19558 cp_declarator
*declarator
;
19559 cp_decl_specifier_seq decl_specs
;
19561 const char *saved_message
= NULL
;
19563 /* Look for the `using' keyword. */
19564 cp_token
*using_token
19565 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19566 if (using_token
== NULL
)
19567 return error_mark_node
;
19569 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19570 id
= cp_parser_identifier (parser
);
19571 if (id
== error_mark_node
)
19572 return error_mark_node
;
19574 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19575 attributes
= cp_parser_attributes_opt (parser
);
19576 if (attributes
== error_mark_node
)
19577 return error_mark_node
;
19579 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19581 if (cp_parser_error_occurred (parser
))
19582 return error_mark_node
;
19584 cp_parser_commit_to_tentative_parse (parser
);
19586 /* Now we are going to parse the type-id of the declaration. */
19591 "A type-specifier-seq shall not define a class or enumeration
19592 unless it appears in the type-id of an alias-declaration (7.1.3) that
19593 is not the declaration of a template-declaration."
19595 In other words, if we currently are in an alias template, the
19596 type-id should not define a type.
19598 So let's set parser->type_definition_forbidden_message in that
19599 case; cp_parser_check_type_definition (called by
19600 cp_parser_class_specifier) will then emit an error if a type is
19601 defined in the type-id. */
19602 if (parser
->num_template_parameter_lists
)
19604 saved_message
= parser
->type_definition_forbidden_message
;
19605 parser
->type_definition_forbidden_message
=
19606 G_("types may not be defined in alias template declarations");
19609 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
19612 /* Restore the error message if need be. */
19613 if (parser
->num_template_parameter_lists
)
19614 parser
->type_definition_forbidden_message
= saved_message
;
19616 if (type
== error_mark_node
19617 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19619 cp_parser_skip_to_end_of_block_or_statement (parser
);
19620 return error_mark_node
;
19623 /* A typedef-name can also be introduced by an alias-declaration. The
19624 identifier following the using keyword becomes a typedef-name. It has
19625 the same semantics as if it were introduced by the typedef
19626 specifier. In particular, it does not define a new type and it shall
19627 not appear in the type-id. */
19629 clear_decl_specs (&decl_specs
);
19630 decl_specs
.type
= type
;
19631 if (attributes
!= NULL_TREE
)
19633 decl_specs
.attributes
= attributes
;
19634 set_and_check_decl_spec_loc (&decl_specs
,
19638 set_and_check_decl_spec_loc (&decl_specs
,
19641 set_and_check_decl_spec_loc (&decl_specs
,
19644 decl_specs
.locations
[ds_type_spec
] = type_location
;
19646 if (parser
->num_template_parameter_lists
19647 && !cp_parser_check_template_parameters (parser
,
19648 /*num_templates=*/0,
19649 /*template_id*/false,
19651 /*declarator=*/NULL
))
19652 return error_mark_node
;
19654 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19656 member_p
= at_class_scope_p ();
19658 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19659 NULL_TREE
, attributes
);
19661 decl
= start_decl (declarator
, &decl_specs
, 0,
19662 attributes
, NULL_TREE
, &pushed_scope
);
19663 if (decl
== error_mark_node
)
19666 // Attach constraints to the alias declaration.
19667 if (flag_concepts
&& current_template_parms
)
19669 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19670 tree constr
= build_constraints (reqs
, NULL_TREE
);
19671 set_constraints (decl
, constr
);
19674 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19677 pop_scope (pushed_scope
);
19679 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19680 added into the symbol table; otherwise, return the TYPE_DECL. */
19681 if (DECL_LANG_SPECIFIC (decl
)
19682 && DECL_TEMPLATE_INFO (decl
)
19683 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19685 decl
= DECL_TI_TEMPLATE (decl
);
19687 check_member_template (decl
);
19693 /* Parse a using-directive.
19696 using namespace :: [opt] nested-name-specifier [opt]
19697 namespace-name ; */
19700 cp_parser_using_directive (cp_parser
* parser
)
19702 tree namespace_decl
;
19705 /* Look for the `using' keyword. */
19706 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19707 /* And the `namespace' keyword. */
19708 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19709 /* Look for the optional `::' operator. */
19710 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19711 /* And the optional nested-name-specifier. */
19712 cp_parser_nested_name_specifier_opt (parser
,
19713 /*typename_keyword_p=*/false,
19714 /*check_dependency_p=*/true,
19716 /*is_declaration=*/true);
19717 /* Get the namespace being used. */
19718 namespace_decl
= cp_parser_namespace_name (parser
);
19719 /* And any specified attributes. */
19720 attribs
= cp_parser_attributes_opt (parser
);
19722 /* Update the symbol table. */
19723 finish_using_directive (namespace_decl
, attribs
);
19725 /* Look for the final `;'. */
19726 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19729 /* Parse an asm-definition.
19736 asm-qualifier-list:
19738 asm-qualifier-list asm-qualifier
19741 asm ( string-literal ) ;
19746 asm asm-qualifier-list [opt] ( string-literal ) ;
19747 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19748 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19749 : asm-operand-list [opt] ) ;
19750 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19751 : asm-operand-list [opt]
19752 : asm-clobber-list [opt] ) ;
19753 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19754 : asm-clobber-list [opt]
19755 : asm-goto-list ) ;
19757 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19758 contains goto, and is the only allowed form in that case. No duplicates are
19759 allowed in an asm-qualifier-list. */
19762 cp_parser_asm_definition (cp_parser
* parser
)
19765 tree outputs
= NULL_TREE
;
19766 tree inputs
= NULL_TREE
;
19767 tree clobbers
= NULL_TREE
;
19768 tree labels
= NULL_TREE
;
19770 bool extended_p
= false;
19771 bool invalid_inputs_p
= false;
19772 bool invalid_outputs_p
= false;
19773 required_token missing
= RT_NONE
;
19775 /* Look for the `asm' keyword. */
19776 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19778 if (parser
->in_function_body
19779 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19781 error ("%<asm%> in %<constexpr%> function");
19782 cp_function_chain
->invalid_constexpr
= true;
19785 /* Handle the asm-qualifier-list. */
19786 location_t volatile_loc
= UNKNOWN_LOCATION
;
19787 location_t inline_loc
= UNKNOWN_LOCATION
;
19788 location_t goto_loc
= UNKNOWN_LOCATION
;
19789 location_t first_loc
= UNKNOWN_LOCATION
;
19791 if (cp_parser_allow_gnu_extensions_p (parser
))
19794 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19795 location_t loc
= token
->location
;
19796 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
19801 error_at (loc
, "duplicate %<asm%> qualifier %qT",
19803 inform (volatile_loc
, "first seen here");
19807 if (!parser
->in_function_body
)
19808 warning_at (loc
, 0, "%<asm%> qualifier %qT ignored "
19809 "outside of function body", token
->u
.value
);
19810 volatile_loc
= loc
;
19812 cp_lexer_consume_token (parser
->lexer
);
19818 error_at (loc
, "duplicate %<asm%> qualifier %qT",
19820 inform (inline_loc
, "first seen here");
19826 cp_lexer_consume_token (parser
->lexer
);
19832 error_at (loc
, "duplicate %<asm%> qualifier %qT",
19834 inform (goto_loc
, "first seen here");
19840 cp_lexer_consume_token (parser
->lexer
);
19845 error_at (loc
, "%qT is not an %<asm%> qualifier", token
->u
.value
);
19846 cp_lexer_consume_token (parser
->lexer
);
19855 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
19856 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
19857 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
19859 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
19861 error_at (first_loc
, "%<asm%> qualifier outside of function body");
19862 inline_p
= goto_p
= false;
19865 /* Look for the opening `('. */
19866 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19868 /* Look for the string. */
19869 string
= cp_parser_string_literal (parser
, false, false);
19870 if (string
== error_mark_node
)
19872 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19873 /*consume_paren=*/true);
19877 /* If we're allowing GNU extensions, check for the extended assembly
19878 syntax. Unfortunately, the `:' tokens need not be separated by
19879 a space in C, and so, for compatibility, we tolerate that here
19880 too. Doing that means that we have to treat the `::' operator as
19882 if (cp_parser_allow_gnu_extensions_p (parser
)
19883 && parser
->in_function_body
19884 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19885 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19887 bool inputs_p
= false;
19888 bool clobbers_p
= false;
19889 bool labels_p
= false;
19891 /* The extended syntax was used. */
19894 /* Look for outputs. */
19895 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19897 /* Consume the `:'. */
19898 cp_lexer_consume_token (parser
->lexer
);
19899 /* Parse the output-operands. */
19900 if (cp_lexer_next_token_is_not (parser
->lexer
,
19902 && cp_lexer_next_token_is_not (parser
->lexer
,
19904 && cp_lexer_next_token_is_not (parser
->lexer
,
19908 outputs
= cp_parser_asm_operand_list (parser
);
19909 if (outputs
== error_mark_node
)
19910 invalid_outputs_p
= true;
19913 /* If the next token is `::', there are no outputs, and the
19914 next token is the beginning of the inputs. */
19915 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19916 /* The inputs are coming next. */
19919 /* Look for inputs. */
19921 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19923 /* Consume the `:' or `::'. */
19924 cp_lexer_consume_token (parser
->lexer
);
19925 /* Parse the output-operands. */
19926 if (cp_lexer_next_token_is_not (parser
->lexer
,
19928 && cp_lexer_next_token_is_not (parser
->lexer
,
19930 && cp_lexer_next_token_is_not (parser
->lexer
,
19933 inputs
= cp_parser_asm_operand_list (parser
);
19934 if (inputs
== error_mark_node
)
19935 invalid_inputs_p
= true;
19938 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19939 /* The clobbers are coming next. */
19942 /* Look for clobbers. */
19944 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19947 /* Consume the `:' or `::'. */
19948 cp_lexer_consume_token (parser
->lexer
);
19949 /* Parse the clobbers. */
19950 if (cp_lexer_next_token_is_not (parser
->lexer
,
19952 && cp_lexer_next_token_is_not (parser
->lexer
,
19954 clobbers
= cp_parser_asm_clobber_list (parser
);
19956 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19957 /* The labels are coming next. */
19960 /* Look for labels. */
19962 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19965 /* Consume the `:' or `::'. */
19966 cp_lexer_consume_token (parser
->lexer
);
19967 /* Parse the labels. */
19968 labels
= cp_parser_asm_label_list (parser
);
19971 if (goto_p
&& !labels_p
)
19972 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19975 missing
= RT_COLON_SCOPE
;
19977 /* Look for the closing `)'. */
19978 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19979 missing
? missing
: RT_CLOSE_PAREN
))
19980 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19981 /*consume_paren=*/true);
19982 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19984 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19986 /* Create the ASM_EXPR. */
19987 if (parser
->in_function_body
)
19989 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19990 inputs
, clobbers
, labels
, inline_p
);
19991 /* If the extended syntax was not used, mark the ASM_EXPR. */
19994 tree temp
= asm_stmt
;
19995 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19996 temp
= TREE_OPERAND (temp
, 0);
19998 ASM_INPUT_P (temp
) = 1;
20002 symtab
->finalize_toplevel_asm (string
);
20006 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20007 type that comes from the decl-specifier-seq. */
20010 strip_declarator_types (tree type
, cp_declarator
*declarator
)
20012 for (cp_declarator
*d
= declarator
; d
;)
20022 if (TYPE_PTRMEMFUNC_P (type
))
20023 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
20024 type
= TREE_TYPE (type
);
20032 /* Declarators [gram.dcl.decl] */
20034 /* Parse an init-declarator.
20037 declarator initializer [opt]
20042 declarator asm-specification [opt] attributes [opt] initializer [opt]
20044 function-definition:
20045 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20047 decl-specifier-seq [opt] declarator function-try-block
20051 function-definition:
20052 __extension__ function-definition
20056 function-definition:
20057 decl-specifier-seq [opt] declarator function-transaction-block
20059 The parser flags FLAGS is used to control type-specifier parsing.
20061 The DECL_SPECIFIERS apply to this declarator. Returns a
20062 representation of the entity declared. If MEMBER_P is TRUE, then
20063 this declarator appears in a class scope. The new DECL created by
20064 this declarator is returned.
20066 The CHECKS are access checks that should be performed once we know
20067 what entity is being declared (and, therefore, what classes have
20070 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20071 for a function-definition here as well. If the declarator is a
20072 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20073 be TRUE upon return. By that point, the function-definition will
20074 have been completely parsed.
20076 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20079 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20080 parsed declaration if it is an uninitialized single declarator not followed
20081 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20082 if present, will not be consumed. If returned, this declarator will be
20083 created with SD_INITIALIZED but will not call cp_finish_decl.
20085 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20086 and there is an initializer, the pointed location_t is set to the
20087 location of the '=' or `(', or '{' in C++11 token introducing the
20091 cp_parser_init_declarator (cp_parser
* parser
,
20092 cp_parser_flags flags
,
20093 cp_decl_specifier_seq
*decl_specifiers
,
20094 vec
<deferred_access_check
, va_gc
> *checks
,
20095 bool function_definition_allowed_p
,
20097 int declares_class_or_enum
,
20098 bool* function_definition_p
,
20099 tree
* maybe_range_for_decl
,
20100 location_t
* init_loc
,
20103 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20104 *attributes_start_token
= NULL
;
20105 cp_declarator
*declarator
;
20106 tree prefix_attributes
;
20107 tree attributes
= NULL
;
20108 tree asm_specification
;
20110 tree decl
= NULL_TREE
;
20112 int is_initialized
;
20113 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20114 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20116 enum cpp_ttype initialization_kind
;
20117 bool is_direct_init
= false;
20118 bool is_non_constant_init
;
20119 int ctor_dtor_or_conv_p
;
20120 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20121 tree pushed_scope
= NULL_TREE
;
20122 bool range_for_decl_p
= false;
20123 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20124 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20126 /* Gather the attributes that were provided with the
20127 decl-specifiers. */
20128 prefix_attributes
= decl_specifiers
->attributes
;
20130 /* Assume that this is not the declarator for a function
20132 if (function_definition_p
)
20133 *function_definition_p
= false;
20135 /* Default arguments are only permitted for function parameters. */
20136 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20137 parser
->default_arg_ok_p
= false;
20139 /* Defer access checks while parsing the declarator; we cannot know
20140 what names are accessible until we know what is being
20142 resume_deferring_access_checks ();
20144 token
= cp_lexer_peek_token (parser
->lexer
);
20146 /* Parse the declarator. */
20148 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20149 flags
, &ctor_dtor_or_conv_p
,
20150 /*parenthesized_p=*/NULL
,
20151 member_p
, friend_p
, /*static_p=*/false);
20152 /* Gather up the deferred checks. */
20153 stop_deferring_access_checks ();
20155 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20157 /* If the DECLARATOR was erroneous, there's no need to go
20159 if (declarator
== cp_error_declarator
)
20160 return error_mark_node
;
20162 /* Check that the number of template-parameter-lists is OK. */
20163 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20165 return error_mark_node
;
20167 if (declares_class_or_enum
& 2)
20168 cp_parser_check_for_definition_in_return_type (declarator
,
20169 decl_specifiers
->type
,
20170 decl_specifiers
->locations
[ds_type_spec
]);
20172 /* Figure out what scope the entity declared by the DECLARATOR is
20173 located in. `grokdeclarator' sometimes changes the scope, so
20174 we compute it now. */
20175 scope
= get_scope_of_declarator (declarator
);
20177 /* Perform any lookups in the declared type which were thought to be
20178 dependent, but are not in the scope of the declarator. */
20179 decl_specifiers
->type
20180 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20182 /* If we're allowing GNU extensions, look for an
20183 asm-specification. */
20184 if (cp_parser_allow_gnu_extensions_p (parser
))
20186 /* Look for an asm-specification. */
20187 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20188 asm_specification
= cp_parser_asm_specification_opt (parser
);
20191 asm_specification
= NULL_TREE
;
20193 /* Look for attributes. */
20194 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20195 attributes
= cp_parser_attributes_opt (parser
);
20197 /* Peek at the next token. */
20198 token
= cp_lexer_peek_token (parser
->lexer
);
20200 bool bogus_implicit_tmpl
= false;
20202 if (function_declarator_p (declarator
))
20204 /* Handle C++17 deduction guides. */
20205 if (!decl_specifiers
->type
20206 && ctor_dtor_or_conv_p
<= 0
20207 && cxx_dialect
>= cxx17
)
20209 cp_declarator
*id
= get_id_declarator (declarator
);
20210 tree name
= id
->u
.id
.unqualified_name
;
20211 parser
->scope
= id
->u
.id
.qualifying_scope
;
20212 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20214 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20215 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20217 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20218 id
->u
.id
.sfk
= sfk_deduction_guide
;
20219 ctor_dtor_or_conv_p
= 1;
20223 /* Check to see if the token indicates the start of a
20224 function-definition. */
20225 if (cp_parser_token_starts_function_definition_p (token
))
20227 if (!function_definition_allowed_p
)
20229 /* If a function-definition should not appear here, issue an
20231 cp_parser_error (parser
,
20232 "a function-definition is not allowed here");
20233 return error_mark_node
;
20236 location_t func_brace_location
20237 = cp_lexer_peek_token (parser
->lexer
)->location
;
20239 /* Neither attributes nor an asm-specification are allowed
20240 on a function-definition. */
20241 if (asm_specification
)
20242 error_at (asm_spec_start_token
->location
,
20243 "an %<asm%> specification is not allowed "
20244 "on a function-definition");
20246 error_at (attributes_start_token
->location
,
20247 "attributes are not allowed "
20248 "on a function-definition");
20249 /* This is a function-definition. */
20250 *function_definition_p
= true;
20252 /* Parse the function definition. */
20254 decl
= cp_parser_save_member_function_body (parser
,
20257 prefix_attributes
);
20260 (cp_parser_function_definition_from_specifiers_and_declarator
20261 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20263 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20265 /* This is where the prologue starts... */
20266 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20267 = func_brace_location
;
20273 else if (parser
->fully_implicit_function_template_p
)
20275 /* A non-template declaration involving a function parameter list
20276 containing an implicit template parameter will be made into a
20277 template. If the resulting declaration is not going to be an
20278 actual function then finish the template scope here to prevent it.
20279 An error message will be issued once we have a decl to talk about.
20281 FIXME probably we should do type deduction rather than create an
20282 implicit template, but the standard currently doesn't allow it. */
20283 bogus_implicit_tmpl
= true;
20284 finish_fully_implicit_template (parser
, NULL_TREE
);
20289 Only in function declarations for constructors, destructors, type
20290 conversions, and deduction guides can the decl-specifier-seq be omitted.
20292 We explicitly postpone this check past the point where we handle
20293 function-definitions because we tolerate function-definitions
20294 that are missing their return types in some modes. */
20295 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20297 cp_parser_error (parser
,
20298 "expected constructor, destructor, or type conversion");
20299 return error_mark_node
;
20302 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20303 if (token
->type
== CPP_EQ
20304 || token
->type
== CPP_OPEN_PAREN
20305 || token
->type
== CPP_OPEN_BRACE
)
20307 is_initialized
= SD_INITIALIZED
;
20308 initialization_kind
= token
->type
;
20309 if (maybe_range_for_decl
)
20310 *maybe_range_for_decl
= error_mark_node
;
20311 tmp_init_loc
= token
->location
;
20312 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20313 *init_loc
= tmp_init_loc
;
20315 if (token
->type
== CPP_EQ
20316 && function_declarator_p (declarator
))
20318 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20319 if (t2
->keyword
== RID_DEFAULT
)
20320 is_initialized
= SD_DEFAULTED
;
20321 else if (t2
->keyword
== RID_DELETE
)
20322 is_initialized
= SD_DELETED
;
20327 /* If the init-declarator isn't initialized and isn't followed by a
20328 `,' or `;', it's not a valid init-declarator. */
20329 if (token
->type
!= CPP_COMMA
20330 && token
->type
!= CPP_SEMICOLON
)
20332 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20333 range_for_decl_p
= true;
20336 if (!maybe_range_for_decl
)
20337 cp_parser_error (parser
, "expected initializer");
20338 return error_mark_node
;
20341 is_initialized
= SD_UNINITIALIZED
;
20342 initialization_kind
= CPP_EOF
;
20345 /* Because start_decl has side-effects, we should only call it if we
20346 know we're going ahead. By this point, we know that we cannot
20347 possibly be looking at any other construct. */
20348 cp_parser_commit_to_tentative_parse (parser
);
20350 /* Enter the newly declared entry in the symbol table. If we're
20351 processing a declaration in a class-specifier, we wait until
20352 after processing the initializer. */
20355 if (parser
->in_unbraced_linkage_specification_p
)
20356 decl_specifiers
->storage_class
= sc_extern
;
20357 decl
= start_decl (declarator
, decl_specifiers
,
20358 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20359 attributes
, prefix_attributes
, &pushed_scope
);
20360 cp_finalize_omp_declare_simd (parser
, decl
);
20361 cp_finalize_oacc_routine (parser
, decl
, false);
20362 /* Adjust location of decl if declarator->id_loc is more appropriate:
20363 set, and decl wasn't merged with another decl, in which case its
20364 location would be different from input_location, and more accurate. */
20366 && declarator
->id_loc
!= UNKNOWN_LOCATION
20367 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20368 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20371 /* Enter the SCOPE. That way unqualified names appearing in the
20372 initializer will be looked up in SCOPE. */
20373 pushed_scope
= push_scope (scope
);
20375 /* Perform deferred access control checks, now that we know in which
20376 SCOPE the declared entity resides. */
20377 if (!member_p
&& decl
)
20379 tree saved_current_function_decl
= NULL_TREE
;
20381 /* If the entity being declared is a function, pretend that we
20382 are in its scope. If it is a `friend', it may have access to
20383 things that would not otherwise be accessible. */
20384 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20386 saved_current_function_decl
= current_function_decl
;
20387 current_function_decl
= decl
;
20390 /* Perform access checks for template parameters. */
20391 cp_parser_perform_template_parameter_access_checks (checks
);
20393 /* Perform the access control checks for the declarator and the
20394 decl-specifiers. */
20395 perform_deferred_access_checks (tf_warning_or_error
);
20397 /* Restore the saved value. */
20398 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20399 current_function_decl
= saved_current_function_decl
;
20402 /* Parse the initializer. */
20403 initializer
= NULL_TREE
;
20404 is_direct_init
= false;
20405 is_non_constant_init
= true;
20406 if (is_initialized
)
20408 if (function_declarator_p (declarator
))
20410 if (initialization_kind
== CPP_EQ
)
20411 initializer
= cp_parser_pure_specifier (parser
);
20414 /* If the declaration was erroneous, we don't really
20415 know what the user intended, so just silently
20416 consume the initializer. */
20417 if (decl
!= error_mark_node
)
20418 error_at (tmp_init_loc
, "initializer provided for function");
20419 cp_parser_skip_to_closing_parenthesis (parser
,
20420 /*recovering=*/true,
20421 /*or_comma=*/false,
20422 /*consume_paren=*/true);
20427 /* We want to record the extra mangling scope for in-class
20428 initializers of class members and initializers of static data
20429 member templates. The former involves deferring
20430 parsing of the initializer until end of class as with default
20431 arguments. So right here we only handle the latter. */
20432 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20433 start_lambda_scope (decl
);
20434 initializer
= cp_parser_initializer (parser
,
20436 &is_non_constant_init
);
20437 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20438 finish_lambda_scope ();
20439 if (initializer
== error_mark_node
)
20440 cp_parser_skip_to_end_of_statement (parser
);
20444 /* The old parser allows attributes to appear after a parenthesized
20445 initializer. Mark Mitchell proposed removing this functionality
20446 on the GCC mailing lists on 2002-08-13. This parser accepts the
20447 attributes -- but ignores them. Made a permerror in GCC 8. */
20448 if (cp_parser_allow_gnu_extensions_p (parser
)
20449 && initialization_kind
== CPP_OPEN_PAREN
20450 && cp_parser_attributes_opt (parser
)
20451 && permerror (input_location
,
20452 "attributes after parenthesized initializer ignored"))
20455 if (flag_permissive
&& !hint
)
20458 inform (input_location
,
20459 "this flexibility is deprecated and will be removed");
20463 /* And now complain about a non-function implicit template. */
20464 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20465 error_at (DECL_SOURCE_LOCATION (decl
),
20466 "non-function %qD declared as implicit template", decl
);
20468 /* For an in-class declaration, use `grokfield' to create the
20474 pop_scope (pushed_scope
);
20475 pushed_scope
= NULL_TREE
;
20477 decl
= grokfield (declarator
, decl_specifiers
,
20478 initializer
, !is_non_constant_init
,
20479 /*asmspec=*/NULL_TREE
,
20480 attr_chainon (attributes
, prefix_attributes
));
20481 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20482 cp_parser_save_default_args (parser
, decl
);
20483 cp_finalize_omp_declare_simd (parser
, decl
);
20484 cp_finalize_oacc_routine (parser
, decl
, false);
20487 /* Finish processing the declaration. But, skip member
20489 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20491 cp_finish_decl (decl
,
20492 initializer
, !is_non_constant_init
,
20494 /* If the initializer is in parentheses, then this is
20495 a direct-initialization, which means that an
20496 `explicit' constructor is OK. Otherwise, an
20497 `explicit' constructor cannot be used. */
20498 ((is_direct_init
|| !is_initialized
)
20499 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20501 else if ((cxx_dialect
!= cxx98
) && friend_p
20502 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20503 /* Core issue #226 (C++0x only): A default template-argument
20504 shall not be specified in a friend class template
20506 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20507 /*is_partial=*/false, /*is_friend_decl=*/1);
20509 if (!friend_p
&& pushed_scope
)
20510 pop_scope (pushed_scope
);
20512 if (function_declarator_p (declarator
)
20513 && parser
->fully_implicit_function_template_p
)
20516 decl
= finish_fully_implicit_template (parser
, decl
);
20518 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20521 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20522 && type_uses_auto (decl_specifiers
->type
))
20523 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20528 /* Parse a declarator.
20532 ptr-operator declarator
20534 abstract-declarator:
20535 ptr-operator abstract-declarator [opt]
20536 direct-abstract-declarator
20541 attributes [opt] direct-declarator
20542 attributes [opt] ptr-operator declarator
20544 abstract-declarator:
20545 attributes [opt] ptr-operator abstract-declarator [opt]
20546 attributes [opt] direct-abstract-declarator
20548 The parser flags FLAGS is used to control type-specifier parsing.
20550 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20551 detect constructors, destructors, deduction guides, or conversion operators.
20552 It is set to -1 if the declarator is a name, and +1 if it is a
20553 function. Otherwise it is set to zero. Usually you just want to
20554 test for >0, but internally the negative value is used.
20556 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20557 a decl-specifier-seq unless it declares a constructor, destructor,
20558 or conversion. It might seem that we could check this condition in
20559 semantic analysis, rather than parsing, but that makes it difficult
20560 to handle something like `f()'. We want to notice that there are
20561 no decl-specifiers, and therefore realize that this is an
20562 expression, not a declaration.)
20564 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20565 the declarator is a direct-declarator of the form "(...)".
20567 MEMBER_P is true iff this declarator is a member-declarator.
20569 FRIEND_P is true iff this declarator is a friend.
20571 STATIC_P is true iff the keyword static was seen. */
20573 static cp_declarator
*
20574 cp_parser_declarator (cp_parser
* parser
,
20575 cp_parser_declarator_kind dcl_kind
,
20576 cp_parser_flags flags
,
20577 int* ctor_dtor_or_conv_p
,
20578 bool* parenthesized_p
,
20579 bool member_p
, bool friend_p
, bool static_p
)
20581 cp_declarator
*declarator
;
20582 enum tree_code code
;
20583 cp_cv_quals cv_quals
;
20585 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20587 /* Assume this is not a constructor, destructor, or type-conversion
20589 if (ctor_dtor_or_conv_p
)
20590 *ctor_dtor_or_conv_p
= 0;
20592 if (cp_parser_allow_gnu_extensions_p (parser
))
20593 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20595 /* Check for the ptr-operator production. */
20596 cp_parser_parse_tentatively (parser
);
20597 /* Parse the ptr-operator. */
20598 code
= cp_parser_ptr_operator (parser
,
20603 /* If that worked, then we have a ptr-operator. */
20604 if (cp_parser_parse_definitely (parser
))
20606 /* If a ptr-operator was found, then this declarator was not
20608 if (parenthesized_p
)
20609 *parenthesized_p
= true;
20610 /* The dependent declarator is optional if we are parsing an
20611 abstract-declarator. */
20612 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20613 cp_parser_parse_tentatively (parser
);
20615 /* Parse the dependent declarator. */
20616 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20617 CP_PARSER_FLAGS_NONE
,
20618 /*ctor_dtor_or_conv_p=*/NULL
,
20619 /*parenthesized_p=*/NULL
,
20620 /*member_p=*/false,
20621 friend_p
, /*static_p=*/false);
20623 /* If we are parsing an abstract-declarator, we must handle the
20624 case where the dependent declarator is absent. */
20625 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20626 && !cp_parser_parse_definitely (parser
))
20629 declarator
= cp_parser_make_indirect_declarator
20630 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20632 /* Everything else is a direct-declarator. */
20635 if (parenthesized_p
)
20636 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20638 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20639 flags
, ctor_dtor_or_conv_p
,
20640 member_p
, friend_p
, static_p
);
20643 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20644 declarator
->attributes
= gnu_attributes
;
20648 /* Parse a direct-declarator or direct-abstract-declarator.
20652 direct-declarator ( parameter-declaration-clause )
20653 cv-qualifier-seq [opt]
20654 ref-qualifier [opt]
20655 exception-specification [opt]
20656 direct-declarator [ constant-expression [opt] ]
20659 direct-abstract-declarator:
20660 direct-abstract-declarator [opt]
20661 ( parameter-declaration-clause )
20662 cv-qualifier-seq [opt]
20663 ref-qualifier [opt]
20664 exception-specification [opt]
20665 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20666 ( abstract-declarator )
20668 Returns a representation of the declarator. DCL_KIND is
20669 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20670 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20671 we are parsing a direct-declarator. It is
20672 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20673 of ambiguity we prefer an abstract declarator, as per
20675 The parser flags FLAGS is used to control type-specifier parsing.
20676 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20677 as for cp_parser_declarator. */
20679 static cp_declarator
*
20680 cp_parser_direct_declarator (cp_parser
* parser
,
20681 cp_parser_declarator_kind dcl_kind
,
20682 cp_parser_flags flags
,
20683 int* ctor_dtor_or_conv_p
,
20684 bool member_p
, bool friend_p
, bool static_p
)
20687 cp_declarator
*declarator
= NULL
;
20688 tree scope
= NULL_TREE
;
20689 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20690 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20692 tree pushed_scope
= NULL_TREE
;
20693 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20697 /* Peek at the next token. */
20698 token
= cp_lexer_peek_token (parser
->lexer
);
20699 if (token
->type
== CPP_OPEN_PAREN
)
20701 /* This is either a parameter-declaration-clause, or a
20702 parenthesized declarator. When we know we are parsing a
20703 named declarator, it must be a parenthesized declarator
20704 if FIRST is true. For instance, `(int)' is a
20705 parameter-declaration-clause, with an omitted
20706 direct-abstract-declarator. But `((*))', is a
20707 parenthesized abstract declarator. Finally, when T is a
20708 template parameter `(T)' is a
20709 parameter-declaration-clause, and not a parenthesized
20712 We first try and parse a parameter-declaration-clause,
20713 and then try a nested declarator (if FIRST is true).
20715 It is not an error for it not to be a
20716 parameter-declaration-clause, even when FIRST is
20722 The first is the declaration of a function while the
20723 second is the definition of a variable, including its
20726 Having seen only the parenthesis, we cannot know which of
20727 these two alternatives should be selected. Even more
20728 complex are examples like:
20733 The former is a function-declaration; the latter is a
20734 variable initialization.
20736 Thus again, we try a parameter-declaration-clause, and if
20737 that fails, we back out and return. */
20739 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20742 bool is_declarator
= false;
20746 /* In a member-declarator, the only valid interpretation
20747 of a parenthesis is the start of a
20748 parameter-declaration-clause. (It is invalid to
20749 initialize a static data member with a parenthesized
20750 initializer; only the "=" form of initialization is
20753 cp_parser_parse_tentatively (parser
);
20755 /* Consume the `('. */
20756 matching_parens parens
;
20757 parens
.consume_open (parser
);
20760 /* If this is going to be an abstract declarator, we're
20761 in a declarator and we can't have default args. */
20762 parser
->default_arg_ok_p
= false;
20763 parser
->in_declarator_p
= true;
20766 begin_scope (sk_function_parms
, NULL_TREE
);
20768 /* Parse the parameter-declaration-clause. */
20770 = cp_parser_parameter_declaration_clause (parser
, flags
);
20772 /* Consume the `)'. */
20773 parens
.require_close (parser
);
20775 /* If all went well, parse the cv-qualifier-seq,
20776 ref-qualifier and the exception-specification. */
20777 if (member_p
|| cp_parser_parse_definitely (parser
))
20779 cp_cv_quals cv_quals
;
20780 cp_virt_specifiers virt_specifiers
;
20781 cp_ref_qualifier ref_qual
;
20782 tree exception_specification
;
20785 bool memfn
= (member_p
|| (pushed_scope
20786 && CLASS_TYPE_P (pushed_scope
)));
20787 unsigned char local_variables_forbidden_p
20788 = parser
->local_variables_forbidden_p
;
20789 /* 'this' is not allowed in static member functions. */
20790 if (static_p
|| friend_p
)
20791 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
20793 is_declarator
= true;
20795 if (ctor_dtor_or_conv_p
)
20796 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20799 /* Parse the cv-qualifier-seq. */
20800 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20801 /* Parse the ref-qualifier. */
20802 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20803 /* Parse the tx-qualifier. */
20804 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20805 /* And the exception-specification. */
20806 exception_specification
20807 = cp_parser_exception_specification_opt (parser
);
20809 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20811 /* In here, we handle cases where attribute is used after
20812 the function declaration. For example:
20813 void func (int x) __attribute__((vector(..))); */
20814 tree gnu_attrs
= NULL_TREE
;
20815 tree requires_clause
= NULL_TREE
;
20816 late_return
= (cp_parser_late_return_type_opt
20817 (parser
, declarator
, requires_clause
,
20818 memfn
? cv_quals
: -1));
20820 /* Parse the virt-specifier-seq. */
20821 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20823 /* Create the function-declarator. */
20824 declarator
= make_call_declarator (declarator
,
20830 exception_specification
,
20833 declarator
->std_attributes
= attrs
;
20834 declarator
->attributes
= gnu_attrs
;
20835 /* Any subsequent parameter lists are to do with
20836 return type, so are not those of the declared
20838 parser
->default_arg_ok_p
= false;
20840 /* Restore the state of local_variables_forbidden_p. */
20841 parser
->local_variables_forbidden_p
20842 = local_variables_forbidden_p
;
20845 /* Remove the function parms from scope. */
20846 pop_bindings_and_leave_scope ();
20849 /* Repeat the main loop. */
20853 /* If this is the first, we can try a parenthesized
20857 bool saved_in_type_id_in_expr_p
;
20859 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20860 parser
->in_declarator_p
= saved_in_declarator_p
;
20862 open_paren
= token
;
20863 /* Consume the `('. */
20864 matching_parens parens
;
20865 parens
.consume_open (parser
);
20866 /* Parse the nested declarator. */
20867 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20868 parser
->in_type_id_in_expr_p
= true;
20870 = cp_parser_declarator (parser
, dcl_kind
, flags
,
20871 ctor_dtor_or_conv_p
,
20872 /*parenthesized_p=*/NULL
,
20873 member_p
, friend_p
,
20874 /*static_p=*/false);
20875 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20877 /* Expect a `)'. */
20878 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20879 if (!parens
.require_close (parser
))
20880 declarator
= cp_error_declarator
;
20881 if (declarator
== cp_error_declarator
)
20884 goto handle_declarator
;
20886 /* Otherwise, we must be done. */
20890 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20891 && token
->type
== CPP_OPEN_SQUARE
20892 && !cp_next_tokens_can_be_attribute_p (parser
))
20894 /* Parse an array-declarator. */
20895 tree bounds
, attrs
;
20897 if (ctor_dtor_or_conv_p
)
20898 *ctor_dtor_or_conv_p
= 0;
20902 parser
->default_arg_ok_p
= false;
20903 parser
->in_declarator_p
= true;
20904 /* Consume the `['. */
20905 cp_lexer_consume_token (parser
->lexer
);
20906 /* Peek at the next token. */
20907 token
= cp_lexer_peek_token (parser
->lexer
);
20908 /* If the next token is `]', then there is no
20909 constant-expression. */
20910 if (token
->type
!= CPP_CLOSE_SQUARE
)
20912 bool non_constant_p
;
20914 = cp_parser_constant_expression (parser
,
20915 /*allow_non_constant=*/true,
20917 if (!non_constant_p
)
20919 else if (error_operand_p (bounds
))
20920 /* Already gave an error. */;
20921 else if (!parser
->in_function_body
20922 || current_binding_level
->kind
== sk_function_parms
)
20924 /* Normally, the array bound must be an integral constant
20925 expression. However, as an extension, we allow VLAs
20926 in function scopes as long as they aren't part of a
20927 parameter declaration. */
20928 cp_parser_error (parser
,
20929 "array bound is not an integer constant");
20930 bounds
= error_mark_node
;
20932 else if (processing_template_decl
20933 && !type_dependent_expression_p (bounds
))
20935 /* Remember this wasn't a constant-expression. */
20936 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20937 TREE_SIDE_EFFECTS (bounds
) = 1;
20941 bounds
= NULL_TREE
;
20942 /* Look for the closing `]'. */
20943 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20945 declarator
= cp_error_declarator
;
20949 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20950 declarator
= make_array_declarator (declarator
, bounds
);
20951 declarator
->std_attributes
= attrs
;
20953 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20956 tree qualifying_scope
;
20957 tree unqualified_name
;
20959 special_function_kind sfk
;
20961 bool pack_expansion_p
= false;
20962 cp_token
*declarator_id_start_token
;
20964 /* Parse a declarator-id */
20965 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20968 cp_parser_parse_tentatively (parser
);
20970 /* If we see an ellipsis, we should be looking at a
20972 if (token
->type
== CPP_ELLIPSIS
)
20974 /* Consume the `...' */
20975 cp_lexer_consume_token (parser
->lexer
);
20977 pack_expansion_p
= true;
20981 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20983 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20984 qualifying_scope
= parser
->scope
;
20989 if (!unqualified_name
&& pack_expansion_p
)
20991 /* Check whether an error occurred. */
20992 okay
= !cp_parser_error_occurred (parser
);
20994 /* We already consumed the ellipsis to mark a
20995 parameter pack, but we have no way to report it,
20996 so abort the tentative parse. We will be exiting
20997 immediately anyway. */
20998 cp_parser_abort_tentative_parse (parser
);
21001 okay
= cp_parser_parse_definitely (parser
);
21004 unqualified_name
= error_mark_node
;
21005 else if (unqualified_name
21006 && (qualifying_scope
21007 || (!identifier_p (unqualified_name
))))
21009 cp_parser_error (parser
, "expected unqualified-id");
21010 unqualified_name
= error_mark_node
;
21014 if (!unqualified_name
)
21016 if (unqualified_name
== error_mark_node
)
21018 declarator
= cp_error_declarator
;
21019 pack_expansion_p
= false;
21020 declarator
->parameter_pack_p
= false;
21024 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21026 if (qualifying_scope
&& at_namespace_scope_p ()
21027 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
21029 /* In the declaration of a member of a template class
21030 outside of the class itself, the SCOPE will sometimes
21031 be a TYPENAME_TYPE. For example, given:
21033 template <typename T>
21034 int S<T>::R::i = 3;
21036 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21037 this context, we must resolve S<T>::R to an ordinary
21038 type, rather than a typename type.
21040 The reason we normally avoid resolving TYPENAME_TYPEs
21041 is that a specialization of `S' might render
21042 `S<T>::R' not a type. However, if `S' is
21043 specialized, then this `i' will not be used, so there
21044 is no harm in resolving the types here. */
21047 /* Resolve the TYPENAME_TYPE. */
21048 type
= resolve_typename_type (qualifying_scope
,
21049 /*only_current_p=*/false);
21050 /* If that failed, the declarator is invalid. */
21051 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21053 if (typedef_variant_p (type
))
21054 error_at (declarator_id_start_token
->location
,
21055 "cannot define member of dependent typedef "
21058 error_at (declarator_id_start_token
->location
,
21059 "%<%T::%E%> is not a type",
21060 TYPE_CONTEXT (qualifying_scope
),
21061 TYPE_IDENTIFIER (qualifying_scope
));
21063 qualifying_scope
= type
;
21068 if (unqualified_name
)
21072 if (qualifying_scope
21073 && CLASS_TYPE_P (qualifying_scope
))
21074 class_type
= qualifying_scope
;
21076 class_type
= current_class_type
;
21078 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21080 tree name_type
= TREE_TYPE (unqualified_name
);
21082 if (!class_type
|| !same_type_p (name_type
, class_type
))
21084 /* We do not attempt to print the declarator
21085 here because we do not have enough
21086 information about its original syntactic
21088 cp_parser_error (parser
, "invalid declarator");
21089 declarator
= cp_error_declarator
;
21092 else if (qualifying_scope
21093 && CLASSTYPE_USE_TEMPLATE (name_type
))
21095 error_at (declarator_id_start_token
->location
,
21096 "invalid use of constructor as a template");
21097 inform (declarator_id_start_token
->location
,
21098 "use %<%T::%D%> instead of %<%T::%D%> to "
21099 "name the constructor in a qualified name",
21101 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21102 class_type
, name_type
);
21103 declarator
= cp_error_declarator
;
21106 unqualified_name
= constructor_name (class_type
);
21111 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21112 sfk
= sfk_destructor
;
21113 else if (identifier_p (unqualified_name
)
21114 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21115 sfk
= sfk_conversion
;
21116 else if (/* There's no way to declare a constructor
21117 for an unnamed type, even if the type
21118 got a name for linkage purposes. */
21119 !TYPE_WAS_UNNAMED (class_type
)
21120 /* Handle correctly (c++/19200):
21134 friend void N::S();
21136 && (!friend_p
|| class_type
== qualifying_scope
)
21137 && constructor_name_p (unqualified_name
,
21139 sfk
= sfk_constructor
;
21140 else if (is_overloaded_fn (unqualified_name
)
21141 && DECL_CONSTRUCTOR_P (get_first_fn
21142 (unqualified_name
)))
21143 sfk
= sfk_constructor
;
21145 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21146 *ctor_dtor_or_conv_p
= -1;
21149 declarator
= make_id_declarator (qualifying_scope
,
21151 sfk
, token
->location
);
21152 declarator
->std_attributes
= attrs
;
21153 declarator
->parameter_pack_p
= pack_expansion_p
;
21155 if (pack_expansion_p
)
21156 maybe_warn_variadic_templates ();
21158 /* We're looking for this case in [temp.res]:
21159 A qualified-id is assumed to name a type if [...]
21160 - it is a decl-specifier of the decl-specifier-seq of a
21161 parameter-declaration in a declarator of a function or
21162 function template declaration, ... */
21163 if (cxx_dialect
>= cxx2a
21164 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
21165 && declarator
->kind
== cdk_id
21166 && !at_class_scope_p ()
21167 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21169 /* ...whose declarator-id is qualified. If it isn't, never
21170 assume the parameters to refer to types. */
21171 if (qualifying_scope
== NULL_TREE
)
21172 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21175 /* Now we have something like
21176 template <typename T> int C::x(S::p);
21177 which can be a function template declaration or a
21178 variable template definition. If name lookup for
21179 the declarator-id C::x finds one or more function
21180 templates, assume S::p to name a type. Otherwise,
21183 = cp_parser_lookup_name_simple (parser
, unqualified_name
,
21185 if (!is_overloaded_fn (decl
)
21187 template<typename T>
21188 A<T>::A(T::type) { } */
21189 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
21190 && constructor_name_p (unqualified_name
,
21191 qualifying_scope
)))
21192 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21197 handle_declarator
:;
21198 scope
= get_scope_of_declarator (declarator
);
21201 /* Any names that appear after the declarator-id for a
21202 member are looked up in the containing scope. */
21203 if (at_function_scope_p ())
21205 /* But declarations with qualified-ids can't appear in a
21207 cp_parser_error (parser
, "qualified-id in declaration");
21208 declarator
= cp_error_declarator
;
21211 pushed_scope
= push_scope (scope
);
21213 parser
->in_declarator_p
= true;
21214 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21215 || (declarator
&& declarator
->kind
== cdk_id
))
21216 /* Default args are only allowed on function
21218 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21220 parser
->default_arg_ok_p
= false;
21229 /* For an abstract declarator, we might wind up with nothing at this
21230 point. That's an error; the declarator is not optional. */
21232 cp_parser_error (parser
, "expected declarator");
21233 else if (open_paren
)
21235 /* Record overly parenthesized declarator so we can give a
21236 diagnostic about confusing decl/expr disambiguation. */
21237 if (declarator
->kind
== cdk_array
)
21239 /* If the open and close parens are on different lines, this
21240 is probably a formatting thing, so ignore. */
21241 expanded_location open
= expand_location (open_paren
->location
);
21242 expanded_location close
= expand_location (close_paren
->location
);
21243 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21247 declarator
->parenthesized
= open_paren
->location
;
21250 /* If we entered a scope, we must exit it now. */
21252 pop_scope (pushed_scope
);
21254 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21255 parser
->in_declarator_p
= saved_in_declarator_p
;
21260 /* Parse a ptr-operator.
21263 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21264 * cv-qualifier-seq [opt]
21266 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21267 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21272 & cv-qualifier-seq [opt]
21274 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21275 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21276 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21277 filled in with the TYPE containing the member. *CV_QUALS is
21278 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21279 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21280 Note that the tree codes returned by this function have nothing
21281 to do with the types of trees that will be eventually be created
21282 to represent the pointer or reference type being parsed. They are
21283 just constants with suggestive names. */
21284 static enum tree_code
21285 cp_parser_ptr_operator (cp_parser
* parser
,
21287 cp_cv_quals
*cv_quals
,
21290 enum tree_code code
= ERROR_MARK
;
21292 tree attrs
= NULL_TREE
;
21294 /* Assume that it's not a pointer-to-member. */
21296 /* And that there are no cv-qualifiers. */
21297 *cv_quals
= TYPE_UNQUALIFIED
;
21299 /* Peek at the next token. */
21300 token
= cp_lexer_peek_token (parser
->lexer
);
21302 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21303 if (token
->type
== CPP_MULT
)
21304 code
= INDIRECT_REF
;
21305 else if (token
->type
== CPP_AND
)
21307 else if ((cxx_dialect
!= cxx98
) &&
21308 token
->type
== CPP_AND_AND
) /* C++0x only */
21309 code
= NON_LVALUE_EXPR
;
21311 if (code
!= ERROR_MARK
)
21313 /* Consume the `*', `&' or `&&'. */
21314 cp_lexer_consume_token (parser
->lexer
);
21316 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21317 `&', if we are allowing GNU extensions. (The only qualifier
21318 that can legally appear after `&' is `restrict', but that is
21319 enforced during semantic analysis. */
21320 if (code
== INDIRECT_REF
21321 || cp_parser_allow_gnu_extensions_p (parser
))
21322 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21324 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21325 if (attributes
!= NULL
)
21326 *attributes
= attrs
;
21330 /* Try the pointer-to-member case. */
21331 cp_parser_parse_tentatively (parser
);
21332 /* Look for the optional `::' operator. */
21333 cp_parser_global_scope_opt (parser
,
21334 /*current_scope_valid_p=*/false);
21335 /* Look for the nested-name specifier. */
21336 token
= cp_lexer_peek_token (parser
->lexer
);
21337 cp_parser_nested_name_specifier (parser
,
21338 /*typename_keyword_p=*/false,
21339 /*check_dependency_p=*/true,
21341 /*is_declaration=*/false);
21342 /* If we found it, and the next token is a `*', then we are
21343 indeed looking at a pointer-to-member operator. */
21344 if (!cp_parser_error_occurred (parser
)
21345 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21347 /* Indicate that the `*' operator was used. */
21348 code
= INDIRECT_REF
;
21350 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21351 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21352 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21353 error_at (token
->location
, "cannot form pointer to member of "
21354 "non-class %q#T", parser
->scope
);
21357 /* The type of which the member is a member is given by the
21359 *type
= parser
->scope
;
21360 /* The next name will not be qualified. */
21361 parser
->scope
= NULL_TREE
;
21362 parser
->qualifying_scope
= NULL_TREE
;
21363 parser
->object_scope
= NULL_TREE
;
21364 /* Look for optional c++11 attributes. */
21365 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21366 if (attributes
!= NULL
)
21367 *attributes
= attrs
;
21368 /* Look for the optional cv-qualifier-seq. */
21369 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21372 /* If that didn't work we don't have a ptr-operator. */
21373 if (!cp_parser_parse_definitely (parser
))
21374 cp_parser_error (parser
, "expected ptr-operator");
21380 /* Parse an (optional) cv-qualifier-seq.
21383 cv-qualifier cv-qualifier-seq [opt]
21394 Returns a bitmask representing the cv-qualifiers. */
21397 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21399 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21404 cp_cv_quals cv_qualifier
;
21406 /* Peek at the next token. */
21407 token
= cp_lexer_peek_token (parser
->lexer
);
21408 /* See if it's a cv-qualifier. */
21409 switch (token
->keyword
)
21412 cv_qualifier
= TYPE_QUAL_CONST
;
21416 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21420 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21424 cv_qualifier
= TYPE_UNQUALIFIED
;
21431 if (cv_quals
& cv_qualifier
)
21433 gcc_rich_location
richloc (token
->location
);
21434 richloc
.add_fixit_remove ();
21435 error_at (&richloc
, "duplicate cv-qualifier");
21436 cp_lexer_purge_token (parser
->lexer
);
21440 cp_lexer_consume_token (parser
->lexer
);
21441 cv_quals
|= cv_qualifier
;
21448 /* Parse an (optional) ref-qualifier
21454 Returns cp_ref_qualifier representing ref-qualifier. */
21456 static cp_ref_qualifier
21457 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21459 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21461 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21462 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21467 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21468 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21470 switch (token
->type
)
21473 curr_ref_qual
= REF_QUAL_LVALUE
;
21477 curr_ref_qual
= REF_QUAL_RVALUE
;
21481 curr_ref_qual
= REF_QUAL_NONE
;
21485 if (!curr_ref_qual
)
21489 error_at (token
->location
, "multiple ref-qualifiers");
21490 cp_lexer_purge_token (parser
->lexer
);
21494 ref_qual
= curr_ref_qual
;
21495 cp_lexer_consume_token (parser
->lexer
);
21502 /* Parse an optional tx-qualifier.
21506 transaction_safe_dynamic */
21509 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21511 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21512 if (token
->type
== CPP_NAME
)
21514 tree name
= token
->u
.value
;
21515 const char *p
= IDENTIFIER_POINTER (name
);
21516 const int len
= strlen ("transaction_safe");
21517 if (!strncmp (p
, "transaction_safe", len
))
21521 || !strcmp (p
, "_dynamic"))
21523 cp_lexer_consume_token (parser
->lexer
);
21526 error ("%qE requires %<-fgnu-tm%>", name
);
21537 /* Parse an (optional) virt-specifier-seq.
21539 virt-specifier-seq:
21540 virt-specifier virt-specifier-seq [opt]
21546 Returns a bitmask representing the virt-specifiers. */
21548 static cp_virt_specifiers
21549 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21551 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21556 cp_virt_specifiers virt_specifier
;
21558 /* Peek at the next token. */
21559 token
= cp_lexer_peek_token (parser
->lexer
);
21560 /* See if it's a virt-specifier-qualifier. */
21561 if (token
->type
!= CPP_NAME
)
21563 if (id_equal (token
->u
.value
, "override"))
21565 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21566 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21568 else if (id_equal (token
->u
.value
, "final"))
21570 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21571 virt_specifier
= VIRT_SPEC_FINAL
;
21573 else if (id_equal (token
->u
.value
, "__final"))
21575 virt_specifier
= VIRT_SPEC_FINAL
;
21580 if (virt_specifiers
& virt_specifier
)
21582 gcc_rich_location
richloc (token
->location
);
21583 richloc
.add_fixit_remove ();
21584 error_at (&richloc
, "duplicate virt-specifier");
21585 cp_lexer_purge_token (parser
->lexer
);
21589 cp_lexer_consume_token (parser
->lexer
);
21590 virt_specifiers
|= virt_specifier
;
21593 return virt_specifiers
;
21596 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21597 is in scope even though it isn't real. */
21600 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21604 if (current_class_ptr
)
21606 /* We don't clear this between NSDMIs. Is it already what we want? */
21607 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21608 if (DECL_P (current_class_ptr
)
21609 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21610 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21611 && cp_type_quals (type
) == quals
)
21615 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21616 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21617 current_class_ptr
= NULL_TREE
;
21619 = cp_build_fold_indirect_ref (this_parm
);
21620 current_class_ptr
= this_parm
;
21623 /* Return true iff our current scope is a non-static data member
21627 parsing_nsdmi (void)
21629 /* We recognize NSDMI context by the context-less 'this' pointer set up
21630 by the function above. */
21631 if (current_class_ptr
21632 && TREE_CODE (current_class_ptr
) == PARM_DECL
21633 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21638 /* Parse a late-specified return type, if any. This is not a separate
21639 non-terminal, but part of a function declarator, which looks like
21641 -> trailing-type-specifier-seq abstract-declarator(opt)
21643 Returns the type indicated by the type-id.
21645 In addition to this, parse any queued up #pragma omp declare simd
21646 clauses, and #pragma acc routine clauses.
21648 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21652 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21653 tree
& requires_clause
, cp_cv_quals quals
)
21656 tree type
= NULL_TREE
;
21657 bool declare_simd_p
= (parser
->omp_declare_simd
21659 && declarator
->kind
== cdk_id
);
21661 bool oacc_routine_p
= (parser
->oacc_routine
21663 && declarator
->kind
== cdk_id
);
21665 /* Peek at the next token. */
21666 token
= cp_lexer_peek_token (parser
->lexer
);
21667 /* A late-specified return type is indicated by an initial '->'. */
21668 if (token
->type
!= CPP_DEREF
21669 && token
->keyword
!= RID_REQUIRES
21670 && !(token
->type
== CPP_NAME
21671 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21672 && !(declare_simd_p
|| oacc_routine_p
))
21675 tree save_ccp
= current_class_ptr
;
21676 tree save_ccr
= current_class_ref
;
21679 /* DR 1207: 'this' is in scope in the trailing return type. */
21680 inject_this_parameter (current_class_type
, quals
);
21683 if (token
->type
== CPP_DEREF
)
21685 /* Consume the ->. */
21686 cp_lexer_consume_token (parser
->lexer
);
21688 type
= cp_parser_trailing_type_id (parser
);
21691 /* Function declarations may be followed by a trailing
21692 requires-clause. */
21693 requires_clause
= cp_parser_requires_clause_opt (parser
);
21695 if (declare_simd_p
)
21696 declarator
->attributes
21697 = cp_parser_late_parsing_omp_declare_simd (parser
,
21698 declarator
->attributes
);
21699 if (oacc_routine_p
)
21700 declarator
->attributes
21701 = cp_parser_late_parsing_oacc_routine (parser
,
21702 declarator
->attributes
);
21706 current_class_ptr
= save_ccp
;
21707 current_class_ref
= save_ccr
;
21713 /* Parse a declarator-id.
21717 :: [opt] nested-name-specifier [opt] type-name
21719 In the `id-expression' case, the value returned is as for
21720 cp_parser_id_expression if the id-expression was an unqualified-id.
21721 If the id-expression was a qualified-id, then a SCOPE_REF is
21722 returned. The first operand is the scope (either a NAMESPACE_DECL
21723 or TREE_TYPE), but the second is still just a representation of an
21727 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21730 /* The expression must be an id-expression. Assume that qualified
21731 names are the names of types so that:
21734 int S<T>::R::i = 3;
21736 will work; we must treat `S<T>::R' as the name of a type.
21737 Similarly, assume that qualified names are templates, where
21741 int S<T>::R<T>::i = 3;
21744 id
= cp_parser_id_expression (parser
,
21745 /*template_keyword_p=*/false,
21746 /*check_dependency_p=*/false,
21747 /*template_p=*/NULL
,
21748 /*declarator_p=*/true,
21750 if (id
&& BASELINK_P (id
))
21751 id
= BASELINK_FUNCTIONS (id
);
21755 /* Parse a type-id.
21758 type-specifier-seq abstract-declarator [opt]
21760 The parser flags FLAGS is used to control type-specifier parsing.
21762 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21764 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21765 i.e. we've just seen "->".
21767 Returns the TYPE specified. */
21770 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
21771 bool is_template_arg
, bool is_trailing_return
,
21772 location_t
*type_location
)
21774 cp_decl_specifier_seq type_specifier_seq
;
21775 cp_declarator
*abstract_declarator
;
21777 /* Parse the type-specifier-seq. */
21778 cp_parser_type_specifier_seq (parser
, flags
,
21779 /*is_declaration=*/false,
21780 is_trailing_return
,
21781 &type_specifier_seq
);
21783 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21785 if (is_template_arg
&& type_specifier_seq
.type
21786 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21787 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21788 /* A bare template name as a template argument is a template template
21789 argument, not a placeholder, so fail parsing it as a type argument. */
21791 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21792 cp_parser_simulate_error (parser
);
21793 return error_mark_node
;
21795 if (type_specifier_seq
.type
== error_mark_node
)
21796 return error_mark_node
;
21798 /* There might or might not be an abstract declarator. */
21799 cp_parser_parse_tentatively (parser
);
21800 /* Look for the declarator. */
21801 abstract_declarator
21802 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
21803 CP_PARSER_FLAGS_NONE
, NULL
,
21804 /*parenthesized_p=*/NULL
,
21805 /*member_p=*/false,
21806 /*friend_p=*/false,
21807 /*static_p=*/false);
21808 /* Check to see if there really was a declarator. */
21809 if (!cp_parser_parse_definitely (parser
))
21810 abstract_declarator
= NULL
;
21812 if (type_specifier_seq
.type
21813 /* The concepts TS allows 'auto' as a type-id. */
21814 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21815 /* None of the valid uses of 'auto' in C++14 involve the type-id
21816 nonterminal, but it is valid in a trailing-return-type. */
21817 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21818 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21820 /* A type-id with type 'auto' is only ok if the abstract declarator
21821 is a function declarator with a late-specified return type.
21823 A type-id with 'auto' is also valid in a trailing-return-type
21824 in a compound-requirement. */
21825 if (abstract_declarator
21826 && abstract_declarator
->kind
== cdk_function
21827 && abstract_declarator
->u
.function
.late_return_type
)
21829 else if (parser
->in_result_type_constraint_p
)
21833 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21834 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21836 error_at (loc
, "missing template arguments after %qT",
21838 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21842 error_at (loc
, "invalid use of %qT", auto_node
);
21843 return error_mark_node
;
21847 return groktypename (&type_specifier_seq
, abstract_declarator
,
21851 /* Wrapper for cp_parser_type_id_1. */
21854 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
21855 location_t
*type_location
)
21857 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
21860 /* Wrapper for cp_parser_type_id_1. */
21863 cp_parser_template_type_arg (cp_parser
*parser
)
21866 const char *saved_message
= parser
->type_definition_forbidden_message
;
21867 parser
->type_definition_forbidden_message
21868 = G_("types may not be defined in template arguments");
21869 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
21870 parser
->type_definition_forbidden_message
= saved_message
;
21871 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21873 error ("invalid use of %<auto%> in template argument");
21874 r
= error_mark_node
;
21879 /* Wrapper for cp_parser_type_id_1. */
21882 cp_parser_trailing_type_id (cp_parser
*parser
)
21884 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21885 false, true, NULL
);
21888 /* Parse a type-specifier-seq.
21890 type-specifier-seq:
21891 type-specifier type-specifier-seq [opt]
21895 type-specifier-seq:
21896 attributes type-specifier-seq [opt]
21898 The parser flags FLAGS is used to control type-specifier parsing.
21900 If IS_DECLARATION is true, we are at the start of a "condition" or
21901 exception-declaration, so we might be followed by a declarator-id.
21903 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21904 i.e. we've just seen "->".
21906 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21909 cp_parser_type_specifier_seq (cp_parser
* parser
,
21910 cp_parser_flags flags
,
21911 bool is_declaration
,
21912 bool is_trailing_return
,
21913 cp_decl_specifier_seq
*type_specifier_seq
)
21915 bool seen_type_specifier
= false;
21916 cp_token
*start_token
= NULL
;
21918 /* Clear the TYPE_SPECIFIER_SEQ. */
21919 clear_decl_specs (type_specifier_seq
);
21921 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
21922 /* In the context of a trailing return type, enum E { } is an
21923 elaborated-type-specifier followed by a function-body, not an
21925 if (is_trailing_return
)
21926 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21928 /* Parse the type-specifiers and attributes. */
21931 tree type_specifier
;
21932 bool is_cv_qualifier
;
21934 /* Check for attributes first. */
21935 if (cp_next_tokens_can_be_attribute_p (parser
))
21937 type_specifier_seq
->attributes
21938 = attr_chainon (type_specifier_seq
->attributes
,
21939 cp_parser_attributes_opt (parser
));
21943 /* record the token of the beginning of the type specifier seq,
21944 for error reporting purposes*/
21946 start_token
= cp_lexer_peek_token (parser
->lexer
);
21948 /* Look for the type-specifier. */
21949 type_specifier
= cp_parser_type_specifier (parser
,
21951 type_specifier_seq
,
21952 /*is_declaration=*/false,
21955 if (!type_specifier
)
21957 /* If the first type-specifier could not be found, this is not a
21958 type-specifier-seq at all. */
21959 if (!seen_type_specifier
)
21961 /* Set in_declarator_p to avoid skipping to the semicolon. */
21962 int in_decl
= parser
->in_declarator_p
;
21963 parser
->in_declarator_p
= true;
21965 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21966 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21967 cp_parser_error (parser
, "expected type-specifier");
21969 parser
->in_declarator_p
= in_decl
;
21971 type_specifier_seq
->type
= error_mark_node
;
21974 /* If subsequent type-specifiers could not be found, the
21975 type-specifier-seq is complete. */
21979 seen_type_specifier
= true;
21980 /* The standard says that a condition can be:
21982 type-specifier-seq declarator = assignment-expression
21989 we should treat the "S" as a declarator, not as a
21990 type-specifier. The standard doesn't say that explicitly for
21991 type-specifier-seq, but it does say that for
21992 decl-specifier-seq in an ordinary declaration. Perhaps it
21993 would be clearer just to allow a decl-specifier-seq here, and
21994 then add a semantic restriction that if any decl-specifiers
21995 that are not type-specifiers appear, the program is invalid. */
21996 if (is_declaration
&& !is_cv_qualifier
)
21997 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
22001 /* Return whether the function currently being declared has an associated
22002 template parameter list. */
22005 function_being_declared_is_template_p (cp_parser
* parser
)
22007 if (!current_template_parms
|| processing_template_parmlist
)
22010 if (parser
->implicit_template_scope
)
22013 if (at_class_scope_p ()
22014 && TYPE_BEING_DEFINED (current_class_type
))
22015 return parser
->num_template_parameter_lists
!= 0;
22017 return ((int) parser
->num_template_parameter_lists
> template_class_depth
22018 (current_class_type
));
22021 /* Parse a parameter-declaration-clause.
22023 parameter-declaration-clause:
22024 parameter-declaration-list [opt] ... [opt]
22025 parameter-declaration-list , ...
22027 The parser flags FLAGS is used to control type-specifier parsing.
22029 Returns a representation for the parameter declarations. A return
22030 value of NULL indicates a parameter-declaration-clause consisting
22031 only of an ellipsis. */
22034 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
22035 cp_parser_flags flags
)
22041 temp_override
<bool> cleanup
22042 (parser
->auto_is_implicit_function_template_parm_p
);
22044 if (!processing_specialization
22045 && !processing_template_parmlist
22046 && !processing_explicit_instantiation
22047 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22048 actual function or a random abstract declarator. */
22049 && parser
->default_arg_ok_p
)
22050 if (!current_function_decl
22051 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
22052 parser
->auto_is_implicit_function_template_parm_p
= true;
22054 /* Peek at the next token. */
22055 token
= cp_lexer_peek_token (parser
->lexer
);
22056 /* Check for trivial parameter-declaration-clauses. */
22057 if (token
->type
== CPP_ELLIPSIS
)
22059 /* Consume the `...' token. */
22060 cp_lexer_consume_token (parser
->lexer
);
22063 else if (token
->type
== CPP_CLOSE_PAREN
)
22064 /* There are no parameters. */
22065 return void_list_node
;
22066 /* Check for `(void)', too, which is a special case. */
22067 else if (token
->keyword
== RID_VOID
22068 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22069 == CPP_CLOSE_PAREN
))
22071 /* Consume the `void' token. */
22072 cp_lexer_consume_token (parser
->lexer
);
22073 /* There are no parameters. */
22074 return void_list_node
;
22077 /* Parse the parameter-declaration-list. */
22078 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
22079 /* If a parse error occurred while parsing the
22080 parameter-declaration-list, then the entire
22081 parameter-declaration-clause is erroneous. */
22082 if (parameters
== error_mark_node
)
22085 /* Peek at the next token. */
22086 token
= cp_lexer_peek_token (parser
->lexer
);
22087 /* If it's a `,', the clause should terminate with an ellipsis. */
22088 if (token
->type
== CPP_COMMA
)
22090 /* Consume the `,'. */
22091 cp_lexer_consume_token (parser
->lexer
);
22092 /* Expect an ellipsis. */
22094 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
22096 /* It might also be `...' if the optional trailing `,' was
22098 else if (token
->type
== CPP_ELLIPSIS
)
22100 /* Consume the `...' token. */
22101 cp_lexer_consume_token (parser
->lexer
);
22102 /* And remember that we saw it. */
22106 ellipsis_p
= false;
22108 /* Finish the parameter list. */
22110 parameters
= chainon (parameters
, void_list_node
);
22115 /* Parse a parameter-declaration-list.
22117 parameter-declaration-list:
22118 parameter-declaration
22119 parameter-declaration-list , parameter-declaration
22121 The parser flags FLAGS is used to control type-specifier parsing.
22123 Returns a representation of the parameter-declaration-list, as for
22124 cp_parser_parameter_declaration_clause. However, the
22125 `void_list_node' is never appended to the list. */
22128 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22130 tree parameters
= NULL_TREE
;
22131 tree
*tail
= ¶meters
;
22132 bool saved_in_unbraced_linkage_specification_p
;
22135 /* The special considerations that apply to a function within an
22136 unbraced linkage specifications do not apply to the parameters
22137 to the function. */
22138 saved_in_unbraced_linkage_specification_p
22139 = parser
->in_unbraced_linkage_specification_p
;
22140 parser
->in_unbraced_linkage_specification_p
= false;
22142 /* Look for more parameters. */
22145 cp_parameter_declarator
*parameter
;
22146 tree decl
= error_mark_node
;
22147 bool parenthesized_p
= false;
22149 /* Parse the parameter. */
22151 = cp_parser_parameter_declaration (parser
, flags
,
22152 /*template_parm_p=*/false,
22155 /* We don't know yet if the enclosing context is deprecated, so wait
22156 and warn in grokparms if appropriate. */
22157 deprecated_state
= DEPRECATED_SUPPRESS
;
22161 decl
= grokdeclarator (parameter
->declarator
,
22162 ¶meter
->decl_specifiers
,
22164 parameter
->default_argument
!= NULL_TREE
,
22165 ¶meter
->decl_specifiers
.attributes
);
22166 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22167 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22170 deprecated_state
= DEPRECATED_NORMAL
;
22172 /* If a parse error occurred parsing the parameter declaration,
22173 then the entire parameter-declaration-list is erroneous. */
22174 if (decl
== error_mark_node
)
22176 parameters
= error_mark_node
;
22180 if (parameter
->decl_specifiers
.attributes
)
22181 cplus_decl_attributes (&decl
,
22182 parameter
->decl_specifiers
.attributes
,
22184 if (DECL_NAME (decl
))
22185 decl
= pushdecl (decl
);
22187 if (decl
!= error_mark_node
)
22189 retrofit_lang_decl (decl
);
22190 DECL_PARM_INDEX (decl
) = ++index
;
22191 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22194 /* Add the new parameter to the list. */
22195 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22196 tail
= &TREE_CHAIN (*tail
);
22198 /* Peek at the next token. */
22199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22200 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22201 /* These are for Objective-C++ */
22202 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22203 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22204 /* The parameter-declaration-list is complete. */
22206 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22210 /* Peek at the next token. */
22211 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22212 /* If it's an ellipsis, then the list is complete. */
22213 if (token
->type
== CPP_ELLIPSIS
)
22215 /* Otherwise, there must be more parameters. Consume the
22217 cp_lexer_consume_token (parser
->lexer
);
22218 /* When parsing something like:
22220 int i(float f, double d)
22222 we can tell after seeing the declaration for "f" that we
22223 are not looking at an initialization of a variable "i",
22224 but rather at the declaration of a function "i".
22226 Due to the fact that the parsing of template arguments
22227 (as specified to a template-id) requires backtracking we
22228 cannot use this technique when inside a template argument
22230 if (!parser
->in_template_argument_list_p
22231 && !parser
->in_type_id_in_expr_p
22232 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22233 /* However, a parameter-declaration of the form
22234 "float(f)" (which is a valid declaration of a
22235 parameter "f") can also be interpreted as an
22236 expression (the conversion of "f" to "float"). */
22237 && !parenthesized_p
)
22238 cp_parser_commit_to_tentative_parse (parser
);
22242 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22243 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22244 cp_parser_skip_to_closing_parenthesis (parser
,
22245 /*recovering=*/true,
22246 /*or_comma=*/false,
22247 /*consume_paren=*/false);
22252 parser
->in_unbraced_linkage_specification_p
22253 = saved_in_unbraced_linkage_specification_p
;
22255 /* Reset implicit_template_scope if we are about to leave the function
22256 parameter list that introduced it. Note that for out-of-line member
22257 definitions, there will be one or more class scopes before we get to
22258 the template parameter scope. */
22260 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22261 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22263 while (maybe_its
->kind
== sk_class
)
22264 maybe_its
= maybe_its
->level_chain
;
22265 if (maybe_its
== its
)
22267 parser
->implicit_template_parms
= 0;
22268 parser
->implicit_template_scope
= 0;
22275 /* Parse a parameter declaration.
22277 parameter-declaration:
22278 decl-specifier-seq ... [opt] declarator
22279 decl-specifier-seq declarator = assignment-expression
22280 decl-specifier-seq ... [opt] abstract-declarator [opt]
22281 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22283 The parser flags FLAGS is used to control type-specifier parsing.
22285 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22286 declares a template parameter. (In that case, a non-nested `>'
22287 token encountered during the parsing of the assignment-expression
22288 is not interpreted as a greater-than operator.)
22290 Returns a representation of the parameter, or NULL if an error
22291 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22292 true iff the declarator is of the form "(p)". */
22294 static cp_parameter_declarator
*
22295 cp_parser_parameter_declaration (cp_parser
*parser
,
22296 cp_parser_flags flags
,
22297 bool template_parm_p
,
22298 bool *parenthesized_p
)
22300 int declares_class_or_enum
;
22301 cp_decl_specifier_seq decl_specifiers
;
22302 cp_declarator
*declarator
;
22303 tree default_argument
;
22304 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22305 const char *saved_message
;
22306 bool template_parameter_pack_p
= false;
22308 /* In a template parameter, `>' is not an operator.
22312 When parsing a default template-argument for a non-type
22313 template-parameter, the first non-nested `>' is taken as the end
22314 of the template parameter-list rather than a greater-than
22317 /* Type definitions may not appear in parameter types. */
22318 saved_message
= parser
->type_definition_forbidden_message
;
22319 parser
->type_definition_forbidden_message
22320 = G_("types may not be defined in parameter types");
22322 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22323 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22324 (current_template_parms
)) : 0);
22326 /* Parse the declaration-specifiers. */
22327 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22328 cp_parser_decl_specifier_seq (parser
,
22331 &declares_class_or_enum
);
22333 /* Complain about missing 'typename' or other invalid type names. */
22334 if (!decl_specifiers
.any_type_specifiers_p
22335 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22336 decl_specifiers
.type
= error_mark_node
;
22338 /* If an error occurred, there's no reason to attempt to parse the
22339 rest of the declaration. */
22340 if (cp_parser_error_occurred (parser
))
22342 parser
->type_definition_forbidden_message
= saved_message
;
22346 /* Peek at the next token. */
22347 token
= cp_lexer_peek_token (parser
->lexer
);
22349 /* If the next token is a `)', `,', `=', `>', or `...', then there
22350 is no declarator. However, when variadic templates are enabled,
22351 there may be a declarator following `...'. */
22352 if (token
->type
== CPP_CLOSE_PAREN
22353 || token
->type
== CPP_COMMA
22354 || token
->type
== CPP_EQ
22355 || token
->type
== CPP_GREATER
)
22358 if (parenthesized_p
)
22359 *parenthesized_p
= false;
22361 /* Otherwise, there should be a declarator. */
22364 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22365 parser
->default_arg_ok_p
= false;
22367 /* After seeing a decl-specifier-seq, if the next token is not a
22368 "(", there is no possibility that the code is a valid
22369 expression. Therefore, if parsing tentatively, we commit at
22371 if (!parser
->in_template_argument_list_p
22372 /* In an expression context, having seen:
22376 we cannot be sure whether we are looking at a
22377 function-type (taking a "char" as a parameter) or a cast
22378 of some object of type "char" to "int". */
22379 && !parser
->in_type_id_in_expr_p
22380 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22381 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22382 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22383 cp_parser_commit_to_tentative_parse (parser
);
22384 /* Parse the declarator. */
22385 declarator_token_start
= token
;
22386 declarator
= cp_parser_declarator (parser
,
22387 CP_PARSER_DECLARATOR_EITHER
,
22388 CP_PARSER_FLAGS_NONE
,
22389 /*ctor_dtor_or_conv_p=*/NULL
,
22391 /*member_p=*/false,
22392 /*friend_p=*/false,
22393 /*static_p=*/false);
22394 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22395 /* After the declarator, allow more attributes. */
22396 decl_specifiers
.attributes
22397 = attr_chainon (decl_specifiers
.attributes
,
22398 cp_parser_attributes_opt (parser
));
22400 /* If the declarator is a template parameter pack, remember that and
22401 clear the flag in the declarator itself so we don't get errors
22402 from grokdeclarator. */
22403 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22405 declarator
->parameter_pack_p
= false;
22406 template_parameter_pack_p
= true;
22410 /* If the next token is an ellipsis, and we have not seen a declarator
22411 name, and if either the type of the declarator contains parameter
22412 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22413 for, eg, abbreviated integral type names), then we actually have a
22414 parameter pack expansion expression. Otherwise, leave the ellipsis
22415 for a C-style variadic function. */
22416 token
= cp_lexer_peek_token (parser
->lexer
);
22418 /* If a function parameter pack was specified and an implicit template
22419 parameter was introduced during cp_parser_parameter_declaration,
22420 change any implicit parameters introduced into packs. */
22421 if (parser
->implicit_template_parms
22422 && ((token
->type
== CPP_ELLIPSIS
22423 && declarator_can_be_parameter_pack (declarator
))
22424 || (declarator
&& declarator
->parameter_pack_p
)))
22426 int latest_template_parm_idx
= TREE_VEC_LENGTH
22427 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22429 if (latest_template_parm_idx
!= template_parm_idx
)
22430 decl_specifiers
.type
= convert_generic_types_to_packs
22431 (decl_specifiers
.type
,
22432 template_parm_idx
, latest_template_parm_idx
);
22435 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22437 tree type
= decl_specifiers
.type
;
22439 if (type
&& DECL_P (type
))
22440 type
= TREE_TYPE (type
);
22443 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22444 && (template_parm_p
|| uses_parameter_packs (type
)))
22445 || (!type
&& template_parm_p
))
22446 && declarator_can_be_parameter_pack (declarator
))
22448 /* Consume the `...'. */
22449 cp_lexer_consume_token (parser
->lexer
);
22450 maybe_warn_variadic_templates ();
22452 /* Build a pack expansion type */
22453 if (template_parm_p
)
22454 template_parameter_pack_p
= true;
22455 else if (declarator
)
22456 declarator
->parameter_pack_p
= true;
22458 decl_specifiers
.type
= make_pack_expansion (type
);
22462 /* The restriction on defining new types applies only to the type
22463 of the parameter, not to the default argument. */
22464 parser
->type_definition_forbidden_message
= saved_message
;
22466 /* If the next token is `=', then process a default argument. */
22467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22469 tree type
= decl_specifiers
.type
;
22470 token
= cp_lexer_peek_token (parser
->lexer
);
22471 /* If we are defining a class, then the tokens that make up the
22472 default argument must be saved and processed later. */
22473 if (!template_parm_p
&& at_class_scope_p ()
22474 && TYPE_BEING_DEFINED (current_class_type
)
22475 && !LAMBDA_TYPE_P (current_class_type
))
22476 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22478 // A constrained-type-specifier may declare a type template-parameter.
22479 else if (declares_constrained_type_template_parameter (type
))
22481 = cp_parser_default_type_template_argument (parser
);
22483 // A constrained-type-specifier may declare a template-template-parameter.
22484 else if (declares_constrained_template_template_parameter (type
))
22486 = cp_parser_default_template_template_argument (parser
);
22488 /* Outside of a class definition, we can just parse the
22489 assignment-expression. */
22492 = cp_parser_default_argument (parser
, template_parm_p
);
22494 if (!parser
->default_arg_ok_p
)
22496 permerror (token
->location
,
22497 "default arguments are only "
22498 "permitted for function parameters");
22500 else if ((declarator
&& declarator
->parameter_pack_p
)
22501 || template_parameter_pack_p
22502 || (decl_specifiers
.type
22503 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22505 /* Find the name of the parameter pack. */
22506 cp_declarator
*id_declarator
= declarator
;
22507 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22508 id_declarator
= id_declarator
->declarator
;
22510 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22511 error_at (declarator_token_start
->location
,
22513 ? G_("template parameter pack %qD "
22514 "cannot have a default argument")
22515 : G_("parameter pack %qD cannot have "
22516 "a default argument"),
22517 id_declarator
->u
.id
.unqualified_name
);
22519 error_at (declarator_token_start
->location
,
22521 ? G_("template parameter pack cannot have "
22522 "a default argument")
22523 : G_("parameter pack cannot have a "
22524 "default argument"));
22526 default_argument
= NULL_TREE
;
22530 default_argument
= NULL_TREE
;
22532 if (default_argument
)
22533 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
22535 /* Generate a location for the parameter, ranging from the start of the
22536 initial token to the end of the final token (using input_location for
22537 the latter, set up by cp_lexer_set_source_position_from_token when
22540 If we have a identifier, then use it for the caret location, e.g.
22542 extern int callee (int one, int (*two)(int, int), float three);
22543 ~~~~~~^~~~~~~~~~~~~~
22545 otherwise, reuse the start location for the caret location e.g.:
22547 extern int callee (int one, int (*)(int, int), float three);
22551 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22552 ? declarator
->id_loc
22553 : decl_spec_token_start
->location
);
22554 location_t param_loc
= make_location (caret_loc
,
22555 decl_spec_token_start
->location
,
22558 return make_parameter_declarator (&decl_specifiers
,
22562 template_parameter_pack_p
);
22565 /* Parse a default argument and return it.
22567 TEMPLATE_PARM_P is true if this is a default argument for a
22568 non-type template parameter. */
22570 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22572 tree default_argument
= NULL_TREE
;
22573 bool saved_greater_than_is_operator_p
;
22574 unsigned char saved_local_variables_forbidden_p
;
22575 bool non_constant_p
, is_direct_init
;
22577 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22579 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22580 parser
->greater_than_is_operator_p
= !template_parm_p
;
22581 /* Local variable names (and the `this' keyword) may not
22582 appear in a default argument. */
22583 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22584 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
22585 /* Parse the assignment-expression. */
22586 if (template_parm_p
)
22587 push_deferring_access_checks (dk_no_deferred
);
22588 tree saved_class_ptr
= NULL_TREE
;
22589 tree saved_class_ref
= NULL_TREE
;
22590 /* The "this" pointer is not valid in a default argument. */
22593 saved_class_ptr
= current_class_ptr
;
22594 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22595 saved_class_ref
= current_class_ref
;
22596 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22599 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22600 /* Restore the "this" pointer. */
22603 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22604 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22606 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22607 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22608 if (template_parm_p
)
22609 pop_deferring_access_checks ();
22610 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22611 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22613 return default_argument
;
22616 /* Parse a function-body.
22619 compound_statement */
22622 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22624 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22625 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22629 /* Parse a ctor-initializer-opt followed by a function-body. Return
22630 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22631 is true we are parsing a function-try-block. */
22634 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22635 bool in_function_try_block
)
22638 const bool check_body_p
22639 = (DECL_CONSTRUCTOR_P (current_function_decl
)
22640 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
22643 if (in_function_try_block
22644 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
22645 && cxx_dialect
< cxx2a
)
22647 if (DECL_CONSTRUCTOR_P (current_function_decl
))
22648 pedwarn (input_location
, 0,
22649 "function-try-block body of %<constexpr%> constructor only "
22650 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22652 pedwarn (input_location
, 0,
22653 "function-try-block body of %<constexpr%> function only "
22654 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22657 /* Begin the function body. */
22658 body
= begin_function_body ();
22659 /* Parse the optional ctor-initializer. */
22660 cp_parser_ctor_initializer_opt (parser
);
22662 /* If we're parsing a constexpr constructor definition, we need
22663 to check that the constructor body is indeed empty. However,
22664 before we get to cp_parser_function_body lot of junk has been
22665 generated, so we can't just check that we have an empty block.
22666 Rather we take a snapshot of the outermost block, and check whether
22667 cp_parser_function_body changed its state. */
22670 list
= cur_stmt_list
;
22671 if (STATEMENT_LIST_TAIL (list
))
22672 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22674 /* Parse the function-body. */
22675 cp_parser_function_body (parser
, in_function_try_block
);
22677 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22678 /* Finish the function body. */
22679 finish_function_body (body
);
22682 /* Parse an initializer.
22685 = initializer-clause
22686 ( expression-list )
22688 Returns an expression representing the initializer. If no
22689 initializer is present, NULL_TREE is returned.
22691 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22692 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22693 set to TRUE if there is no initializer present. If there is an
22694 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22695 is set to true; otherwise it is set to false. */
22698 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22699 bool* non_constant_p
, bool subexpression_p
)
22704 /* Peek at the next token. */
22705 token
= cp_lexer_peek_token (parser
->lexer
);
22707 /* Let our caller know whether or not this initializer was
22709 *is_direct_init
= (token
->type
!= CPP_EQ
);
22710 /* Assume that the initializer is constant. */
22711 *non_constant_p
= false;
22713 if (token
->type
== CPP_EQ
)
22715 /* Consume the `='. */
22716 cp_lexer_consume_token (parser
->lexer
);
22717 /* Parse the initializer-clause. */
22718 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22720 else if (token
->type
== CPP_OPEN_PAREN
)
22722 vec
<tree
, va_gc
> *vec
;
22723 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22725 /*allow_expansion_p=*/true,
22728 return error_mark_node
;
22729 init
= build_tree_list_vec (vec
);
22730 release_tree_vector (vec
);
22732 else if (token
->type
== CPP_OPEN_BRACE
)
22734 cp_lexer_set_source_position (parser
->lexer
);
22735 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22736 init
= cp_parser_braced_list (parser
, non_constant_p
);
22737 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22741 /* Anything else is an error. */
22742 cp_parser_error (parser
, "expected initializer");
22743 init
= error_mark_node
;
22746 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22747 init
= error_mark_node
;
22752 /* Parse an initializer-clause.
22754 initializer-clause:
22755 assignment-expression
22758 Returns an expression representing the initializer.
22760 If the `assignment-expression' production is used the value
22761 returned is simply a representation for the expression.
22763 Otherwise, calls cp_parser_braced_list. */
22766 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22768 cp_expr initializer
;
22770 /* Assume the expression is constant. */
22771 *non_constant_p
= false;
22773 /* If it is not a `{', then we are looking at an
22774 assignment-expression. */
22775 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22778 = cp_parser_constant_expression (parser
,
22779 /*allow_non_constant_p=*/true,
22783 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22785 return initializer
;
22788 /* Parse a brace-enclosed initializer list.
22791 { initializer-list , [opt] }
22792 { designated-initializer-list , [opt] }
22795 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22796 the elements of the initializer-list (or NULL, if the last
22797 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22798 NULL_TREE. There is no way to detect whether or not the optional
22799 trailing `,' was provided. NON_CONSTANT_P is as for
22800 cp_parser_initializer. */
22803 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22806 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22808 /* Consume the `{' token. */
22809 matching_braces braces
;
22810 braces
.require_open (parser
);
22811 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22812 initializer
= make_node (CONSTRUCTOR
);
22813 /* If it's not a `}', then there is a non-trivial initializer. */
22814 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22817 /* Parse the initializer list. */
22818 CONSTRUCTOR_ELTS (initializer
)
22819 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
22820 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
22821 /* A trailing `,' token is allowed. */
22822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22823 cp_lexer_consume_token (parser
->lexer
);
22826 *non_constant_p
= false;
22827 /* Now, there should be a trailing `}'. */
22828 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22829 braces
.require_close (parser
);
22830 TREE_TYPE (initializer
) = init_list_type_node
;
22832 cp_expr
result (initializer
);
22833 /* Build a location of the form:
22836 with caret==start at the open brace, finish at the close brace. */
22837 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22838 result
.set_location (combined_loc
);
22842 /* Consume tokens up to, and including, the next non-nested closing `]'.
22843 Returns true iff we found a closing `]'. */
22846 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22848 unsigned square_depth
= 0;
22852 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22854 switch (token
->type
)
22856 case CPP_PRAGMA_EOL
:
22857 if (!parser
->lexer
->in_pragma
)
22861 /* If we've run out of tokens, then there is no closing `]'. */
22864 case CPP_OPEN_SQUARE
:
22868 case CPP_CLOSE_SQUARE
:
22869 if (!square_depth
--)
22871 cp_lexer_consume_token (parser
->lexer
);
22880 /* Consume the token. */
22881 cp_lexer_consume_token (parser
->lexer
);
22885 /* Return true if we are looking at an array-designator, false otherwise. */
22888 cp_parser_array_designator_p (cp_parser
*parser
)
22890 /* Consume the `['. */
22891 cp_lexer_consume_token (parser
->lexer
);
22893 cp_lexer_save_tokens (parser
->lexer
);
22895 /* Skip tokens until the next token is a closing square bracket.
22896 If we find the closing `]', and the next token is a `=', then
22897 we are looking at an array designator. */
22898 bool array_designator_p
22899 = (cp_parser_skip_to_closing_square_bracket (parser
)
22900 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22902 /* Roll back the tokens we skipped. */
22903 cp_lexer_rollback_tokens (parser
->lexer
);
22905 return array_designator_p
;
22908 /* Parse an initializer-list.
22911 initializer-clause ... [opt]
22912 initializer-list , initializer-clause ... [opt]
22916 designated-initializer-list:
22917 designated-initializer-clause
22918 designated-initializer-list , designated-initializer-clause
22920 designated-initializer-clause:
22921 designator brace-or-equal-initializer
22929 designation initializer-clause ...[opt]
22930 initializer-list , designation initializer-clause ...[opt]
22935 [ constant-expression ] =
22937 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22938 for the initializer. If the INDEX of the elt is non-NULL, it is the
22939 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22940 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22941 are any designators. */
22943 static vec
<constructor_elt
, va_gc
> *
22944 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
22947 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22948 bool first_p
= true;
22949 tree first_designator
= NULL_TREE
;
22951 /* Assume all of the expressions are constant. */
22952 *non_constant_p
= false;
22954 /* Parse the rest of the list. */
22960 bool clause_non_constant_p
;
22961 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22963 /* Handle the C++2A syntax, '. id ='. */
22964 if ((cxx_dialect
>= cxx2a
22965 || cp_parser_allow_gnu_extensions_p (parser
))
22966 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22967 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22968 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22969 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22970 == CPP_OPEN_BRACE
)))
22972 if (cxx_dialect
< cxx2a
)
22973 pedwarn (loc
, OPT_Wpedantic
,
22974 "C++ designated initializers only available with "
22975 "%<-std=c++2a%> or %<-std=gnu++2a%>");
22976 /* Consume the `.'. */
22977 cp_lexer_consume_token (parser
->lexer
);
22978 /* Consume the identifier. */
22979 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22981 /* Consume the `='. */
22982 cp_lexer_consume_token (parser
->lexer
);
22984 /* Also, if the next token is an identifier and the following one is a
22985 colon, we are looking at the GNU designated-initializer
22987 else if (cp_parser_allow_gnu_extensions_p (parser
)
22988 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22989 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22992 /* Warn the user that they are using an extension. */
22993 pedwarn (loc
, OPT_Wpedantic
,
22994 "ISO C++ does not allow GNU designated initializers");
22995 /* Consume the identifier. */
22996 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22997 /* Consume the `:'. */
22998 cp_lexer_consume_token (parser
->lexer
);
23000 /* Also handle C99 array designators, '[ const ] ='. */
23001 else if (cp_parser_allow_gnu_extensions_p (parser
)
23002 && !c_dialect_objc ()
23003 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
23005 /* In C++11, [ could start a lambda-introducer. */
23006 bool non_const
= false;
23008 cp_parser_parse_tentatively (parser
);
23010 if (!cp_parser_array_designator_p (parser
))
23012 cp_parser_simulate_error (parser
);
23013 designator
= NULL_TREE
;
23017 designator
= cp_parser_constant_expression (parser
, true,
23019 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23020 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23023 if (!cp_parser_parse_definitely (parser
))
23024 designator
= NULL_TREE
;
23026 && (!require_potential_rvalue_constant_expression
23028 designator
= NULL_TREE
;
23030 /* Warn the user that they are using an extension. */
23031 pedwarn (loc
, OPT_Wpedantic
,
23032 "ISO C++ does not allow C99 designated initializers");
23035 designator
= NULL_TREE
;
23039 first_designator
= designator
;
23042 else if (cxx_dialect
>= cxx2a
23043 && first_designator
!= error_mark_node
23044 && (!first_designator
!= !designator
))
23046 error_at (loc
, "either all initializer clauses should be designated "
23047 "or none of them should be");
23048 first_designator
= error_mark_node
;
23050 else if (cxx_dialect
< cxx2a
&& !first_designator
)
23051 first_designator
= designator
;
23053 /* Parse the initializer. */
23054 initializer
= cp_parser_initializer_clause (parser
,
23055 &clause_non_constant_p
);
23056 /* If any clause is non-constant, so is the entire initializer. */
23057 if (clause_non_constant_p
)
23058 *non_constant_p
= true;
23060 /* If we have an ellipsis, this is an initializer pack
23062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23064 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23066 /* Consume the `...'. */
23067 cp_lexer_consume_token (parser
->lexer
);
23069 if (designator
&& cxx_dialect
>= cxx2a
)
23071 "%<...%> not allowed in designated initializer list");
23073 /* Turn the initializer into an initializer expansion. */
23074 initializer
= make_pack_expansion (initializer
);
23077 /* Add it to the vector. */
23078 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
23080 /* If the next token is not a comma, we have reached the end of
23082 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23085 /* Peek at the next token. */
23086 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23087 /* If the next token is a `}', then we're still done. An
23088 initializer-clause can have a trailing `,' after the
23089 initializer-list and before the closing `}'. */
23090 if (token
->type
== CPP_CLOSE_BRACE
)
23093 /* Consume the `,' token. */
23094 cp_lexer_consume_token (parser
->lexer
);
23097 /* The same identifier shall not appear in multiple designators
23098 of a designated-initializer-list. */
23099 if (first_designator
)
23102 tree designator
, val
;
23103 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23104 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23106 if (IDENTIFIER_MARKED (designator
))
23108 error_at (cp_expr_loc_or_loc (val
, input_location
),
23109 "%<.%s%> designator used multiple times in "
23110 "the same initializer list",
23111 IDENTIFIER_POINTER (designator
));
23112 (*v
)[i
].index
= error_mark_node
;
23115 IDENTIFIER_MARKED (designator
) = 1;
23117 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23118 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23119 IDENTIFIER_MARKED (designator
) = 0;
23122 *designated
= first_designator
!= NULL_TREE
;
23126 /* Classes [gram.class] */
23128 /* Parse a class-name.
23134 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23135 to indicate that names looked up in dependent types should be
23136 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23137 keyword has been used to indicate that the name that appears next
23138 is a template. TAG_TYPE indicates the explicit tag given before
23139 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23140 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23141 is the class being defined in a class-head. If ENUM_OK is TRUE,
23142 enum-names are also accepted.
23144 Returns the TYPE_DECL representing the class. */
23147 cp_parser_class_name (cp_parser
*parser
,
23148 bool typename_keyword_p
,
23149 bool template_keyword_p
,
23150 enum tag_types tag_type
,
23151 bool check_dependency_p
,
23153 bool is_declaration
,
23160 tree identifier
= NULL_TREE
;
23162 /* All class-names start with an identifier. */
23163 token
= cp_lexer_peek_token (parser
->lexer
);
23164 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23166 cp_parser_error (parser
, "expected class-name");
23167 return error_mark_node
;
23170 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23171 to a template-id, so we save it here. */
23172 scope
= parser
->scope
;
23173 if (scope
== error_mark_node
)
23174 return error_mark_node
;
23176 /* Any name names a type if we're following the `typename' keyword
23177 in a qualified name where the enclosing scope is type-dependent. */
23178 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
23179 && dependent_type_p (scope
));
23180 /* Handle the common case (an identifier, but not a template-id)
23182 if (token
->type
== CPP_NAME
23183 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
23185 cp_token
*identifier_token
;
23188 /* Look for the identifier. */
23189 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
23190 ambiguous_p
= identifier_token
->error_reported
;
23191 identifier
= cp_parser_identifier (parser
);
23192 /* If the next token isn't an identifier, we are certainly not
23193 looking at a class-name. */
23194 if (identifier
== error_mark_node
)
23195 decl
= error_mark_node
;
23196 /* If we know this is a type-name, there's no need to look it
23198 else if (typename_p
)
23202 tree ambiguous_decls
;
23203 /* If we already know that this lookup is ambiguous, then
23204 we've already issued an error message; there's no reason
23208 cp_parser_simulate_error (parser
);
23209 return error_mark_node
;
23211 /* If the next token is a `::', then the name must be a type
23214 [basic.lookup.qual]
23216 During the lookup for a name preceding the :: scope
23217 resolution operator, object, function, and enumerator
23218 names are ignored. */
23219 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23220 tag_type
= scope_type
;
23221 /* Look up the name. */
23222 decl
= cp_parser_lookup_name (parser
, identifier
,
23224 /*is_template=*/false,
23225 /*is_namespace=*/false,
23226 check_dependency_p
,
23228 identifier_token
->location
);
23229 if (ambiguous_decls
)
23231 if (cp_parser_parsing_tentatively (parser
))
23232 cp_parser_simulate_error (parser
);
23233 return error_mark_node
;
23239 /* Try a template-id. */
23240 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23241 check_dependency_p
,
23244 if (decl
== error_mark_node
)
23245 return error_mark_node
;
23248 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23250 /* If this is a typename, create a TYPENAME_TYPE. */
23252 && decl
!= error_mark_node
23253 && !is_overloaded_fn (decl
))
23255 decl
= make_typename_type (scope
, decl
, typename_type
,
23256 /*complain=*/tf_error
);
23257 if (decl
!= error_mark_node
)
23258 decl
= TYPE_NAME (decl
);
23261 decl
= strip_using_decl (decl
);
23263 /* Check to see that it is really the name of a class. */
23264 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23265 && identifier_p (TREE_OPERAND (decl
, 0))
23266 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23267 /* Situations like this:
23269 template <typename T> struct A {
23270 typename T::template X<int>::I i;
23273 are problematic. Is `T::template X<int>' a class-name? The
23274 standard does not seem to be definitive, but there is no other
23275 valid interpretation of the following `::'. Therefore, those
23276 names are considered class-names. */
23278 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23279 if (decl
!= error_mark_node
)
23280 decl
= TYPE_NAME (decl
);
23282 else if (TREE_CODE (decl
) != TYPE_DECL
23283 || TREE_TYPE (decl
) == error_mark_node
23284 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23285 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23286 /* In Objective-C 2.0, a classname followed by '.' starts a
23287 dot-syntax expression, and it's not a type-name. */
23288 || (c_dialect_objc ()
23289 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23290 && objc_is_class_name (decl
)))
23291 decl
= error_mark_node
;
23293 if (decl
== error_mark_node
)
23294 cp_parser_error (parser
, "expected class-name");
23295 else if (identifier
&& !parser
->scope
)
23296 maybe_note_name_used_in_class (identifier
, decl
);
23301 /* Parse a class-specifier.
23304 class-head { member-specification [opt] }
23306 Returns the TREE_TYPE representing the class. */
23309 cp_parser_class_specifier_1 (cp_parser
* parser
)
23312 tree attributes
= NULL_TREE
;
23313 bool nested_name_specifier_p
;
23314 unsigned saved_num_template_parameter_lists
;
23315 bool saved_in_function_body
;
23316 unsigned char in_statement
;
23317 bool in_switch_statement_p
;
23318 bool saved_in_unbraced_linkage_specification_p
;
23319 tree old_scope
= NULL_TREE
;
23320 tree scope
= NULL_TREE
;
23321 cp_token
*closing_brace
;
23323 push_deferring_access_checks (dk_no_deferred
);
23325 /* Parse the class-head. */
23326 type
= cp_parser_class_head (parser
,
23327 &nested_name_specifier_p
);
23328 /* If the class-head was a semantic disaster, skip the entire body
23332 cp_parser_skip_to_end_of_block_or_statement (parser
);
23333 pop_deferring_access_checks ();
23334 return error_mark_node
;
23337 /* Look for the `{'. */
23338 matching_braces braces
;
23339 if (!braces
.require_open (parser
))
23341 pop_deferring_access_checks ();
23342 return error_mark_node
;
23345 cp_ensure_no_omp_declare_simd (parser
);
23346 cp_ensure_no_oacc_routine (parser
);
23348 /* Issue an error message if type-definitions are forbidden here. */
23349 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
23350 /* Remember that we are defining one more class. */
23351 ++parser
->num_classes_being_defined
;
23352 /* Inside the class, surrounding template-parameter-lists do not
23354 saved_num_template_parameter_lists
23355 = parser
->num_template_parameter_lists
;
23356 parser
->num_template_parameter_lists
= 0;
23357 /* We are not in a function body. */
23358 saved_in_function_body
= parser
->in_function_body
;
23359 parser
->in_function_body
= false;
23360 /* Or in a loop. */
23361 in_statement
= parser
->in_statement
;
23362 parser
->in_statement
= 0;
23363 /* Or in a switch. */
23364 in_switch_statement_p
= parser
->in_switch_statement_p
;
23365 parser
->in_switch_statement_p
= false;
23366 /* We are not immediately inside an extern "lang" block. */
23367 saved_in_unbraced_linkage_specification_p
23368 = parser
->in_unbraced_linkage_specification_p
;
23369 parser
->in_unbraced_linkage_specification_p
= false;
23371 // Associate constraints with the type.
23373 type
= associate_classtype_constraints (type
);
23375 /* Start the class. */
23376 if (nested_name_specifier_p
)
23378 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23379 old_scope
= push_inner_scope (scope
);
23381 type
= begin_class_definition (type
);
23383 if (type
== error_mark_node
)
23384 /* If the type is erroneous, skip the entire body of the class. */
23385 cp_parser_skip_to_closing_brace (parser
);
23387 /* Parse the member-specification. */
23388 cp_parser_member_specification_opt (parser
);
23390 /* Look for the trailing `}'. */
23391 closing_brace
= braces
.require_close (parser
);
23392 /* Look for trailing attributes to apply to this class. */
23393 if (cp_parser_allow_gnu_extensions_p (parser
))
23394 attributes
= cp_parser_gnu_attributes_opt (parser
);
23395 if (type
!= error_mark_node
)
23396 type
= finish_struct (type
, attributes
);
23397 if (nested_name_specifier_p
)
23398 pop_inner_scope (old_scope
, scope
);
23400 /* We've finished a type definition. Check for the common syntax
23401 error of forgetting a semicolon after the definition. We need to
23402 be careful, as we can't just check for not-a-semicolon and be done
23403 with it; the user might have typed:
23405 class X { } c = ...;
23406 class X { } *p = ...;
23408 and so forth. Instead, enumerate all the possible tokens that
23409 might follow this production; if we don't see one of them, then
23410 complain and silently insert the semicolon. */
23412 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23413 bool want_semicolon
= true;
23415 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23416 /* Don't try to parse c++11 attributes here. As per the
23417 grammar, that should be a task for
23418 cp_parser_decl_specifier_seq. */
23419 want_semicolon
= false;
23421 switch (token
->type
)
23424 case CPP_SEMICOLON
:
23427 case CPP_OPEN_PAREN
:
23428 case CPP_CLOSE_PAREN
:
23430 want_semicolon
= false;
23433 /* While it's legal for type qualifiers and storage class
23434 specifiers to follow type definitions in the grammar, only
23435 compiler testsuites contain code like that. Assume that if
23436 we see such code, then what we're really seeing is a case
23440 const <type> var = ...;
23445 static <type> func (...) ...
23447 i.e. the qualifier or specifier applies to the next
23448 declaration. To do so, however, we need to look ahead one
23449 more token to see if *that* token is a type specifier.
23451 This code could be improved to handle:
23454 static const <type> var = ...; */
23456 if (keyword_is_decl_specifier (token
->keyword
))
23458 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23460 /* Handling user-defined types here would be nice, but very
23463 = (lookahead
->type
== CPP_KEYWORD
23464 && keyword_begins_type_specifier (lookahead
->keyword
));
23471 /* If we don't have a type, then something is very wrong and we
23472 shouldn't try to do anything clever. Likewise for not seeing the
23474 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23476 /* Locate the closing brace. */
23477 cp_token_position prev
23478 = cp_lexer_previous_token_position (parser
->lexer
);
23479 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23480 location_t loc
= prev_token
->location
;
23482 /* We want to suggest insertion of a ';' immediately *after* the
23483 closing brace, so, if we can, offset the location by 1 column. */
23484 location_t next_loc
= loc
;
23485 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23486 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23488 rich_location
richloc (line_table
, next_loc
);
23490 /* If we successfully offset the location, suggest the fix-it. */
23491 if (next_loc
!= loc
)
23492 richloc
.add_fixit_insert_before (next_loc
, ";");
23494 if (CLASSTYPE_DECLARED_CLASS (type
))
23495 error_at (&richloc
,
23496 "expected %<;%> after class definition");
23497 else if (TREE_CODE (type
) == RECORD_TYPE
)
23498 error_at (&richloc
,
23499 "expected %<;%> after struct definition");
23500 else if (TREE_CODE (type
) == UNION_TYPE
)
23501 error_at (&richloc
,
23502 "expected %<;%> after union definition");
23504 gcc_unreachable ();
23506 /* Unget one token and smash it to look as though we encountered
23507 a semicolon in the input stream. */
23508 cp_lexer_set_token_position (parser
->lexer
, prev
);
23509 token
= cp_lexer_peek_token (parser
->lexer
);
23510 token
->type
= CPP_SEMICOLON
;
23511 token
->keyword
= RID_MAX
;
23515 /* If this class is not itself within the scope of another class,
23516 then we need to parse the bodies of all of the queued function
23517 definitions. Note that the queued functions defined in a class
23518 are not always processed immediately following the
23519 class-specifier for that class. Consider:
23522 struct B { void f() { sizeof (A); } };
23525 If `f' were processed before the processing of `A' were
23526 completed, there would be no way to compute the size of `A'.
23527 Note that the nesting we are interested in here is lexical --
23528 not the semantic nesting given by TYPE_CONTEXT. In particular,
23531 struct A { struct B; };
23532 struct A::B { void f() { } };
23534 there is no need to delay the parsing of `A::B::f'. */
23535 if (--parser
->num_classes_being_defined
== 0)
23538 tree class_type
= NULL_TREE
;
23539 tree pushed_scope
= NULL_TREE
;
23541 cp_default_arg_entry
*e
;
23542 tree save_ccp
, save_ccr
;
23544 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
23546 /* Skip default arguments, NSDMIs, etc, in order to improve
23547 error recovery (c++/71169, c++/71832). */
23548 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23549 vec_safe_truncate (unparsed_nsdmis
, 0);
23550 vec_safe_truncate (unparsed_classes
, 0);
23551 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23554 /* In a first pass, parse default arguments to the functions.
23555 Then, in a second pass, parse the bodies of the functions.
23556 This two-phased approach handles cases like:
23564 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23567 /* If there are default arguments that have not yet been processed,
23568 take care of them now. */
23569 if (class_type
!= e
->class_type
)
23572 pop_scope (pushed_scope
);
23573 class_type
= e
->class_type
;
23574 pushed_scope
= push_scope (class_type
);
23576 /* Make sure that any template parameters are in scope. */
23577 maybe_begin_member_template_processing (decl
);
23578 /* Parse the default argument expressions. */
23579 cp_parser_late_parsing_default_args (parser
, decl
);
23580 /* Remove any template parameters from the symbol table. */
23581 maybe_end_member_template_processing ();
23583 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23584 /* Now parse any NSDMIs. */
23585 save_ccp
= current_class_ptr
;
23586 save_ccr
= current_class_ref
;
23587 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23589 if (class_type
!= DECL_CONTEXT (decl
))
23592 pop_scope (pushed_scope
);
23593 class_type
= DECL_CONTEXT (decl
);
23594 pushed_scope
= push_scope (class_type
);
23596 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23597 cp_parser_late_parsing_nsdmi (parser
, decl
);
23599 vec_safe_truncate (unparsed_nsdmis
, 0);
23600 current_class_ptr
= save_ccp
;
23601 current_class_ref
= save_ccr
;
23603 pop_scope (pushed_scope
);
23605 /* Now do some post-NSDMI bookkeeping. */
23606 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23607 after_nsdmi_defaulted_late_checks (class_type
);
23608 vec_safe_truncate (unparsed_classes
, 0);
23609 after_nsdmi_defaulted_late_checks (type
);
23611 /* Now parse the body of the functions. */
23614 /* OpenMP UDRs need to be parsed before all other functions. */
23615 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23616 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23617 cp_parser_late_parsing_for_member (parser
, decl
);
23618 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23619 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23620 cp_parser_late_parsing_for_member (parser
, decl
);
23623 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23624 cp_parser_late_parsing_for_member (parser
, decl
);
23625 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23628 vec_safe_push (unparsed_classes
, type
);
23630 /* Put back any saved access checks. */
23631 pop_deferring_access_checks ();
23633 /* Restore saved state. */
23634 parser
->in_switch_statement_p
= in_switch_statement_p
;
23635 parser
->in_statement
= in_statement
;
23636 parser
->in_function_body
= saved_in_function_body
;
23637 parser
->num_template_parameter_lists
23638 = saved_num_template_parameter_lists
;
23639 parser
->in_unbraced_linkage_specification_p
23640 = saved_in_unbraced_linkage_specification_p
;
23646 cp_parser_class_specifier (cp_parser
* parser
)
23649 timevar_push (TV_PARSE_STRUCT
);
23650 ret
= cp_parser_class_specifier_1 (parser
);
23651 timevar_pop (TV_PARSE_STRUCT
);
23655 /* Parse a class-head.
23658 class-key identifier [opt] base-clause [opt]
23659 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23660 class-key nested-name-specifier [opt] template-id
23663 class-virt-specifier:
23667 class-key attributes identifier [opt] base-clause [opt]
23668 class-key attributes nested-name-specifier identifier base-clause [opt]
23669 class-key attributes nested-name-specifier [opt] template-id
23672 Upon return BASES is initialized to the list of base classes (or
23673 NULL, if there are none) in the same form returned by
23674 cp_parser_base_clause.
23676 Returns the TYPE of the indicated class. Sets
23677 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23678 involving a nested-name-specifier was used, and FALSE otherwise.
23680 Returns error_mark_node if this is not a class-head.
23682 Returns NULL_TREE if the class-head is syntactically valid, but
23683 semantically invalid in a way that means we should skip the entire
23684 body of the class. */
23687 cp_parser_class_head (cp_parser
* parser
,
23688 bool* nested_name_specifier_p
)
23690 tree nested_name_specifier
;
23691 enum tag_types class_key
;
23692 tree id
= NULL_TREE
;
23693 tree type
= NULL_TREE
;
23696 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23697 bool template_id_p
= false;
23698 bool qualified_p
= false;
23699 bool invalid_nested_name_p
= false;
23700 bool invalid_explicit_specialization_p
= false;
23701 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23702 tree pushed_scope
= NULL_TREE
;
23703 unsigned num_templates
;
23704 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23705 /* Assume no nested-name-specifier will be present. */
23706 *nested_name_specifier_p
= false;
23707 /* Assume no template parameter lists will be used in defining the
23710 parser
->colon_corrects_to_scope_p
= false;
23712 /* Look for the class-key. */
23713 class_key
= cp_parser_class_key (parser
);
23714 if (class_key
== none_type
)
23715 return error_mark_node
;
23717 location_t class_head_start_location
= input_location
;
23719 /* Parse the attributes. */
23720 attributes
= cp_parser_attributes_opt (parser
);
23722 /* If the next token is `::', that is invalid -- but sometimes
23723 people do try to write:
23727 Handle this gracefully by accepting the extra qualifier, and then
23728 issuing an error about it later if this really is a
23729 class-head. If it turns out just to be an elaborated type
23730 specifier, remain silent. */
23731 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23732 qualified_p
= true;
23734 push_deferring_access_checks (dk_no_check
);
23736 /* Determine the name of the class. Begin by looking for an
23737 optional nested-name-specifier. */
23738 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23739 nested_name_specifier
23740 = cp_parser_nested_name_specifier_opt (parser
,
23741 /*typename_keyword_p=*/false,
23742 /*check_dependency_p=*/false,
23744 /*is_declaration=*/false);
23745 /* If there was a nested-name-specifier, then there *must* be an
23748 cp_token
*bad_template_keyword
= NULL
;
23750 if (nested_name_specifier
)
23752 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23753 /* Although the grammar says `identifier', it really means
23754 `class-name' or `template-name'. You are only allowed to
23755 define a class that has already been declared with this
23758 The proposed resolution for Core Issue 180 says that wherever
23759 you see `class T::X' you should treat `X' as a type-name.
23761 It is OK to define an inaccessible class; for example:
23763 class A { class B; };
23766 We do not know if we will see a class-name, or a
23767 template-name. We look for a class-name first, in case the
23768 class-name is a template-id; if we looked for the
23769 template-name first we would stop after the template-name. */
23770 cp_parser_parse_tentatively (parser
);
23771 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23772 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23773 type
= cp_parser_class_name (parser
,
23774 /*typename_keyword_p=*/false,
23775 /*template_keyword_p=*/false,
23777 /*check_dependency_p=*/false,
23778 /*class_head_p=*/true,
23779 /*is_declaration=*/false);
23780 /* If that didn't work, ignore the nested-name-specifier. */
23781 if (!cp_parser_parse_definitely (parser
))
23783 invalid_nested_name_p
= true;
23784 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23785 id
= cp_parser_identifier (parser
);
23786 if (id
== error_mark_node
)
23789 /* If we could not find a corresponding TYPE, treat this
23790 declaration like an unqualified declaration. */
23791 if (type
== error_mark_node
)
23792 nested_name_specifier
= NULL_TREE
;
23793 /* Otherwise, count the number of templates used in TYPE and its
23794 containing scopes. */
23796 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23798 /* Otherwise, the identifier is optional. */
23801 /* We don't know whether what comes next is a template-id,
23802 an identifier, or nothing at all. */
23803 cp_parser_parse_tentatively (parser
);
23804 /* Check for a template-id. */
23805 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23806 id
= cp_parser_template_id (parser
,
23807 /*template_keyword_p=*/false,
23808 /*check_dependency_p=*/true,
23810 /*is_declaration=*/true);
23811 /* If that didn't work, it could still be an identifier. */
23812 if (!cp_parser_parse_definitely (parser
))
23814 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23816 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23817 id
= cp_parser_identifier (parser
);
23824 template_id_p
= true;
23829 pop_deferring_access_checks ();
23833 cp_parser_check_for_invalid_template_id (parser
, id
,
23835 type_start_token
->location
);
23837 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23839 /* If it's not a `:' or a `{' then we can't really be looking at a
23840 class-head, since a class-head only appears as part of a
23841 class-specifier. We have to detect this situation before calling
23842 xref_tag, since that has irreversible side-effects. */
23843 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23845 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23846 type
= error_mark_node
;
23850 /* At this point, we're going ahead with the class-specifier, even
23851 if some other problem occurs. */
23852 cp_parser_commit_to_tentative_parse (parser
);
23853 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23855 cp_parser_error (parser
,
23856 "cannot specify %<override%> for a class");
23857 type
= error_mark_node
;
23860 /* Issue the error about the overly-qualified name now. */
23863 cp_parser_error (parser
,
23864 "global qualification of class name is invalid");
23865 type
= error_mark_node
;
23868 else if (invalid_nested_name_p
)
23870 cp_parser_error (parser
,
23871 "qualified name does not name a class");
23872 type
= error_mark_node
;
23875 else if (nested_name_specifier
)
23879 if (bad_template_keyword
)
23880 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23881 keyword template shall not appear at the top level. */
23882 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23883 "keyword %<template%> not allowed in class-head-name");
23885 /* Reject typedef-names in class heads. */
23886 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23888 error_at (type_start_token
->location
,
23889 "invalid class name in declaration of %qD",
23895 /* Figure out in what scope the declaration is being placed. */
23896 scope
= current_scope ();
23897 /* If that scope does not contain the scope in which the
23898 class was originally declared, the program is invalid. */
23899 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23901 if (at_namespace_scope_p ())
23902 error_at (type_start_token
->location
,
23903 "declaration of %qD in namespace %qD which does not "
23905 type
, scope
, nested_name_specifier
);
23907 error_at (type_start_token
->location
,
23908 "declaration of %qD in %qD which does not enclose %qD",
23909 type
, scope
, nested_name_specifier
);
23915 A declarator-id shall not be qualified except for the
23916 definition of a ... nested class outside of its class
23917 ... [or] the definition or explicit instantiation of a
23918 class member of a namespace outside of its namespace. */
23919 if (scope
== nested_name_specifier
)
23921 permerror (nested_name_specifier_token_start
->location
,
23922 "extra qualification not allowed");
23923 nested_name_specifier
= NULL_TREE
;
23927 /* An explicit-specialization must be preceded by "template <>". If
23928 it is not, try to recover gracefully. */
23929 if (at_namespace_scope_p ()
23930 && parser
->num_template_parameter_lists
== 0
23931 && !processing_template_parmlist
23934 /* Build a location of this form:
23935 struct typename <ARGS>
23936 ^~~~~~~~~~~~~~~~~~~~~~
23937 with caret==start at the start token, and
23938 finishing at the end of the type. */
23939 location_t reported_loc
23940 = make_location (class_head_start_location
,
23941 class_head_start_location
,
23942 get_finish (type_start_token
->location
));
23943 rich_location
richloc (line_table
, reported_loc
);
23944 richloc
.add_fixit_insert_before (class_head_start_location
,
23946 error_at (&richloc
,
23947 "an explicit specialization must be preceded by"
23948 " %<template <>%>");
23949 invalid_explicit_specialization_p
= true;
23950 /* Take the same action that would have been taken by
23951 cp_parser_explicit_specialization. */
23952 ++parser
->num_template_parameter_lists
;
23953 begin_specialization ();
23955 /* There must be no "return" statements between this point and the
23956 end of this function; set "type "to the correct return value and
23957 use "goto done;" to return. */
23958 /* Make sure that the right number of template parameters were
23960 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23962 type_start_token
->location
,
23963 /*declarator=*/NULL
))
23965 /* If something went wrong, there is no point in even trying to
23966 process the class-definition. */
23971 /* Look up the type. */
23974 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23975 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23976 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23978 error_at (type_start_token
->location
,
23979 "function template %qD redeclared as a class template", id
);
23980 type
= error_mark_node
;
23984 type
= TREE_TYPE (id
);
23985 type
= maybe_process_partial_specialization (type
);
23987 /* Check the scope while we still know whether or not we had a
23988 nested-name-specifier. */
23989 if (type
!= error_mark_node
)
23990 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23992 if (nested_name_specifier
)
23993 pushed_scope
= push_scope (nested_name_specifier
);
23995 else if (nested_name_specifier
)
24001 template <typename T> struct S { struct T };
24002 template <typename T> struct S<T>::T { };
24004 we will get a TYPENAME_TYPE when processing the definition of
24005 `S::T'. We need to resolve it to the actual type before we
24006 try to define it. */
24007 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
24009 class_type
= resolve_typename_type (TREE_TYPE (type
),
24010 /*only_current_p=*/false);
24011 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
24012 type
= TYPE_NAME (class_type
);
24015 cp_parser_error (parser
, "could not resolve typename type");
24016 type
= error_mark_node
;
24020 if (maybe_process_partial_specialization (TREE_TYPE (type
))
24021 == error_mark_node
)
24027 class_type
= current_class_type
;
24028 /* Enter the scope indicated by the nested-name-specifier. */
24029 pushed_scope
= push_scope (nested_name_specifier
);
24030 /* Get the canonical version of this type. */
24031 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
24032 /* Call push_template_decl if it seems like we should be defining a
24033 template either from the template headers or the type we're
24034 defining, so that we diagnose both extra and missing headers. */
24035 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24036 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
24037 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
24039 type
= push_template_decl (type
);
24040 if (type
== error_mark_node
)
24047 type
= TREE_TYPE (type
);
24048 *nested_name_specifier_p
= true;
24050 else /* The name is not a nested name. */
24052 /* If the class was unnamed, create a dummy name. */
24054 id
= make_anon_name ();
24055 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
24056 ? ts_within_enclosing_non_class
24058 type
= xref_tag (class_key
, id
, tag_scope
,
24059 parser
->num_template_parameter_lists
);
24062 /* Indicate whether this class was declared as a `class' or as a
24064 if (TREE_CODE (type
) == RECORD_TYPE
)
24065 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
24066 cp_parser_check_class_key (class_key
, type
);
24068 /* If this type was already complete, and we see another definition,
24069 that's an error. Likewise if the type is already being defined:
24070 this can happen, eg, when it's defined from within an expression
24072 if (type
!= error_mark_node
24073 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
24075 error_at (type_start_token
->location
, "redefinition of %q#T",
24077 inform (location_of (type
), "previous definition of %q#T",
24082 else if (type
== error_mark_node
)
24087 /* Apply attributes now, before any use of the class as a template
24088 argument in its base list. */
24089 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
24090 fixup_attribute_variants (type
);
24093 /* We will have entered the scope containing the class; the names of
24094 base classes should be looked up in that context. For example:
24096 struct A { struct B {}; struct C; };
24097 struct A::C : B {};
24101 /* Get the list of base-classes, if there is one. */
24102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
24104 /* PR59482: enter the class scope so that base-specifiers are looked
24108 bases
= cp_parser_base_clause (parser
);
24109 /* PR59482: get out of the previously pushed class scope so that the
24110 subsequent pops pop the right thing. */
24117 /* If we're really defining a class, process the base classes.
24118 If they're invalid, fail. */
24119 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24120 xref_basetypes (type
, bases
);
24123 /* Leave the scope given by the nested-name-specifier. We will
24124 enter the class scope itself while processing the members. */
24126 pop_scope (pushed_scope
);
24128 if (invalid_explicit_specialization_p
)
24130 end_specialization ();
24131 --parser
->num_template_parameter_lists
;
24135 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
24136 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
24137 CLASSTYPE_FINAL (type
) = 1;
24139 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24143 /* Parse a class-key.
24150 Returns the kind of class-key specified, or none_type to indicate
24153 static enum tag_types
24154 cp_parser_class_key (cp_parser
* parser
)
24157 enum tag_types tag_type
;
24159 /* Look for the class-key. */
24160 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
24164 /* Check to see if the TOKEN is a class-key. */
24165 tag_type
= cp_parser_token_is_class_key (token
);
24167 cp_parser_error (parser
, "expected class-key");
24171 /* Parse a type-parameter-key.
24173 type-parameter-key:
24179 cp_parser_type_parameter_key (cp_parser
* parser
)
24181 /* Look for the type-parameter-key. */
24182 enum tag_types tag_type
= none_type
;
24183 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24184 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
24186 cp_lexer_consume_token (parser
->lexer
);
24187 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
24188 /* typename is not allowed in a template template parameter
24189 by the standard until C++17. */
24190 pedwarn (token
->location
, OPT_Wpedantic
,
24191 "ISO C++ forbids typename key in template template parameter;"
24192 " use %<-std=c++17%> or %<-std=gnu++17%>");
24195 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
24200 /* Parse an (optional) member-specification.
24202 member-specification:
24203 member-declaration member-specification [opt]
24204 access-specifier : member-specification [opt] */
24207 cp_parser_member_specification_opt (cp_parser
* parser
)
24214 /* Peek at the next token. */
24215 token
= cp_lexer_peek_token (parser
->lexer
);
24216 /* If it's a `}', or EOF then we've seen all the members. */
24217 if (token
->type
== CPP_CLOSE_BRACE
24218 || token
->type
== CPP_EOF
24219 || token
->type
== CPP_PRAGMA_EOL
)
24222 /* See if this token is a keyword. */
24223 keyword
= token
->keyword
;
24227 case RID_PROTECTED
:
24229 /* Consume the access-specifier. */
24230 cp_lexer_consume_token (parser
->lexer
);
24231 /* Remember which access-specifier is active. */
24232 current_access_specifier
= token
->u
.value
;
24233 /* Look for the `:'. */
24234 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24238 /* Accept #pragmas at class scope. */
24239 if (token
->type
== CPP_PRAGMA
)
24241 cp_parser_pragma (parser
, pragma_member
, NULL
);
24245 /* Otherwise, the next construction must be a
24246 member-declaration. */
24247 cp_parser_member_declaration (parser
);
24252 /* Parse a member-declaration.
24254 member-declaration:
24255 decl-specifier-seq [opt] member-declarator-list [opt] ;
24256 function-definition ; [opt]
24257 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24259 template-declaration
24262 member-declarator-list:
24264 member-declarator-list , member-declarator
24267 declarator pure-specifier [opt]
24268 declarator constant-initializer [opt]
24269 identifier [opt] : constant-expression
24273 member-declaration:
24274 __extension__ member-declaration
24277 declarator attributes [opt] pure-specifier [opt]
24278 declarator attributes [opt] constant-initializer [opt]
24279 identifier [opt] attributes [opt] : constant-expression
24283 member-declaration:
24284 static_assert-declaration */
24287 cp_parser_member_declaration (cp_parser
* parser
)
24289 cp_decl_specifier_seq decl_specifiers
;
24290 tree prefix_attributes
;
24292 int declares_class_or_enum
;
24294 cp_token
*token
= NULL
;
24295 cp_token
*decl_spec_token_start
= NULL
;
24296 cp_token
*initializer_token_start
= NULL
;
24297 int saved_pedantic
;
24298 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24300 /* Check for the `__extension__' keyword. */
24301 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24304 cp_parser_member_declaration (parser
);
24305 /* Restore the old value of the PEDANTIC flag. */
24306 pedantic
= saved_pedantic
;
24311 /* Check for a template-declaration. */
24312 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24314 /* An explicit specialization here is an error condition, and we
24315 expect the specialization handler to detect and report this. */
24316 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24317 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24318 cp_parser_explicit_specialization (parser
);
24320 cp_parser_template_declaration (parser
, /*member_p=*/true);
24324 /* Check for a template introduction. */
24325 else if (cp_parser_template_declaration_after_export (parser
, true))
24328 /* Check for a using-declaration. */
24329 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24331 if (cxx_dialect
< cxx11
)
24333 /* Parse the using-declaration. */
24334 cp_parser_using_declaration (parser
,
24335 /*access_declaration_p=*/false);
24341 bool alias_decl_expected
;
24342 cp_parser_parse_tentatively (parser
);
24343 decl
= cp_parser_alias_declaration (parser
);
24344 /* Note that if we actually see the '=' token after the
24345 identifier, cp_parser_alias_declaration commits the
24346 tentative parse. In that case, we really expect an
24347 alias-declaration. Otherwise, we expect a using
24349 alias_decl_expected
=
24350 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24351 cp_parser_parse_definitely (parser
);
24353 if (alias_decl_expected
)
24354 finish_member_declaration (decl
);
24356 cp_parser_using_declaration (parser
,
24357 /*access_declaration_p=*/false);
24362 /* Check for @defs. */
24363 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24366 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24367 ivar
= ivar_chains
;
24371 ivar
= TREE_CHAIN (member
);
24372 TREE_CHAIN (member
) = NULL_TREE
;
24373 finish_member_declaration (member
);
24378 /* If the next token is `static_assert' we have a static assertion. */
24379 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24381 cp_parser_static_assert (parser
, /*member_p=*/true);
24385 parser
->colon_corrects_to_scope_p
= false;
24387 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24390 /* Parse the decl-specifier-seq. */
24391 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24392 cp_parser_decl_specifier_seq (parser
,
24393 (CP_PARSER_FLAGS_OPTIONAL
24394 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
24396 &declares_class_or_enum
);
24397 /* Check for an invalid type-name. */
24398 if (!decl_specifiers
.any_type_specifiers_p
24399 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24401 /* If there is no declarator, then the decl-specifier-seq should
24403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24405 /* If there was no decl-specifier-seq, and the next token is a
24406 `;', then we have something like:
24412 Each member-declaration shall declare at least one member
24413 name of the class. */
24414 if (!decl_specifiers
.any_specifiers_p
)
24416 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24417 if (!in_system_header_at (token
->location
))
24419 gcc_rich_location
richloc (token
->location
);
24420 richloc
.add_fixit_remove ();
24421 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24428 /* See if this declaration is a friend. */
24429 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24430 /* If there were decl-specifiers, check to see if there was
24431 a class-declaration. */
24432 type
= check_tag_decl (&decl_specifiers
,
24433 /*explicit_type_instantiation_p=*/false);
24434 /* Nested classes have already been added to the class, but
24435 a `friend' needs to be explicitly registered. */
24438 /* If the `friend' keyword was present, the friend must
24439 be introduced with a class-key. */
24440 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24441 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24442 "in C++03 a class-key must be used "
24443 "when declaring a friend");
24446 template <typename T> struct A {
24447 friend struct A<T>::B;
24450 A<T>::B will be represented by a TYPENAME_TYPE, and
24451 therefore not recognized by check_tag_decl. */
24454 type
= decl_specifiers
.type
;
24455 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24456 type
= TREE_TYPE (type
);
24458 if (!type
|| !TYPE_P (type
))
24459 error_at (decl_spec_token_start
->location
,
24460 "friend declaration does not name a class or "
24463 make_friend_class (current_class_type
, type
,
24464 /*complain=*/true);
24466 /* If there is no TYPE, an error message will already have
24468 else if (!type
|| type
== error_mark_node
)
24470 /* An anonymous aggregate has to be handled specially; such
24471 a declaration really declares a data member (with a
24472 particular type), as opposed to a nested class. */
24473 else if (ANON_AGGR_TYPE_P (type
))
24476 if (decl_specifiers
.storage_class
!= sc_none
)
24477 error_at (decl_spec_token_start
->location
,
24478 "a storage class on an anonymous aggregate "
24479 "in class scope is not allowed");
24481 /* Remove constructors and such from TYPE, now that we
24482 know it is an anonymous aggregate. */
24483 fixup_anonymous_aggr (type
);
24484 /* And make the corresponding data member. */
24485 decl
= build_decl (decl_spec_token_start
->location
,
24486 FIELD_DECL
, NULL_TREE
, type
);
24487 /* Add it to the class. */
24488 finish_member_declaration (decl
);
24491 cp_parser_check_access_in_redeclaration
24493 decl_spec_token_start
->location
);
24498 bool assume_semicolon
= false;
24500 /* Clear attributes from the decl_specifiers but keep them
24501 around as prefix attributes that apply them to the entity
24503 prefix_attributes
= decl_specifiers
.attributes
;
24504 decl_specifiers
.attributes
= NULL_TREE
;
24506 /* See if these declarations will be friends. */
24507 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24509 /* Keep going until we hit the `;' at the end of the
24511 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24513 tree attributes
= NULL_TREE
;
24514 tree first_attribute
;
24516 bool named_bitfld
= false;
24518 /* Peek at the next token. */
24519 token
= cp_lexer_peek_token (parser
->lexer
);
24521 /* The following code wants to know early if it is a bit-field
24522 or some other declaration. Attributes can appear before
24523 the `:' token. Skip over them without consuming any tokens
24524 to peek if they are followed by `:'. */
24525 if (cp_next_tokens_can_be_attribute_p (parser
)
24526 || (token
->type
== CPP_NAME
24527 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24528 && (named_bitfld
= true)))
24531 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24532 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24535 /* Check for a bitfield declaration. */
24536 if (token
->type
== CPP_COLON
24537 || (token
->type
== CPP_NAME
24538 && token
== cp_lexer_peek_token (parser
->lexer
)
24539 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24540 && (named_bitfld
= true)))
24544 tree late_attributes
= NULL_TREE
;
24545 location_t id_location
24546 = cp_lexer_peek_token (parser
->lexer
)->location
;
24549 identifier
= cp_parser_identifier (parser
);
24551 identifier
= NULL_TREE
;
24553 /* Look for attributes that apply to the bitfield. */
24554 attributes
= cp_parser_attributes_opt (parser
);
24556 /* Consume the `:' token. */
24557 cp_lexer_consume_token (parser
->lexer
);
24559 /* Get the width of the bitfield. */
24560 width
= cp_parser_constant_expression (parser
, false, NULL
,
24561 cxx_dialect
>= cxx11
);
24563 /* In C++2A and as extension for C++11 and above we allow
24564 default member initializers for bit-fields. */
24565 initializer
= NULL_TREE
;
24566 if (cxx_dialect
>= cxx11
24567 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24568 || cp_lexer_next_token_is (parser
->lexer
,
24572 = cp_lexer_peek_token (parser
->lexer
)->location
;
24573 if (cxx_dialect
< cxx2a
24574 && !in_system_header_at (loc
)
24575 && identifier
!= NULL_TREE
)
24577 "default member initializers for bit-fields "
24578 "only available with %<-std=c++2a%> or "
24579 "%<-std=gnu++2a%>");
24581 initializer
= cp_parser_save_nsdmi (parser
);
24582 if (identifier
== NULL_TREE
)
24584 error_at (loc
, "default member initializer for "
24585 "unnamed bit-field");
24586 initializer
= NULL_TREE
;
24591 /* Look for attributes that apply to the bitfield after
24592 the `:' token and width. This is where GCC used to
24593 parse attributes in the past, pedwarn if there is
24594 a std attribute. */
24595 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24596 pedwarn (input_location
, OPT_Wpedantic
,
24597 "ISO C++ allows bit-field attributes only "
24598 "before the %<:%> token");
24600 late_attributes
= cp_parser_attributes_opt (parser
);
24603 attributes
= attr_chainon (attributes
, late_attributes
);
24605 /* Remember which attributes are prefix attributes and
24607 first_attribute
= attributes
;
24608 /* Combine the attributes. */
24609 attributes
= attr_chainon (prefix_attributes
, attributes
);
24611 /* Create the bitfield declaration. */
24612 decl
= grokbitfield (identifier
24613 ? make_id_declarator (NULL_TREE
,
24619 width
, initializer
,
24624 cp_declarator
*declarator
;
24625 tree asm_specification
;
24626 int ctor_dtor_or_conv_p
;
24627 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
24629 /* Parse the declarator. */
24631 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24632 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24633 &ctor_dtor_or_conv_p
,
24634 /*parenthesized_p=*/NULL
,
24636 friend_p
, static_p
);
24638 /* If something went wrong parsing the declarator, make sure
24639 that we at least consume some tokens. */
24640 if (declarator
== cp_error_declarator
)
24642 /* Skip to the end of the statement. */
24643 cp_parser_skip_to_end_of_statement (parser
);
24644 /* If the next token is not a semicolon, that is
24645 probably because we just skipped over the body of
24646 a function. So, we consume a semicolon if
24647 present, but do not issue an error message if it
24649 if (cp_lexer_next_token_is (parser
->lexer
,
24651 cp_lexer_consume_token (parser
->lexer
);
24655 if (declares_class_or_enum
& 2)
24656 cp_parser_check_for_definition_in_return_type
24657 (declarator
, decl_specifiers
.type
,
24658 decl_specifiers
.locations
[ds_type_spec
]);
24660 /* Look for an asm-specification. */
24661 asm_specification
= cp_parser_asm_specification_opt (parser
);
24662 /* Look for attributes that apply to the declaration. */
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
= attr_chainon (prefix_attributes
, attributes
);
24670 /* If it's an `=', then we have a constant-initializer or a
24671 pure-specifier. It is not correct to parse the
24672 initializer before registering the member declaration
24673 since the member declaration should be in scope while
24674 its initializer is processed. However, the rest of the
24675 front end does not yet provide an interface that allows
24676 us to handle this correctly. */
24677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24681 A pure-specifier shall be used only in the declaration of
24682 a virtual function.
24684 A member-declarator can contain a constant-initializer
24685 only if it declares a static member of integral or
24688 Therefore, if the DECLARATOR is for a function, we look
24689 for a pure-specifier; otherwise, we look for a
24690 constant-initializer. When we call `grokfield', it will
24691 perform more stringent semantics checks. */
24692 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24693 if (function_declarator_p (declarator
)
24694 || (decl_specifiers
.type
24695 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24696 && declarator
->kind
== cdk_id
24697 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24698 == FUNCTION_TYPE
)))
24699 initializer
= cp_parser_pure_specifier (parser
);
24700 else if (decl_specifiers
.storage_class
!= sc_static
)
24701 initializer
= cp_parser_save_nsdmi (parser
);
24702 else if (cxx_dialect
>= cxx11
)
24705 /* Don't require a constant rvalue in C++11, since we
24706 might want a reference constant. We'll enforce
24707 constancy later. */
24708 cp_lexer_consume_token (parser
->lexer
);
24709 /* Parse the initializer. */
24710 initializer
= cp_parser_initializer_clause (parser
,
24714 /* Parse the initializer. */
24715 initializer
= cp_parser_constant_initializer (parser
);
24717 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24718 && !function_declarator_p (declarator
))
24721 if (decl_specifiers
.storage_class
!= sc_static
)
24722 initializer
= cp_parser_save_nsdmi (parser
);
24724 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24726 /* Otherwise, there is no initializer. */
24728 initializer
= NULL_TREE
;
24730 /* See if we are probably looking at a function
24731 definition. We are certainly not looking at a
24732 member-declarator. Calling `grokfield' has
24733 side-effects, so we must not do it unless we are sure
24734 that we are looking at a member-declarator. */
24735 if (cp_parser_token_starts_function_definition_p
24736 (cp_lexer_peek_token (parser
->lexer
)))
24738 /* The grammar does not allow a pure-specifier to be
24739 used when a member function is defined. (It is
24740 possible that this fact is an oversight in the
24741 standard, since a pure function may be defined
24742 outside of the class-specifier. */
24743 if (initializer
&& initializer_token_start
)
24744 error_at (initializer_token_start
->location
,
24745 "pure-specifier on function-definition");
24746 decl
= cp_parser_save_member_function_body (parser
,
24750 if (parser
->fully_implicit_function_template_p
)
24751 decl
= finish_fully_implicit_template (parser
, decl
);
24752 /* If the member was not a friend, declare it here. */
24754 finish_member_declaration (decl
);
24755 /* Peek at the next token. */
24756 token
= cp_lexer_peek_token (parser
->lexer
);
24757 /* If the next token is a semicolon, consume it. */
24758 if (token
->type
== CPP_SEMICOLON
)
24760 location_t semicolon_loc
24761 = cp_lexer_consume_token (parser
->lexer
)->location
;
24762 gcc_rich_location
richloc (semicolon_loc
);
24763 richloc
.add_fixit_remove ();
24764 warning_at (&richloc
, OPT_Wextra_semi
,
24765 "extra %<;%> after in-class "
24766 "function definition");
24771 if (declarator
->kind
== cdk_function
)
24772 declarator
->id_loc
= token
->location
;
24773 /* Create the declaration. */
24774 decl
= grokfield (declarator
, &decl_specifiers
,
24775 initializer
, /*init_const_expr_p=*/true,
24776 asm_specification
, attributes
);
24777 if (parser
->fully_implicit_function_template_p
)
24780 finish_fully_implicit_template (parser
, 0);
24782 decl
= finish_fully_implicit_template (parser
, decl
);
24786 cp_finalize_omp_declare_simd (parser
, decl
);
24787 cp_finalize_oacc_routine (parser
, decl
, false);
24789 /* Reset PREFIX_ATTRIBUTES. */
24790 if (attributes
!= error_mark_node
)
24792 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24793 attributes
= TREE_CHAIN (attributes
);
24795 TREE_CHAIN (attributes
) = NULL_TREE
;
24798 /* If there is any qualification still in effect, clear it
24799 now; we will be starting fresh with the next declarator. */
24800 parser
->scope
= NULL_TREE
;
24801 parser
->qualifying_scope
= NULL_TREE
;
24802 parser
->object_scope
= NULL_TREE
;
24803 /* If it's a `,', then there are more declarators. */
24804 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24806 cp_lexer_consume_token (parser
->lexer
);
24807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24809 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24810 gcc_rich_location
richloc (token
->location
);
24811 richloc
.add_fixit_remove ();
24812 error_at (&richloc
, "stray %<,%> at end of "
24813 "member declaration");
24816 /* If the next token isn't a `;', then we have a parse error. */
24817 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24820 /* The next token might be a ways away from where the
24821 actual semicolon is missing. Find the previous token
24822 and use that for our error position. */
24823 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24824 gcc_rich_location
richloc (token
->location
);
24825 richloc
.add_fixit_insert_after (";");
24826 error_at (&richloc
, "expected %<;%> at end of "
24827 "member declaration");
24829 /* Assume that the user meant to provide a semicolon. If
24830 we were to cp_parser_skip_to_end_of_statement, we might
24831 skip to a semicolon inside a member function definition
24832 and issue nonsensical error messages. */
24833 assume_semicolon
= true;
24838 /* Add DECL to the list of members. */
24840 /* Explicitly include, eg, NSDMIs, for better error
24841 recovery (c++/58650). */
24842 || !DECL_DECLARES_FUNCTION_P (decl
))
24843 finish_member_declaration (decl
);
24845 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24846 cp_parser_save_default_args (parser
, decl
);
24847 else if (TREE_CODE (decl
) == FIELD_DECL
24848 && DECL_INITIAL (decl
))
24849 /* Add DECL to the queue of NSDMI to be parsed later. */
24850 vec_safe_push (unparsed_nsdmis
, decl
);
24853 if (assume_semicolon
)
24858 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24860 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24863 /* Parse a pure-specifier.
24868 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24869 Otherwise, ERROR_MARK_NODE is returned. */
24872 cp_parser_pure_specifier (cp_parser
* parser
)
24876 /* Look for the `=' token. */
24877 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24878 return error_mark_node
;
24879 /* Look for the `0' token. */
24880 token
= cp_lexer_peek_token (parser
->lexer
);
24882 if (token
->type
== CPP_EOF
24883 || token
->type
== CPP_PRAGMA_EOL
)
24884 return error_mark_node
;
24886 cp_lexer_consume_token (parser
->lexer
);
24888 /* Accept = default or = delete in c++0x mode. */
24889 if (token
->keyword
== RID_DEFAULT
24890 || token
->keyword
== RID_DELETE
)
24892 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24893 return token
->u
.value
;
24896 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24897 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24899 cp_parser_error (parser
,
24900 "invalid pure specifier (only %<= 0%> is allowed)");
24901 cp_parser_skip_to_end_of_statement (parser
);
24902 return error_mark_node
;
24904 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24906 error_at (token
->location
, "templates may not be %<virtual%>");
24907 return error_mark_node
;
24910 return integer_zero_node
;
24913 /* Parse a constant-initializer.
24915 constant-initializer:
24916 = constant-expression
24918 Returns a representation of the constant-expression. */
24921 cp_parser_constant_initializer (cp_parser
* parser
)
24923 /* Look for the `=' token. */
24924 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24925 return error_mark_node
;
24927 /* It is invalid to write:
24929 struct S { static const int i = { 7 }; };
24932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24934 cp_parser_error (parser
,
24935 "a brace-enclosed initializer is not allowed here");
24936 /* Consume the opening brace. */
24937 matching_braces braces
;
24938 braces
.consume_open (parser
);
24939 /* Skip the initializer. */
24940 cp_parser_skip_to_closing_brace (parser
);
24941 /* Look for the trailing `}'. */
24942 braces
.require_close (parser
);
24944 return error_mark_node
;
24947 return cp_parser_constant_expression (parser
);
24950 /* Derived classes [gram.class.derived] */
24952 /* Parse a base-clause.
24955 : base-specifier-list
24957 base-specifier-list:
24958 base-specifier ... [opt]
24959 base-specifier-list , base-specifier ... [opt]
24961 Returns a TREE_LIST representing the base-classes, in the order in
24962 which they were declared. The representation of each node is as
24963 described by cp_parser_base_specifier.
24965 In the case that no bases are specified, this function will return
24966 NULL_TREE, not ERROR_MARK_NODE. */
24969 cp_parser_base_clause (cp_parser
* parser
)
24971 tree bases
= NULL_TREE
;
24973 /* Look for the `:' that begins the list. */
24974 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24976 /* Scan the base-specifier-list. */
24981 bool pack_expansion_p
= false;
24983 /* Look for the base-specifier. */
24984 base
= cp_parser_base_specifier (parser
);
24985 /* Look for the (optional) ellipsis. */
24986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24988 /* Consume the `...'. */
24989 cp_lexer_consume_token (parser
->lexer
);
24991 pack_expansion_p
= true;
24994 /* Add BASE to the front of the list. */
24995 if (base
&& base
!= error_mark_node
)
24997 if (pack_expansion_p
)
24998 /* Make this a pack expansion type. */
24999 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
25001 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
25003 TREE_CHAIN (base
) = bases
;
25007 /* Peek at the next token. */
25008 token
= cp_lexer_peek_token (parser
->lexer
);
25009 /* If it's not a comma, then the list is complete. */
25010 if (token
->type
!= CPP_COMMA
)
25012 /* Consume the `,'. */
25013 cp_lexer_consume_token (parser
->lexer
);
25016 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25017 base class had a qualified name. However, the next name that
25018 appears is certainly not qualified. */
25019 parser
->scope
= NULL_TREE
;
25020 parser
->qualifying_scope
= NULL_TREE
;
25021 parser
->object_scope
= NULL_TREE
;
25023 return nreverse (bases
);
25026 /* Parse a base-specifier.
25029 :: [opt] nested-name-specifier [opt] class-name
25030 virtual access-specifier [opt] :: [opt] nested-name-specifier
25032 access-specifier virtual [opt] :: [opt] nested-name-specifier
25035 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25036 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25037 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25038 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25041 cp_parser_base_specifier (cp_parser
* parser
)
25045 bool virtual_p
= false;
25046 bool duplicate_virtual_error_issued_p
= false;
25047 bool duplicate_access_error_issued_p
= false;
25048 bool class_scope_p
, template_p
;
25049 tree access
= access_default_node
;
25052 /* Process the optional `virtual' and `access-specifier'. */
25055 /* Peek at the next token. */
25056 token
= cp_lexer_peek_token (parser
->lexer
);
25057 /* Process `virtual'. */
25058 switch (token
->keyword
)
25061 /* If `virtual' appears more than once, issue an error. */
25062 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
25064 cp_parser_error (parser
,
25065 "%<virtual%> specified more than once in base-specifier");
25066 duplicate_virtual_error_issued_p
= true;
25071 /* Consume the `virtual' token. */
25072 cp_lexer_consume_token (parser
->lexer
);
25077 case RID_PROTECTED
:
25079 /* If more than one access specifier appears, issue an
25081 if (access
!= access_default_node
25082 && !duplicate_access_error_issued_p
)
25084 cp_parser_error (parser
,
25085 "more than one access specifier in base-specifier");
25086 duplicate_access_error_issued_p
= true;
25089 access
= ridpointers
[(int) token
->keyword
];
25091 /* Consume the access-specifier. */
25092 cp_lexer_consume_token (parser
->lexer
);
25101 /* It is not uncommon to see programs mechanically, erroneously, use
25102 the 'typename' keyword to denote (dependent) qualified types
25103 as base classes. */
25104 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25106 token
= cp_lexer_peek_token (parser
->lexer
);
25107 if (!processing_template_decl
)
25108 error_at (token
->location
,
25109 "keyword %<typename%> not allowed outside of templates");
25111 error_at (token
->location
,
25112 "keyword %<typename%> not allowed in this context "
25113 "(the base class is implicitly a type)");
25114 cp_lexer_consume_token (parser
->lexer
);
25117 /* Look for the optional `::' operator. */
25118 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
25119 /* Look for the nested-name-specifier. The simplest way to
25124 The keyword `typename' is not permitted in a base-specifier or
25125 mem-initializer; in these contexts a qualified name that
25126 depends on a template-parameter is implicitly assumed to be a
25129 is to pretend that we have seen the `typename' keyword at this
25131 cp_parser_nested_name_specifier_opt (parser
,
25132 /*typename_keyword_p=*/true,
25133 /*check_dependency_p=*/true,
25135 /*is_declaration=*/true);
25136 /* If the base class is given by a qualified name, assume that names
25137 we see are type names or templates, as appropriate. */
25138 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
25139 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
25142 && cp_lexer_next_token_is_decltype (parser
->lexer
))
25143 /* DR 950 allows decltype as a base-specifier. */
25144 type
= cp_parser_decltype (parser
);
25147 /* Otherwise, look for the class-name. */
25148 type
= cp_parser_class_name (parser
,
25152 /*check_dependency_p=*/true,
25153 /*class_head_p=*/false,
25154 /*is_declaration=*/true);
25155 type
= TREE_TYPE (type
);
25158 if (type
== error_mark_node
)
25159 return error_mark_node
;
25161 return finish_base_specifier (type
, access
, virtual_p
);
25164 /* Exception handling [gram.exception] */
25166 /* Parse an (optional) noexcept-specification.
25168 noexcept-specification:
25169 noexcept ( constant-expression ) [opt]
25171 If no noexcept-specification is present, returns NULL_TREE.
25172 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25173 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25174 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25175 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25176 in which case a boolean condition is returned instead. */
25179 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
25180 bool require_constexpr
,
25181 bool* consumed_expr
,
25185 const char *saved_message
;
25187 /* Peek at the next token. */
25188 token
= cp_lexer_peek_token (parser
->lexer
);
25190 /* Is it a noexcept-specification? */
25191 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
25194 cp_lexer_consume_token (parser
->lexer
);
25196 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
25198 matching_parens parens
;
25199 parens
.consume_open (parser
);
25201 tree save_ccp
= current_class_ptr
;
25202 tree save_ccr
= current_class_ref
;
25204 if (current_class_type
)
25205 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
25207 if (require_constexpr
)
25209 /* Types may not be defined in an exception-specification. */
25210 saved_message
= parser
->type_definition_forbidden_message
;
25211 parser
->type_definition_forbidden_message
25212 = G_("types may not be defined in an exception-specification");
25214 bool non_constant_p
;
25216 = cp_parser_constant_expression (parser
,
25217 /*allow_non_constant=*/true,
25220 && !require_potential_rvalue_constant_expression (expr
))
25223 return_cond
= true;
25226 /* Restore the saved message. */
25227 parser
->type_definition_forbidden_message
= saved_message
;
25231 expr
= cp_parser_expression (parser
);
25232 *consumed_expr
= true;
25235 parens
.require_close (parser
);
25237 current_class_ptr
= save_ccp
;
25238 current_class_ref
= save_ccr
;
25242 expr
= boolean_true_node
;
25243 if (!require_constexpr
)
25244 *consumed_expr
= false;
25247 /* We cannot build a noexcept-spec right away because this will check
25248 that expr is a constexpr. */
25250 return build_noexcept_spec (expr
, tf_warning_or_error
);
25258 /* Parse an (optional) exception-specification.
25260 exception-specification:
25261 throw ( type-id-list [opt] )
25263 Returns a TREE_LIST representing the exception-specification. The
25264 TREE_VALUE of each node is a type. */
25267 cp_parser_exception_specification_opt (cp_parser
* parser
)
25271 const char *saved_message
;
25273 /* Peek at the next token. */
25274 token
= cp_lexer_peek_token (parser
->lexer
);
25276 /* Is it a noexcept-specification? */
25277 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
25279 if (type_id_list
!= NULL_TREE
)
25280 return type_id_list
;
25282 /* If it's not `throw', then there's no exception-specification. */
25283 if (!cp_parser_is_keyword (token
, RID_THROW
))
25286 location_t loc
= token
->location
;
25288 /* Consume the `throw'. */
25289 cp_lexer_consume_token (parser
->lexer
);
25291 /* Look for the `('. */
25292 matching_parens parens
;
25293 parens
.require_open (parser
);
25295 /* Peek at the next token. */
25296 token
= cp_lexer_peek_token (parser
->lexer
);
25297 /* If it's not a `)', then there is a type-id-list. */
25298 if (token
->type
!= CPP_CLOSE_PAREN
)
25300 /* Types may not be defined in an exception-specification. */
25301 saved_message
= parser
->type_definition_forbidden_message
;
25302 parser
->type_definition_forbidden_message
25303 = G_("types may not be defined in an exception-specification");
25304 /* Parse the type-id-list. */
25305 type_id_list
= cp_parser_type_id_list (parser
);
25306 /* Restore the saved message. */
25307 parser
->type_definition_forbidden_message
= saved_message
;
25309 if (cxx_dialect
>= cxx17
)
25311 error_at (loc
, "ISO C++17 does not allow dynamic exception "
25313 type_id_list
= NULL_TREE
;
25315 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
25316 warning_at (loc
, OPT_Wdeprecated
,
25317 "dynamic exception specifications are deprecated in "
25320 /* In C++17, throw() is equivalent to noexcept (true). throw()
25321 is deprecated in C++11 and above as well, but is still widely used,
25322 so don't warn about it yet. */
25323 else if (cxx_dialect
>= cxx17
)
25324 type_id_list
= noexcept_true_spec
;
25326 type_id_list
= empty_except_spec
;
25328 /* Look for the `)'. */
25329 parens
.require_close (parser
);
25331 return type_id_list
;
25334 /* Parse an (optional) type-id-list.
25338 type-id-list , type-id ... [opt]
25340 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25341 in the order that the types were presented. */
25344 cp_parser_type_id_list (cp_parser
* parser
)
25346 tree types
= NULL_TREE
;
25353 token
= cp_lexer_peek_token (parser
->lexer
);
25355 /* Get the next type-id. */
25356 type
= cp_parser_type_id (parser
);
25357 /* Check for invalid 'auto'. */
25358 if (flag_concepts
&& type_uses_auto (type
))
25360 error_at (token
->location
,
25361 "invalid use of %<auto%> in exception-specification");
25362 type
= error_mark_node
;
25364 /* Parse the optional ellipsis. */
25365 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25367 /* Consume the `...'. */
25368 cp_lexer_consume_token (parser
->lexer
);
25370 /* Turn the type into a pack expansion expression. */
25371 type
= make_pack_expansion (type
);
25373 /* Add it to the list. */
25374 types
= add_exception_specifier (types
, type
, /*complain=*/1);
25375 /* Peek at the next token. */
25376 token
= cp_lexer_peek_token (parser
->lexer
);
25377 /* If it is not a `,', we are done. */
25378 if (token
->type
!= CPP_COMMA
)
25380 /* Consume the `,'. */
25381 cp_lexer_consume_token (parser
->lexer
);
25384 return nreverse (types
);
25387 /* Parse a try-block.
25390 try compound-statement handler-seq */
25393 cp_parser_try_block (cp_parser
* parser
)
25397 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25398 if (parser
->in_function_body
25399 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
25400 && cxx_dialect
< cxx2a
)
25401 pedwarn (input_location
, 0,
25402 "%<try%> in %<constexpr%> function only "
25403 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25405 try_block
= begin_try_block ();
25406 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25407 finish_try_block (try_block
);
25408 cp_parser_handler_seq (parser
);
25409 finish_handler_sequence (try_block
);
25414 /* Parse a function-try-block.
25416 function-try-block:
25417 try ctor-initializer [opt] function-body handler-seq */
25420 cp_parser_function_try_block (cp_parser
* parser
)
25422 tree compound_stmt
;
25425 /* Look for the `try' keyword. */
25426 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25428 /* Let the rest of the front end know where we are. */
25429 try_block
= begin_function_try_block (&compound_stmt
);
25430 /* Parse the function-body. */
25431 cp_parser_ctor_initializer_opt_and_function_body
25432 (parser
, /*in_function_try_block=*/true);
25433 /* We're done with the `try' part. */
25434 finish_function_try_block (try_block
);
25435 /* Parse the handlers. */
25436 cp_parser_handler_seq (parser
);
25437 /* We're done with the handlers. */
25438 finish_function_handler_sequence (try_block
, compound_stmt
);
25441 /* Parse a handler-seq.
25444 handler handler-seq [opt] */
25447 cp_parser_handler_seq (cp_parser
* parser
)
25453 /* Parse the handler. */
25454 cp_parser_handler (parser
);
25455 /* Peek at the next token. */
25456 token
= cp_lexer_peek_token (parser
->lexer
);
25457 /* If it's not `catch' then there are no more handlers. */
25458 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25463 /* Parse a handler.
25466 catch ( exception-declaration ) compound-statement */
25469 cp_parser_handler (cp_parser
* parser
)
25474 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25475 handler
= begin_handler ();
25476 matching_parens parens
;
25477 parens
.require_open (parser
);
25478 declaration
= cp_parser_exception_declaration (parser
);
25479 finish_handler_parms (declaration
, handler
);
25480 parens
.require_close (parser
);
25481 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25482 finish_handler (handler
);
25485 /* Parse an exception-declaration.
25487 exception-declaration:
25488 type-specifier-seq declarator
25489 type-specifier-seq abstract-declarator
25493 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25494 ellipsis variant is used. */
25497 cp_parser_exception_declaration (cp_parser
* parser
)
25499 cp_decl_specifier_seq type_specifiers
;
25500 cp_declarator
*declarator
;
25501 const char *saved_message
;
25503 /* If it's an ellipsis, it's easy to handle. */
25504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25506 /* Consume the `...' token. */
25507 cp_lexer_consume_token (parser
->lexer
);
25511 /* Types may not be defined in exception-declarations. */
25512 saved_message
= parser
->type_definition_forbidden_message
;
25513 parser
->type_definition_forbidden_message
25514 = G_("types may not be defined in exception-declarations");
25516 /* Parse the type-specifier-seq. */
25517 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
25518 /*is_declaration=*/true,
25519 /*is_trailing_return=*/false,
25521 /* If it's a `)', then there is no declarator. */
25522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25525 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25526 CP_PARSER_FLAGS_NONE
,
25527 /*ctor_dtor_or_conv_p=*/NULL
,
25528 /*parenthesized_p=*/NULL
,
25529 /*member_p=*/false,
25530 /*friend_p=*/false,
25531 /*static_p=*/false);
25533 /* Restore the saved message. */
25534 parser
->type_definition_forbidden_message
= saved_message
;
25536 if (!type_specifiers
.any_specifiers_p
)
25537 return error_mark_node
;
25539 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25542 /* Parse a throw-expression.
25545 throw assignment-expression [opt]
25547 Returns a THROW_EXPR representing the throw-expression. */
25550 cp_parser_throw_expression (cp_parser
* parser
)
25555 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25556 token
= cp_lexer_peek_token (parser
->lexer
);
25557 /* Figure out whether or not there is an assignment-expression
25558 following the "throw" keyword. */
25559 if (token
->type
== CPP_COMMA
25560 || token
->type
== CPP_SEMICOLON
25561 || token
->type
== CPP_CLOSE_PAREN
25562 || token
->type
== CPP_CLOSE_SQUARE
25563 || token
->type
== CPP_CLOSE_BRACE
25564 || token
->type
== CPP_COLON
)
25565 expression
= NULL_TREE
;
25567 expression
= cp_parser_assignment_expression (parser
);
25569 return build_throw (expression
);
25572 /* GNU Extensions */
25574 /* Parse an (optional) asm-specification.
25577 asm ( string-literal )
25579 If the asm-specification is present, returns a STRING_CST
25580 corresponding to the string-literal. Otherwise, returns
25584 cp_parser_asm_specification_opt (cp_parser
* parser
)
25587 tree asm_specification
;
25589 /* Peek at the next token. */
25590 token
= cp_lexer_peek_token (parser
->lexer
);
25591 /* If the next token isn't the `asm' keyword, then there's no
25592 asm-specification. */
25593 if (!cp_parser_is_keyword (token
, RID_ASM
))
25596 /* Consume the `asm' token. */
25597 cp_lexer_consume_token (parser
->lexer
);
25598 /* Look for the `('. */
25599 matching_parens parens
;
25600 parens
.require_open (parser
);
25602 /* Look for the string-literal. */
25603 asm_specification
= cp_parser_string_literal (parser
, false, false);
25605 /* Look for the `)'. */
25606 parens
.require_close (parser
);
25608 return asm_specification
;
25611 /* Parse an asm-operand-list.
25615 asm-operand-list , asm-operand
25618 string-literal ( expression )
25619 [ string-literal ] string-literal ( expression )
25621 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25622 each node is the expression. The TREE_PURPOSE is itself a
25623 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25624 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25625 is a STRING_CST for the string literal before the parenthesis. Returns
25626 ERROR_MARK_NODE if any of the operands are invalid. */
25629 cp_parser_asm_operand_list (cp_parser
* parser
)
25631 tree asm_operands
= NULL_TREE
;
25632 bool invalid_operands
= false;
25636 tree string_literal
;
25640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25642 /* Consume the `[' token. */
25643 cp_lexer_consume_token (parser
->lexer
);
25644 /* Read the operand name. */
25645 name
= cp_parser_identifier (parser
);
25646 if (name
!= error_mark_node
)
25647 name
= build_string (IDENTIFIER_LENGTH (name
),
25648 IDENTIFIER_POINTER (name
));
25649 /* Look for the closing `]'. */
25650 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25654 /* Look for the string-literal. */
25655 string_literal
= cp_parser_string_literal (parser
, false, false);
25657 /* Look for the `('. */
25658 matching_parens parens
;
25659 parens
.require_open (parser
);
25660 /* Parse the expression. */
25661 expression
= cp_parser_expression (parser
);
25662 /* Look for the `)'. */
25663 parens
.require_close (parser
);
25665 if (name
== error_mark_node
25666 || string_literal
== error_mark_node
25667 || expression
== error_mark_node
)
25668 invalid_operands
= true;
25670 /* Add this operand to the list. */
25671 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25674 /* If the next token is not a `,', there are no more
25676 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25678 /* Consume the `,'. */
25679 cp_lexer_consume_token (parser
->lexer
);
25682 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25685 /* Parse an asm-clobber-list.
25689 asm-clobber-list , string-literal
25691 Returns a TREE_LIST, indicating the clobbers in the order that they
25692 appeared. The TREE_VALUE of each node is a STRING_CST. */
25695 cp_parser_asm_clobber_list (cp_parser
* parser
)
25697 tree clobbers
= NULL_TREE
;
25701 tree string_literal
;
25703 /* Look for the string literal. */
25704 string_literal
= cp_parser_string_literal (parser
, false, false);
25705 /* Add it to the list. */
25706 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25707 /* If the next token is not a `,', then the list is
25709 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25711 /* Consume the `,' token. */
25712 cp_lexer_consume_token (parser
->lexer
);
25718 /* Parse an asm-label-list.
25722 asm-label-list , identifier
25724 Returns a TREE_LIST, indicating the labels in the order that they
25725 appeared. The TREE_VALUE of each node is a label. */
25728 cp_parser_asm_label_list (cp_parser
* parser
)
25730 tree labels
= NULL_TREE
;
25734 tree identifier
, label
, name
;
25736 /* Look for the identifier. */
25737 identifier
= cp_parser_identifier (parser
);
25738 if (!error_operand_p (identifier
))
25740 label
= lookup_label (identifier
);
25741 if (TREE_CODE (label
) == LABEL_DECL
)
25743 TREE_USED (label
) = 1;
25744 check_goto (label
);
25745 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25746 IDENTIFIER_POINTER (identifier
));
25747 labels
= tree_cons (name
, label
, labels
);
25750 /* If the next token is not a `,', then the list is
25752 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25754 /* Consume the `,' token. */
25755 cp_lexer_consume_token (parser
->lexer
);
25758 return nreverse (labels
);
25761 /* Return TRUE iff the next tokens in the stream are possibly the
25762 beginning of a GNU extension attribute. */
25765 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25767 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25770 /* Return TRUE iff the next tokens in the stream are possibly the
25771 beginning of a standard C++-11 attribute specifier. */
25774 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25776 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25779 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25780 beginning of a standard C++-11 attribute specifier. */
25783 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25785 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25787 return (cxx_dialect
>= cxx11
25788 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25789 || (token
->type
== CPP_OPEN_SQUARE
25790 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25791 && token
->type
== CPP_OPEN_SQUARE
)));
25794 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25795 beginning of a GNU extension attribute. */
25798 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25800 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25802 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25805 /* Return true iff the next tokens can be the beginning of either a
25806 GNU attribute list, or a standard C++11 attribute sequence. */
25809 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25811 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25812 || cp_next_tokens_can_be_std_attribute_p (parser
));
25815 /* Return true iff the next Nth tokens can be the beginning of either
25816 a GNU attribute list, or a standard C++11 attribute sequence. */
25819 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25821 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25822 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25825 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25826 of GNU attributes, or return NULL. */
25829 cp_parser_attributes_opt (cp_parser
*parser
)
25831 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25832 return cp_parser_gnu_attributes_opt (parser
);
25833 return cp_parser_std_attribute_spec_seq (parser
);
25836 /* Parse an (optional) series of attributes.
25839 attributes attribute
25842 __attribute__ (( attribute-list [opt] ))
25844 The return value is as for cp_parser_gnu_attribute_list. */
25847 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25849 tree attributes
= NULL_TREE
;
25851 temp_override
<bool> cleanup
25852 (parser
->auto_is_implicit_function_template_parm_p
, false);
25857 tree attribute_list
;
25860 /* Peek at the next token. */
25861 token
= cp_lexer_peek_token (parser
->lexer
);
25862 /* If it's not `__attribute__', then we're done. */
25863 if (token
->keyword
!= RID_ATTRIBUTE
)
25866 /* Consume the `__attribute__' keyword. */
25867 cp_lexer_consume_token (parser
->lexer
);
25868 /* Look for the two `(' tokens. */
25869 matching_parens outer_parens
;
25870 if (!outer_parens
.require_open (parser
))
25872 matching_parens inner_parens
;
25873 if (!inner_parens
.require_open (parser
))
25876 /* Peek at the next token. */
25877 token
= cp_lexer_peek_token (parser
->lexer
);
25878 if (token
->type
!= CPP_CLOSE_PAREN
)
25879 /* Parse the attribute-list. */
25880 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25882 /* If the next token is a `)', then there is no attribute
25884 attribute_list
= NULL
;
25886 /* Look for the two `)' tokens. */
25887 if (!inner_parens
.require_close (parser
))
25889 if (!outer_parens
.require_close (parser
))
25892 cp_parser_skip_to_end_of_statement (parser
);
25894 /* Add these new attributes to the list. */
25895 attributes
= attr_chainon (attributes
, attribute_list
);
25901 /* Parse a GNU attribute-list.
25905 attribute-list , attribute
25909 identifier ( identifier )
25910 identifier ( identifier , expression-list )
25911 identifier ( expression-list )
25913 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25914 to an attribute. The TREE_PURPOSE of each node is the identifier
25915 indicating which attribute is in use. The TREE_VALUE represents
25916 the arguments, if any. */
25919 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25921 tree attribute_list
= NULL_TREE
;
25922 bool save_translate_strings_p
= parser
->translate_strings_p
;
25924 /* Don't create wrapper nodes within attributes: the
25925 handlers don't know how to handle them. */
25926 auto_suppress_location_wrappers sentinel
;
25928 parser
->translate_strings_p
= false;
25935 /* Look for the identifier. We also allow keywords here; for
25936 example `__attribute__ ((const))' is legal. */
25937 token
= cp_lexer_peek_token (parser
->lexer
);
25938 if (token
->type
== CPP_NAME
25939 || token
->type
== CPP_KEYWORD
)
25941 tree arguments
= NULL_TREE
;
25943 /* Consume the token, but save it since we need it for the
25944 SIMD enabled function parsing. */
25945 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25947 /* Save away the identifier that indicates which attribute
25949 identifier
= (token
->type
== CPP_KEYWORD
)
25950 /* For keywords, use the canonical spelling, not the
25951 parsed identifier. */
25952 ? ridpointers
[(int) token
->keyword
]
25953 : id_token
->u
.value
;
25955 identifier
= canonicalize_attr_name (identifier
);
25956 attribute
= build_tree_list (identifier
, NULL_TREE
);
25958 /* Peek at the next token. */
25959 token
= cp_lexer_peek_token (parser
->lexer
);
25960 /* If it's an `(', then parse the attribute arguments. */
25961 if (token
->type
== CPP_OPEN_PAREN
)
25963 vec
<tree
, va_gc
> *vec
;
25964 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25965 ? id_attr
: normal_attr
);
25966 vec
= cp_parser_parenthesized_expression_list
25967 (parser
, attr_flag
, /*cast_p=*/false,
25968 /*allow_expansion_p=*/false,
25969 /*non_constant_p=*/NULL
);
25971 arguments
= error_mark_node
;
25974 arguments
= build_tree_list_vec (vec
);
25975 release_tree_vector (vec
);
25977 /* Save the arguments away. */
25978 TREE_VALUE (attribute
) = arguments
;
25981 if (arguments
!= error_mark_node
)
25983 /* Add this attribute to the list. */
25984 TREE_CHAIN (attribute
) = attribute_list
;
25985 attribute_list
= attribute
;
25988 token
= cp_lexer_peek_token (parser
->lexer
);
25990 /* Unless EXACTLY_ONE is set look for more attributes.
25991 If the next token isn't a `,', we're done. */
25992 if (exactly_one
|| token
->type
!= CPP_COMMA
)
25995 /* Consume the comma and keep going. */
25996 cp_lexer_consume_token (parser
->lexer
);
25998 parser
->translate_strings_p
= save_translate_strings_p
;
26000 /* We built up the list in reverse order. */
26001 return nreverse (attribute_list
);
26004 /* Parse a standard C++11 attribute.
26006 The returned representation is a TREE_LIST which TREE_PURPOSE is
26007 the scoped name of the attribute, and the TREE_VALUE is its
26010 Note that the scoped name of the attribute is itself a TREE_LIST
26011 which TREE_PURPOSE is the namespace of the attribute, and
26012 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26013 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26014 and which TREE_PURPOSE is directly the attribute name.
26016 Clients of the attribute code should use get_attribute_namespace
26017 and get_attribute_name to get the actual namespace and name of
26018 attributes, regardless of their being GNU or C++11 attributes.
26021 attribute-token attribute-argument-clause [opt]
26025 attribute-scoped-token
26027 attribute-scoped-token:
26028 attribute-namespace :: identifier
26030 attribute-namespace:
26033 attribute-argument-clause:
26034 ( balanced-token-seq )
26036 balanced-token-seq:
26037 balanced-token [opt]
26038 balanced-token-seq balanced-token
26041 ( balanced-token-seq )
26042 [ balanced-token-seq ]
26043 { balanced-token-seq }. */
26046 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
26048 tree attribute
, attr_id
= NULL_TREE
, arguments
;
26051 temp_override
<bool> cleanup
26052 (parser
->auto_is_implicit_function_template_parm_p
, false);
26054 /* First, parse name of the attribute, a.k.a attribute-token. */
26056 token
= cp_lexer_peek_token (parser
->lexer
);
26057 if (token
->type
== CPP_NAME
)
26058 attr_id
= token
->u
.value
;
26059 else if (token
->type
== CPP_KEYWORD
)
26060 attr_id
= ridpointers
[(int) token
->keyword
];
26061 else if (token
->flags
& NAMED_OP
)
26062 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26064 if (attr_id
== NULL_TREE
)
26067 cp_lexer_consume_token (parser
->lexer
);
26069 token
= cp_lexer_peek_token (parser
->lexer
);
26070 if (token
->type
== CPP_SCOPE
)
26072 /* We are seeing a scoped attribute token. */
26074 cp_lexer_consume_token (parser
->lexer
);
26076 error_at (token
->location
, "attribute using prefix used together "
26077 "with scoped attribute token");
26080 token
= cp_lexer_consume_token (parser
->lexer
);
26081 if (token
->type
== CPP_NAME
)
26082 attr_id
= token
->u
.value
;
26083 else if (token
->type
== CPP_KEYWORD
)
26084 attr_id
= ridpointers
[(int) token
->keyword
];
26085 else if (token
->flags
& NAMED_OP
)
26086 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26089 error_at (token
->location
,
26090 "expected an identifier for the attribute name");
26091 return error_mark_node
;
26094 attr_ns
= canonicalize_attr_name (attr_ns
);
26095 attr_id
= canonicalize_attr_name (attr_id
);
26096 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26098 token
= cp_lexer_peek_token (parser
->lexer
);
26102 attr_ns
= canonicalize_attr_name (attr_ns
);
26103 attr_id
= canonicalize_attr_name (attr_id
);
26104 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26109 attr_id
= canonicalize_attr_name (attr_id
);
26110 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
26112 /* C++11 noreturn attribute is equivalent to GNU's. */
26113 if (is_attribute_p ("noreturn", attr_id
))
26114 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26115 /* C++14 deprecated attribute is equivalent to GNU's. */
26116 else if (is_attribute_p ("deprecated", attr_id
))
26117 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26118 /* C++17 fallthrough attribute is equivalent to GNU's. */
26119 else if (is_attribute_p ("fallthrough", attr_id
))
26120 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26121 /* Transactional Memory TS optimize_for_synchronized attribute is
26122 equivalent to GNU transaction_callable. */
26123 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
26124 TREE_PURPOSE (attribute
)
26125 = get_identifier ("transaction_callable");
26126 /* Transactional Memory attributes are GNU attributes. */
26127 else if (tm_attr_to_mask (attr_id
))
26128 TREE_PURPOSE (attribute
) = attr_id
;
26131 /* Now parse the optional argument clause of the attribute. */
26133 if (token
->type
!= CPP_OPEN_PAREN
)
26137 vec
<tree
, va_gc
> *vec
;
26138 int attr_flag
= normal_attr
;
26140 if (attr_ns
== gnu_identifier
26141 && attribute_takes_identifier_p (attr_id
))
26142 /* A GNU attribute that takes an identifier in parameter. */
26143 attr_flag
= id_attr
;
26145 vec
= cp_parser_parenthesized_expression_list
26146 (parser
, attr_flag
, /*cast_p=*/false,
26147 /*allow_expansion_p=*/true,
26148 /*non_constant_p=*/NULL
);
26150 arguments
= error_mark_node
;
26153 arguments
= build_tree_list_vec (vec
);
26154 release_tree_vector (vec
);
26157 if (arguments
== error_mark_node
)
26158 attribute
= error_mark_node
;
26160 TREE_VALUE (attribute
) = arguments
;
26166 /* Check that the attribute ATTRIBUTE appears at most once in the
26167 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26168 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26169 isn't implemented yet in GCC. */
26172 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
26176 tree name
= get_attribute_name (attribute
);
26177 if (is_attribute_p ("noreturn", name
)
26178 && lookup_attribute ("noreturn", attributes
))
26179 error ("attribute %<noreturn%> can appear at most once "
26180 "in an attribute-list");
26181 else if (is_attribute_p ("deprecated", name
)
26182 && lookup_attribute ("deprecated", attributes
))
26183 error ("attribute %<deprecated%> can appear at most once "
26184 "in an attribute-list");
26188 /* Parse a list of standard C++-11 attributes.
26192 attribute-list , attribute[opt]
26194 attribute-list , attribute ...
26198 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
26200 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
26201 cp_token
*token
= NULL
;
26205 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
26206 if (attribute
== error_mark_node
)
26208 if (attribute
!= NULL_TREE
)
26210 cp_parser_check_std_attribute (attributes
, attribute
);
26211 TREE_CHAIN (attribute
) = attributes
;
26212 attributes
= attribute
;
26214 token
= cp_lexer_peek_token (parser
->lexer
);
26215 if (token
->type
== CPP_ELLIPSIS
)
26217 cp_lexer_consume_token (parser
->lexer
);
26218 if (attribute
== NULL_TREE
)
26219 error_at (token
->location
,
26220 "expected attribute before %<...%>");
26223 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
26224 if (pack
== error_mark_node
)
26225 return error_mark_node
;
26226 TREE_VALUE (attribute
) = pack
;
26228 token
= cp_lexer_peek_token (parser
->lexer
);
26230 if (token
->type
!= CPP_COMMA
)
26232 cp_lexer_consume_token (parser
->lexer
);
26234 attributes
= nreverse (attributes
);
26238 /* Parse a standard C++-11 attribute specifier.
26240 attribute-specifier:
26241 [ [ attribute-using-prefix [opt] attribute-list ] ]
26242 alignment-specifier
26244 attribute-using-prefix:
26245 using attribute-namespace :
26247 alignment-specifier:
26248 alignas ( type-id ... [opt] )
26249 alignas ( alignment-expression ... [opt] ). */
26252 cp_parser_std_attribute_spec (cp_parser
*parser
)
26254 tree attributes
= NULL_TREE
;
26255 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26257 if (token
->type
== CPP_OPEN_SQUARE
26258 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
26260 tree attr_ns
= NULL_TREE
;
26262 cp_lexer_consume_token (parser
->lexer
);
26263 cp_lexer_consume_token (parser
->lexer
);
26265 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26267 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26268 if (token
->type
== CPP_NAME
)
26269 attr_ns
= token
->u
.value
;
26270 else if (token
->type
== CPP_KEYWORD
)
26271 attr_ns
= ridpointers
[(int) token
->keyword
];
26272 else if (token
->flags
& NAMED_OP
)
26273 attr_ns
= get_identifier (cpp_type2name (token
->type
,
26276 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
26278 if (cxx_dialect
< cxx17
26279 && !in_system_header_at (input_location
))
26280 pedwarn (input_location
, 0,
26281 "attribute using prefix only available "
26282 "with %<-std=c++17%> or %<-std=gnu++17%>");
26284 cp_lexer_consume_token (parser
->lexer
);
26285 cp_lexer_consume_token (parser
->lexer
);
26286 cp_lexer_consume_token (parser
->lexer
);
26289 attr_ns
= NULL_TREE
;
26292 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
26294 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
26295 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
26296 cp_parser_skip_to_end_of_statement (parser
);
26298 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26299 when we are sure that we have actually parsed them. */
26300 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26306 /* Look for an alignment-specifier. */
26308 token
= cp_lexer_peek_token (parser
->lexer
);
26310 if (token
->type
!= CPP_KEYWORD
26311 || token
->keyword
!= RID_ALIGNAS
)
26314 cp_lexer_consume_token (parser
->lexer
);
26315 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26317 matching_parens parens
;
26318 if (!parens
.require_open (parser
))
26319 return error_mark_node
;
26321 cp_parser_parse_tentatively (parser
);
26322 alignas_expr
= cp_parser_type_id (parser
);
26324 if (!cp_parser_parse_definitely (parser
))
26326 alignas_expr
= cp_parser_assignment_expression (parser
);
26327 if (alignas_expr
== error_mark_node
)
26328 cp_parser_skip_to_end_of_statement (parser
);
26329 if (alignas_expr
== NULL_TREE
26330 || alignas_expr
== error_mark_node
)
26331 return alignas_expr
;
26334 alignas_expr
= cxx_alignas_expr (alignas_expr
);
26335 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
26337 /* Handle alignas (pack...). */
26338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26340 cp_lexer_consume_token (parser
->lexer
);
26341 alignas_expr
= make_pack_expansion (alignas_expr
);
26344 /* Something went wrong, so don't build the attribute. */
26345 if (alignas_expr
== error_mark_node
)
26346 return error_mark_node
;
26348 if (!parens
.require_close (parser
))
26349 return error_mark_node
;
26351 /* Build the C++-11 representation of an 'aligned'
26354 = build_tree_list (build_tree_list (gnu_identifier
,
26355 aligned_identifier
), alignas_expr
);
26361 /* Parse a standard C++-11 attribute-specifier-seq.
26363 attribute-specifier-seq:
26364 attribute-specifier-seq [opt] attribute-specifier
26368 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
26370 tree attr_specs
= NULL_TREE
;
26371 tree attr_last
= NULL_TREE
;
26373 /* Don't create wrapper nodes within attributes: the
26374 handlers don't know how to handle them. */
26375 auto_suppress_location_wrappers sentinel
;
26379 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
26380 if (attr_spec
== NULL_TREE
)
26382 if (attr_spec
== error_mark_node
)
26383 return error_mark_node
;
26386 TREE_CHAIN (attr_last
) = attr_spec
;
26388 attr_specs
= attr_last
= attr_spec
;
26389 attr_last
= tree_last (attr_last
);
26395 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26396 return index of the first token after balanced-token, or N on failure. */
26399 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26402 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26404 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26406 case CPP_PRAGMA_EOL
:
26407 if (!parser
->lexer
->in_pragma
)
26411 /* Ran out of tokens. */
26413 case CPP_OPEN_PAREN
:
26416 case CPP_OPEN_BRACE
:
26419 case CPP_OPEN_SQUARE
:
26422 case CPP_CLOSE_PAREN
:
26425 case CPP_CLOSE_BRACE
:
26428 case CPP_CLOSE_SQUARE
:
26434 while (nparens
|| nbraces
|| nsquares
);
26438 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26439 return index of the first token after the GNU attribute tokens, or N on
26443 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26447 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26448 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26449 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26452 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26455 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26462 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26463 next token), return index of the first token after the standard C++11
26464 attribute tokens, or N on failure. */
26467 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26471 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26472 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26474 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26477 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26481 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26482 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26484 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26495 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26496 as the next token), return index of the first token after the attribute
26497 tokens, or N on failure. */
26500 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26502 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26503 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26504 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26507 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26508 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26509 current value of the PEDANTIC flag, regardless of whether or not
26510 the `__extension__' keyword is present. The caller is responsible
26511 for restoring the value of the PEDANTIC flag. */
26514 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26516 /* Save the old value of the PEDANTIC flag. */
26517 *saved_pedantic
= pedantic
;
26519 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26521 /* Consume the `__extension__' token. */
26522 cp_lexer_consume_token (parser
->lexer
);
26523 /* We're not being pedantic while the `__extension__' keyword is
26533 /* Parse a label declaration.
26536 __label__ label-declarator-seq ;
26538 label-declarator-seq:
26539 identifier , label-declarator-seq
26543 cp_parser_label_declaration (cp_parser
* parser
)
26545 /* Look for the `__label__' keyword. */
26546 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26552 /* Look for an identifier. */
26553 identifier
= cp_parser_identifier (parser
);
26554 /* If we failed, stop. */
26555 if (identifier
== error_mark_node
)
26557 /* Declare it as a label. */
26558 finish_label_decl (identifier
);
26559 /* If the next token is a `;', stop. */
26560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26562 /* Look for the `,' separating the label declarations. */
26563 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26566 /* Look for the final `;'. */
26567 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26570 // -------------------------------------------------------------------------- //
26573 // Parse a requires clause.
26575 // requires-clause:
26576 // 'requires' logical-or-expression
26578 // The required logical-or-expression must be a constant expression. Note
26579 // that we don't check that the expression is constepxr here. We defer until
26580 // we analyze constraints and then, we only check atomic constraints.
26582 cp_parser_requires_clause (cp_parser
*parser
)
26584 // Parse the requires clause so that it is not automatically folded.
26585 ++processing_template_decl
;
26586 tree expr
= cp_parser_binary_expression (parser
, false, false,
26587 PREC_NOT_OPERATOR
, NULL
);
26588 if (check_for_bare_parameter_packs (expr
))
26589 expr
= error_mark_node
;
26590 --processing_template_decl
;
26594 // Optionally parse a requires clause:
26596 cp_parser_requires_clause_opt (cp_parser
*parser
)
26598 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26599 if (tok
->keyword
!= RID_REQUIRES
)
26601 if (!flag_concepts
&& tok
->type
== CPP_NAME
26602 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26604 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26605 "%<requires%> only available with %<-fconcepts%>");
26606 /* Parse and discard the requires-clause. */
26607 cp_lexer_consume_token (parser
->lexer
);
26608 cp_parser_requires_clause (parser
);
26612 cp_lexer_consume_token (parser
->lexer
);
26613 return cp_parser_requires_clause (parser
);
26617 /*---------------------------------------------------------------------------
26618 Requires expressions
26619 ---------------------------------------------------------------------------*/
26621 /* Parse a requires expression
26623 requirement-expression:
26624 'requires' requirement-parameter-list [opt] requirement-body */
26626 cp_parser_requires_expression (cp_parser
*parser
)
26628 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26629 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26631 /* A requires-expression shall appear only within a concept
26632 definition or a requires-clause.
26634 TODO: Implement this diagnostic correctly. */
26635 if (!processing_template_decl
)
26637 error_at (loc
, "a requires expression cannot appear outside a template");
26638 cp_parser_skip_to_end_of_statement (parser
);
26639 return error_mark_node
;
26644 /* Local parameters are delared as variables within the scope
26645 of the expression. They are not visible past the end of
26646 the expression. Expressions within the requires-expression
26647 are unevaluated. */
26648 struct scope_sentinel
26652 ++cp_unevaluated_operand
;
26653 begin_scope (sk_block
, NULL_TREE
);
26658 pop_bindings_and_leave_scope ();
26659 --cp_unevaluated_operand
;
26663 /* Parse the optional parameter list. */
26664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26666 parms
= cp_parser_requirement_parameter_list (parser
);
26667 if (parms
== error_mark_node
)
26668 return error_mark_node
;
26673 /* Parse the requirement body. */
26674 reqs
= cp_parser_requirement_body (parser
);
26675 if (reqs
== error_mark_node
)
26676 return error_mark_node
;
26679 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26681 grokparms (parms
, &parms
);
26682 return finish_requires_expr (parms
, reqs
);
26685 /* Parse a parameterized requirement.
26687 requirement-parameter-list:
26688 '(' parameter-declaration-clause ')' */
26690 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26692 matching_parens parens
;
26693 if (!parens
.require_open (parser
))
26694 return error_mark_node
;
26697 = cp_parser_parameter_declaration_clause (parser
, CP_PARSER_FLAGS_NONE
);
26699 if (!parens
.require_close (parser
))
26700 return error_mark_node
;
26705 /* Parse the body of a requirement.
26708 '{' requirement-list '}' */
26710 cp_parser_requirement_body (cp_parser
*parser
)
26712 matching_braces braces
;
26713 if (!braces
.require_open (parser
))
26714 return error_mark_node
;
26716 tree reqs
= cp_parser_requirement_list (parser
);
26718 if (!braces
.require_close (parser
))
26719 return error_mark_node
;
26724 /* Parse a list of requirements.
26728 requirement-list ';' requirement[opt] */
26730 cp_parser_requirement_list (cp_parser
*parser
)
26732 tree result
= NULL_TREE
;
26735 tree req
= cp_parser_requirement (parser
);
26736 if (req
== error_mark_node
)
26737 return error_mark_node
;
26739 result
= tree_cons (NULL_TREE
, req
, result
);
26741 /* If we see a semi-colon, consume it. */
26742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26743 cp_lexer_consume_token (parser
->lexer
);
26745 /* Stop processing at the end of the list. */
26746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26750 /* Reverse the order of requirements so they are analyzed in
26751 declaration order. */
26752 return nreverse (result
);
26755 /* Parse a syntactic requirement or type requirement.
26759 compound-requirement
26761 nested-requirement */
26763 cp_parser_requirement (cp_parser
*parser
)
26765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26766 return cp_parser_compound_requirement (parser
);
26767 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26768 return cp_parser_type_requirement (parser
);
26769 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26770 return cp_parser_nested_requirement (parser
);
26772 return cp_parser_simple_requirement (parser
);
26775 /* Parse a simple requirement.
26777 simple-requirement:
26780 cp_parser_simple_requirement (cp_parser
*parser
)
26782 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26783 if (!expr
|| expr
== error_mark_node
)
26784 return error_mark_node
;
26786 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26787 return error_mark_node
;
26789 return finish_simple_requirement (expr
);
26792 /* Parse a type requirement
26795 nested-name-specifier [opt] required-type-name ';'
26797 required-type-name:
26799 'template' [opt] simple-template-id */
26801 cp_parser_type_requirement (cp_parser
*parser
)
26803 cp_lexer_consume_token (parser
->lexer
);
26805 // Save the scope before parsing name specifiers.
26806 tree saved_scope
= parser
->scope
;
26807 tree saved_object_scope
= parser
->object_scope
;
26808 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26809 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26810 cp_parser_nested_name_specifier_opt (parser
,
26811 /*typename_keyword_p=*/true,
26812 /*check_dependency_p=*/false,
26814 /*is_declaration=*/false);
26817 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26819 cp_lexer_consume_token (parser
->lexer
);
26820 type
= cp_parser_template_id (parser
,
26821 /*template_keyword_p=*/true,
26822 /*check_dependency=*/false,
26823 /*tag_type=*/none_type
,
26824 /*is_declaration=*/false);
26825 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26826 /*complain=*/tf_error
);
26829 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26831 if (TREE_CODE (type
) == TYPE_DECL
)
26832 type
= TREE_TYPE (type
);
26834 parser
->scope
= saved_scope
;
26835 parser
->object_scope
= saved_object_scope
;
26836 parser
->qualifying_scope
= saved_qualifying_scope
;
26838 if (type
== error_mark_node
)
26839 cp_parser_skip_to_end_of_statement (parser
);
26841 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26842 return error_mark_node
;
26843 if (type
== error_mark_node
)
26844 return error_mark_node
;
26846 return finish_type_requirement (type
);
26849 /* Parse a compound requirement
26851 compound-requirement:
26852 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26854 cp_parser_compound_requirement (cp_parser
*parser
)
26856 /* Parse an expression enclosed in '{ }'s. */
26857 matching_braces braces
;
26858 if (!braces
.require_open (parser
))
26859 return error_mark_node
;
26861 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26862 if (!expr
|| expr
== error_mark_node
)
26863 return error_mark_node
;
26865 if (!braces
.require_close (parser
))
26866 return error_mark_node
;
26868 /* Parse the optional noexcept. */
26869 bool noexcept_p
= false;
26870 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26872 cp_lexer_consume_token (parser
->lexer
);
26876 /* Parse the optional trailing return type. */
26877 tree type
= NULL_TREE
;
26878 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26880 cp_lexer_consume_token (parser
->lexer
);
26881 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26882 parser
->in_result_type_constraint_p
= true;
26883 type
= cp_parser_trailing_type_id (parser
);
26884 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26885 if (type
== error_mark_node
)
26886 return error_mark_node
;
26889 return finish_compound_requirement (expr
, type
, noexcept_p
);
26892 /* Parse a nested requirement. This is the same as a requires clause.
26894 nested-requirement:
26897 cp_parser_nested_requirement (cp_parser
*parser
)
26899 cp_lexer_consume_token (parser
->lexer
);
26900 tree req
= cp_parser_requires_clause (parser
);
26901 if (req
== error_mark_node
)
26902 return error_mark_node
;
26903 return finish_nested_requirement (req
);
26906 /* Support Functions */
26908 /* Return the appropriate prefer_type argument for lookup_name_real based on
26909 tag_type and template_mem_access. */
26912 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26914 /* DR 141: When looking in the current enclosing context for a template-name
26915 after -> or ., only consider class templates. */
26916 if (template_mem_access
)
26920 case none_type
: return 0; // No preference.
26921 case scope_type
: return 1; // Type or namespace.
26922 default: return 2; // Type only.
26926 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26927 NAME should have one of the representations used for an
26928 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26929 is returned. If PARSER->SCOPE is a dependent type, then a
26930 SCOPE_REF is returned.
26932 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26933 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26934 was formed. Abstractly, such entities should not be passed to this
26935 function, because they do not need to be looked up, but it is
26936 simpler to check for this special case here, rather than at the
26939 In cases not explicitly covered above, this function returns a
26940 DECL, OVERLOAD, or baselink representing the result of the lookup.
26941 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26944 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26945 (e.g., "struct") that was used. In that case bindings that do not
26946 refer to types are ignored.
26948 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26951 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26954 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26957 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26958 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26959 NULL_TREE otherwise. */
26962 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26963 enum tag_types tag_type
,
26966 bool check_dependency
,
26967 tree
*ambiguous_decls
,
26968 location_t name_location
)
26971 tree object_type
= parser
->context
->object_type
;
26973 /* Assume that the lookup will be unambiguous. */
26974 if (ambiguous_decls
)
26975 *ambiguous_decls
= NULL_TREE
;
26977 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26978 no longer valid. Note that if we are parsing tentatively, and
26979 the parse fails, OBJECT_TYPE will be automatically restored. */
26980 parser
->context
->object_type
= NULL_TREE
;
26982 if (name
== error_mark_node
)
26983 return error_mark_node
;
26985 /* A template-id has already been resolved; there is no lookup to
26987 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26989 if (BASELINK_P (name
))
26991 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26992 == TEMPLATE_ID_EXPR
);
26996 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26997 it should already have been checked to make sure that the name
26998 used matches the type being destroyed. */
26999 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
27003 /* Figure out to which type this destructor applies. */
27005 type
= parser
->scope
;
27006 else if (object_type
)
27007 type
= object_type
;
27009 type
= current_class_type
;
27010 /* If that's not a class type, there is no destructor. */
27011 if (!type
|| !CLASS_TYPE_P (type
))
27012 return error_mark_node
;
27014 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
27015 lazily_declare_fn (sfk_destructor
, type
);
27017 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
27020 return error_mark_node
;
27023 /* By this point, the NAME should be an ordinary identifier. If
27024 the id-expression was a qualified name, the qualifying scope is
27025 stored in PARSER->SCOPE at this point. */
27026 gcc_assert (identifier_p (name
));
27028 /* Perform the lookup. */
27033 if (parser
->scope
== error_mark_node
)
27034 return error_mark_node
;
27036 /* If the SCOPE is dependent, the lookup must be deferred until
27037 the template is instantiated -- unless we are explicitly
27038 looking up names in uninstantiated templates. Even then, we
27039 cannot look up the name if the scope is not a class type; it
27040 might, for example, be a template type parameter. */
27041 dependent_p
= (TYPE_P (parser
->scope
)
27042 && dependent_scope_p (parser
->scope
));
27043 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
27045 /* Defer lookup. */
27046 decl
= error_mark_node
;
27049 tree pushed_scope
= NULL_TREE
;
27051 /* If PARSER->SCOPE is a dependent type, then it must be a
27052 class type, and we must not be checking dependencies;
27053 otherwise, we would have processed this lookup above. So
27054 that PARSER->SCOPE is not considered a dependent base by
27055 lookup_member, we must enter the scope here. */
27057 pushed_scope
= push_scope (parser
->scope
);
27059 /* If the PARSER->SCOPE is a template specialization, it
27060 may be instantiated during name lookup. In that case,
27061 errors may be issued. Even if we rollback the current
27062 tentative parse, those errors are valid. */
27063 decl
= lookup_qualified_name (parser
->scope
, name
,
27064 prefer_type_arg (tag_type
),
27065 /*complain=*/true);
27067 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27068 lookup result and the nested-name-specifier nominates a class C:
27069 * if the name specified after the nested-name-specifier, when
27070 looked up in C, is the injected-class-name of C (Clause 9), or
27071 * if the name specified after the nested-name-specifier is the
27072 same as the identifier or the simple-template-id's template-
27073 name in the last component of the nested-name-specifier,
27074 the name is instead considered to name the constructor of
27075 class C. [ Note: for example, the constructor is not an
27076 acceptable lookup result in an elaborated-type-specifier so
27077 the constructor would not be used in place of the
27078 injected-class-name. --end note ] Such a constructor name
27079 shall be used only in the declarator-id of a declaration that
27080 names a constructor or in a using-declaration. */
27081 if (tag_type
== none_type
27082 && DECL_SELF_REFERENCE_P (decl
)
27083 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
27084 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
27085 prefer_type_arg (tag_type
),
27086 /*complain=*/true);
27088 /* If we have a single function from a using decl, pull it out. */
27089 if (TREE_CODE (decl
) == OVERLOAD
27090 && !really_overloaded_fn (decl
))
27091 decl
= OVL_FUNCTION (decl
);
27094 pop_scope (pushed_scope
);
27097 /* If the scope is a dependent type and either we deferred lookup or
27098 we did lookup but didn't find the name, rememeber the name. */
27099 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
27100 && dependent_type_p (parser
->scope
))
27106 /* The resolution to Core Issue 180 says that `struct
27107 A::B' should be considered a type-name, even if `A'
27109 type
= make_typename_type (parser
->scope
, name
, tag_type
,
27110 /*complain=*/tf_error
);
27111 if (type
!= error_mark_node
)
27112 decl
= TYPE_NAME (type
);
27114 else if (is_template
27115 && (cp_parser_next_token_ends_template_argument_p (parser
)
27116 || cp_lexer_next_token_is (parser
->lexer
,
27118 decl
= make_unbound_class_template (parser
->scope
,
27120 /*complain=*/tf_error
);
27122 decl
= build_qualified_name (/*type=*/NULL_TREE
,
27123 parser
->scope
, name
,
27126 parser
->qualifying_scope
= parser
->scope
;
27127 parser
->object_scope
= NULL_TREE
;
27129 else if (object_type
)
27131 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27132 OBJECT_TYPE is not a class. */
27133 if (CLASS_TYPE_P (object_type
))
27134 /* If the OBJECT_TYPE is a template specialization, it may
27135 be instantiated during name lookup. In that case, errors
27136 may be issued. Even if we rollback the current tentative
27137 parse, those errors are valid. */
27138 decl
= lookup_member (object_type
,
27141 prefer_type_arg (tag_type
),
27142 tf_warning_or_error
);
27147 /* Look it up in the enclosing context. DR 141: When looking for a
27148 template-name after -> or ., only consider class templates. */
27149 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
27151 /*block_p=*/true, is_namespace
, 0);
27152 if (object_type
== unknown_type_node
)
27153 /* The object is type-dependent, so we can't look anything up; we used
27154 this to get the DR 141 behavior. */
27155 object_type
= NULL_TREE
;
27156 parser
->object_scope
= object_type
;
27157 parser
->qualifying_scope
= NULL_TREE
;
27161 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
27163 /*block_p=*/true, is_namespace
, 0);
27164 parser
->qualifying_scope
= NULL_TREE
;
27165 parser
->object_scope
= NULL_TREE
;
27168 /* If the lookup failed, let our caller know. */
27169 if (!decl
|| decl
== error_mark_node
)
27170 return error_mark_node
;
27172 /* Pull out the template from an injected-class-name (or multiple). */
27174 decl
= maybe_get_template_decl_from_type_decl (decl
);
27176 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27177 if (TREE_CODE (decl
) == TREE_LIST
)
27179 if (ambiguous_decls
)
27180 *ambiguous_decls
= decl
;
27181 /* The error message we have to print is too complicated for
27182 cp_parser_error, so we incorporate its actions directly. */
27183 if (!cp_parser_simulate_error (parser
))
27185 error_at (name_location
, "reference to %qD is ambiguous",
27187 print_candidates (decl
);
27189 return error_mark_node
;
27192 gcc_assert (DECL_P (decl
)
27193 || TREE_CODE (decl
) == OVERLOAD
27194 || TREE_CODE (decl
) == SCOPE_REF
27195 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
27196 || BASELINK_P (decl
));
27198 /* If we have resolved the name of a member declaration, check to
27199 see if the declaration is accessible. When the name resolves to
27200 set of overloaded functions, accessibility is checked when
27201 overload resolution is done.
27203 During an explicit instantiation, access is not checked at all,
27204 as per [temp.explicit]. */
27206 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
27208 maybe_record_typedef_use (decl
);
27210 return cp_expr (decl
, name_location
);
27213 /* Like cp_parser_lookup_name, but for use in the typical case where
27214 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27215 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27218 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
27220 return cp_parser_lookup_name (parser
, name
,
27222 /*is_template=*/false,
27223 /*is_namespace=*/false,
27224 /*check_dependency=*/true,
27225 /*ambiguous_decls=*/NULL
,
27229 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27230 the current context, return the TYPE_DECL. If TAG_NAME_P is
27231 true, the DECL indicates the class being defined in a class-head,
27232 or declared in an elaborated-type-specifier.
27234 Otherwise, return DECL. */
27237 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
27239 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27240 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27243 template <typename T> struct B;
27246 template <typename T> struct A::B {};
27248 Similarly, in an elaborated-type-specifier:
27250 namespace N { struct X{}; }
27253 template <typename T> friend struct N::X;
27256 However, if the DECL refers to a class type, and we are in
27257 the scope of the class, then the name lookup automatically
27258 finds the TYPE_DECL created by build_self_reference rather
27259 than a TEMPLATE_DECL. For example, in:
27261 template <class T> struct S {
27265 there is no need to handle such case. */
27267 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
27268 return DECL_TEMPLATE_RESULT (decl
);
27273 /* If too many, or too few, template-parameter lists apply to the
27274 declarator, issue an error message. Returns TRUE if all went well,
27275 and FALSE otherwise. */
27278 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
27279 cp_declarator
*declarator
,
27280 location_t declarator_location
)
27282 switch (declarator
->kind
)
27286 unsigned num_templates
= 0;
27287 tree scope
= declarator
->u
.id
.qualifying_scope
;
27288 bool template_id_p
= false;
27291 num_templates
= num_template_headers_for_class (scope
);
27292 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
27293 == TEMPLATE_ID_EXPR
)
27295 /* If the DECLARATOR has the form `X<y>' then it uses one
27296 additional level of template parameters. */
27298 template_id_p
= true;
27301 return cp_parser_check_template_parameters
27302 (parser
, num_templates
, template_id_p
, declarator_location
,
27309 case cdk_reference
:
27311 return (cp_parser_check_declarator_template_parameters
27312 (parser
, declarator
->declarator
, declarator_location
));
27319 gcc_unreachable ();
27324 /* NUM_TEMPLATES were used in the current declaration. If that is
27325 invalid, return FALSE and issue an error messages. Otherwise,
27326 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27327 declarator and we can print more accurate diagnostics. */
27330 cp_parser_check_template_parameters (cp_parser
* parser
,
27331 unsigned num_templates
,
27332 bool template_id_p
,
27333 location_t location
,
27334 cp_declarator
*declarator
)
27336 /* If there are the same number of template classes and parameter
27337 lists, that's OK. */
27338 if (parser
->num_template_parameter_lists
== num_templates
)
27340 /* If there are more, but only one more, and the name ends in an identifier,
27341 then we are declaring a primary template. That's OK too. */
27343 && parser
->num_template_parameter_lists
== num_templates
+ 1)
27345 /* If there are more template classes than parameter lists, we have
27348 template <class T> void S<T>::R<T>::f (); */
27349 if (parser
->num_template_parameter_lists
< num_templates
)
27351 if (declarator
&& !current_function_decl
)
27352 error_at (location
, "specializing member %<%T::%E%> "
27353 "requires %<template<>%> syntax",
27354 declarator
->u
.id
.qualifying_scope
,
27355 declarator
->u
.id
.unqualified_name
);
27356 else if (declarator
)
27357 error_at (location
, "invalid declaration of %<%T::%E%>",
27358 declarator
->u
.id
.qualifying_scope
,
27359 declarator
->u
.id
.unqualified_name
);
27361 error_at (location
, "too few template-parameter-lists");
27364 /* Otherwise, there are too many template parameter lists. We have
27367 template <class T> template <class U> void S::f(); */
27368 error_at (location
, "too many template-parameter-lists");
27372 /* Parse an optional `::' token indicating that the following name is
27373 from the global namespace. If so, PARSER->SCOPE is set to the
27374 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27375 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27376 Returns the new value of PARSER->SCOPE, if the `::' token is
27377 present, and NULL_TREE otherwise. */
27380 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
27384 /* Peek at the next token. */
27385 token
= cp_lexer_peek_token (parser
->lexer
);
27386 /* If we're looking at a `::' token then we're starting from the
27387 global namespace, not our current location. */
27388 if (token
->type
== CPP_SCOPE
)
27390 /* Consume the `::' token. */
27391 cp_lexer_consume_token (parser
->lexer
);
27392 /* Set the SCOPE so that we know where to start the lookup. */
27393 parser
->scope
= global_namespace
;
27394 parser
->qualifying_scope
= global_namespace
;
27395 parser
->object_scope
= NULL_TREE
;
27397 return parser
->scope
;
27399 else if (!current_scope_valid_p
)
27401 parser
->scope
= NULL_TREE
;
27402 parser
->qualifying_scope
= NULL_TREE
;
27403 parser
->object_scope
= NULL_TREE
;
27409 /* Returns TRUE if the upcoming token sequence is the start of a
27410 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27411 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27412 is used to control type-specifier parsing. */
27415 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
27418 bool constructor_p
;
27419 bool outside_class_specifier_p
;
27420 tree nested_name_specifier
;
27421 cp_token
*next_token
;
27423 /* The common case is that this is not a constructor declarator, so
27424 try to avoid doing lots of work if at all possible. It's not
27425 valid declare a constructor at function scope. */
27426 if (parser
->in_function_body
)
27428 /* And only certain tokens can begin a constructor declarator. */
27429 next_token
= cp_lexer_peek_token (parser
->lexer
);
27430 if (next_token
->type
!= CPP_NAME
27431 && next_token
->type
!= CPP_SCOPE
27432 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27433 && next_token
->type
!= CPP_TEMPLATE_ID
)
27436 /* Parse tentatively; we are going to roll back all of the tokens
27438 cp_parser_parse_tentatively (parser
);
27439 /* Assume that we are looking at a constructor declarator. */
27440 constructor_p
= true;
27442 /* Look for the optional `::' operator. */
27443 cp_parser_global_scope_opt (parser
,
27444 /*current_scope_valid_p=*/false);
27445 /* Look for the nested-name-specifier. */
27446 nested_name_specifier
27447 = (cp_parser_nested_name_specifier_opt (parser
,
27448 /*typename_keyword_p=*/false,
27449 /*check_dependency_p=*/false,
27451 /*is_declaration=*/false));
27453 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27454 if (nested_name_specifier
27455 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
27457 tree s
= resolve_typename_type (nested_name_specifier
,
27458 /*only_current_p=*/false);
27459 if (TREE_CODE (s
) != TYPENAME_TYPE
)
27460 nested_name_specifier
= s
;
27463 outside_class_specifier_p
= (!at_class_scope_p ()
27464 || !TYPE_BEING_DEFINED (current_class_type
)
27467 /* Outside of a class-specifier, there must be a
27468 nested-name-specifier. Except in C++17 mode, where we
27469 might be declaring a guiding declaration. */
27470 if (!nested_name_specifier
&& outside_class_specifier_p
27471 && cxx_dialect
< cxx17
)
27472 constructor_p
= false;
27473 else if (nested_name_specifier
== error_mark_node
)
27474 constructor_p
= false;
27476 /* If we have a class scope, this is easy; DR 147 says that S::S always
27477 names the constructor, and no other qualified name could. */
27478 if (constructor_p
&& nested_name_specifier
27479 && CLASS_TYPE_P (nested_name_specifier
))
27481 tree id
= cp_parser_unqualified_id (parser
,
27482 /*template_keyword_p=*/false,
27483 /*check_dependency_p=*/false,
27484 /*declarator_p=*/true,
27485 /*optional_p=*/false);
27486 if (is_overloaded_fn (id
))
27487 id
= DECL_NAME (get_first_fn (id
));
27488 if (!constructor_name_p (id
, nested_name_specifier
))
27489 constructor_p
= false;
27491 /* If we still think that this might be a constructor-declarator,
27492 look for a class-name. */
27493 else if (constructor_p
)
27497 template <typename T> struct S {
27501 we must recognize that the nested `S' names a class. */
27502 if (cxx_dialect
>= cxx17
)
27503 cp_parser_parse_tentatively (parser
);
27506 type_decl
= cp_parser_class_name (parser
,
27507 /*typename_keyword_p=*/false,
27508 /*template_keyword_p=*/false,
27510 /*check_dependency_p=*/false,
27511 /*class_head_p=*/false,
27512 /*is_declaration=*/false);
27514 if (cxx_dialect
>= cxx17
27515 && !cp_parser_parse_definitely (parser
))
27517 type_decl
= NULL_TREE
;
27518 tree tmpl
= cp_parser_template_name (parser
,
27519 /*template_keyword*/false,
27520 /*check_dependency_p*/false,
27521 /*is_declaration*/false,
27523 /*is_identifier*/NULL
);
27524 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27525 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27526 /* It's a deduction guide, return true. */;
27528 cp_parser_simulate_error (parser
);
27531 /* If there was no class-name, then this is not a constructor.
27532 Otherwise, if we are in a class-specifier and we aren't
27533 handling a friend declaration, check that its type matches
27534 current_class_type (c++/38313). Note: error_mark_node
27535 is left alone for error recovery purposes. */
27536 constructor_p
= (!cp_parser_error_occurred (parser
)
27537 && (outside_class_specifier_p
27538 || type_decl
== NULL_TREE
27539 || type_decl
== error_mark_node
27540 || same_type_p (current_class_type
,
27541 TREE_TYPE (type_decl
))));
27543 /* If we're still considering a constructor, we have to see a `(',
27544 to begin the parameter-declaration-clause, followed by either a
27545 `)', an `...', or a decl-specifier. We need to check for a
27546 type-specifier to avoid being fooled into thinking that:
27550 is a constructor. (It is actually a function named `f' that
27551 takes one parameter (of type `int') and returns a value of type
27554 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27555 constructor_p
= false;
27558 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27559 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27560 /* A parameter declaration begins with a decl-specifier,
27561 which is either the "attribute" keyword, a storage class
27562 specifier, or (usually) a type-specifier. */
27563 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27566 tree pushed_scope
= NULL_TREE
;
27567 unsigned saved_num_template_parameter_lists
;
27569 /* Names appearing in the type-specifier should be looked up
27570 in the scope of the class. */
27571 if (current_class_type
)
27573 else if (type_decl
)
27575 type
= TREE_TYPE (type_decl
);
27576 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27578 type
= resolve_typename_type (type
,
27579 /*only_current_p=*/false);
27580 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27582 cp_parser_abort_tentative_parse (parser
);
27586 pushed_scope
= push_scope (type
);
27589 /* Inside the constructor parameter list, surrounding
27590 template-parameter-lists do not apply. */
27591 saved_num_template_parameter_lists
27592 = parser
->num_template_parameter_lists
;
27593 parser
->num_template_parameter_lists
= 0;
27595 /* Look for the type-specifier. It's not optional, but its typename
27597 cp_parser_type_specifier (parser
,
27598 (flags
& ~CP_PARSER_FLAGS_OPTIONAL
),
27599 /*decl_specs=*/NULL
,
27600 /*is_declarator=*/true,
27601 /*declares_class_or_enum=*/NULL
,
27602 /*is_cv_qualifier=*/NULL
);
27604 parser
->num_template_parameter_lists
27605 = saved_num_template_parameter_lists
;
27607 /* Leave the scope of the class. */
27609 pop_scope (pushed_scope
);
27611 constructor_p
= !cp_parser_error_occurred (parser
);
27615 /* We did not really want to consume any tokens. */
27616 cp_parser_abort_tentative_parse (parser
);
27618 return constructor_p
;
27621 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27622 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27623 they must be performed once we are in the scope of the function.
27625 Returns the function defined. */
27628 cp_parser_function_definition_from_specifiers_and_declarator
27629 (cp_parser
* parser
,
27630 cp_decl_specifier_seq
*decl_specifiers
,
27632 const cp_declarator
*declarator
)
27637 /* Begin the function-definition. */
27638 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27640 /* The things we're about to see are not directly qualified by any
27641 template headers we've seen thus far. */
27642 reset_specialization ();
27644 /* If there were names looked up in the decl-specifier-seq that we
27645 did not check, check them now. We must wait until we are in the
27646 scope of the function to perform the checks, since the function
27647 might be a friend. */
27648 perform_deferred_access_checks (tf_warning_or_error
);
27652 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27653 parser
->omp_declare_simd
= NULL
;
27654 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27655 parser
->oacc_routine
= NULL
;
27660 /* Skip the entire function. */
27661 cp_parser_skip_to_end_of_block_or_statement (parser
);
27662 fn
= error_mark_node
;
27664 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27666 /* Seen already, skip it. An error message has already been output. */
27667 cp_parser_skip_to_end_of_block_or_statement (parser
);
27668 fn
= current_function_decl
;
27669 current_function_decl
= NULL_TREE
;
27670 /* If this is a function from a class, pop the nested class. */
27671 if (current_class_name
)
27672 pop_nested_class ();
27677 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27678 tv
= TV_PARSE_INLINE
;
27680 tv
= TV_PARSE_FUNC
;
27682 fn
= cp_parser_function_definition_after_declarator (parser
,
27683 /*inline_p=*/false);
27690 /* Parse the part of a function-definition that follows the
27691 declarator. INLINE_P is TRUE iff this function is an inline
27692 function defined within a class-specifier.
27694 Returns the function defined. */
27697 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27701 bool saved_in_unbraced_linkage_specification_p
;
27702 bool saved_in_function_body
;
27703 unsigned saved_num_template_parameter_lists
;
27705 bool fully_implicit_function_template_p
27706 = parser
->fully_implicit_function_template_p
;
27707 parser
->fully_implicit_function_template_p
= false;
27708 tree implicit_template_parms
27709 = parser
->implicit_template_parms
;
27710 parser
->implicit_template_parms
= 0;
27711 cp_binding_level
* implicit_template_scope
27712 = parser
->implicit_template_scope
;
27713 parser
->implicit_template_scope
= 0;
27715 saved_in_function_body
= parser
->in_function_body
;
27716 parser
->in_function_body
= true;
27717 /* If the next token is `return', then the code may be trying to
27718 make use of the "named return value" extension that G++ used to
27720 token
= cp_lexer_peek_token (parser
->lexer
);
27721 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27723 /* Consume the `return' keyword. */
27724 cp_lexer_consume_token (parser
->lexer
);
27725 /* Look for the identifier that indicates what value is to be
27727 cp_parser_identifier (parser
);
27728 /* Issue an error message. */
27729 error_at (token
->location
,
27730 "named return values are no longer supported");
27731 /* Skip tokens until we reach the start of the function body. */
27734 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27735 if (token
->type
== CPP_OPEN_BRACE
27736 || token
->type
== CPP_EOF
27737 || token
->type
== CPP_PRAGMA_EOL
)
27739 cp_lexer_consume_token (parser
->lexer
);
27742 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27743 anything declared inside `f'. */
27744 saved_in_unbraced_linkage_specification_p
27745 = parser
->in_unbraced_linkage_specification_p
;
27746 parser
->in_unbraced_linkage_specification_p
= false;
27747 /* Inside the function, surrounding template-parameter-lists do not
27749 saved_num_template_parameter_lists
27750 = parser
->num_template_parameter_lists
;
27751 parser
->num_template_parameter_lists
= 0;
27753 /* If the next token is `try', `__transaction_atomic', or
27754 `__transaction_relaxed`, then we are looking at either function-try-block
27755 or function-transaction-block. Note that all of these include the
27757 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27758 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27759 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27760 RID_TRANSACTION_RELAXED
))
27761 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27762 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27763 cp_parser_function_try_block (parser
);
27765 cp_parser_ctor_initializer_opt_and_function_body
27766 (parser
, /*in_function_try_block=*/false);
27768 /* Finish the function. */
27769 fn
= finish_function (inline_p
);
27770 /* Generate code for it, if necessary. */
27771 expand_or_defer_fn (fn
);
27772 /* Restore the saved values. */
27773 parser
->in_unbraced_linkage_specification_p
27774 = saved_in_unbraced_linkage_specification_p
;
27775 parser
->num_template_parameter_lists
27776 = saved_num_template_parameter_lists
;
27777 parser
->in_function_body
= saved_in_function_body
;
27779 parser
->fully_implicit_function_template_p
27780 = fully_implicit_function_template_p
;
27781 parser
->implicit_template_parms
27782 = implicit_template_parms
;
27783 parser
->implicit_template_scope
27784 = implicit_template_scope
;
27786 if (parser
->fully_implicit_function_template_p
)
27787 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27792 /* Parse a template-declaration body (following argument list). */
27795 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27796 tree parameter_list
,
27799 tree decl
= NULL_TREE
;
27800 bool friend_p
= false;
27802 /* We just processed one more parameter list. */
27803 ++parser
->num_template_parameter_lists
;
27805 /* Get the deferred access checks from the parameter list. These
27806 will be checked once we know what is being declared, as for a
27807 member template the checks must be performed in the scope of the
27808 class containing the member. */
27809 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27811 /* Tentatively parse for a new template parameter list, which can either be
27812 the template keyword or a template introduction. */
27813 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27815 else if (cxx_dialect
>= cxx11
27816 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27817 decl
= cp_parser_alias_declaration (parser
);
27820 /* There are no access checks when parsing a template, as we do not
27821 know if a specialization will be a friend. */
27822 push_deferring_access_checks (dk_no_check
);
27823 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27824 decl
= cp_parser_single_declaration (parser
,
27827 /*explicit_specialization_p=*/false,
27829 pop_deferring_access_checks ();
27831 /* If this is a member template declaration, let the front
27833 if (member_p
&& !friend_p
&& decl
)
27835 if (TREE_CODE (decl
) == TYPE_DECL
)
27836 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27838 decl
= finish_member_template_decl (decl
);
27840 else if (friend_p
&& decl
27841 && DECL_DECLARES_TYPE_P (decl
))
27842 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27843 /*complain=*/true);
27845 /* We are done with the current parameter list. */
27846 --parser
->num_template_parameter_lists
;
27848 pop_deferring_access_checks ();
27851 finish_template_decl (parameter_list
);
27853 /* Check the template arguments for a literal operator template. */
27855 && DECL_DECLARES_FUNCTION_P (decl
)
27856 && UDLIT_OPER_P (DECL_NAME (decl
)))
27859 if (parameter_list
== NULL_TREE
)
27863 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27864 if (num_parms
== 1)
27866 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27867 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27868 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27869 /* OK, C++20 string literal operator template. We don't need
27870 to warn in lower dialects here because we will have already
27871 warned about the template parameter. */;
27872 else if (TREE_TYPE (parm
) != char_type_node
27873 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27876 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27878 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27879 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27880 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27881 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27882 if (parm
== error_mark_node
27883 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27884 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27887 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27888 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27889 "ISO C++ did not adopt string literal operator templa"
27890 "tes taking an argument pack of characters");
27897 if (cxx_dialect
> cxx17
)
27898 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
27899 "template %qD has invalid parameter list; expected "
27900 "non-type template parameter pack %<<char...>%> or "
27901 "single non-type parameter of class type",
27904 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
27905 "template %qD has invalid parameter list; expected "
27906 "non-type template parameter pack %<<char...>%>",
27911 /* Register member declarations. */
27912 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27913 finish_member_declaration (decl
);
27914 /* If DECL is a function template, we must return to parse it later.
27915 (Even though there is no definition, there might be default
27916 arguments that need handling.) */
27917 if (member_p
&& decl
27918 && DECL_DECLARES_FUNCTION_P (decl
))
27919 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27922 /* Parse a template introduction header for a template-declaration. Returns
27923 false if tentative parse fails. */
27926 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27928 cp_parser_parse_tentatively (parser
);
27930 tree saved_scope
= parser
->scope
;
27931 tree saved_object_scope
= parser
->object_scope
;
27932 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27934 /* Look for the optional `::' operator. */
27935 cp_parser_global_scope_opt (parser
,
27936 /*current_scope_valid_p=*/false);
27937 /* Look for the nested-name-specifier. */
27938 cp_parser_nested_name_specifier_opt (parser
,
27939 /*typename_keyword_p=*/false,
27940 /*check_dependency_p=*/true,
27942 /*is_declaration=*/false);
27944 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27945 tree concept_name
= cp_parser_identifier (parser
);
27947 /* Look up the concept for which we will be matching
27948 template parameters. */
27949 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27951 parser
->scope
= saved_scope
;
27952 parser
->object_scope
= saved_object_scope
;
27953 parser
->qualifying_scope
= saved_qualifying_scope
;
27955 if (concept_name
== error_mark_node
)
27956 cp_parser_simulate_error (parser
);
27958 /* Look for opening brace for introduction. */
27959 matching_braces braces
;
27960 braces
.require_open (parser
);
27962 if (!cp_parser_parse_definitely (parser
))
27965 push_deferring_access_checks (dk_deferred
);
27967 /* Build vector of placeholder parameters and grab
27968 matching identifiers. */
27969 tree introduction_list
= cp_parser_introduction_list (parser
);
27971 /* Look for closing brace for introduction. */
27972 if (!braces
.require_close (parser
))
27975 /* The introduction-list shall not be empty. */
27976 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27979 /* In cp_parser_introduction_list we have already issued an error. */
27983 if (tmpl_decl
== error_mark_node
)
27985 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27990 /* Build and associate the constraint. */
27991 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27992 if (parms
&& parms
!= error_mark_node
)
27994 cp_parser_template_declaration_after_parameters (parser
, parms
,
27999 error_at (token
->location
, "no matching concept for template-introduction");
28003 /* Parse a normal template-declaration following the template keyword. */
28006 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
28008 tree parameter_list
;
28009 bool need_lang_pop
;
28010 location_t location
= input_location
;
28012 /* Look for the `<' token. */
28013 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
28015 if (at_class_scope_p () && current_function_decl
)
28017 /* 14.5.2.2 [temp.mem]
28019 A local class shall not have member templates. */
28020 error_at (location
,
28021 "invalid declaration of member template in local class");
28022 cp_parser_skip_to_end_of_block_or_statement (parser
);
28027 A template ... shall not have C linkage. */
28028 if (current_lang_name
== lang_name_c
)
28030 error_at (location
, "template with C linkage");
28031 maybe_show_extern_c_location ();
28032 /* Give it C++ linkage to avoid confusing other parts of the
28034 push_lang_context (lang_name_cplusplus
);
28035 need_lang_pop
= true;
28038 need_lang_pop
= false;
28040 /* We cannot perform access checks on the template parameter
28041 declarations until we know what is being declared, just as we
28042 cannot check the decl-specifier list. */
28043 push_deferring_access_checks (dk_deferred
);
28045 /* If the next token is `>', then we have an invalid
28046 specialization. Rather than complain about an invalid template
28047 parameter, issue an error message here. */
28048 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
28050 cp_parser_error (parser
, "invalid explicit specialization");
28051 begin_specialization ();
28052 parameter_list
= NULL_TREE
;
28056 /* Parse the template parameters. */
28057 parameter_list
= cp_parser_template_parameter_list (parser
);
28060 /* Look for the `>'. */
28061 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28063 /* Manage template requirements */
28066 tree reqs
= get_shorthand_constraints (current_template_parms
);
28067 if (tree r
= cp_parser_requires_clause_opt (parser
))
28068 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
28069 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
28072 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
28075 /* For the erroneous case of a template with C linkage, we pushed an
28076 implicit C++ linkage scope; exit that scope now. */
28078 pop_lang_context ();
28081 /* Parse a template-declaration, assuming that the `export' (and
28082 `extern') keywords, if present, has already been scanned. MEMBER_P
28083 is as for cp_parser_template_declaration. */
28086 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
28088 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28090 cp_lexer_consume_token (parser
->lexer
);
28091 cp_parser_explicit_template_declaration (parser
, member_p
);
28094 else if (flag_concepts
)
28095 return cp_parser_template_introduction (parser
, member_p
);
28100 /* Perform the deferred access checks from a template-parameter-list.
28101 CHECKS is a TREE_LIST of access checks, as returned by
28102 get_deferred_access_checks. */
28105 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
28107 ++processing_template_parmlist
;
28108 perform_access_checks (checks
, tf_warning_or_error
);
28109 --processing_template_parmlist
;
28112 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28113 `function-definition' sequence that follows a template header.
28114 If MEMBER_P is true, this declaration appears in a class scope.
28116 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28117 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28120 cp_parser_single_declaration (cp_parser
* parser
,
28121 vec
<deferred_access_check
, va_gc
> *checks
,
28123 bool explicit_specialization_p
,
28126 int declares_class_or_enum
;
28127 tree decl
= NULL_TREE
;
28128 cp_decl_specifier_seq decl_specifiers
;
28129 bool function_definition_p
= false;
28130 cp_token
*decl_spec_token_start
;
28132 /* This function is only used when processing a template
28134 gcc_assert (innermost_scope_kind () == sk_template_parms
28135 || innermost_scope_kind () == sk_template_spec
);
28137 /* Defer access checks until we know what is being declared. */
28138 push_deferring_access_checks (dk_deferred
);
28140 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28142 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
28143 cp_parser_decl_specifier_seq (parser
,
28144 (CP_PARSER_FLAGS_OPTIONAL
28145 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
28147 &declares_class_or_enum
);
28149 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
28151 /* There are no template typedefs. */
28152 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
28154 error_at (decl_spec_token_start
->location
,
28155 "template declaration of %<typedef%>");
28156 decl
= error_mark_node
;
28159 /* Gather up the access checks that occurred the
28160 decl-specifier-seq. */
28161 stop_deferring_access_checks ();
28163 /* Check for the declaration of a template class. */
28164 if (declares_class_or_enum
)
28166 if (cp_parser_declares_only_class_p (parser
)
28167 || (declares_class_or_enum
& 2))
28169 // If this is a declaration, but not a definition, associate
28170 // any constraints with the type declaration. Constraints
28171 // are associated with definitions in cp_parser_class_specifier.
28172 if (declares_class_or_enum
== 1)
28173 associate_classtype_constraints (decl_specifiers
.type
);
28175 decl
= shadow_tag (&decl_specifiers
);
28180 friend template <typename T> struct A<T>::B;
28183 A<T>::B will be represented by a TYPENAME_TYPE, and
28184 therefore not recognized by shadow_tag. */
28185 if (friend_p
&& *friend_p
28187 && decl_specifiers
.type
28188 && TYPE_P (decl_specifiers
.type
))
28189 decl
= decl_specifiers
.type
;
28191 if (decl
&& decl
!= error_mark_node
)
28192 decl
= TYPE_NAME (decl
);
28194 decl
= error_mark_node
;
28196 /* Perform access checks for template parameters. */
28197 cp_parser_perform_template_parameter_access_checks (checks
);
28199 /* Give a helpful diagnostic for
28200 template <class T> struct A { } a;
28201 if we aren't already recovering from an error. */
28202 if (!cp_parser_declares_only_class_p (parser
)
28205 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28206 "a class template declaration must not declare "
28208 cp_parser_skip_to_end_of_block_or_statement (parser
);
28214 /* Complain about missing 'typename' or other invalid type names. */
28215 if (!decl_specifiers
.any_type_specifiers_p
28216 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
28218 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28219 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28220 the rest of this declaration. */
28221 decl
= error_mark_node
;
28225 /* If it's not a template class, try for a template function. If
28226 the next token is a `;', then this declaration does not declare
28227 anything. But, if there were errors in the decl-specifiers, then
28228 the error might well have come from an attempted class-specifier.
28229 In that case, there's no need to warn about a missing declarator. */
28231 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
28232 || decl_specifiers
.type
!= error_mark_node
))
28234 decl
= cp_parser_init_declarator (parser
,
28235 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
28238 /*function_definition_allowed_p=*/true,
28240 declares_class_or_enum
,
28241 &function_definition_p
,
28244 /* 7.1.1-1 [dcl.stc]
28246 A storage-class-specifier shall not be specified in an explicit
28247 specialization... */
28249 && explicit_specialization_p
28250 && decl_specifiers
.storage_class
!= sc_none
)
28252 error_at (decl_spec_token_start
->location
,
28253 "explicit template specialization cannot have a storage class");
28254 decl
= error_mark_node
;
28257 if (decl
&& VAR_P (decl
))
28258 check_template_variable (decl
);
28261 /* Look for a trailing `;' after the declaration. */
28262 if (!function_definition_p
28263 && (decl
== error_mark_node
28264 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
28265 cp_parser_skip_to_end_of_block_or_statement (parser
);
28268 pop_deferring_access_checks ();
28270 /* Clear any current qualification; whatever comes next is the start
28271 of something new. */
28272 parser
->scope
= NULL_TREE
;
28273 parser
->qualifying_scope
= NULL_TREE
;
28274 parser
->object_scope
= NULL_TREE
;
28279 /* Parse a cast-expression that is not the operand of a unary "&". */
28282 cp_parser_simple_cast_expression (cp_parser
*parser
)
28284 return cp_parser_cast_expression (parser
, /*address_p=*/false,
28285 /*cast_p=*/false, /*decltype*/false, NULL
);
28288 /* Parse a functional cast to TYPE. Returns an expression
28289 representing the cast. */
28292 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
28294 vec
<tree
, va_gc
> *vec
;
28295 tree expression_list
;
28299 location_t start_loc
= input_location
;
28302 type
= error_mark_node
;
28304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28306 cp_lexer_set_source_position (parser
->lexer
);
28307 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28308 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
28309 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
28310 if (TREE_CODE (type
) == TYPE_DECL
)
28311 type
= TREE_TYPE (type
);
28313 cast
= finish_compound_literal (type
, expression_list
,
28314 tf_warning_or_error
, fcl_functional
);
28315 /* Create a location of the form:
28318 with caret == start at the start of the type name,
28319 finishing at the closing brace. */
28320 location_t finish_loc
28321 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28322 location_t combined_loc
= make_location (start_loc
, start_loc
,
28324 cast
.set_location (combined_loc
);
28329 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28331 /*allow_expansion_p=*/true,
28332 /*non_constant_p=*/NULL
);
28334 expression_list
= error_mark_node
;
28337 expression_list
= build_tree_list_vec (vec
);
28338 release_tree_vector (vec
);
28341 cast
= build_functional_cast (type
, expression_list
,
28342 tf_warning_or_error
);
28343 /* [expr.const]/1: In an integral constant expression "only type
28344 conversions to integral or enumeration type can be used". */
28345 if (TREE_CODE (type
) == TYPE_DECL
)
28346 type
= TREE_TYPE (type
);
28347 if (cast
!= error_mark_node
28348 && !cast_valid_in_integral_constant_expression_p (type
)
28349 && cp_parser_non_integral_constant_expression (parser
,
28351 return error_mark_node
;
28353 /* Create a location of the form:
28356 with caret == start at the start of the type name,
28357 finishing at the closing paren. */
28358 location_t finish_loc
28359 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28360 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
28361 cast
.set_location (combined_loc
);
28365 /* Save the tokens that make up the body of a member function defined
28366 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28367 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28368 specifiers applied to the declaration. Returns the FUNCTION_DECL
28369 for the member function. */
28372 cp_parser_save_member_function_body (cp_parser
* parser
,
28373 cp_decl_specifier_seq
*decl_specifiers
,
28374 cp_declarator
*declarator
,
28380 bool function_try_block
= false;
28382 /* Create the FUNCTION_DECL. */
28383 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
28384 cp_finalize_omp_declare_simd (parser
, fn
);
28385 cp_finalize_oacc_routine (parser
, fn
, true);
28386 /* If something went badly wrong, bail out now. */
28387 if (fn
== error_mark_node
)
28389 /* If there's a function-body, skip it. */
28390 if (cp_parser_token_starts_function_definition_p
28391 (cp_lexer_peek_token (parser
->lexer
)))
28392 cp_parser_skip_to_end_of_block_or_statement (parser
);
28393 return error_mark_node
;
28396 /* Remember it, if there default args to post process. */
28397 cp_parser_save_default_args (parser
, fn
);
28399 /* Save away the tokens that make up the body of the
28401 first
= parser
->lexer
->next_token
;
28403 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
28404 cp_lexer_consume_token (parser
->lexer
);
28405 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
28406 RID_TRANSACTION_ATOMIC
))
28408 cp_lexer_consume_token (parser
->lexer
);
28409 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28410 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
28411 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
28412 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
28413 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
28414 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
28415 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
28417 cp_lexer_consume_token (parser
->lexer
);
28418 cp_lexer_consume_token (parser
->lexer
);
28419 cp_lexer_consume_token (parser
->lexer
);
28420 cp_lexer_consume_token (parser
->lexer
);
28421 cp_lexer_consume_token (parser
->lexer
);
28424 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28425 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
28427 cp_lexer_consume_token (parser
->lexer
);
28428 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28433 /* Handle function try blocks. */
28434 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28436 cp_lexer_consume_token (parser
->lexer
);
28437 function_try_block
= true;
28439 /* We can have braced-init-list mem-initializers before the fn body. */
28440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28442 cp_lexer_consume_token (parser
->lexer
);
28443 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28445 /* cache_group will stop after an un-nested { } pair, too. */
28446 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28449 /* variadic mem-inits have ... after the ')'. */
28450 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28451 cp_lexer_consume_token (parser
->lexer
);
28454 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28455 /* Handle function try blocks. */
28456 if (function_try_block
)
28457 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28458 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28459 last
= parser
->lexer
->next_token
;
28461 /* Save away the inline definition; we will process it when the
28462 class is complete. */
28463 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28464 DECL_PENDING_INLINE_P (fn
) = 1;
28466 /* We need to know that this was defined in the class, so that
28467 friend templates are handled correctly. */
28468 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28470 /* Add FN to the queue of functions to be parsed later. */
28471 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28476 /* Save the tokens that make up the in-class initializer for a non-static
28477 data member. Returns a DEFAULT_ARG. */
28480 cp_parser_save_nsdmi (cp_parser
* parser
)
28482 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28485 /* Parse a template-argument-list, as well as the trailing ">" (but
28486 not the opening "<"). See cp_parser_template_argument_list for the
28490 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28494 tree saved_qualifying_scope
;
28495 tree saved_object_scope
;
28496 bool saved_greater_than_is_operator_p
;
28500 When parsing a template-id, the first non-nested `>' is taken as
28501 the end of the template-argument-list rather than a greater-than
28503 saved_greater_than_is_operator_p
28504 = parser
->greater_than_is_operator_p
;
28505 parser
->greater_than_is_operator_p
= false;
28506 /* Parsing the argument list may modify SCOPE, so we save it
28508 saved_scope
= parser
->scope
;
28509 saved_qualifying_scope
= parser
->qualifying_scope
;
28510 saved_object_scope
= parser
->object_scope
;
28511 /* We need to evaluate the template arguments, even though this
28512 template-id may be nested within a "sizeof". */
28514 /* Parse the template-argument-list itself. */
28515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28516 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28517 arguments
= NULL_TREE
;
28519 arguments
= cp_parser_template_argument_list (parser
);
28520 /* Look for the `>' that ends the template-argument-list. If we find
28521 a '>>' instead, it's probably just a typo. */
28522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28524 if (cxx_dialect
!= cxx98
)
28526 /* In C++0x, a `>>' in a template argument list or cast
28527 expression is considered to be two separate `>'
28528 tokens. So, change the current token to a `>', but don't
28529 consume it: it will be consumed later when the outer
28530 template argument list (or cast expression) is parsed.
28531 Note that this replacement of `>' for `>>' is necessary
28532 even if we are parsing tentatively: in the tentative
28533 case, after calling
28534 cp_parser_enclosed_template_argument_list we will always
28535 throw away all of the template arguments and the first
28536 closing `>', either because the template argument list
28537 was erroneous or because we are replacing those tokens
28538 with a CPP_TEMPLATE_ID token. The second `>' (which will
28539 not have been thrown away) is needed either to close an
28540 outer template argument list or to complete a new-style
28542 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28543 token
->type
= CPP_GREATER
;
28545 else if (!saved_greater_than_is_operator_p
)
28547 /* If we're in a nested template argument list, the '>>' has
28548 to be a typo for '> >'. We emit the error message, but we
28549 continue parsing and we push a '>' as next token, so that
28550 the argument list will be parsed correctly. Note that the
28551 global source location is still on the token before the
28552 '>>', so we need to say explicitly where we want it. */
28553 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28554 gcc_rich_location
richloc (token
->location
);
28555 richloc
.add_fixit_replace ("> >");
28556 error_at (&richloc
, "%<>>%> should be %<> >%> "
28557 "within a nested template argument list");
28559 token
->type
= CPP_GREATER
;
28563 /* If this is not a nested template argument list, the '>>'
28564 is a typo for '>'. Emit an error message and continue.
28565 Same deal about the token location, but here we can get it
28566 right by consuming the '>>' before issuing the diagnostic. */
28567 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28568 error_at (token
->location
,
28569 "spurious %<>>%>, use %<>%> to terminate "
28570 "a template argument list");
28574 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28575 /* The `>' token might be a greater-than operator again now. */
28576 parser
->greater_than_is_operator_p
28577 = saved_greater_than_is_operator_p
;
28578 /* Restore the SAVED_SCOPE. */
28579 parser
->scope
= saved_scope
;
28580 parser
->qualifying_scope
= saved_qualifying_scope
;
28581 parser
->object_scope
= saved_object_scope
;
28586 /* MEMBER_FUNCTION is a member function, or a friend. If default
28587 arguments, or the body of the function have not yet been parsed,
28591 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28593 timevar_push (TV_PARSE_INMETH
);
28594 /* If this member is a template, get the underlying
28596 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28597 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28599 /* There should not be any class definitions in progress at this
28600 point; the bodies of members are only parsed outside of all class
28602 gcc_assert (parser
->num_classes_being_defined
== 0);
28603 /* While we're parsing the member functions we might encounter more
28604 classes. We want to handle them right away, but we don't want
28605 them getting mixed up with functions that are currently in the
28607 push_unparsed_function_queues (parser
);
28609 /* Make sure that any template parameters are in scope. */
28610 maybe_begin_member_template_processing (member_function
);
28612 /* If the body of the function has not yet been parsed, parse it
28614 if (DECL_PENDING_INLINE_P (member_function
))
28616 tree function_scope
;
28617 cp_token_cache
*tokens
;
28619 /* The function is no longer pending; we are processing it. */
28620 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28621 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28622 DECL_PENDING_INLINE_P (member_function
) = 0;
28624 /* If this is a local class, enter the scope of the containing
28626 function_scope
= current_function_decl
;
28627 if (function_scope
)
28628 push_function_context ();
28630 /* Push the body of the function onto the lexer stack. */
28631 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28633 /* Let the front end know that we going to be defining this
28635 start_preparsed_function (member_function
, NULL_TREE
,
28636 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28638 /* Don't do access checking if it is a templated function. */
28639 if (processing_template_decl
)
28640 push_deferring_access_checks (dk_no_check
);
28642 /* #pragma omp declare reduction needs special parsing. */
28643 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28645 parser
->lexer
->in_pragma
= true;
28646 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28647 finish_function (/*inline_p=*/true);
28648 cp_check_omp_declare_reduction (member_function
);
28651 /* Now, parse the body of the function. */
28652 cp_parser_function_definition_after_declarator (parser
,
28653 /*inline_p=*/true);
28655 if (processing_template_decl
)
28656 pop_deferring_access_checks ();
28658 /* Leave the scope of the containing function. */
28659 if (function_scope
)
28660 pop_function_context ();
28661 cp_parser_pop_lexer (parser
);
28664 /* Remove any template parameters from the symbol table. */
28665 maybe_end_member_template_processing ();
28667 /* Restore the queue. */
28668 pop_unparsed_function_queues (parser
);
28669 timevar_pop (TV_PARSE_INMETH
);
28672 /* If DECL contains any default args, remember it on the unparsed
28673 functions queue. */
28676 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28680 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28682 probe
= TREE_CHAIN (probe
))
28683 if (TREE_PURPOSE (probe
))
28685 cp_default_arg_entry entry
= {current_class_type
, decl
};
28686 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28691 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28692 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28693 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28694 from the parameter-type-list. */
28697 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28698 tree default_arg
, tree parmtype
)
28700 cp_token_cache
*tokens
;
28704 if (default_arg
== error_mark_node
)
28705 return error_mark_node
;
28707 /* Push the saved tokens for the default argument onto the parser's
28709 tokens
= DEFARG_TOKENS (default_arg
);
28710 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28712 start_lambda_scope (decl
);
28714 /* Parse the default argument. */
28715 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28716 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28717 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28719 finish_lambda_scope ();
28721 if (parsed_arg
== error_mark_node
)
28722 cp_parser_skip_to_end_of_statement (parser
);
28724 if (!processing_template_decl
)
28726 /* In a non-template class, check conversions now. In a template,
28727 we'll wait and instantiate these as needed. */
28728 if (TREE_CODE (decl
) == PARM_DECL
)
28729 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28730 tf_warning_or_error
);
28731 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28732 parsed_arg
= error_mark_node
;
28734 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28737 /* If the token stream has not been completely used up, then
28738 there was extra junk after the end of the default
28740 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28742 if (TREE_CODE (decl
) == PARM_DECL
)
28743 cp_parser_error (parser
, "expected %<,%>");
28745 cp_parser_error (parser
, "expected %<;%>");
28748 /* Revert to the main lexer. */
28749 cp_parser_pop_lexer (parser
);
28754 /* FIELD is a non-static data member with an initializer which we saved for
28755 later; parse it now. */
28758 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28762 maybe_begin_member_template_processing (field
);
28764 push_unparsed_function_queues (parser
);
28765 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28766 DECL_INITIAL (field
),
28768 pop_unparsed_function_queues (parser
);
28770 maybe_end_member_template_processing ();
28772 DECL_INITIAL (field
) = def
;
28775 /* FN is a FUNCTION_DECL which may contains a parameter with an
28776 unparsed DEFAULT_ARG. Parse the default args now. This function
28777 assumes that the current scope is the scope in which the default
28778 argument should be processed. */
28781 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28783 unsigned char saved_local_variables_forbidden_p
;
28784 tree parm
, parmdecl
;
28786 /* While we're parsing the default args, we might (due to the
28787 statement expression extension) encounter more classes. We want
28788 to handle them right away, but we don't want them getting mixed
28789 up with default args that are currently in the queue. */
28790 push_unparsed_function_queues (parser
);
28792 /* Local variable names (and the `this' keyword) may not appear
28793 in a default argument. */
28794 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28795 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
28797 push_defarg_context (fn
);
28799 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28800 parmdecl
= DECL_ARGUMENTS (fn
);
28801 parm
&& parm
!= void_list_node
;
28802 parm
= TREE_CHAIN (parm
),
28803 parmdecl
= DECL_CHAIN (parmdecl
))
28805 tree default_arg
= TREE_PURPOSE (parm
);
28807 vec
<tree
, va_gc
> *insts
;
28814 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28815 /* This can happen for a friend declaration for a function
28816 already declared with default arguments. */
28820 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28822 TREE_VALUE (parm
));
28823 TREE_PURPOSE (parm
) = parsed_arg
;
28825 /* Update any instantiations we've already created. */
28826 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28827 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28828 TREE_PURPOSE (copy
) = parsed_arg
;
28831 pop_defarg_context ();
28833 /* Make sure no default arg is missing. */
28834 check_default_args (fn
);
28836 /* Restore the state of local_variables_forbidden_p. */
28837 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28839 /* Restore the queue. */
28840 pop_unparsed_function_queues (parser
);
28843 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28845 sizeof ... ( identifier )
28847 where the 'sizeof' token has already been consumed. */
28850 cp_parser_sizeof_pack (cp_parser
*parser
)
28852 /* Consume the `...'. */
28853 cp_lexer_consume_token (parser
->lexer
);
28854 maybe_warn_variadic_templates ();
28856 matching_parens parens
;
28857 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28859 parens
.consume_open (parser
);
28861 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28862 "%<sizeof...%> argument must be surrounded by parentheses");
28864 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28865 tree name
= cp_parser_identifier (parser
);
28866 if (name
== error_mark_node
)
28867 return error_mark_node
;
28868 /* The name is not qualified. */
28869 parser
->scope
= NULL_TREE
;
28870 parser
->qualifying_scope
= NULL_TREE
;
28871 parser
->object_scope
= NULL_TREE
;
28872 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28873 if (expr
== error_mark_node
)
28874 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28876 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28877 expr
= TREE_TYPE (expr
);
28878 else if (TREE_CODE (expr
) == CONST_DECL
)
28879 expr
= DECL_INITIAL (expr
);
28880 expr
= make_pack_expansion (expr
);
28881 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28884 parens
.require_close (parser
);
28889 /* Parse the operand of `sizeof' (or a similar operator). Returns
28890 either a TYPE or an expression, depending on the form of the
28891 input. The KEYWORD indicates which kind of expression we have
28895 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28897 tree expr
= NULL_TREE
;
28898 const char *saved_message
;
28899 const char *saved_message_arg
;
28900 bool saved_integral_constant_expression_p
;
28901 bool saved_non_integral_constant_expression_p
;
28903 /* If it's a `...', then we are computing the length of a parameter
28905 if (keyword
== RID_SIZEOF
28906 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28907 return cp_parser_sizeof_pack (parser
);
28909 /* Types cannot be defined in a `sizeof' expression. Save away the
28911 saved_message
= parser
->type_definition_forbidden_message
;
28912 saved_message_arg
= parser
->type_definition_forbidden_message_arg
;
28913 parser
->type_definition_forbidden_message
28914 = G_("types may not be defined in %qs expressions");
28915 parser
->type_definition_forbidden_message_arg
28916 = IDENTIFIER_POINTER (ridpointers
[keyword
]);
28918 /* The restrictions on constant-expressions do not apply inside
28919 sizeof expressions. */
28920 saved_integral_constant_expression_p
28921 = parser
->integral_constant_expression_p
;
28922 saved_non_integral_constant_expression_p
28923 = parser
->non_integral_constant_expression_p
;
28924 parser
->integral_constant_expression_p
= false;
28926 /* Do not actually evaluate the expression. */
28927 ++cp_unevaluated_operand
;
28928 ++c_inhibit_evaluation_warnings
;
28929 /* If it's a `(', then we might be looking at the type-id
28931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28933 tree type
= NULL_TREE
;
28935 /* We can't be sure yet whether we're looking at a type-id or an
28937 cp_parser_parse_tentatively (parser
);
28939 matching_parens parens
;
28940 parens
.consume_open (parser
);
28942 /* Note: as a GNU Extension, compound literals are considered
28943 postfix-expressions as they are in C99, so they are valid
28944 arguments to sizeof. See comment in cp_parser_cast_expression
28946 if (cp_parser_compound_literal_p (parser
))
28947 cp_parser_simulate_error (parser
);
28950 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28951 parser
->in_type_id_in_expr_p
= true;
28952 /* Look for the type-id. */
28953 type
= cp_parser_type_id (parser
);
28954 /* Look for the closing `)'. */
28955 parens
.require_close (parser
);
28956 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28959 /* If all went well, then we're done. */
28960 if (cp_parser_parse_definitely (parser
))
28964 /* If the type-id production did not work out, then we must be
28965 looking at the unary-expression production. */
28967 expr
= cp_parser_unary_expression (parser
);
28969 /* Go back to evaluating expressions. */
28970 --cp_unevaluated_operand
;
28971 --c_inhibit_evaluation_warnings
;
28973 /* And restore the old one. */
28974 parser
->type_definition_forbidden_message
= saved_message
;
28975 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
28976 parser
->integral_constant_expression_p
28977 = saved_integral_constant_expression_p
;
28978 parser
->non_integral_constant_expression_p
28979 = saved_non_integral_constant_expression_p
;
28984 /* If the current declaration has no declarator, return true. */
28987 cp_parser_declares_only_class_p (cp_parser
*parser
)
28989 /* If the next token is a `;' or a `,' then there is no
28991 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28992 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28995 /* Update the DECL_SPECS to reflect the storage class indicated by
28999 cp_parser_set_storage_class (cp_parser
*parser
,
29000 cp_decl_specifier_seq
*decl_specs
,
29004 cp_storage_class storage_class
;
29006 if (parser
->in_unbraced_linkage_specification_p
)
29008 error_at (token
->location
, "invalid use of %qD in linkage specification",
29009 ridpointers
[keyword
]);
29012 else if (decl_specs
->storage_class
!= sc_none
)
29014 decl_specs
->conflicting_specifiers_p
= true;
29018 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
29019 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
29020 && decl_specs
->gnu_thread_keyword_p
)
29022 pedwarn (decl_specs
->locations
[ds_thread
], 0,
29023 "%<__thread%> before %qD", ridpointers
[keyword
]);
29029 storage_class
= sc_auto
;
29032 storage_class
= sc_register
;
29035 storage_class
= sc_static
;
29038 storage_class
= sc_extern
;
29041 storage_class
= sc_mutable
;
29044 gcc_unreachable ();
29046 decl_specs
->storage_class
= storage_class
;
29047 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
29049 /* A storage class specifier cannot be applied alongside a typedef
29050 specifier. If there is a typedef specifier present then set
29051 conflicting_specifiers_p which will trigger an error later
29052 on in grokdeclarator. */
29053 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
29054 decl_specs
->conflicting_specifiers_p
= true;
29057 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29058 is true, the type is a class or enum definition. */
29061 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
29064 bool type_definition_p
)
29066 decl_specs
->any_specifiers_p
= true;
29068 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29069 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29070 this is what happened. In system headers, we ignore these
29071 declarations so that G++ can work with system headers that are not
29073 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
29074 && !type_definition_p
29075 && (type_spec
== boolean_type_node
29076 || type_spec
== char8_type_node
29077 || type_spec
== char16_type_node
29078 || type_spec
== char32_type_node
29079 || type_spec
== wchar_type_node
)
29080 && (decl_specs
->type
29081 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
29082 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
29083 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
29084 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
29086 decl_specs
->redefined_builtin_type
= type_spec
;
29087 set_and_check_decl_spec_loc (decl_specs
,
29088 ds_redefined_builtin_type_spec
,
29090 if (!decl_specs
->type
)
29092 decl_specs
->type
= type_spec
;
29093 decl_specs
->type_definition_p
= false;
29094 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
29097 else if (decl_specs
->type
)
29098 decl_specs
->multiple_types_p
= true;
29101 decl_specs
->type
= type_spec
;
29102 decl_specs
->type_definition_p
= type_definition_p
;
29103 decl_specs
->redefined_builtin_type
= NULL_TREE
;
29104 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
29108 /* True iff TOKEN is the GNU keyword __thread. */
29111 token_is__thread (cp_token
*token
)
29113 gcc_assert (token
->keyword
== RID_THREAD
);
29114 return id_equal (token
->u
.value
, "__thread");
29117 /* Set the location for a declarator specifier and check if it is
29120 DECL_SPECS is the sequence of declarator specifiers onto which to
29123 DS is the single declarator specifier to set which location is to
29124 be set onto the existing sequence of declarators.
29126 LOCATION is the location for the declarator specifier to
29130 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
29131 cp_decl_spec ds
, cp_token
*token
)
29133 gcc_assert (ds
< ds_last
);
29135 if (decl_specs
== NULL
)
29138 location_t location
= token
->location
;
29140 if (decl_specs
->locations
[ds
] == 0)
29142 decl_specs
->locations
[ds
] = location
;
29143 if (ds
== ds_thread
)
29144 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
29150 if (decl_specs
->locations
[ds_long_long
] != 0)
29151 error_at (location
,
29152 "%<long long long%> is too long for GCC");
29155 decl_specs
->locations
[ds_long_long
] = location
;
29156 pedwarn_cxx98 (location
,
29158 "ISO C++ 1998 does not support %<long long%>");
29161 else if (ds
== ds_thread
)
29163 bool gnu
= token_is__thread (token
);
29164 gcc_rich_location
richloc (location
);
29165 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
29167 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
29168 error_at (&richloc
,
29169 "both %<__thread%> and %<thread_local%> specified");
29173 richloc
.add_fixit_remove ();
29174 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
29179 static const char *const decl_spec_names
[] = {
29196 gcc_rich_location
richloc (location
);
29197 richloc
.add_fixit_remove ();
29198 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
29203 /* Return true iff the declarator specifier DS is present in the
29204 sequence of declarator specifiers DECL_SPECS. */
29207 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
29210 gcc_assert (ds
< ds_last
);
29212 if (decl_specs
== NULL
)
29215 return decl_specs
->locations
[ds
] != 0;
29218 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29219 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29222 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
29224 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
29227 /* Issue an error message indicating that TOKEN_DESC was expected.
29228 If KEYWORD is true, it indicated this function is called by
29229 cp_parser_require_keword and the required token can only be
29230 a indicated keyword.
29232 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29233 within any error as the location of an "opening" token matching
29234 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29235 RT_CLOSE_PAREN). */
29238 cp_parser_required_error (cp_parser
*parser
,
29239 required_token token_desc
,
29241 location_t matching_location
)
29243 if (cp_parser_simulate_error (parser
))
29246 const char *gmsgid
= NULL
;
29247 switch (token_desc
)
29250 gmsgid
= G_("expected %<new%>");
29253 gmsgid
= G_("expected %<delete%>");
29256 gmsgid
= G_("expected %<return%>");
29259 gmsgid
= G_("expected %<while%>");
29262 gmsgid
= G_("expected %<extern%>");
29264 case RT_STATIC_ASSERT
:
29265 gmsgid
= G_("expected %<static_assert%>");
29268 gmsgid
= G_("expected %<decltype%>");
29271 gmsgid
= G_("expected %<operator%>");
29274 gmsgid
= G_("expected %<class%>");
29277 gmsgid
= G_("expected %<template%>");
29280 gmsgid
= G_("expected %<namespace%>");
29283 gmsgid
= G_("expected %<using%>");
29286 gmsgid
= G_("expected %<asm%>");
29289 gmsgid
= G_("expected %<try%>");
29292 gmsgid
= G_("expected %<catch%>");
29295 gmsgid
= G_("expected %<throw%>");
29298 gmsgid
= G_("expected %<__label__%>");
29301 gmsgid
= G_("expected %<@try%>");
29303 case RT_AT_SYNCHRONIZED
:
29304 gmsgid
= G_("expected %<@synchronized%>");
29307 gmsgid
= G_("expected %<@throw%>");
29309 case RT_TRANSACTION_ATOMIC
:
29310 gmsgid
= G_("expected %<__transaction_atomic%>");
29312 case RT_TRANSACTION_RELAXED
:
29313 gmsgid
= G_("expected %<__transaction_relaxed%>");
29319 if (!gmsgid
&& !keyword
)
29321 switch (token_desc
)
29324 gmsgid
= G_("expected %<;%>");
29326 case RT_OPEN_PAREN
:
29327 gmsgid
= G_("expected %<(%>");
29329 case RT_CLOSE_BRACE
:
29330 gmsgid
= G_("expected %<}%>");
29332 case RT_OPEN_BRACE
:
29333 gmsgid
= G_("expected %<{%>");
29335 case RT_CLOSE_SQUARE
:
29336 gmsgid
= G_("expected %<]%>");
29338 case RT_OPEN_SQUARE
:
29339 gmsgid
= G_("expected %<[%>");
29342 gmsgid
= G_("expected %<,%>");
29345 gmsgid
= G_("expected %<::%>");
29348 gmsgid
= G_("expected %<<%>");
29351 gmsgid
= G_("expected %<>%>");
29354 gmsgid
= G_("expected %<=%>");
29357 gmsgid
= G_("expected %<...%>");
29360 gmsgid
= G_("expected %<*%>");
29363 gmsgid
= G_("expected %<~%>");
29366 gmsgid
= G_("expected %<:%>");
29368 case RT_COLON_SCOPE
:
29369 gmsgid
= G_("expected %<:%> or %<::%>");
29371 case RT_CLOSE_PAREN
:
29372 gmsgid
= G_("expected %<)%>");
29374 case RT_COMMA_CLOSE_PAREN
:
29375 gmsgid
= G_("expected %<,%> or %<)%>");
29377 case RT_PRAGMA_EOL
:
29378 gmsgid
= G_("expected end of line");
29381 gmsgid
= G_("expected identifier");
29384 gmsgid
= G_("expected selection-statement");
29387 gmsgid
= G_("expected iteration-statement");
29390 gmsgid
= G_("expected jump-statement");
29393 gmsgid
= G_("expected class-key");
29395 case RT_CLASS_TYPENAME_TEMPLATE
:
29396 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
29399 gcc_unreachable ();
29404 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
29408 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29409 issue an error message indicating that TOKEN_DESC was expected.
29411 Returns the token consumed, if the token had the appropriate type.
29412 Otherwise, returns NULL.
29414 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29415 within any error as the location of an "opening" token matching
29416 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29417 RT_CLOSE_PAREN). */
29420 cp_parser_require (cp_parser
* parser
,
29421 enum cpp_ttype type
,
29422 required_token token_desc
,
29423 location_t matching_location
)
29425 if (cp_lexer_next_token_is (parser
->lexer
, type
))
29426 return cp_lexer_consume_token (parser
->lexer
);
29429 /* Output the MESSAGE -- unless we're parsing tentatively. */
29430 if (!cp_parser_simulate_error (parser
))
29431 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
29432 matching_location
);
29437 /* An error message is produced if the next token is not '>'.
29438 All further tokens are skipped until the desired token is
29439 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29442 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29444 /* Current level of '< ... >'. */
29445 unsigned level
= 0;
29446 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29447 unsigned nesting_depth
= 0;
29449 /* Are we ready, yet? If not, issue error message. */
29450 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29453 /* Skip tokens until the desired token is found. */
29456 /* Peek at the next token. */
29457 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29460 if (!nesting_depth
)
29465 if (cxx_dialect
== cxx98
)
29466 /* C++0x views the `>>' operator as two `>' tokens, but
29469 else if (!nesting_depth
&& level
-- == 0)
29471 /* We've hit a `>>' where the first `>' closes the
29472 template argument list, and the second `>' is
29473 spurious. Just consume the `>>' and stop; we've
29474 already produced at least one error. */
29475 cp_lexer_consume_token (parser
->lexer
);
29478 /* Fall through for C++0x, so we handle the second `>' in
29480 gcc_fallthrough ();
29483 if (!nesting_depth
&& level
-- == 0)
29485 /* We've reached the token we want, consume it and stop. */
29486 cp_lexer_consume_token (parser
->lexer
);
29491 case CPP_OPEN_PAREN
:
29492 case CPP_OPEN_SQUARE
:
29496 case CPP_CLOSE_PAREN
:
29497 case CPP_CLOSE_SQUARE
:
29498 if (nesting_depth
-- == 0)
29503 case CPP_PRAGMA_EOL
:
29504 case CPP_SEMICOLON
:
29505 case CPP_OPEN_BRACE
:
29506 case CPP_CLOSE_BRACE
:
29507 /* The '>' was probably forgotten, don't look further. */
29514 /* Consume this token. */
29515 cp_lexer_consume_token (parser
->lexer
);
29519 /* If the next token is the indicated keyword, consume it. Otherwise,
29520 issue an error message indicating that TOKEN_DESC was expected.
29522 Returns the token consumed, if the token had the appropriate type.
29523 Otherwise, returns NULL. */
29526 cp_parser_require_keyword (cp_parser
* parser
,
29528 required_token token_desc
)
29530 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29532 if (token
&& token
->keyword
!= keyword
)
29534 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29542 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29543 function-definition. */
29546 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29548 return (/* An ordinary function-body begins with an `{'. */
29549 token
->type
== CPP_OPEN_BRACE
29550 /* A ctor-initializer begins with a `:'. */
29551 || token
->type
== CPP_COLON
29552 /* A function-try-block begins with `try'. */
29553 || token
->keyword
== RID_TRY
29554 /* A function-transaction-block begins with `__transaction_atomic'
29555 or `__transaction_relaxed'. */
29556 || token
->keyword
== RID_TRANSACTION_ATOMIC
29557 || token
->keyword
== RID_TRANSACTION_RELAXED
29558 /* The named return value extension begins with `return'. */
29559 || token
->keyword
== RID_RETURN
);
29562 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29566 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29570 token
= cp_lexer_peek_token (parser
->lexer
);
29571 return (token
->type
== CPP_OPEN_BRACE
29572 || (token
->type
== CPP_COLON
29573 && !parser
->colon_doesnt_start_class_def_p
));
29576 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29577 C++0x) ending a template-argument. */
29580 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29584 token
= cp_lexer_peek_token (parser
->lexer
);
29585 return (token
->type
== CPP_COMMA
29586 || token
->type
== CPP_GREATER
29587 || token
->type
== CPP_ELLIPSIS
29588 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29591 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29592 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29595 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29600 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29601 if (token
->type
== CPP_LESS
)
29603 /* Check for the sequence `<::' in the original code. It would be lexed as
29604 `[:', where `[' is a digraph, and there is no whitespace before
29606 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29609 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29610 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29616 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29617 or none_type otherwise. */
29619 static enum tag_types
29620 cp_parser_token_is_class_key (cp_token
* token
)
29622 switch (token
->keyword
)
29627 return record_type
;
29636 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29637 or none_type otherwise or if the token is null. */
29639 static enum tag_types
29640 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29645 switch (token
->keyword
)
29650 return typename_type
;
29657 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29660 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29662 if (type
== error_mark_node
)
29664 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29666 if (permerror (input_location
, "%qs tag used in naming %q#T",
29667 class_key
== union_type
? "union"
29668 : class_key
== record_type
? "struct" : "class",
29670 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29671 "%q#T was previously declared here", type
);
29675 /* Issue an error message if DECL is redeclared with different
29676 access than its original declaration [class.access.spec/3].
29677 This applies to nested classes, nested class templates and
29678 enumerations [class.mem/1]. */
29681 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29684 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29685 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29688 if ((TREE_PRIVATE (decl
)
29689 != (current_access_specifier
== access_private_node
))
29690 || (TREE_PROTECTED (decl
)
29691 != (current_access_specifier
== access_protected_node
)))
29692 error_at (location
, "%qD redeclared with different access", decl
);
29695 /* Look for the `template' keyword, as a syntactic disambiguator.
29696 Return TRUE iff it is present, in which case it will be
29700 cp_parser_optional_template_keyword (cp_parser
*parser
)
29702 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29704 /* In C++98 the `template' keyword can only be used within templates;
29705 outside templates the parser can always figure out what is a
29706 template and what is not. In C++11, per the resolution of DR 468,
29707 `template' is allowed in cases where it is not strictly necessary. */
29708 if (!processing_template_decl
29709 && pedantic
&& cxx_dialect
== cxx98
)
29711 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29712 pedwarn (token
->location
, OPT_Wpedantic
,
29713 "in C++98 %<template%> (as a disambiguator) is only "
29714 "allowed within templates");
29715 /* If this part of the token stream is rescanned, the same
29716 error message would be generated. So, we purge the token
29717 from the stream. */
29718 cp_lexer_purge_token (parser
->lexer
);
29723 /* Consume the `template' keyword. */
29724 cp_lexer_consume_token (parser
->lexer
);
29731 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29732 set PARSER->SCOPE, and perform other related actions. */
29735 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29737 struct tree_check
*check_value
;
29739 /* Get the stored value. */
29740 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29741 /* Set the scope from the stored value. */
29742 parser
->scope
= saved_checks_value (check_value
);
29743 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29744 parser
->object_scope
= NULL_TREE
;
29747 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29748 encounter the end of a block before what we were looking for. */
29751 cp_parser_cache_group (cp_parser
*parser
,
29752 enum cpp_ttype end
,
29757 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29759 /* Abort a parenthesized expression if we encounter a semicolon. */
29760 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29761 && token
->type
== CPP_SEMICOLON
)
29763 /* If we've reached the end of the file, stop. */
29764 if (token
->type
== CPP_EOF
29765 || (end
!= CPP_PRAGMA_EOL
29766 && token
->type
== CPP_PRAGMA_EOL
))
29768 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29769 /* We've hit the end of an enclosing block, so there's been some
29770 kind of syntax error. */
29773 /* Consume the token. */
29774 cp_lexer_consume_token (parser
->lexer
);
29775 /* See if it starts a new group. */
29776 if (token
->type
== CPP_OPEN_BRACE
)
29778 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29779 /* In theory this should probably check end == '}', but
29780 cp_parser_save_member_function_body needs it to exit
29781 after either '}' or ')' when called with ')'. */
29785 else if (token
->type
== CPP_OPEN_PAREN
)
29787 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29788 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29791 else if (token
->type
== CPP_PRAGMA
)
29792 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29793 else if (token
->type
== end
)
29798 /* Like above, for caching a default argument or NSDMI. Both of these are
29799 terminated by a non-nested comma, but it can be unclear whether or not a
29800 comma is nested in a template argument list unless we do more parsing.
29801 In order to handle this ambiguity, when we encounter a ',' after a '<'
29802 we try to parse what follows as a parameter-declaration-list (in the
29803 case of a default argument) or a member-declarator (in the case of an
29804 NSDMI). If that succeeds, then we stop caching. */
29807 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29809 unsigned depth
= 0;
29810 int maybe_template_id
= 0;
29811 cp_token
*first_token
;
29813 tree default_argument
;
29815 /* Add tokens until we have processed the entire default
29816 argument. We add the range [first_token, token). */
29817 first_token
= cp_lexer_peek_token (parser
->lexer
);
29818 if (first_token
->type
== CPP_OPEN_BRACE
)
29820 /* For list-initialization, this is straightforward. */
29821 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29822 token
= cp_lexer_peek_token (parser
->lexer
);
29828 /* Peek at the next token. */
29829 token
= cp_lexer_peek_token (parser
->lexer
);
29830 /* What we do depends on what token we have. */
29831 switch (token
->type
)
29833 /* In valid code, a default argument must be
29834 immediately followed by a `,' `)', or `...'. */
29836 if (depth
== 0 && maybe_template_id
)
29838 /* If we've seen a '<', we might be in a
29839 template-argument-list. Until Core issue 325 is
29840 resolved, we don't know how this situation ought
29841 to be handled, so try to DTRT. We check whether
29842 what comes after the comma is a valid parameter
29843 declaration list. If it is, then the comma ends
29844 the default argument; otherwise the default
29845 argument continues. */
29846 bool error
= false;
29849 /* Set ITALP so cp_parser_parameter_declaration_list
29850 doesn't decide to commit to this parse. */
29851 bool saved_italp
= parser
->in_template_argument_list_p
;
29852 parser
->in_template_argument_list_p
= true;
29854 cp_parser_parse_tentatively (parser
);
29858 /* Parse declarators until we reach a non-comma or
29859 somthing that cannot be an initializer.
29860 Just checking whether we're looking at a single
29861 declarator is insufficient. Consider:
29862 int var = tuple<T,U>::x;
29863 The template parameter 'U' looks exactly like a
29867 int ctor_dtor_or_conv_p
;
29868 cp_lexer_consume_token (parser
->lexer
);
29869 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29870 CP_PARSER_FLAGS_NONE
,
29871 &ctor_dtor_or_conv_p
,
29872 /*parenthesized_p=*/NULL
,
29874 /*friend_p=*/false,
29875 /*static_p=*/false);
29876 peek
= cp_lexer_peek_token (parser
->lexer
);
29877 if (cp_parser_error_occurred (parser
))
29880 while (peek
->type
== CPP_COMMA
);
29881 /* If we met an '=' or ';' then the original comma
29882 was the end of the NSDMI. Otherwise assume
29883 we're still in the NSDMI. */
29884 error
= (peek
->type
!= CPP_EQ
29885 && peek
->type
!= CPP_SEMICOLON
);
29889 cp_lexer_consume_token (parser
->lexer
);
29890 begin_scope (sk_function_parms
, NULL_TREE
);
29891 tree t
= cp_parser_parameter_declaration_list
29892 (parser
, CP_PARSER_FLAGS_NONE
);
29893 if (t
== error_mark_node
)
29895 pop_bindings_and_leave_scope ();
29897 if (!cp_parser_error_occurred (parser
) && !error
)
29899 cp_parser_abort_tentative_parse (parser
);
29901 parser
->in_template_argument_list_p
= saved_italp
;
29905 case CPP_CLOSE_PAREN
:
29907 /* If we run into a non-nested `;', `}', or `]',
29908 then the code is invalid -- but the default
29909 argument is certainly over. */
29910 case CPP_SEMICOLON
:
29911 case CPP_CLOSE_BRACE
:
29912 case CPP_CLOSE_SQUARE
:
29914 /* Handle correctly int n = sizeof ... ( p ); */
29915 && token
->type
!= CPP_ELLIPSIS
)
29917 /* Update DEPTH, if necessary. */
29918 else if (token
->type
== CPP_CLOSE_PAREN
29919 || token
->type
== CPP_CLOSE_BRACE
29920 || token
->type
== CPP_CLOSE_SQUARE
)
29924 case CPP_OPEN_PAREN
:
29925 case CPP_OPEN_SQUARE
:
29926 case CPP_OPEN_BRACE
:
29932 /* This might be the comparison operator, or it might
29933 start a template argument list. */
29934 ++maybe_template_id
;
29938 if (cxx_dialect
== cxx98
)
29940 /* Fall through for C++0x, which treats the `>>'
29941 operator like two `>' tokens in certain
29943 gcc_fallthrough ();
29948 /* This might be an operator, or it might close a
29949 template argument list. But if a previous '<'
29950 started a template argument list, this will have
29951 closed it, so we can't be in one anymore. */
29952 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29953 if (maybe_template_id
< 0)
29954 maybe_template_id
= 0;
29958 /* If we run out of tokens, issue an error message. */
29960 case CPP_PRAGMA_EOL
:
29961 error_at (token
->location
, "file ends in default argument");
29962 return error_mark_node
;
29966 /* In these cases, we should look for template-ids.
29967 For example, if the default argument is
29968 `X<int, double>()', we need to do name lookup to
29969 figure out whether or not `X' is a template; if
29970 so, the `,' does not end the default argument.
29972 That is not yet done. */
29979 /* If we've reached the end, stop. */
29983 /* Add the token to the token block. */
29984 token
= cp_lexer_consume_token (parser
->lexer
);
29987 /* Create a DEFAULT_ARG to represent the unparsed default
29989 default_argument
= make_node (DEFAULT_ARG
);
29990 DEFARG_TOKENS (default_argument
)
29991 = cp_token_cache_new (first_token
, token
);
29992 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29994 return default_argument
;
29997 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
30000 defarg_location (tree default_argument
)
30002 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
30003 location_t start
= tokens
->first
->location
;
30004 location_t end
= tokens
->last
->location
;
30005 return make_location (start
, start
, end
);
30008 /* Begin parsing tentatively. We always save tokens while parsing
30009 tentatively so that if the tentative parsing fails we can restore the
30013 cp_parser_parse_tentatively (cp_parser
* parser
)
30015 /* Enter a new parsing context. */
30016 parser
->context
= cp_parser_context_new (parser
->context
);
30017 /* Begin saving tokens. */
30018 cp_lexer_save_tokens (parser
->lexer
);
30019 /* In order to avoid repetitive access control error messages,
30020 access checks are queued up until we are no longer parsing
30022 push_deferring_access_checks (dk_deferred
);
30025 /* Commit to the currently active tentative parse. */
30028 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
30030 cp_parser_context
*context
;
30033 /* Mark all of the levels as committed. */
30034 lexer
= parser
->lexer
;
30035 for (context
= parser
->context
; context
->next
; context
= context
->next
)
30037 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
30039 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
30040 while (!cp_lexer_saving_tokens (lexer
))
30041 lexer
= lexer
->next
;
30042 cp_lexer_commit_tokens (lexer
);
30046 /* Commit to the topmost currently active tentative parse.
30048 Note that this function shouldn't be called when there are
30049 irreversible side-effects while in a tentative state. For
30050 example, we shouldn't create a permanent entry in the symbol
30051 table, or issue an error message that might not apply if the
30052 tentative parse is aborted. */
30055 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
30057 cp_parser_context
*context
= parser
->context
;
30058 cp_lexer
*lexer
= parser
->lexer
;
30062 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
30064 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
30066 while (!cp_lexer_saving_tokens (lexer
))
30067 lexer
= lexer
->next
;
30068 cp_lexer_commit_tokens (lexer
);
30072 /* Abort the currently active tentative parse. All consumed tokens
30073 will be rolled back, and no diagnostics will be issued. */
30076 cp_parser_abort_tentative_parse (cp_parser
* parser
)
30078 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
30079 || errorcount
> 0);
30080 cp_parser_simulate_error (parser
);
30081 /* Now, pretend that we want to see if the construct was
30082 successfully parsed. */
30083 cp_parser_parse_definitely (parser
);
30086 /* Stop parsing tentatively. If a parse error has occurred, restore the
30087 token stream. Otherwise, commit to the tokens we have consumed.
30088 Returns true if no error occurred; false otherwise. */
30091 cp_parser_parse_definitely (cp_parser
* parser
)
30093 bool error_occurred
;
30094 cp_parser_context
*context
;
30096 /* Remember whether or not an error occurred, since we are about to
30097 destroy that information. */
30098 error_occurred
= cp_parser_error_occurred (parser
);
30099 /* Remove the topmost context from the stack. */
30100 context
= parser
->context
;
30101 parser
->context
= context
->next
;
30102 /* If no parse errors occurred, commit to the tentative parse. */
30103 if (!error_occurred
)
30105 /* Commit to the tokens read tentatively, unless that was
30107 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
30108 cp_lexer_commit_tokens (parser
->lexer
);
30110 pop_to_parent_deferring_access_checks ();
30112 /* Otherwise, if errors occurred, roll back our state so that things
30113 are just as they were before we began the tentative parse. */
30116 cp_lexer_rollback_tokens (parser
->lexer
);
30117 pop_deferring_access_checks ();
30119 /* Add the context to the front of the free list. */
30120 context
->next
= cp_parser_context_free_list
;
30121 cp_parser_context_free_list
= context
;
30123 return !error_occurred
;
30126 /* Returns true if we are parsing tentatively and are not committed to
30127 this tentative parse. */
30130 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
30132 return (cp_parser_parsing_tentatively (parser
)
30133 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
30136 /* Returns nonzero iff an error has occurred during the most recent
30137 tentative parse. */
30140 cp_parser_error_occurred (cp_parser
* parser
)
30142 return (cp_parser_parsing_tentatively (parser
)
30143 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
30146 /* Returns nonzero if GNU extensions are allowed. */
30149 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
30151 return parser
->allow_gnu_extensions_p
;
30154 /* Objective-C++ Productions */
30157 /* Parse an Objective-C expression, which feeds into a primary-expression
30161 objc-message-expression
30162 objc-string-literal
30163 objc-encode-expression
30164 objc-protocol-expression
30165 objc-selector-expression
30167 Returns a tree representation of the expression. */
30170 cp_parser_objc_expression (cp_parser
* parser
)
30172 /* Try to figure out what kind of declaration is present. */
30173 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30177 case CPP_OPEN_SQUARE
:
30178 return cp_parser_objc_message_expression (parser
);
30180 case CPP_OBJC_STRING
:
30181 kwd
= cp_lexer_consume_token (parser
->lexer
);
30182 return objc_build_string_object (kwd
->u
.value
);
30185 switch (kwd
->keyword
)
30187 case RID_AT_ENCODE
:
30188 return cp_parser_objc_encode_expression (parser
);
30190 case RID_AT_PROTOCOL
:
30191 return cp_parser_objc_protocol_expression (parser
);
30193 case RID_AT_SELECTOR
:
30194 return cp_parser_objc_selector_expression (parser
);
30201 error_at (kwd
->location
,
30202 "misplaced %<@%D%> Objective-C++ construct",
30204 cp_parser_skip_to_end_of_block_or_statement (parser
);
30207 return error_mark_node
;
30210 /* Parse an Objective-C message expression.
30212 objc-message-expression:
30213 [ objc-message-receiver objc-message-args ]
30215 Returns a representation of an Objective-C message. */
30218 cp_parser_objc_message_expression (cp_parser
* parser
)
30220 tree receiver
, messageargs
;
30222 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30223 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
30224 receiver
= cp_parser_objc_message_receiver (parser
);
30225 messageargs
= cp_parser_objc_message_args (parser
);
30226 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30227 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30229 tree result
= objc_build_message_expr (receiver
, messageargs
);
30231 /* Construct a location e.g.
30234 ranging from the '[' to the ']', with the caret at the start. */
30235 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
30236 protected_set_expr_location (result
, combined_loc
);
30241 /* Parse an objc-message-receiver.
30243 objc-message-receiver:
30245 simple-type-specifier
30247 Returns a representation of the type or expression. */
30250 cp_parser_objc_message_receiver (cp_parser
* parser
)
30254 /* An Objective-C message receiver may be either (1) a type
30255 or (2) an expression. */
30256 cp_parser_parse_tentatively (parser
);
30257 rcv
= cp_parser_expression (parser
);
30259 /* If that worked out, fine. */
30260 if (cp_parser_parse_definitely (parser
))
30263 cp_parser_parse_tentatively (parser
);
30264 rcv
= cp_parser_simple_type_specifier (parser
,
30265 /*decl_specs=*/NULL
,
30266 CP_PARSER_FLAGS_NONE
);
30268 if (cp_parser_parse_definitely (parser
))
30269 return objc_get_class_reference (rcv
);
30271 cp_parser_error (parser
, "objective-c++ message receiver expected");
30272 return error_mark_node
;
30275 /* Parse the arguments and selectors comprising an Objective-C message.
30280 objc-selector-args , objc-comma-args
30282 objc-selector-args:
30283 objc-selector [opt] : assignment-expression
30284 objc-selector-args objc-selector [opt] : assignment-expression
30287 assignment-expression
30288 objc-comma-args , assignment-expression
30290 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30291 selector arguments and TREE_VALUE containing a list of comma
30295 cp_parser_objc_message_args (cp_parser
* parser
)
30297 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
30298 bool maybe_unary_selector_p
= true;
30299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30301 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30303 tree selector
= NULL_TREE
, arg
;
30305 if (token
->type
!= CPP_COLON
)
30306 selector
= cp_parser_objc_selector (parser
);
30308 /* Detect if we have a unary selector. */
30309 if (maybe_unary_selector_p
30310 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30311 return build_tree_list (selector
, NULL_TREE
);
30313 maybe_unary_selector_p
= false;
30314 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30315 arg
= cp_parser_assignment_expression (parser
);
30318 = chainon (sel_args
,
30319 build_tree_list (selector
, arg
));
30321 token
= cp_lexer_peek_token (parser
->lexer
);
30324 /* Handle non-selector arguments, if any. */
30325 while (token
->type
== CPP_COMMA
)
30329 cp_lexer_consume_token (parser
->lexer
);
30330 arg
= cp_parser_assignment_expression (parser
);
30333 = chainon (addl_args
,
30334 build_tree_list (NULL_TREE
, arg
));
30336 token
= cp_lexer_peek_token (parser
->lexer
);
30339 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
30341 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
30342 return build_tree_list (error_mark_node
, error_mark_node
);
30345 return build_tree_list (sel_args
, addl_args
);
30348 /* Parse an Objective-C encode expression.
30350 objc-encode-expression:
30351 @encode objc-typename
30353 Returns an encoded representation of the type argument. */
30356 cp_parser_objc_encode_expression (cp_parser
* parser
)
30360 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30362 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
30363 matching_parens parens
;
30364 parens
.require_open (parser
);
30365 token
= cp_lexer_peek_token (parser
->lexer
);
30366 type
= complete_type (cp_parser_type_id (parser
));
30367 parens
.require_close (parser
);
30371 error_at (token
->location
,
30372 "%<@encode%> must specify a type as an argument");
30373 return error_mark_node
;
30376 /* This happens if we find @encode(T) (where T is a template
30377 typename or something dependent on a template typename) when
30378 parsing a template. In that case, we can't compile it
30379 immediately, but we rather create an AT_ENCODE_EXPR which will
30380 need to be instantiated when the template is used.
30382 if (dependent_type_p (type
))
30384 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
30385 TREE_READONLY (value
) = 1;
30390 /* Build a location of the form:
30393 with caret==start at the @ token, finishing at the close paren. */
30394 location_t combined_loc
30395 = make_location (start_loc
, start_loc
,
30396 cp_lexer_previous_token (parser
->lexer
)->location
);
30398 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
30401 /* Parse an Objective-C @defs expression. */
30404 cp_parser_objc_defs_expression (cp_parser
*parser
)
30408 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
30409 matching_parens parens
;
30410 parens
.require_open (parser
);
30411 name
= cp_parser_identifier (parser
);
30412 parens
.require_close (parser
);
30414 return objc_get_class_ivars (name
);
30417 /* Parse an Objective-C protocol expression.
30419 objc-protocol-expression:
30420 @protocol ( identifier )
30422 Returns a representation of the protocol expression. */
30425 cp_parser_objc_protocol_expression (cp_parser
* parser
)
30428 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30430 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30431 matching_parens parens
;
30432 parens
.require_open (parser
);
30433 proto
= cp_parser_identifier (parser
);
30434 parens
.require_close (parser
);
30436 /* Build a location of the form:
30439 with caret==start at the @ token, finishing at the close paren. */
30440 location_t combined_loc
30441 = make_location (start_loc
, start_loc
,
30442 cp_lexer_previous_token (parser
->lexer
)->location
);
30443 tree result
= objc_build_protocol_expr (proto
);
30444 protected_set_expr_location (result
, combined_loc
);
30448 /* Parse an Objective-C selector expression.
30450 objc-selector-expression:
30451 @selector ( objc-method-signature )
30453 objc-method-signature:
30459 objc-selector-seq objc-selector :
30461 Returns a representation of the method selector. */
30464 cp_parser_objc_selector_expression (cp_parser
* parser
)
30466 tree sel_seq
= NULL_TREE
;
30467 bool maybe_unary_selector_p
= true;
30469 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30471 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30472 matching_parens parens
;
30473 parens
.require_open (parser
);
30474 token
= cp_lexer_peek_token (parser
->lexer
);
30476 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30477 || token
->type
== CPP_SCOPE
)
30479 tree selector
= NULL_TREE
;
30481 if (token
->type
!= CPP_COLON
30482 || token
->type
== CPP_SCOPE
)
30483 selector
= cp_parser_objc_selector (parser
);
30485 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30486 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30488 /* Detect if we have a unary selector. */
30489 if (maybe_unary_selector_p
)
30491 sel_seq
= selector
;
30492 goto finish_selector
;
30496 cp_parser_error (parser
, "expected %<:%>");
30499 maybe_unary_selector_p
= false;
30500 token
= cp_lexer_consume_token (parser
->lexer
);
30502 if (token
->type
== CPP_SCOPE
)
30505 = chainon (sel_seq
,
30506 build_tree_list (selector
, NULL_TREE
));
30508 = chainon (sel_seq
,
30509 build_tree_list (NULL_TREE
, NULL_TREE
));
30513 = chainon (sel_seq
,
30514 build_tree_list (selector
, NULL_TREE
));
30516 token
= cp_lexer_peek_token (parser
->lexer
);
30520 parens
.require_close (parser
);
30523 /* Build a location of the form:
30526 with caret==start at the @ token, finishing at the close paren. */
30527 location_t combined_loc
30528 = make_location (loc
, loc
,
30529 cp_lexer_previous_token (parser
->lexer
)->location
);
30530 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30531 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30532 protected_set_expr_location (result
, combined_loc
);
30536 /* Parse a list of identifiers.
30538 objc-identifier-list:
30540 objc-identifier-list , identifier
30542 Returns a TREE_LIST of identifier nodes. */
30545 cp_parser_objc_identifier_list (cp_parser
* parser
)
30551 identifier
= cp_parser_identifier (parser
);
30552 if (identifier
== error_mark_node
)
30553 return error_mark_node
;
30555 list
= build_tree_list (NULL_TREE
, identifier
);
30556 sep
= cp_lexer_peek_token (parser
->lexer
);
30558 while (sep
->type
== CPP_COMMA
)
30560 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30561 identifier
= cp_parser_identifier (parser
);
30562 if (identifier
== error_mark_node
)
30565 list
= chainon (list
, build_tree_list (NULL_TREE
,
30567 sep
= cp_lexer_peek_token (parser
->lexer
);
30573 /* Parse an Objective-C alias declaration.
30575 objc-alias-declaration:
30576 @compatibility_alias identifier identifier ;
30578 This function registers the alias mapping with the Objective-C front end.
30579 It returns nothing. */
30582 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30586 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30587 alias
= cp_parser_identifier (parser
);
30588 orig
= cp_parser_identifier (parser
);
30589 objc_declare_alias (alias
, orig
);
30590 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30593 /* Parse an Objective-C class forward-declaration.
30595 objc-class-declaration:
30596 @class objc-identifier-list ;
30598 The function registers the forward declarations with the Objective-C
30599 front end. It returns nothing. */
30602 cp_parser_objc_class_declaration (cp_parser
* parser
)
30604 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30609 id
= cp_parser_identifier (parser
);
30610 if (id
== error_mark_node
)
30613 objc_declare_class (id
);
30615 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30616 cp_lexer_consume_token (parser
->lexer
);
30620 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30623 /* Parse a list of Objective-C protocol references.
30625 objc-protocol-refs-opt:
30626 objc-protocol-refs [opt]
30628 objc-protocol-refs:
30629 < objc-identifier-list >
30631 Returns a TREE_LIST of identifiers, if any. */
30634 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30636 tree protorefs
= NULL_TREE
;
30638 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30640 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30641 protorefs
= cp_parser_objc_identifier_list (parser
);
30642 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30648 /* Parse a Objective-C visibility specification. */
30651 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30653 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30655 switch (vis
->keyword
)
30657 case RID_AT_PRIVATE
:
30658 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30660 case RID_AT_PROTECTED
:
30661 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30663 case RID_AT_PUBLIC
:
30664 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30666 case RID_AT_PACKAGE
:
30667 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30673 /* Eat '@private'/'@protected'/'@public'. */
30674 cp_lexer_consume_token (parser
->lexer
);
30677 /* Parse an Objective-C method type. Return 'true' if it is a class
30678 (+) method, and 'false' if it is an instance (-) method. */
30681 cp_parser_objc_method_type (cp_parser
* parser
)
30683 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30689 /* Parse an Objective-C protocol qualifier. */
30692 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30694 tree quals
= NULL_TREE
, node
;
30695 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30697 node
= token
->u
.value
;
30699 while (node
&& identifier_p (node
)
30700 && (node
== ridpointers
[(int) RID_IN
]
30701 || node
== ridpointers
[(int) RID_OUT
]
30702 || node
== ridpointers
[(int) RID_INOUT
]
30703 || node
== ridpointers
[(int) RID_BYCOPY
]
30704 || node
== ridpointers
[(int) RID_BYREF
]
30705 || node
== ridpointers
[(int) RID_ONEWAY
]))
30707 quals
= tree_cons (NULL_TREE
, node
, quals
);
30708 cp_lexer_consume_token (parser
->lexer
);
30709 token
= cp_lexer_peek_token (parser
->lexer
);
30710 node
= token
->u
.value
;
30716 /* Parse an Objective-C typename. */
30719 cp_parser_objc_typename (cp_parser
* parser
)
30721 tree type_name
= NULL_TREE
;
30723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30725 tree proto_quals
, cp_type
= NULL_TREE
;
30727 matching_parens parens
;
30728 parens
.consume_open (parser
); /* Eat '('. */
30729 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30731 /* An ObjC type name may consist of just protocol qualifiers, in which
30732 case the type shall default to 'id'. */
30733 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30735 cp_type
= cp_parser_type_id (parser
);
30737 /* If the type could not be parsed, an error has already
30738 been produced. For error recovery, behave as if it had
30739 not been specified, which will use the default type
30741 if (cp_type
== error_mark_node
)
30743 cp_type
= NULL_TREE
;
30744 /* We need to skip to the closing parenthesis as
30745 cp_parser_type_id() does not seem to do it for
30747 cp_parser_skip_to_closing_parenthesis (parser
,
30748 /*recovering=*/true,
30749 /*or_comma=*/false,
30750 /*consume_paren=*/false);
30754 parens
.require_close (parser
);
30755 type_name
= build_tree_list (proto_quals
, cp_type
);
30761 /* Check to see if TYPE refers to an Objective-C selector name. */
30764 cp_parser_objc_selector_p (enum cpp_ttype type
)
30766 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30767 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30768 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30769 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30770 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30773 /* Parse an Objective-C selector. */
30776 cp_parser_objc_selector (cp_parser
* parser
)
30778 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30780 if (!cp_parser_objc_selector_p (token
->type
))
30782 error_at (token
->location
, "invalid Objective-C++ selector name");
30783 return error_mark_node
;
30786 /* C++ operator names are allowed to appear in ObjC selectors. */
30787 switch (token
->type
)
30789 case CPP_AND_AND
: return get_identifier ("and");
30790 case CPP_AND_EQ
: return get_identifier ("and_eq");
30791 case CPP_AND
: return get_identifier ("bitand");
30792 case CPP_OR
: return get_identifier ("bitor");
30793 case CPP_COMPL
: return get_identifier ("compl");
30794 case CPP_NOT
: return get_identifier ("not");
30795 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30796 case CPP_OR_OR
: return get_identifier ("or");
30797 case CPP_OR_EQ
: return get_identifier ("or_eq");
30798 case CPP_XOR
: return get_identifier ("xor");
30799 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30800 default: return token
->u
.value
;
30804 /* Parse an Objective-C params list. */
30807 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30809 tree params
= NULL_TREE
;
30810 bool maybe_unary_selector_p
= true;
30811 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30813 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30815 tree selector
= NULL_TREE
, type_name
, identifier
;
30816 tree parm_attr
= NULL_TREE
;
30818 if (token
->keyword
== RID_ATTRIBUTE
)
30821 if (token
->type
!= CPP_COLON
)
30822 selector
= cp_parser_objc_selector (parser
);
30824 /* Detect if we have a unary selector. */
30825 if (maybe_unary_selector_p
30826 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30828 params
= selector
; /* Might be followed by attributes. */
30832 maybe_unary_selector_p
= false;
30833 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30835 /* Something went quite wrong. There should be a colon
30836 here, but there is not. Stop parsing parameters. */
30839 type_name
= cp_parser_objc_typename (parser
);
30840 /* New ObjC allows attributes on parameters too. */
30841 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30842 parm_attr
= cp_parser_attributes_opt (parser
);
30843 identifier
= cp_parser_identifier (parser
);
30847 objc_build_keyword_decl (selector
,
30852 token
= cp_lexer_peek_token (parser
->lexer
);
30855 if (params
== NULL_TREE
)
30857 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30858 return error_mark_node
;
30861 /* We allow tail attributes for the method. */
30862 if (token
->keyword
== RID_ATTRIBUTE
)
30864 *attributes
= cp_parser_attributes_opt (parser
);
30865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30866 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30868 cp_parser_error (parser
,
30869 "method attributes must be specified at the end");
30870 return error_mark_node
;
30873 if (params
== NULL_TREE
)
30875 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30876 return error_mark_node
;
30881 /* Parse the non-keyword Objective-C params. */
30884 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30887 tree params
= make_node (TREE_LIST
);
30888 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30889 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30891 while (token
->type
== CPP_COMMA
)
30893 cp_parameter_declarator
*parmdecl
;
30896 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30897 token
= cp_lexer_peek_token (parser
->lexer
);
30899 if (token
->type
== CPP_ELLIPSIS
)
30901 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30903 token
= cp_lexer_peek_token (parser
->lexer
);
30907 /* TODO: parse attributes for tail parameters. */
30908 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
30910 parm
= grokdeclarator (parmdecl
->declarator
,
30911 &parmdecl
->decl_specifiers
,
30912 PARM
, /*initialized=*/0,
30913 /*attrlist=*/NULL
);
30915 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30916 token
= cp_lexer_peek_token (parser
->lexer
);
30919 /* We allow tail attributes for the method. */
30920 if (token
->keyword
== RID_ATTRIBUTE
)
30922 if (*attributes
== NULL_TREE
)
30924 *attributes
= cp_parser_attributes_opt (parser
);
30925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30926 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30930 /* We have an error, but parse the attributes, so that we can
30932 *attributes
= cp_parser_attributes_opt (parser
);
30934 cp_parser_error (parser
,
30935 "method attributes must be specified at the end");
30936 return error_mark_node
;
30942 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30945 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30947 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30949 /* If the next token is `extern' and the following token is a string
30950 literal, then we have a linkage specification. */
30951 if (token
->keyword
== RID_EXTERN
30952 && cp_parser_is_pure_string_literal
30953 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30954 cp_parser_linkage_specification (parser
);
30955 /* Handle #pragma, if any. */
30956 else if (token
->type
== CPP_PRAGMA
)
30957 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30958 /* Allow stray semicolons. */
30959 else if (token
->type
== CPP_SEMICOLON
)
30960 cp_lexer_consume_token (parser
->lexer
);
30961 /* Mark methods as optional or required, when building protocols. */
30962 else if (token
->keyword
== RID_AT_OPTIONAL
)
30964 cp_lexer_consume_token (parser
->lexer
);
30965 objc_set_method_opt (true);
30967 else if (token
->keyword
== RID_AT_REQUIRED
)
30969 cp_lexer_consume_token (parser
->lexer
);
30970 objc_set_method_opt (false);
30972 else if (token
->keyword
== RID_NAMESPACE
)
30973 cp_parser_namespace_definition (parser
);
30974 /* Other stray characters must generate errors. */
30975 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30977 cp_lexer_consume_token (parser
->lexer
);
30978 error ("stray %qs between Objective-C++ methods",
30979 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30981 /* Finally, try to parse a block-declaration, or a function-definition. */
30983 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30986 /* Parse a method signature. */
30989 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30991 tree rettype
, kwdparms
, optparms
;
30992 bool ellipsis
= false;
30993 bool is_class_method
;
30995 is_class_method
= cp_parser_objc_method_type (parser
);
30996 rettype
= cp_parser_objc_typename (parser
);
30997 *attributes
= NULL_TREE
;
30998 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30999 if (kwdparms
== error_mark_node
)
31000 return error_mark_node
;
31001 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
31002 if (optparms
== error_mark_node
)
31003 return error_mark_node
;
31005 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
31009 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
31012 cp_lexer_save_tokens (parser
->lexer
);
31013 tattr
= cp_parser_attributes_opt (parser
);
31014 gcc_assert (tattr
) ;
31016 /* If the attributes are followed by a method introducer, this is not allowed.
31017 Dump the attributes and flag the situation. */
31018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
31019 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
31022 /* Otherwise, the attributes introduce some interstitial code, possibly so
31023 rewind to allow that check. */
31024 cp_lexer_rollback_tokens (parser
->lexer
);
31028 /* Parse an Objective-C method prototype list. */
31031 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
31033 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31035 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31037 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
31039 tree attributes
, sig
;
31040 bool is_class_method
;
31041 if (token
->type
== CPP_PLUS
)
31042 is_class_method
= true;
31044 is_class_method
= false;
31045 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
31046 if (sig
== error_mark_node
)
31048 cp_parser_skip_to_end_of_block_or_statement (parser
);
31049 token
= cp_lexer_peek_token (parser
->lexer
);
31052 objc_add_method_declaration (is_class_method
, sig
, attributes
);
31053 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31055 else if (token
->keyword
== RID_AT_PROPERTY
)
31056 cp_parser_objc_at_property_declaration (parser
);
31057 else if (token
->keyword
== RID_ATTRIBUTE
31058 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31059 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31061 "prefix attributes are ignored for methods");
31063 /* Allow for interspersed non-ObjC++ code. */
31064 cp_parser_objc_interstitial_code (parser
);
31066 token
= cp_lexer_peek_token (parser
->lexer
);
31069 if (token
->type
!= CPP_EOF
)
31070 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31072 cp_parser_error (parser
, "expected %<@end%>");
31074 objc_finish_interface ();
31077 /* Parse an Objective-C method definition list. */
31080 cp_parser_objc_method_definition_list (cp_parser
* parser
)
31082 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31084 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31088 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
31091 tree sig
, attribute
;
31092 bool is_class_method
;
31093 if (token
->type
== CPP_PLUS
)
31094 is_class_method
= true;
31096 is_class_method
= false;
31097 push_deferring_access_checks (dk_deferred
);
31098 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
31099 if (sig
== error_mark_node
)
31101 cp_parser_skip_to_end_of_block_or_statement (parser
);
31102 token
= cp_lexer_peek_token (parser
->lexer
);
31105 objc_start_method_definition (is_class_method
, sig
, attribute
,
31108 /* For historical reasons, we accept an optional semicolon. */
31109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31110 cp_lexer_consume_token (parser
->lexer
);
31112 ptk
= cp_lexer_peek_token (parser
->lexer
);
31113 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
31114 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
31116 perform_deferred_access_checks (tf_warning_or_error
);
31117 stop_deferring_access_checks ();
31118 meth
= cp_parser_function_definition_after_declarator (parser
,
31120 pop_deferring_access_checks ();
31121 objc_finish_method_definition (meth
);
31124 /* The following case will be removed once @synthesize is
31125 completely implemented. */
31126 else if (token
->keyword
== RID_AT_PROPERTY
)
31127 cp_parser_objc_at_property_declaration (parser
);
31128 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
31129 cp_parser_objc_at_synthesize_declaration (parser
);
31130 else if (token
->keyword
== RID_AT_DYNAMIC
)
31131 cp_parser_objc_at_dynamic_declaration (parser
);
31132 else if (token
->keyword
== RID_ATTRIBUTE
31133 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31134 warning_at (token
->location
, OPT_Wattributes
,
31135 "prefix attributes are ignored for methods");
31137 /* Allow for interspersed non-ObjC++ code. */
31138 cp_parser_objc_interstitial_code (parser
);
31140 token
= cp_lexer_peek_token (parser
->lexer
);
31143 if (token
->type
!= CPP_EOF
)
31144 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31146 cp_parser_error (parser
, "expected %<@end%>");
31148 objc_finish_implementation ();
31151 /* Parse Objective-C ivars. */
31154 cp_parser_objc_class_ivars (cp_parser
* parser
)
31156 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31158 if (token
->type
!= CPP_OPEN_BRACE
)
31159 return; /* No ivars specified. */
31161 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
31162 token
= cp_lexer_peek_token (parser
->lexer
);
31164 while (token
->type
!= CPP_CLOSE_BRACE
31165 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31167 cp_decl_specifier_seq declspecs
;
31168 int decl_class_or_enum_p
;
31169 tree prefix_attributes
;
31171 cp_parser_objc_visibility_spec (parser
);
31173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
31176 cp_parser_decl_specifier_seq (parser
,
31177 CP_PARSER_FLAGS_OPTIONAL
,
31179 &decl_class_or_enum_p
);
31181 /* auto, register, static, extern, mutable. */
31182 if (declspecs
.storage_class
!= sc_none
)
31184 cp_parser_error (parser
, "invalid type for instance variable");
31185 declspecs
.storage_class
= sc_none
;
31188 /* thread_local. */
31189 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31191 cp_parser_error (parser
, "invalid type for instance variable");
31192 declspecs
.locations
[ds_thread
] = 0;
31196 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31198 cp_parser_error (parser
, "invalid type for instance variable");
31199 declspecs
.locations
[ds_typedef
] = 0;
31202 prefix_attributes
= declspecs
.attributes
;
31203 declspecs
.attributes
= NULL_TREE
;
31205 /* Keep going until we hit the `;' at the end of the
31207 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31209 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
31210 cp_declarator
*declarator
= NULL
;
31211 int ctor_dtor_or_conv_p
;
31213 /* Check for a (possibly unnamed) bitfield declaration. */
31214 token
= cp_lexer_peek_token (parser
->lexer
);
31215 if (token
->type
== CPP_COLON
)
31218 if (token
->type
== CPP_NAME
31219 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31222 /* Get the name of the bitfield. */
31223 declarator
= make_id_declarator (NULL_TREE
,
31224 cp_parser_identifier (parser
),
31225 sfk_none
, token
->location
);
31228 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31229 /* Get the width of the bitfield. */
31231 = cp_parser_constant_expression (parser
);
31235 /* Parse the declarator. */
31237 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31238 CP_PARSER_FLAGS_NONE
,
31239 &ctor_dtor_or_conv_p
,
31240 /*parenthesized_p=*/NULL
,
31241 /*member_p=*/false,
31242 /*friend_p=*/false,
31243 /*static_p=*/false);
31246 /* Look for attributes that apply to the ivar. */
31247 attributes
= cp_parser_attributes_opt (parser
);
31248 /* Remember which attributes are prefix attributes and
31250 first_attribute
= attributes
;
31251 /* Combine the attributes. */
31252 attributes
= attr_chainon (prefix_attributes
, attributes
);
31255 /* Create the bitfield declaration. */
31256 decl
= grokbitfield (declarator
, &declspecs
,
31257 width
, NULL_TREE
, attributes
);
31259 decl
= grokfield (declarator
, &declspecs
,
31260 NULL_TREE
, /*init_const_expr_p=*/false,
31261 NULL_TREE
, attributes
);
31263 /* Add the instance variable. */
31264 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
31265 objc_add_instance_variable (decl
);
31267 /* Reset PREFIX_ATTRIBUTES. */
31268 if (attributes
!= error_mark_node
)
31270 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31271 attributes
= TREE_CHAIN (attributes
);
31273 TREE_CHAIN (attributes
) = NULL_TREE
;
31276 token
= cp_lexer_peek_token (parser
->lexer
);
31278 if (token
->type
== CPP_COMMA
)
31280 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31286 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31287 token
= cp_lexer_peek_token (parser
->lexer
);
31290 if (token
->keyword
== RID_AT_END
)
31291 cp_parser_error (parser
, "expected %<}%>");
31293 /* Do not consume the RID_AT_END, so it will be read again as terminating
31294 the @interface of @implementation. */
31295 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31296 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
31298 /* For historical reasons, we accept an optional semicolon. */
31299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31300 cp_lexer_consume_token (parser
->lexer
);
31303 /* Parse an Objective-C protocol declaration. */
31306 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
31308 tree proto
, protorefs
;
31311 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
31312 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31314 tok
= cp_lexer_peek_token (parser
->lexer
);
31315 error_at (tok
->location
, "identifier expected after %<@protocol%>");
31316 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31320 /* See if we have a forward declaration or a definition. */
31321 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
31323 /* Try a forward declaration first. */
31324 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
31330 id
= cp_parser_identifier (parser
);
31331 if (id
== error_mark_node
)
31334 objc_declare_protocol (id
, attributes
);
31336 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31337 cp_lexer_consume_token (parser
->lexer
);
31341 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31344 /* Ok, we got a full-fledged definition (or at least should). */
31347 proto
= cp_parser_identifier (parser
);
31348 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
31349 objc_start_protocol (proto
, protorefs
, attributes
);
31350 cp_parser_objc_method_prototype_list (parser
);
31354 /* Parse an Objective-C superclass or category. */
31357 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
31360 tree
*categ
, bool *is_class_extension
)
31362 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
31364 *super
= *categ
= NULL_TREE
;
31365 *is_class_extension
= false;
31366 if (next
->type
== CPP_COLON
)
31368 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31369 *super
= cp_parser_identifier (parser
);
31371 else if (next
->type
== CPP_OPEN_PAREN
)
31373 matching_parens parens
;
31374 parens
.consume_open (parser
); /* Eat '('. */
31376 /* If there is no category name, and this is an @interface, we
31377 have a class extension. */
31378 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31380 *categ
= NULL_TREE
;
31381 *is_class_extension
= true;
31384 *categ
= cp_parser_identifier (parser
);
31386 parens
.require_close (parser
);
31390 /* Parse an Objective-C class interface. */
31393 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
31395 tree name
, super
, categ
, protos
;
31396 bool is_class_extension
;
31398 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
31399 name
= cp_parser_identifier (parser
);
31400 if (name
== error_mark_node
)
31402 /* It's hard to recover because even if valid @interface stuff
31403 is to follow, we can't compile it (or validate it) if we
31404 don't even know which class it refers to. Let's assume this
31405 was a stray '@interface' token in the stream and skip it.
31409 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
31410 &is_class_extension
);
31411 protos
= cp_parser_objc_protocol_refs_opt (parser
);
31413 /* We have either a class or a category on our hands. */
31414 if (categ
|| is_class_extension
)
31415 objc_start_category_interface (name
, categ
, protos
, attributes
);
31418 objc_start_class_interface (name
, super
, protos
, attributes
);
31419 /* Handle instance variable declarations, if any. */
31420 cp_parser_objc_class_ivars (parser
);
31421 objc_continue_interface ();
31424 cp_parser_objc_method_prototype_list (parser
);
31427 /* Parse an Objective-C class implementation. */
31430 cp_parser_objc_class_implementation (cp_parser
* parser
)
31432 tree name
, super
, categ
;
31433 bool is_class_extension
;
31435 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
31436 name
= cp_parser_identifier (parser
);
31437 if (name
== error_mark_node
)
31439 /* It's hard to recover because even if valid @implementation
31440 stuff is to follow, we can't compile it (or validate it) if
31441 we don't even know which class it refers to. Let's assume
31442 this was a stray '@implementation' token in the stream and
31447 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31448 &is_class_extension
);
31450 /* We have either a class or a category on our hands. */
31452 objc_start_category_implementation (name
, categ
);
31455 objc_start_class_implementation (name
, super
);
31456 /* Handle instance variable declarations, if any. */
31457 cp_parser_objc_class_ivars (parser
);
31458 objc_continue_implementation ();
31461 cp_parser_objc_method_definition_list (parser
);
31464 /* Consume the @end token and finish off the implementation. */
31467 cp_parser_objc_end_implementation (cp_parser
* parser
)
31469 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31470 objc_finish_implementation ();
31473 /* Parse an Objective-C declaration. */
31476 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31478 /* Try to figure out what kind of declaration is present. */
31479 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31482 switch (kwd
->keyword
)
31487 error_at (kwd
->location
, "attributes may not be specified before"
31488 " the %<@%D%> Objective-C++ keyword",
31492 case RID_AT_IMPLEMENTATION
:
31493 warning_at (kwd
->location
, OPT_Wattributes
,
31494 "prefix attributes are ignored before %<@%D%>",
31501 switch (kwd
->keyword
)
31504 cp_parser_objc_alias_declaration (parser
);
31507 cp_parser_objc_class_declaration (parser
);
31509 case RID_AT_PROTOCOL
:
31510 cp_parser_objc_protocol_declaration (parser
, attributes
);
31512 case RID_AT_INTERFACE
:
31513 cp_parser_objc_class_interface (parser
, attributes
);
31515 case RID_AT_IMPLEMENTATION
:
31516 cp_parser_objc_class_implementation (parser
);
31519 cp_parser_objc_end_implementation (parser
);
31522 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31524 cp_parser_skip_to_end_of_block_or_statement (parser
);
31528 /* Parse an Objective-C try-catch-finally statement.
31530 objc-try-catch-finally-stmt:
31531 @try compound-statement objc-catch-clause-seq [opt]
31532 objc-finally-clause [opt]
31534 objc-catch-clause-seq:
31535 objc-catch-clause objc-catch-clause-seq [opt]
31538 @catch ( objc-exception-declaration ) compound-statement
31540 objc-finally-clause:
31541 @finally compound-statement
31543 objc-exception-declaration:
31544 parameter-declaration
31547 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31551 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31552 for C. Keep them in sync. */
31555 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31557 location_t location
;
31560 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31561 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31562 objc_maybe_warn_exceptions (location
);
31563 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31564 node, lest it get absorbed into the surrounding block. */
31565 stmt
= push_stmt_list ();
31566 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31567 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31569 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31571 cp_parameter_declarator
*parm
;
31572 tree parameter_declaration
= error_mark_node
;
31573 bool seen_open_paren
= false;
31574 matching_parens parens
;
31576 cp_lexer_consume_token (parser
->lexer
);
31577 if (parens
.require_open (parser
))
31578 seen_open_paren
= true;
31579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31581 /* We have "@catch (...)" (where the '...' are literally
31582 what is in the code). Skip the '...'.
31583 parameter_declaration is set to NULL_TREE, and
31584 objc_being_catch_clauses() knows that that means
31586 cp_lexer_consume_token (parser
->lexer
);
31587 parameter_declaration
= NULL_TREE
;
31591 /* We have "@catch (NSException *exception)" or something
31592 like that. Parse the parameter declaration. */
31593 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
31596 parameter_declaration
= error_mark_node
;
31598 parameter_declaration
= grokdeclarator (parm
->declarator
,
31599 &parm
->decl_specifiers
,
31600 PARM
, /*initialized=*/0,
31601 /*attrlist=*/NULL
);
31603 if (seen_open_paren
)
31604 parens
.require_close (parser
);
31607 /* If there was no open parenthesis, we are recovering from
31608 an error, and we are trying to figure out what mistake
31609 the user has made. */
31611 /* If there is an immediate closing parenthesis, the user
31612 probably forgot the opening one (ie, they typed "@catch
31613 NSException *e)". Parse the closing parenthesis and keep
31615 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31616 cp_lexer_consume_token (parser
->lexer
);
31618 /* If these is no immediate closing parenthesis, the user
31619 probably doesn't know that parenthesis are required at
31620 all (ie, they typed "@catch NSException *e"). So, just
31621 forget about the closing parenthesis and keep going. */
31623 objc_begin_catch_clause (parameter_declaration
);
31624 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31625 objc_finish_catch_clause ();
31627 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31629 cp_lexer_consume_token (parser
->lexer
);
31630 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31631 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31632 node, lest it get absorbed into the surrounding block. */
31633 stmt
= push_stmt_list ();
31634 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31635 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31638 return objc_finish_try_stmt ();
31641 /* Parse an Objective-C synchronized statement.
31643 objc-synchronized-stmt:
31644 @synchronized ( expression ) compound-statement
31646 Returns NULL_TREE. */
31649 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31651 location_t location
;
31654 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31656 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31657 objc_maybe_warn_exceptions (location
);
31658 matching_parens parens
;
31659 parens
.require_open (parser
);
31660 lock
= cp_parser_expression (parser
);
31661 parens
.require_close (parser
);
31663 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31664 node, lest it get absorbed into the surrounding block. */
31665 stmt
= push_stmt_list ();
31666 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31668 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31671 /* Parse an Objective-C throw statement.
31674 @throw assignment-expression [opt] ;
31676 Returns a constructed '@throw' statement. */
31679 cp_parser_objc_throw_statement (cp_parser
*parser
)
31681 tree expr
= NULL_TREE
;
31682 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31684 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31686 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31687 expr
= cp_parser_expression (parser
);
31689 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31691 return objc_build_throw_stmt (loc
, expr
);
31694 /* Parse an Objective-C statement. */
31697 cp_parser_objc_statement (cp_parser
* parser
)
31699 /* Try to figure out what kind of declaration is present. */
31700 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31702 switch (kwd
->keyword
)
31705 return cp_parser_objc_try_catch_finally_statement (parser
);
31706 case RID_AT_SYNCHRONIZED
:
31707 return cp_parser_objc_synchronized_statement (parser
);
31709 return cp_parser_objc_throw_statement (parser
);
31711 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31713 cp_parser_skip_to_end_of_block_or_statement (parser
);
31716 return error_mark_node
;
31719 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31720 look ahead to see if an objc keyword follows the attributes. This
31721 is to detect the use of prefix attributes on ObjC @interface and
31725 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31727 cp_lexer_save_tokens (parser
->lexer
);
31728 *attrib
= cp_parser_attributes_opt (parser
);
31729 gcc_assert (*attrib
);
31730 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31732 cp_lexer_commit_tokens (parser
->lexer
);
31735 cp_lexer_rollback_tokens (parser
->lexer
);
31739 /* This routine is a minimal replacement for
31740 c_parser_struct_declaration () used when parsing the list of
31741 types/names or ObjC++ properties. For example, when parsing the
31744 @property (readonly) int a, b, c;
31746 this function is responsible for parsing "int a, int b, int c" and
31747 returning the declarations as CHAIN of DECLs.
31749 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31750 similar parsing. */
31752 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31754 tree decls
= NULL_TREE
;
31755 cp_decl_specifier_seq declspecs
;
31756 int decl_class_or_enum_p
;
31757 tree prefix_attributes
;
31759 cp_parser_decl_specifier_seq (parser
,
31760 CP_PARSER_FLAGS_NONE
,
31762 &decl_class_or_enum_p
);
31764 if (declspecs
.type
== error_mark_node
)
31765 return error_mark_node
;
31767 /* auto, register, static, extern, mutable. */
31768 if (declspecs
.storage_class
!= sc_none
)
31770 cp_parser_error (parser
, "invalid type for property");
31771 declspecs
.storage_class
= sc_none
;
31774 /* thread_local. */
31775 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31777 cp_parser_error (parser
, "invalid type for property");
31778 declspecs
.locations
[ds_thread
] = 0;
31782 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31784 cp_parser_error (parser
, "invalid type for property");
31785 declspecs
.locations
[ds_typedef
] = 0;
31788 prefix_attributes
= declspecs
.attributes
;
31789 declspecs
.attributes
= NULL_TREE
;
31791 /* Keep going until we hit the `;' at the end of the declaration. */
31792 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31794 tree attributes
, first_attribute
, decl
;
31795 cp_declarator
*declarator
;
31798 /* Parse the declarator. */
31799 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31800 CP_PARSER_FLAGS_NONE
,
31801 NULL
, NULL
, false, false, false);
31803 /* Look for attributes that apply to the ivar. */
31804 attributes
= cp_parser_attributes_opt (parser
);
31805 /* Remember which attributes are prefix attributes and
31807 first_attribute
= attributes
;
31808 /* Combine the attributes. */
31809 attributes
= attr_chainon (prefix_attributes
, attributes
);
31811 decl
= grokfield (declarator
, &declspecs
,
31812 NULL_TREE
, /*init_const_expr_p=*/false,
31813 NULL_TREE
, attributes
);
31815 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31816 return error_mark_node
;
31818 /* Reset PREFIX_ATTRIBUTES. */
31819 if (attributes
!= error_mark_node
)
31821 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31822 attributes
= TREE_CHAIN (attributes
);
31824 TREE_CHAIN (attributes
) = NULL_TREE
;
31827 DECL_CHAIN (decl
) = decls
;
31830 token
= cp_lexer_peek_token (parser
->lexer
);
31831 if (token
->type
== CPP_COMMA
)
31833 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31842 /* Parse an Objective-C @property declaration. The syntax is:
31844 objc-property-declaration:
31845 '@property' objc-property-attributes[opt] struct-declaration ;
31847 objc-property-attributes:
31848 '(' objc-property-attribute-list ')'
31850 objc-property-attribute-list:
31851 objc-property-attribute
31852 objc-property-attribute-list, objc-property-attribute
31854 objc-property-attribute
31855 'getter' = identifier
31856 'setter' = identifier
31865 @property NSString *name;
31866 @property (readonly) id object;
31867 @property (retain, nonatomic, getter=getTheName) id name;
31868 @property int a, b, c;
31870 PS: This function is identical to
31871 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31873 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31875 /* The following variables hold the attributes of the properties as
31876 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31877 seen. When we see an attribute, we set them to 'true' (if they
31878 are boolean properties) or to the identifier (if they have an
31879 argument, ie, for getter and setter). Note that here we only
31880 parse the list of attributes, check the syntax and accumulate the
31881 attributes that we find. objc_add_property_declaration() will
31882 then process the information. */
31883 bool property_assign
= false;
31884 bool property_copy
= false;
31885 tree property_getter_ident
= NULL_TREE
;
31886 bool property_nonatomic
= false;
31887 bool property_readonly
= false;
31888 bool property_readwrite
= false;
31889 bool property_retain
= false;
31890 tree property_setter_ident
= NULL_TREE
;
31892 /* 'properties' is the list of properties that we read. Usually a
31893 single one, but maybe more (eg, in "@property int a, b, c;" there
31898 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31900 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31902 /* Parse the optional attribute list... */
31903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31906 matching_parens parens
;
31907 parens
.consume_open (parser
);
31911 bool syntax_error
= false;
31912 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31915 if (token
->type
!= CPP_NAME
)
31917 cp_parser_error (parser
, "expected identifier");
31920 keyword
= C_RID_CODE (token
->u
.value
);
31921 cp_lexer_consume_token (parser
->lexer
);
31924 case RID_ASSIGN
: property_assign
= true; break;
31925 case RID_COPY
: property_copy
= true; break;
31926 case RID_NONATOMIC
: property_nonatomic
= true; break;
31927 case RID_READONLY
: property_readonly
= true; break;
31928 case RID_READWRITE
: property_readwrite
= true; break;
31929 case RID_RETAIN
: property_retain
= true; break;
31933 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31935 if (keyword
== RID_GETTER
)
31936 cp_parser_error (parser
,
31937 "missing %<=%> (after %<getter%> attribute)");
31939 cp_parser_error (parser
,
31940 "missing %<=%> (after %<setter%> attribute)");
31941 syntax_error
= true;
31944 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31945 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31947 cp_parser_error (parser
, "expected identifier");
31948 syntax_error
= true;
31951 if (keyword
== RID_SETTER
)
31953 if (property_setter_ident
!= NULL_TREE
)
31955 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31956 cp_lexer_consume_token (parser
->lexer
);
31959 property_setter_ident
= cp_parser_objc_selector (parser
);
31960 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31961 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31963 cp_lexer_consume_token (parser
->lexer
);
31967 if (property_getter_ident
!= NULL_TREE
)
31969 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31970 cp_lexer_consume_token (parser
->lexer
);
31973 property_getter_ident
= cp_parser_objc_selector (parser
);
31977 cp_parser_error (parser
, "unknown property attribute");
31978 syntax_error
= true;
31985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31986 cp_lexer_consume_token (parser
->lexer
);
31991 /* FIXME: "@property (setter, assign);" will generate a spurious
31992 "error: expected ‘)’ before ‘,’ token". This is because
31993 cp_parser_require, unlike the C counterpart, will produce an
31994 error even if we are in error recovery. */
31995 if (!parens
.require_close (parser
))
31997 cp_parser_skip_to_closing_parenthesis (parser
,
31998 /*recovering=*/true,
31999 /*or_comma=*/false,
32000 /*consume_paren=*/true);
32004 /* ... and the property declaration(s). */
32005 properties
= cp_parser_objc_struct_declaration (parser
);
32007 if (properties
== error_mark_node
)
32009 cp_parser_skip_to_end_of_statement (parser
);
32010 /* If the next token is now a `;', consume it. */
32011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32012 cp_lexer_consume_token (parser
->lexer
);
32016 if (properties
== NULL_TREE
)
32017 cp_parser_error (parser
, "expected identifier");
32020 /* Comma-separated properties are chained together in
32021 reverse order; add them one by one. */
32022 properties
= nreverse (properties
);
32024 for (; properties
; properties
= TREE_CHAIN (properties
))
32025 objc_add_property_declaration (loc
, copy_node (properties
),
32026 property_readonly
, property_readwrite
,
32027 property_assign
, property_retain
,
32028 property_copy
, property_nonatomic
,
32029 property_getter_ident
, property_setter_ident
);
32032 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32035 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32037 objc-synthesize-declaration:
32038 @synthesize objc-synthesize-identifier-list ;
32040 objc-synthesize-identifier-list:
32041 objc-synthesize-identifier
32042 objc-synthesize-identifier-list, objc-synthesize-identifier
32044 objc-synthesize-identifier
32046 identifier = identifier
32049 @synthesize MyProperty;
32050 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32052 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32053 for C. Keep them in sync.
32056 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
32058 tree list
= NULL_TREE
;
32060 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32062 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
32065 tree property
, ivar
;
32066 property
= cp_parser_identifier (parser
);
32067 if (property
== error_mark_node
)
32069 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
32074 cp_lexer_consume_token (parser
->lexer
);
32075 ivar
= cp_parser_identifier (parser
);
32076 if (ivar
== error_mark_node
)
32078 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32084 list
= chainon (list
, build_tree_list (ivar
, property
));
32085 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32086 cp_lexer_consume_token (parser
->lexer
);
32090 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32091 objc_add_synthesize_declaration (loc
, list
);
32094 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32096 objc-dynamic-declaration:
32097 @dynamic identifier-list ;
32100 @dynamic MyProperty;
32101 @dynamic MyProperty, AnotherProperty;
32103 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32104 for C. Keep them in sync.
32107 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
32109 tree list
= NULL_TREE
;
32111 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32113 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
32117 property
= cp_parser_identifier (parser
);
32118 if (property
== error_mark_node
)
32120 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32123 list
= chainon (list
, build_tree_list (NULL
, property
));
32124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32125 cp_lexer_consume_token (parser
->lexer
);
32129 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32130 objc_add_dynamic_declaration (loc
, list
);
32134 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32136 /* Returns name of the next clause.
32137 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32138 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32139 returned and the token is consumed. */
32141 static pragma_omp_clause
32142 cp_parser_omp_clause_name (cp_parser
*parser
)
32144 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
32146 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32147 result
= PRAGMA_OACC_CLAUSE_AUTO
;
32148 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
32149 result
= PRAGMA_OMP_CLAUSE_IF
;
32150 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
32151 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
32152 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
32153 result
= PRAGMA_OACC_CLAUSE_DELETE
;
32154 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
32155 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
32156 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
32157 result
= PRAGMA_OMP_CLAUSE_FOR
;
32158 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32160 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32161 const char *p
= IDENTIFIER_POINTER (id
);
32166 if (!strcmp ("aligned", p
))
32167 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
32168 else if (!strcmp ("async", p
))
32169 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
32172 if (!strcmp ("collapse", p
))
32173 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
32174 else if (!strcmp ("copy", p
))
32175 result
= PRAGMA_OACC_CLAUSE_COPY
;
32176 else if (!strcmp ("copyin", p
))
32177 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
32178 else if (!strcmp ("copyout", p
))
32179 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32180 else if (!strcmp ("copyprivate", p
))
32181 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
32182 else if (!strcmp ("create", p
))
32183 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32186 if (!strcmp ("defaultmap", p
))
32187 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
32188 else if (!strcmp ("depend", p
))
32189 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
32190 else if (!strcmp ("device", p
))
32191 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
32192 else if (!strcmp ("deviceptr", p
))
32193 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
32194 else if (!strcmp ("device_resident", p
))
32195 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
32196 else if (!strcmp ("dist_schedule", p
))
32197 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
32200 if (!strcmp ("final", p
))
32201 result
= PRAGMA_OMP_CLAUSE_FINAL
;
32202 else if (!strcmp ("finalize", p
))
32203 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
32204 else if (!strcmp ("firstprivate", p
))
32205 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
32206 else if (!strcmp ("from", p
))
32207 result
= PRAGMA_OMP_CLAUSE_FROM
;
32210 if (!strcmp ("gang", p
))
32211 result
= PRAGMA_OACC_CLAUSE_GANG
;
32212 else if (!strcmp ("grainsize", p
))
32213 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
32216 if (!strcmp ("hint", p
))
32217 result
= PRAGMA_OMP_CLAUSE_HINT
;
32218 else if (!strcmp ("host", p
))
32219 result
= PRAGMA_OACC_CLAUSE_HOST
;
32222 if (!strcmp ("if_present", p
))
32223 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
32224 else if (!strcmp ("in_reduction", p
))
32225 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
32226 else if (!strcmp ("inbranch", p
))
32227 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
32228 else if (!strcmp ("independent", p
))
32229 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
32230 else if (!strcmp ("is_device_ptr", p
))
32231 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
32234 if (!strcmp ("lastprivate", p
))
32235 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
32236 else if (!strcmp ("linear", p
))
32237 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
32238 else if (!strcmp ("link", p
))
32239 result
= PRAGMA_OMP_CLAUSE_LINK
;
32242 if (!strcmp ("map", p
))
32243 result
= PRAGMA_OMP_CLAUSE_MAP
;
32244 else if (!strcmp ("mergeable", p
))
32245 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
32248 if (!strcmp ("nogroup", p
))
32249 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
32250 else if (!strcmp ("nontemporal", p
))
32251 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
32252 else if (!strcmp ("notinbranch", p
))
32253 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
32254 else if (!strcmp ("nowait", p
))
32255 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
32256 else if (!strcmp ("num_gangs", p
))
32257 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
32258 else if (!strcmp ("num_tasks", p
))
32259 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
32260 else if (!strcmp ("num_teams", p
))
32261 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
32262 else if (!strcmp ("num_threads", p
))
32263 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
32264 else if (!strcmp ("num_workers", p
))
32265 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
32268 if (!strcmp ("ordered", p
))
32269 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
32272 if (!strcmp ("parallel", p
))
32273 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
32274 else if (!strcmp ("present", p
))
32275 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
32276 else if (!strcmp ("present_or_copy", p
)
32277 || !strcmp ("pcopy", p
))
32278 result
= PRAGMA_OACC_CLAUSE_COPY
;
32279 else if (!strcmp ("present_or_copyin", p
)
32280 || !strcmp ("pcopyin", p
))
32281 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
32282 else if (!strcmp ("present_or_copyout", p
)
32283 || !strcmp ("pcopyout", p
))
32284 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32285 else if (!strcmp ("present_or_create", p
)
32286 || !strcmp ("pcreate", p
))
32287 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32288 else if (!strcmp ("priority", p
))
32289 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
32290 else if (!strcmp ("proc_bind", p
))
32291 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
32294 if (!strcmp ("reduction", p
))
32295 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
32298 if (!strcmp ("safelen", p
))
32299 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
32300 else if (!strcmp ("schedule", p
))
32301 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
32302 else if (!strcmp ("sections", p
))
32303 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
32304 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
32305 result
= PRAGMA_OACC_CLAUSE_HOST
;
32306 else if (!strcmp ("seq", p
))
32307 result
= PRAGMA_OACC_CLAUSE_SEQ
;
32308 else if (!strcmp ("shared", p
))
32309 result
= PRAGMA_OMP_CLAUSE_SHARED
;
32310 else if (!strcmp ("simd", p
))
32311 result
= PRAGMA_OMP_CLAUSE_SIMD
;
32312 else if (!strcmp ("simdlen", p
))
32313 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
32316 if (!strcmp ("task_reduction", p
))
32317 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
32318 else if (!strcmp ("taskgroup", p
))
32319 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
32320 else if (!strcmp ("thread_limit", p
))
32321 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
32322 else if (!strcmp ("threads", p
))
32323 result
= PRAGMA_OMP_CLAUSE_THREADS
;
32324 else if (!strcmp ("tile", p
))
32325 result
= PRAGMA_OACC_CLAUSE_TILE
;
32326 else if (!strcmp ("to", p
))
32327 result
= PRAGMA_OMP_CLAUSE_TO
;
32330 if (!strcmp ("uniform", p
))
32331 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
32332 else if (!strcmp ("untied", p
))
32333 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
32334 else if (!strcmp ("use_device", p
))
32335 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
32336 else if (!strcmp ("use_device_ptr", p
))
32337 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
32340 if (!strcmp ("vector", p
))
32341 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
32342 else if (!strcmp ("vector_length", p
))
32343 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
32346 if (!strcmp ("wait", p
))
32347 result
= PRAGMA_OACC_CLAUSE_WAIT
;
32348 else if (!strcmp ("worker", p
))
32349 result
= PRAGMA_OACC_CLAUSE_WORKER
;
32354 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
32355 cp_lexer_consume_token (parser
->lexer
);
32360 /* Validate that a clause of the given type does not already exist. */
32363 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
32364 const char *name
, location_t location
)
32368 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32369 if (OMP_CLAUSE_CODE (c
) == code
)
32371 error_at (location
, "too many %qs clauses", name
);
32379 variable-list , identifier
32381 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32382 colon). An opening parenthesis will have been consumed by the caller.
32384 If KIND is nonzero, create the appropriate node and install the decl
32385 in OMP_CLAUSE_DECL and add the node to the head of the list.
32387 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32388 return the list created.
32390 COLON can be NULL if only closing parenthesis should end the list,
32391 or pointer to bool which will receive false if the list is terminated
32392 by closing parenthesis or true if the list is terminated by colon. */
32395 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
32396 tree list
, bool *colon
)
32399 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32402 parser
->colon_corrects_to_scope_p
= false;
32409 if (kind
== OMP_CLAUSE_DEPEND
)
32410 cp_parser_parse_tentatively (parser
);
32411 token
= cp_lexer_peek_token (parser
->lexer
);
32413 && current_class_ptr
32414 && cp_parser_is_keyword (token
, RID_THIS
))
32416 decl
= finish_this_expr ();
32417 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
32418 || CONVERT_EXPR_P (decl
))
32419 decl
= TREE_OPERAND (decl
, 0);
32420 cp_lexer_consume_token (parser
->lexer
);
32424 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32425 /*check_dependency_p=*/true,
32426 /*template_p=*/NULL
,
32427 /*declarator_p=*/false,
32428 /*optional_p=*/false);
32429 if (name
== error_mark_node
)
32431 if (kind
== OMP_CLAUSE_DEPEND
32432 && cp_parser_simulate_error (parser
))
32433 goto depend_lvalue
;
32437 if (identifier_p (name
))
32438 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32441 if (decl
== error_mark_node
)
32443 if (kind
== OMP_CLAUSE_DEPEND
32444 && cp_parser_simulate_error (parser
))
32445 goto depend_lvalue
;
32446 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32450 if (decl
== error_mark_node
)
32452 else if (kind
!= 0)
32456 case OMP_CLAUSE__CACHE_
:
32457 /* The OpenACC cache directive explicitly only allows "array
32458 elements or subarrays". */
32459 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32461 error_at (token
->location
, "expected %<[%>");
32462 decl
= error_mark_node
;
32466 case OMP_CLAUSE_MAP
:
32467 case OMP_CLAUSE_FROM
:
32468 case OMP_CLAUSE_TO
:
32469 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32472 = cp_lexer_peek_token (parser
->lexer
)->location
;
32473 cp_id_kind idk
= CP_ID_KIND_NONE
;
32474 cp_lexer_consume_token (parser
->lexer
);
32475 decl
= convert_from_reference (decl
);
32477 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32482 case OMP_CLAUSE_DEPEND
:
32483 case OMP_CLAUSE_REDUCTION
:
32484 case OMP_CLAUSE_IN_REDUCTION
:
32485 case OMP_CLAUSE_TASK_REDUCTION
:
32486 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32488 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32490 parser
->colon_corrects_to_scope_p
= false;
32491 cp_lexer_consume_token (parser
->lexer
);
32492 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32493 low_bound
= cp_parser_expression (parser
);
32495 parser
->colon_corrects_to_scope_p
32496 = saved_colon_corrects_to_scope_p
;
32497 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32498 length
= integer_one_node
;
32501 /* Look for `:'. */
32502 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32504 if (kind
== OMP_CLAUSE_DEPEND
32505 && cp_parser_simulate_error (parser
))
32506 goto depend_lvalue
;
32509 if (kind
== OMP_CLAUSE_DEPEND
)
32510 cp_parser_commit_to_tentative_parse (parser
);
32511 if (!cp_lexer_next_token_is (parser
->lexer
,
32513 length
= cp_parser_expression (parser
);
32515 /* Look for the closing `]'. */
32516 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32519 if (kind
== OMP_CLAUSE_DEPEND
32520 && cp_parser_simulate_error (parser
))
32521 goto depend_lvalue
;
32525 decl
= tree_cons (low_bound
, length
, decl
);
32532 if (kind
== OMP_CLAUSE_DEPEND
)
32534 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32535 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32536 && cp_parser_simulate_error (parser
))
32539 cp_parser_abort_tentative_parse (parser
);
32540 decl
= cp_parser_assignment_expression (parser
, NULL
,
32544 cp_parser_parse_definitely (parser
);
32547 tree u
= build_omp_clause (token
->location
, kind
);
32548 OMP_CLAUSE_DECL (u
) = decl
;
32549 OMP_CLAUSE_CHAIN (u
) = list
;
32553 list
= tree_cons (decl
, NULL_TREE
, list
);
32556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32558 cp_lexer_consume_token (parser
->lexer
);
32562 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32564 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32567 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32571 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32575 /* Try to resync to an unnested comma. Copied from
32576 cp_parser_parenthesized_expression_list. */
32579 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32580 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32581 /*recovering=*/true,
32583 /*consume_paren=*/true);
32591 /* Similarly, but expect leading and trailing parenthesis. This is a very
32592 common case for omp clauses. */
32595 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32597 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32598 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32603 copy ( variable-list )
32604 copyin ( variable-list )
32605 copyout ( variable-list )
32606 create ( variable-list )
32607 delete ( variable-list )
32608 present ( variable-list ) */
32611 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32614 enum gomp_map_kind kind
;
32617 case PRAGMA_OACC_CLAUSE_COPY
:
32618 kind
= GOMP_MAP_TOFROM
;
32620 case PRAGMA_OACC_CLAUSE_COPYIN
:
32621 kind
= GOMP_MAP_TO
;
32623 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32624 kind
= GOMP_MAP_FROM
;
32626 case PRAGMA_OACC_CLAUSE_CREATE
:
32627 kind
= GOMP_MAP_ALLOC
;
32629 case PRAGMA_OACC_CLAUSE_DELETE
:
32630 kind
= GOMP_MAP_RELEASE
;
32632 case PRAGMA_OACC_CLAUSE_DEVICE
:
32633 kind
= GOMP_MAP_FORCE_TO
;
32635 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32636 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32638 case PRAGMA_OACC_CLAUSE_HOST
:
32639 kind
= GOMP_MAP_FORCE_FROM
;
32641 case PRAGMA_OACC_CLAUSE_LINK
:
32642 kind
= GOMP_MAP_LINK
;
32644 case PRAGMA_OACC_CLAUSE_PRESENT
:
32645 kind
= GOMP_MAP_FORCE_PRESENT
;
32648 gcc_unreachable ();
32651 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32653 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32654 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32660 deviceptr ( variable-list ) */
32663 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32665 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32668 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32669 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32670 variable-list must only allow for pointer variables. */
32671 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32672 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32674 tree v
= TREE_PURPOSE (t
);
32675 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32676 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32677 OMP_CLAUSE_DECL (u
) = v
;
32678 OMP_CLAUSE_CHAIN (u
) = list
;
32693 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
32696 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
32698 tree c
= build_omp_clause (loc
, code
);
32699 OMP_CLAUSE_CHAIN (c
) = list
;
32705 num_gangs ( expression )
32706 num_workers ( expression )
32707 vector_length ( expression ) */
32710 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32711 const char *str
, tree list
)
32713 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32715 matching_parens parens
;
32716 if (!parens
.require_open (parser
))
32719 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32721 if (t
== error_mark_node
32722 || !parens
.require_close (parser
))
32724 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32725 /*or_comma=*/false,
32726 /*consume_paren=*/true);
32730 check_no_duplicate_clause (list
, code
, str
, loc
);
32732 tree c
= build_omp_clause (loc
, code
);
32733 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32734 OMP_CLAUSE_CHAIN (c
) = list
;
32740 gang [( gang-arg-list )]
32741 worker [( [num:] int-expr )]
32742 vector [( [length:] int-expr )]
32744 where gang-arg is one of:
32749 and size-expr may be:
32756 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
32757 omp_clause_code kind
,
32758 const char *str
, tree list
)
32760 const char *id
= "num";
32761 cp_lexer
*lexer
= parser
->lexer
;
32762 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32764 if (kind
== OMP_CLAUSE_VECTOR
)
32767 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32769 matching_parens parens
;
32770 parens
.consume_open (parser
);
32774 cp_token
*next
= cp_lexer_peek_token (lexer
);
32777 /* Gang static argument. */
32778 if (kind
== OMP_CLAUSE_GANG
32779 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32781 cp_lexer_consume_token (lexer
);
32783 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32784 goto cleanup_error
;
32787 if (ops
[idx
] != NULL
)
32789 cp_parser_error (parser
, "too many %<static%> arguments");
32790 goto cleanup_error
;
32793 /* Check for the '*' argument. */
32794 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32795 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32796 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32799 cp_lexer_consume_token (lexer
);
32800 ops
[idx
] = integer_minus_one_node
;
32802 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32804 cp_lexer_consume_token (lexer
);
32810 /* Worker num: argument and vector length: arguments. */
32811 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32812 && id_equal (next
->u
.value
, id
)
32813 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32815 cp_lexer_consume_token (lexer
); /* id */
32816 cp_lexer_consume_token (lexer
); /* ':' */
32819 /* Now collect the actual argument. */
32820 if (ops
[idx
] != NULL_TREE
)
32822 cp_parser_error (parser
, "unexpected argument");
32823 goto cleanup_error
;
32826 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32828 if (expr
== error_mark_node
)
32829 goto cleanup_error
;
32831 mark_exp_read (expr
);
32834 if (kind
== OMP_CLAUSE_GANG
32835 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32837 cp_lexer_consume_token (lexer
);
32844 if (!parens
.require_close (parser
))
32845 goto cleanup_error
;
32848 check_no_duplicate_clause (list
, kind
, str
, loc
);
32850 c
= build_omp_clause (loc
, kind
);
32853 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32855 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32856 OMP_CLAUSE_CHAIN (c
) = list
;
32861 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32866 tile ( size-expr-list ) */
32869 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32871 tree c
, expr
= error_mark_node
;
32872 tree tile
= NULL_TREE
;
32874 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32875 so, but the spec authors never considered such a case and have
32876 differing opinions on what it might mean, including 'not
32878 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32879 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32882 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32887 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32891 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32892 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32894 cp_lexer_consume_token (parser
->lexer
);
32895 expr
= integer_zero_node
;
32898 expr
= cp_parser_constant_expression (parser
);
32900 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32902 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32904 /* Consume the trailing ')'. */
32905 cp_lexer_consume_token (parser
->lexer
);
32907 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32908 tile
= nreverse (tile
);
32909 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32910 OMP_CLAUSE_CHAIN (c
) = list
;
32915 Parse wait clause or directive parameters. */
32918 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32920 vec
<tree
, va_gc
> *args
;
32923 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32925 /*allow_expansion_p=*/true,
32926 /*non_constant_p=*/NULL
);
32928 if (args
== NULL
|| args
->length () == 0)
32932 cp_parser_error (parser
, "expected integer expression list");
32933 release_tree_vector (args
);
32938 args_tree
= build_tree_list_vec (args
);
32940 release_tree_vector (args
);
32942 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32944 tree targ
= TREE_VALUE (t
);
32946 if (targ
!= error_mark_node
)
32948 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32949 error ("%<wait%> expression must be integral");
32952 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32954 targ
= mark_rvalue_use (targ
);
32955 OMP_CLAUSE_DECL (c
) = targ
;
32956 OMP_CLAUSE_CHAIN (c
) = list
;
32966 wait [( int-expr-list )] */
32969 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32971 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32973 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32974 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32977 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
32979 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
32980 OMP_CLAUSE_CHAIN (c
) = list
;
32988 collapse ( constant-expression ) */
32991 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32997 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32998 matching_parens parens
;
32999 if (!parens
.require_open (parser
))
33002 num
= cp_parser_constant_expression (parser
);
33004 if (!parens
.require_close (parser
))
33005 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33006 /*or_comma=*/false,
33007 /*consume_paren=*/true);
33009 if (num
== error_mark_node
)
33011 num
= fold_non_dependent_expr (num
);
33012 if (!tree_fits_shwi_p (num
)
33013 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33014 || (n
= tree_to_shwi (num
)) <= 0
33017 error_at (loc
, "collapse argument needs positive constant integer expression");
33021 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
33022 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
33023 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
33024 OMP_CLAUSE_CHAIN (c
) = list
;
33025 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
33031 default ( none | shared )
33034 default ( none | present ) */
33037 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
33038 location_t location
, bool is_oacc
)
33040 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
33043 matching_parens parens
;
33044 if (!parens
.require_open (parser
))
33046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33048 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33049 const char *p
= IDENTIFIER_POINTER (id
);
33054 if (strcmp ("none", p
) != 0)
33056 kind
= OMP_CLAUSE_DEFAULT_NONE
;
33060 if (strcmp ("present", p
) != 0 || !is_oacc
)
33062 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
33066 if (strcmp ("shared", p
) != 0 || is_oacc
)
33068 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
33075 cp_lexer_consume_token (parser
->lexer
);
33081 cp_parser_error (parser
, "expected %<none%> or %<present%>");
33083 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
33086 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
33087 || !parens
.require_close (parser
))
33088 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33089 /*or_comma=*/false,
33090 /*consume_paren=*/true);
33092 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
33095 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
33096 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
33097 OMP_CLAUSE_CHAIN (c
) = list
;
33098 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
33104 final ( expression ) */
33107 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
33111 matching_parens parens
;
33112 if (!parens
.require_open (parser
))
33115 t
= cp_parser_assignment_expression (parser
);
33117 if (t
== error_mark_node
33118 || !parens
.require_close (parser
))
33119 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33120 /*or_comma=*/false,
33121 /*consume_paren=*/true);
33123 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
33125 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
33126 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
33127 OMP_CLAUSE_CHAIN (c
) = list
;
33136 if ( directive-name-modifier : expression )
33138 directive-name-modifier:
33139 parallel | task | taskloop | target data | target | target update
33140 | target enter data | target exit data
33143 directive-name-modifier:
33144 ... | simd | cancel */
33147 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
33151 enum tree_code if_modifier
= ERROR_MARK
;
33153 matching_parens parens
;
33154 if (!parens
.require_open (parser
))
33157 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33159 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33160 const char *p
= IDENTIFIER_POINTER (id
);
33163 if (strcmp ("cancel", p
) == 0)
33164 if_modifier
= VOID_CST
;
33165 else if (strcmp ("parallel", p
) == 0)
33166 if_modifier
= OMP_PARALLEL
;
33167 else if (strcmp ("simd", p
) == 0)
33168 if_modifier
= OMP_SIMD
;
33169 else if (strcmp ("task", p
) == 0)
33170 if_modifier
= OMP_TASK
;
33171 else if (strcmp ("taskloop", p
) == 0)
33172 if_modifier
= OMP_TASKLOOP
;
33173 else if (strcmp ("target", p
) == 0)
33175 if_modifier
= OMP_TARGET
;
33176 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
33178 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
33179 p
= IDENTIFIER_POINTER (id
);
33180 if (strcmp ("data", p
) == 0)
33181 if_modifier
= OMP_TARGET_DATA
;
33182 else if (strcmp ("update", p
) == 0)
33183 if_modifier
= OMP_TARGET_UPDATE
;
33184 else if (strcmp ("enter", p
) == 0)
33185 if_modifier
= OMP_TARGET_ENTER_DATA
;
33186 else if (strcmp ("exit", p
) == 0)
33187 if_modifier
= OMP_TARGET_EXIT_DATA
;
33188 if (if_modifier
!= OMP_TARGET
)
33193 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
33194 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
33196 if_modifier
= ERROR_MARK
;
33198 if (if_modifier
== OMP_TARGET_ENTER_DATA
33199 || if_modifier
== OMP_TARGET_EXIT_DATA
)
33201 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
33203 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
33204 p
= IDENTIFIER_POINTER (id
);
33205 if (strcmp ("data", p
) == 0)
33211 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
33212 error_at (loc
, "expected %<data%>");
33213 if_modifier
= ERROR_MARK
;
33218 if (if_modifier
!= ERROR_MARK
)
33220 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
33223 cp_lexer_consume_token (parser
->lexer
);
33230 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
33231 error_at (loc
, "expected %<:%>");
33233 if_modifier
= ERROR_MARK
;
33238 t
= cp_parser_assignment_expression (parser
);
33240 if (t
== error_mark_node
33241 || !parens
.require_close (parser
))
33242 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33243 /*or_comma=*/false,
33244 /*consume_paren=*/true);
33246 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33247 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
33249 if (if_modifier
!= ERROR_MARK
33250 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33252 const char *p
= NULL
;
33253 switch (if_modifier
)
33255 case VOID_CST
: p
= "cancel"; break;
33256 case OMP_PARALLEL
: p
= "parallel"; break;
33257 case OMP_SIMD
: p
= "simd"; break;
33258 case OMP_TASK
: p
= "task"; break;
33259 case OMP_TASKLOOP
: p
= "taskloop"; break;
33260 case OMP_TARGET_DATA
: p
= "target data"; break;
33261 case OMP_TARGET
: p
= "target"; break;
33262 case OMP_TARGET_UPDATE
: p
= "target update"; break;
33263 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
33264 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
33265 default: gcc_unreachable ();
33267 error_at (location
, "too many %<if%> clauses with %qs modifier",
33271 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33274 error_at (location
, "too many %<if%> clauses");
33276 error_at (location
, "too many %<if%> clauses without modifier");
33279 else if (if_modifier
== ERROR_MARK
33280 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
33282 error_at (location
, "if any %<if%> clause has modifier, then all "
33283 "%<if%> clauses have to use modifier");
33288 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
33289 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
33290 OMP_CLAUSE_IF_EXPR (c
) = t
;
33291 OMP_CLAUSE_CHAIN (c
) = list
;
33300 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
33301 tree list
, location_t location
)
33305 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
33308 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
33309 OMP_CLAUSE_CHAIN (c
) = list
;
33317 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
33318 tree list
, location_t location
)
33322 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
33324 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
33325 OMP_CLAUSE_CHAIN (c
) = list
;
33330 num_threads ( expression ) */
33333 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
33334 location_t location
)
33338 matching_parens parens
;
33339 if (!parens
.require_open (parser
))
33342 t
= cp_parser_assignment_expression (parser
);
33344 if (t
== error_mark_node
33345 || !parens
.require_close (parser
))
33346 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33347 /*or_comma=*/false,
33348 /*consume_paren=*/true);
33350 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
33351 "num_threads", location
);
33353 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
33354 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
33355 OMP_CLAUSE_CHAIN (c
) = list
;
33361 num_tasks ( expression ) */
33364 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
33365 location_t location
)
33369 matching_parens parens
;
33370 if (!parens
.require_open (parser
))
33373 t
= cp_parser_assignment_expression (parser
);
33375 if (t
== error_mark_node
33376 || !parens
.require_close (parser
))
33377 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33378 /*or_comma=*/false,
33379 /*consume_paren=*/true);
33381 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
33382 "num_tasks", location
);
33384 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
33385 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
33386 OMP_CLAUSE_CHAIN (c
) = list
;
33392 grainsize ( expression ) */
33395 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
33396 location_t location
)
33400 matching_parens parens
;
33401 if (!parens
.require_open (parser
))
33404 t
= cp_parser_assignment_expression (parser
);
33406 if (t
== error_mark_node
33407 || !parens
.require_close (parser
))
33408 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33409 /*or_comma=*/false,
33410 /*consume_paren=*/true);
33412 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
33413 "grainsize", location
);
33415 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
33416 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
33417 OMP_CLAUSE_CHAIN (c
) = list
;
33423 priority ( expression ) */
33426 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
33427 location_t location
)
33431 matching_parens parens
;
33432 if (!parens
.require_open (parser
))
33435 t
= cp_parser_assignment_expression (parser
);
33437 if (t
== error_mark_node
33438 || !parens
.require_close (parser
))
33439 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33440 /*or_comma=*/false,
33441 /*consume_paren=*/true);
33443 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
33444 "priority", location
);
33446 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
33447 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
33448 OMP_CLAUSE_CHAIN (c
) = list
;
33454 hint ( expression ) */
33457 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33461 matching_parens parens
;
33462 if (!parens
.require_open (parser
))
33465 t
= cp_parser_assignment_expression (parser
);
33467 if (t
== error_mark_node
33468 || !parens
.require_close (parser
))
33469 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33470 /*or_comma=*/false,
33471 /*consume_paren=*/true);
33473 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33475 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33476 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33477 OMP_CLAUSE_CHAIN (c
) = list
;
33483 defaultmap ( tofrom : scalar )
33486 defaultmap ( implicit-behavior [ : variable-category ] ) */
33489 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33490 location_t location
)
33494 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33495 enum omp_clause_defaultmap_kind category
33496 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33498 matching_parens parens
;
33499 if (!parens
.require_open (parser
))
33502 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33504 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33507 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33508 "%<tofrom%>, %<firstprivate%>, %<none%> "
33514 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33515 p
= IDENTIFIER_POINTER (id
);
33521 if (strcmp ("alloc", p
) == 0)
33522 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33524 goto invalid_behavior
;
33528 if (strcmp ("default", p
) == 0)
33529 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33531 goto invalid_behavior
;
33535 if (strcmp ("firstprivate", p
) == 0)
33536 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33537 else if (strcmp ("from", p
) == 0)
33538 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33540 goto invalid_behavior
;
33544 if (strcmp ("none", p
) == 0)
33545 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33547 goto invalid_behavior
;
33551 if (strcmp ("tofrom", p
) == 0)
33552 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33553 else if (strcmp ("to", p
) == 0)
33554 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33556 goto invalid_behavior
;
33560 goto invalid_behavior
;
33562 cp_lexer_consume_token (parser
->lexer
);
33564 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33566 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33569 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33572 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33576 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33577 p
= IDENTIFIER_POINTER (id
);
33582 if (strcmp ("aggregate", p
) == 0)
33583 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33585 goto invalid_category
;
33589 if (strcmp ("pointer", p
) == 0)
33590 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33592 goto invalid_category
;
33596 if (strcmp ("scalar", p
) == 0)
33597 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33599 goto invalid_category
;
33603 goto invalid_category
;
33606 cp_lexer_consume_token (parser
->lexer
);
33608 if (!parens
.require_close (parser
))
33611 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33612 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33613 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33614 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33615 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33616 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33618 enum omp_clause_defaultmap_kind cat
= category
;
33619 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33620 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33621 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33625 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33628 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33631 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33634 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33638 gcc_unreachable ();
33641 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33644 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33649 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33650 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33651 OMP_CLAUSE_CHAIN (c
) = list
;
33655 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33656 /*or_comma=*/false,
33657 /*consume_paren=*/true);
33665 ordered ( constant-expression ) */
33668 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33669 tree list
, location_t location
)
33671 tree c
, num
= NULL_TREE
;
33674 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33675 "ordered", location
);
33677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33679 matching_parens parens
;
33680 parens
.consume_open (parser
);
33682 num
= cp_parser_constant_expression (parser
);
33684 if (!parens
.require_close (parser
))
33685 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33686 /*or_comma=*/false,
33687 /*consume_paren=*/true);
33689 if (num
== error_mark_node
)
33691 num
= fold_non_dependent_expr (num
);
33692 if (!tree_fits_shwi_p (num
)
33693 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33694 || (n
= tree_to_shwi (num
)) <= 0
33697 error_at (location
,
33698 "ordered argument needs positive constant integer "
33704 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33705 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33706 OMP_CLAUSE_CHAIN (c
) = list
;
33711 reduction ( reduction-operator : variable-list )
33713 reduction-operator:
33714 One of: + * - & ^ | && ||
33718 reduction-operator:
33719 One of: + * - & ^ | && || min max
33723 reduction-operator:
33724 One of: + * - & ^ | && ||
33728 reduction ( reduction-modifier, reduction-operator : variable-list )
33729 in_reduction ( reduction-operator : variable-list )
33730 task_reduction ( reduction-operator : variable-list ) */
33733 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33734 bool is_omp
, tree list
)
33736 enum tree_code code
= ERROR_MARK
;
33737 tree nlist
, c
, id
= NULL_TREE
;
33739 bool inscan
= false;
33741 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33744 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33746 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33747 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33749 cp_lexer_consume_token (parser
->lexer
);
33750 cp_lexer_consume_token (parser
->lexer
);
33752 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33753 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33755 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33756 const char *p
= IDENTIFIER_POINTER (id
);
33757 if (strcmp (p
, "task") == 0)
33759 else if (strcmp (p
, "inscan") == 0)
33762 sorry ("%<inscan%> modifier on %<reduction%> clause "
33763 "not supported yet");
33765 if (task
|| inscan
)
33767 cp_lexer_consume_token (parser
->lexer
);
33768 cp_lexer_consume_token (parser
->lexer
);
33773 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33775 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33776 case CPP_MULT
: code
= MULT_EXPR
; break;
33777 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33778 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33779 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33780 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33781 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33782 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33786 if (code
!= ERROR_MARK
)
33787 cp_lexer_consume_token (parser
->lexer
);
33790 bool saved_colon_corrects_to_scope_p
;
33791 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33792 parser
->colon_corrects_to_scope_p
= false;
33793 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33794 /*check_dependency_p=*/true,
33795 /*template_p=*/NULL
,
33796 /*declarator_p=*/false,
33797 /*optional_p=*/false);
33798 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33799 if (identifier_p (id
))
33801 const char *p
= IDENTIFIER_POINTER (id
);
33803 if (strcmp (p
, "min") == 0)
33805 else if (strcmp (p
, "max") == 0)
33807 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33809 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33811 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33813 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33814 code
= BIT_AND_EXPR
;
33815 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33816 code
= BIT_IOR_EXPR
;
33817 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33818 code
= BIT_XOR_EXPR
;
33819 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33820 code
= TRUTH_ANDIF_EXPR
;
33821 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33822 code
= TRUTH_ORIF_EXPR
;
33823 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33824 tree scope
= parser
->scope
;
33826 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33827 parser
->scope
= NULL_TREE
;
33828 parser
->qualifying_scope
= NULL_TREE
;
33829 parser
->object_scope
= NULL_TREE
;
33833 error ("invalid reduction-identifier");
33835 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33836 /*or_comma=*/false,
33837 /*consume_paren=*/true);
33842 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33845 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33847 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33849 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33851 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33853 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33854 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33861 schedule ( schedule-kind )
33862 schedule ( schedule-kind , expression )
33865 static | dynamic | guided | runtime | auto
33868 schedule ( schedule-modifier : schedule-kind )
33869 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33877 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33880 int modifiers
= 0, nmodifiers
= 0;
33882 matching_parens parens
;
33883 if (!parens
.require_open (parser
))
33886 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33888 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33890 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33891 const char *p
= IDENTIFIER_POINTER (id
);
33892 if (strcmp ("simd", p
) == 0)
33893 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33894 else if (strcmp ("monotonic", p
) == 0)
33895 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33896 else if (strcmp ("nonmonotonic", p
) == 0)
33897 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33900 cp_lexer_consume_token (parser
->lexer
);
33901 if (nmodifiers
++ == 0
33902 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33903 cp_lexer_consume_token (parser
->lexer
);
33906 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33913 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33914 const char *p
= IDENTIFIER_POINTER (id
);
33919 if (strcmp ("dynamic", p
) != 0)
33921 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33925 if (strcmp ("guided", p
) != 0)
33927 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33931 if (strcmp ("runtime", p
) != 0)
33933 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33940 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33941 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33942 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33943 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33946 cp_lexer_consume_token (parser
->lexer
);
33948 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33949 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33950 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33951 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33953 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33961 cp_lexer_consume_token (parser
->lexer
);
33963 token
= cp_lexer_peek_token (parser
->lexer
);
33964 t
= cp_parser_assignment_expression (parser
);
33966 if (t
== error_mark_node
)
33968 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
33969 error_at (token
->location
, "schedule %<runtime%> does not take "
33970 "a %<chunk_size%> parameter");
33971 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
33972 error_at (token
->location
, "schedule %<auto%> does not take "
33973 "a %<chunk_size%> parameter");
33975 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
33977 if (!parens
.require_close (parser
))
33980 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33983 OMP_CLAUSE_SCHEDULE_KIND (c
)
33984 = (enum omp_clause_schedule_kind
)
33985 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
33987 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
33988 OMP_CLAUSE_CHAIN (c
) = list
;
33992 cp_parser_error (parser
, "invalid schedule kind");
33994 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33995 /*or_comma=*/false,
33996 /*consume_paren=*/true);
34004 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
34005 tree list
, location_t location
)
34009 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
34011 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
34012 OMP_CLAUSE_CHAIN (c
) = list
;
34021 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
34022 tree list
, location_t location
)
34024 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
34025 tree c
= build_omp_clause (location
, code
);
34026 OMP_CLAUSE_CHAIN (c
) = list
;
34037 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
34038 enum omp_clause_code code
,
34039 tree list
, location_t location
)
34041 tree c
= build_omp_clause (location
, code
);
34042 OMP_CLAUSE_CHAIN (c
) = list
;
34050 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
34051 tree list
, location_t location
)
34053 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
34054 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
34055 OMP_CLAUSE_CHAIN (c
) = list
;
34064 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
34065 enum omp_clause_code code
,
34066 tree list
, location_t location
)
34068 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
34069 tree c
= build_omp_clause (location
, code
);
34070 OMP_CLAUSE_CHAIN (c
) = list
;
34075 num_teams ( expression ) */
34078 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
34079 location_t location
)
34083 matching_parens parens
;
34084 if (!parens
.require_open (parser
))
34087 t
= cp_parser_assignment_expression (parser
);
34089 if (t
== error_mark_node
34090 || !parens
.require_close (parser
))
34091 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34092 /*or_comma=*/false,
34093 /*consume_paren=*/true);
34095 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
34096 "num_teams", location
);
34098 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
34099 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
34100 OMP_CLAUSE_CHAIN (c
) = list
;
34106 thread_limit ( expression ) */
34109 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
34110 location_t location
)
34114 matching_parens parens
;
34115 if (!parens
.require_open (parser
))
34118 t
= cp_parser_assignment_expression (parser
);
34120 if (t
== error_mark_node
34121 || !parens
.require_close (parser
))
34122 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34123 /*or_comma=*/false,
34124 /*consume_paren=*/true);
34126 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
34127 "thread_limit", location
);
34129 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
34130 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
34131 OMP_CLAUSE_CHAIN (c
) = list
;
34137 aligned ( variable-list )
34138 aligned ( variable-list : constant-expression ) */
34141 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
34143 tree nlist
, c
, alignment
= NULL_TREE
;
34146 matching_parens parens
;
34147 if (!parens
.require_open (parser
))
34150 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
34155 alignment
= cp_parser_constant_expression (parser
);
34157 if (!parens
.require_close (parser
))
34158 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34159 /*or_comma=*/false,
34160 /*consume_paren=*/true);
34162 if (alignment
== error_mark_node
)
34163 alignment
= NULL_TREE
;
34166 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34167 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
34173 lastprivate ( variable-list )
34176 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34179 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
34181 bool conditional
= false;
34183 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34187 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
34189 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34190 const char *p
= IDENTIFIER_POINTER (id
);
34192 if (strcmp ("conditional", p
) == 0)
34194 conditional
= true;
34195 cp_lexer_consume_token (parser
->lexer
);
34196 cp_lexer_consume_token (parser
->lexer
);
34200 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
34204 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34205 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
34210 linear ( variable-list )
34211 linear ( variable-list : expression )
34214 linear ( modifier ( variable-list ) )
34215 linear ( modifier ( variable-list ) : expression ) */
34218 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
34221 tree nlist
, c
, step
= integer_one_node
;
34223 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34225 matching_parens parens
;
34226 if (!parens
.require_open (parser
))
34229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34231 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34232 const char *p
= IDENTIFIER_POINTER (id
);
34234 if (strcmp ("ref", p
) == 0)
34235 kind
= OMP_CLAUSE_LINEAR_REF
;
34236 else if (strcmp ("val", p
) == 0)
34237 kind
= OMP_CLAUSE_LINEAR_VAL
;
34238 else if (strcmp ("uval", p
) == 0)
34239 kind
= OMP_CLAUSE_LINEAR_UVAL
;
34240 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
34241 cp_lexer_consume_token (parser
->lexer
);
34243 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34246 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
34247 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
34251 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
34252 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
34254 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34255 else if (!parens
.require_close (parser
))
34256 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34257 /*or_comma=*/false,
34258 /*consume_paren=*/true);
34265 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34266 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
34268 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34269 cp_parser_parse_tentatively (parser
);
34270 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
34271 /*check_dependency_p=*/true,
34272 /*template_p=*/NULL
,
34273 /*declarator_p=*/false,
34274 /*optional_p=*/false);
34275 if (step
!= error_mark_node
)
34276 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
34277 if (step
== error_mark_node
)
34280 cp_parser_abort_tentative_parse (parser
);
34282 else if (!cp_parser_parse_definitely (parser
))
34286 step
= cp_parser_assignment_expression (parser
);
34288 if (!parens
.require_close (parser
))
34289 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34290 /*or_comma=*/false,
34291 /*consume_paren=*/true);
34293 if (step
== error_mark_node
)
34297 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34299 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
34300 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
34307 safelen ( constant-expression ) */
34310 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
34311 location_t location
)
34315 matching_parens parens
;
34316 if (!parens
.require_open (parser
))
34319 t
= cp_parser_constant_expression (parser
);
34321 if (t
== error_mark_node
34322 || !parens
.require_close (parser
))
34323 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34324 /*or_comma=*/false,
34325 /*consume_paren=*/true);
34327 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
34329 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
34330 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
34331 OMP_CLAUSE_CHAIN (c
) = list
;
34337 simdlen ( constant-expression ) */
34340 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
34341 location_t location
)
34345 matching_parens parens
;
34346 if (!parens
.require_open (parser
))
34349 t
= cp_parser_constant_expression (parser
);
34351 if (t
== error_mark_node
34352 || !parens
.require_close (parser
))
34353 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34354 /*or_comma=*/false,
34355 /*consume_paren=*/true);
34357 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
34359 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
34360 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
34361 OMP_CLAUSE_CHAIN (c
) = list
;
34368 identifier [+/- integer]
34369 vec , identifier [+/- integer]
34373 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
34378 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34380 cp_parser_error (parser
, "expected identifier");
34384 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34386 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34387 tree t
, identifier
= cp_parser_identifier (parser
);
34388 tree addend
= NULL
;
34390 if (identifier
== error_mark_node
)
34391 t
= error_mark_node
;
34394 t
= cp_parser_lookup_name_simple
34395 (parser
, identifier
,
34396 cp_lexer_peek_token (parser
->lexer
)->location
);
34397 if (t
== error_mark_node
)
34398 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
34403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
34405 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
34407 addend
= integer_zero_node
;
34408 goto add_to_vector
;
34410 cp_lexer_consume_token (parser
->lexer
);
34412 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
34414 cp_parser_error (parser
, "expected integer");
34418 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34419 if (TREE_CODE (addend
) != INTEGER_CST
)
34421 cp_parser_error (parser
, "expected integer");
34424 cp_lexer_consume_token (parser
->lexer
);
34427 if (t
!= error_mark_node
)
34429 vec
= tree_cons (addend
, t
, vec
);
34431 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
34434 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34437 cp_lexer_consume_token (parser
->lexer
);
34440 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
34442 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
34443 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
34444 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
34445 OMP_CLAUSE_CHAIN (u
) = list
;
34452 iterators ( iterators-definition )
34454 iterators-definition:
34456 iterator-specifier , iterators-definition
34458 iterator-specifier:
34459 identifier = range-specification
34460 iterator-type identifier = range-specification
34462 range-specification:
34464 begin : end : step */
34467 cp_parser_omp_iterators (cp_parser
*parser
)
34469 tree ret
= NULL_TREE
, *last
= &ret
;
34470 cp_lexer_consume_token (parser
->lexer
);
34472 matching_parens parens
;
34473 if (!parens
.require_open (parser
))
34474 return error_mark_node
;
34476 bool saved_colon_corrects_to_scope_p
34477 = parser
->colon_corrects_to_scope_p
;
34478 bool saved_colon_doesnt_start_class_def_p
34479 = parser
->colon_doesnt_start_class_def_p
;
34484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34485 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34486 iter_type
= integer_type_node
;
34489 const char *saved_message
34490 = parser
->type_definition_forbidden_message
;
34491 parser
->type_definition_forbidden_message
34492 = G_("types may not be defined in iterator type");
34494 iter_type
= cp_parser_type_id (parser
);
34496 parser
->type_definition_forbidden_message
= saved_message
;
34499 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34500 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34502 cp_parser_error (parser
, "expected identifier");
34506 tree id
= cp_parser_identifier (parser
);
34507 if (id
== error_mark_node
)
34510 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34513 parser
->colon_corrects_to_scope_p
= false;
34514 parser
->colon_doesnt_start_class_def_p
= true;
34515 tree begin
= cp_parser_assignment_expression (parser
);
34517 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34520 tree end
= cp_parser_assignment_expression (parser
);
34522 tree step
= integer_one_node
;
34523 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34525 cp_lexer_consume_token (parser
->lexer
);
34526 step
= cp_parser_assignment_expression (parser
);
34529 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34530 DECL_ARTIFICIAL (iter_var
) = 1;
34531 DECL_CONTEXT (iter_var
) = current_function_decl
;
34532 pushdecl (iter_var
);
34534 *last
= make_tree_vec (6);
34535 TREE_VEC_ELT (*last
, 0) = iter_var
;
34536 TREE_VEC_ELT (*last
, 1) = begin
;
34537 TREE_VEC_ELT (*last
, 2) = end
;
34538 TREE_VEC_ELT (*last
, 3) = step
;
34539 last
= &TREE_CHAIN (*last
);
34541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34543 cp_lexer_consume_token (parser
->lexer
);
34550 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34551 parser
->colon_doesnt_start_class_def_p
34552 = saved_colon_doesnt_start_class_def_p
;
34554 if (!parens
.require_close (parser
))
34555 cp_parser_skip_to_closing_parenthesis (parser
,
34556 /*recovering=*/true,
34557 /*or_comma=*/false,
34558 /*consume_paren=*/true);
34560 return ret
? ret
: error_mark_node
;
34564 depend ( depend-kind : variable-list )
34572 depend ( sink : vec )
34575 depend ( depend-modifier , depend-kind: variable-list )
34578 in | out | inout | mutexinoutset | depobj
34581 iterator ( iterators-definition ) */
34584 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34586 tree nlist
, c
, iterators
= NULL_TREE
;
34587 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34589 matching_parens parens
;
34590 if (!parens
.require_open (parser
))
34595 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34598 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34599 const char *p
= IDENTIFIER_POINTER (id
);
34601 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34603 begin_scope (sk_omp
, NULL
);
34604 iterators
= cp_parser_omp_iterators (parser
);
34605 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34608 if (strcmp ("in", p
) == 0)
34609 kind
= OMP_CLAUSE_DEPEND_IN
;
34610 else if (strcmp ("inout", p
) == 0)
34611 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34612 else if (strcmp ("mutexinoutset", p
) == 0)
34613 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34614 else if (strcmp ("out", p
) == 0)
34615 kind
= OMP_CLAUSE_DEPEND_OUT
;
34616 else if (strcmp ("depobj", p
) == 0)
34617 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34618 else if (strcmp ("sink", p
) == 0)
34619 kind
= OMP_CLAUSE_DEPEND_SINK
;
34620 else if (strcmp ("source", p
) == 0)
34621 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34628 cp_lexer_consume_token (parser
->lexer
);
34631 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34633 poplevel (0, 1, 0);
34634 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34635 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34636 iterators
= NULL_TREE
;
34639 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34641 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34642 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34643 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34644 OMP_CLAUSE_CHAIN (c
) = list
;
34645 if (!parens
.require_close (parser
))
34646 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34647 /*or_comma=*/false,
34648 /*consume_paren=*/true);
34652 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34655 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34656 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34659 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34664 tree block
= poplevel (1, 1, 0);
34665 if (iterators
== error_mark_node
)
34666 iterators
= NULL_TREE
;
34668 TREE_VEC_ELT (iterators
, 5) = block
;
34671 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34673 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34675 OMP_CLAUSE_DECL (c
)
34676 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34682 cp_parser_error (parser
, "invalid depend kind");
34685 poplevel (0, 1, 0);
34686 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34687 /*or_comma=*/false,
34688 /*consume_paren=*/true);
34693 map ( map-kind : variable-list )
34694 map ( variable-list )
34697 alloc | to | from | tofrom
34701 alloc | to | from | tofrom | release | delete
34703 map ( always [,] map-kind: variable-list ) */
34706 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34709 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34710 bool always
= false;
34712 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34717 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34718 const char *p
= IDENTIFIER_POINTER (id
);
34720 if (strcmp ("always", p
) == 0)
34723 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34725 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34726 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34728 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34732 cp_lexer_consume_token (parser
->lexer
);
34734 cp_lexer_consume_token (parser
->lexer
);
34739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34740 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34742 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34743 const char *p
= IDENTIFIER_POINTER (id
);
34745 if (strcmp ("alloc", p
) == 0)
34746 kind
= GOMP_MAP_ALLOC
;
34747 else if (strcmp ("to", p
) == 0)
34748 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34749 else if (strcmp ("from", p
) == 0)
34750 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34751 else if (strcmp ("tofrom", p
) == 0)
34752 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34753 else if (strcmp ("release", p
) == 0)
34754 kind
= GOMP_MAP_RELEASE
;
34757 cp_parser_error (parser
, "invalid map kind");
34758 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34759 /*or_comma=*/false,
34760 /*consume_paren=*/true);
34763 cp_lexer_consume_token (parser
->lexer
);
34764 cp_lexer_consume_token (parser
->lexer
);
34766 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34767 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34769 kind
= GOMP_MAP_DELETE
;
34770 cp_lexer_consume_token (parser
->lexer
);
34771 cp_lexer_consume_token (parser
->lexer
);
34774 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34777 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34778 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34784 device ( expression ) */
34787 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34788 location_t location
)
34792 matching_parens parens
;
34793 if (!parens
.require_open (parser
))
34796 t
= cp_parser_assignment_expression (parser
);
34798 if (t
== error_mark_node
34799 || !parens
.require_close (parser
))
34800 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34801 /*or_comma=*/false,
34802 /*consume_paren=*/true);
34804 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34805 "device", location
);
34807 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34808 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34809 OMP_CLAUSE_CHAIN (c
) = list
;
34815 dist_schedule ( static )
34816 dist_schedule ( static , expression ) */
34819 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34820 location_t location
)
34824 matching_parens parens
;
34825 if (!parens
.require_open (parser
))
34828 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34830 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34832 cp_lexer_consume_token (parser
->lexer
);
34834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34836 cp_lexer_consume_token (parser
->lexer
);
34838 t
= cp_parser_assignment_expression (parser
);
34840 if (t
== error_mark_node
)
34842 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34844 if (!parens
.require_close (parser
))
34847 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34850 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34852 OMP_CLAUSE_CHAIN (c
) = list
;
34856 cp_parser_error (parser
, "invalid dist_schedule kind");
34858 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34859 /*or_comma=*/false,
34860 /*consume_paren=*/true);
34865 proc_bind ( proc-bind-kind )
34868 master | close | spread */
34871 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34872 location_t location
)
34875 enum omp_clause_proc_bind_kind kind
;
34877 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34882 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34883 const char *p
= IDENTIFIER_POINTER (id
);
34885 if (strcmp ("master", p
) == 0)
34886 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34887 else if (strcmp ("close", p
) == 0)
34888 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34889 else if (strcmp ("spread", p
) == 0)
34890 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34897 cp_lexer_consume_token (parser
->lexer
);
34898 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34901 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34902 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34904 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34905 OMP_CLAUSE_CHAIN (c
) = list
;
34909 cp_parser_error (parser
, "invalid depend kind");
34911 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34912 /*or_comma=*/false,
34913 /*consume_paren=*/true);
34918 async [( int-expr )] */
34921 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34924 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34926 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34928 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34930 matching_parens parens
;
34931 parens
.consume_open (parser
);
34933 t
= cp_parser_expression (parser
);
34934 if (t
== error_mark_node
34935 || !parens
.require_close (parser
))
34936 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34937 /*or_comma=*/false,
34938 /*consume_paren=*/true);
34941 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34943 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34944 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34945 OMP_CLAUSE_CHAIN (c
) = list
;
34951 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34952 is a bitmask in MASK. Return the list of clauses found. */
34955 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34956 const char *where
, cp_token
*pragma_tok
,
34957 bool finish_p
= true)
34959 tree clauses
= NULL
;
34962 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34965 pragma_omp_clause c_kind
;
34966 omp_clause_code code
;
34967 const char *c_name
;
34968 tree prev
= clauses
;
34970 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34971 cp_lexer_consume_token (parser
->lexer
);
34973 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
34974 c_kind
= cp_parser_omp_clause_name (parser
);
34978 case PRAGMA_OACC_CLAUSE_ASYNC
:
34979 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
34982 case PRAGMA_OACC_CLAUSE_AUTO
:
34983 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
34987 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
34988 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
34989 c_name
= "collapse";
34991 case PRAGMA_OACC_CLAUSE_COPY
:
34992 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34995 case PRAGMA_OACC_CLAUSE_COPYIN
:
34996 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
34999 case PRAGMA_OACC_CLAUSE_COPYOUT
:
35000 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35001 c_name
= "copyout";
35003 case PRAGMA_OACC_CLAUSE_CREATE
:
35004 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35007 case PRAGMA_OACC_CLAUSE_DELETE
:
35008 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35011 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35012 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
35013 c_name
= "default";
35015 case PRAGMA_OACC_CLAUSE_DEVICE
:
35016 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35019 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
35020 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
35021 c_name
= "deviceptr";
35023 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
35024 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35025 c_name
= "device_resident";
35027 case PRAGMA_OACC_CLAUSE_FINALIZE
:
35028 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
35030 c_name
= "finalize";
35032 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
35033 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35035 c_name
= "firstprivate";
35037 case PRAGMA_OACC_CLAUSE_GANG
:
35039 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
35042 case PRAGMA_OACC_CLAUSE_HOST
:
35043 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35046 case PRAGMA_OACC_CLAUSE_IF
:
35047 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
35050 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
35051 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
35053 c_name
= "if_present";
35055 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
35056 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
35058 c_name
= "independent";
35060 case PRAGMA_OACC_CLAUSE_LINK
:
35061 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35064 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
35065 code
= OMP_CLAUSE_NUM_GANGS
;
35066 c_name
= "num_gangs";
35067 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35070 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
35071 c_name
= "num_workers";
35072 code
= OMP_CLAUSE_NUM_WORKERS
;
35073 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35076 case PRAGMA_OACC_CLAUSE_PRESENT
:
35077 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35078 c_name
= "present";
35080 case PRAGMA_OACC_CLAUSE_PRIVATE
:
35081 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35083 c_name
= "private";
35085 case PRAGMA_OACC_CLAUSE_REDUCTION
:
35087 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35089 c_name
= "reduction";
35091 case PRAGMA_OACC_CLAUSE_SEQ
:
35092 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
35096 case PRAGMA_OACC_CLAUSE_TILE
:
35097 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
35100 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
35101 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35103 c_name
= "use_device";
35105 case PRAGMA_OACC_CLAUSE_VECTOR
:
35107 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
35111 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
35112 c_name
= "vector_length";
35113 code
= OMP_CLAUSE_VECTOR_LENGTH
;
35114 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35117 case PRAGMA_OACC_CLAUSE_WAIT
:
35118 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
35121 case PRAGMA_OACC_CLAUSE_WORKER
:
35123 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
35128 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
35134 if (((mask
>> c_kind
) & 1) == 0)
35136 /* Remove the invalid clause(s) from the list to avoid
35137 confusing the rest of the compiler. */
35139 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
35144 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35147 return finish_omp_clauses (clauses
, C_ORT_ACC
);
35152 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35153 is a bitmask in MASK. Return the list of clauses found; the result
35154 of clause default goes in *pdefault. */
35157 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
35158 const char *where
, cp_token
*pragma_tok
,
35159 bool finish_p
= true)
35161 tree clauses
= NULL
;
35163 cp_token
*token
= NULL
;
35165 /* Don't create location wrapper nodes within OpenMP clauses. */
35166 auto_suppress_location_wrappers sentinel
;
35168 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35170 pragma_omp_clause c_kind
;
35171 const char *c_name
;
35172 tree prev
= clauses
;
35174 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35175 cp_lexer_consume_token (parser
->lexer
);
35177 token
= cp_lexer_peek_token (parser
->lexer
);
35178 c_kind
= cp_parser_omp_clause_name (parser
);
35182 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
35183 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
35185 c_name
= "collapse";
35187 case PRAGMA_OMP_CLAUSE_COPYIN
:
35188 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
35191 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
35192 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
35194 c_name
= "copyprivate";
35196 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35197 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
35198 token
->location
, false);
35199 c_name
= "default";
35201 case PRAGMA_OMP_CLAUSE_FINAL
:
35202 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
35205 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
35206 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35208 c_name
= "firstprivate";
35210 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
35211 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
35213 c_name
= "grainsize";
35215 case PRAGMA_OMP_CLAUSE_HINT
:
35216 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
35220 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
35221 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
35223 c_name
= "defaultmap";
35225 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
35226 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35228 c_name
= "use_device_ptr";
35230 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
35231 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
35233 c_name
= "is_device_ptr";
35235 case PRAGMA_OMP_CLAUSE_IF
:
35236 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
35240 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
35242 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
35244 c_name
= "in_reduction";
35246 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
35247 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
35248 c_name
= "lastprivate";
35250 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
35251 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
35253 c_name
= "mergeable";
35255 case PRAGMA_OMP_CLAUSE_NOWAIT
:
35256 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
35259 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
35260 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
35262 c_name
= "num_tasks";
35264 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
35265 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
35267 c_name
= "num_threads";
35269 case PRAGMA_OMP_CLAUSE_ORDERED
:
35270 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
35272 c_name
= "ordered";
35274 case PRAGMA_OMP_CLAUSE_PRIORITY
:
35275 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
35277 c_name
= "priority";
35279 case PRAGMA_OMP_CLAUSE_PRIVATE
:
35280 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35282 c_name
= "private";
35284 case PRAGMA_OMP_CLAUSE_REDUCTION
:
35286 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35288 c_name
= "reduction";
35290 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
35291 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
35293 c_name
= "schedule";
35295 case PRAGMA_OMP_CLAUSE_SHARED
:
35296 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
35300 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
35302 = cp_parser_omp_clause_reduction (parser
,
35303 OMP_CLAUSE_TASK_REDUCTION
,
35305 c_name
= "task_reduction";
35307 case PRAGMA_OMP_CLAUSE_UNTIED
:
35308 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
35312 case PRAGMA_OMP_CLAUSE_INBRANCH
:
35313 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
35314 clauses
, token
->location
);
35315 c_name
= "inbranch";
35317 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
35318 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
35320 c_name
= "nontemporal";
35322 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
35323 clauses
= cp_parser_omp_clause_branch (parser
,
35324 OMP_CLAUSE_NOTINBRANCH
,
35325 clauses
, token
->location
);
35326 c_name
= "notinbranch";
35328 case PRAGMA_OMP_CLAUSE_PARALLEL
:
35329 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
35330 clauses
, token
->location
);
35331 c_name
= "parallel";
35335 error_at (token
->location
, "%qs must be the first clause of %qs",
35340 case PRAGMA_OMP_CLAUSE_FOR
:
35341 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
35342 clauses
, token
->location
);
35345 goto clause_not_first
;
35347 case PRAGMA_OMP_CLAUSE_SECTIONS
:
35348 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
35349 clauses
, token
->location
);
35350 c_name
= "sections";
35352 goto clause_not_first
;
35354 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
35355 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
35356 clauses
, token
->location
);
35357 c_name
= "taskgroup";
35359 goto clause_not_first
;
35361 case PRAGMA_OMP_CLAUSE_LINK
:
35362 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
35365 case PRAGMA_OMP_CLAUSE_TO
:
35366 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
35367 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35370 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
35373 case PRAGMA_OMP_CLAUSE_FROM
:
35374 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
35377 case PRAGMA_OMP_CLAUSE_UNIFORM
:
35378 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
35380 c_name
= "uniform";
35382 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
35383 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
35385 c_name
= "num_teams";
35387 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
35388 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
35390 c_name
= "thread_limit";
35392 case PRAGMA_OMP_CLAUSE_ALIGNED
:
35393 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
35394 c_name
= "aligned";
35396 case PRAGMA_OMP_CLAUSE_LINEAR
:
35398 bool declare_simd
= false;
35399 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
35400 declare_simd
= true;
35401 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
35405 case PRAGMA_OMP_CLAUSE_DEPEND
:
35406 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
35410 case PRAGMA_OMP_CLAUSE_MAP
:
35411 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
35414 case PRAGMA_OMP_CLAUSE_DEVICE
:
35415 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
35419 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
35420 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
35422 c_name
= "dist_schedule";
35424 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
35425 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
35427 c_name
= "proc_bind";
35429 case PRAGMA_OMP_CLAUSE_SAFELEN
:
35430 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
35432 c_name
= "safelen";
35434 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
35435 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
35437 c_name
= "simdlen";
35439 case PRAGMA_OMP_CLAUSE_NOGROUP
:
35440 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
35442 c_name
= "nogroup";
35444 case PRAGMA_OMP_CLAUSE_THREADS
:
35446 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
35447 clauses
, token
->location
);
35448 c_name
= "threads";
35450 case PRAGMA_OMP_CLAUSE_SIMD
:
35452 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
35453 clauses
, token
->location
);
35457 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35463 if (((mask
>> c_kind
) & 1) == 0)
35465 /* Remove the invalid clause(s) from the list to avoid
35466 confusing the rest of the compiler. */
35468 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35472 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35475 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35476 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35478 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35487 In practice, we're also interested in adding the statement to an
35488 outer node. So it is convenient if we work around the fact that
35489 cp_parser_statement calls add_stmt. */
35492 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35494 unsigned save
= parser
->in_statement
;
35496 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35497 This preserves the "not within loop or switch" style error messages
35498 for nonsense cases like
35504 if (parser
->in_statement
)
35505 parser
->in_statement
= IN_OMP_BLOCK
;
35511 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35513 parser
->in_statement
= save
;
35517 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35519 tree stmt
= begin_omp_structured_block ();
35520 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35522 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35524 cp_parser_end_omp_structured_block (parser
, save
);
35525 return finish_omp_structured_block (stmt
);
35529 # pragma omp atomic new-line
35533 x binop= expr | x++ | ++x | x-- | --x
35535 +, *, -, /, &, ^, |, <<, >>
35537 where x is an lvalue expression with scalar type.
35540 # pragma omp atomic new-line
35543 # pragma omp atomic read new-line
35546 # pragma omp atomic write new-line
35549 # pragma omp atomic update new-line
35552 # pragma omp atomic capture new-line
35555 # pragma omp atomic capture new-line
35563 expression-stmt | x = x binop expr
35565 v = expression-stmt
35567 { v = x; update-stmt; } | { update-stmt; v = x; }
35571 expression-stmt | x = x binop expr | x = expr binop x
35575 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35577 where x and v are lvalue expressions with scalar type. */
35580 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35582 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35583 tree rhs1
= NULL_TREE
, orig_lhs
;
35584 location_t loc
= pragma_tok
->location
;
35585 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35586 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35587 bool structured_block
= false;
35589 tree clauses
= NULL_TREE
;
35591 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35593 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35594 cp_lexer_consume_token (parser
->lexer
);
35598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35600 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35601 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35602 const char *p
= IDENTIFIER_POINTER (id
);
35603 enum tree_code new_code
= ERROR_MARK
;
35604 enum omp_memory_order new_memory_order
35605 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35607 if (!strcmp (p
, "read"))
35608 new_code
= OMP_ATOMIC_READ
;
35609 else if (!strcmp (p
, "write"))
35610 new_code
= NOP_EXPR
;
35611 else if (!strcmp (p
, "update"))
35612 new_code
= OMP_ATOMIC
;
35613 else if (!strcmp (p
, "capture"))
35614 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35615 else if (!strcmp (p
, "seq_cst"))
35616 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35617 else if (!strcmp (p
, "acq_rel"))
35618 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35619 else if (!strcmp (p
, "release"))
35620 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35621 else if (!strcmp (p
, "acquire"))
35622 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35623 else if (!strcmp (p
, "relaxed"))
35624 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35625 else if (!strcmp (p
, "hint"))
35627 cp_lexer_consume_token (parser
->lexer
);
35628 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35634 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35635 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35636 "%<release%>, %<relaxed%> or %<hint%> clause");
35640 if (new_code
!= ERROR_MARK
)
35642 if (code
!= ERROR_MARK
)
35643 error_at (cloc
, "too many atomic clauses");
35647 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35649 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35650 error_at (cloc
, "too many memory order clauses");
35652 memory_order
= new_memory_order
;
35654 cp_lexer_consume_token (parser
->lexer
);
35660 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35662 if (code
== ERROR_MARK
)
35664 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35667 = (enum omp_requires
) (omp_requires_mask
35668 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35669 switch ((enum omp_memory_order
)
35670 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35672 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35673 case OMP_MEMORY_ORDER_RELAXED
:
35674 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35676 case OMP_MEMORY_ORDER_SEQ_CST
:
35677 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35679 case OMP_MEMORY_ORDER_ACQ_REL
:
35682 case OMP_ATOMIC_READ
:
35683 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35685 case NOP_EXPR
: /* atomic write */
35687 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35690 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35695 gcc_unreachable ();
35701 case OMP_ATOMIC_READ
:
35702 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35703 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35705 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35706 "%<acq_rel%> or %<release%> clauses");
35707 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35710 case NOP_EXPR
: /* atomic write */
35711 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35712 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35714 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35715 "%<acq_rel%> or %<acquire%> clauses");
35716 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35720 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35721 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35723 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35724 "%<acq_rel%> or %<acquire%> clauses");
35725 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35734 case OMP_ATOMIC_READ
:
35735 case NOP_EXPR
: /* atomic write */
35736 v
= cp_parser_unary_expression (parser
);
35737 if (v
== error_mark_node
)
35739 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35741 if (code
== NOP_EXPR
)
35742 lhs
= cp_parser_expression (parser
);
35744 lhs
= cp_parser_unary_expression (parser
);
35745 if (lhs
== error_mark_node
)
35747 if (code
== NOP_EXPR
)
35749 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35757 case OMP_ATOMIC_CAPTURE_NEW
:
35758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35760 cp_lexer_consume_token (parser
->lexer
);
35761 structured_block
= true;
35765 v
= cp_parser_unary_expression (parser
);
35766 if (v
== error_mark_node
)
35768 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35776 lhs
= cp_parser_unary_expression (parser
);
35778 switch (TREE_CODE (lhs
))
35783 case POSTINCREMENT_EXPR
:
35784 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35785 code
= OMP_ATOMIC_CAPTURE_OLD
;
35787 case PREINCREMENT_EXPR
:
35788 lhs
= TREE_OPERAND (lhs
, 0);
35789 opcode
= PLUS_EXPR
;
35790 rhs
= integer_one_node
;
35793 case POSTDECREMENT_EXPR
:
35794 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35795 code
= OMP_ATOMIC_CAPTURE_OLD
;
35797 case PREDECREMENT_EXPR
:
35798 lhs
= TREE_OPERAND (lhs
, 0);
35799 opcode
= MINUS_EXPR
;
35800 rhs
= integer_one_node
;
35803 case COMPOUND_EXPR
:
35804 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35805 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35806 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35807 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35808 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35809 (TREE_OPERAND (lhs
, 1), 0), 0)))
35811 /* Undo effects of boolean_increment for post {in,de}crement. */
35812 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35815 if (TREE_CODE (lhs
) == MODIFY_EXPR
35816 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35818 /* Undo effects of boolean_increment. */
35819 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35821 /* This is pre or post increment. */
35822 rhs
= TREE_OPERAND (lhs
, 1);
35823 lhs
= TREE_OPERAND (lhs
, 0);
35825 if (code
== OMP_ATOMIC_CAPTURE_NEW
35826 && !structured_block
35827 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35828 code
= OMP_ATOMIC_CAPTURE_OLD
;
35834 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35837 opcode
= MULT_EXPR
;
35840 opcode
= TRUNC_DIV_EXPR
;
35843 opcode
= PLUS_EXPR
;
35846 opcode
= MINUS_EXPR
;
35848 case CPP_LSHIFT_EQ
:
35849 opcode
= LSHIFT_EXPR
;
35851 case CPP_RSHIFT_EQ
:
35852 opcode
= RSHIFT_EXPR
;
35855 opcode
= BIT_AND_EXPR
;
35858 opcode
= BIT_IOR_EXPR
;
35861 opcode
= BIT_XOR_EXPR
;
35864 enum cp_parser_prec oprec
;
35866 cp_lexer_consume_token (parser
->lexer
);
35867 cp_parser_parse_tentatively (parser
);
35868 rhs1
= cp_parser_simple_cast_expression (parser
);
35869 if (rhs1
== error_mark_node
)
35871 cp_parser_abort_tentative_parse (parser
);
35872 cp_parser_simple_cast_expression (parser
);
35875 token
= cp_lexer_peek_token (parser
->lexer
);
35876 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35878 cp_parser_abort_tentative_parse (parser
);
35879 cp_parser_parse_tentatively (parser
);
35880 rhs
= cp_parser_binary_expression (parser
, false, true,
35881 PREC_NOT_OPERATOR
, NULL
);
35882 if (rhs
== error_mark_node
)
35884 cp_parser_abort_tentative_parse (parser
);
35885 cp_parser_binary_expression (parser
, false, true,
35886 PREC_NOT_OPERATOR
, NULL
);
35889 switch (TREE_CODE (rhs
))
35892 case TRUNC_DIV_EXPR
:
35901 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35903 if (cp_parser_parse_definitely (parser
))
35905 opcode
= TREE_CODE (rhs
);
35906 rhs1
= TREE_OPERAND (rhs
, 0);
35907 rhs
= TREE_OPERAND (rhs
, 1);
35917 cp_parser_abort_tentative_parse (parser
);
35918 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35920 rhs
= cp_parser_expression (parser
);
35921 if (rhs
== error_mark_node
)
35927 cp_parser_error (parser
,
35928 "invalid form of %<#pragma omp atomic%>");
35931 if (!cp_parser_parse_definitely (parser
))
35933 switch (token
->type
)
35935 case CPP_SEMICOLON
:
35936 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35938 code
= OMP_ATOMIC_CAPTURE_OLD
;
35943 cp_lexer_consume_token (parser
->lexer
);
35946 else if (structured_block
)
35953 cp_parser_error (parser
,
35954 "invalid form of %<#pragma omp atomic%>");
35957 opcode
= MULT_EXPR
;
35960 opcode
= TRUNC_DIV_EXPR
;
35963 opcode
= PLUS_EXPR
;
35966 opcode
= MINUS_EXPR
;
35969 opcode
= LSHIFT_EXPR
;
35972 opcode
= RSHIFT_EXPR
;
35975 opcode
= BIT_AND_EXPR
;
35978 opcode
= BIT_IOR_EXPR
;
35981 opcode
= BIT_XOR_EXPR
;
35984 cp_parser_error (parser
,
35985 "invalid operator for %<#pragma omp atomic%>");
35988 oprec
= TOKEN_PRECEDENCE (token
);
35989 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
35990 if (commutative_tree_code (opcode
))
35991 oprec
= (enum cp_parser_prec
) (oprec
- 1);
35992 cp_lexer_consume_token (parser
->lexer
);
35993 rhs
= cp_parser_binary_expression (parser
, false, false,
35995 if (rhs
== error_mark_node
)
36000 cp_parser_error (parser
,
36001 "invalid operator for %<#pragma omp atomic%>");
36004 cp_lexer_consume_token (parser
->lexer
);
36006 rhs
= cp_parser_expression (parser
);
36007 if (rhs
== error_mark_node
)
36012 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
36014 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
36016 v
= cp_parser_unary_expression (parser
);
36017 if (v
== error_mark_node
)
36019 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36021 lhs1
= cp_parser_unary_expression (parser
);
36022 if (lhs1
== error_mark_node
)
36025 if (structured_block
)
36027 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36028 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
36031 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
36032 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
36033 rhs1
, clauses
, memory_order
);
36034 if (!structured_block
)
36035 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36039 cp_parser_skip_to_end_of_block_or_statement (parser
);
36040 if (structured_block
)
36042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36043 cp_lexer_consume_token (parser
->lexer
);
36044 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
36046 cp_parser_skip_to_end_of_block_or_statement (parser
);
36047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36048 cp_lexer_consume_token (parser
->lexer
);
36055 # pragma omp barrier new-line */
36058 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
36060 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36061 finish_omp_barrier ();
36065 # pragma omp critical [(name)] new-line
36069 # pragma omp critical [(name) [hint(expression)]] new-line
36070 structured-block */
36072 #define OMP_CRITICAL_CLAUSE_MASK \
36073 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36076 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36078 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
36080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36082 matching_parens parens
;
36083 parens
.consume_open (parser
);
36085 name
= cp_parser_identifier (parser
);
36087 if (name
== error_mark_node
36088 || !parens
.require_close (parser
))
36089 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36090 /*or_comma=*/false,
36091 /*consume_paren=*/true);
36092 if (name
== error_mark_node
)
36095 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
36096 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
36097 cp_lexer_consume_token (parser
->lexer
);
36099 clauses
= cp_parser_omp_all_clauses (parser
,
36100 OMP_CRITICAL_CLAUSE_MASK
,
36101 "#pragma omp critical", pragma_tok
);
36104 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36106 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
36107 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
36111 # pragma omp depobj ( depobj ) depobj-clause new-line
36114 depend (dependence-type : locator)
36116 update (dependence-type)
36125 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
36127 location_t loc
= pragma_tok
->location
;
36128 matching_parens parens
;
36129 if (!parens
.require_open (parser
))
36131 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36135 tree depobj
= cp_parser_assignment_expression (parser
);
36137 if (!parens
.require_close (parser
))
36138 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36139 /*or_comma=*/false,
36140 /*consume_paren=*/true);
36142 tree clause
= NULL_TREE
;
36143 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
36144 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36147 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36148 const char *p
= IDENTIFIER_POINTER (id
);
36150 cp_lexer_consume_token (parser
->lexer
);
36151 if (!strcmp ("depend", p
))
36153 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
36155 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
36157 clause
= error_mark_node
;
36159 else if (!strcmp ("destroy", p
))
36160 kind
= OMP_CLAUSE_DEPEND_LAST
;
36161 else if (!strcmp ("update", p
))
36163 matching_parens c_parens
;
36164 if (c_parens
.require_open (parser
))
36167 = cp_lexer_peek_token (parser
->lexer
)->location
;
36168 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36170 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36171 const char *p2
= IDENTIFIER_POINTER (id2
);
36173 cp_lexer_consume_token (parser
->lexer
);
36174 if (!strcmp ("in", p2
))
36175 kind
= OMP_CLAUSE_DEPEND_IN
;
36176 else if (!strcmp ("out", p2
))
36177 kind
= OMP_CLAUSE_DEPEND_OUT
;
36178 else if (!strcmp ("inout", p2
))
36179 kind
= OMP_CLAUSE_DEPEND_INOUT
;
36180 else if (!strcmp ("mutexinoutset", p2
))
36181 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
36183 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36185 clause
= error_mark_node
;
36186 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
36187 "%<mutexinoutset%>");
36189 if (!c_parens
.require_close (parser
))
36190 cp_parser_skip_to_closing_parenthesis (parser
,
36191 /*recovering=*/true,
36192 /*or_comma=*/false,
36193 /*consume_paren=*/true);
36196 clause
= error_mark_node
;
36199 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36201 clause
= error_mark_node
;
36202 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
36204 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36206 finish_omp_depobj (loc
, depobj
, kind
, clause
);
36211 # pragma omp flush flush-vars[opt] new-line
36217 # pragma omp flush memory-order-clause new-line */
36220 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
36222 enum memmodel mo
= MEMMODEL_LAST
;
36223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36225 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36226 const char *p
= IDENTIFIER_POINTER (id
);
36227 if (!strcmp (p
, "acq_rel"))
36228 mo
= MEMMODEL_ACQ_REL
;
36229 else if (!strcmp (p
, "release"))
36230 mo
= MEMMODEL_RELEASE
;
36231 else if (!strcmp (p
, "acquire"))
36232 mo
= MEMMODEL_ACQUIRE
;
36234 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36235 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36236 cp_lexer_consume_token (parser
->lexer
);
36238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36240 if (mo
!= MEMMODEL_LAST
)
36241 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36242 "%<flush%> list specified together with memory order "
36244 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36246 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36248 finish_omp_flush (mo
);
36251 /* Helper function, to parse omp for increment expression. */
36254 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
36256 tree cond
= cp_parser_binary_expression (parser
, false, true,
36257 PREC_NOT_OPERATOR
, NULL
);
36258 if (cond
== error_mark_node
36259 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36261 cp_parser_skip_to_end_of_statement (parser
);
36262 return error_mark_node
;
36265 switch (TREE_CODE (cond
))
36273 if (code
!= OACC_LOOP
)
36275 gcc_fallthrough ();
36277 return error_mark_node
;
36280 /* If decl is an iterator, preserve LHS and RHS of the relational
36281 expr until finish_omp_for. */
36283 && (type_dependent_expression_p (decl
)
36284 || CLASS_TYPE_P (TREE_TYPE (decl
))))
36287 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
36289 TREE_OPERAND (cond
, 0), ERROR_MARK
,
36290 TREE_OPERAND (cond
, 1), ERROR_MARK
,
36291 /*overload=*/NULL
, tf_warning_or_error
);
36294 /* Helper function, to parse omp for increment expression. */
36297 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
36299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36305 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36307 op
= (token
->type
== CPP_PLUS_PLUS
36308 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
36309 cp_lexer_consume_token (parser
->lexer
);
36310 lhs
= cp_parser_simple_cast_expression (parser
);
36312 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36313 return error_mark_node
;
36314 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36317 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
36319 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36320 return error_mark_node
;
36322 token
= cp_lexer_peek_token (parser
->lexer
);
36323 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36325 op
= (token
->type
== CPP_PLUS_PLUS
36326 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
36327 cp_lexer_consume_token (parser
->lexer
);
36328 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36331 op
= cp_parser_assignment_operator_opt (parser
);
36332 if (op
== ERROR_MARK
)
36333 return error_mark_node
;
36335 if (op
!= NOP_EXPR
)
36337 rhs
= cp_parser_assignment_expression (parser
);
36338 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
36339 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36342 lhs
= cp_parser_binary_expression (parser
, false, false,
36343 PREC_ADDITIVE_EXPRESSION
, NULL
);
36344 token
= cp_lexer_peek_token (parser
->lexer
);
36345 decl_first
= (lhs
== decl
36346 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
36349 if (token
->type
!= CPP_PLUS
36350 && token
->type
!= CPP_MINUS
)
36351 return error_mark_node
;
36355 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
36356 cp_lexer_consume_token (parser
->lexer
);
36357 rhs
= cp_parser_binary_expression (parser
, false, false,
36358 PREC_ADDITIVE_EXPRESSION
, NULL
);
36359 token
= cp_lexer_peek_token (parser
->lexer
);
36360 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
36362 if (lhs
== NULL_TREE
)
36364 if (op
== PLUS_EXPR
)
36367 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
36368 tf_warning_or_error
);
36371 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
36372 ERROR_MARK
, NULL
, tf_warning_or_error
);
36375 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
36380 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
36381 || op
== MINUS_EXPR
)
36382 return error_mark_node
;
36383 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
36386 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
36388 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36391 /* Parse the initialization statement of an OpenMP for loop.
36393 Return true if the resulting construct should have an
36394 OMP_CLAUSE_PRIVATE added to it. */
36397 cp_parser_omp_for_loop_init (cp_parser
*parser
,
36398 tree
&this_pre_body
,
36399 releasing_vec
&for_block
,
36405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36408 tree add_private_clause
= NULL_TREE
;
36410 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36414 integer-type var = lb
36415 random-access-iterator-type var = lb
36416 pointer-type var = lb
36418 cp_decl_specifier_seq type_specifiers
;
36420 /* First, try to parse as an initialized declaration. See
36421 cp_parser_condition, from whence the bulk of this is copied. */
36423 cp_parser_parse_tentatively (parser
);
36424 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
36425 /*is_declaration=*/true,
36426 /*is_trailing_return=*/false,
36428 if (cp_parser_parse_definitely (parser
))
36430 /* If parsing a type specifier seq succeeded, then this
36431 MUST be a initialized declaration. */
36432 tree asm_specification
, attributes
;
36433 cp_declarator
*declarator
;
36435 declarator
= cp_parser_declarator (parser
,
36436 CP_PARSER_DECLARATOR_NAMED
,
36437 CP_PARSER_FLAGS_NONE
,
36438 /*ctor_dtor_or_conv_p=*/NULL
,
36439 /*parenthesized_p=*/NULL
,
36440 /*member_p=*/false,
36441 /*friend_p=*/false,
36442 /*static_p=*/false);
36443 attributes
= cp_parser_attributes_opt (parser
);
36444 asm_specification
= cp_parser_asm_specification_opt (parser
);
36446 if (declarator
== cp_error_declarator
)
36447 cp_parser_skip_to_end_of_statement (parser
);
36451 tree pushed_scope
, auto_node
;
36453 decl
= start_decl (declarator
, &type_specifiers
,
36454 SD_INITIALIZED
, attributes
,
36455 /*prefix_attributes=*/NULL_TREE
,
36458 auto_node
= type_uses_auto (TREE_TYPE (decl
));
36459 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36461 if (cp_lexer_next_token_is (parser
->lexer
,
36463 error ("parenthesized initialization is not allowed in "
36464 "OpenMP %<for%> loop");
36466 /* Trigger an error. */
36467 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36469 init
= error_mark_node
;
36470 cp_parser_skip_to_end_of_statement (parser
);
36472 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36473 || type_dependent_expression_p (decl
)
36476 bool is_direct_init
, is_non_constant_init
;
36478 init
= cp_parser_initializer (parser
,
36480 &is_non_constant_init
);
36485 = do_auto_deduction (TREE_TYPE (decl
), init
,
36488 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36489 && !type_dependent_expression_p (decl
))
36493 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36495 LOOKUP_ONLYCONVERTING
);
36497 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36499 vec_safe_push (for_block
, this_pre_body
);
36504 init
= pop_stmt_list (this_pre_body
);
36505 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36507 tree_stmt_iterator i
= tsi_start (init
);
36508 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36509 while (!tsi_end_p (i
))
36511 tree t
= tsi_stmt (i
);
36512 if (TREE_CODE (t
) == DECL_EXPR
36513 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36516 vec_safe_push (for_block
, t
);
36521 if (tsi_one_before_end_p (i
))
36523 tree t
= tsi_stmt (i
);
36525 free_stmt_list (init
);
36530 this_pre_body
= NULL_TREE
;
36535 cp_lexer_consume_token (parser
->lexer
);
36536 init
= cp_parser_assignment_expression (parser
);
36539 if (TYPE_REF_P (TREE_TYPE (decl
)))
36540 init
= error_mark_node
;
36542 cp_finish_decl (decl
, NULL_TREE
,
36543 /*init_const_expr_p=*/false,
36545 LOOKUP_ONLYCONVERTING
);
36549 pop_scope (pushed_scope
);
36555 /* If parsing a type specifier sequence failed, then
36556 this MUST be a simple expression. */
36557 cp_parser_parse_tentatively (parser
);
36558 decl
= cp_parser_primary_expression (parser
, false, false,
36560 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36561 if (!cp_parser_error_occurred (parser
)
36563 && (TREE_CODE (decl
) == COMPONENT_REF
36564 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36566 cp_parser_abort_tentative_parse (parser
);
36567 cp_parser_parse_tentatively (parser
);
36568 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36569 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36570 /*check_dependency_p=*/true,
36571 /*template_p=*/NULL
,
36572 /*declarator_p=*/false,
36573 /*optional_p=*/false);
36574 if (name
!= error_mark_node
36575 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36577 decl
= cp_parser_lookup_name_simple (parser
, name
,
36579 if (TREE_CODE (decl
) == FIELD_DECL
)
36580 add_private_clause
= omp_privatize_field (decl
, false);
36582 cp_parser_abort_tentative_parse (parser
);
36583 cp_parser_parse_tentatively (parser
);
36584 decl
= cp_parser_primary_expression (parser
, false, false,
36587 if (!cp_parser_error_occurred (parser
)
36590 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36594 cp_parser_parse_definitely (parser
);
36595 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36596 rhs
= cp_parser_assignment_expression (parser
);
36598 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36601 tf_warning_or_error
));
36602 if (!add_private_clause
)
36603 add_private_clause
= decl
;
36608 cp_parser_abort_tentative_parse (parser
);
36609 init
= cp_parser_expression (parser
);
36612 if (TREE_CODE (init
) == MODIFY_EXPR
36613 || TREE_CODE (init
) == MODOP_EXPR
)
36614 real_decl
= TREE_OPERAND (init
, 0);
36618 return add_private_clause
;
36621 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36624 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36625 tree
&decl
, tree
&orig_decl
, tree
&init
,
36626 tree
&orig_init
, tree
&cond
, tree
&incr
)
36628 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36629 tree iter_type
, begin_expr
, end_expr
;
36631 if (processing_template_decl
)
36633 if (check_for_bare_parameter_packs (init
))
36634 init
= error_mark_node
;
36635 if (!type_dependent_expression_p (init
)
36636 /* do_auto_deduction doesn't mess with template init-lists. */
36637 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36640 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36642 tree v
= DECL_VALUE_EXPR (decl
);
36643 if (TREE_CODE (v
) == ARRAY_REF
36644 && VAR_P (TREE_OPERAND (v
, 0))
36645 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36646 d
= TREE_OPERAND (v
, 0);
36648 do_range_for_auto_deduction (d
, init
);
36650 cond
= global_namespace
;
36654 this_pre_body
= pop_stmt_list (this_pre_body
);
36658 init
= mark_lvalue_use (init
);
36660 if (decl
== error_mark_node
|| init
== error_mark_node
)
36661 /* If an error happened previously do nothing or else a lot of
36662 unhelpful errors would be issued. */
36663 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36669 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36670 /* Can't bind a reference to an array of runtime bound. */
36674 range_temp
= build_range_temp (init
);
36675 DECL_NAME (range_temp
) = NULL_TREE
;
36676 pushdecl (range_temp
);
36677 cp_finish_decl (range_temp
, init
,
36678 /*is_constant_init*/false, NULL_TREE
,
36679 LOOKUP_ONLYCONVERTING
);
36680 range_temp_decl
= range_temp
;
36681 range_temp
= convert_from_reference (range_temp
);
36683 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36684 &begin_expr
, &end_expr
);
36687 tree end_iter_type
= iter_type
;
36688 if (cxx_dialect
>= cxx17
)
36689 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36690 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36691 TREE_USED (end
) = 1;
36692 DECL_ARTIFICIAL (end
) = 1;
36694 cp_finish_decl (end
, end_expr
,
36695 /*is_constant_init*/false, NULL_TREE
,
36696 LOOKUP_ONLYCONVERTING
);
36698 /* The new for initialization statement. */
36699 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36700 TREE_USED (begin
) = 1;
36701 DECL_ARTIFICIAL (begin
) = 1;
36704 if (CLASS_TYPE_P (iter_type
))
36709 begin_expr
= NULL_TREE
;
36711 cp_finish_decl (begin
, begin_expr
,
36712 /*is_constant_init*/false, NULL_TREE
,
36713 LOOKUP_ONLYCONVERTING
);
36715 /* The new for condition. */
36716 if (CLASS_TYPE_P (iter_type
))
36717 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36719 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36722 NULL
, tf_warning_or_error
);
36724 /* The new increment expression. */
36725 if (CLASS_TYPE_P (iter_type
))
36726 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36728 incr
= finish_unary_op_expr (input_location
,
36729 PREINCREMENT_EXPR
, begin
,
36730 tf_warning_or_error
);
36736 vec_safe_push (for_block
, this_pre_body
);
36737 this_pre_body
= NULL_TREE
;
36740 tree decomp_first_name
= NULL_TREE
;
36741 unsigned decomp_cnt
= 0;
36742 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36744 tree v
= DECL_VALUE_EXPR (orig_decl
);
36745 if (TREE_CODE (v
) == ARRAY_REF
36746 && VAR_P (TREE_OPERAND (v
, 0))
36747 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36749 tree d
= orig_decl
;
36750 orig_decl
= TREE_OPERAND (v
, 0);
36751 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36752 decomp_first_name
= d
;
36756 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36759 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36761 if (!error_operand_p (t
))
36762 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36766 tree v
= make_tree_vec (decomp_cnt
+ 3);
36767 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36768 TREE_VEC_ELT (v
, 1) = end
;
36769 TREE_VEC_ELT (v
, 2) = orig_decl
;
36770 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36772 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36773 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36775 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36778 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36779 inside of the collapsed body. */
36782 cp_finish_omp_range_for (tree orig
, tree begin
)
36784 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36785 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36786 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36787 tree decomp_first_name
= NULL_TREE
;
36788 unsigned int decomp_cnt
= 0;
36790 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36792 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36793 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36794 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36797 /* The declaration is initialized with *__begin inside the loop body. */
36798 cp_finish_decl (decl
,
36799 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36800 tf_warning_or_error
),
36801 /*is_constant_init*/false, NULL_TREE
,
36802 LOOKUP_ONLYCONVERTING
);
36803 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36804 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36807 /* Parse the restricted form of the for statement allowed by OpenMP. */
36810 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36811 tree
*cclauses
, bool *if_p
)
36813 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36815 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36816 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36817 location_t loc_first
;
36818 bool collapse_err
= false;
36819 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36820 releasing_vec for_block
;
36821 auto_vec
<tree
, 4> orig_inits
;
36822 bool tiling
= false;
36824 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36825 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36826 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36827 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36830 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36832 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36833 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36836 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36839 if (ordered
&& ordered
< collapse
)
36841 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36842 "%<ordered%> clause parameter is less than %<collapse%>");
36843 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36844 = build_int_cst (NULL_TREE
, collapse
);
36845 ordered
= collapse
;
36849 for (tree
*pc
= &clauses
; *pc
; )
36850 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36852 error_at (OMP_CLAUSE_LOCATION (*pc
),
36853 "%<linear%> clause may not be specified together "
36854 "with %<ordered%> clause with a parameter");
36855 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36858 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36861 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36862 count
= ordered
? ordered
: collapse
;
36864 declv
= make_tree_vec (count
);
36865 initv
= make_tree_vec (count
);
36866 condv
= make_tree_vec (count
);
36867 incrv
= make_tree_vec (count
);
36868 orig_declv
= NULL_TREE
;
36870 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36872 for (i
= 0; i
< count
; i
++)
36874 int bracecount
= 0;
36875 tree add_private_clause
= NULL_TREE
;
36878 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36881 cp_parser_error (parser
, "for statement expected");
36884 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36886 /* Don't create location wrapper nodes within an OpenMP "for"
36888 auto_suppress_location_wrappers sentinel
;
36890 matching_parens parens
;
36891 if (!parens
.require_open (parser
))
36894 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36895 this_pre_body
= push_stmt_list ();
36897 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36899 /* Save tokens so that we can put them back. */
36900 cp_lexer_save_tokens (parser
->lexer
);
36902 /* Look for ':' that is not nested in () or {}. */
36904 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36905 /*recovering=*/false,
36910 /* Roll back the tokens we skipped. */
36911 cp_lexer_rollback_tokens (parser
->lexer
);
36915 bool saved_colon_corrects_to_scope_p
36916 = parser
->colon_corrects_to_scope_p
;
36918 /* A colon is used in range-based for. */
36919 parser
->colon_corrects_to_scope_p
= false;
36921 /* Parse the declaration. */
36922 cp_parser_simple_declaration (parser
,
36923 /*function_definition_allowed_p=*/
36925 parser
->colon_corrects_to_scope_p
36926 = saved_colon_corrects_to_scope_p
;
36928 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36930 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36933 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36934 orig_decl
, init
, orig_init
,
36941 pre_body
= push_stmt_list ();
36943 add_stmt (this_pre_body
);
36944 pre_body
= pop_stmt_list (pre_body
);
36947 pre_body
= this_pre_body
;
36951 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36952 "%<ordered%> clause with parameter on "
36953 "range-based %<for%> loop");
36955 goto parse_close_paren
;
36960 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36961 init
, orig_init
, decl
, real_decl
);
36963 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36966 this_pre_body
= pop_stmt_list (this_pre_body
);
36970 pre_body
= push_stmt_list ();
36972 add_stmt (this_pre_body
);
36973 pre_body
= pop_stmt_list (pre_body
);
36976 pre_body
= this_pre_body
;
36981 if (cclauses
!= NULL
36982 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
36983 && real_decl
!= NULL_TREE
)
36986 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
36987 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
36988 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36990 error_at (loc
, "iteration variable %qD"
36991 " should not be firstprivate", real_decl
);
36992 *c
= OMP_CLAUSE_CHAIN (*c
);
36994 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
36995 && OMP_CLAUSE_DECL (*c
) == real_decl
)
36997 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36999 *c
= OMP_CLAUSE_CHAIN (*c
);
37000 if (code
== OMP_SIMD
)
37002 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37003 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
37007 OMP_CLAUSE_CHAIN (l
) = clauses
;
37010 add_private_clause
= NULL_TREE
;
37014 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
37015 && OMP_CLAUSE_DECL (*c
) == real_decl
)
37016 add_private_clause
= NULL_TREE
;
37017 c
= &OMP_CLAUSE_CHAIN (*c
);
37021 if (add_private_clause
)
37024 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37026 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
37027 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
37028 && OMP_CLAUSE_DECL (c
) == decl
)
37030 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
37031 && OMP_CLAUSE_DECL (c
) == decl
)
37032 error_at (loc
, "iteration variable %qD "
37033 "should not be firstprivate",
37035 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
37036 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
37037 && OMP_CLAUSE_DECL (c
) == decl
)
37038 error_at (loc
, "iteration variable %qD should not be reduction",
37043 if (code
!= OMP_SIMD
)
37044 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
37045 else if (collapse
== 1)
37046 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
37048 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
37049 OMP_CLAUSE_DECL (c
) = add_private_clause
;
37050 c
= finish_omp_clauses (c
, C_ORT_OMP
);
37053 OMP_CLAUSE_CHAIN (c
) = clauses
;
37055 /* For linear, signal that we need to fill up
37056 the so far unknown linear step. */
37057 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
37058 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
37064 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
37065 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
37066 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
37069 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
37071 /* If decl is an iterator, preserve the operator on decl
37072 until finish_omp_for. */
37074 && ((processing_template_decl
37075 && (TREE_TYPE (real_decl
) == NULL_TREE
37076 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
37077 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
37078 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
37080 incr
= cp_parser_expression (parser
);
37081 if (!EXPR_HAS_LOCATION (incr
))
37082 protected_set_expr_location (incr
, input_location
);
37086 if (!parens
.require_close (parser
))
37087 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37088 /*or_comma=*/false,
37089 /*consume_paren=*/true);
37091 TREE_VEC_ELT (declv
, i
) = decl
;
37092 TREE_VEC_ELT (initv
, i
) = init
;
37093 TREE_VEC_ELT (condv
, i
) = cond
;
37094 TREE_VEC_ELT (incrv
, i
) = incr
;
37097 orig_inits
.safe_grow_cleared (i
+ 1);
37098 orig_inits
[i
] = orig_init
;
37103 orig_declv
= copy_node (declv
);
37104 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
37106 else if (orig_declv
)
37107 TREE_VEC_ELT (orig_declv
, i
) = decl
;
37109 if (i
== count
- 1)
37112 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37113 in between the collapsed for loops to be still considered perfectly
37114 nested. Hopefully the final version clarifies this.
37115 For now handle (multiple) {'s and empty statements. */
37116 cp_parser_parse_tentatively (parser
);
37119 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37121 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
37123 cp_lexer_consume_token (parser
->lexer
);
37126 else if (bracecount
37127 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37128 cp_lexer_consume_token (parser
->lexer
);
37131 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37132 error_at (loc
, "not enough for loops to collapse");
37133 collapse_err
= true;
37134 cp_parser_abort_tentative_parse (parser
);
37142 cp_parser_parse_definitely (parser
);
37143 nbraces
+= bracecount
;
37150 /* Note that we saved the original contents of this flag when we entered
37151 the structured block, and so we don't need to re-save it here. */
37152 parser
->in_statement
= IN_OMP_FOR
;
37154 /* Note that the grammar doesn't call for a structured block here,
37155 though the loop as a whole is a structured block. */
37158 body
= begin_omp_structured_block ();
37159 for (i
= 0; i
< count
; i
++)
37160 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
37161 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
37162 TREE_VEC_ELT (declv
, i
));
37165 body
= push_stmt_list ();
37166 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37168 body
= finish_omp_structured_block (body
);
37170 body
= pop_stmt_list (body
);
37172 if (declv
== NULL_TREE
)
37175 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
37176 incrv
, body
, pre_body
, &orig_inits
, clauses
);
37180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
37182 cp_lexer_consume_token (parser
->lexer
);
37185 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37186 cp_lexer_consume_token (parser
->lexer
);
37191 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37192 "collapsed loops not perfectly nested");
37194 collapse_err
= true;
37195 cp_parser_statement_seq_opt (parser
, NULL
);
37196 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
37201 while (!for_block
->is_empty ())
37203 tree t
= for_block
->pop ();
37204 if (TREE_CODE (t
) == STATEMENT_LIST
)
37205 add_stmt (pop_stmt_list (t
));
37213 /* Helper function for OpenMP parsing, split clauses and call
37214 finish_omp_clauses on each of the set of clauses afterwards. */
37217 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
37218 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
37221 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
37222 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
37224 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
37228 #pragma omp simd simd-clause[optseq] new-line
37231 #define OMP_SIMD_CLAUSE_MASK \
37232 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37244 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37245 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37248 tree clauses
, sb
, ret
;
37250 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37252 strcat (p_name
, " simd");
37253 mask
|= OMP_SIMD_CLAUSE_MASK
;
37255 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37259 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
37260 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
37261 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
37262 OMP_CLAUSE_ORDERED
);
37263 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
37265 error_at (OMP_CLAUSE_LOCATION (c
),
37266 "%<ordered%> clause with parameter may not be specified "
37267 "on %qs construct", p_name
);
37268 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
37272 keep_next_level (true);
37273 sb
= begin_omp_structured_block ();
37274 save
= cp_parser_begin_omp_structured_block (parser
);
37276 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
37278 cp_parser_end_omp_structured_block (parser
, save
);
37279 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37285 #pragma omp for for-clause[optseq] new-line
37289 #pragma omp for simd for-simd-clause[optseq] new-line
37292 #define OMP_FOR_CLAUSE_MASK \
37293 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37304 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
37305 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37308 tree clauses
, sb
, ret
;
37310 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37312 strcat (p_name
, " for");
37313 mask
|= OMP_FOR_CLAUSE_MASK
;
37314 /* parallel for{, simd} disallows nowait clause, but for
37315 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37316 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
37317 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37318 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37319 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37320 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
37322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37324 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37325 const char *p
= IDENTIFIER_POINTER (id
);
37327 if (strcmp (p
, "simd") == 0)
37329 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37330 if (cclauses
== NULL
)
37331 cclauses
= cclauses_buf
;
37333 cp_lexer_consume_token (parser
->lexer
);
37334 if (!flag_openmp
) /* flag_openmp_simd */
37335 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37337 sb
= begin_omp_structured_block ();
37338 save
= cp_parser_begin_omp_structured_block (parser
);
37339 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37341 cp_parser_end_omp_structured_block (parser
, save
);
37342 tree body
= finish_omp_structured_block (sb
);
37345 ret
= make_node (OMP_FOR
);
37346 TREE_TYPE (ret
) = void_type_node
;
37347 OMP_FOR_BODY (ret
) = body
;
37348 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37349 SET_EXPR_LOCATION (ret
, loc
);
37354 if (!flag_openmp
) /* flag_openmp_simd */
37356 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37360 /* Composite distribute parallel for disallows linear clause. */
37361 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37362 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
37364 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37368 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
37369 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37372 keep_next_level (true);
37373 sb
= begin_omp_structured_block ();
37374 save
= cp_parser_begin_omp_structured_block (parser
);
37376 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
37378 cp_parser_end_omp_structured_block (parser
, save
);
37379 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37384 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
37385 omp_clause_mask
, tree
*, bool *);
37388 # pragma omp master new-line
37389 structured-block */
37392 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
37393 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37396 tree clauses
, sb
, ret
;
37398 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37400 strcat (p_name
, " master");
37402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37404 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37405 const char *p
= IDENTIFIER_POINTER (id
);
37407 if (strcmp (p
, "taskloop") == 0)
37409 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37410 if (cclauses
== NULL
)
37411 cclauses
= cclauses_buf
;
37413 cp_lexer_consume_token (parser
->lexer
);
37414 if (!flag_openmp
) /* flag_openmp_simd */
37415 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37417 sb
= begin_omp_structured_block ();
37418 save
= cp_parser_begin_omp_structured_block (parser
);
37419 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37421 cp_parser_end_omp_structured_block (parser
, save
);
37422 tree body
= finish_omp_structured_block (sb
);
37425 return c_finish_omp_master (loc
, body
);
37428 if (!flag_openmp
) /* flag_openmp_simd */
37430 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37436 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37438 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
37441 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37443 return c_finish_omp_master (loc
,
37444 cp_parser_omp_structured_block (parser
, if_p
));
37448 # pragma omp ordered new-line
37452 # pragma omp ordered ordered-clauses new-line
37453 structured-block */
37455 #define OMP_ORDERED_CLAUSE_MASK \
37456 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37459 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37460 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37463 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37464 enum pragma_context context
, bool *if_p
)
37466 location_t loc
= pragma_tok
->location
;
37468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37470 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37471 const char *p
= IDENTIFIER_POINTER (id
);
37473 if (strcmp (p
, "depend") == 0)
37475 if (!flag_openmp
) /* flag_openmp_simd */
37477 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37480 if (context
== pragma_stmt
)
37482 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37483 "%<depend%> clause may only be used in compound "
37485 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37489 = cp_parser_omp_all_clauses (parser
,
37490 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37491 "#pragma omp ordered", pragma_tok
);
37492 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37498 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37499 "#pragma omp ordered", pragma_tok
);
37501 if (!flag_openmp
/* flag_openmp_simd */
37502 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37505 c_finish_omp_ordered (loc
, clauses
,
37506 cp_parser_omp_structured_block (parser
, if_p
));
37513 { section-sequence }
37516 section-directive[opt] structured-block
37517 section-sequence section-directive structured-block */
37520 cp_parser_omp_sections_scope (cp_parser
*parser
)
37522 tree stmt
, substmt
;
37523 bool error_suppress
= false;
37526 matching_braces braces
;
37527 if (!braces
.require_open (parser
))
37530 stmt
= push_stmt_list ();
37532 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37533 != PRAGMA_OMP_SECTION
)
37535 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37536 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37537 add_stmt (substmt
);
37542 tok
= cp_lexer_peek_token (parser
->lexer
);
37543 if (tok
->type
== CPP_CLOSE_BRACE
)
37545 if (tok
->type
== CPP_EOF
)
37548 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37550 cp_lexer_consume_token (parser
->lexer
);
37551 cp_parser_require_pragma_eol (parser
, tok
);
37552 error_suppress
= false;
37554 else if (!error_suppress
)
37556 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37557 error_suppress
= true;
37560 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37561 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37562 add_stmt (substmt
);
37564 braces
.require_close (parser
);
37566 substmt
= pop_stmt_list (stmt
);
37568 stmt
= make_node (OMP_SECTIONS
);
37569 TREE_TYPE (stmt
) = void_type_node
;
37570 OMP_SECTIONS_BODY (stmt
) = substmt
;
37577 # pragma omp sections sections-clause[optseq] newline
37580 #define OMP_SECTIONS_CLAUSE_MASK \
37581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37588 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37589 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37592 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37594 strcat (p_name
, " sections");
37595 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37597 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37599 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37603 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37604 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37607 ret
= cp_parser_omp_sections_scope (parser
);
37609 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37615 # pragma omp parallel parallel-clause[optseq] new-line
37617 # pragma omp parallel for parallel-for-clause[optseq] new-line
37619 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37623 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37624 structured-block */
37626 #define OMP_PARALLEL_CLAUSE_MASK \
37627 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37638 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37639 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37642 tree stmt
, clauses
, block
;
37644 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37646 strcat (p_name
, " parallel");
37647 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37648 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37649 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37650 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37651 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37653 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37655 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37656 if (cclauses
== NULL
)
37657 cclauses
= cclauses_buf
;
37659 cp_lexer_consume_token (parser
->lexer
);
37660 if (!flag_openmp
) /* flag_openmp_simd */
37661 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37663 block
= begin_omp_parallel ();
37664 save
= cp_parser_begin_omp_structured_block (parser
);
37665 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37667 cp_parser_end_omp_structured_block (parser
, save
);
37668 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37670 if (ret
== NULL_TREE
)
37672 OMP_PARALLEL_COMBINED (stmt
) = 1;
37675 /* When combined with distribute, parallel has to be followed by for.
37676 #pragma omp target parallel is allowed though. */
37678 && (mask
& (OMP_CLAUSE_MASK_1
37679 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37681 error_at (loc
, "expected %<for%> after %qs", p_name
);
37682 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37685 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37687 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37688 const char *p
= IDENTIFIER_POINTER (id
);
37689 if (strcmp (p
, "master") == 0)
37691 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37692 cclauses
= cclauses_buf
;
37694 cp_lexer_consume_token (parser
->lexer
);
37695 block
= begin_omp_parallel ();
37696 save
= cp_parser_begin_omp_structured_block (parser
);
37697 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37699 cp_parser_end_omp_structured_block (parser
, save
);
37700 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37702 OMP_PARALLEL_COMBINED (stmt
) = 1;
37703 if (ret
== NULL_TREE
)
37707 else if (!flag_openmp
) /* flag_openmp_simd */
37709 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37712 else if (strcmp (p
, "sections") == 0)
37714 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37715 cclauses
= cclauses_buf
;
37717 cp_lexer_consume_token (parser
->lexer
);
37718 block
= begin_omp_parallel ();
37719 save
= cp_parser_begin_omp_structured_block (parser
);
37720 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37721 cp_parser_end_omp_structured_block (parser
, save
);
37722 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37724 OMP_PARALLEL_COMBINED (stmt
) = 1;
37728 else if (!flag_openmp
) /* flag_openmp_simd */
37730 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37734 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37738 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37739 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37742 block
= begin_omp_parallel ();
37743 save
= cp_parser_begin_omp_structured_block (parser
);
37744 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37745 cp_parser_end_omp_structured_block (parser
, save
);
37746 stmt
= finish_omp_parallel (clauses
, block
);
37751 # pragma omp single single-clause[optseq] new-line
37752 structured-block */
37754 #define OMP_SINGLE_CLAUSE_MASK \
37755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37761 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37763 tree stmt
= make_node (OMP_SINGLE
);
37764 TREE_TYPE (stmt
) = void_type_node
;
37765 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37767 OMP_SINGLE_CLAUSES (stmt
)
37768 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37769 "#pragma omp single", pragma_tok
);
37770 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37772 return add_stmt (stmt
);
37776 # pragma omp task task-clause[optseq] new-line
37777 structured-block */
37779 #define OMP_TASK_CLAUSE_MASK \
37780 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37793 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37795 tree clauses
, block
;
37798 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37799 "#pragma omp task", pragma_tok
);
37800 block
= begin_omp_task ();
37801 save
= cp_parser_begin_omp_structured_block (parser
);
37802 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37803 cp_parser_end_omp_structured_block (parser
, save
);
37804 return finish_omp_task (clauses
, block
);
37808 # pragma omp taskwait new-line
37811 # pragma omp taskwait taskwait-clause[opt] new-line */
37813 #define OMP_TASKWAIT_CLAUSE_MASK \
37814 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37817 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37820 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37821 "#pragma omp taskwait", pragma_tok
);
37825 tree stmt
= make_node (OMP_TASK
);
37826 TREE_TYPE (stmt
) = void_node
;
37827 OMP_TASK_CLAUSES (stmt
) = clauses
;
37828 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37829 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37833 finish_omp_taskwait ();
37837 # pragma omp taskyield new-line */
37840 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37842 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37843 finish_omp_taskyield ();
37847 # pragma omp taskgroup new-line
37851 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37853 #define OMP_TASKGROUP_CLAUSE_MASK \
37854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37857 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37860 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37861 "#pragma omp taskgroup", pragma_tok
);
37862 return c_finish_omp_taskgroup (input_location
,
37863 cp_parser_omp_structured_block (parser
,
37870 # pragma omp threadprivate (variable-list) */
37873 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37877 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37878 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37880 finish_omp_threadprivate (vars
);
37884 # pragma omp cancel cancel-clause[optseq] new-line */
37886 #define OMP_CANCEL_CLAUSE_MASK \
37887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37894 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37896 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37897 "#pragma omp cancel", pragma_tok
);
37898 finish_omp_cancel (clauses
);
37902 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37904 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37911 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37912 enum pragma_context context
)
37915 bool point_seen
= false;
37917 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37919 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37920 const char *p
= IDENTIFIER_POINTER (id
);
37922 if (strcmp (p
, "point") == 0)
37924 cp_lexer_consume_token (parser
->lexer
);
37930 cp_parser_error (parser
, "expected %<point%>");
37931 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37935 if (context
!= pragma_compound
)
37937 if (context
== pragma_stmt
)
37938 error_at (pragma_tok
->location
,
37939 "%<#pragma %s%> may only be used in compound statements",
37940 "omp cancellation point");
37942 cp_parser_error (parser
, "expected declaration specifiers");
37943 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37947 clauses
= cp_parser_omp_all_clauses (parser
,
37948 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37949 "#pragma omp cancellation point",
37951 finish_omp_cancellation_point (clauses
);
37955 #pragma omp distribute distribute-clause[optseq] new-line
37958 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37966 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37967 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37970 tree clauses
, sb
, ret
;
37972 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37974 strcat (p_name
, " distribute");
37975 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
37977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37979 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37980 const char *p
= IDENTIFIER_POINTER (id
);
37982 bool parallel
= false;
37984 if (strcmp (p
, "simd") == 0)
37987 parallel
= strcmp (p
, "parallel") == 0;
37988 if (parallel
|| simd
)
37990 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37991 if (cclauses
== NULL
)
37992 cclauses
= cclauses_buf
;
37993 cp_lexer_consume_token (parser
->lexer
);
37994 if (!flag_openmp
) /* flag_openmp_simd */
37997 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38000 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
38003 sb
= begin_omp_structured_block ();
38004 save
= cp_parser_begin_omp_structured_block (parser
);
38006 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38009 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
38011 cp_parser_end_omp_structured_block (parser
, save
);
38012 tree body
= finish_omp_structured_block (sb
);
38015 ret
= make_node (OMP_DISTRIBUTE
);
38016 TREE_TYPE (ret
) = void_type_node
;
38017 OMP_FOR_BODY (ret
) = body
;
38018 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
38019 SET_EXPR_LOCATION (ret
, loc
);
38024 if (!flag_openmp
) /* flag_openmp_simd */
38026 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38030 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38034 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
38035 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
38038 keep_next_level (true);
38039 sb
= begin_omp_structured_block ();
38040 save
= cp_parser_begin_omp_structured_block (parser
);
38042 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
38044 cp_parser_end_omp_structured_block (parser
, save
);
38045 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
38051 # pragma omp teams teams-clause[optseq] new-line
38052 structured-block */
38054 #define OMP_TEAMS_CLAUSE_MASK \
38055 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38064 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
38065 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38068 tree clauses
, sb
, ret
;
38070 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38072 strcat (p_name
, " teams");
38073 mask
|= OMP_TEAMS_CLAUSE_MASK
;
38075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38077 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38078 const char *p
= IDENTIFIER_POINTER (id
);
38079 if (strcmp (p
, "distribute") == 0)
38081 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38082 if (cclauses
== NULL
)
38083 cclauses
= cclauses_buf
;
38085 cp_lexer_consume_token (parser
->lexer
);
38086 if (!flag_openmp
) /* flag_openmp_simd */
38087 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
38089 keep_next_level (true);
38090 sb
= begin_omp_structured_block ();
38091 save
= cp_parser_begin_omp_structured_block (parser
);
38092 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
38094 cp_parser_end_omp_structured_block (parser
, save
);
38095 tree body
= finish_omp_structured_block (sb
);
38098 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38099 ret
= make_node (OMP_TEAMS
);
38100 TREE_TYPE (ret
) = void_type_node
;
38101 OMP_TEAMS_CLAUSES (ret
) = clauses
;
38102 OMP_TEAMS_BODY (ret
) = body
;
38103 OMP_TEAMS_COMBINED (ret
) = 1;
38104 SET_EXPR_LOCATION (ret
, loc
);
38105 return add_stmt (ret
);
38108 if (!flag_openmp
) /* flag_openmp_simd */
38110 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38114 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38118 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
38119 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38122 tree stmt
= make_node (OMP_TEAMS
);
38123 TREE_TYPE (stmt
) = void_type_node
;
38124 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
38125 keep_next_level (true);
38126 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38127 SET_EXPR_LOCATION (stmt
, loc
);
38129 return add_stmt (stmt
);
38133 # pragma omp target data target-data-clause[optseq] new-line
38134 structured-block */
38136 #define OMP_TARGET_DATA_CLAUSE_MASK \
38137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38143 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38146 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
38147 "#pragma omp target data", pragma_tok
);
38149 for (tree
*pc
= &clauses
; *pc
;)
38151 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38152 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38155 case GOMP_MAP_ALWAYS_TO
:
38156 case GOMP_MAP_FROM
:
38157 case GOMP_MAP_ALWAYS_FROM
:
38158 case GOMP_MAP_TOFROM
:
38159 case GOMP_MAP_ALWAYS_TOFROM
:
38160 case GOMP_MAP_ALLOC
:
38163 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38164 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38165 case GOMP_MAP_ALWAYS_POINTER
:
38169 error_at (OMP_CLAUSE_LOCATION (*pc
),
38170 "%<#pragma omp target data%> with map-type other "
38171 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38172 "on %<map%> clause");
38173 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38176 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
38178 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38184 error_at (pragma_tok
->location
,
38185 "%<#pragma omp target data%> must contain at least "
38186 "one %<map%> or %<use_device_ptr%> clause");
38190 tree stmt
= make_node (OMP_TARGET_DATA
);
38191 TREE_TYPE (stmt
) = void_type_node
;
38192 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
38194 keep_next_level (true);
38195 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38197 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38198 return add_stmt (stmt
);
38202 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38203 structured-block */
38205 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38213 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38214 enum pragma_context context
)
38216 bool data_seen
= false;
38217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38219 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38220 const char *p
= IDENTIFIER_POINTER (id
);
38222 if (strcmp (p
, "data") == 0)
38224 cp_lexer_consume_token (parser
->lexer
);
38230 cp_parser_error (parser
, "expected %<data%>");
38231 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38235 if (context
== pragma_stmt
)
38237 error_at (pragma_tok
->location
,
38238 "%<#pragma %s%> may only be used in compound statements",
38239 "omp target enter data");
38240 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38245 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
38246 "#pragma omp target enter data", pragma_tok
);
38248 for (tree
*pc
= &clauses
; *pc
;)
38250 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38251 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38254 case GOMP_MAP_ALWAYS_TO
:
38255 case GOMP_MAP_ALLOC
:
38258 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38259 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38260 case GOMP_MAP_ALWAYS_POINTER
:
38264 error_at (OMP_CLAUSE_LOCATION (*pc
),
38265 "%<#pragma omp target enter data%> with map-type other "
38266 "than %<to%> or %<alloc%> on %<map%> clause");
38267 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38270 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38276 error_at (pragma_tok
->location
,
38277 "%<#pragma omp target enter data%> must contain at least "
38278 "one %<map%> clause");
38282 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
38283 TREE_TYPE (stmt
) = void_type_node
;
38284 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
38285 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38286 return add_stmt (stmt
);
38290 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38291 structured-block */
38293 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38301 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38302 enum pragma_context context
)
38304 bool data_seen
= false;
38305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38307 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38308 const char *p
= IDENTIFIER_POINTER (id
);
38310 if (strcmp (p
, "data") == 0)
38312 cp_lexer_consume_token (parser
->lexer
);
38318 cp_parser_error (parser
, "expected %<data%>");
38319 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38323 if (context
== pragma_stmt
)
38325 error_at (pragma_tok
->location
,
38326 "%<#pragma %s%> may only be used in compound statements",
38327 "omp target exit data");
38328 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38333 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
38334 "#pragma omp target exit data", pragma_tok
);
38336 for (tree
*pc
= &clauses
; *pc
;)
38338 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38339 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38341 case GOMP_MAP_FROM
:
38342 case GOMP_MAP_ALWAYS_FROM
:
38343 case GOMP_MAP_RELEASE
:
38344 case GOMP_MAP_DELETE
:
38347 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38348 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38349 case GOMP_MAP_ALWAYS_POINTER
:
38353 error_at (OMP_CLAUSE_LOCATION (*pc
),
38354 "%<#pragma omp target exit data%> with map-type other "
38355 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38357 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38360 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38366 error_at (pragma_tok
->location
,
38367 "%<#pragma omp target exit data%> must contain at least "
38368 "one %<map%> clause");
38372 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
38373 TREE_TYPE (stmt
) = void_type_node
;
38374 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
38375 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38376 return add_stmt (stmt
);
38380 # pragma omp target update target-update-clause[optseq] new-line */
38382 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38391 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
38392 enum pragma_context context
)
38394 if (context
== pragma_stmt
)
38396 error_at (pragma_tok
->location
,
38397 "%<#pragma %s%> may only be used in compound statements",
38398 "omp target update");
38399 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38404 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
38405 "#pragma omp target update", pragma_tok
);
38406 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
38407 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
38409 error_at (pragma_tok
->location
,
38410 "%<#pragma omp target update%> must contain at least one "
38411 "%<from%> or %<to%> clauses");
38415 tree stmt
= make_node (OMP_TARGET_UPDATE
);
38416 TREE_TYPE (stmt
) = void_type_node
;
38417 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
38418 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38424 # pragma omp target target-clause[optseq] new-line
38425 structured-block */
38427 #define OMP_TARGET_CLAUSE_MASK \
38428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38439 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
38440 enum pragma_context context
, bool *if_p
)
38442 tree
*pc
= NULL
, stmt
;
38446 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
38448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38450 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38451 const char *p
= IDENTIFIER_POINTER (id
);
38452 enum tree_code ccode
= ERROR_MARK
;
38454 if (strcmp (p
, "teams") == 0)
38456 else if (strcmp (p
, "parallel") == 0)
38457 ccode
= OMP_PARALLEL
;
38458 else if (strcmp (p
, "simd") == 0)
38460 if (ccode
!= ERROR_MARK
)
38462 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38463 char p_name
[sizeof ("#pragma omp target teams distribute "
38464 "parallel for simd")];
38466 cp_lexer_consume_token (parser
->lexer
);
38467 strcpy (p_name
, "#pragma omp target");
38468 if (!flag_openmp
) /* flag_openmp_simd */
38474 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38475 OMP_TARGET_CLAUSE_MASK
,
38479 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38480 OMP_TARGET_CLAUSE_MASK
,
38484 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38485 OMP_TARGET_CLAUSE_MASK
,
38489 gcc_unreachable ();
38491 return stmt
!= NULL_TREE
;
38493 keep_next_level (true);
38494 tree sb
= begin_omp_structured_block (), ret
;
38495 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38499 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38500 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38504 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38505 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38509 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38510 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38514 gcc_unreachable ();
38516 cp_parser_end_omp_structured_block (parser
, save
);
38517 tree body
= finish_omp_structured_block (sb
);
38518 if (ret
== NULL_TREE
)
38520 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38522 /* For combined target teams, ensure the num_teams and
38523 thread_limit clause expressions are evaluated on the host,
38524 before entering the target construct. */
38526 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38527 c
; c
= OMP_CLAUSE_CHAIN (c
))
38528 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38529 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38530 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38532 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38533 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38534 if (expr
== error_mark_node
)
38536 tree tmp
= TARGET_EXPR_SLOT (expr
);
38538 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38539 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38540 OMP_CLAUSE_FIRSTPRIVATE
);
38541 OMP_CLAUSE_DECL (tc
) = tmp
;
38542 OMP_CLAUSE_CHAIN (tc
)
38543 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38544 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38547 tree stmt
= make_node (OMP_TARGET
);
38548 TREE_TYPE (stmt
) = void_type_node
;
38549 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38550 OMP_TARGET_BODY (stmt
) = body
;
38551 OMP_TARGET_COMBINED (stmt
) = 1;
38552 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38554 pc
= &OMP_TARGET_CLAUSES (stmt
);
38555 goto check_clauses
;
38557 else if (!flag_openmp
) /* flag_openmp_simd */
38559 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38562 else if (strcmp (p
, "data") == 0)
38564 cp_lexer_consume_token (parser
->lexer
);
38565 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38568 else if (strcmp (p
, "enter") == 0)
38570 cp_lexer_consume_token (parser
->lexer
);
38571 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38574 else if (strcmp (p
, "exit") == 0)
38576 cp_lexer_consume_token (parser
->lexer
);
38577 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38580 else if (strcmp (p
, "update") == 0)
38582 cp_lexer_consume_token (parser
->lexer
);
38583 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38586 if (!flag_openmp
) /* flag_openmp_simd */
38588 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38592 stmt
= make_node (OMP_TARGET
);
38593 TREE_TYPE (stmt
) = void_type_node
;
38595 OMP_TARGET_CLAUSES (stmt
)
38596 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38597 "#pragma omp target", pragma_tok
);
38598 pc
= &OMP_TARGET_CLAUSES (stmt
);
38599 keep_next_level (true);
38600 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38602 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38608 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38609 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38612 case GOMP_MAP_ALWAYS_TO
:
38613 case GOMP_MAP_FROM
:
38614 case GOMP_MAP_ALWAYS_FROM
:
38615 case GOMP_MAP_TOFROM
:
38616 case GOMP_MAP_ALWAYS_TOFROM
:
38617 case GOMP_MAP_ALLOC
:
38618 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38619 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38620 case GOMP_MAP_ALWAYS_POINTER
:
38623 error_at (OMP_CLAUSE_LOCATION (*pc
),
38624 "%<#pragma omp target%> with map-type other "
38625 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38626 "on %<map%> clause");
38627 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38630 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38636 # pragma acc cache (variable-list) new-line
38640 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38642 tree stmt
, clauses
;
38644 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38645 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38647 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38649 stmt
= make_node (OACC_CACHE
);
38650 TREE_TYPE (stmt
) = void_type_node
;
38651 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38652 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38659 # pragma acc data oacc-data-clause[optseq] new-line
38660 structured-block */
38662 #define OACC_DATA_CLAUSE_MASK \
38663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38672 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38674 tree stmt
, clauses
, block
;
38677 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38678 "#pragma acc data", pragma_tok
);
38680 block
= begin_omp_parallel ();
38681 save
= cp_parser_begin_omp_structured_block (parser
);
38682 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38683 cp_parser_end_omp_structured_block (parser
, save
);
38684 stmt
= finish_oacc_data (clauses
, block
);
38689 # pragma acc host_data <clauses> new-line
38690 structured-block */
38692 #define OACC_HOST_DATA_CLAUSE_MASK \
38693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38696 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38698 tree stmt
, clauses
, block
;
38701 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38702 "#pragma acc host_data", pragma_tok
);
38704 block
= begin_omp_parallel ();
38705 save
= cp_parser_begin_omp_structured_block (parser
);
38706 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38707 cp_parser_end_omp_structured_block (parser
, save
);
38708 stmt
= finish_oacc_host_data (clauses
, block
);
38713 # pragma acc declare oacc-data-clause[optseq] new-line
38716 #define OACC_DECLARE_CLAUSE_MASK \
38717 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38727 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38729 tree clauses
, stmt
;
38730 bool error
= false;
38732 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38733 "#pragma acc declare", pragma_tok
, true);
38736 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38738 error_at (pragma_tok
->location
,
38739 "no valid clauses specified in %<#pragma acc declare%>");
38743 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38745 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38746 tree decl
= OMP_CLAUSE_DECL (t
);
38747 if (!DECL_P (decl
))
38749 error_at (loc
, "array section in %<#pragma acc declare%>");
38753 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38754 switch (OMP_CLAUSE_MAP_KIND (t
))
38756 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38757 case GOMP_MAP_ALLOC
:
38759 case GOMP_MAP_FORCE_DEVICEPTR
:
38760 case GOMP_MAP_DEVICE_RESIDENT
:
38763 case GOMP_MAP_LINK
:
38764 if (!global_bindings_p ()
38765 && (TREE_STATIC (decl
)
38766 || !DECL_EXTERNAL (decl
)))
38769 "%qD must be a global variable in "
38770 "%<#pragma acc declare link%>",
38778 if (global_bindings_p ())
38780 error_at (loc
, "invalid OpenACC clause at file scope");
38784 if (DECL_EXTERNAL (decl
))
38787 "invalid use of %<extern%> variable %qD "
38788 "in %<#pragma acc declare%>", decl
);
38792 else if (TREE_PUBLIC (decl
))
38795 "invalid use of %<global%> variable %qD "
38796 "in %<#pragma acc declare%>", decl
);
38803 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38804 || lookup_attribute ("omp declare target link",
38805 DECL_ATTRIBUTES (decl
)))
38807 error_at (loc
, "variable %qD used more than once with "
38808 "%<#pragma acc declare%>", decl
);
38817 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38818 id
= get_identifier ("omp declare target link");
38820 id
= get_identifier ("omp declare target");
38822 DECL_ATTRIBUTES (decl
)
38823 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38824 if (global_bindings_p ())
38826 symtab_node
*node
= symtab_node::get (decl
);
38829 node
->offloadable
= 1;
38830 if (ENABLE_OFFLOADING
)
38832 g
->have_offload
= true;
38833 if (is_a
<varpool_node
*> (node
))
38834 vec_safe_push (offload_vars
, decl
);
38841 if (error
|| global_bindings_p ())
38844 stmt
= make_node (OACC_DECLARE
);
38845 TREE_TYPE (stmt
) = void_type_node
;
38846 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38847 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38855 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38859 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38861 LOC is the location of the #pragma token.
38864 #define OACC_ENTER_DATA_CLAUSE_MASK \
38865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38871 #define OACC_EXIT_DATA_CLAUSE_MASK \
38872 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38880 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38883 location_t loc
= pragma_tok
->location
;
38884 tree stmt
, clauses
;
38885 const char *p
= "";
38887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38888 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38890 if (strcmp (p
, "data") != 0)
38892 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38893 enter
? "enter" : "exit");
38894 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38898 cp_lexer_consume_token (parser
->lexer
);
38901 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38902 "#pragma acc enter data", pragma_tok
);
38904 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38905 "#pragma acc exit data", pragma_tok
);
38907 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38909 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38910 enter
? "enter" : "exit");
38914 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38915 TREE_TYPE (stmt
) = void_type_node
;
38916 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38917 SET_EXPR_LOCATION (stmt
, loc
);
38923 # pragma acc loop oacc-loop-clause[optseq] new-line
38924 structured-block */
38926 #define OACC_LOOP_CLAUSE_MASK \
38927 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38939 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38940 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38942 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38944 strcat (p_name
, " loop");
38945 mask
|= OACC_LOOP_CLAUSE_MASK
;
38947 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38951 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38953 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38955 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38958 tree block
= begin_omp_structured_block ();
38959 int save
= cp_parser_begin_omp_structured_block (parser
);
38960 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38961 cp_parser_end_omp_structured_block (parser
, save
);
38962 add_stmt (finish_omp_structured_block (block
));
38968 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38973 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38977 #define OACC_KERNELS_CLAUSE_MASK \
38978 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38992 #define OACC_PARALLEL_CLAUSE_MASK \
38993 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39011 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
39012 char *p_name
, bool *if_p
)
39014 omp_clause_mask mask
;
39015 enum tree_code code
;
39016 switch (cp_parser_pragma_kind (pragma_tok
))
39018 case PRAGMA_OACC_KERNELS
:
39019 strcat (p_name
, " kernels");
39020 mask
= OACC_KERNELS_CLAUSE_MASK
;
39021 code
= OACC_KERNELS
;
39023 case PRAGMA_OACC_PARALLEL
:
39024 strcat (p_name
, " parallel");
39025 mask
= OACC_PARALLEL_CLAUSE_MASK
;
39026 code
= OACC_PARALLEL
;
39029 gcc_unreachable ();
39032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39035 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
39036 if (strcmp (p
, "loop") == 0)
39038 cp_lexer_consume_token (parser
->lexer
);
39039 tree block
= begin_omp_parallel ();
39041 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
39043 protected_set_expr_location (stmt
, pragma_tok
->location
);
39044 return finish_omp_construct (code
, block
, clauses
);
39048 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
39050 tree block
= begin_omp_parallel ();
39051 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
39052 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39053 cp_parser_end_omp_structured_block (parser
, save
);
39054 return finish_omp_construct (code
, block
, clauses
);
39058 # pragma acc update oacc-update-clause[optseq] new-line
39061 #define OACC_UPDATE_CLAUSE_MASK \
39062 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39070 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
39072 tree stmt
, clauses
;
39074 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
39075 "#pragma acc update", pragma_tok
);
39077 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
39079 error_at (pragma_tok
->location
,
39080 "%<#pragma acc update%> must contain at least one "
39081 "%<device%> or %<host%> or %<self%> clause");
39085 stmt
= make_node (OACC_UPDATE
);
39086 TREE_TYPE (stmt
) = void_type_node
;
39087 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
39088 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39094 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39096 LOC is the location of the #pragma token.
39099 #define OACC_WAIT_CLAUSE_MASK \
39100 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39103 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
39105 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
39106 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39108 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
39109 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
39111 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
39112 "#pragma acc wait", pragma_tok
);
39114 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
39115 stmt
= finish_expr_stmt (stmt
);
39121 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39123 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39132 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
39133 enum pragma_context context
)
39135 bool first_p
= parser
->omp_declare_simd
== NULL
;
39136 cp_omp_declare_simd_data data
;
39139 data
.error_seen
= false;
39140 data
.fndecl_seen
= false;
39141 data
.tokens
= vNULL
;
39142 data
.clauses
= NULL_TREE
;
39143 /* It is safe to take the address of a local variable; it will only be
39144 used while this scope is live. */
39145 parser
->omp_declare_simd
= &data
;
39148 /* Store away all pragma tokens. */
39149 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39150 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39151 cp_lexer_consume_token (parser
->lexer
);
39152 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39153 parser
->omp_declare_simd
->error_seen
= true;
39154 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39155 struct cp_token_cache
*cp
39156 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39157 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
39161 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39162 cp_parser_pragma (parser
, context
, NULL
);
39165 case pragma_external
:
39166 cp_parser_declaration (parser
);
39168 case pragma_member
:
39169 cp_parser_member_declaration (parser
);
39171 case pragma_objc_icode
:
39172 cp_parser_block_declaration (parser
, /*statement_p=*/false);
39175 cp_parser_declaration_statement (parser
);
39178 if (parser
->omp_declare_simd
39179 && !parser
->omp_declare_simd
->error_seen
39180 && !parser
->omp_declare_simd
->fndecl_seen
)
39181 error_at (pragma_tok
->location
,
39182 "%<#pragma omp declare simd%> not immediately followed by "
39183 "function declaration or definition");
39184 data
.tokens
.release ();
39185 parser
->omp_declare_simd
= NULL
;
39189 /* Finalize #pragma omp declare simd clauses after direct declarator has
39190 been parsed, and put that into "omp declare simd" attribute. */
39193 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
39195 struct cp_token_cache
*ce
;
39196 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
39199 if (!data
->error_seen
&& data
->fndecl_seen
)
39201 error ("%<#pragma omp declare simd%> not immediately followed by "
39202 "a single function declaration or definition");
39203 data
->error_seen
= true;
39205 if (data
->error_seen
)
39208 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
39212 cp_parser_push_lexer_for_tokens (parser
, ce
);
39213 parser
->lexer
->in_pragma
= true;
39214 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39215 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39216 cp_lexer_consume_token (parser
->lexer
);
39217 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
39218 "#pragma omp declare simd", pragma_tok
);
39219 cp_parser_pop_lexer (parser
);
39221 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
39222 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
39223 TREE_CHAIN (c
) = attrs
;
39224 if (processing_template_decl
)
39225 ATTR_IS_DEPENDENT (c
) = 1;
39229 data
->fndecl_seen
= true;
39235 # pragma omp declare target new-line
39236 declarations and definitions
39237 # pragma omp end declare target new-line
39240 # pragma omp declare target ( extended-list ) new-line
39242 # pragma omp declare target declare-target-clauses[seq] new-line */
39244 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39245 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39249 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39251 tree clauses
= NULL_TREE
;
39252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39254 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
39255 "#pragma omp declare target", pragma_tok
);
39256 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39258 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
39260 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39261 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39265 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39266 scope_chain
->omp_declare_target_attribute
++;
39269 if (scope_chain
->omp_declare_target_attribute
)
39270 error_at (pragma_tok
->location
,
39271 "%<#pragma omp declare target%> with clauses in between "
39272 "%<#pragma omp declare target%> without clauses and "
39273 "%<#pragma omp end declare target%>");
39274 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39276 tree t
= OMP_CLAUSE_DECL (c
), id
;
39277 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
39278 tree at2
= lookup_attribute ("omp declare target link",
39279 DECL_ATTRIBUTES (t
));
39280 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
39282 id
= get_identifier ("omp declare target link");
39283 std::swap (at1
, at2
);
39286 id
= get_identifier ("omp declare target");
39289 error_at (OMP_CLAUSE_LOCATION (c
),
39290 "%qD specified both in declare target %<link%> and %<to%>"
39296 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
39297 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
39300 symtab_node
*node
= symtab_node::get (t
);
39303 node
->offloadable
= 1;
39304 if (ENABLE_OFFLOADING
)
39306 g
->have_offload
= true;
39307 if (is_a
<varpool_node
*> (node
))
39308 vec_safe_push (offload_vars
, t
);
39316 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39318 const char *p
= "";
39319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39321 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39322 p
= IDENTIFIER_POINTER (id
);
39324 if (strcmp (p
, "declare") == 0)
39326 cp_lexer_consume_token (parser
->lexer
);
39328 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39330 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39331 p
= IDENTIFIER_POINTER (id
);
39333 if (strcmp (p
, "target") == 0)
39334 cp_lexer_consume_token (parser
->lexer
);
39337 cp_parser_error (parser
, "expected %<target%>");
39338 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39344 cp_parser_error (parser
, "expected %<declare%>");
39345 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39348 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39349 if (!scope_chain
->omp_declare_target_attribute
)
39350 error_at (pragma_tok
->location
,
39351 "%<#pragma omp end declare target%> without corresponding "
39352 "%<#pragma omp declare target%>");
39354 scope_chain
->omp_declare_target_attribute
--;
39357 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39358 expression and optional initializer clause of
39359 #pragma omp declare reduction. We store the expression(s) as
39360 either 3, 6 or 7 special statements inside of the artificial function's
39361 body. The first two statements are DECL_EXPRs for the artificial
39362 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39363 expression that uses those variables.
39364 If there was any INITIALIZER clause, this is followed by further statements,
39365 the fourth and fifth statements are DECL_EXPRs for the artificial
39366 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39367 constructor variant (first token after open paren is not omp_priv),
39368 then the sixth statement is a statement with the function call expression
39369 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39370 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39371 to initialize the OMP_PRIV artificial variable and there is seventh
39372 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39375 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
39377 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
39378 gcc_assert (TYPE_REF_P (type
));
39379 type
= TREE_TYPE (type
);
39380 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
39381 DECL_ARTIFICIAL (omp_out
) = 1;
39382 pushdecl (omp_out
);
39383 add_decl_expr (omp_out
);
39384 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
39385 DECL_ARTIFICIAL (omp_in
) = 1;
39387 add_decl_expr (omp_in
);
39389 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
39391 keep_next_level (true);
39392 tree block
= begin_omp_structured_block ();
39393 combiner
= cp_parser_expression (parser
);
39394 finish_expr_stmt (combiner
);
39395 block
= finish_omp_structured_block (block
);
39398 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
39401 const char *p
= "";
39402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39404 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39405 p
= IDENTIFIER_POINTER (id
);
39408 if (strcmp (p
, "initializer") == 0)
39410 cp_lexer_consume_token (parser
->lexer
);
39411 matching_parens parens
;
39412 if (!parens
.require_open (parser
))
39416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39418 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39419 p
= IDENTIFIER_POINTER (id
);
39422 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
39423 DECL_ARTIFICIAL (omp_priv
) = 1;
39424 pushdecl (omp_priv
);
39425 add_decl_expr (omp_priv
);
39426 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
39427 DECL_ARTIFICIAL (omp_orig
) = 1;
39428 pushdecl (omp_orig
);
39429 add_decl_expr (omp_orig
);
39431 keep_next_level (true);
39432 block
= begin_omp_structured_block ();
39435 if (strcmp (p
, "omp_priv") == 0)
39437 bool is_direct_init
, is_non_constant_init
;
39439 cp_lexer_consume_token (parser
->lexer
);
39440 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
39442 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39443 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
39445 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
39446 == CPP_CLOSE_PAREN
))
39448 finish_omp_structured_block (block
);
39449 error ("invalid initializer clause");
39452 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
39453 &is_non_constant_init
);
39454 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
39455 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
39459 cp_parser_parse_tentatively (parser
);
39460 /* Don't create location wrapper nodes here. */
39461 auto_suppress_location_wrappers sentinel
;
39462 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39463 /*check_dependency_p=*/true,
39464 /*template_p=*/NULL
,
39465 /*declarator_p=*/false,
39466 /*optional_p=*/false);
39467 vec
<tree
, va_gc
> *args
;
39468 if (fn_name
== error_mark_node
39469 || cp_parser_error_occurred (parser
)
39470 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39471 || ((args
= cp_parser_parenthesized_expression_list
39472 (parser
, non_attr
, /*cast_p=*/false,
39473 /*allow_expansion_p=*/true,
39474 /*non_constant_p=*/NULL
)),
39475 cp_parser_error_occurred (parser
)))
39477 finish_omp_structured_block (block
);
39478 cp_parser_abort_tentative_parse (parser
);
39479 cp_parser_error (parser
, "expected id-expression (arguments)");
39484 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39485 if (arg
== omp_priv
39486 || (TREE_CODE (arg
) == ADDR_EXPR
39487 && TREE_OPERAND (arg
, 0) == omp_priv
))
39489 cp_parser_abort_tentative_parse (parser
);
39490 if (arg
== NULL_TREE
)
39491 error ("one of the initializer call arguments should be %<omp_priv%>"
39492 " or %<&omp_priv%>");
39493 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39495 finish_expr_stmt (initializer
);
39498 block
= finish_omp_structured_block (block
);
39499 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39503 add_decl_expr (omp_orig
);
39505 if (!parens
.require_close (parser
))
39509 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39510 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39517 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39518 initializer-clause[opt] new-line
39520 initializer-clause:
39521 initializer (omp_priv initializer)
39522 initializer (function-name (argument-list)) */
39525 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39526 enum pragma_context
)
39528 auto_vec
<tree
> types
;
39529 enum tree_code reduc_code
= ERROR_MARK
;
39530 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39532 cp_token
*first_token
;
39533 cp_token_cache
*cp
;
39537 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39538 p
= obstack_alloc (&declarator_obstack
, 0);
39540 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39543 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39546 reduc_code
= PLUS_EXPR
;
39549 reduc_code
= MULT_EXPR
;
39552 reduc_code
= MINUS_EXPR
;
39555 reduc_code
= BIT_AND_EXPR
;
39558 reduc_code
= BIT_XOR_EXPR
;
39561 reduc_code
= BIT_IOR_EXPR
;
39564 reduc_code
= TRUTH_ANDIF_EXPR
;
39567 reduc_code
= TRUTH_ORIF_EXPR
;
39570 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39573 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39574 "%<|%>, %<&&%>, %<||%> or identifier");
39578 if (reduc_code
!= ERROR_MARK
)
39579 cp_lexer_consume_token (parser
->lexer
);
39581 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39582 if (reduc_id
== error_mark_node
)
39585 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39588 /* Types may not be defined in declare reduction type list. */
39589 const char *saved_message
;
39590 saved_message
= parser
->type_definition_forbidden_message
;
39591 parser
->type_definition_forbidden_message
39592 = G_("types may not be defined in declare reduction type list");
39593 bool saved_colon_corrects_to_scope_p
;
39594 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39595 parser
->colon_corrects_to_scope_p
= false;
39596 bool saved_colon_doesnt_start_class_def_p
;
39597 saved_colon_doesnt_start_class_def_p
39598 = parser
->colon_doesnt_start_class_def_p
;
39599 parser
->colon_doesnt_start_class_def_p
= true;
39603 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39604 type
= cp_parser_type_id (parser
);
39605 if (type
== error_mark_node
)
39607 else if (ARITHMETIC_TYPE_P (type
)
39608 && (orig_reduc_id
== NULL_TREE
39609 || (TREE_CODE (type
) != COMPLEX_TYPE
39610 && (id_equal (orig_reduc_id
, "min")
39611 || id_equal (orig_reduc_id
, "max")))))
39612 error_at (loc
, "predeclared arithmetic type %qT in "
39613 "%<#pragma omp declare reduction%>", type
);
39614 else if (FUNC_OR_METHOD_TYPE_P (type
)
39615 || TREE_CODE (type
) == ARRAY_TYPE
)
39616 error_at (loc
, "function or array type %qT in "
39617 "%<#pragma omp declare reduction%>", type
);
39618 else if (TYPE_REF_P (type
))
39619 error_at (loc
, "reference type %qT in "
39620 "%<#pragma omp declare reduction%>", type
);
39621 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39622 error_at (loc
, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
39623 "type %qT in %<#pragma omp declare reduction%>", type
);
39625 types
.safe_push (type
);
39627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39628 cp_lexer_consume_token (parser
->lexer
);
39633 /* Restore the saved message. */
39634 parser
->type_definition_forbidden_message
= saved_message
;
39635 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39636 parser
->colon_doesnt_start_class_def_p
39637 = saved_colon_doesnt_start_class_def_p
;
39639 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39640 || types
.is_empty ())
39643 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39647 first_token
= cp_lexer_peek_token (parser
->lexer
);
39650 FOR_EACH_VEC_ELT (types
, i
, type
)
39653 = build_function_type_list (void_type_node
,
39654 cp_build_reference_type (type
, false),
39656 tree this_reduc_id
= reduc_id
;
39657 if (!dependent_type_p (type
))
39658 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39659 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39660 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39661 DECL_ARTIFICIAL (fndecl
) = 1;
39662 DECL_EXTERNAL (fndecl
) = 1;
39663 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39664 DECL_IGNORED_P (fndecl
) = 1;
39665 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39666 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39667 DECL_ATTRIBUTES (fndecl
)
39668 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39669 DECL_ATTRIBUTES (fndecl
));
39670 if (processing_template_decl
)
39671 fndecl
= push_template_decl (fndecl
);
39672 bool block_scope
= false;
39673 tree block
= NULL_TREE
;
39674 if (current_function_decl
)
39676 block_scope
= true;
39677 DECL_CONTEXT (fndecl
) = global_namespace
;
39678 if (!processing_template_decl
)
39681 else if (current_class_type
)
39685 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39686 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39687 cp_lexer_consume_token (parser
->lexer
);
39688 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39690 cp
= cp_token_cache_new (first_token
,
39691 cp_lexer_peek_nth_token (parser
->lexer
,
39694 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39695 finish_member_declaration (fndecl
);
39696 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39697 DECL_PENDING_INLINE_P (fndecl
) = 1;
39698 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39703 DECL_CONTEXT (fndecl
) = current_namespace
;
39707 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39709 block
= begin_omp_structured_block ();
39712 cp_parser_push_lexer_for_tokens (parser
, cp
);
39713 parser
->lexer
->in_pragma
= true;
39715 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39718 finish_function (/*inline_p=*/false);
39720 DECL_CONTEXT (fndecl
) = current_function_decl
;
39722 cp_parser_pop_lexer (parser
);
39726 cp_parser_pop_lexer (parser
);
39728 finish_function (/*inline_p=*/false);
39731 DECL_CONTEXT (fndecl
) = current_function_decl
;
39732 block
= finish_omp_structured_block (block
);
39733 if (TREE_CODE (block
) == BIND_EXPR
)
39734 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39735 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39736 DECL_SAVED_TREE (fndecl
) = block
;
39737 if (processing_template_decl
)
39738 add_decl_expr (fndecl
);
39740 cp_check_omp_declare_reduction (fndecl
);
39741 if (cp
== NULL
&& types
.length () > 1)
39742 cp
= cp_token_cache_new (first_token
,
39743 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39744 if (errs
!= errorcount
)
39748 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39751 /* Free any declarators allocated. */
39752 obstack_free (&declarator_obstack
, p
);
39756 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39757 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39758 initializer-clause[opt] new-line
39759 #pragma omp declare target new-line */
39762 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39763 enum pragma_context context
)
39765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39767 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39768 const char *p
= IDENTIFIER_POINTER (id
);
39770 if (strcmp (p
, "simd") == 0)
39772 cp_lexer_consume_token (parser
->lexer
);
39773 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39777 cp_ensure_no_omp_declare_simd (parser
);
39778 if (strcmp (p
, "reduction") == 0)
39780 cp_lexer_consume_token (parser
->lexer
);
39781 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39785 if (!flag_openmp
) /* flag_openmp_simd */
39787 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39790 if (strcmp (p
, "target") == 0)
39792 cp_lexer_consume_token (parser
->lexer
);
39793 cp_parser_omp_declare_target (parser
, pragma_tok
);
39797 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39799 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39804 #pragma omp requires clauses[optseq] new-line */
39807 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39810 enum omp_requires new_req
= (enum omp_requires
) 0;
39812 location_t loc
= pragma_tok
->location
;
39813 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39815 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39816 cp_lexer_consume_token (parser
->lexer
);
39820 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39822 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39823 const char *p
= IDENTIFIER_POINTER (id
);
39824 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39825 enum omp_requires this_req
= (enum omp_requires
) 0;
39827 if (!strcmp (p
, "unified_address"))
39828 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39829 else if (!strcmp (p
, "unified_shared_memory"))
39830 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39831 else if (!strcmp (p
, "dynamic_allocators"))
39832 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39833 else if (!strcmp (p
, "reverse_offload"))
39834 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39835 else if (!strcmp (p
, "atomic_default_mem_order"))
39837 cp_lexer_consume_token (parser
->lexer
);
39839 matching_parens parens
;
39840 if (parens
.require_open (parser
))
39842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39844 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39845 p
= IDENTIFIER_POINTER (id
);
39847 if (!strcmp (p
, "seq_cst"))
39849 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39850 else if (!strcmp (p
, "relaxed"))
39852 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39853 else if (!strcmp (p
, "acq_rel"))
39855 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39859 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39860 "expected %<seq_cst%>, %<relaxed%> or "
39862 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39864 cp_lexer_consume_token (parser
->lexer
);
39867 cp_lexer_consume_token (parser
->lexer
);
39869 if (!parens
.require_close (parser
))
39870 cp_parser_skip_to_closing_parenthesis (parser
,
39871 /*recovering=*/true,
39872 /*or_comma=*/false,
39878 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39886 error_at (cloc
, "expected %<unified_address%>, "
39887 "%<unified_shared_memory%>, "
39888 "%<dynamic_allocators%>, "
39889 "%<reverse_offload%> "
39890 "or %<atomic_default_mem_order%> clause");
39891 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39895 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39896 "supported yet", p
);
39898 cp_lexer_consume_token (parser
->lexer
);
39901 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39903 if ((this_req
& new_req
) != 0)
39904 error_at (cloc
, "too many %qs clauses", p
);
39905 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39906 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39907 error_at (cloc
, "%qs clause used lexically after first "
39908 "target construct or offloading API", p
);
39910 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39912 error_at (cloc
, "too many %qs clauses",
39913 "atomic_default_mem_order");
39914 this_req
= (enum omp_requires
) 0;
39916 else if ((omp_requires_mask
39917 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39919 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39920 " clause in a single compilation unit");
39922 = (enum omp_requires
)
39924 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39926 else if ((omp_requires_mask
39927 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39928 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39929 "lexically after first %<atomic%> construct "
39930 "without memory order clause");
39931 new_req
= (enum omp_requires
) (new_req
| this_req
);
39933 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39939 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39942 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39948 #pragma omp taskloop taskloop-clause[optseq] new-line
39951 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39954 #define OMP_TASKLOOP_CLAUSE_MASK \
39955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39973 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
39974 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39977 tree clauses
, sb
, ret
;
39979 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39981 strcat (p_name
, " taskloop");
39982 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
39983 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39985 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
39986 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
39988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39990 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39991 const char *p
= IDENTIFIER_POINTER (id
);
39993 if (strcmp (p
, "simd") == 0)
39995 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39996 if (cclauses
== NULL
)
39997 cclauses
= cclauses_buf
;
39999 cp_lexer_consume_token (parser
->lexer
);
40000 if (!flag_openmp
) /* flag_openmp_simd */
40001 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40003 sb
= begin_omp_structured_block ();
40004 save
= cp_parser_begin_omp_structured_block (parser
);
40005 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40007 cp_parser_end_omp_structured_block (parser
, save
);
40008 tree body
= finish_omp_structured_block (sb
);
40011 ret
= make_node (OMP_TASKLOOP
);
40012 TREE_TYPE (ret
) = void_type_node
;
40013 OMP_FOR_BODY (ret
) = body
;
40014 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
40015 SET_EXPR_LOCATION (ret
, loc
);
40020 if (!flag_openmp
) /* flag_openmp_simd */
40022 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40026 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40030 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
40031 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
40034 keep_next_level (true);
40035 sb
= begin_omp_structured_block ();
40036 save
= cp_parser_begin_omp_structured_block (parser
);
40038 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
40041 cp_parser_end_omp_structured_block (parser
, save
);
40042 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
40049 # pragma acc routine oacc-routine-clause[optseq] new-line
40050 function-definition
40052 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40055 #define OACC_ROUTINE_CLAUSE_MASK \
40056 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40062 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40063 component, which must resolve to a declared namespace-scope
40064 function. The clauses are either processed directly (for a named
40065 function), or defered until the immediatley following declaration
40069 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
40070 enum pragma_context context
)
40072 gcc_checking_assert (context
== pragma_external
);
40073 /* The checking for "another pragma following this one" in the "no optional
40074 '( name )'" case makes sure that we dont re-enter. */
40075 gcc_checking_assert (parser
->oacc_routine
== NULL
);
40077 cp_oacc_routine_data data
;
40078 data
.error_seen
= false;
40079 data
.fndecl_seen
= false;
40080 data
.tokens
= vNULL
;
40081 data
.clauses
= NULL_TREE
;
40082 data
.loc
= pragma_tok
->location
;
40083 /* It is safe to take the address of a local variable; it will only be
40084 used while this scope is live. */
40085 parser
->oacc_routine
= &data
;
40087 /* Look for optional '( name )'. */
40088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
40090 matching_parens parens
;
40091 parens
.consume_open (parser
); /* '(' */
40093 /* We parse the name as an id-expression. If it resolves to
40094 anything other than a non-overloaded function at namespace
40095 scope, it's an error. */
40096 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40097 tree name
= cp_parser_id_expression (parser
,
40098 /*template_keyword_p=*/false,
40099 /*check_dependency_p=*/false,
40100 /*template_p=*/NULL
,
40101 /*declarator_p=*/false,
40102 /*optional_p=*/false);
40103 tree decl
= (identifier_p (name
)
40104 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
40106 if (name
!= error_mark_node
&& decl
== error_mark_node
)
40107 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
40109 if (decl
== error_mark_node
40110 || !parens
.require_close (parser
))
40112 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40113 parser
->oacc_routine
= NULL
;
40118 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40119 "#pragma acc routine",
40120 cp_lexer_peek_token (parser
->lexer
));
40121 /* The clauses are in reverse order; fix that to make later diagnostic
40122 emission easier. */
40123 data
.clauses
= nreverse (data
.clauses
);
40125 if (decl
&& is_overloaded_fn (decl
)
40126 && (TREE_CODE (decl
) != FUNCTION_DECL
40127 || DECL_FUNCTION_TEMPLATE_P (decl
)))
40129 error_at (name_loc
,
40130 "%<#pragma acc routine%> names a set of overloads");
40131 parser
->oacc_routine
= NULL
;
40135 /* Perhaps we should use the same rule as declarations in different
40137 if (!DECL_NAMESPACE_SCOPE_P (decl
))
40139 error_at (name_loc
,
40140 "%qD does not refer to a namespace scope function", decl
);
40141 parser
->oacc_routine
= NULL
;
40145 if (TREE_CODE (decl
) != FUNCTION_DECL
)
40147 error_at (name_loc
, "%qD does not refer to a function", decl
);
40148 parser
->oacc_routine
= NULL
;
40152 cp_finalize_oacc_routine (parser
, decl
, false);
40153 parser
->oacc_routine
= NULL
;
40155 else /* No optional '( name )'. */
40157 /* Store away all pragma tokens. */
40158 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
40159 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
40160 cp_lexer_consume_token (parser
->lexer
);
40161 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40162 parser
->oacc_routine
->error_seen
= true;
40163 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40164 struct cp_token_cache
*cp
40165 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
40166 parser
->oacc_routine
->tokens
.safe_push (cp
);
40168 /* Emit a helpful diagnostic if there's another pragma following this
40170 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
40172 cp_ensure_no_oacc_routine (parser
);
40173 data
.tokens
.release ();
40174 /* ..., and then just keep going. */
40178 /* We only have to consider the pragma_external case here. */
40179 cp_parser_declaration (parser
);
40180 if (parser
->oacc_routine
40181 && !parser
->oacc_routine
->fndecl_seen
)
40182 cp_ensure_no_oacc_routine (parser
);
40184 parser
->oacc_routine
= NULL
;
40185 data
.tokens
.release ();
40189 /* Finalize #pragma acc routine clauses after direct declarator has
40193 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
40195 struct cp_token_cache
*ce
;
40196 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
40198 if (!data
->error_seen
&& data
->fndecl_seen
)
40200 error_at (data
->loc
,
40201 "%<#pragma acc routine%> not immediately followed by "
40202 "a single function declaration or definition");
40203 data
->error_seen
= true;
40205 if (data
->error_seen
)
40208 gcc_checking_assert (data
->tokens
.length () == 1);
40209 ce
= data
->tokens
[0];
40211 cp_parser_push_lexer_for_tokens (parser
, ce
);
40212 parser
->lexer
->in_pragma
= true;
40213 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
40215 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40216 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
40217 parser
->oacc_routine
->clauses
40218 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40219 "#pragma acc routine", pragma_tok
);
40220 /* The clauses are in reverse order; fix that to make later diagnostic
40221 emission easier. */
40222 parser
->oacc_routine
->clauses
= nreverse (parser
->oacc_routine
->clauses
);
40223 cp_parser_pop_lexer (parser
);
40224 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40230 /* Apply any saved OpenACC routine clauses to a just-parsed
40234 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
40236 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
40238 /* Keep going if we're in error reporting mode. */
40239 if (parser
->oacc_routine
->error_seen
40240 || fndecl
== error_mark_node
)
40243 if (parser
->oacc_routine
->fndecl_seen
)
40245 error_at (parser
->oacc_routine
->loc
,
40246 "%<#pragma acc routine%> not immediately followed by"
40247 " a single function declaration or definition");
40248 parser
->oacc_routine
= NULL
;
40251 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
40253 cp_ensure_no_oacc_routine (parser
);
40258 = oacc_verify_routine_clauses (fndecl
, &parser
->oacc_routine
->clauses
,
40259 parser
->oacc_routine
->loc
,
40260 "#pragma acc routine");
40261 if (compatible
< 0)
40263 parser
->oacc_routine
= NULL
;
40266 if (compatible
> 0)
40271 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
40273 error_at (parser
->oacc_routine
->loc
,
40275 ? G_("%<#pragma acc routine%> must be applied before"
40277 : G_("%<#pragma acc routine%> must be applied before"
40279 parser
->oacc_routine
= NULL
;
40283 /* Set the routine's level of parallelism. */
40284 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
40285 oacc_replace_fn_attrib (fndecl
, dims
);
40287 /* Add an "omp declare target" attribute. */
40288 DECL_ATTRIBUTES (fndecl
)
40289 = tree_cons (get_identifier ("omp declare target"),
40290 parser
->oacc_routine
->clauses
,
40291 DECL_ATTRIBUTES (fndecl
));
40294 /* Don't unset parser->oacc_routine here: we may still need it to
40295 diagnose wrong usage. But, remember that we've used this "#pragma acc
40297 parser
->oacc_routine
->fndecl_seen
= true;
40301 /* Main entry point to OpenMP statement pragmas. */
40304 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40307 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
40308 omp_clause_mask
mask (0);
40310 switch (cp_parser_pragma_kind (pragma_tok
))
40312 case PRAGMA_OACC_ATOMIC
:
40313 cp_parser_omp_atomic (parser
, pragma_tok
);
40315 case PRAGMA_OACC_CACHE
:
40316 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
40318 case PRAGMA_OACC_DATA
:
40319 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
40321 case PRAGMA_OACC_ENTER_DATA
:
40322 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
40324 case PRAGMA_OACC_EXIT_DATA
:
40325 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
40327 case PRAGMA_OACC_HOST_DATA
:
40328 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
40330 case PRAGMA_OACC_KERNELS
:
40331 case PRAGMA_OACC_PARALLEL
:
40332 strcpy (p_name
, "#pragma acc");
40333 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
40336 case PRAGMA_OACC_LOOP
:
40337 strcpy (p_name
, "#pragma acc");
40338 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40341 case PRAGMA_OACC_UPDATE
:
40342 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
40344 case PRAGMA_OACC_WAIT
:
40345 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
40347 case PRAGMA_OMP_ATOMIC
:
40348 cp_parser_omp_atomic (parser
, pragma_tok
);
40350 case PRAGMA_OMP_CRITICAL
:
40351 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
40353 case PRAGMA_OMP_DISTRIBUTE
:
40354 strcpy (p_name
, "#pragma omp");
40355 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
40358 case PRAGMA_OMP_FOR
:
40359 strcpy (p_name
, "#pragma omp");
40360 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
40363 case PRAGMA_OMP_MASTER
:
40364 strcpy (p_name
, "#pragma omp");
40365 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
40368 case PRAGMA_OMP_PARALLEL
:
40369 strcpy (p_name
, "#pragma omp");
40370 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
40373 case PRAGMA_OMP_SECTIONS
:
40374 strcpy (p_name
, "#pragma omp");
40375 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
40377 case PRAGMA_OMP_SIMD
:
40378 strcpy (p_name
, "#pragma omp");
40379 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
40382 case PRAGMA_OMP_SINGLE
:
40383 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
40385 case PRAGMA_OMP_TASK
:
40386 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
40388 case PRAGMA_OMP_TASKGROUP
:
40389 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
40391 case PRAGMA_OMP_TASKLOOP
:
40392 strcpy (p_name
, "#pragma omp");
40393 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40396 case PRAGMA_OMP_TEAMS
:
40397 strcpy (p_name
, "#pragma omp");
40398 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
40402 gcc_unreachable ();
40405 protected_set_expr_location (stmt
, pragma_tok
->location
);
40408 /* Transactional Memory parsing routines. */
40410 /* Parse a transaction attribute.
40416 We use this instead of cp_parser_attributes_opt for transactions to avoid
40417 the pedwarn in C++98 mode. */
40420 cp_parser_txn_attribute_opt (cp_parser
*parser
)
40423 tree attr_name
, attr
= NULL
;
40425 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
40426 return cp_parser_attributes_opt (parser
);
40428 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
40430 cp_lexer_consume_token (parser
->lexer
);
40431 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
40434 token
= cp_lexer_peek_token (parser
->lexer
);
40435 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
40437 token
= cp_lexer_consume_token (parser
->lexer
);
40439 attr_name
= (token
->type
== CPP_KEYWORD
40440 /* For keywords, use the canonical spelling,
40441 not the parsed identifier. */
40442 ? ridpointers
[(int) token
->keyword
]
40444 attr
= build_tree_list (attr_name
, NULL_TREE
);
40447 cp_parser_error (parser
, "expected identifier");
40449 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40451 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40455 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40457 transaction-statement:
40458 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40460 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40464 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
40466 unsigned char old_in
= parser
->in_transaction
;
40467 unsigned char this_in
= 1, new_in
;
40468 enum rid keyword
= token
->keyword
;
40469 tree stmt
, attrs
, noex
;
40471 cp_lexer_consume_token (parser
->lexer
);
40473 if (keyword
== RID_TRANSACTION_RELAXED
40474 || keyword
== RID_SYNCHRONIZED
)
40475 this_in
|= TM_STMT_ATTR_RELAXED
;
40478 attrs
= cp_parser_txn_attribute_opt (parser
);
40480 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40483 /* Parse a noexcept specification. */
40484 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40485 noex
= boolean_true_node
;
40486 else if (keyword
== RID_ATOMIC_CANCEL
)
40488 /* cancel-and-throw is unimplemented. */
40489 sorry ("%<atomic_cancel%>");
40493 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40495 /* Keep track if we're in the lexical scope of an outer transaction. */
40496 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40498 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40500 parser
->in_transaction
= new_in
;
40501 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40502 parser
->in_transaction
= old_in
;
40504 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40509 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40511 transaction-expression:
40512 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40513 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40517 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40519 unsigned char old_in
= parser
->in_transaction
;
40520 unsigned char this_in
= 1;
40524 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40526 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40527 || keyword
== RID_TRANSACTION_RELAXED
);
40531 keyword
== RID_TRANSACTION_RELAXED
40532 ? G_("%<__transaction_relaxed%> without transactional memory "
40534 : G_("%<__transaction_atomic%> without transactional memory "
40535 "support enabled"));
40537 token
= cp_parser_require_keyword (parser
, keyword
,
40538 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40539 : RT_TRANSACTION_RELAXED
));
40540 gcc_assert (token
!= NULL
);
40542 if (keyword
== RID_TRANSACTION_RELAXED
)
40543 this_in
|= TM_STMT_ATTR_RELAXED
;
40545 /* Set this early. This might mean that we allow transaction_cancel in
40546 an expression that we find out later actually has to be a constexpr.
40547 However, we expect that cxx_constant_value will be able to deal with
40548 this; also, if the noexcept has no constexpr, then what we parse next
40549 really is a transaction's body. */
40550 parser
->in_transaction
= this_in
;
40552 /* Parse a noexcept specification. */
40553 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40556 if (!noex
|| !noex_expr
40557 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40559 matching_parens parens
;
40560 parens
.require_open (parser
);
40562 expr
= cp_parser_expression (parser
);
40563 expr
= finish_parenthesized_expr (expr
);
40565 parens
.require_close (parser
);
40569 /* The only expression that is available got parsed for the noexcept
40570 already. noexcept is true then. */
40572 noex
= boolean_true_node
;
40575 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40576 parser
->in_transaction
= old_in
;
40578 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40579 return error_mark_node
;
40581 return (flag_tm
? expr
: error_mark_node
);
40584 /* Parse a function-transaction-block.
40586 function-transaction-block:
40587 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40589 __transaction_atomic txn-attribute[opt] function-try-block
40590 __transaction_relaxed ctor-initializer[opt] function-body
40591 __transaction_relaxed function-try-block
40595 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40597 unsigned char old_in
= parser
->in_transaction
;
40598 unsigned char new_in
= 1;
40599 tree compound_stmt
, stmt
, attrs
;
40602 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40603 || keyword
== RID_TRANSACTION_RELAXED
);
40604 token
= cp_parser_require_keyword (parser
, keyword
,
40605 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40606 : RT_TRANSACTION_RELAXED
));
40607 gcc_assert (token
!= NULL
);
40609 if (keyword
== RID_TRANSACTION_RELAXED
)
40610 new_in
|= TM_STMT_ATTR_RELAXED
;
40613 attrs
= cp_parser_txn_attribute_opt (parser
);
40615 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40618 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40620 parser
->in_transaction
= new_in
;
40622 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40623 cp_parser_function_try_block (parser
);
40625 cp_parser_ctor_initializer_opt_and_function_body
40626 (parser
, /*in_function_try_block=*/false);
40628 parser
->in_transaction
= old_in
;
40630 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40633 /* Parse a __transaction_cancel statement.
40636 __transaction_cancel txn-attribute[opt] ;
40637 __transaction_cancel txn-attribute[opt] throw-expression ;
40639 ??? Cancel and throw is not yet implemented. */
40642 cp_parser_transaction_cancel (cp_parser
*parser
)
40645 bool is_outer
= false;
40648 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40649 RT_TRANSACTION_CANCEL
);
40650 gcc_assert (token
!= NULL
);
40652 attrs
= cp_parser_txn_attribute_opt (parser
);
40654 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40656 /* ??? Parse cancel-and-throw here. */
40658 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40662 error_at (token
->location
, "%<__transaction_cancel%> without "
40663 "transactional memory support enabled");
40664 return error_mark_node
;
40666 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40668 error_at (token
->location
, "%<__transaction_cancel%> within a "
40669 "%<__transaction_relaxed%>");
40670 return error_mark_node
;
40674 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40675 && !is_tm_may_cancel_outer (current_function_decl
))
40677 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40678 "within outer %<__transaction_atomic%>");
40679 error_at (token
->location
,
40680 " or a %<transaction_may_cancel_outer%> function");
40681 return error_mark_node
;
40684 else if (parser
->in_transaction
== 0)
40686 error_at (token
->location
, "%<__transaction_cancel%> not within "
40687 "%<__transaction_atomic%>");
40688 return error_mark_node
;
40691 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40699 static GTY (()) cp_parser
*the_parser
;
40702 /* Special handling for the first token or line in the file. The first
40703 thing in the file might be #pragma GCC pch_preprocess, which loads a
40704 PCH file, which is a GC collection point. So we need to handle this
40705 first pragma without benefit of an existing lexer structure.
40707 Always returns one token to the caller in *FIRST_TOKEN. This is
40708 either the true first token of the file, or the first token after
40709 the initial pragma. */
40712 cp_parser_initial_pragma (cp_token
*first_token
)
40716 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40717 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40720 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40721 if (first_token
->type
== CPP_STRING
)
40723 name
= first_token
->u
.value
;
40725 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40726 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40727 error_at (first_token
->location
,
40728 "junk at end of %<#pragma GCC pch_preprocess%>");
40731 error_at (first_token
->location
, "expected string literal");
40733 /* Skip to the end of the pragma. */
40734 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40735 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40737 /* Now actually load the PCH file. */
40739 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40741 /* Read one more token to return to our caller. We have to do this
40742 after reading the PCH file in, since its pointers have to be
40744 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40747 /* Parse a pragma GCC ivdep. */
40750 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40752 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40756 /* Parse a pragma GCC unroll. */
40758 static unsigned short
40759 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40761 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40762 tree expr
= cp_parser_constant_expression (parser
);
40763 unsigned short unroll
;
40764 expr
= maybe_constant_value (expr
);
40765 HOST_WIDE_INT lunroll
= 0;
40766 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40767 || TREE_CODE (expr
) != INTEGER_CST
40768 || (lunroll
= tree_to_shwi (expr
)) < 0
40769 || lunroll
>= USHRT_MAX
)
40771 error_at (location
, "%<#pragma GCC unroll%> requires an"
40772 " assignment-expression that evaluates to a non-negative"
40773 " integral constant less than %u", USHRT_MAX
);
40778 unroll
= (unsigned short)lunroll
;
40782 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40786 /* Normal parsing of a pragma token. Here we can (and must) use the
40790 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40792 cp_token
*pragma_tok
;
40797 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40798 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40799 parser
->lexer
->in_pragma
= true;
40801 id
= cp_parser_pragma_kind (pragma_tok
);
40802 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40803 cp_ensure_no_omp_declare_simd (parser
);
40806 case PRAGMA_GCC_PCH_PREPROCESS
:
40807 error_at (pragma_tok
->location
,
40808 "%<#pragma GCC pch_preprocess%> must be first");
40811 case PRAGMA_OMP_BARRIER
:
40814 case pragma_compound
:
40815 cp_parser_omp_barrier (parser
, pragma_tok
);
40818 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40819 "used in compound statements", "omp barrier");
40826 case PRAGMA_OMP_DEPOBJ
:
40829 case pragma_compound
:
40830 cp_parser_omp_depobj (parser
, pragma_tok
);
40833 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40834 "used in compound statements", "omp depobj");
40841 case PRAGMA_OMP_FLUSH
:
40844 case pragma_compound
:
40845 cp_parser_omp_flush (parser
, pragma_tok
);
40848 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40849 "used in compound statements", "omp flush");
40856 case PRAGMA_OMP_TASKWAIT
:
40859 case pragma_compound
:
40860 cp_parser_omp_taskwait (parser
, pragma_tok
);
40863 error_at (pragma_tok
->location
,
40864 "%<#pragma %s%> may only be used in compound statements",
40872 case PRAGMA_OMP_TASKYIELD
:
40875 case pragma_compound
:
40876 cp_parser_omp_taskyield (parser
, pragma_tok
);
40879 error_at (pragma_tok
->location
,
40880 "%<#pragma %s%> may only be used in compound statements",
40888 case PRAGMA_OMP_CANCEL
:
40891 case pragma_compound
:
40892 cp_parser_omp_cancel (parser
, pragma_tok
);
40895 error_at (pragma_tok
->location
,
40896 "%<#pragma %s%> may only be used in compound statements",
40904 case PRAGMA_OMP_CANCELLATION_POINT
:
40905 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40908 case PRAGMA_OMP_THREADPRIVATE
:
40909 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40912 case PRAGMA_OMP_DECLARE
:
40913 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40915 case PRAGMA_OACC_DECLARE
:
40916 cp_parser_oacc_declare (parser
, pragma_tok
);
40919 case PRAGMA_OACC_ENTER_DATA
:
40920 if (context
== pragma_stmt
)
40922 error_at (pragma_tok
->location
,
40923 "%<#pragma %s%> may only be used in compound statements",
40927 else if (context
!= pragma_compound
)
40929 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40932 case PRAGMA_OACC_EXIT_DATA
:
40933 if (context
== pragma_stmt
)
40935 error_at (pragma_tok
->location
,
40936 "%<#pragma %s%> may only be used in compound statements",
40940 else if (context
!= pragma_compound
)
40942 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40945 case PRAGMA_OACC_ROUTINE
:
40946 if (context
!= pragma_external
)
40948 error_at (pragma_tok
->location
,
40949 "%<#pragma acc routine%> must be at file scope");
40952 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40955 case PRAGMA_OACC_UPDATE
:
40956 if (context
== pragma_stmt
)
40958 error_at (pragma_tok
->location
,
40959 "%<#pragma %s%> may only be used in compound statements",
40963 else if (context
!= pragma_compound
)
40965 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40968 case PRAGMA_OACC_WAIT
:
40969 if (context
== pragma_stmt
)
40971 error_at (pragma_tok
->location
,
40972 "%<#pragma %s%> may only be used in compound statements",
40976 else if (context
!= pragma_compound
)
40978 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40981 case PRAGMA_OACC_ATOMIC
:
40982 case PRAGMA_OACC_CACHE
:
40983 case PRAGMA_OACC_DATA
:
40984 case PRAGMA_OACC_HOST_DATA
:
40985 case PRAGMA_OACC_KERNELS
:
40986 case PRAGMA_OACC_PARALLEL
:
40987 case PRAGMA_OACC_LOOP
:
40988 case PRAGMA_OMP_ATOMIC
:
40989 case PRAGMA_OMP_CRITICAL
:
40990 case PRAGMA_OMP_DISTRIBUTE
:
40991 case PRAGMA_OMP_FOR
:
40992 case PRAGMA_OMP_MASTER
:
40993 case PRAGMA_OMP_PARALLEL
:
40994 case PRAGMA_OMP_SECTIONS
:
40995 case PRAGMA_OMP_SIMD
:
40996 case PRAGMA_OMP_SINGLE
:
40997 case PRAGMA_OMP_TASK
:
40998 case PRAGMA_OMP_TASKGROUP
:
40999 case PRAGMA_OMP_TASKLOOP
:
41000 case PRAGMA_OMP_TEAMS
:
41001 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41003 stmt
= push_omp_privatization_clauses (false);
41004 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
41005 pop_omp_privatization_clauses (stmt
);
41008 case PRAGMA_OMP_REQUIRES
:
41009 return cp_parser_omp_requires (parser
, pragma_tok
);
41011 case PRAGMA_OMP_ORDERED
:
41012 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41014 stmt
= push_omp_privatization_clauses (false);
41015 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
41016 pop_omp_privatization_clauses (stmt
);
41019 case PRAGMA_OMP_TARGET
:
41020 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41022 stmt
= push_omp_privatization_clauses (false);
41023 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
41024 pop_omp_privatization_clauses (stmt
);
41027 case PRAGMA_OMP_END_DECLARE_TARGET
:
41028 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
41031 case PRAGMA_OMP_SECTION
:
41032 error_at (pragma_tok
->location
,
41033 "%<#pragma omp section%> may only be used in "
41034 "%<#pragma omp sections%> construct");
41039 if (context
== pragma_external
)
41041 error_at (pragma_tok
->location
,
41042 "%<#pragma GCC ivdep%> must be inside a function");
41045 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
41046 unsigned short unroll
;
41047 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41048 if (tok
->type
== CPP_PRAGMA
41049 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
41051 tok
= cp_lexer_consume_token (parser
->lexer
);
41052 unroll
= cp_parser_pragma_unroll (parser
, tok
);
41053 tok
= cp_lexer_peek_token (the_parser
->lexer
);
41057 if (tok
->type
!= CPP_KEYWORD
41058 || (tok
->keyword
!= RID_FOR
41059 && tok
->keyword
!= RID_WHILE
41060 && tok
->keyword
!= RID_DO
))
41062 cp_parser_error (parser
, "for, while or do statement expected");
41065 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
41069 case PRAGMA_UNROLL
:
41071 if (context
== pragma_external
)
41073 error_at (pragma_tok
->location
,
41074 "%<#pragma GCC unroll%> must be inside a function");
41077 const unsigned short unroll
41078 = cp_parser_pragma_unroll (parser
, pragma_tok
);
41080 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41081 if (tok
->type
== CPP_PRAGMA
41082 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
41084 tok
= cp_lexer_consume_token (parser
->lexer
);
41085 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
41086 tok
= cp_lexer_peek_token (the_parser
->lexer
);
41090 if (tok
->type
!= CPP_KEYWORD
41091 || (tok
->keyword
!= RID_FOR
41092 && tok
->keyword
!= RID_WHILE
41093 && tok
->keyword
!= RID_DO
))
41095 cp_parser_error (parser
, "for, while or do statement expected");
41098 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
41103 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
41104 c_invoke_pragma_handler (id
);
41108 cp_parser_error (parser
, "expected declaration specifiers");
41112 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41116 /* The interface the pragma parsers have to the lexer. */
41119 pragma_lex (tree
*value
, location_t
*loc
)
41121 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41122 enum cpp_ttype ret
= tok
->type
;
41124 *value
= tok
->u
.value
;
41126 *loc
= tok
->location
;
41128 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
41130 else if (ret
== CPP_STRING
)
41131 *value
= cp_parser_string_literal (the_parser
, false, false);
41134 if (ret
== CPP_KEYWORD
)
41136 cp_lexer_consume_token (the_parser
->lexer
);
41143 /* External interface. */
41145 /* Parse one entire translation unit. */
41148 c_parse_file (void)
41150 static bool already_called
= false;
41152 if (already_called
)
41153 fatal_error (input_location
,
41154 "inter-module optimizations not implemented for C++");
41155 already_called
= true;
41157 the_parser
= cp_parser_new ();
41158 push_deferring_access_checks (flag_access_control
41159 ? dk_no_deferred
: dk_no_check
);
41160 cp_parser_translation_unit (the_parser
);
41163 finish_translation_unit ();
41166 /* Create an identifier for a generic parameter type (a synthesized
41167 template parameter implied by `auto' or a concept identifier). */
41169 static GTY(()) int generic_parm_count
;
41171 make_generic_type_name ()
41174 sprintf (buf
, "auto:%d", ++generic_parm_count
);
41175 return get_identifier (buf
);
41178 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41179 (creating a new template parameter list if necessary). Returns the newly
41180 created template type parm. */
41183 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
41185 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
41187 /* Before committing to modifying any scope, if we're in an
41188 implicit template scope, and we're trying to synthesize a
41189 constrained parameter, try to find a previous parameter with
41190 the same name. This is the same-type rule for abbreviated
41191 function templates.
41193 NOTE: We can generate implicit parameters when tentatively
41194 parsing a nested name specifier, only to reject that parse
41195 later. However, matching the same template-id as part of a
41196 direct-declarator should generate an identical template
41197 parameter, so this rule will merge them. */
41198 if (parser
->implicit_template_scope
&& constr
)
41200 tree t
= parser
->implicit_template_parms
;
41203 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
41205 tree d
= TREE_VALUE (t
);
41206 if (TREE_CODE (d
) == PARM_DECL
)
41207 /* Return the TEMPLATE_PARM_INDEX. */
41208 d
= DECL_INITIAL (d
);
41211 t
= TREE_CHAIN (t
);
41215 /* We are either continuing a function template that already contains implicit
41216 template parameters, creating a new fully-implicit function template, or
41217 extending an existing explicit function template with implicit template
41220 cp_binding_level
*const entry_scope
= current_binding_level
;
41222 bool become_template
= false;
41223 cp_binding_level
*parent_scope
= 0;
41225 if (parser
->implicit_template_scope
)
41227 gcc_assert (parser
->implicit_template_parms
);
41229 current_binding_level
= parser
->implicit_template_scope
;
41233 /* Roll back to the existing template parameter scope (in the case of
41234 extending an explicit function template) or introduce a new template
41235 parameter scope ahead of the function parameter scope (or class scope
41236 in the case of out-of-line member definitions). The function scope is
41237 added back after template parameter synthesis below. */
41239 cp_binding_level
*scope
= entry_scope
;
41241 while (scope
->kind
== sk_function_parms
)
41243 parent_scope
= scope
;
41244 scope
= scope
->level_chain
;
41246 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
41248 /* If not defining a class, then any class scope is a scope level in
41249 an out-of-line member definition. In this case simply wind back
41250 beyond the first such scope to inject the template parameter list.
41251 Otherwise wind back to the class being defined. The latter can
41252 occur in class member friend declarations such as:
41258 friend void A::foo (auto);
41261 The template parameter list synthesized for the friend declaration
41262 must be injected in the scope of 'B'. This can also occur in
41263 erroneous cases such as:
41269 void B::foo (auto) {}
41272 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41273 but, nevertheless, the template parameter list synthesized for the
41274 declarator should be injected into the scope of 'A' as if the
41275 ill-formed template was specified explicitly. */
41277 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
41279 parent_scope
= scope
;
41280 scope
= scope
->level_chain
;
41284 current_binding_level
= scope
;
41286 if (scope
->kind
!= sk_template_parms
41287 || !function_being_declared_is_template_p (parser
))
41289 /* Introduce a new template parameter list for implicit template
41292 become_template
= true;
41294 parser
->implicit_template_scope
41295 = begin_scope (sk_template_parms
, NULL
);
41297 ++processing_template_decl
;
41299 parser
->fully_implicit_function_template_p
= true;
41300 ++parser
->num_template_parameter_lists
;
41304 /* Synthesize implicit template parameters at the end of the explicit
41305 template parameter list. */
41307 gcc_assert (current_template_parms
);
41309 parser
->implicit_template_scope
= scope
;
41311 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41312 parser
->implicit_template_parms
41313 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
41317 /* Synthesize a new template parameter and track the current template
41318 parameter chain with implicit_template_parms. */
41320 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
41321 tree synth_id
= make_generic_type_name ();
41322 tree synth_tmpl_parm
;
41323 bool non_type
= false;
41325 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
41327 = finish_template_type_parm (class_type_node
, synth_id
);
41328 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
41330 = finish_constrained_template_template_parm (proto
, synth_id
);
41333 synth_tmpl_parm
= copy_decl (proto
);
41334 DECL_NAME (synth_tmpl_parm
) = synth_id
;
41338 // Attach the constraint to the parm before processing.
41339 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
41340 TREE_TYPE (node
) = constr
;
41342 = process_template_parm (parser
->implicit_template_parms
,
41345 /*non_type=*/non_type
,
41346 /*param_pack=*/false);
41348 // Chain the new parameter to the list of implicit parameters.
41349 if (parser
->implicit_template_parms
)
41350 parser
->implicit_template_parms
41351 = TREE_CHAIN (parser
->implicit_template_parms
);
41353 parser
->implicit_template_parms
= new_parm
;
41355 tree new_decl
= get_local_decls ();
41357 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41358 new_decl
= DECL_INITIAL (new_decl
);
41360 /* If creating a fully implicit function template, start the new implicit
41361 template parameter list with this synthesized type, otherwise grow the
41362 current template parameter list. */
41364 if (become_template
)
41366 parent_scope
->level_chain
= current_binding_level
;
41368 tree new_parms
= make_tree_vec (1);
41369 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
41370 current_template_parms
= tree_cons (size_int (processing_template_decl
),
41371 new_parms
, current_template_parms
);
41375 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41376 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
41377 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
41378 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
41381 // If the new parameter was constrained, we need to add that to the
41382 // constraints in the template parameter list.
41383 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
41385 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
41386 reqs
= conjoin_constraints (reqs
, req
);
41387 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
41390 current_binding_level
= entry_scope
;
41395 /* Finish the declaration of a fully implicit function template. Such a
41396 template has no explicit template parameter list so has not been through the
41397 normal template head and tail processing. synthesize_implicit_template_parm
41398 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41399 provided if the declaration is a class member such that its template
41400 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41401 form is returned. Otherwise NULL_TREE is returned. */
41404 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
41406 gcc_assert (parser
->fully_implicit_function_template_p
);
41408 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
41409 && DECL_VIRTUAL_P (member_decl_opt
))
41411 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
41412 "implicit templates may not be %<virtual%>");
41413 DECL_VIRTUAL_P (member_decl_opt
) = false;
41416 if (member_decl_opt
)
41417 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
41418 end_template_decl ();
41420 parser
->fully_implicit_function_template_p
= false;
41421 parser
->implicit_template_parms
= 0;
41422 parser
->implicit_template_scope
= 0;
41423 --parser
->num_template_parameter_lists
;
41425 return member_decl_opt
;
41428 /* Like finish_fully_implicit_template, but to be used in error
41429 recovery, rearranging scopes so that we restore the state we had
41430 before synthesize_implicit_template_parm inserted the implement
41431 template parms scope. */
41434 abort_fully_implicit_template (cp_parser
*parser
)
41436 cp_binding_level
*return_to_scope
= current_binding_level
;
41438 if (parser
->implicit_template_scope
41439 && return_to_scope
!= parser
->implicit_template_scope
)
41441 cp_binding_level
*child
= return_to_scope
;
41442 for (cp_binding_level
*scope
= child
->level_chain
;
41443 scope
!= parser
->implicit_template_scope
;
41444 scope
= child
->level_chain
)
41446 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
41447 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
41448 current_binding_level
= parser
->implicit_template_scope
;
41451 return_to_scope
= return_to_scope
->level_chain
;
41453 finish_fully_implicit_template (parser
, NULL
);
41455 gcc_assert (current_binding_level
== return_to_scope
);
41458 /* Helper function for diagnostics that have complained about things
41459 being used with 'extern "C"' linkage.
41461 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41464 maybe_show_extern_c_location (void)
41466 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
41467 inform (the_parser
->innermost_linkage_specification_location
,
41468 "%<extern \"C\"%> linkage started here");
41471 #include "gt-cp-parser.h"