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. */
4348 vec
<tree
, va_gc
> *args
= make_tree_vector ();
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 release_tree_vector (args
);
4356 return error_mark_node
;
4358 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4359 release_tree_vector (args
);
4363 /* A subroutine of cp_parser_userdef_numeric_literal to
4364 create a char... template parameter pack from a string node. */
4367 make_char_string_pack (tree value
)
4370 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4371 const char *str
= TREE_STRING_POINTER (value
);
4372 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4373 tree argvec
= make_tree_vec (1);
4375 /* Fill in CHARVEC with all of the parameters. */
4376 charvec
= make_tree_vec (len
);
4377 for (i
= 0; i
< len
; ++i
)
4379 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4380 cpp_string in
= { 3, s
};
4381 cpp_string out
= { 0, 0 };
4382 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4384 gcc_assert (out
.len
== 2);
4385 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4389 /* Build the argument packs. */
4390 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4392 TREE_VEC_ELT (argvec
, 0) = argpack
;
4397 /* A subroutine of cp_parser_userdef_numeric_literal to
4398 create a char... template parameter pack from a string node. */
4401 make_string_pack (tree value
)
4404 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4405 const unsigned char *str
4406 = (const unsigned char *) TREE_STRING_POINTER (value
);
4407 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4408 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4409 tree argvec
= make_tree_vec (2);
4411 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4412 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4414 /* First template parm is character type. */
4415 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4417 /* Fill in CHARVEC with all of the parameters. */
4418 charvec
= make_tree_vec (len
);
4419 for (int i
= 0; i
< len
; ++i
)
4420 TREE_VEC_ELT (charvec
, i
)
4421 = double_int_to_tree (str_char_type_node
,
4422 double_int::from_buffer (str
+ i
* sz
, sz
));
4424 /* Build the argument packs. */
4425 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4427 TREE_VEC_ELT (argvec
, 1) = argpack
;
4432 /* Parse a user-defined numeric constant. returns a call to a user-defined
4433 literal operator. */
4436 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4438 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4439 tree literal
= token
->u
.value
;
4440 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4441 tree value
= USERDEF_LITERAL_VALUE (literal
);
4442 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4443 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4444 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4446 vec
<tree
, va_gc
> *args
;
4448 /* Look for a literal operator taking the exact type of numeric argument
4449 as the literal value. */
4450 args
= make_tree_vector ();
4451 vec_safe_push (args
, value
);
4452 decl
= lookup_literal_operator (name
, args
);
4453 if (decl
&& decl
!= error_mark_node
)
4455 result
= finish_call_expr (decl
, &args
, false, true,
4456 tf_warning_or_error
);
4458 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4460 warning_at (token
->location
, OPT_Woverflow
,
4461 "integer literal exceeds range of %qT type",
4462 long_long_unsigned_type_node
);
4467 warning_at (token
->location
, OPT_Woverflow
,
4468 "floating literal exceeds range of %qT type",
4469 long_double_type_node
);
4470 else if (overflow
< 0)
4471 warning_at (token
->location
, OPT_Woverflow
,
4472 "floating literal truncated to zero");
4475 release_tree_vector (args
);
4478 release_tree_vector (args
);
4480 /* If the numeric argument didn't work, look for a raw literal
4481 operator taking a const char* argument consisting of the number
4482 in string format. */
4483 args
= make_tree_vector ();
4484 vec_safe_push (args
, num_string
);
4485 decl
= lookup_literal_operator (name
, args
);
4486 if (decl
&& decl
!= error_mark_node
)
4488 result
= finish_call_expr (decl
, &args
, false, true,
4489 tf_warning_or_error
);
4490 release_tree_vector (args
);
4493 release_tree_vector (args
);
4495 /* If the raw literal didn't work, look for a non-type template
4496 function with parameter pack char.... Call the function with
4497 template parameter characters representing the number. */
4498 args
= make_tree_vector ();
4499 decl
= lookup_literal_operator (name
, args
);
4500 if (decl
&& decl
!= error_mark_node
)
4502 tree tmpl_args
= make_char_string_pack (num_string
);
4503 if (tmpl_args
== NULL_TREE
)
4505 error ("failed to translate literal to execution character set %qT",
4507 return error_mark_node
;
4509 decl
= lookup_template_function (decl
, tmpl_args
);
4510 result
= finish_call_expr (decl
, &args
, false, true,
4511 tf_warning_or_error
);
4512 release_tree_vector (args
);
4516 release_tree_vector (args
);
4518 /* In C++14 the standard library defines complex number suffixes that
4519 conflict with GNU extensions. Prefer them if <complex> is #included. */
4520 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4521 bool i14
= (cxx_dialect
> cxx11
4522 && (id_equal (suffix_id
, "i")
4523 || id_equal (suffix_id
, "if")
4524 || id_equal (suffix_id
, "il")));
4525 diagnostic_t kind
= DK_ERROR
;
4530 tree cxlit
= lookup_qualified_name (std_node
,
4531 get_identifier ("complex_literals"),
4533 if (cxlit
== error_mark_node
)
4535 /* No <complex>, so pedwarn and use GNU semantics. */
4537 opt
= OPT_Wpedantic
;
4542 = emit_diagnostic (kind
, input_location
, opt
,
4543 "unable to find numeric literal operator %qD", name
);
4546 /* Don't inform either. */;
4549 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4550 "(from <complex>) to enable the C++14 user-defined literal "
4553 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4554 "GNU built-in suffix");
4557 inform (token
->location
, "use %<-fext-numeric-literals%> "
4558 "to enable more built-in suffixes");
4560 if (kind
== DK_ERROR
)
4561 value
= error_mark_node
;
4564 /* Use the built-in semantics. */
4566 if (id_equal (suffix_id
, "i"))
4568 if (TREE_CODE (value
) == INTEGER_CST
)
4569 type
= integer_type_node
;
4571 type
= double_type_node
;
4573 else if (id_equal (suffix_id
, "if"))
4574 type
= float_type_node
;
4575 else /* if (id_equal (suffix_id, "il")) */
4576 type
= long_double_type_node
;
4578 value
= build_complex (build_complex_type (type
),
4579 fold_convert (type
, integer_zero_node
),
4580 fold_convert (type
, value
));
4583 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4584 /* Avoid repeated diagnostics. */
4585 token
->u
.value
= value
;
4589 /* Parse a user-defined string constant. Returns a call to a user-defined
4590 literal operator taking a character pointer and the length of the string
4594 cp_parser_userdef_string_literal (tree literal
)
4596 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4597 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4598 tree value
= USERDEF_LITERAL_VALUE (literal
);
4599 int len
= TREE_STRING_LENGTH (value
)
4600 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4603 /* Build up a call to the user-defined operator. */
4604 /* Lookup the name we got back from the id-expression. */
4605 releasing_vec rargs
;
4606 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4607 vec_safe_push (args
, value
);
4608 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4609 decl
= lookup_literal_operator (name
, args
);
4611 if (decl
&& decl
!= error_mark_node
)
4612 return finish_call_expr (decl
, &args
, false, true,
4613 tf_warning_or_error
);
4615 /* Look for a suitable template function, either (C++20) with a single
4616 parameter of class type, or (N3599) with typename parameter CharT and
4617 parameter pack CharT... */
4619 decl
= lookup_literal_operator (name
, args
);
4620 if (decl
&& decl
!= error_mark_node
)
4622 /* Use resolve_nondeduced_context to try to choose one form of template
4624 tree tmpl_args
= make_tree_vec (1);
4625 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4626 decl
= lookup_template_function (decl
, tmpl_args
);
4627 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4632 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4633 res
= resolve_nondeduced_context (decl
, tf_none
);
4637 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4638 TREE_OPERAND (decl
, 1) = tmpl_args
;
4639 return finish_call_expr (decl
, &args
, false, true,
4640 tf_warning_or_error
);
4643 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4644 name
, TREE_TYPE (value
), size_type_node
);
4645 return error_mark_node
;
4649 /* Basic concepts [gram.basic] */
4651 /* Parse a translation-unit.
4654 declaration-seq [opt] */
4657 cp_parser_translation_unit (cp_parser
* parser
)
4659 gcc_checking_assert (!cp_error_declarator
);
4661 /* Create the declarator obstack. */
4662 gcc_obstack_init (&declarator_obstack
);
4663 /* Create the error declarator. */
4664 cp_error_declarator
= make_declarator (cdk_error
);
4665 /* Create the empty parameter list. */
4666 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4668 /* Remember where the base of the declarator obstack lies. */
4669 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4671 bool implicit_extern_c
= false;
4675 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4677 /* If we're entering or exiting a region that's implicitly
4678 extern "C", modify the lang context appropriately. */
4679 if (implicit_extern_c
4680 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4682 implicit_extern_c
= !implicit_extern_c
;
4683 if (implicit_extern_c
)
4684 push_lang_context (lang_name_c
);
4686 pop_lang_context ();
4689 if (token
->type
== CPP_EOF
)
4692 if (token
->type
== CPP_CLOSE_BRACE
)
4694 cp_parser_error (parser
, "expected declaration");
4695 cp_lexer_consume_token (parser
->lexer
);
4696 /* If the next token is now a `;', consume it. */
4697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4698 cp_lexer_consume_token (parser
->lexer
);
4701 cp_parser_toplevel_declaration (parser
);
4704 /* Get rid of the token array; we don't need it any more. */
4705 cp_lexer_destroy (parser
->lexer
);
4706 parser
->lexer
= NULL
;
4708 /* The EOF should have reset this. */
4709 gcc_checking_assert (!implicit_extern_c
);
4711 /* Make sure the declarator obstack was fully cleaned up. */
4712 gcc_assert (obstack_next_free (&declarator_obstack
)
4713 == declarator_obstack_base
);
4716 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4717 decltype context. */
4719 static inline tsubst_flags_t
4720 complain_flags (bool decltype_p
)
4722 tsubst_flags_t complain
= tf_warning_or_error
;
4724 complain
|= tf_decltype
;
4728 /* We're about to parse a collection of statements. If we're currently
4729 parsing tentatively, set up a firewall so that any nested
4730 cp_parser_commit_to_tentative_parse won't affect the current context. */
4732 static cp_token_position
4733 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4735 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4738 cp_parser_parse_tentatively (parser
);
4739 cp_parser_commit_to_topmost_tentative_parse (parser
);
4740 return cp_lexer_token_position (parser
->lexer
, false);
4743 /* We've finished parsing the collection of statements. Wrap up the
4744 firewall and replace the relevant tokens with the parsed form. */
4747 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4753 /* Finish the firewall level. */
4754 cp_parser_parse_definitely (parser
);
4755 /* And remember the result of the parse for when we try again. */
4756 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4757 token
->type
= CPP_PREPARSED_EXPR
;
4758 token
->u
.value
= expr
;
4759 token
->keyword
= RID_MAX
;
4760 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4763 /* Like the above functions, but let the user modify the tokens. Used by
4764 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4765 later parses, so it makes sense to localize the effects of
4766 cp_parser_commit_to_tentative_parse. */
4768 struct tentative_firewall
4773 tentative_firewall (cp_parser
*p
): parser(p
)
4775 /* If we're currently parsing tentatively, start a committed level as a
4776 firewall and then an inner tentative parse. */
4777 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4779 cp_parser_parse_tentatively (parser
);
4780 cp_parser_commit_to_topmost_tentative_parse (parser
);
4781 cp_parser_parse_tentatively (parser
);
4785 ~tentative_firewall()
4789 /* Finish the inner tentative parse and the firewall, propagating any
4790 uncommitted error state to the outer tentative parse. */
4791 bool err
= cp_parser_error_occurred (parser
);
4792 cp_parser_parse_definitely (parser
);
4793 cp_parser_parse_definitely (parser
);
4795 cp_parser_simulate_error (parser
);
4800 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4801 This class is for tracking such a matching pair of symbols.
4802 In particular, it tracks the location of the first token,
4803 so that if the second token is missing, we can highlight the
4804 location of the first token when notifying the user about the
4807 template <typename traits_t
>
4811 /* token_pair's ctor. */
4812 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4814 /* If the next token is the opening symbol for this pair, consume it and
4816 Otherwise, issue an error and return false.
4817 In either case, record the location of the opening token. */
4819 bool require_open (cp_parser
*parser
)
4821 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4822 return cp_parser_require (parser
, traits_t::open_token_type
,
4823 traits_t::required_token_open
);
4826 /* Consume the next token from PARSER, recording its location as
4827 that of the opening token within the pair. */
4829 cp_token
* consume_open (cp_parser
*parser
)
4831 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4832 gcc_assert (tok
->type
== traits_t::open_token_type
);
4833 m_open_loc
= tok
->location
;
4837 /* If the next token is the closing symbol for this pair, consume it
4839 Otherwise, issue an error, highlighting the location of the
4840 corresponding opening token, and return NULL. */
4842 cp_token
*require_close (cp_parser
*parser
) const
4844 return cp_parser_require (parser
, traits_t::close_token_type
,
4845 traits_t::required_token_close
,
4850 location_t m_open_loc
;
4853 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4855 struct matching_paren_traits
4857 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4858 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4859 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4860 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4863 /* "matching_parens" is a token_pair<T> class for tracking matching
4864 pairs of parentheses. */
4866 typedef token_pair
<matching_paren_traits
> matching_parens
;
4868 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4870 struct matching_brace_traits
4872 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4873 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4874 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4875 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4878 /* "matching_braces" is a token_pair<T> class for tracking matching
4881 typedef token_pair
<matching_brace_traits
> matching_braces
;
4884 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4885 enclosing parentheses. */
4888 cp_parser_statement_expr (cp_parser
*parser
)
4890 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4892 /* Consume the '('. */
4893 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4894 matching_parens parens
;
4895 parens
.consume_open (parser
);
4896 /* Start the statement-expression. */
4897 tree expr
= begin_stmt_expr ();
4898 /* Parse the compound-statement. */
4899 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4901 expr
= finish_stmt_expr (expr
, false);
4902 /* Consume the ')'. */
4903 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4904 if (!parens
.require_close (parser
))
4905 cp_parser_skip_to_end_of_statement (parser
);
4907 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4908 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4909 return cp_expr (expr
, combined_loc
);
4912 /* Expressions [gram.expr] */
4914 /* Parse a fold-operator.
4917 - * / % ^ & | = < > << >>
4918 = -= *= /= %= ^= &= |= <<= >>=
4919 == != <= >= && || , .* ->*
4921 This returns the tree code corresponding to the matched operator
4922 as an int. When the current token matches a compound assignment
4923 opertor, the resulting tree code is the negative value of the
4924 non-assignment operator. */
4927 cp_parser_fold_operator (cp_token
*token
)
4929 switch (token
->type
)
4931 case CPP_PLUS
: return PLUS_EXPR
;
4932 case CPP_MINUS
: return MINUS_EXPR
;
4933 case CPP_MULT
: return MULT_EXPR
;
4934 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4935 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4936 case CPP_XOR
: return BIT_XOR_EXPR
;
4937 case CPP_AND
: return BIT_AND_EXPR
;
4938 case CPP_OR
: return BIT_IOR_EXPR
;
4939 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4940 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4942 case CPP_EQ
: return -NOP_EXPR
;
4943 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4944 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4945 case CPP_MULT_EQ
: return -MULT_EXPR
;
4946 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4947 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4948 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4949 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4950 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4951 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4952 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4954 case CPP_EQ_EQ
: return EQ_EXPR
;
4955 case CPP_NOT_EQ
: return NE_EXPR
;
4956 case CPP_LESS
: return LT_EXPR
;
4957 case CPP_GREATER
: return GT_EXPR
;
4958 case CPP_LESS_EQ
: return LE_EXPR
;
4959 case CPP_GREATER_EQ
: return GE_EXPR
;
4961 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4962 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4964 case CPP_COMMA
: return COMPOUND_EXPR
;
4966 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4967 case CPP_DEREF_STAR
: return MEMBER_REF
;
4969 default: return ERROR_MARK
;
4973 /* Returns true if CODE indicates a binary expression, which is not allowed in
4974 the LHS of a fold-expression. More codes will need to be added to use this
4975 function in other contexts. */
4978 is_binary_op (tree_code code
)
4983 case POINTER_PLUS_EXPR
:
4986 case TRUNC_DIV_EXPR
:
4987 case TRUNC_MOD_EXPR
:
5003 case TRUTH_ANDIF_EXPR
:
5004 case TRUTH_ORIF_EXPR
:
5017 /* If the next token is a suitable fold operator, consume it and return as
5018 the function above. */
5021 cp_parser_fold_operator (cp_parser
*parser
)
5023 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5024 int code
= cp_parser_fold_operator (token
);
5025 if (code
!= ERROR_MARK
)
5026 cp_lexer_consume_token (parser
->lexer
);
5030 /* Parse a fold-expression.
5033 ( ... folding-operator cast-expression)
5034 ( cast-expression folding-operator ... )
5035 ( cast-expression folding operator ... folding-operator cast-expression)
5037 Note that the '(' and ')' are matched in primary expression. */
5040 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5047 cp_lexer_consume_token (parser
->lexer
);
5048 int op
= cp_parser_fold_operator (parser
);
5049 if (op
== ERROR_MARK
)
5051 cp_parser_error (parser
, "expected binary operator");
5052 return error_mark_node
;
5055 tree expr
= cp_parser_cast_expression (parser
, false, false,
5057 if (expr
== error_mark_node
)
5058 return error_mark_node
;
5059 return finish_left_unary_fold_expr (expr
, op
);
5062 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5063 int op
= cp_parser_fold_operator (parser
);
5064 if (op
== ERROR_MARK
)
5066 cp_parser_error (parser
, "expected binary operator");
5067 return error_mark_node
;
5070 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5072 cp_parser_error (parser
, "expected ...");
5073 return error_mark_node
;
5075 cp_lexer_consume_token (parser
->lexer
);
5077 /* The operands of a fold-expression are cast-expressions, so binary or
5078 conditional expressions are not allowed. We check this here to avoid
5079 tentative parsing. */
5080 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5081 /* OK, the expression was parenthesized. */;
5082 else if (is_binary_op (TREE_CODE (expr1
)))
5083 error_at (location_of (expr1
),
5084 "binary expression in operand of fold-expression");
5085 else if (TREE_CODE (expr1
) == COND_EXPR
5086 || (REFERENCE_REF_P (expr1
)
5087 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5088 error_at (location_of (expr1
),
5089 "conditional expression in operand of fold-expression");
5092 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5093 return finish_right_unary_fold_expr (expr1
, op
);
5095 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5097 cp_parser_error (parser
, "mismatched operator in fold-expression");
5098 return error_mark_node
;
5100 cp_lexer_consume_token (parser
->lexer
);
5102 // Binary left or right fold.
5103 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5104 if (expr2
== error_mark_node
)
5105 return error_mark_node
;
5106 return finish_binary_fold_expr (expr1
, expr2
, op
);
5109 /* Parse a primary-expression.
5116 lambda-expression (C++11)
5121 ( compound-statement )
5122 __builtin_va_arg ( assignment-expression , type-id )
5123 __builtin_offsetof ( type-id , offsetof-expression )
5126 __has_nothrow_assign ( type-id )
5127 __has_nothrow_constructor ( type-id )
5128 __has_nothrow_copy ( type-id )
5129 __has_trivial_assign ( type-id )
5130 __has_trivial_constructor ( type-id )
5131 __has_trivial_copy ( type-id )
5132 __has_trivial_destructor ( type-id )
5133 __has_virtual_destructor ( type-id )
5134 __is_abstract ( type-id )
5135 __is_base_of ( type-id , type-id )
5136 __is_class ( type-id )
5137 __is_empty ( type-id )
5138 __is_enum ( type-id )
5139 __is_final ( type-id )
5140 __is_literal_type ( type-id )
5141 __is_pod ( type-id )
5142 __is_polymorphic ( type-id )
5143 __is_std_layout ( type-id )
5144 __is_trivial ( type-id )
5145 __is_union ( type-id )
5147 Objective-C++ Extension:
5155 ADDRESS_P is true iff this expression was immediately preceded by
5156 "&" and therefore might denote a pointer-to-member. CAST_P is true
5157 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5158 true iff this expression is a template argument.
5160 Returns a representation of the expression. Upon return, *IDK
5161 indicates what kind of id-expression (if any) was present. */
5164 cp_parser_primary_expression (cp_parser
*parser
,
5167 bool template_arg_p
,
5171 cp_token
*token
= NULL
;
5173 /* Assume the primary expression is not an id-expression. */
5174 *idk
= CP_ID_KIND_NONE
;
5176 /* Peek at the next token. */
5177 token
= cp_lexer_peek_token (parser
->lexer
);
5178 switch ((int) token
->type
)
5187 user-defined-literal */
5194 case CPP_PREPARSED_EXPR
:
5195 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5196 return cp_parser_userdef_numeric_literal (parser
);
5197 token
= cp_lexer_consume_token (parser
->lexer
);
5198 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5200 error_at (token
->location
,
5201 "fixed-point types not supported in C++");
5202 return error_mark_node
;
5204 /* Floating-point literals are only allowed in an integral
5205 constant expression if they are cast to an integral or
5206 enumeration type. */
5207 if (TREE_CODE (token
->u
.value
) == REAL_CST
5208 && parser
->integral_constant_expression_p
5211 /* CAST_P will be set even in invalid code like "int(2.7 +
5212 ...)". Therefore, we have to check that the next token
5213 is sure to end the cast. */
5216 cp_token
*next_token
;
5218 next_token
= cp_lexer_peek_token (parser
->lexer
);
5219 if (/* The comma at the end of an
5220 enumerator-definition. */
5221 next_token
->type
!= CPP_COMMA
5222 /* The curly brace at the end of an enum-specifier. */
5223 && next_token
->type
!= CPP_CLOSE_BRACE
5224 /* The end of a statement. */
5225 && next_token
->type
!= CPP_SEMICOLON
5226 /* The end of the cast-expression. */
5227 && next_token
->type
!= CPP_CLOSE_PAREN
5228 /* The end of an array bound. */
5229 && next_token
->type
!= CPP_CLOSE_SQUARE
5230 /* The closing ">" in a template-argument-list. */
5231 && (next_token
->type
!= CPP_GREATER
5232 || parser
->greater_than_is_operator_p
)
5233 /* C++0x only: A ">>" treated like two ">" tokens,
5234 in a template-argument-list. */
5235 && (next_token
->type
!= CPP_RSHIFT
5236 || (cxx_dialect
== cxx98
)
5237 || parser
->greater_than_is_operator_p
))
5241 /* If we are within a cast, then the constraint that the
5242 cast is to an integral or enumeration type will be
5243 checked at that point. If we are not within a cast, then
5244 this code is invalid. */
5246 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5248 return (cp_expr (token
->u
.value
, token
->location
)
5249 .maybe_add_location_wrapper ());
5251 case CPP_CHAR_USERDEF
:
5252 case CPP_CHAR16_USERDEF
:
5253 case CPP_CHAR32_USERDEF
:
5254 case CPP_WCHAR_USERDEF
:
5255 case CPP_UTF8CHAR_USERDEF
:
5256 return cp_parser_userdef_char_literal (parser
);
5262 case CPP_UTF8STRING
:
5263 case CPP_STRING_USERDEF
:
5264 case CPP_STRING16_USERDEF
:
5265 case CPP_STRING32_USERDEF
:
5266 case CPP_WSTRING_USERDEF
:
5267 case CPP_UTF8STRING_USERDEF
:
5268 /* ??? Should wide strings be allowed when parser->translate_strings_p
5269 is false (i.e. in attributes)? If not, we can kill the third
5270 argument to cp_parser_string_literal. */
5271 return (cp_parser_string_literal (parser
,
5272 parser
->translate_strings_p
,
5274 .maybe_add_location_wrapper ());
5276 case CPP_OPEN_PAREN
:
5277 /* If we see `( { ' then we are looking at the beginning of
5278 a GNU statement-expression. */
5279 if (cp_parser_allow_gnu_extensions_p (parser
)
5280 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5282 /* Statement-expressions are not allowed by the standard. */
5283 pedwarn (token
->location
, OPT_Wpedantic
,
5284 "ISO C++ forbids braced-groups within expressions");
5286 /* And they're not allowed outside of a function-body; you
5287 cannot, for example, write:
5289 int i = ({ int j = 3; j + 1; });
5291 at class or namespace scope. */
5292 if (!parser
->in_function_body
5293 || parser
->in_template_argument_list_p
)
5295 error_at (token
->location
,
5296 "statement-expressions are not allowed outside "
5297 "functions nor in template-argument lists");
5298 cp_parser_skip_to_end_of_block_or_statement (parser
);
5299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5300 cp_lexer_consume_token (parser
->lexer
);
5301 return error_mark_node
;
5304 return cp_parser_statement_expr (parser
);
5306 /* Otherwise it's a normal parenthesized expression. */
5309 bool saved_greater_than_is_operator_p
;
5311 location_t open_paren_loc
= token
->location
;
5313 /* Consume the `('. */
5314 matching_parens parens
;
5315 parens
.consume_open (parser
);
5316 /* Within a parenthesized expression, a `>' token is always
5317 the greater-than operator. */
5318 saved_greater_than_is_operator_p
5319 = parser
->greater_than_is_operator_p
;
5320 parser
->greater_than_is_operator_p
= true;
5322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5323 /* Left fold expression. */
5326 /* Parse the parenthesized expression. */
5327 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5329 token
= cp_lexer_peek_token (parser
->lexer
);
5330 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5332 expr
= cp_parser_fold_expression (parser
, expr
);
5333 if (expr
!= error_mark_node
5334 && cxx_dialect
< cxx17
5335 && !in_system_header_at (input_location
))
5336 pedwarn (input_location
, 0, "fold-expressions only available "
5337 "with %<-std=c++17%> or %<-std=gnu++17%>");
5340 /* Let the front end know that this expression was
5341 enclosed in parentheses. This matters in case, for
5342 example, the expression is of the form `A::B', since
5343 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5345 expr
= finish_parenthesized_expr (expr
);
5347 /* DR 705: Wrapping an unqualified name in parentheses
5348 suppresses arg-dependent lookup. We want to pass back
5349 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5350 (c++/37862), but none of the others. */
5351 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5352 *idk
= CP_ID_KIND_NONE
;
5354 /* The `>' token might be the end of a template-id or
5355 template-parameter-list now. */
5356 parser
->greater_than_is_operator_p
5357 = saved_greater_than_is_operator_p
;
5359 /* Consume the `)'. */
5360 token
= cp_lexer_peek_token (parser
->lexer
);
5361 location_t close_paren_loc
= token
->location
;
5362 expr
.set_range (open_paren_loc
, close_paren_loc
);
5363 if (!parens
.require_close (parser
)
5364 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5365 cp_parser_skip_to_end_of_statement (parser
);
5370 case CPP_OPEN_SQUARE
:
5372 if (c_dialect_objc ())
5374 /* We might have an Objective-C++ message. */
5375 cp_parser_parse_tentatively (parser
);
5376 tree msg
= cp_parser_objc_message_expression (parser
);
5377 /* If that works out, we're done ... */
5378 if (cp_parser_parse_definitely (parser
))
5380 /* ... else, fall though to see if it's a lambda. */
5382 cp_expr lam
= cp_parser_lambda_expression (parser
);
5383 /* Don't warn about a failed tentative parse. */
5384 if (cp_parser_error_occurred (parser
))
5385 return error_mark_node
;
5386 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5390 case CPP_OBJC_STRING
:
5391 if (c_dialect_objc ())
5392 /* We have an Objective-C++ string literal. */
5393 return cp_parser_objc_expression (parser
);
5394 cp_parser_error (parser
, "expected primary-expression");
5395 return error_mark_node
;
5398 switch (token
->keyword
)
5400 /* These two are the boolean literals. */
5402 cp_lexer_consume_token (parser
->lexer
);
5403 return cp_expr (boolean_true_node
, token
->location
);
5405 cp_lexer_consume_token (parser
->lexer
);
5406 return cp_expr (boolean_false_node
, token
->location
);
5408 /* The `__null' literal. */
5410 cp_lexer_consume_token (parser
->lexer
);
5411 return cp_expr (null_node
, token
->location
);
5413 /* The `nullptr' literal. */
5415 cp_lexer_consume_token (parser
->lexer
);
5416 return cp_expr (nullptr_node
, token
->location
);
5418 /* Recognize the `this' keyword. */
5420 cp_lexer_consume_token (parser
->lexer
);
5421 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5423 error_at (token
->location
,
5424 "%<this%> may not be used in this context");
5425 return error_mark_node
;
5427 /* Pointers cannot appear in constant-expressions. */
5428 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5429 return error_mark_node
;
5430 return cp_expr (finish_this_expr (), token
->location
);
5432 /* The `operator' keyword can be the beginning of an
5437 case RID_FUNCTION_NAME
:
5438 case RID_PRETTY_FUNCTION_NAME
:
5439 case RID_C99_FUNCTION_NAME
:
5441 non_integral_constant name
;
5443 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5444 __func__ are the names of variables -- but they are
5445 treated specially. Therefore, they are handled here,
5446 rather than relying on the generic id-expression logic
5447 below. Grammatically, these names are id-expressions.
5449 Consume the token. */
5450 token
= cp_lexer_consume_token (parser
->lexer
);
5452 switch (token
->keyword
)
5454 case RID_FUNCTION_NAME
:
5455 name
= NIC_FUNC_NAME
;
5457 case RID_PRETTY_FUNCTION_NAME
:
5458 name
= NIC_PRETTY_FUNC
;
5460 case RID_C99_FUNCTION_NAME
:
5461 name
= NIC_C99_FUNC
;
5467 if (cp_parser_non_integral_constant_expression (parser
, name
))
5468 return error_mark_node
;
5470 /* Look up the name. */
5471 return finish_fname (token
->u
.value
);
5478 location_t type_location
;
5479 location_t start_loc
5480 = cp_lexer_peek_token (parser
->lexer
)->location
;
5481 /* The `__builtin_va_arg' construct is used to handle
5482 `va_arg'. Consume the `__builtin_va_arg' token. */
5483 cp_lexer_consume_token (parser
->lexer
);
5484 /* Look for the opening `('. */
5485 matching_parens parens
;
5486 parens
.require_open (parser
);
5487 /* Now, parse the assignment-expression. */
5488 expression
= cp_parser_assignment_expression (parser
);
5489 /* Look for the `,'. */
5490 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5491 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5492 /* Parse the type-id. */
5494 type_id_in_expr_sentinel
s (parser
);
5495 type
= cp_parser_type_id (parser
);
5497 /* Look for the closing `)'. */
5498 location_t finish_loc
5499 = cp_lexer_peek_token (parser
->lexer
)->location
;
5500 parens
.require_close (parser
);
5501 /* Using `va_arg' in a constant-expression is not
5503 if (cp_parser_non_integral_constant_expression (parser
,
5505 return error_mark_node
;
5506 /* Construct a location of the form:
5507 __builtin_va_arg (v, int)
5508 ~~~~~~~~~~~~~~~~~~~~~^~~~
5509 with the caret at the type, ranging from the start of the
5510 "__builtin_va_arg" token to the close paren. */
5511 location_t combined_loc
5512 = make_location (type_location
, start_loc
, finish_loc
);
5513 return build_x_va_arg (combined_loc
, expression
, type
);
5517 return cp_parser_builtin_offsetof (parser
);
5519 case RID_HAS_NOTHROW_ASSIGN
:
5520 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5521 case RID_HAS_NOTHROW_COPY
:
5522 case RID_HAS_TRIVIAL_ASSIGN
:
5523 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5524 case RID_HAS_TRIVIAL_COPY
:
5525 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5526 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5527 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5528 case RID_IS_ABSTRACT
:
5529 case RID_IS_AGGREGATE
:
5530 case RID_IS_BASE_OF
:
5535 case RID_IS_LITERAL_TYPE
:
5537 case RID_IS_POLYMORPHIC
:
5538 case RID_IS_SAME_AS
:
5539 case RID_IS_STD_LAYOUT
:
5540 case RID_IS_TRIVIAL
:
5541 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5542 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5543 case RID_IS_TRIVIALLY_COPYABLE
:
5545 case RID_IS_ASSIGNABLE
:
5546 case RID_IS_CONSTRUCTIBLE
:
5547 return cp_parser_trait_expr (parser
, token
->keyword
);
5551 return cp_parser_requires_expression (parser
);
5553 /* Objective-C++ expressions. */
5555 case RID_AT_PROTOCOL
:
5556 case RID_AT_SELECTOR
:
5557 return cp_parser_objc_expression (parser
);
5560 if (parser
->in_function_body
5561 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5564 error_at (token
->location
,
5565 "a template declaration cannot appear at block scope");
5566 cp_parser_skip_to_end_of_block_or_statement (parser
);
5567 return error_mark_node
;
5571 cp_parser_error (parser
, "expected primary-expression");
5572 return error_mark_node
;
5575 /* An id-expression can start with either an identifier, a
5576 `::' as the beginning of a qualified-id, or the "operator"
5580 case CPP_TEMPLATE_ID
:
5581 case CPP_NESTED_NAME_SPECIFIER
:
5584 cp_expr id_expression
;
5586 const char *error_msg
;
5589 cp_token
*id_expr_token
;
5591 /* Parse the id-expression. */
5593 = cp_parser_id_expression (parser
,
5594 /*template_keyword_p=*/false,
5595 /*check_dependency_p=*/true,
5597 /*declarator_p=*/false,
5598 /*optional_p=*/false);
5599 if (id_expression
== error_mark_node
)
5600 return error_mark_node
;
5601 id_expr_token
= token
;
5602 token
= cp_lexer_peek_token (parser
->lexer
);
5603 done
= (token
->type
!= CPP_OPEN_SQUARE
5604 && token
->type
!= CPP_OPEN_PAREN
5605 && token
->type
!= CPP_DOT
5606 && token
->type
!= CPP_DEREF
5607 && token
->type
!= CPP_PLUS_PLUS
5608 && token
->type
!= CPP_MINUS_MINUS
);
5609 /* If we have a template-id, then no further lookup is
5610 required. If the template-id was for a template-class, we
5611 will sometimes have a TYPE_DECL at this point. */
5612 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5613 || TREE_CODE (id_expression
) == TYPE_DECL
)
5614 decl
= id_expression
;
5615 /* Look up the name. */
5618 tree ambiguous_decls
;
5620 /* If we already know that this lookup is ambiguous, then
5621 we've already issued an error message; there's no reason
5623 if (id_expr_token
->type
== CPP_NAME
5624 && id_expr_token
->error_reported
)
5626 cp_parser_simulate_error (parser
);
5627 return error_mark_node
;
5630 decl
= cp_parser_lookup_name (parser
, id_expression
,
5633 /*is_namespace=*/false,
5634 /*check_dependency=*/true,
5636 id_expression
.get_location ());
5637 /* If the lookup was ambiguous, an error will already have
5639 if (ambiguous_decls
)
5640 return error_mark_node
;
5642 /* In Objective-C++, we may have an Objective-C 2.0
5643 dot-syntax for classes here. */
5644 if (c_dialect_objc ()
5645 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5646 && TREE_CODE (decl
) == TYPE_DECL
5647 && objc_is_class_name (decl
))
5650 cp_lexer_consume_token (parser
->lexer
);
5651 component
= cp_parser_identifier (parser
);
5652 if (component
== error_mark_node
)
5653 return error_mark_node
;
5655 tree result
= objc_build_class_component_ref (id_expression
,
5657 /* Build a location of the form:
5660 with caret at the start of the component name (at
5661 input_location), ranging from the start of the id_expression
5662 to the end of the component name. */
5663 location_t combined_loc
5664 = make_location (input_location
, id_expression
.get_start (),
5665 get_finish (input_location
));
5666 protected_set_expr_location (result
, combined_loc
);
5670 /* In Objective-C++, an instance variable (ivar) may be preferred
5671 to whatever cp_parser_lookup_name() found.
5672 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5673 rest of c-family, we have to do a little extra work to preserve
5674 any location information in cp_expr "decl". Given that
5675 objc_lookup_ivar is implemented in "c-family" and "objc", we
5676 have a trip through the pure "tree" type, rather than cp_expr.
5677 Naively copying it back to "decl" would implicitly give the
5678 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5679 store an EXPR_LOCATION. Hence we only update "decl" (and
5680 hence its location_t) if we get back a different tree node. */
5681 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5683 if (decl_tree
!= decl
.get_value ())
5684 decl
= cp_expr (decl_tree
);
5686 /* If name lookup gives us a SCOPE_REF, then the
5687 qualifying scope was dependent. */
5688 if (TREE_CODE (decl
) == SCOPE_REF
)
5690 /* At this point, we do not know if DECL is a valid
5691 integral constant expression. We assume that it is
5692 in fact such an expression, so that code like:
5694 template <int N> struct A {
5698 is accepted. At template-instantiation time, we
5699 will check that B<N>::i is actually a constant. */
5702 /* Check to see if DECL is a local variable in a context
5703 where that is forbidden. */
5704 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5705 && local_variable_p (decl
))
5707 error_at (id_expression
.get_location (),
5708 "local variable %qD may not appear in this context",
5710 return error_mark_node
;
5714 decl
= (finish_id_expression
5715 (id_expression
, decl
, parser
->scope
,
5717 parser
->integral_constant_expression_p
,
5718 parser
->allow_non_integral_constant_expression_p
,
5719 &parser
->non_integral_constant_expression_p
,
5720 template_p
, done
, address_p
,
5723 id_expression
.get_location ()));
5725 cp_parser_error (parser
, error_msg
);
5726 /* Build a location for an id-expression of the form:
5732 i.e. from the start of the first token to the end of the final
5733 token, with the caret at the start of the unqualified-id. */
5734 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5735 location_t start_loc
= get_start (id_expr_token
->location
);
5736 location_t finish_loc
= get_finish (id_expression
.get_location ());
5737 location_t combined_loc
5738 = make_location (caret_loc
, start_loc
, finish_loc
);
5740 decl
.set_location (combined_loc
);
5744 /* Anything else is an error. */
5746 cp_parser_error (parser
, "expected primary-expression");
5747 return error_mark_node
;
5751 static inline cp_expr
5752 cp_parser_primary_expression (cp_parser
*parser
,
5755 bool template_arg_p
,
5758 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5759 /*decltype*/false, idk
);
5762 /* Parse an id-expression.
5769 :: [opt] nested-name-specifier template [opt] unqualified-id
5771 :: operator-function-id
5774 Return a representation of the unqualified portion of the
5775 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5776 a `::' or nested-name-specifier.
5778 Often, if the id-expression was a qualified-id, the caller will
5779 want to make a SCOPE_REF to represent the qualified-id. This
5780 function does not do this in order to avoid wastefully creating
5781 SCOPE_REFs when they are not required.
5783 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5786 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5787 uninstantiated templates.
5789 If *TEMPLATE_P is non-NULL, it is set to true iff the
5790 `template' keyword is used to explicitly indicate that the entity
5791 named is a template.
5793 If DECLARATOR_P is true, the id-expression is appearing as part of
5794 a declarator, rather than as part of an expression. */
5797 cp_parser_id_expression (cp_parser
*parser
,
5798 bool template_keyword_p
,
5799 bool check_dependency_p
,
5804 bool global_scope_p
;
5805 bool nested_name_specifier_p
;
5807 /* Assume the `template' keyword was not used. */
5809 *template_p
= template_keyword_p
;
5811 /* Look for the optional `::' operator. */
5813 = (!template_keyword_p
5814 && (cp_parser_global_scope_opt (parser
,
5815 /*current_scope_valid_p=*/false)
5818 /* Look for the optional nested-name-specifier. */
5819 nested_name_specifier_p
5820 = (cp_parser_nested_name_specifier_opt (parser
,
5821 /*typename_keyword_p=*/false,
5828 /* If there is a nested-name-specifier, then we are looking at
5829 the first qualified-id production. */
5830 if (nested_name_specifier_p
)
5833 tree saved_object_scope
;
5834 tree saved_qualifying_scope
;
5835 cp_expr unqualified_id
;
5838 /* See if the next token is the `template' keyword. */
5840 template_p
= &is_template
;
5841 *template_p
= cp_parser_optional_template_keyword (parser
);
5842 /* Name lookup we do during the processing of the
5843 unqualified-id might obliterate SCOPE. */
5844 saved_scope
= parser
->scope
;
5845 saved_object_scope
= parser
->object_scope
;
5846 saved_qualifying_scope
= parser
->qualifying_scope
;
5847 /* Process the final unqualified-id. */
5848 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5851 /*optional_p=*/false);
5852 /* Restore the SAVED_SCOPE for our caller. */
5853 parser
->scope
= saved_scope
;
5854 parser
->object_scope
= saved_object_scope
;
5855 parser
->qualifying_scope
= saved_qualifying_scope
;
5857 return unqualified_id
;
5859 /* Otherwise, if we are in global scope, then we are looking at one
5860 of the other qualified-id productions. */
5861 else if (global_scope_p
)
5866 /* Peek at the next token. */
5867 token
= cp_lexer_peek_token (parser
->lexer
);
5869 /* If it's an identifier, and the next token is not a "<", then
5870 we can avoid the template-id case. This is an optimization
5871 for this common case. */
5872 if (token
->type
== CPP_NAME
5873 && !cp_parser_nth_token_starts_template_argument_list_p
5875 return cp_parser_identifier (parser
);
5877 cp_parser_parse_tentatively (parser
);
5878 /* Try a template-id. */
5879 id
= cp_parser_template_id (parser
,
5880 /*template_keyword_p=*/false,
5881 /*check_dependency_p=*/true,
5884 /* If that worked, we're done. */
5885 if (cp_parser_parse_definitely (parser
))
5888 /* Peek at the next token. (Changes in the token buffer may
5889 have invalidated the pointer obtained above.) */
5890 token
= cp_lexer_peek_token (parser
->lexer
);
5892 switch (token
->type
)
5895 return cp_parser_identifier (parser
);
5898 if (token
->keyword
== RID_OPERATOR
)
5899 return cp_parser_operator_function_id (parser
);
5903 cp_parser_error (parser
, "expected id-expression");
5904 return error_mark_node
;
5908 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5909 /*check_dependency_p=*/true,
5914 /* Parse an unqualified-id.
5918 operator-function-id
5919 conversion-function-id
5923 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5924 keyword, in a construct like `A::template ...'.
5926 Returns a representation of unqualified-id. For the `identifier'
5927 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5928 production a BIT_NOT_EXPR is returned; the operand of the
5929 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5930 other productions, see the documentation accompanying the
5931 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5932 names are looked up in uninstantiated templates. If DECLARATOR_P
5933 is true, the unqualified-id is appearing as part of a declarator,
5934 rather than as part of an expression. */
5937 cp_parser_unqualified_id (cp_parser
* parser
,
5938 bool template_keyword_p
,
5939 bool check_dependency_p
,
5945 /* Peek at the next token. */
5946 token
= cp_lexer_peek_token (parser
->lexer
);
5948 switch ((int) token
->type
)
5954 /* We don't know yet whether or not this will be a
5956 cp_parser_parse_tentatively (parser
);
5957 /* Try a template-id. */
5958 id
= cp_parser_template_id (parser
, template_keyword_p
,
5962 /* If it worked, we're done. */
5963 if (cp_parser_parse_definitely (parser
))
5965 /* Otherwise, it's an ordinary identifier. */
5966 return cp_parser_identifier (parser
);
5969 case CPP_TEMPLATE_ID
:
5970 return cp_parser_template_id (parser
, template_keyword_p
,
5978 tree qualifying_scope
;
5982 location_t tilde_loc
= token
->location
;
5984 /* Consume the `~' token. */
5985 cp_lexer_consume_token (parser
->lexer
);
5986 /* Parse the class-name. The standard, as written, seems to
5989 template <typename T> struct S { ~S (); };
5990 template <typename T> S<T>::~S() {}
5992 is invalid, since `~' must be followed by a class-name, but
5993 `S<T>' is dependent, and so not known to be a class.
5994 That's not right; we need to look in uninstantiated
5995 templates. A further complication arises from:
5997 template <typename T> void f(T t) {
6001 Here, it is not possible to look up `T' in the scope of `T'
6002 itself. We must look in both the current scope, and the
6003 scope of the containing complete expression.
6005 Yet another issue is:
6014 The standard does not seem to say that the `S' in `~S'
6015 should refer to the type `S' and not the data member
6018 /* DR 244 says that we look up the name after the "~" in the
6019 same scope as we looked up the qualifying name. That idea
6020 isn't fully worked out; it's more complicated than that. */
6021 scope
= parser
->scope
;
6022 object_scope
= parser
->object_scope
;
6023 qualifying_scope
= parser
->qualifying_scope
;
6025 /* Check for invalid scopes. */
6026 if (scope
== error_mark_node
)
6028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6029 cp_lexer_consume_token (parser
->lexer
);
6030 return error_mark_node
;
6032 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6034 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6035 error_at (token
->location
,
6036 "scope %qT before %<~%> is not a class-name",
6038 cp_parser_simulate_error (parser
);
6039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6040 cp_lexer_consume_token (parser
->lexer
);
6041 return error_mark_node
;
6043 gcc_assert (!scope
|| TYPE_P (scope
));
6045 token
= cp_lexer_peek_token (parser
->lexer
);
6047 /* Create a location with caret == start at the tilde,
6048 finishing at the end of the peeked token, e.g:
6052 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6054 /* If the name is of the form "X::~X" it's OK even if X is a
6058 && token
->type
== CPP_NAME
6059 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6061 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6062 || (CLASS_TYPE_P (scope
)
6063 && constructor_name_p (token
->u
.value
, scope
))))
6065 cp_lexer_consume_token (parser
->lexer
);
6066 return cp_expr (build_nt (BIT_NOT_EXPR
, scope
), loc
);
6069 /* ~auto means the destructor of whatever the object is. */
6070 if (cp_parser_is_keyword (token
, RID_AUTO
))
6072 if (cxx_dialect
< cxx14
)
6074 "%<~auto%> only available with "
6075 "%<-std=c++14%> or %<-std=gnu++14%>");
6076 cp_lexer_consume_token (parser
->lexer
);
6077 return cp_expr (build_nt (BIT_NOT_EXPR
, make_auto (), loc
));
6080 /* If there was an explicit qualification (S::~T), first look
6081 in the scope given by the qualification (i.e., S).
6083 Note: in the calls to cp_parser_class_name below we pass
6084 typename_type so that lookup finds the injected-class-name
6085 rather than the constructor. */
6087 type_decl
= NULL_TREE
;
6090 cp_parser_parse_tentatively (parser
);
6091 type_decl
= cp_parser_class_name (parser
,
6092 /*typename_keyword_p=*/false,
6093 /*template_keyword_p=*/false,
6095 /*check_dependency=*/false,
6096 /*class_head_p=*/false,
6098 if (cp_parser_parse_definitely (parser
))
6101 /* In "N::S::~S", look in "N" as well. */
6102 if (!done
&& scope
&& qualifying_scope
)
6104 cp_parser_parse_tentatively (parser
);
6105 parser
->scope
= qualifying_scope
;
6106 parser
->object_scope
= NULL_TREE
;
6107 parser
->qualifying_scope
= NULL_TREE
;
6109 = cp_parser_class_name (parser
,
6110 /*typename_keyword_p=*/false,
6111 /*template_keyword_p=*/false,
6113 /*check_dependency=*/false,
6114 /*class_head_p=*/false,
6116 if (cp_parser_parse_definitely (parser
))
6119 /* In "p->S::~T", look in the scope given by "*p" as well. */
6120 else if (!done
&& object_scope
)
6122 cp_parser_parse_tentatively (parser
);
6123 parser
->scope
= object_scope
;
6124 parser
->object_scope
= NULL_TREE
;
6125 parser
->qualifying_scope
= NULL_TREE
;
6127 = cp_parser_class_name (parser
,
6128 /*typename_keyword_p=*/false,
6129 /*template_keyword_p=*/false,
6131 /*check_dependency=*/false,
6132 /*class_head_p=*/false,
6134 if (cp_parser_parse_definitely (parser
))
6137 /* Look in the surrounding context. */
6140 parser
->scope
= NULL_TREE
;
6141 parser
->object_scope
= NULL_TREE
;
6142 parser
->qualifying_scope
= NULL_TREE
;
6143 if (processing_template_decl
)
6144 cp_parser_parse_tentatively (parser
);
6146 = cp_parser_class_name (parser
,
6147 /*typename_keyword_p=*/false,
6148 /*template_keyword_p=*/false,
6150 /*check_dependency=*/false,
6151 /*class_head_p=*/false,
6153 if (processing_template_decl
6154 && ! cp_parser_parse_definitely (parser
))
6156 /* We couldn't find a type with this name. If we're parsing
6157 tentatively, fail and try something else. */
6158 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6160 cp_parser_simulate_error (parser
);
6161 return error_mark_node
;
6163 /* Otherwise, accept it and check for a match at instantiation
6165 type_decl
= cp_parser_identifier (parser
);
6166 if (type_decl
!= error_mark_node
)
6167 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6168 return cp_expr (type_decl
, loc
);
6171 /* If an error occurred, assume that the name of the
6172 destructor is the same as the name of the qualifying
6173 class. That allows us to keep parsing after running
6174 into ill-formed destructor names. */
6175 if (type_decl
== error_mark_node
&& scope
)
6176 return build_nt (BIT_NOT_EXPR
, scope
);
6177 else if (type_decl
== error_mark_node
)
6178 return error_mark_node
;
6180 /* Check that destructor name and scope match. */
6181 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6183 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6185 "declaration of %<~%T%> as member of %qT",
6187 cp_parser_simulate_error (parser
);
6188 return error_mark_node
;
6193 A typedef-name that names a class shall not be used as the
6194 identifier in the declarator for a destructor declaration. */
6196 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6197 && !DECL_SELF_REFERENCE_P (type_decl
)
6198 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6200 "typedef-name %qD used as destructor declarator",
6203 return cp_expr (build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
), loc
));
6207 if (token
->keyword
== RID_OPERATOR
)
6211 /* This could be a template-id, so we try that first. */
6212 cp_parser_parse_tentatively (parser
);
6213 /* Try a template-id. */
6214 id
= cp_parser_template_id (parser
, template_keyword_p
,
6215 /*check_dependency_p=*/true,
6218 /* If that worked, we're done. */
6219 if (cp_parser_parse_definitely (parser
))
6221 /* We still don't know whether we're looking at an
6222 operator-function-id or a conversion-function-id. */
6223 cp_parser_parse_tentatively (parser
);
6224 /* Try an operator-function-id. */
6225 id
= cp_parser_operator_function_id (parser
);
6226 /* If that didn't work, try a conversion-function-id. */
6227 if (!cp_parser_parse_definitely (parser
))
6228 id
= cp_parser_conversion_function_id (parser
);
6237 cp_parser_error (parser
, "expected unqualified-id");
6238 return error_mark_node
;
6242 /* Parse an (optional) nested-name-specifier.
6244 nested-name-specifier: [C++98]
6245 class-or-namespace-name :: nested-name-specifier [opt]
6246 class-or-namespace-name :: template nested-name-specifier [opt]
6248 nested-name-specifier: [C++0x]
6251 nested-name-specifier identifier ::
6252 nested-name-specifier template [opt] simple-template-id ::
6254 PARSER->SCOPE should be set appropriately before this function is
6255 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6256 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6259 Sets PARSER->SCOPE to the class (TYPE) or namespace
6260 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6261 it unchanged if there is no nested-name-specifier. Returns the new
6262 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6264 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6265 part of a declaration and/or decl-specifier. */
6268 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6269 bool typename_keyword_p
,
6270 bool check_dependency_p
,
6272 bool is_declaration
,
6273 bool template_keyword_p
/* = false */)
6275 bool success
= false;
6276 cp_token_position start
= 0;
6279 /* Remember where the nested-name-specifier starts. */
6280 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6282 start
= cp_lexer_token_position (parser
->lexer
, false);
6283 push_deferring_access_checks (dk_deferred
);
6290 tree saved_qualifying_scope
;
6292 /* Spot cases that cannot be the beginning of a
6293 nested-name-specifier. */
6294 token
= cp_lexer_peek_token (parser
->lexer
);
6296 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6297 the already parsed nested-name-specifier. */
6298 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6300 /* Grab the nested-name-specifier and continue the loop. */
6301 cp_parser_pre_parsed_nested_name_specifier (parser
);
6302 /* If we originally encountered this nested-name-specifier
6303 with IS_DECLARATION set to false, we will not have
6304 resolved TYPENAME_TYPEs, so we must do so here. */
6306 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6308 new_scope
= resolve_typename_type (parser
->scope
,
6309 /*only_current_p=*/false);
6310 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6311 parser
->scope
= new_scope
;
6317 /* Spot cases that cannot be the beginning of a
6318 nested-name-specifier. On the second and subsequent times
6319 through the loop, we look for the `template' keyword. */
6320 if (success
&& token
->keyword
== RID_TEMPLATE
)
6322 /* A template-id can start a nested-name-specifier. */
6323 else if (token
->type
== CPP_TEMPLATE_ID
)
6325 /* DR 743: decltype can be used in a nested-name-specifier. */
6326 else if (token_is_decltype (token
))
6330 /* If the next token is not an identifier, then it is
6331 definitely not a type-name or namespace-name. */
6332 if (token
->type
!= CPP_NAME
)
6334 /* If the following token is neither a `<' (to begin a
6335 template-id), nor a `::', then we are not looking at a
6336 nested-name-specifier. */
6337 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6339 if (token
->type
== CPP_COLON
6340 && parser
->colon_corrects_to_scope_p
6341 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6343 gcc_rich_location
richloc (token
->location
);
6344 richloc
.add_fixit_replace ("::");
6346 "found %<:%> in nested-name-specifier, "
6348 token
->type
= CPP_SCOPE
;
6351 if (token
->type
!= CPP_SCOPE
6352 && !cp_parser_nth_token_starts_template_argument_list_p
6357 /* The nested-name-specifier is optional, so we parse
6359 cp_parser_parse_tentatively (parser
);
6361 /* Look for the optional `template' keyword, if this isn't the
6362 first time through the loop. */
6364 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6366 /* Save the old scope since the name lookup we are about to do
6367 might destroy it. */
6368 old_scope
= parser
->scope
;
6369 saved_qualifying_scope
= parser
->qualifying_scope
;
6370 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6371 look up names in "X<T>::I" in order to determine that "Y" is
6372 a template. So, if we have a typename at this point, we make
6373 an effort to look through it. */
6375 && !typename_keyword_p
6377 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6378 parser
->scope
= resolve_typename_type (parser
->scope
,
6379 /*only_current_p=*/false);
6380 /* Parse the qualifying entity. */
6382 = cp_parser_qualifying_entity (parser
,
6388 /* Look for the `::' token. */
6389 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6391 /* If we found what we wanted, we keep going; otherwise, we're
6393 if (!cp_parser_parse_definitely (parser
))
6395 bool error_p
= false;
6397 /* Restore the OLD_SCOPE since it was valid before the
6398 failed attempt at finding the last
6399 class-or-namespace-name. */
6400 parser
->scope
= old_scope
;
6401 parser
->qualifying_scope
= saved_qualifying_scope
;
6403 /* If the next token is a decltype, and the one after that is a
6404 `::', then the decltype has failed to resolve to a class or
6405 enumeration type. Give this error even when parsing
6406 tentatively since it can't possibly be valid--and we're going
6407 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6408 won't get another chance.*/
6409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6410 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6413 token
= cp_lexer_consume_token (parser
->lexer
);
6414 error_at (token
->location
, "decltype evaluates to %qT, "
6415 "which is not a class or enumeration type",
6416 token
->u
.tree_check_value
->value
);
6417 parser
->scope
= error_mark_node
;
6421 cp_lexer_consume_token (parser
->lexer
);
6424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6425 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6427 /* If we have a non-type template-id followed by ::, it can't
6428 possibly be valid. */
6429 token
= cp_lexer_peek_token (parser
->lexer
);
6430 tree tid
= token
->u
.tree_check_value
->value
;
6431 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6432 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6434 tree tmpl
= NULL_TREE
;
6435 if (is_overloaded_fn (tid
))
6437 tree fns
= get_fns (tid
);
6438 if (OVL_SINGLE_P (fns
))
6439 tmpl
= OVL_FIRST (fns
);
6440 error_at (token
->location
, "function template-id %qD "
6441 "in nested-name-specifier", tid
);
6445 /* Variable template. */
6446 tmpl
= TREE_OPERAND (tid
, 0);
6447 gcc_assert (variable_template_p (tmpl
));
6448 error_at (token
->location
, "variable template-id %qD "
6449 "in nested-name-specifier", tid
);
6452 inform (DECL_SOURCE_LOCATION (tmpl
),
6453 "%qD declared here", tmpl
);
6455 parser
->scope
= error_mark_node
;
6459 cp_lexer_consume_token (parser
->lexer
);
6460 cp_lexer_consume_token (parser
->lexer
);
6464 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6466 /* If the next token is an identifier, and the one after
6467 that is a `::', then any valid interpretation would have
6468 found a class-or-namespace-name. */
6469 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6470 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6472 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6475 token
= cp_lexer_consume_token (parser
->lexer
);
6478 if (!token
->error_reported
)
6481 tree ambiguous_decls
;
6483 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6485 /*is_template=*/false,
6486 /*is_namespace=*/false,
6487 /*check_dependency=*/true,
6490 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6491 error_at (token
->location
,
6492 "%qD used without template arguments",
6494 else if (ambiguous_decls
)
6496 // cp_parser_lookup_name has the same diagnostic,
6497 // thus make sure to emit it at most once.
6498 if (cp_parser_uncommitted_to_tentative_parse_p
6501 error_at (token
->location
,
6502 "reference to %qD is ambiguous",
6504 print_candidates (ambiguous_decls
);
6506 decl
= error_mark_node
;
6510 if (cxx_dialect
!= cxx98
)
6511 cp_parser_name_lookup_error
6512 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6515 cp_parser_name_lookup_error
6516 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6520 parser
->scope
= error_mark_node
;
6522 /* Treat this as a successful nested-name-specifier
6527 If the name found is not a class-name (clause
6528 _class_) or namespace-name (_namespace.def_), the
6529 program is ill-formed. */
6532 cp_lexer_consume_token (parser
->lexer
);
6536 /* We've found one valid nested-name-specifier. */
6538 /* Name lookup always gives us a DECL. */
6539 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6540 new_scope
= TREE_TYPE (new_scope
);
6541 /* Uses of "template" must be followed by actual templates. */
6542 if (template_keyword_p
6543 && !(CLASS_TYPE_P (new_scope
)
6544 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6545 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6546 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6547 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6548 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6549 == TEMPLATE_ID_EXPR
)))
6550 permerror (input_location
, TYPE_P (new_scope
)
6551 ? G_("%qT is not a template")
6552 : G_("%qD is not a template"),
6554 /* If it is a class scope, try to complete it; we are about to
6555 be looking up names inside the class. */
6556 if (TYPE_P (new_scope
)
6557 /* Since checking types for dependency can be expensive,
6558 avoid doing it if the type is already complete. */
6559 && !COMPLETE_TYPE_P (new_scope
)
6560 /* Do not try to complete dependent types. */
6561 && !dependent_type_p (new_scope
))
6563 new_scope
= complete_type (new_scope
);
6564 /* If it is a typedef to current class, use the current
6565 class instead, as the typedef won't have any names inside
6567 if (!COMPLETE_TYPE_P (new_scope
)
6568 && currently_open_class (new_scope
))
6569 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6571 /* Make sure we look in the right scope the next time through
6573 parser
->scope
= new_scope
;
6576 /* If parsing tentatively, replace the sequence of tokens that makes
6577 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6578 token. That way, should we re-parse the token stream, we will
6579 not have to repeat the effort required to do the parse, nor will
6580 we issue duplicate error messages. */
6581 if (success
&& start
)
6585 token
= cp_lexer_token_at (parser
->lexer
, start
);
6586 /* Reset the contents of the START token. */
6587 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6588 /* Retrieve any deferred checks. Do not pop this access checks yet
6589 so the memory will not be reclaimed during token replacing below. */
6590 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6591 token
->u
.tree_check_value
->value
= parser
->scope
;
6592 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6593 token
->u
.tree_check_value
->qualifying_scope
=
6594 parser
->qualifying_scope
;
6595 token
->keyword
= RID_MAX
;
6597 /* Purge all subsequent tokens. */
6598 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6602 pop_to_parent_deferring_access_checks ();
6604 return success
? parser
->scope
: NULL_TREE
;
6607 /* Parse a nested-name-specifier. See
6608 cp_parser_nested_name_specifier_opt for details. This function
6609 behaves identically, except that it will an issue an error if no
6610 nested-name-specifier is present. */
6613 cp_parser_nested_name_specifier (cp_parser
*parser
,
6614 bool typename_keyword_p
,
6615 bool check_dependency_p
,
6617 bool is_declaration
)
6621 /* Look for the nested-name-specifier. */
6622 scope
= cp_parser_nested_name_specifier_opt (parser
,
6627 /* If it was not present, issue an error message. */
6630 cp_parser_error (parser
, "expected nested-name-specifier");
6631 parser
->scope
= NULL_TREE
;
6637 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6638 this is either a class-name or a namespace-name (which corresponds
6639 to the class-or-namespace-name production in the grammar). For
6640 C++0x, it can also be a type-name that refers to an enumeration
6641 type or a simple-template-id.
6643 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6644 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6645 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6646 TYPE_P is TRUE iff the next name should be taken as a class-name,
6647 even the same name is declared to be another entity in the same
6650 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6651 specified by the class-or-namespace-name. If neither is found the
6652 ERROR_MARK_NODE is returned. */
6655 cp_parser_qualifying_entity (cp_parser
*parser
,
6656 bool typename_keyword_p
,
6657 bool template_keyword_p
,
6658 bool check_dependency_p
,
6660 bool is_declaration
)
6663 tree saved_qualifying_scope
;
6664 tree saved_object_scope
;
6667 bool successful_parse_p
;
6669 /* DR 743: decltype can appear in a nested-name-specifier. */
6670 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6672 scope
= cp_parser_decltype (parser
);
6673 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6674 && !MAYBE_CLASS_TYPE_P (scope
))
6676 cp_parser_simulate_error (parser
);
6677 return error_mark_node
;
6679 if (TYPE_NAME (scope
))
6680 scope
= TYPE_NAME (scope
);
6684 /* Before we try to parse the class-name, we must save away the
6685 current PARSER->SCOPE since cp_parser_class_name will destroy
6687 saved_scope
= parser
->scope
;
6688 saved_qualifying_scope
= parser
->qualifying_scope
;
6689 saved_object_scope
= parser
->object_scope
;
6690 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6691 there is no need to look for a namespace-name. */
6692 only_class_p
= template_keyword_p
6693 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6695 cp_parser_parse_tentatively (parser
);
6696 scope
= cp_parser_class_name (parser
,
6699 type_p
? class_type
: none_type
,
6701 /*class_head_p=*/false,
6703 /*enum_ok=*/cxx_dialect
> cxx98
);
6704 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6705 /* If that didn't work, try for a namespace-name. */
6706 if (!only_class_p
&& !successful_parse_p
)
6708 /* Restore the saved scope. */
6709 parser
->scope
= saved_scope
;
6710 parser
->qualifying_scope
= saved_qualifying_scope
;
6711 parser
->object_scope
= saved_object_scope
;
6712 /* If we are not looking at an identifier followed by the scope
6713 resolution operator, then this is not part of a
6714 nested-name-specifier. (Note that this function is only used
6715 to parse the components of a nested-name-specifier.) */
6716 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6717 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6718 return error_mark_node
;
6719 scope
= cp_parser_namespace_name (parser
);
6725 /* Return true if we are looking at a compound-literal, false otherwise. */
6728 cp_parser_compound_literal_p (cp_parser
*parser
)
6730 cp_lexer_save_tokens (parser
->lexer
);
6732 /* Skip tokens until the next token is a closing parenthesis.
6733 If we find the closing `)', and the next token is a `{', then
6734 we are looking at a compound-literal. */
6735 bool compound_literal_p
6736 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6737 /*consume_paren=*/true)
6738 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6740 /* Roll back the tokens we skipped. */
6741 cp_lexer_rollback_tokens (parser
->lexer
);
6743 return compound_literal_p
;
6746 /* Return true if EXPR is the integer constant zero or a complex constant
6747 of zero, without any folding, but ignoring location wrappers. */
6750 literal_integer_zerop (const_tree expr
)
6752 return (location_wrapper_p (expr
)
6753 && integer_zerop (TREE_OPERAND (expr
, 0)));
6756 /* Parse a postfix-expression.
6760 postfix-expression [ expression ]
6761 postfix-expression ( expression-list [opt] )
6762 simple-type-specifier ( expression-list [opt] )
6763 typename :: [opt] nested-name-specifier identifier
6764 ( expression-list [opt] )
6765 typename :: [opt] nested-name-specifier template [opt] template-id
6766 ( expression-list [opt] )
6767 postfix-expression . template [opt] id-expression
6768 postfix-expression -> template [opt] id-expression
6769 postfix-expression . pseudo-destructor-name
6770 postfix-expression -> pseudo-destructor-name
6771 postfix-expression ++
6772 postfix-expression --
6773 dynamic_cast < type-id > ( expression )
6774 static_cast < type-id > ( expression )
6775 reinterpret_cast < type-id > ( expression )
6776 const_cast < type-id > ( expression )
6777 typeid ( expression )
6783 ( type-id ) { initializer-list , [opt] }
6785 This extension is a GNU version of the C99 compound-literal
6786 construct. (The C99 grammar uses `type-name' instead of `type-id',
6787 but they are essentially the same concept.)
6789 If ADDRESS_P is true, the postfix expression is the operand of the
6790 `&' operator. CAST_P is true if this expression is the target of a
6793 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6794 class member access expressions [expr.ref].
6796 Returns a representation of the expression. */
6799 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6800 bool member_access_only_p
, bool decltype_p
,
6801 cp_id_kind
* pidk_return
)
6806 cp_id_kind idk
= CP_ID_KIND_NONE
;
6807 cp_expr postfix_expression
= NULL_TREE
;
6808 bool is_member_access
= false;
6810 /* Peek at the next token. */
6811 token
= cp_lexer_peek_token (parser
->lexer
);
6812 loc
= token
->location
;
6813 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6815 /* Some of the productions are determined by keywords. */
6816 keyword
= token
->keyword
;
6826 const char *saved_message
;
6827 bool saved_in_type_id_in_expr_p
;
6829 /* All of these can be handled in the same way from the point
6830 of view of parsing. Begin by consuming the token
6831 identifying the cast. */
6832 cp_lexer_consume_token (parser
->lexer
);
6834 /* New types cannot be defined in the cast. */
6835 saved_message
= parser
->type_definition_forbidden_message
;
6836 parser
->type_definition_forbidden_message
6837 = G_("types may not be defined in casts");
6839 /* Look for the opening `<'. */
6840 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6841 /* Parse the type to which we are casting. */
6842 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6843 parser
->in_type_id_in_expr_p
= true;
6844 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
6846 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6847 /* Look for the closing `>'. */
6848 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6849 /* Restore the old message. */
6850 parser
->type_definition_forbidden_message
= saved_message
;
6852 bool saved_greater_than_is_operator_p
6853 = parser
->greater_than_is_operator_p
;
6854 parser
->greater_than_is_operator_p
= true;
6856 /* And the expression which is being cast. */
6857 matching_parens parens
;
6858 parens
.require_open (parser
);
6859 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6860 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6862 location_t end_loc
= close_paren
?
6863 close_paren
->location
: UNKNOWN_LOCATION
;
6865 parser
->greater_than_is_operator_p
6866 = saved_greater_than_is_operator_p
;
6868 /* Only type conversions to integral or enumeration types
6869 can be used in constant-expressions. */
6870 if (!cast_valid_in_integral_constant_expression_p (type
)
6871 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6873 postfix_expression
= error_mark_node
;
6881 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6885 = build_static_cast (type
, expression
, tf_warning_or_error
);
6889 = build_reinterpret_cast (type
, expression
,
6890 tf_warning_or_error
);
6894 = build_const_cast (type
, expression
, tf_warning_or_error
);
6900 /* Construct a location e.g. :
6901 reinterpret_cast <int *> (expr)
6902 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6903 ranging from the start of the "*_cast" token to the final closing
6904 paren, with the caret at the start. */
6905 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6906 postfix_expression
.set_location (cp_cast_loc
);
6913 const char *saved_message
;
6914 bool saved_in_type_id_in_expr_p
;
6916 /* Consume the `typeid' token. */
6917 cp_lexer_consume_token (parser
->lexer
);
6918 /* Look for the `(' token. */
6919 matching_parens parens
;
6920 parens
.require_open (parser
);
6921 /* Types cannot be defined in a `typeid' expression. */
6922 saved_message
= parser
->type_definition_forbidden_message
;
6923 parser
->type_definition_forbidden_message
6924 = G_("types may not be defined in a %<typeid%> expression");
6925 /* We can't be sure yet whether we're looking at a type-id or an
6927 cp_parser_parse_tentatively (parser
);
6928 /* Try a type-id first. */
6929 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6930 parser
->in_type_id_in_expr_p
= true;
6931 type
= cp_parser_type_id (parser
);
6932 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6933 /* Look for the `)' token. Otherwise, we can't be sure that
6934 we're not looking at an expression: consider `typeid (int
6935 (3))', for example. */
6936 cp_token
*close_paren
= parens
.require_close (parser
);
6937 /* If all went well, simply lookup the type-id. */
6938 if (cp_parser_parse_definitely (parser
))
6939 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6940 /* Otherwise, fall back to the expression variant. */
6945 /* Look for an expression. */
6946 expression
= cp_parser_expression (parser
, & idk
);
6947 /* Compute its typeid. */
6948 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6949 /* Look for the `)' token. */
6950 close_paren
= parens
.require_close (parser
);
6952 /* Restore the saved message. */
6953 parser
->type_definition_forbidden_message
= saved_message
;
6954 /* `typeid' may not appear in an integral constant expression. */
6955 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6956 postfix_expression
= error_mark_node
;
6958 /* Construct a location e.g. :
6961 ranging from the start of the "typeid" token to the final closing
6962 paren, with the caret at the start. */
6965 location_t typeid_loc
6966 = make_location (start_loc
, start_loc
, close_paren
->location
);
6967 postfix_expression
.set_location (typeid_loc
);
6968 postfix_expression
.maybe_add_location_wrapper ();
6976 /* The syntax permitted here is the same permitted for an
6977 elaborated-type-specifier. */
6978 ++parser
->prevent_constrained_type_specifiers
;
6979 type
= cp_parser_elaborated_type_specifier (parser
,
6980 /*is_friend=*/false,
6981 /*is_declaration=*/false);
6982 --parser
->prevent_constrained_type_specifiers
;
6983 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6988 case RID_BUILTIN_SHUFFLE
:
6989 case RID_BUILTIN_LAUNDER
:
6991 vec
<tree
, va_gc
> *vec
;
6995 cp_lexer_consume_token (parser
->lexer
);
6996 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6997 /*cast_p=*/false, /*allow_expansion_p=*/true,
6998 /*non_constant_p=*/NULL
);
7001 postfix_expression
= error_mark_node
;
7005 FOR_EACH_VEC_ELT (*vec
, i
, p
)
7011 if (vec
->length () == 1)
7013 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7016 error_at (loc
, "wrong number of arguments to "
7017 "%<__builtin_addressof%>");
7018 postfix_expression
= error_mark_node
;
7022 case RID_BUILTIN_LAUNDER
:
7023 if (vec
->length () == 1)
7024 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7025 tf_warning_or_error
);
7028 error_at (loc
, "wrong number of arguments to "
7029 "%<__builtin_launder%>");
7030 postfix_expression
= error_mark_node
;
7034 case RID_BUILTIN_SHUFFLE
:
7035 if (vec
->length () == 2)
7037 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7038 (*vec
)[1], tf_warning_or_error
);
7039 else if (vec
->length () == 3)
7041 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7042 (*vec
)[2], tf_warning_or_error
);
7045 error_at (loc
, "wrong number of arguments to "
7046 "%<__builtin_shuffle%>");
7047 postfix_expression
= error_mark_node
;
7057 case RID_BUILTIN_CONVERTVECTOR
:
7061 /* Consume the `__builtin_convertvector' token. */
7062 cp_lexer_consume_token (parser
->lexer
);
7063 /* Look for the opening `('. */
7064 matching_parens parens
;
7065 parens
.require_open (parser
);
7066 /* Now, parse the assignment-expression. */
7067 expression
= cp_parser_assignment_expression (parser
);
7068 /* Look for the `,'. */
7069 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7070 location_t type_location
7071 = cp_lexer_peek_token (parser
->lexer
)->location
;
7072 /* Parse the type-id. */
7074 type_id_in_expr_sentinel
s (parser
);
7075 type
= cp_parser_type_id (parser
);
7077 /* Look for the closing `)'. */
7078 parens
.require_close (parser
);
7079 return cp_build_vec_convert (expression
, type_location
, type
,
7080 tf_warning_or_error
);
7087 /* If the next thing is a simple-type-specifier, we may be
7088 looking at a functional cast. We could also be looking at
7089 an id-expression. So, we try the functional cast, and if
7090 that doesn't work we fall back to the primary-expression. */
7091 cp_parser_parse_tentatively (parser
);
7092 /* Look for the simple-type-specifier. */
7093 ++parser
->prevent_constrained_type_specifiers
;
7094 type
= cp_parser_simple_type_specifier (parser
,
7095 /*decl_specs=*/NULL
,
7096 CP_PARSER_FLAGS_NONE
);
7097 --parser
->prevent_constrained_type_specifiers
;
7098 /* Parse the cast itself. */
7099 if (!cp_parser_error_occurred (parser
))
7101 = cp_parser_functional_cast (parser
, type
);
7102 /* If that worked, we're done. */
7103 if (cp_parser_parse_definitely (parser
))
7106 /* If the functional-cast didn't work out, try a
7107 compound-literal. */
7108 if (cp_parser_allow_gnu_extensions_p (parser
)
7109 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7111 cp_expr initializer
= NULL_TREE
;
7113 cp_parser_parse_tentatively (parser
);
7115 matching_parens parens
;
7116 parens
.consume_open (parser
);
7118 /* Avoid calling cp_parser_type_id pointlessly, see comment
7119 in cp_parser_cast_expression about c++/29234. */
7120 if (!cp_parser_compound_literal_p (parser
))
7121 cp_parser_simulate_error (parser
);
7124 /* Parse the type. */
7125 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7126 parser
->in_type_id_in_expr_p
= true;
7127 type
= cp_parser_type_id (parser
);
7128 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7129 parens
.require_close (parser
);
7132 /* If things aren't going well, there's no need to
7134 if (!cp_parser_error_occurred (parser
))
7136 bool non_constant_p
;
7137 /* Parse the brace-enclosed initializer list. */
7138 initializer
= cp_parser_braced_list (parser
,
7141 /* If that worked, we're definitely looking at a
7142 compound-literal expression. */
7143 if (cp_parser_parse_definitely (parser
))
7145 /* Warn the user that a compound literal is not
7146 allowed in standard C++. */
7147 pedwarn (input_location
, OPT_Wpedantic
,
7148 "ISO C++ forbids compound-literals");
7149 /* For simplicity, we disallow compound literals in
7150 constant-expressions. We could
7151 allow compound literals of integer type, whose
7152 initializer was a constant, in constant
7153 expressions. Permitting that usage, as a further
7154 extension, would not change the meaning of any
7155 currently accepted programs. (Of course, as
7156 compound literals are not part of ISO C++, the
7157 standard has nothing to say.) */
7158 if (cp_parser_non_integral_constant_expression (parser
,
7161 postfix_expression
= error_mark_node
;
7164 /* Form the representation of the compound-literal. */
7166 = finish_compound_literal (type
, initializer
,
7167 tf_warning_or_error
, fcl_c99
);
7168 postfix_expression
.set_location (initializer
.get_location ());
7173 /* It must be a primary-expression. */
7175 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7176 /*template_arg_p=*/false,
7183 /* Note that we don't need to worry about calling build_cplus_new on a
7184 class-valued CALL_EXPR in decltype when it isn't the end of the
7185 postfix-expression; unary_complex_lvalue will take care of that for
7188 /* Keep looping until the postfix-expression is complete. */
7191 if (idk
== CP_ID_KIND_UNQUALIFIED
7192 && identifier_p (postfix_expression
)
7193 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7194 /* It is not a Koenig lookup function call. */
7196 = unqualified_name_lookup_error (postfix_expression
);
7198 /* Peek at the next token. */
7199 token
= cp_lexer_peek_token (parser
->lexer
);
7201 switch (token
->type
)
7203 case CPP_OPEN_SQUARE
:
7204 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7206 cp_parser_error (parser
,
7207 "two consecutive %<[%> shall "
7208 "only introduce an attribute");
7209 return error_mark_node
;
7212 = cp_parser_postfix_open_square_expression (parser
,
7216 postfix_expression
.set_range (start_loc
,
7217 postfix_expression
.get_location ());
7219 idk
= CP_ID_KIND_NONE
;
7220 is_member_access
= false;
7223 case CPP_OPEN_PAREN
:
7224 /* postfix-expression ( expression-list [opt] ) */
7227 bool is_builtin_constant_p
;
7228 bool saved_integral_constant_expression_p
= false;
7229 bool saved_non_integral_constant_expression_p
= false;
7230 tsubst_flags_t complain
= complain_flags (decltype_p
);
7231 vec
<tree
, va_gc
> *args
;
7232 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7234 is_member_access
= false;
7236 tree stripped_expression
7237 = tree_strip_any_location_wrapper (postfix_expression
);
7238 is_builtin_constant_p
7239 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7240 if (is_builtin_constant_p
)
7242 /* The whole point of __builtin_constant_p is to allow
7243 non-constant expressions to appear as arguments. */
7244 saved_integral_constant_expression_p
7245 = parser
->integral_constant_expression_p
;
7246 saved_non_integral_constant_expression_p
7247 = parser
->non_integral_constant_expression_p
;
7248 parser
->integral_constant_expression_p
= false;
7250 args
= (cp_parser_parenthesized_expression_list
7252 /*cast_p=*/false, /*allow_expansion_p=*/true,
7253 /*non_constant_p=*/NULL
,
7254 /*close_paren_loc=*/&close_paren_loc
,
7255 /*wrap_locations_p=*/true));
7256 if (is_builtin_constant_p
)
7258 parser
->integral_constant_expression_p
7259 = saved_integral_constant_expression_p
;
7260 parser
->non_integral_constant_expression_p
7261 = saved_non_integral_constant_expression_p
;
7266 postfix_expression
= error_mark_node
;
7270 /* Function calls are not permitted in
7271 constant-expressions. */
7272 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7273 && cp_parser_non_integral_constant_expression (parser
,
7276 postfix_expression
= error_mark_node
;
7277 release_tree_vector (args
);
7282 if (idk
== CP_ID_KIND_UNQUALIFIED
7283 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7285 if (identifier_p (postfix_expression
)
7286 /* In C++2A, we may need to perform ADL for a template
7288 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7289 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7291 if (!args
->is_empty ())
7294 if (!any_type_dependent_arguments_p (args
))
7296 = perform_koenig_lookup (postfix_expression
, args
,
7301 = unqualified_fn_lookup_error (postfix_expression
);
7303 /* We do not perform argument-dependent lookup if
7304 normal lookup finds a non-function, in accordance
7305 with the expected resolution of DR 218. */
7306 else if (!args
->is_empty ()
7307 && is_overloaded_fn (postfix_expression
))
7309 /* We only need to look at the first function,
7310 because all the fns share the attribute we're
7311 concerned with (all member fns or all local
7313 tree fn
= get_first_fn (postfix_expression
);
7314 fn
= STRIP_TEMPLATE (fn
);
7316 /* Do not do argument dependent lookup if regular
7317 lookup finds a member function or a block-scope
7318 function declaration. [basic.lookup.argdep]/3 */
7319 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7320 || DECL_FUNCTION_MEMBER_P (fn
)
7321 || DECL_LOCAL_FUNCTION_P (fn
)))
7324 if (!any_type_dependent_arguments_p (args
))
7326 = perform_koenig_lookup (postfix_expression
, args
,
7332 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7334 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7335 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7337 if (processing_template_decl
7338 && (type_dependent_object_expression_p (instance
)
7339 || (!BASELINK_P (fn
)
7340 && TREE_CODE (fn
) != FIELD_DECL
)
7341 || type_dependent_expression_p (fn
)
7342 || any_type_dependent_arguments_p (args
)))
7344 maybe_generic_this_capture (instance
, fn
);
7346 = build_min_nt_call_vec (postfix_expression
, args
);
7347 release_tree_vector (args
);
7351 if (BASELINK_P (fn
))
7354 = (build_new_method_call
7355 (instance
, fn
, &args
, NULL_TREE
,
7356 (idk
== CP_ID_KIND_QUALIFIED
7357 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7364 = finish_call_expr (postfix_expression
, &args
,
7365 /*disallow_virtual=*/false,
7369 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7370 || TREE_CODE (postfix_expression
) == MEMBER_REF
7371 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7372 postfix_expression
= (build_offset_ref_call_from_tree
7373 (postfix_expression
, &args
,
7375 else if (idk
== CP_ID_KIND_QUALIFIED
)
7376 /* A call to a static class member, or a namespace-scope
7379 = finish_call_expr (postfix_expression
, &args
,
7380 /*disallow_virtual=*/true,
7384 /* All other function calls. */
7386 = finish_call_expr (postfix_expression
, &args
,
7387 /*disallow_virtual=*/false,
7391 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7393 location_t combined_loc
= make_location (token
->location
,
7396 postfix_expression
.set_location (combined_loc
);
7399 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7400 idk
= CP_ID_KIND_NONE
;
7402 release_tree_vector (args
);
7408 /* postfix-expression . template [opt] id-expression
7409 postfix-expression . pseudo-destructor-name
7410 postfix-expression -> template [opt] id-expression
7411 postfix-expression -> pseudo-destructor-name */
7413 /* Consume the `.' or `->' operator. */
7414 cp_lexer_consume_token (parser
->lexer
);
7417 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7421 is_member_access
= true;
7425 /* postfix-expression ++ */
7426 /* Consume the `++' token. */
7427 cp_lexer_consume_token (parser
->lexer
);
7428 /* Generate a representation for the complete expression. */
7430 = finish_increment_expr (postfix_expression
,
7431 POSTINCREMENT_EXPR
);
7432 /* Increments may not appear in constant-expressions. */
7433 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7434 postfix_expression
= error_mark_node
;
7435 idk
= CP_ID_KIND_NONE
;
7436 is_member_access
= false;
7439 case CPP_MINUS_MINUS
:
7440 /* postfix-expression -- */
7441 /* Consume the `--' token. */
7442 cp_lexer_consume_token (parser
->lexer
);
7443 /* Generate a representation for the complete expression. */
7445 = finish_increment_expr (postfix_expression
,
7446 POSTDECREMENT_EXPR
);
7447 /* Decrements may not appear in constant-expressions. */
7448 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7449 postfix_expression
= error_mark_node
;
7450 idk
= CP_ID_KIND_NONE
;
7451 is_member_access
= false;
7455 if (pidk_return
!= NULL
)
7456 * pidk_return
= idk
;
7457 if (member_access_only_p
)
7458 return is_member_access
7459 ? postfix_expression
7460 : cp_expr (error_mark_node
);
7462 return postfix_expression
;
7466 /* We should never get here. */
7468 return error_mark_node
;
7471 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7472 by cp_parser_builtin_offsetof. We're looking for
7474 postfix-expression [ expression ]
7475 postfix-expression [ braced-init-list ] (C++11)
7477 FOR_OFFSETOF is set if we're being called in that context, which
7478 changes how we deal with integer constant expressions. */
7481 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7482 tree postfix_expression
,
7486 tree index
= NULL_TREE
;
7487 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7488 bool saved_greater_than_is_operator_p
;
7490 /* Consume the `[' token. */
7491 cp_lexer_consume_token (parser
->lexer
);
7493 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7494 parser
->greater_than_is_operator_p
= true;
7496 /* Parse the index expression. */
7497 /* ??? For offsetof, there is a question of what to allow here. If
7498 offsetof is not being used in an integral constant expression context,
7499 then we *could* get the right answer by computing the value at runtime.
7500 If we are in an integral constant expression context, then we might
7501 could accept any constant expression; hard to say without analysis.
7502 Rather than open the barn door too wide right away, allow only integer
7503 constant expressions here. */
7505 index
= cp_parser_constant_expression (parser
);
7508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7510 bool expr_nonconst_p
;
7511 cp_lexer_set_source_position (parser
->lexer
);
7512 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7513 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7516 index
= cp_parser_expression (parser
);
7519 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7521 /* Look for the closing `]'. */
7522 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7524 /* Build the ARRAY_REF. */
7525 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7528 /* When not doing offsetof, array references are not permitted in
7529 constant-expressions. */
7531 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7532 postfix_expression
= error_mark_node
;
7534 return postfix_expression
;
7537 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7538 dereference of incomplete type, returns true if error_mark_node should
7539 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7540 and *DEPENDENT_P. */
7543 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7546 /* In a template, be permissive by treating an object expression
7547 of incomplete type as dependent (after a pedwarn). */
7548 diagnostic_t kind
= (processing_template_decl
7549 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7551 switch (TREE_CODE (*postfix_expression
))
7554 case REINTERPRET_CAST_EXPR
:
7555 case CONST_CAST_EXPR
:
7556 case STATIC_CAST_EXPR
:
7557 case DYNAMIC_CAST_EXPR
:
7558 case IMPLICIT_CONV_EXPR
:
7559 case VIEW_CONVERT_EXPR
:
7560 case NON_LVALUE_EXPR
:
7564 /* Don't emit any diagnostic for OVERLOADs. */
7568 /* Avoid clobbering e.g. DECLs. */
7569 if (!EXPR_P (*postfix_expression
))
7574 if (kind
== DK_IGNORED
)
7577 location_t exploc
= location_of (*postfix_expression
);
7578 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7579 if (!MAYBE_CLASS_TYPE_P (*scope
))
7581 if (kind
== DK_ERROR
)
7582 *scope
= *postfix_expression
= error_mark_node
;
7583 else if (processing_template_decl
)
7585 *dependent_p
= true;
7586 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7591 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7592 by cp_parser_builtin_offsetof. We're looking for
7594 postfix-expression . template [opt] id-expression
7595 postfix-expression . pseudo-destructor-name
7596 postfix-expression -> template [opt] id-expression
7597 postfix-expression -> pseudo-destructor-name
7599 FOR_OFFSETOF is set if we're being called in that context. That sorta
7600 limits what of the above we'll actually accept, but nevermind.
7601 TOKEN_TYPE is the "." or "->" token, which will already have been
7602 removed from the stream. */
7605 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7606 enum cpp_ttype token_type
,
7607 cp_expr postfix_expression
,
7608 bool for_offsetof
, cp_id_kind
*idk
,
7609 location_t location
)
7613 bool pseudo_destructor_p
;
7614 tree scope
= NULL_TREE
;
7615 location_t start_loc
= postfix_expression
.get_start ();
7617 /* If this is a `->' operator, dereference the pointer. */
7618 if (token_type
== CPP_DEREF
)
7619 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7620 tf_warning_or_error
);
7621 /* Check to see whether or not the expression is type-dependent and
7622 not the current instantiation. */
7623 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7624 /* The identifier following the `->' or `.' is not qualified. */
7625 parser
->scope
= NULL_TREE
;
7626 parser
->qualifying_scope
= NULL_TREE
;
7627 parser
->object_scope
= NULL_TREE
;
7628 *idk
= CP_ID_KIND_NONE
;
7630 /* Enter the scope corresponding to the type of the object
7631 given by the POSTFIX_EXPRESSION. */
7634 scope
= TREE_TYPE (postfix_expression
);
7635 /* According to the standard, no expression should ever have
7636 reference type. Unfortunately, we do not currently match
7637 the standard in this respect in that our internal representation
7638 of an expression may have reference type even when the standard
7639 says it does not. Therefore, we have to manually obtain the
7640 underlying type here. */
7641 scope
= non_reference (scope
);
7642 /* The type of the POSTFIX_EXPRESSION must be complete. */
7643 /* Unlike the object expression in other contexts, *this is not
7644 required to be of complete type for purposes of class member
7645 access (5.2.5) outside the member function body. */
7646 if (postfix_expression
!= current_class_ref
7647 && scope
!= error_mark_node
7648 && !currently_open_class (scope
))
7650 scope
= complete_type (scope
);
7651 if (!COMPLETE_TYPE_P (scope
)
7652 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7654 return error_mark_node
;
7659 /* Let the name lookup machinery know that we are processing a
7660 class member access expression. */
7661 parser
->context
->object_type
= scope
;
7662 /* If something went wrong, we want to be able to discern that case,
7663 as opposed to the case where there was no SCOPE due to the type
7664 of expression being dependent. */
7666 scope
= error_mark_node
;
7667 /* If the SCOPE was erroneous, make the various semantic analysis
7668 functions exit quickly -- and without issuing additional error
7670 if (scope
== error_mark_node
)
7671 postfix_expression
= error_mark_node
;
7676 /* Tell cp_parser_lookup_name that there was an object, even though it's
7678 parser
->context
->object_type
= unknown_type_node
;
7680 /* Assume this expression is not a pseudo-destructor access. */
7681 pseudo_destructor_p
= false;
7683 /* If the SCOPE is a scalar type, then, if this is a valid program,
7684 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7685 is type dependent, it can be pseudo-destructor-name or something else.
7686 Try to parse it as pseudo-destructor-name first. */
7687 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7692 cp_parser_parse_tentatively (parser
);
7693 /* Parse the pseudo-destructor-name. */
7695 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7698 && (cp_parser_error_occurred (parser
)
7699 || !SCALAR_TYPE_P (type
)))
7700 cp_parser_abort_tentative_parse (parser
);
7701 else if (cp_parser_parse_definitely (parser
))
7703 pseudo_destructor_p
= true;
7705 = finish_pseudo_destructor_expr (postfix_expression
,
7710 if (!pseudo_destructor_p
)
7712 /* If the SCOPE is not a scalar type, we are looking at an
7713 ordinary class member access expression, rather than a
7714 pseudo-destructor-name. */
7716 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7717 /* Parse the id-expression. */
7718 name
= (cp_parser_id_expression
7720 cp_parser_optional_template_keyword (parser
),
7721 /*check_dependency_p=*/true,
7723 /*declarator_p=*/false,
7724 /*optional_p=*/false));
7725 /* In general, build a SCOPE_REF if the member name is qualified.
7726 However, if the name was not dependent and has already been
7727 resolved; there is no need to build the SCOPE_REF. For example;
7729 struct X { void f(); };
7730 template <typename T> void f(T* t) { t->X::f(); }
7732 Even though "t" is dependent, "X::f" is not and has been resolved
7733 to a BASELINK; there is no need to include scope information. */
7735 /* But we do need to remember that there was an explicit scope for
7736 virtual function calls. */
7738 *idk
= CP_ID_KIND_QUALIFIED
;
7740 /* If the name is a template-id that names a type, we will get a
7741 TYPE_DECL here. That is invalid code. */
7742 if (TREE_CODE (name
) == TYPE_DECL
)
7744 error_at (token
->location
, "invalid use of %qD", name
);
7745 postfix_expression
= error_mark_node
;
7749 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7751 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7753 error_at (token
->location
, "%<%D::%D%> is not a class member",
7754 parser
->scope
, name
);
7755 postfix_expression
= error_mark_node
;
7758 name
= build_qualified_name (/*type=*/NULL_TREE
,
7762 parser
->scope
= NULL_TREE
;
7763 parser
->qualifying_scope
= NULL_TREE
;
7764 parser
->object_scope
= NULL_TREE
;
7766 if (parser
->scope
&& name
&& BASELINK_P (name
))
7767 adjust_result_of_qualified_name_lookup
7768 (name
, parser
->scope
, scope
);
7770 = finish_class_member_access_expr (postfix_expression
, name
,
7772 tf_warning_or_error
);
7773 /* Build a location e.g.:
7776 where the caret is at the deref token, ranging from
7777 the start of postfix_expression to the end of the access expr. */
7779 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7780 location_t combined_loc
7781 = make_location (input_location
, start_loc
, end_loc
);
7782 protected_set_expr_location (postfix_expression
, combined_loc
);
7786 /* We no longer need to look up names in the scope of the object on
7787 the left-hand side of the `.' or `->' operator. */
7788 parser
->context
->object_type
= NULL_TREE
;
7790 /* Outside of offsetof, these operators may not appear in
7791 constant-expressions. */
7793 && (cp_parser_non_integral_constant_expression
7794 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7795 postfix_expression
= error_mark_node
;
7797 return postfix_expression
;
7800 /* Parse a parenthesized expression-list.
7803 assignment-expression
7804 expression-list, assignment-expression
7809 identifier, expression-list
7811 CAST_P is true if this expression is the target of a cast.
7813 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7816 WRAP_LOCATIONS_P is true if expressions within this list for which
7817 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7818 their source locations.
7820 Returns a vector of trees. Each element is a representation of an
7821 assignment-expression. NULL is returned if the ( and or ) are
7822 missing. An empty, but allocated, vector is returned on no
7823 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7824 if we are parsing an attribute list for an attribute that wants a
7825 plain identifier argument, normal_attr for an attribute that wants
7826 an expression, or non_attr if we aren't parsing an attribute list. If
7827 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7828 not all of the expressions in the list were constant.
7829 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7830 will be written to with the location of the closing parenthesis. If
7831 an error occurs, it may or may not be written to. */
7833 static vec
<tree
, va_gc
> *
7834 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7835 int is_attribute_list
,
7837 bool allow_expansion_p
,
7838 bool *non_constant_p
,
7839 location_t
*close_paren_loc
,
7840 bool wrap_locations_p
)
7842 vec
<tree
, va_gc
> *expression_list
;
7843 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7844 tree identifier
= NULL_TREE
;
7845 bool saved_greater_than_is_operator_p
;
7847 /* Assume all the expressions will be constant. */
7849 *non_constant_p
= false;
7851 matching_parens parens
;
7852 if (!parens
.require_open (parser
))
7855 expression_list
= make_tree_vector ();
7857 /* Within a parenthesized expression, a `>' token is always
7858 the greater-than operator. */
7859 saved_greater_than_is_operator_p
7860 = parser
->greater_than_is_operator_p
;
7861 parser
->greater_than_is_operator_p
= true;
7863 cp_expr
expr (NULL_TREE
);
7865 /* Consume expressions until there are no more. */
7866 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7869 /* At the beginning of attribute lists, check to see if the
7870 next token is an identifier. */
7871 if (is_attribute_list
== id_attr
7872 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7876 /* Consume the identifier. */
7877 token
= cp_lexer_consume_token (parser
->lexer
);
7878 /* Save the identifier. */
7879 identifier
= token
->u
.value
;
7883 bool expr_non_constant_p
;
7885 /* Parse the next assignment-expression. */
7886 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7888 /* A braced-init-list. */
7889 cp_lexer_set_source_position (parser
->lexer
);
7890 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7891 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7892 if (non_constant_p
&& expr_non_constant_p
)
7893 *non_constant_p
= true;
7895 else if (non_constant_p
)
7897 expr
= (cp_parser_constant_expression
7898 (parser
, /*allow_non_constant_p=*/true,
7899 &expr_non_constant_p
));
7900 if (expr_non_constant_p
)
7901 *non_constant_p
= true;
7904 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7908 expr
= instantiate_non_dependent_expr (expr
);
7910 /* If we have an ellipsis, then this is an expression
7912 if (allow_expansion_p
7913 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7915 /* Consume the `...'. */
7916 cp_lexer_consume_token (parser
->lexer
);
7918 /* Build the argument pack. */
7919 expr
= make_pack_expansion (expr
);
7922 if (wrap_locations_p
)
7923 expr
.maybe_add_location_wrapper ();
7925 /* Add it to the list. We add error_mark_node
7926 expressions to the list, so that we can still tell if
7927 the correct form for a parenthesized expression-list
7928 is found. That gives better errors. */
7929 vec_safe_push (expression_list
, expr
.get_value ());
7931 if (expr
== error_mark_node
)
7935 /* After the first item, attribute lists look the same as
7936 expression lists. */
7937 is_attribute_list
= non_attr
;
7940 /* If the next token isn't a `,', then we are done. */
7941 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7944 /* Otherwise, consume the `,' and keep going. */
7945 cp_lexer_consume_token (parser
->lexer
);
7948 if (close_paren_loc
)
7949 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7951 if (!parens
.require_close (parser
))
7956 /* We try and resync to an unnested comma, as that will give the
7957 user better diagnostics. */
7958 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7959 /*recovering=*/true,
7961 /*consume_paren=*/true);
7966 parser
->greater_than_is_operator_p
7967 = saved_greater_than_is_operator_p
;
7972 parser
->greater_than_is_operator_p
7973 = saved_greater_than_is_operator_p
;
7976 vec_safe_insert (expression_list
, 0, identifier
);
7978 return expression_list
;
7981 /* Parse a pseudo-destructor-name.
7983 pseudo-destructor-name:
7984 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7985 :: [opt] nested-name-specifier template template-id :: ~ type-name
7986 :: [opt] nested-name-specifier [opt] ~ type-name
7988 If either of the first two productions is used, sets *SCOPE to the
7989 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7990 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7991 or ERROR_MARK_NODE if the parse fails. */
7994 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7999 bool nested_name_specifier_p
;
8002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
8003 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
8004 && !type_dependent_expression_p (object
))
8006 if (cxx_dialect
< cxx14
)
8007 pedwarn (input_location
, 0,
8008 "%<~auto%> only available with "
8009 "%<-std=c++14%> or %<-std=gnu++14%>");
8010 cp_lexer_consume_token (parser
->lexer
);
8011 cp_lexer_consume_token (parser
->lexer
);
8013 *type
= TREE_TYPE (object
);
8017 /* Assume that things will not work out. */
8018 *type
= error_mark_node
;
8020 /* Look for the optional `::' operator. */
8021 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8022 /* Look for the optional nested-name-specifier. */
8023 nested_name_specifier_p
8024 = (cp_parser_nested_name_specifier_opt (parser
,
8025 /*typename_keyword_p=*/false,
8026 /*check_dependency_p=*/true,
8028 /*is_declaration=*/false)
8030 /* Now, if we saw a nested-name-specifier, we might be doing the
8031 second production. */
8032 if (nested_name_specifier_p
8033 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8035 /* Consume the `template' keyword. */
8036 cp_lexer_consume_token (parser
->lexer
);
8037 /* Parse the template-id. */
8038 cp_parser_template_id (parser
,
8039 /*template_keyword_p=*/true,
8040 /*check_dependency_p=*/false,
8042 /*is_declaration=*/true);
8043 /* Look for the `::' token. */
8044 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8046 /* If the next token is not a `~', then there might be some
8047 additional qualification. */
8048 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8050 /* At this point, we're looking for "type-name :: ~". The type-name
8051 must not be a class-name, since this is a pseudo-destructor. So,
8052 it must be either an enum-name, or a typedef-name -- both of which
8053 are just identifiers. So, we peek ahead to check that the "::"
8054 and "~" tokens are present; if they are not, then we can avoid
8055 calling type_name. */
8056 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8057 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8058 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8060 cp_parser_error (parser
, "non-scalar type");
8064 /* Look for the type-name. */
8065 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8066 if (*scope
== error_mark_node
)
8069 /* Look for the `::' token. */
8070 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8075 /* Look for the `~'. */
8076 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8078 /* Once we see the ~, this has to be a pseudo-destructor. */
8079 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8080 cp_parser_commit_to_topmost_tentative_parse (parser
);
8082 /* Look for the type-name again. We are not responsible for
8083 checking that it matches the first type-name. */
8084 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8087 /* Parse a unary-expression.
8093 unary-operator cast-expression
8094 sizeof unary-expression
8096 alignof ( type-id ) [C++0x]
8103 __extension__ cast-expression
8104 __alignof__ unary-expression
8105 __alignof__ ( type-id )
8106 alignof unary-expression [C++0x]
8107 __real__ cast-expression
8108 __imag__ cast-expression
8110 sizeof ( type-id ) { initializer-list , [opt] }
8111 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8112 __alignof__ ( type-id ) { initializer-list , [opt] }
8114 ADDRESS_P is true iff the unary-expression is appearing as the
8115 operand of the `&' operator. CAST_P is true if this expression is
8116 the target of a cast.
8118 Returns a representation of the expression. */
8121 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8122 bool address_p
, bool cast_p
, bool decltype_p
)
8125 enum tree_code unary_operator
;
8127 /* Peek at the next token. */
8128 token
= cp_lexer_peek_token (parser
->lexer
);
8129 /* Some keywords give away the kind of expression. */
8130 if (token
->type
== CPP_KEYWORD
)
8132 enum rid keyword
= token
->keyword
;
8141 location_t start_loc
= token
->location
;
8143 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8144 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8146 /* Consume the token. */
8147 cp_lexer_consume_token (parser
->lexer
);
8148 /* Parse the operand. */
8149 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8151 if (TYPE_P (operand
))
8152 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8156 /* ISO C++ defines alignof only with types, not with
8157 expressions. So pedwarn if alignof is used with a non-
8158 type expression. However, __alignof__ is ok. */
8160 pedwarn (token
->location
, OPT_Wpedantic
,
8161 "ISO C++ does not allow %<alignof%> "
8164 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8166 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8167 SIZEOF_EXPR with the original operand. */
8168 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8170 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8172 if (!processing_template_decl
&& TYPE_P (operand
))
8174 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8175 build1 (NOP_EXPR
, operand
,
8177 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8180 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8181 TREE_SIDE_EFFECTS (ret
) = 0;
8182 TREE_READONLY (ret
) = 1;
8186 /* Construct a location e.g. :
8189 with start == caret at the start of the "alignof"/"sizeof"
8190 token, with the endpoint at the final closing paren. */
8191 location_t finish_loc
8192 = cp_lexer_previous_token (parser
->lexer
)->location
;
8193 location_t compound_loc
8194 = make_location (start_loc
, start_loc
, finish_loc
);
8196 cp_expr
ret_expr (ret
);
8197 ret_expr
.set_location (compound_loc
);
8198 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8202 case RID_BUILTIN_HAS_ATTRIBUTE
:
8203 return cp_parser_has_attribute_expression (parser
);
8206 return cp_parser_new_expression (parser
);
8209 return cp_parser_delete_expression (parser
);
8213 /* The saved value of the PEDANTIC flag. */
8217 /* Save away the PEDANTIC flag. */
8218 cp_parser_extension_opt (parser
, &saved_pedantic
);
8219 /* Parse the cast-expression. */
8220 expr
= cp_parser_simple_cast_expression (parser
);
8221 /* Restore the PEDANTIC flag. */
8222 pedantic
= saved_pedantic
;
8232 /* Consume the `__real__' or `__imag__' token. */
8233 cp_lexer_consume_token (parser
->lexer
);
8234 /* Parse the cast-expression. */
8235 expression
= cp_parser_simple_cast_expression (parser
);
8236 /* Create the complete representation. */
8237 return build_x_unary_op (token
->location
,
8238 (keyword
== RID_REALPART
8239 ? REALPART_EXPR
: IMAGPART_EXPR
),
8241 tf_warning_or_error
);
8245 case RID_TRANSACTION_ATOMIC
:
8246 case RID_TRANSACTION_RELAXED
:
8247 return cp_parser_transaction_expression (parser
, keyword
);
8252 const char *saved_message
;
8253 bool saved_integral_constant_expression_p
;
8254 bool saved_non_integral_constant_expression_p
;
8255 bool saved_greater_than_is_operator_p
;
8257 location_t start_loc
= token
->location
;
8259 cp_lexer_consume_token (parser
->lexer
);
8260 matching_parens parens
;
8261 parens
.require_open (parser
);
8263 saved_message
= parser
->type_definition_forbidden_message
;
8264 parser
->type_definition_forbidden_message
8265 = G_("types may not be defined in %<noexcept%> expressions");
8267 saved_integral_constant_expression_p
8268 = parser
->integral_constant_expression_p
;
8269 saved_non_integral_constant_expression_p
8270 = parser
->non_integral_constant_expression_p
;
8271 parser
->integral_constant_expression_p
= false;
8273 saved_greater_than_is_operator_p
8274 = parser
->greater_than_is_operator_p
;
8275 parser
->greater_than_is_operator_p
= true;
8277 ++cp_unevaluated_operand
;
8278 ++c_inhibit_evaluation_warnings
;
8279 ++cp_noexcept_operand
;
8280 expr
= cp_parser_expression (parser
);
8281 --cp_noexcept_operand
;
8282 --c_inhibit_evaluation_warnings
;
8283 --cp_unevaluated_operand
;
8285 parser
->greater_than_is_operator_p
8286 = saved_greater_than_is_operator_p
;
8288 parser
->integral_constant_expression_p
8289 = saved_integral_constant_expression_p
;
8290 parser
->non_integral_constant_expression_p
8291 = saved_non_integral_constant_expression_p
;
8293 parser
->type_definition_forbidden_message
= saved_message
;
8295 location_t finish_loc
8296 = cp_lexer_peek_token (parser
->lexer
)->location
;
8297 parens
.require_close (parser
);
8299 /* Construct a location of the form:
8302 with start == caret, finishing at the close-paren. */
8303 location_t noexcept_loc
8304 = make_location (start_loc
, start_loc
, finish_loc
);
8306 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8315 /* Look for the `:: new' and `:: delete', which also signal the
8316 beginning of a new-expression, or delete-expression,
8317 respectively. If the next token is `::', then it might be one of
8319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8323 /* See if the token after the `::' is one of the keywords in
8324 which we're interested. */
8325 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8326 /* If it's `new', we have a new-expression. */
8327 if (keyword
== RID_NEW
)
8328 return cp_parser_new_expression (parser
);
8329 /* Similarly, for `delete'. */
8330 else if (keyword
== RID_DELETE
)
8331 return cp_parser_delete_expression (parser
);
8334 /* Look for a unary operator. */
8335 unary_operator
= cp_parser_unary_operator (token
);
8336 /* The `++' and `--' operators can be handled similarly, even though
8337 they are not technically unary-operators in the grammar. */
8338 if (unary_operator
== ERROR_MARK
)
8340 if (token
->type
== CPP_PLUS_PLUS
)
8341 unary_operator
= PREINCREMENT_EXPR
;
8342 else if (token
->type
== CPP_MINUS_MINUS
)
8343 unary_operator
= PREDECREMENT_EXPR
;
8344 /* Handle the GNU address-of-label extension. */
8345 else if (cp_parser_allow_gnu_extensions_p (parser
)
8346 && token
->type
== CPP_AND_AND
)
8350 location_t start_loc
= token
->location
;
8352 /* Consume the '&&' token. */
8353 cp_lexer_consume_token (parser
->lexer
);
8354 /* Look for the identifier. */
8355 location_t finish_loc
8356 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8357 identifier
= cp_parser_identifier (parser
);
8358 /* Construct a location of the form:
8361 with caret==start at the "&&", finish at the end of the label. */
8362 location_t combined_loc
8363 = make_location (start_loc
, start_loc
, finish_loc
);
8364 /* Create an expression representing the address. */
8365 expression
= finish_label_address_expr (identifier
, combined_loc
);
8366 if (cp_parser_non_integral_constant_expression (parser
,
8368 expression
= error_mark_node
;
8372 if (unary_operator
!= ERROR_MARK
)
8374 cp_expr cast_expression
;
8375 cp_expr expression
= error_mark_node
;
8376 non_integral_constant non_constant_p
= NIC_NONE
;
8377 location_t loc
= token
->location
;
8378 tsubst_flags_t complain
= complain_flags (decltype_p
);
8380 /* Consume the operator token. */
8381 token
= cp_lexer_consume_token (parser
->lexer
);
8382 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8384 /* Parse the cast-expression. */
8386 = cp_parser_cast_expression (parser
,
8387 unary_operator
== ADDR_EXPR
,
8393 OP_TOKEN CAST_EXPRESSION
8394 ^~~~~~~~~~~~~~~~~~~~~~~~~
8395 with start==caret at the operator token, and
8396 extending to the end of the cast_expression. */
8397 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8399 /* Now, build an appropriate representation. */
8400 switch (unary_operator
)
8403 non_constant_p
= NIC_STAR
;
8404 expression
= build_x_indirect_ref (loc
, cast_expression
,
8407 /* TODO: build_x_indirect_ref does not always honor the
8408 location, so ensure it is set. */
8409 expression
.set_location (loc
);
8413 non_constant_p
= NIC_ADDR
;
8416 expression
= build_x_unary_op (loc
, unary_operator
,
8419 /* TODO: build_x_unary_op does not always honor the location,
8420 so ensure it is set. */
8421 expression
.set_location (loc
);
8424 case PREINCREMENT_EXPR
:
8425 case PREDECREMENT_EXPR
:
8426 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8427 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8430 /* Immediately fold negation of a constant, unless the constant is 0
8431 (since -0 == 0) or it would overflow. */
8432 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8435 = tree_strip_any_location_wrapper (cast_expression
);
8436 if (CONSTANT_CLASS_P (stripped_expr
)
8437 && !integer_zerop (stripped_expr
)
8438 && !TREE_OVERFLOW (stripped_expr
))
8440 tree folded
= fold_build1 (unary_operator
,
8441 TREE_TYPE (stripped_expr
),
8443 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8445 expression
= maybe_wrap_with_location (folded
, loc
);
8451 case UNARY_PLUS_EXPR
:
8452 case TRUTH_NOT_EXPR
:
8453 expression
= finish_unary_op_expr (loc
, unary_operator
,
8454 cast_expression
, complain
);
8461 if (non_constant_p
!= NIC_NONE
8462 && cp_parser_non_integral_constant_expression (parser
,
8464 expression
= error_mark_node
;
8469 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8470 /*member_access_only_p=*/false,
8475 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8476 unary-operator, the corresponding tree code is returned. */
8478 static enum tree_code
8479 cp_parser_unary_operator (cp_token
* token
)
8481 switch (token
->type
)
8484 return INDIRECT_REF
;
8490 return UNARY_PLUS_EXPR
;
8496 return TRUTH_NOT_EXPR
;
8499 return BIT_NOT_EXPR
;
8506 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8507 Returns a representation of the expression. */
8510 cp_parser_has_attribute_expression (cp_parser
*parser
)
8512 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8514 /* Consume the __builtin_has_attribute token. */
8515 cp_lexer_consume_token (parser
->lexer
);
8517 matching_parens parens
;
8518 if (!parens
.require_open (parser
))
8519 return error_mark_node
;
8521 /* Types cannot be defined in a `sizeof' expression. Save away the
8523 const char *saved_message
= parser
->type_definition_forbidden_message
;
8524 const char *saved_message_arg
8525 = parser
->type_definition_forbidden_message_arg
;
8526 parser
->type_definition_forbidden_message
8527 = G_("types may not be defined in %qs expressions");
8528 parser
->type_definition_forbidden_message_arg
8529 = IDENTIFIER_POINTER (ridpointers
[RID_BUILTIN_HAS_ATTRIBUTE
]);
8531 /* The restrictions on constant-expressions do not apply inside
8532 sizeof expressions. */
8533 bool saved_integral_constant_expression_p
8534 = parser
->integral_constant_expression_p
;
8535 bool saved_non_integral_constant_expression_p
8536 = parser
->non_integral_constant_expression_p
;
8537 parser
->integral_constant_expression_p
= false;
8539 /* Do not actually evaluate the expression. */
8540 ++cp_unevaluated_operand
;
8541 ++c_inhibit_evaluation_warnings
;
8543 tree oper
= NULL_TREE
;
8545 /* We can't be sure yet whether we're looking at a type-id or an
8547 cp_parser_parse_tentatively (parser
);
8549 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8550 parser
->in_type_id_in_expr_p
= true;
8551 /* Look for the type-id. */
8552 oper
= cp_parser_type_id (parser
);
8553 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8555 cp_parser_parse_definitely (parser
);
8557 /* If the type-id production did not work out, then we must be
8558 looking at an expression. */
8559 if (!oper
|| oper
== error_mark_node
)
8560 oper
= cp_parser_assignment_expression (parser
);
8562 STRIP_ANY_LOCATION_WRAPPER (oper
);
8564 /* Go back to evaluating expressions. */
8565 --cp_unevaluated_operand
;
8566 --c_inhibit_evaluation_warnings
;
8568 /* And restore the old one. */
8569 parser
->type_definition_forbidden_message
= saved_message
;
8570 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
8571 parser
->integral_constant_expression_p
8572 = saved_integral_constant_expression_p
;
8573 parser
->non_integral_constant_expression_p
8574 = saved_non_integral_constant_expression_p
;
8576 /* Consume the comma if it's there. */
8577 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8579 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8580 /*consume_paren=*/true);
8581 return error_mark_node
;
8584 /* Parse the attribute specification. */
8586 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8587 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8589 if (oper
!= error_mark_node
)
8591 /* Fold constant expressions used in attributes first. */
8592 cp_check_const_attributes (attr
);
8594 /* Finally, see if OPER has been declared with ATTR. */
8595 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8598 parens
.require_close (parser
);
8602 error_at (atloc
, "expected identifier");
8603 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8606 /* Construct a location e.g. :
8607 __builtin_has_attribute (oper, attr)
8608 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8609 with start == caret at the start of the built-in token,
8610 and with the endpoint at the final closing paren. */
8611 location_t finish_loc
8612 = cp_lexer_previous_token (parser
->lexer
)->location
;
8613 location_t compound_loc
8614 = make_location (start_loc
, start_loc
, finish_loc
);
8616 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8617 ret_expr
.set_location (compound_loc
);
8618 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8622 /* Parse a new-expression.
8625 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8626 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8628 Returns a representation of the expression. */
8631 cp_parser_new_expression (cp_parser
* parser
)
8633 bool global_scope_p
;
8634 vec
<tree
, va_gc
> *placement
;
8636 vec
<tree
, va_gc
> *initializer
;
8637 tree nelts
= NULL_TREE
;
8640 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8642 /* Look for the optional `::' operator. */
8644 = (cp_parser_global_scope_opt (parser
,
8645 /*current_scope_valid_p=*/false)
8647 /* Look for the `new' operator. */
8648 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8649 /* There's no easy way to tell a new-placement from the
8650 `( type-id )' construct. */
8651 cp_parser_parse_tentatively (parser
);
8652 /* Look for a new-placement. */
8653 placement
= cp_parser_new_placement (parser
);
8654 /* If that didn't work out, there's no new-placement. */
8655 if (!cp_parser_parse_definitely (parser
))
8657 if (placement
!= NULL
)
8658 release_tree_vector (placement
);
8662 /* If the next token is a `(', then we have a parenthesized
8664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8667 const char *saved_message
= parser
->type_definition_forbidden_message
;
8669 /* Consume the `('. */
8670 matching_parens parens
;
8671 parens
.consume_open (parser
);
8673 /* Parse the type-id. */
8674 parser
->type_definition_forbidden_message
8675 = G_("types may not be defined in a new-expression");
8677 type_id_in_expr_sentinel
s (parser
);
8678 type
= cp_parser_type_id (parser
);
8680 parser
->type_definition_forbidden_message
= saved_message
;
8682 /* Look for the closing `)'. */
8683 parens
.require_close (parser
);
8684 token
= cp_lexer_peek_token (parser
->lexer
);
8685 /* There should not be a direct-new-declarator in this production,
8686 but GCC used to allowed this, so we check and emit a sensible error
8687 message for this case. */
8688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8690 error_at (token
->location
,
8691 "array bound forbidden after parenthesized type-id");
8692 inform (token
->location
,
8693 "try removing the parentheses around the type-id");
8694 cp_parser_direct_new_declarator (parser
);
8697 /* Otherwise, there must be a new-type-id. */
8699 type
= cp_parser_new_type_id (parser
, &nelts
);
8701 /* If the next token is a `(' or '{', then we have a new-initializer. */
8702 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8703 if (token
->type
== CPP_OPEN_PAREN
8704 || token
->type
== CPP_OPEN_BRACE
)
8705 initializer
= cp_parser_new_initializer (parser
);
8709 /* A new-expression may not appear in an integral constant
8711 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8712 ret
= error_mark_node
;
8713 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8714 of a new-type-id or type-id of a new-expression, the new-expression shall
8715 contain a new-initializer of the form ( assignment-expression )".
8716 Additionally, consistently with the spirit of DR 1467, we want to accept
8717 'new auto { 2 }' too. */
8718 else if ((ret
= type_uses_auto (type
))
8719 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8720 && (vec_safe_length (initializer
) != 1
8721 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8722 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8724 error_at (token
->location
,
8725 "initialization of new-expression for type %<auto%> "
8726 "requires exactly one element");
8727 ret
= error_mark_node
;
8731 /* Construct a location e.g.:
8734 with caret == start at the start of the "new" token, and the end
8735 at the end of the final token we consumed. */
8736 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8737 location_t end_loc
= get_finish (end_tok
->location
);
8738 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8740 /* Create a representation of the new-expression. */
8741 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8742 tf_warning_or_error
);
8743 protected_set_expr_location (ret
, combined_loc
);
8746 if (placement
!= NULL
)
8747 release_tree_vector (placement
);
8748 if (initializer
!= NULL
)
8749 release_tree_vector (initializer
);
8754 /* Parse a new-placement.
8759 Returns the same representation as for an expression-list. */
8761 static vec
<tree
, va_gc
> *
8762 cp_parser_new_placement (cp_parser
* parser
)
8764 vec
<tree
, va_gc
> *expression_list
;
8766 /* Parse the expression-list. */
8767 expression_list
= (cp_parser_parenthesized_expression_list
8768 (parser
, non_attr
, /*cast_p=*/false,
8769 /*allow_expansion_p=*/true,
8770 /*non_constant_p=*/NULL
));
8772 if (expression_list
&& expression_list
->is_empty ())
8773 error ("expected expression-list or type-id");
8775 return expression_list
;
8778 /* Parse a new-type-id.
8781 type-specifier-seq new-declarator [opt]
8783 Returns the TYPE allocated. If the new-type-id indicates an array
8784 type, *NELTS is set to the number of elements in the last array
8785 bound; the TYPE will not include the last array bound. */
8788 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8790 cp_decl_specifier_seq type_specifier_seq
;
8791 cp_declarator
*new_declarator
;
8792 cp_declarator
*declarator
;
8793 cp_declarator
*outer_declarator
;
8794 const char *saved_message
;
8796 /* The type-specifier sequence must not contain type definitions.
8797 (It cannot contain declarations of new types either, but if they
8798 are not definitions we will catch that because they are not
8800 saved_message
= parser
->type_definition_forbidden_message
;
8801 parser
->type_definition_forbidden_message
8802 = G_("types may not be defined in a new-type-id");
8803 /* Parse the type-specifier-seq. */
8804 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8805 /*is_declaration=*/false,
8806 /*is_trailing_return=*/false,
8807 &type_specifier_seq
);
8808 /* Restore the old message. */
8809 parser
->type_definition_forbidden_message
= saved_message
;
8811 if (type_specifier_seq
.type
== error_mark_node
)
8812 return error_mark_node
;
8814 /* Parse the new-declarator. */
8815 new_declarator
= cp_parser_new_declarator_opt (parser
);
8817 /* Determine the number of elements in the last array dimension, if
8820 /* Skip down to the last array dimension. */
8821 declarator
= new_declarator
;
8822 outer_declarator
= NULL
;
8823 while (declarator
&& (declarator
->kind
== cdk_pointer
8824 || declarator
->kind
== cdk_ptrmem
))
8826 outer_declarator
= declarator
;
8827 declarator
= declarator
->declarator
;
8830 && declarator
->kind
== cdk_array
8831 && declarator
->declarator
8832 && declarator
->declarator
->kind
== cdk_array
)
8834 outer_declarator
= declarator
;
8835 declarator
= declarator
->declarator
;
8838 if (declarator
&& declarator
->kind
== cdk_array
)
8840 *nelts
= declarator
->u
.array
.bounds
;
8841 if (*nelts
== error_mark_node
)
8842 *nelts
= integer_one_node
;
8844 if (outer_declarator
)
8845 outer_declarator
->declarator
= declarator
->declarator
;
8847 new_declarator
= NULL
;
8850 return groktypename (&type_specifier_seq
, new_declarator
, false);
8853 /* Parse an (optional) new-declarator.
8856 ptr-operator new-declarator [opt]
8857 direct-new-declarator
8859 Returns the declarator. */
8861 static cp_declarator
*
8862 cp_parser_new_declarator_opt (cp_parser
* parser
)
8864 enum tree_code code
;
8865 tree type
, std_attributes
= NULL_TREE
;
8866 cp_cv_quals cv_quals
;
8868 /* We don't know if there's a ptr-operator next, or not. */
8869 cp_parser_parse_tentatively (parser
);
8870 /* Look for a ptr-operator. */
8871 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8872 /* If that worked, look for more new-declarators. */
8873 if (cp_parser_parse_definitely (parser
))
8875 cp_declarator
*declarator
;
8877 /* Parse another optional declarator. */
8878 declarator
= cp_parser_new_declarator_opt (parser
);
8880 declarator
= cp_parser_make_indirect_declarator
8881 (code
, type
, cv_quals
, declarator
, std_attributes
);
8886 /* If the next token is a `[', there is a direct-new-declarator. */
8887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8888 return cp_parser_direct_new_declarator (parser
);
8893 /* Parse a direct-new-declarator.
8895 direct-new-declarator:
8897 direct-new-declarator [constant-expression]
8901 static cp_declarator
*
8902 cp_parser_direct_new_declarator (cp_parser
* parser
)
8904 cp_declarator
*declarator
= NULL
;
8911 /* Look for the opening `['. */
8912 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8914 token
= cp_lexer_peek_token (parser
->lexer
);
8915 expression
= cp_parser_expression (parser
);
8916 /* The standard requires that the expression have integral
8917 type. DR 74 adds enumeration types. We believe that the
8918 real intent is that these expressions be handled like the
8919 expression in a `switch' condition, which also allows
8920 classes with a single conversion to integral or
8921 enumeration type. */
8922 if (!processing_template_decl
)
8925 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8930 error_at (token
->location
,
8931 "expression in new-declarator must have integral "
8932 "or enumeration type");
8933 expression
= error_mark_node
;
8937 /* Look for the closing `]'. */
8938 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8940 /* Add this bound to the declarator. */
8941 declarator
= make_array_declarator (declarator
, expression
);
8943 /* If the next token is not a `[', then there are no more
8945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8952 /* Parse a new-initializer.
8955 ( expression-list [opt] )
8958 Returns a representation of the expression-list. */
8960 static vec
<tree
, va_gc
> *
8961 cp_parser_new_initializer (cp_parser
* parser
)
8963 vec
<tree
, va_gc
> *expression_list
;
8965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8968 bool expr_non_constant_p
;
8969 cp_lexer_set_source_position (parser
->lexer
);
8970 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8971 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8972 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8973 expression_list
= make_tree_vector_single (t
);
8976 expression_list
= (cp_parser_parenthesized_expression_list
8977 (parser
, non_attr
, /*cast_p=*/false,
8978 /*allow_expansion_p=*/true,
8979 /*non_constant_p=*/NULL
));
8981 return expression_list
;
8984 /* Parse a delete-expression.
8987 :: [opt] delete cast-expression
8988 :: [opt] delete [ ] cast-expression
8990 Returns a representation of the expression. */
8993 cp_parser_delete_expression (cp_parser
* parser
)
8995 bool global_scope_p
;
8999 /* Look for the optional `::' operator. */
9001 = (cp_parser_global_scope_opt (parser
,
9002 /*current_scope_valid_p=*/false)
9004 /* Look for the `delete' keyword. */
9005 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
9006 /* See if the array syntax is in use. */
9007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9009 /* Consume the `[' token. */
9010 cp_lexer_consume_token (parser
->lexer
);
9011 /* Look for the `]' token. */
9012 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9013 /* Remember that this is the `[]' construct. */
9019 /* Parse the cast-expression. */
9020 expression
= cp_parser_simple_cast_expression (parser
);
9022 /* A delete-expression may not appear in an integral constant
9024 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9025 return error_mark_node
;
9027 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
9028 tf_warning_or_error
);
9031 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9032 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9036 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9039 switch (token
->type
)
9045 case CPP_CLOSE_SQUARE
:
9046 case CPP_CLOSE_PAREN
:
9047 case CPP_CLOSE_BRACE
:
9048 case CPP_OPEN_BRACE
:
9052 case CPP_DEREF_STAR
:
9060 case CPP_GREATER_EQ
:
9081 case CPP_OPEN_PAREN
:
9082 /* In ((type ()) () the last () isn't a valid cast-expression,
9083 so the whole must be parsed as postfix-expression. */
9084 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9087 case CPP_OPEN_SQUARE
:
9088 /* '[' may start a primary-expression in obj-c++ and in C++11,
9089 as a lambda-expression, eg, '(void)[]{}'. */
9090 if (cxx_dialect
>= cxx11
)
9092 return c_dialect_objc ();
9095 case CPP_MINUS_MINUS
:
9096 /* '++' and '--' may or may not start a cast-expression:
9098 struct T { void operator++(int); };
9099 void f() { (T())++; }
9112 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9113 in the order: const_cast, static_cast, reinterpret_cast.
9115 Don't suggest dynamic_cast.
9117 Return the first legal cast kind found, or NULL otherwise. */
9120 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9124 /* Reuse the parser logic by attempting to build the various kinds of
9125 cast, with "complain" disabled.
9126 Identify the first such cast that is valid. */
9128 /* Don't attempt to run such logic within template processing. */
9129 if (processing_template_decl
)
9132 /* First try const_cast. */
9133 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9134 if (trial
!= error_mark_node
)
9135 return "const_cast";
9137 /* If that fails, try static_cast. */
9138 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9139 if (trial
!= error_mark_node
)
9140 return "static_cast";
9142 /* Finally, try reinterpret_cast. */
9143 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9144 if (trial
!= error_mark_node
)
9145 return "reinterpret_cast";
9147 /* No such cast possible. */
9151 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9152 suggesting how to convert a C-style cast of the form:
9156 to a C++-style cast.
9158 The primary range of RICHLOC is asssumed to be that of the original
9159 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9160 of the parens in the C-style cast. */
9163 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9164 location_t close_paren_loc
, tree orig_expr
,
9167 /* This function is non-trivial, so bail out now if the warning isn't
9168 going to be emitted. */
9169 if (!warn_old_style_cast
)
9172 /* Try to find a legal C++ cast, trying them in order:
9173 const_cast, static_cast, reinterpret_cast. */
9174 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9175 if (!cast_suggestion
)
9178 /* Replace the open paren with "CAST_SUGGESTION<". */
9180 pp_printf (&pp
, "%s<", cast_suggestion
);
9181 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9183 /* Replace the close paren with "> (". */
9184 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9186 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9187 rich_loc
->add_fixit_insert_after (")");
9191 /* Parse a cast-expression.
9195 ( type-id ) cast-expression
9197 ADDRESS_P is true iff the unary-expression is appearing as the
9198 operand of the `&' operator. CAST_P is true if this expression is
9199 the target of a cast.
9201 Returns a representation of the expression. */
9204 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9205 bool decltype_p
, cp_id_kind
* pidk
)
9207 /* If it's a `(', then we might be looking at a cast. */
9208 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9210 tree type
= NULL_TREE
;
9211 cp_expr
expr (NULL_TREE
);
9212 int cast_expression
= 0;
9213 const char *saved_message
;
9215 /* There's no way to know yet whether or not this is a cast.
9216 For example, `(int (3))' is a unary-expression, while `(int)
9217 3' is a cast. So, we resort to parsing tentatively. */
9218 cp_parser_parse_tentatively (parser
);
9219 /* Types may not be defined in a cast. */
9220 saved_message
= parser
->type_definition_forbidden_message
;
9221 parser
->type_definition_forbidden_message
9222 = G_("types may not be defined in casts");
9223 /* Consume the `('. */
9224 matching_parens parens
;
9225 cp_token
*open_paren
= parens
.consume_open (parser
);
9226 location_t open_paren_loc
= open_paren
->location
;
9227 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9229 /* A very tricky bit is that `(struct S) { 3 }' is a
9230 compound-literal (which we permit in C++ as an extension).
9231 But, that construct is not a cast-expression -- it is a
9232 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9233 is legal; if the compound-literal were a cast-expression,
9234 you'd need an extra set of parentheses.) But, if we parse
9235 the type-id, and it happens to be a class-specifier, then we
9236 will commit to the parse at that point, because we cannot
9237 undo the action that is done when creating a new class. So,
9238 then we cannot back up and do a postfix-expression.
9240 Another tricky case is the following (c++/29234):
9242 struct S { void operator () (); };
9249 As a type-id we parse the parenthesized S()() as a function
9250 returning a function, groktypename complains and we cannot
9251 back up in this case either.
9253 Therefore, we scan ahead to the closing `)', and check to see
9254 if the tokens after the `)' can start a cast-expression. Otherwise
9255 we are dealing with an unary-expression, a postfix-expression
9258 Yet another tricky case, in C++11, is the following (c++/54891):
9262 The issue is that usually, besides the case of lambda-expressions,
9263 the parenthesized type-id cannot be followed by '[', and, eg, we
9264 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9265 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9266 we don't commit, we try a cast-expression, then an unary-expression.
9268 Save tokens so that we can put them back. */
9269 cp_lexer_save_tokens (parser
->lexer
);
9271 /* We may be looking at a cast-expression. */
9272 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9273 /*consume_paren=*/true))
9275 = cp_parser_tokens_start_cast_expression (parser
);
9277 /* Roll back the tokens we skipped. */
9278 cp_lexer_rollback_tokens (parser
->lexer
);
9279 /* If we aren't looking at a cast-expression, simulate an error so
9280 that the call to cp_parser_error_occurred below returns true. */
9281 if (!cast_expression
)
9282 cp_parser_simulate_error (parser
);
9285 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9286 parser
->in_type_id_in_expr_p
= true;
9287 /* Look for the type-id. */
9288 type
= cp_parser_type_id (parser
);
9289 /* Look for the closing `)'. */
9290 cp_token
*close_paren
= parens
.require_close (parser
);
9292 close_paren_loc
= close_paren
->location
;
9293 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9296 /* Restore the saved message. */
9297 parser
->type_definition_forbidden_message
= saved_message
;
9299 /* At this point this can only be either a cast or a
9300 parenthesized ctor such as `(T ())' that looks like a cast to
9301 function returning T. */
9302 if (!cp_parser_error_occurred (parser
))
9304 /* Only commit if the cast-expression doesn't start with
9305 '++', '--', or '[' in C++11. */
9306 if (cast_expression
> 0)
9307 cp_parser_commit_to_topmost_tentative_parse (parser
);
9309 expr
= cp_parser_cast_expression (parser
,
9310 /*address_p=*/false,
9312 /*decltype_p=*/false,
9315 if (cp_parser_parse_definitely (parser
))
9317 /* Warn about old-style casts, if so requested. */
9318 if (warn_old_style_cast
9319 && !in_system_header_at (input_location
)
9320 && !VOID_TYPE_P (type
)
9321 && current_lang_name
!= lang_name_c
)
9323 gcc_rich_location
rich_loc (input_location
);
9324 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9326 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9327 "use of old-style cast to %q#T", type
);
9330 /* Only type conversions to integral or enumeration types
9331 can be used in constant-expressions. */
9332 if (!cast_valid_in_integral_constant_expression_p (type
)
9333 && cp_parser_non_integral_constant_expression (parser
,
9335 return error_mark_node
;
9337 /* Perform the cast. */
9341 with start==caret at the open paren, extending to the
9343 location_t cast_loc
= make_location (open_paren_loc
,
9345 expr
.get_finish ());
9346 expr
= build_c_cast (cast_loc
, type
, expr
);
9351 cp_parser_abort_tentative_parse (parser
);
9354 /* If we get here, then it's not a cast, so it must be a
9355 unary-expression. */
9356 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9357 cast_p
, decltype_p
);
9360 /* Parse a binary expression of the general form:
9364 pm-expression .* cast-expression
9365 pm-expression ->* cast-expression
9367 multiplicative-expression:
9369 multiplicative-expression * pm-expression
9370 multiplicative-expression / pm-expression
9371 multiplicative-expression % pm-expression
9373 additive-expression:
9374 multiplicative-expression
9375 additive-expression + multiplicative-expression
9376 additive-expression - multiplicative-expression
9380 shift-expression << additive-expression
9381 shift-expression >> additive-expression
9383 relational-expression:
9385 relational-expression < shift-expression
9386 relational-expression > shift-expression
9387 relational-expression <= shift-expression
9388 relational-expression >= shift-expression
9392 relational-expression:
9393 relational-expression <? shift-expression
9394 relational-expression >? shift-expression
9396 equality-expression:
9397 relational-expression
9398 equality-expression == relational-expression
9399 equality-expression != relational-expression
9403 and-expression & equality-expression
9405 exclusive-or-expression:
9407 exclusive-or-expression ^ and-expression
9409 inclusive-or-expression:
9410 exclusive-or-expression
9411 inclusive-or-expression | exclusive-or-expression
9413 logical-and-expression:
9414 inclusive-or-expression
9415 logical-and-expression && inclusive-or-expression
9417 logical-or-expression:
9418 logical-and-expression
9419 logical-or-expression || logical-and-expression
9421 All these are implemented with a single function like:
9424 simple-cast-expression
9425 binary-expression <token> binary-expression
9427 CAST_P is true if this expression is the target of a cast.
9429 The binops_by_token map is used to get the tree codes for each <token> type.
9430 binary-expressions are associated according to a precedence table. */
9432 #define TOKEN_PRECEDENCE(token) \
9433 (((token->type == CPP_GREATER \
9434 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9435 && !parser->greater_than_is_operator_p) \
9436 ? PREC_NOT_OPERATOR \
9437 : binops_by_token[token->type].prec)
9440 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9441 bool no_toplevel_fold_p
,
9443 enum cp_parser_prec prec
,
9446 cp_parser_expression_stack stack
;
9447 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9448 cp_parser_expression_stack_entry
*disable_warnings_sp
= NULL
;
9449 cp_parser_expression_stack_entry current
;
9452 enum tree_code rhs_type
;
9453 enum cp_parser_prec new_prec
, lookahead_prec
;
9456 /* Parse the first expression. */
9457 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9458 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9459 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9460 cast_p
, decltype_p
, pidk
);
9461 current
.prec
= prec
;
9463 if (cp_parser_error_occurred (parser
))
9464 return error_mark_node
;
9468 /* Get an operator token. */
9469 token
= cp_lexer_peek_token (parser
->lexer
);
9471 if (warn_cxx11_compat
9472 && token
->type
== CPP_RSHIFT
9473 && !parser
->greater_than_is_operator_p
)
9475 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9476 "%<>>%> operator is treated"
9477 " as two right angle brackets in C++11"))
9478 inform (token
->location
,
9479 "suggest parentheses around %<>>%> expression");
9482 new_prec
= TOKEN_PRECEDENCE (token
);
9483 if (new_prec
!= PREC_NOT_OPERATOR
9484 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9485 /* This is a fold-expression; handle it later. */
9486 new_prec
= PREC_NOT_OPERATOR
;
9488 /* Popping an entry off the stack means we completed a subexpression:
9489 - either we found a token which is not an operator (`>' where it is not
9490 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9491 will happen repeatedly;
9492 - or, we found an operator which has lower priority. This is the case
9493 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9495 if (new_prec
<= current
.prec
)
9504 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9505 current
.loc
= token
->location
;
9507 /* We used the operator token. */
9508 cp_lexer_consume_token (parser
->lexer
);
9510 /* For "false && x" or "true || x", x will never be executed;
9511 disable warnings while evaluating it. */
9512 if ((current
.tree_type
== TRUTH_ANDIF_EXPR
9513 && cp_fully_fold (current
.lhs
) == truthvalue_false_node
)
9514 || (current
.tree_type
== TRUTH_ORIF_EXPR
9515 && cp_fully_fold (current
.lhs
) == truthvalue_true_node
))
9517 disable_warnings_sp
= sp
;
9518 ++c_inhibit_evaluation_warnings
;
9521 /* Extract another operand. It may be the RHS of this expression
9522 or the LHS of a new, higher priority expression. */
9523 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9524 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9525 rhs
= cp_parser_simple_cast_expression (parser
);
9527 /* Get another operator token. Look up its precedence to avoid
9528 building a useless (immediately popped) stack entry for common
9529 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9530 token
= cp_lexer_peek_token (parser
->lexer
);
9531 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9532 if (lookahead_prec
!= PREC_NOT_OPERATOR
9533 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9534 lookahead_prec
= PREC_NOT_OPERATOR
;
9535 if (lookahead_prec
> new_prec
)
9537 /* ... and prepare to parse the RHS of the new, higher priority
9538 expression. Since precedence levels on the stack are
9539 monotonically increasing, we do not have to care about
9544 current
.lhs_type
= rhs_type
;
9545 current
.prec
= new_prec
;
9546 new_prec
= lookahead_prec
;
9550 lookahead_prec
= new_prec
;
9551 /* If the stack is not empty, we have parsed into LHS the right side
9552 (`4' in the example above) of an expression we had suspended.
9553 We can use the information on the stack to recover the LHS (`3')
9554 from the stack together with the tree code (`MULT_EXPR'), and
9555 the precedence of the higher level subexpression
9556 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9557 which will be used to actually build the additive expression. */
9559 rhs_type
= current
.lhs_type
;
9564 /* Undo the disabling of warnings done above. */
9565 if (sp
== disable_warnings_sp
)
9567 disable_warnings_sp
= NULL
;
9568 --c_inhibit_evaluation_warnings
;
9571 if (warn_logical_not_paren
9572 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9573 && current
.lhs_type
== TRUTH_NOT_EXPR
9574 /* Avoid warning for !!x == y. */
9575 && (TREE_CODE (current
.lhs
) != NE_EXPR
9576 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9577 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9578 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9579 /* Avoid warning for !b == y where b is boolean. */
9580 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9581 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9583 /* Avoid warning for !!b == y where b is boolean. */
9584 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9585 || TREE_TYPE (current
.lhs
) == NULL_TREE
9586 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9587 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9588 current
.lhs
, maybe_constant_value (rhs
));
9592 location_t combined_loc
= make_location (current
.loc
,
9593 current
.lhs
.get_start (),
9596 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9597 ERROR_MARK for everything that is not a binary expression.
9598 This makes warn_about_parentheses miss some warnings that
9599 involve unary operators. For unary expressions we should
9600 pass the correct tree_code unless the unary expression was
9601 surrounded by parentheses.
9603 if (no_toplevel_fold_p
9604 && lookahead_prec
<= current
.prec
9607 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9608 current
.lhs
= error_mark_node
;
9612 = build_min (current
.tree_type
,
9613 TREE_CODE_CLASS (current
.tree_type
)
9615 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9616 current
.lhs
.get_value (), rhs
.get_value ());
9617 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9622 op_location_t
op_loc (current
.loc
, combined_loc
);
9623 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9624 current
.lhs
, current
.lhs_type
,
9625 rhs
, rhs_type
, &overload
,
9626 complain_flags (decltype_p
));
9627 /* TODO: build_x_binary_op doesn't always honor the location. */
9628 current
.lhs
.set_location (combined_loc
);
9630 current
.lhs_type
= current
.tree_type
;
9632 /* If the binary operator required the use of an overloaded operator,
9633 then this expression cannot be an integral constant-expression.
9634 An overloaded operator can be used even if both operands are
9635 otherwise permissible in an integral constant-expression if at
9636 least one of the operands is of enumeration type. */
9639 && cp_parser_non_integral_constant_expression (parser
,
9641 return error_mark_node
;
9648 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9649 bool no_toplevel_fold_p
,
9650 enum cp_parser_prec prec
,
9653 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9654 /*decltype*/false, prec
, pidk
);
9657 /* Parse the `? expression : assignment-expression' part of a
9658 conditional-expression. The LOGICAL_OR_EXPR is the
9659 logical-or-expression that started the conditional-expression.
9660 Returns a representation of the entire conditional-expression.
9662 This routine is used by cp_parser_assignment_expression.
9664 ? expression : assignment-expression
9668 ? : assignment-expression */
9671 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9673 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9674 cp_expr assignment_expr
;
9675 struct cp_token
*token
;
9676 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9678 /* Consume the `?' token. */
9679 cp_lexer_consume_token (parser
->lexer
);
9680 token
= cp_lexer_peek_token (parser
->lexer
);
9681 if (cp_parser_allow_gnu_extensions_p (parser
)
9682 && token
->type
== CPP_COLON
)
9684 pedwarn (token
->location
, OPT_Wpedantic
,
9685 "ISO C++ does not allow ?: with omitted middle operand");
9686 /* Implicit true clause. */
9688 c_inhibit_evaluation_warnings
+=
9689 folded_logical_or_expr
== truthvalue_true_node
;
9690 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9694 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9695 parser
->colon_corrects_to_scope_p
= false;
9696 /* Parse the expression. */
9697 c_inhibit_evaluation_warnings
+=
9698 folded_logical_or_expr
== truthvalue_false_node
;
9699 expr
= cp_parser_expression (parser
);
9700 c_inhibit_evaluation_warnings
+=
9701 ((folded_logical_or_expr
== truthvalue_true_node
)
9702 - (folded_logical_or_expr
== truthvalue_false_node
));
9703 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9706 /* The next token should be a `:'. */
9707 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9708 /* Parse the assignment-expression. */
9709 assignment_expr
= cp_parser_assignment_expression (parser
);
9710 c_inhibit_evaluation_warnings
-=
9711 folded_logical_or_expr
== truthvalue_true_node
;
9714 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9715 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9716 with the caret at the "?", ranging from the start of
9717 the logical_or_expr to the end of the assignment_expr. */
9718 loc
= make_location (loc
,
9719 logical_or_expr
.get_start (),
9720 assignment_expr
.get_finish ());
9722 /* Build the conditional-expression. */
9723 return build_x_conditional_expr (loc
, logical_or_expr
,
9726 tf_warning_or_error
);
9729 /* Parse an assignment-expression.
9731 assignment-expression:
9732 conditional-expression
9733 logical-or-expression assignment-operator assignment_expression
9736 CAST_P is true if this expression is the target of a cast.
9737 DECLTYPE_P is true if this expression is the operand of decltype.
9739 Returns a representation for the expression. */
9742 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9743 bool cast_p
, bool decltype_p
)
9747 /* If the next token is the `throw' keyword, then we're looking at
9748 a throw-expression. */
9749 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9750 expr
= cp_parser_throw_expression (parser
);
9751 /* Otherwise, it must be that we are looking at a
9752 logical-or-expression. */
9755 /* Parse the binary expressions (logical-or-expression). */
9756 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9758 PREC_NOT_OPERATOR
, pidk
);
9759 /* If the next token is a `?' then we're actually looking at a
9760 conditional-expression. */
9761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9762 return cp_parser_question_colon_clause (parser
, expr
);
9765 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9767 /* If it's an assignment-operator, we're using the second
9769 enum tree_code assignment_operator
9770 = cp_parser_assignment_operator_opt (parser
);
9771 if (assignment_operator
!= ERROR_MARK
)
9773 bool non_constant_p
;
9775 /* Parse the right-hand side of the assignment. */
9776 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9779 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9780 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9782 /* An assignment may not appear in a
9783 constant-expression. */
9784 if (cp_parser_non_integral_constant_expression (parser
,
9786 return error_mark_node
;
9787 /* Build the assignment expression. Its default
9791 is the location of the '=' token as the
9792 caret, ranging from the start of the lhs to the
9794 loc
= make_location (loc
,
9797 expr
= build_x_modify_expr (loc
, expr
,
9798 assignment_operator
,
9800 complain_flags (decltype_p
));
9801 /* TODO: build_x_modify_expr doesn't honor the location,
9802 so we must set it here. */
9803 expr
.set_location (loc
);
9811 /* Parse an (optional) assignment-operator.
9813 assignment-operator: one of
9814 = *= /= %= += -= >>= <<= &= ^= |=
9818 assignment-operator: one of
9821 If the next token is an assignment operator, the corresponding tree
9822 code is returned, and the token is consumed. For example, for
9823 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9824 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9825 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9826 operator, ERROR_MARK is returned. */
9828 static enum tree_code
9829 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9834 /* Peek at the next token. */
9835 token
= cp_lexer_peek_token (parser
->lexer
);
9837 switch (token
->type
)
9848 op
= TRUNC_DIV_EXPR
;
9852 op
= TRUNC_MOD_EXPR
;
9884 /* Nothing else is an assignment operator. */
9888 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9889 if (op
!= ERROR_MARK
9890 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9893 /* If it was an assignment operator, consume it. */
9894 if (op
!= ERROR_MARK
)
9895 cp_lexer_consume_token (parser
->lexer
);
9900 /* Parse an expression.
9903 assignment-expression
9904 expression , assignment-expression
9906 CAST_P is true if this expression is the target of a cast.
9907 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9908 except possibly parenthesized or on the RHS of a comma (N3276).
9910 Returns a representation of the expression. */
9913 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9914 bool cast_p
, bool decltype_p
)
9916 cp_expr expression
= NULL_TREE
;
9917 location_t loc
= UNKNOWN_LOCATION
;
9921 cp_expr assignment_expression
;
9923 /* Parse the next assignment-expression. */
9924 assignment_expression
9925 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9927 /* We don't create a temporary for a call that is the immediate operand
9928 of decltype or on the RHS of a comma. But when we see a comma, we
9929 need to create a temporary for a call on the LHS. */
9930 if (decltype_p
&& !processing_template_decl
9931 && TREE_CODE (assignment_expression
) == CALL_EXPR
9932 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9933 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9934 assignment_expression
9935 = build_cplus_new (TREE_TYPE (assignment_expression
),
9936 assignment_expression
, tf_warning_or_error
);
9938 /* If this is the first assignment-expression, we can just
9941 expression
= assignment_expression
;
9944 /* Create a location with caret at the comma, ranging
9945 from the start of the LHS to the end of the RHS. */
9946 loc
= make_location (loc
,
9947 expression
.get_start (),
9948 assignment_expression
.get_finish ());
9949 expression
= build_x_compound_expr (loc
, expression
,
9950 assignment_expression
,
9951 complain_flags (decltype_p
));
9952 expression
.set_location (loc
);
9954 /* If the next token is not a comma, or we're in a fold-expression, then
9955 we are done with the expression. */
9956 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9957 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9959 /* Consume the `,'. */
9960 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9961 cp_lexer_consume_token (parser
->lexer
);
9962 /* A comma operator cannot appear in a constant-expression. */
9963 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9964 expression
= error_mark_node
;
9970 /* Parse a constant-expression.
9972 constant-expression:
9973 conditional-expression
9975 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9976 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9977 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9978 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9979 only parse a conditional-expression, otherwise parse an
9980 assignment-expression. See below for rationale. */
9983 cp_parser_constant_expression (cp_parser
* parser
,
9984 bool allow_non_constant_p
,
9985 bool *non_constant_p
,
9988 bool saved_integral_constant_expression_p
;
9989 bool saved_allow_non_integral_constant_expression_p
;
9990 bool saved_non_integral_constant_expression_p
;
9993 /* It might seem that we could simply parse the
9994 conditional-expression, and then check to see if it were
9995 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9996 one that the compiler can figure out is constant, possibly after
9997 doing some simplifications or optimizations. The standard has a
9998 precise definition of constant-expression, and we must honor
9999 that, even though it is somewhat more restrictive.
10005 is not a legal declaration, because `(2, 3)' is not a
10006 constant-expression. The `,' operator is forbidden in a
10007 constant-expression. However, GCC's constant-folding machinery
10008 will fold this operation to an INTEGER_CST for `3'. */
10010 /* Save the old settings. */
10011 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10012 saved_allow_non_integral_constant_expression_p
10013 = parser
->allow_non_integral_constant_expression_p
;
10014 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10015 /* We are now parsing a constant-expression. */
10016 parser
->integral_constant_expression_p
= true;
10017 parser
->allow_non_integral_constant_expression_p
10018 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10019 parser
->non_integral_constant_expression_p
= false;
10020 /* Although the grammar says "conditional-expression", when not STRICT_P,
10021 we parse an "assignment-expression", which also permits
10022 "throw-expression" and the use of assignment operators. In the case
10023 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10024 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10025 actually essential that we look for an assignment-expression.
10026 For example, cp_parser_initializer_clauses uses this function to
10027 determine whether a particular assignment-expression is in fact
10031 /* Parse the binary expressions (logical-or-expression). */
10032 expression
= cp_parser_binary_expression (parser
, false, false, false,
10033 PREC_NOT_OPERATOR
, NULL
);
10034 /* If the next token is a `?' then we're actually looking at
10035 a conditional-expression; otherwise we're done. */
10036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10037 expression
= cp_parser_question_colon_clause (parser
, expression
);
10040 expression
= cp_parser_assignment_expression (parser
);
10041 /* Restore the old settings. */
10042 parser
->integral_constant_expression_p
10043 = saved_integral_constant_expression_p
;
10044 parser
->allow_non_integral_constant_expression_p
10045 = saved_allow_non_integral_constant_expression_p
;
10046 if (cxx_dialect
>= cxx11
)
10048 /* Require an rvalue constant expression here; that's what our
10049 callers expect. Reference constant expressions are handled
10050 separately in e.g. cp_parser_template_argument. */
10051 tree decay
= expression
;
10052 if (TREE_TYPE (expression
)
10053 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10054 decay
= build_address (expression
);
10055 bool is_const
= potential_rvalue_constant_expression (decay
);
10056 parser
->non_integral_constant_expression_p
= !is_const
;
10057 if (!is_const
&& !allow_non_constant_p
)
10058 require_potential_rvalue_constant_expression (decay
);
10060 if (allow_non_constant_p
)
10061 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10062 parser
->non_integral_constant_expression_p
10063 = saved_non_integral_constant_expression_p
;
10068 /* Parse __builtin_offsetof.
10070 offsetof-expression:
10071 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10073 offsetof-member-designator:
10075 | offsetof-member-designator "." id-expression
10076 | offsetof-member-designator "[" expression "]"
10077 | offsetof-member-designator "->" id-expression */
10080 cp_parser_builtin_offsetof (cp_parser
*parser
)
10082 int save_ice_p
, save_non_ice_p
;
10087 location_t finish_loc
;
10089 /* We're about to accept non-integral-constant things, but will
10090 definitely yield an integral constant expression. Save and
10091 restore these values around our local parsing. */
10092 save_ice_p
= parser
->integral_constant_expression_p
;
10093 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10095 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10097 /* Consume the "__builtin_offsetof" token. */
10098 cp_lexer_consume_token (parser
->lexer
);
10099 /* Consume the opening `('. */
10100 matching_parens parens
;
10101 parens
.require_open (parser
);
10102 /* Parse the type-id. */
10103 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10105 const char *saved_message
= parser
->type_definition_forbidden_message
;
10106 parser
->type_definition_forbidden_message
10107 = G_("types may not be defined within %<__builtin_offsetof%>");
10108 type
= cp_parser_type_id (parser
);
10109 parser
->type_definition_forbidden_message
= saved_message
;
10111 /* Look for the `,'. */
10112 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10113 token
= cp_lexer_peek_token (parser
->lexer
);
10115 /* Build the (type *)null that begins the traditional offsetof macro. */
10117 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10118 tf_warning_or_error
);
10120 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10121 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10122 true, &dummy
, token
->location
);
10125 token
= cp_lexer_peek_token (parser
->lexer
);
10126 switch (token
->type
)
10128 case CPP_OPEN_SQUARE
:
10129 /* offsetof-member-designator "[" expression "]" */
10130 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10135 /* offsetof-member-designator "->" identifier */
10136 expr
= grok_array_decl (token
->location
, expr
,
10137 integer_zero_node
, false);
10141 /* offsetof-member-designator "." identifier */
10142 cp_lexer_consume_token (parser
->lexer
);
10143 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10144 expr
, true, &dummy
,
10148 case CPP_CLOSE_PAREN
:
10149 /* Consume the ")" token. */
10150 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10151 cp_lexer_consume_token (parser
->lexer
);
10155 /* Error. We know the following require will fail, but
10156 that gives the proper error message. */
10157 parens
.require_close (parser
);
10158 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10159 expr
= error_mark_node
;
10165 /* Make a location of the form:
10166 __builtin_offsetof (struct s, f)
10167 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10168 with caret at the type-id, ranging from the start of the
10169 "_builtin_offsetof" token to the close paren. */
10170 loc
= make_location (loc
, start_loc
, finish_loc
);
10171 /* The result will be an INTEGER_CST, so we need to explicitly
10172 preserve the location. */
10173 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10176 parser
->integral_constant_expression_p
= save_ice_p
;
10177 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10179 expr
= expr
.maybe_add_location_wrapper ();
10183 /* Parse a trait expression.
10185 Returns a representation of the expression, the underlying type
10186 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10189 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10191 cp_trait_kind kind
;
10192 tree type1
, type2
= NULL_TREE
;
10193 bool binary
= false;
10194 bool variadic
= false;
10198 case RID_HAS_NOTHROW_ASSIGN
:
10199 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10201 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10202 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10204 case RID_HAS_NOTHROW_COPY
:
10205 kind
= CPTK_HAS_NOTHROW_COPY
;
10207 case RID_HAS_TRIVIAL_ASSIGN
:
10208 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10210 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10211 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10213 case RID_HAS_TRIVIAL_COPY
:
10214 kind
= CPTK_HAS_TRIVIAL_COPY
;
10216 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10217 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10219 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10220 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10222 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10223 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10225 case RID_IS_ABSTRACT
:
10226 kind
= CPTK_IS_ABSTRACT
;
10228 case RID_IS_AGGREGATE
:
10229 kind
= CPTK_IS_AGGREGATE
;
10231 case RID_IS_BASE_OF
:
10232 kind
= CPTK_IS_BASE_OF
;
10236 kind
= CPTK_IS_CLASS
;
10239 kind
= CPTK_IS_EMPTY
;
10242 kind
= CPTK_IS_ENUM
;
10245 kind
= CPTK_IS_FINAL
;
10247 case RID_IS_LITERAL_TYPE
:
10248 kind
= CPTK_IS_LITERAL_TYPE
;
10251 kind
= CPTK_IS_POD
;
10253 case RID_IS_POLYMORPHIC
:
10254 kind
= CPTK_IS_POLYMORPHIC
;
10256 case RID_IS_SAME_AS
:
10257 kind
= CPTK_IS_SAME_AS
;
10260 case RID_IS_STD_LAYOUT
:
10261 kind
= CPTK_IS_STD_LAYOUT
;
10263 case RID_IS_TRIVIAL
:
10264 kind
= CPTK_IS_TRIVIAL
;
10266 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10267 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10270 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10271 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10274 case RID_IS_TRIVIALLY_COPYABLE
:
10275 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10278 kind
= CPTK_IS_UNION
;
10280 case RID_UNDERLYING_TYPE
:
10281 kind
= CPTK_UNDERLYING_TYPE
;
10286 case RID_DIRECT_BASES
:
10287 kind
= CPTK_DIRECT_BASES
;
10289 case RID_IS_ASSIGNABLE
:
10290 kind
= CPTK_IS_ASSIGNABLE
;
10293 case RID_IS_CONSTRUCTIBLE
:
10294 kind
= CPTK_IS_CONSTRUCTIBLE
;
10298 gcc_unreachable ();
10301 /* Get location of initial token. */
10302 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10304 /* Consume the token. */
10305 cp_lexer_consume_token (parser
->lexer
);
10307 matching_parens parens
;
10308 parens
.require_open (parser
);
10311 type_id_in_expr_sentinel
s (parser
);
10312 type1
= cp_parser_type_id (parser
);
10315 if (type1
== error_mark_node
)
10316 return error_mark_node
;
10320 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10323 type_id_in_expr_sentinel
s (parser
);
10324 type2
= cp_parser_type_id (parser
);
10327 if (type2
== error_mark_node
)
10328 return error_mark_node
;
10332 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10334 cp_lexer_consume_token (parser
->lexer
);
10335 tree elt
= cp_parser_type_id (parser
);
10336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10338 cp_lexer_consume_token (parser
->lexer
);
10339 elt
= make_pack_expansion (elt
);
10341 if (elt
== error_mark_node
)
10342 return error_mark_node
;
10343 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10347 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10348 parens
.require_close (parser
);
10350 /* Construct a location of the form:
10351 __is_trivially_copyable(_Tp)
10352 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10353 with start == caret, finishing at the close-paren. */
10354 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10356 /* Complete the trait expression, which may mean either processing
10357 the trait expr now or saving it for template instantiation. */
10360 case CPTK_UNDERLYING_TYPE
:
10361 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10363 return cp_expr (finish_bases (type1
, false), trait_loc
);
10364 case CPTK_DIRECT_BASES
:
10365 return cp_expr (finish_bases (type1
, true), trait_loc
);
10367 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10371 /* Parse a lambda expression.
10374 lambda-introducer lambda-declarator [opt] compound-statement
10376 Returns a representation of the expression. */
10379 cp_parser_lambda_expression (cp_parser
* parser
)
10381 tree lambda_expr
= build_lambda_expr ();
10384 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10385 cp_token_position start
= 0;
10387 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10389 if (cxx_dialect
>= cxx2a
)
10390 /* C++20 allows lambdas in unevaluated context. */;
10391 else if (cp_unevaluated_operand
)
10393 if (!token
->error_reported
)
10395 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10396 "lambda-expression in unevaluated context"
10397 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10398 token
->error_reported
= true;
10402 else if (parser
->in_template_argument_list_p
|| processing_template_parmlist
)
10404 if (!token
->error_reported
)
10406 error_at (token
->location
, "lambda-expression in template-argument"
10407 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10408 token
->error_reported
= true;
10413 /* We may be in the middle of deferred access check. Disable
10415 push_deferring_access_checks (dk_no_deferred
);
10417 cp_parser_lambda_introducer (parser
, lambda_expr
);
10418 if (cp_parser_error_occurred (parser
))
10419 return error_mark_node
;
10421 type
= begin_lambda_type (lambda_expr
);
10422 if (type
== error_mark_node
)
10423 return error_mark_node
;
10425 record_lambda_scope (lambda_expr
);
10427 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10428 determine_visibility (TYPE_NAME (type
));
10430 /* Now that we've started the type, add the capture fields for any
10431 explicit captures. */
10432 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10435 /* Inside the class, surrounding template-parameter-lists do not apply. */
10436 unsigned int saved_num_template_parameter_lists
10437 = parser
->num_template_parameter_lists
;
10438 unsigned char in_statement
= parser
->in_statement
;
10439 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10440 bool fully_implicit_function_template_p
10441 = parser
->fully_implicit_function_template_p
;
10442 tree implicit_template_parms
= parser
->implicit_template_parms
;
10443 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10444 bool auto_is_implicit_function_template_parm_p
10445 = parser
->auto_is_implicit_function_template_parm_p
;
10447 parser
->num_template_parameter_lists
= 0;
10448 parser
->in_statement
= 0;
10449 parser
->in_switch_statement_p
= false;
10450 parser
->fully_implicit_function_template_p
= false;
10451 parser
->implicit_template_parms
= 0;
10452 parser
->implicit_template_scope
= 0;
10453 parser
->auto_is_implicit_function_template_parm_p
= false;
10455 /* By virtue of defining a local class, a lambda expression has access to
10456 the private variables of enclosing classes. */
10458 if (cp_parser_start_tentative_firewall (parser
))
10461 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10463 if (ok
&& cp_parser_error_occurred (parser
))
10468 cp_parser_lambda_body (parser
, lambda_expr
);
10470 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10472 if (cp_parser_skip_to_closing_brace (parser
))
10473 cp_lexer_consume_token (parser
->lexer
);
10476 /* The capture list was built up in reverse order; fix that now. */
10477 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10478 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10481 maybe_add_lambda_conv_op (type
);
10483 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10485 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10486 parser
->in_statement
= in_statement
;
10487 parser
->in_switch_statement_p
= in_switch_statement_p
;
10488 parser
->fully_implicit_function_template_p
10489 = fully_implicit_function_template_p
;
10490 parser
->implicit_template_parms
= implicit_template_parms
;
10491 parser
->implicit_template_scope
= implicit_template_scope
;
10492 parser
->auto_is_implicit_function_template_parm_p
10493 = auto_is_implicit_function_template_parm_p
;
10496 /* This field is only used during parsing of the lambda. */
10497 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10499 /* This lambda shouldn't have any proxies left at this point. */
10500 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10501 /* And now that we're done, push proxies for an enclosing lambda. */
10502 insert_pending_capture_proxies ();
10504 /* Update the lambda expression to a range. */
10505 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10506 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10508 end_tok
->location
);
10511 lambda_expr
= build_lambda_object (lambda_expr
);
10513 lambda_expr
= error_mark_node
;
10515 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10517 pop_deferring_access_checks ();
10519 return lambda_expr
;
10522 /* Parse the beginning of a lambda expression.
10525 [ lambda-capture [opt] ]
10527 LAMBDA_EXPR is the current representation of the lambda expression. */
10530 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10532 /* Need commas after the first capture. */
10535 /* Eat the leading `['. */
10536 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10538 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10539 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10540 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10541 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10542 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10543 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10545 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10547 cp_lexer_consume_token (parser
->lexer
);
10550 if (!(at_function_scope_p () || parsing_nsdmi ()))
10551 error ("non-local lambda expression cannot have a capture-default");
10554 hash_set
<tree
, true> ids
;
10555 tree first_capture_id
= NULL_TREE
;
10556 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10558 cp_token
* capture_token
;
10560 tree capture_init_expr
;
10561 cp_id_kind idk
= CP_ID_KIND_NONE
;
10562 bool explicit_init_p
= false;
10564 enum capture_kind_type
10569 enum capture_kind_type capture_kind
= BY_COPY
;
10571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10573 error ("expected end of capture-list");
10580 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10582 /* Possibly capture `this'. */
10583 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10585 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10586 if (cxx_dialect
< cxx2a
10587 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10588 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10589 "with by-copy capture default");
10590 cp_lexer_consume_token (parser
->lexer
);
10591 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10592 pedwarn (input_location
, 0,
10593 "already captured %qD in lambda expression",
10596 add_capture (lambda_expr
, /*id=*/this_identifier
,
10597 /*initializer=*/finish_this_expr (),
10598 /*by_reference_p=*/true, explicit_init_p
);
10602 /* Possibly capture `*this'. */
10603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10604 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10606 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10607 if (cxx_dialect
< cxx17
)
10608 pedwarn (loc
, 0, "%<*this%> capture only available with "
10609 "%<-std=c++17%> or %<-std=gnu++17%>");
10610 cp_lexer_consume_token (parser
->lexer
);
10611 cp_lexer_consume_token (parser
->lexer
);
10612 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10613 pedwarn (input_location
, 0,
10614 "already captured %qD in lambda expression",
10617 add_capture (lambda_expr
, /*id=*/this_identifier
,
10618 /*initializer=*/finish_this_expr (),
10619 /*by_reference_p=*/false, explicit_init_p
);
10623 bool init_pack_expansion
= false;
10624 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
10625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10627 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10628 if (cxx_dialect
< cxx2a
)
10629 pedwarn (ellipsis_loc
, 0, "pack init-capture only available with "
10630 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10631 cp_lexer_consume_token (parser
->lexer
);
10632 init_pack_expansion
= true;
10635 /* Remember whether we want to capture as a reference or not. */
10636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10638 capture_kind
= BY_REFERENCE
;
10639 cp_lexer_consume_token (parser
->lexer
);
10642 /* Get the identifier. */
10643 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10644 capture_id
= cp_parser_identifier (parser
);
10646 if (capture_id
== error_mark_node
)
10647 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10648 delimiters, but I modified this to stop on unnested ']' as well. It
10649 was already changed to stop on unnested '}', so the
10650 "closing_parenthesis" name is no more misleading with my change. */
10652 cp_parser_skip_to_closing_parenthesis (parser
,
10653 /*recovering=*/true,
10655 /*consume_paren=*/true);
10659 /* Find the initializer for this capture. */
10660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10661 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10662 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10664 bool direct
, non_constant
;
10665 /* An explicit initializer exists. */
10666 if (cxx_dialect
< cxx14
)
10667 pedwarn (input_location
, 0,
10668 "lambda capture initializers "
10669 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10670 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10671 &non_constant
, true);
10672 explicit_init_p
= true;
10673 if (capture_init_expr
== NULL_TREE
)
10675 error ("empty initializer for lambda init-capture");
10676 capture_init_expr
= error_mark_node
;
10678 if (init_pack_expansion
)
10679 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10683 const char* error_msg
;
10685 /* Turn the identifier into an id-expression. */
10687 = cp_parser_lookup_name_simple (parser
, capture_id
,
10688 capture_token
->location
);
10690 if (capture_init_expr
== error_mark_node
)
10692 unqualified_name_lookup_error (capture_id
);
10695 else if (!VAR_P (capture_init_expr
)
10696 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10698 error_at (capture_token
->location
,
10699 "capture of non-variable %qE",
10700 capture_init_expr
);
10701 if (DECL_P (capture_init_expr
))
10702 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10703 "%q#D declared here", capture_init_expr
);
10706 if (VAR_P (capture_init_expr
)
10707 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10709 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10710 "%qD with non-automatic storage duration",
10711 capture_init_expr
))
10712 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10713 "%q#D declared here", capture_init_expr
);
10718 = finish_id_expression
10723 /*integral_constant_expression_p=*/false,
10724 /*allow_non_integral_constant_expression_p=*/false,
10725 /*non_integral_constant_expression_p=*/NULL
,
10726 /*template_p=*/false,
10728 /*address_p=*/false,
10729 /*template_arg_p=*/false,
10731 capture_token
->location
);
10733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10735 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10736 cp_lexer_consume_token (parser
->lexer
);
10737 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10738 if (init_pack_expansion
)
10740 /* If what follows is an initializer, the second '...' is
10741 invalid. But for cases like [...xs...], the first one
10743 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10744 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10745 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10746 ellipsis_loc
= loc
;
10747 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
10753 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10754 && !explicit_init_p
)
10756 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10757 && capture_kind
== BY_COPY
)
10758 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10759 "of %qD redundant with by-copy capture default",
10761 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10762 && capture_kind
== BY_REFERENCE
)
10763 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10764 "capture of %qD redundant with by-reference capture "
10765 "default", capture_id
);
10768 /* Check for duplicates.
10769 Optimize for the zero or one explicit captures cases and only create
10770 the hash_set after adding second capture. */
10771 bool found
= false;
10772 if (!ids
.is_empty ())
10773 found
= ids
.add (capture_id
);
10774 else if (first_capture_id
== NULL_TREE
)
10775 first_capture_id
= capture_id
;
10776 else if (capture_id
== first_capture_id
)
10780 ids
.add (first_capture_id
);
10781 ids
.add (capture_id
);
10784 pedwarn (input_location
, 0,
10785 "already captured %qD in lambda expression", capture_id
);
10787 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
10788 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10791 /* If there is any qualification still in effect, clear it
10792 now; we will be starting fresh with the next capture. */
10793 parser
->scope
= NULL_TREE
;
10794 parser
->qualifying_scope
= NULL_TREE
;
10795 parser
->object_scope
= NULL_TREE
;
10798 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10801 /* Parse the (optional) middle of a lambda expression.
10804 < template-parameter-list [opt] >
10805 ( parameter-declaration-clause [opt] )
10806 attribute-specifier [opt]
10807 decl-specifier-seq [opt]
10808 exception-specification [opt]
10809 lambda-return-type-clause [opt]
10811 LAMBDA_EXPR is the current representation of the lambda expression. */
10814 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10816 /* 5.1.1.4 of the standard says:
10817 If a lambda-expression does not include a lambda-declarator, it is as if
10818 the lambda-declarator were ().
10819 This means an empty parameter list, no attributes, and no exception
10821 tree param_list
= void_list_node
;
10822 tree std_attrs
= NULL_TREE
;
10823 tree gnu_attrs
= NULL_TREE
;
10824 tree exception_spec
= NULL_TREE
;
10825 tree template_param_list
= NULL_TREE
;
10826 tree tx_qual
= NULL_TREE
;
10827 tree return_type
= NULL_TREE
;
10828 cp_decl_specifier_seq lambda_specs
;
10829 clear_decl_specs (&lambda_specs
);
10831 /* The template-parameter-list is optional, but must begin with
10832 an opening angle if present. */
10833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10835 if (cxx_dialect
< cxx14
)
10836 pedwarn (parser
->lexer
->next_token
->location
, 0,
10837 "lambda templates are only available with "
10838 "%<-std=c++14%> or %<-std=gnu++14%>");
10839 else if (cxx_dialect
< cxx2a
)
10840 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10841 "lambda templates are only available with "
10842 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10844 cp_lexer_consume_token (parser
->lexer
);
10846 template_param_list
= cp_parser_template_parameter_list (parser
);
10848 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10850 /* We just processed one more parameter list. */
10851 ++parser
->num_template_parameter_lists
;
10854 /* The parameter-declaration-clause is optional (unless
10855 template-parameter-list was given), but must begin with an
10856 opening parenthesis if present. */
10857 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10859 matching_parens parens
;
10860 parens
.consume_open (parser
);
10862 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10864 /* Parse parameters. */
10866 = cp_parser_parameter_declaration_clause
10867 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
10869 /* Default arguments shall not be specified in the
10870 parameter-declaration-clause of a lambda-declarator. */
10871 if (cxx_dialect
< cxx14
)
10872 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10873 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10874 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10875 "default argument specified for lambda parameter");
10877 parens
.require_close (parser
);
10879 /* In the decl-specifier-seq of the lambda-declarator, each
10880 decl-specifier shall either be mutable or constexpr. */
10881 int declares_class_or_enum
;
10882 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
10883 && !cp_next_tokens_can_be_gnu_attribute_p (parser
))
10884 cp_parser_decl_specifier_seq (parser
,
10885 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10886 &lambda_specs
, &declares_class_or_enum
);
10887 if (lambda_specs
.storage_class
== sc_mutable
)
10889 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10890 if (lambda_specs
.conflicting_specifiers_p
)
10891 error_at (lambda_specs
.locations
[ds_storage_class
],
10892 "duplicate %<mutable%>");
10895 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10897 /* Parse optional exception specification. */
10898 exception_spec
= cp_parser_exception_specification_opt (parser
);
10900 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10902 /* Parse optional trailing return type. */
10903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10905 cp_lexer_consume_token (parser
->lexer
);
10906 return_type
= cp_parser_trailing_type_id (parser
);
10909 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
10910 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
10912 /* The function parameters must be in scope all the way until after the
10913 trailing-return-type in case of decltype. */
10914 pop_bindings_and_leave_scope ();
10916 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10917 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10919 /* Create the function call operator.
10921 Messing with declarators like this is no uglier than building up the
10922 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10925 cp_decl_specifier_seq return_type_specs
;
10926 cp_declarator
* declarator
;
10931 clear_decl_specs (&return_type_specs
);
10932 return_type_specs
.type
= make_auto ();
10934 if (lambda_specs
.locations
[ds_constexpr
])
10936 if (cxx_dialect
>= cxx17
)
10937 return_type_specs
.locations
[ds_constexpr
]
10938 = lambda_specs
.locations
[ds_constexpr
];
10940 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10941 "lambda only available with %<-std=c++17%> or "
10942 "%<-std=gnu++17%>");
10945 p
= obstack_alloc (&declarator_obstack
, 0);
10947 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10948 LAMBDA_EXPR_LOCATION (lambda_expr
));
10950 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10951 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10952 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10953 VIRT_SPEC_UNSPECIFIED
,
10958 /*requires_clause*/NULL_TREE
);
10959 declarator
->std_attributes
= std_attrs
;
10961 fco
= grokmethod (&return_type_specs
,
10964 if (fco
!= error_mark_node
)
10966 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10967 DECL_ARTIFICIAL (fco
) = 1;
10968 /* Give the object parameter a different name. */
10969 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10970 DECL_LAMBDA_FUNCTION (fco
) = 1;
10972 if (template_param_list
)
10974 fco
= finish_member_template_decl (fco
);
10975 finish_template_decl (template_param_list
);
10976 --parser
->num_template_parameter_lists
;
10978 else if (parser
->fully_implicit_function_template_p
)
10979 fco
= finish_fully_implicit_template (parser
, fco
);
10981 finish_member_declaration (fco
);
10983 obstack_free (&declarator_obstack
, p
);
10985 return (fco
!= error_mark_node
);
10989 /* Parse the body of a lambda expression, which is simply
10993 but which requires special handling.
10994 LAMBDA_EXPR is the current representation of the lambda expression. */
10997 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10999 bool nested
= (current_function_decl
!= NULL_TREE
);
11000 unsigned char local_variables_forbidden_p
11001 = parser
->local_variables_forbidden_p
;
11002 bool in_function_body
= parser
->in_function_body
;
11004 /* The body of a lambda-expression is not a subexpression of the enclosing
11009 push_function_context ();
11011 /* Still increment function_depth so that we don't GC in the
11012 middle of an expression. */
11015 vec
<tree
> omp_privatization_save
;
11016 save_omp_privatization_clauses (omp_privatization_save
);
11017 /* Clear this in case we're in the middle of a default argument. */
11018 parser
->local_variables_forbidden_p
= 0;
11019 parser
->in_function_body
= true;
11022 local_specialization_stack
s (lss_copy
);
11023 tree fco
= lambda_function (lambda_expr
);
11024 tree body
= start_lambda_function (fco
, lambda_expr
);
11025 matching_braces braces
;
11027 if (braces
.require_open (parser
))
11029 tree compound_stmt
= begin_compound_stmt (0);
11031 /* Originally C++11 required us to peek for 'return expr'; and
11032 process it specially here to deduce the return type. N3638
11033 removed the need for that. */
11035 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11036 cp_parser_label_declaration (parser
);
11037 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11038 braces
.require_close (parser
);
11040 finish_compound_stmt (compound_stmt
);
11043 finish_lambda_function (body
);
11046 restore_omp_privatization_clauses (omp_privatization_save
);
11047 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11048 parser
->in_function_body
= in_function_body
;
11050 pop_function_context();
11055 /* Statements [gram.stmt.stmt] */
11057 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11060 add_debug_begin_stmt (location_t loc
)
11062 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11064 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11065 /* A concept is never expanded normally. */
11068 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11069 SET_EXPR_LOCATION (stmt
, loc
);
11073 /* Parse a statement.
11077 expression-statement
11079 selection-statement
11080 iteration-statement
11082 declaration-statement
11089 attribute-specifier-seq (opt) expression-statement
11090 attribute-specifier-seq (opt) compound-statement
11091 attribute-specifier-seq (opt) selection-statement
11092 attribute-specifier-seq (opt) iteration-statement
11093 attribute-specifier-seq (opt) jump-statement
11094 declaration-statement
11095 attribute-specifier-seq (opt) try-block
11098 expression-statement
11106 IN_COMPOUND is true when the statement is nested inside a
11107 cp_parser_compound_statement; this matters for certain pragmas.
11109 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11110 is a (possibly labeled) if statement which is not enclosed in braces
11111 and has an else clause. This is used to implement -Wparentheses.
11113 CHAIN is a vector of if-else-if conditions. */
11116 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11117 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11118 location_t
*loc_after_labels
)
11120 tree statement
, std_attrs
= NULL_TREE
;
11122 location_t statement_location
, attrs_loc
;
11127 /* There is no statement yet. */
11128 statement
= NULL_TREE
;
11130 saved_token_sentinel
saved_tokens (parser
->lexer
);
11131 attrs_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11132 if (c_dialect_objc ())
11133 /* In obj-c++, seeing '[[' might be the either the beginning of
11134 c++11 attributes, or a nested objc-message-expression. So
11135 let's parse the c++11 attributes tentatively. */
11136 cp_parser_parse_tentatively (parser
);
11137 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11141 = cp_lexer_previous_token (parser
->lexer
)->location
;
11142 attrs_loc
= make_location (attrs_loc
, attrs_loc
, end_loc
);
11144 if (c_dialect_objc ())
11146 if (!cp_parser_parse_definitely (parser
))
11147 std_attrs
= NULL_TREE
;
11150 /* Peek at the next token. */
11151 token
= cp_lexer_peek_token (parser
->lexer
);
11152 /* Remember the location of the first token in the statement. */
11153 cp_token
*statement_token
= token
;
11154 statement_location
= token
->location
;
11155 add_debug_begin_stmt (statement_location
);
11156 /* If this is a keyword, then that will often determine what kind of
11157 statement we have. */
11158 if (token
->type
== CPP_KEYWORD
)
11160 enum rid keyword
= token
->keyword
;
11166 /* Looks like a labeled-statement with a case label.
11167 Parse the label, and then use tail recursion to parse
11169 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11170 in_compound
= false;
11175 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11176 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11182 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11183 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11190 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11191 statement
= cp_parser_jump_statement (parser
);
11194 /* Objective-C++ exception-handling constructs. */
11197 case RID_AT_FINALLY
:
11198 case RID_AT_SYNCHRONIZED
:
11200 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11201 statement
= cp_parser_objc_statement (parser
);
11205 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11206 statement
= cp_parser_try_block (parser
);
11209 case RID_NAMESPACE
:
11210 /* This must be a namespace alias definition. */
11211 if (std_attrs
!= NULL_TREE
)
11213 /* Attributes should be parsed as part of the the
11214 declaration, so let's un-parse them. */
11215 saved_tokens
.rollback();
11216 std_attrs
= NULL_TREE
;
11218 cp_parser_declaration_statement (parser
);
11221 case RID_TRANSACTION_ATOMIC
:
11222 case RID_TRANSACTION_RELAXED
:
11223 case RID_SYNCHRONIZED
:
11224 case RID_ATOMIC_NOEXCEPT
:
11225 case RID_ATOMIC_CANCEL
:
11226 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11227 statement
= cp_parser_transaction (parser
, token
);
11229 case RID_TRANSACTION_CANCEL
:
11230 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11231 statement
= cp_parser_transaction_cancel (parser
);
11235 /* It might be a keyword like `int' that can start a
11236 declaration-statement. */
11240 else if (token
->type
== CPP_NAME
)
11242 /* If the next token is a `:', then we are looking at a
11243 labeled-statement. */
11244 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11245 if (token
->type
== CPP_COLON
)
11247 /* Looks like a labeled-statement with an ordinary label.
11248 Parse the label, and then use tail recursion to parse
11251 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11252 in_compound
= false;
11256 /* Anything that starts with a `{' must be a compound-statement. */
11257 else if (token
->type
== CPP_OPEN_BRACE
)
11258 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11259 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11260 a statement all its own. */
11261 else if (token
->type
== CPP_PRAGMA
)
11263 /* Only certain OpenMP pragmas are attached to statements, and thus
11264 are considered statements themselves. All others are not. In
11265 the context of a compound, accept the pragma as a "statement" and
11266 return so that we can check for a close brace. Otherwise we
11267 require a real statement and must go back and read one. */
11269 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11270 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11274 else if (token
->type
== CPP_EOF
)
11276 cp_parser_error (parser
, "expected statement");
11280 /* Everything else must be a declaration-statement or an
11281 expression-statement. Try for the declaration-statement
11282 first, unless we are looking at a `;', in which case we know that
11283 we have an expression-statement. */
11286 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11288 if (std_attrs
!= NULL_TREE
)
11289 /* Attributes should be parsed as part of the declaration,
11290 so let's un-parse them. */
11291 saved_tokens
.rollback();
11293 cp_parser_parse_tentatively (parser
);
11294 /* Try to parse the declaration-statement. */
11295 cp_parser_declaration_statement (parser
);
11296 /* If that worked, we're done. */
11297 if (cp_parser_parse_definitely (parser
))
11299 /* It didn't work, restore the post-attribute position. */
11301 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11303 /* All preceding labels have been parsed at this point. */
11304 if (loc_after_labels
!= NULL
)
11305 *loc_after_labels
= statement_location
;
11307 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11309 /* Look for an expression-statement instead. */
11310 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11312 /* Handle [[fallthrough]];. */
11313 if (attribute_fallthrough_p (std_attrs
))
11315 /* The next token after the fallthrough attribute is ';'. */
11316 if (statement
== NULL_TREE
)
11318 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11319 statement
= build_call_expr_internal_loc (statement_location
,
11321 void_type_node
, 0);
11322 finish_expr_stmt (statement
);
11325 warning_at (statement_location
, OPT_Wattributes
,
11326 "%<fallthrough%> attribute not followed by %<;%>");
11327 std_attrs
= NULL_TREE
;
11331 /* Set the line number for the statement. */
11332 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11333 SET_EXPR_LOCATION (statement
, statement_location
);
11335 /* Allow "[[fallthrough]];", but warn otherwise. */
11336 if (std_attrs
!= NULL_TREE
)
11337 warning_at (attrs_loc
,
11339 "attributes at the beginning of statement are ignored");
11342 /* Append ATTR to attribute list ATTRS. */
11345 attr_chainon (tree attrs
, tree attr
)
11347 if (attrs
== error_mark_node
)
11348 return error_mark_node
;
11349 if (attr
== error_mark_node
)
11350 return error_mark_node
;
11351 return chainon (attrs
, attr
);
11354 /* Parse the label for a labeled-statement, i.e.
11357 case constant-expression :
11361 case constant-expression ... constant-expression : statement
11363 When a label is parsed without errors, the label is added to the
11364 parse tree by the finish_* functions, so this function doesn't
11365 have to return the label. */
11368 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11371 tree label
= NULL_TREE
;
11372 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11374 /* The next token should be an identifier. */
11375 token
= cp_lexer_peek_token (parser
->lexer
);
11376 if (token
->type
!= CPP_NAME
11377 && token
->type
!= CPP_KEYWORD
)
11379 cp_parser_error (parser
, "expected labeled-statement");
11383 /* Remember whether this case or a user-defined label is allowed to fall
11385 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11387 parser
->colon_corrects_to_scope_p
= false;
11388 switch (token
->keyword
)
11392 tree expr
, expr_hi
;
11393 cp_token
*ellipsis
;
11395 /* Consume the `case' token. */
11396 cp_lexer_consume_token (parser
->lexer
);
11397 /* Parse the constant-expression. */
11398 expr
= cp_parser_constant_expression (parser
);
11399 if (check_for_bare_parameter_packs (expr
))
11400 expr
= error_mark_node
;
11402 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11403 if (ellipsis
->type
== CPP_ELLIPSIS
)
11405 /* Consume the `...' token. */
11406 cp_lexer_consume_token (parser
->lexer
);
11407 expr_hi
= cp_parser_constant_expression (parser
);
11408 if (check_for_bare_parameter_packs (expr_hi
))
11409 expr_hi
= error_mark_node
;
11411 /* We don't need to emit warnings here, as the common code
11412 will do this for us. */
11415 expr_hi
= NULL_TREE
;
11417 if (parser
->in_switch_statement_p
)
11419 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11420 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11422 label
= CASE_LABEL (l
);
11423 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11427 error_at (token
->location
,
11428 "case label %qE not within a switch statement",
11434 /* Consume the `default' token. */
11435 cp_lexer_consume_token (parser
->lexer
);
11437 if (parser
->in_switch_statement_p
)
11439 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11440 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11442 label
= CASE_LABEL (l
);
11443 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11447 error_at (token
->location
, "case label not within a switch statement");
11451 /* Anything else must be an ordinary label. */
11452 label
= finish_label_stmt (cp_parser_identifier (parser
));
11453 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11454 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11458 /* Require the `:' token. */
11459 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11461 /* An ordinary label may optionally be followed by attributes.
11462 However, this is only permitted if the attributes are then
11463 followed by a semicolon. This is because, for backward
11464 compatibility, when parsing
11465 lab: __attribute__ ((unused)) int i;
11466 we want the attribute to attach to "i", not "lab". */
11467 if (label
!= NULL_TREE
11468 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11471 cp_parser_parse_tentatively (parser
);
11472 attrs
= cp_parser_gnu_attributes_opt (parser
);
11473 if (attrs
== NULL_TREE
11474 /* And fallthrough always binds to the expression-statement. */
11475 || attribute_fallthrough_p (attrs
)
11476 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11477 cp_parser_abort_tentative_parse (parser
);
11478 else if (!cp_parser_parse_definitely (parser
))
11481 attributes
= attr_chainon (attributes
, attrs
);
11484 if (attributes
!= NULL_TREE
)
11485 cplus_decl_attributes (&label
, attributes
, 0);
11487 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11490 /* Parse an expression-statement.
11492 expression-statement:
11495 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11496 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11497 indicates whether this expression-statement is part of an
11498 expression statement. */
11501 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11503 tree statement
= NULL_TREE
;
11504 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11505 location_t loc
= token
->location
;
11507 /* There might be attribute fallthrough. */
11508 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11510 /* If the next token is a ';', then there is no expression
11512 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11514 statement
= cp_parser_expression (parser
);
11515 if (statement
== error_mark_node
11516 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11518 cp_parser_skip_to_end_of_block_or_statement (parser
);
11519 return error_mark_node
;
11523 /* Handle [[fallthrough]];. */
11524 if (attribute_fallthrough_p (attr
))
11526 /* The next token after the fallthrough attribute is ';'. */
11527 if (statement
== NULL_TREE
)
11528 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11529 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11530 void_type_node
, 0);
11532 warning_at (loc
, OPT_Wattributes
,
11533 "%<fallthrough%> attribute not followed by %<;%>");
11537 /* Allow "[[fallthrough]];", but warn otherwise. */
11538 if (attr
!= NULL_TREE
)
11539 warning_at (loc
, OPT_Wattributes
,
11540 "attributes at the beginning of statement are ignored");
11542 /* Give a helpful message for "A<T>::type t;" and the like. */
11543 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11544 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11546 if (TREE_CODE (statement
) == SCOPE_REF
)
11547 error_at (token
->location
, "need %<typename%> before %qE because "
11548 "%qT is a dependent scope",
11549 statement
, TREE_OPERAND (statement
, 0));
11550 else if (is_overloaded_fn (statement
)
11551 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11554 tree fn
= get_first_fn (statement
);
11555 error_at (token
->location
,
11556 "%<%T::%D%> names the constructor, not the type",
11557 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11561 /* Consume the final `;'. */
11562 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11564 if (in_statement_expr
11565 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11566 /* This is the final expression statement of a statement
11568 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11569 else if (statement
)
11570 statement
= finish_expr_stmt (statement
);
11575 /* Parse a compound-statement.
11577 compound-statement:
11578 { statement-seq [opt] }
11582 compound-statement:
11583 { label-declaration-seq [opt] statement-seq [opt] }
11585 label-declaration-seq:
11587 label-declaration-seq label-declaration
11589 Returns a tree representing the statement. */
11592 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11593 int bcs_flags
, bool function_body
)
11595 tree compound_stmt
;
11596 matching_braces braces
;
11598 /* Consume the `{'. */
11599 if (!braces
.require_open (parser
))
11600 return error_mark_node
;
11601 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11602 && !function_body
&& cxx_dialect
< cxx14
)
11603 pedwarn (input_location
, OPT_Wpedantic
,
11604 "compound-statement in %<constexpr%> function");
11605 /* Begin the compound-statement. */
11606 compound_stmt
= begin_compound_stmt (bcs_flags
);
11607 /* If the next keyword is `__label__' we have a label declaration. */
11608 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11609 cp_parser_label_declaration (parser
);
11610 /* Parse an (optional) statement-seq. */
11611 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11612 /* Finish the compound-statement. */
11613 finish_compound_stmt (compound_stmt
);
11614 /* Consume the `}'. */
11615 braces
.require_close (parser
);
11617 return compound_stmt
;
11620 /* Parse an (optional) statement-seq.
11624 statement-seq [opt] statement */
11627 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11629 /* Scan statements until there aren't any more. */
11632 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11634 /* If we are looking at a `}', then we have run out of
11635 statements; the same is true if we have reached the end
11636 of file, or have stumbled upon a stray '@end'. */
11637 if (token
->type
== CPP_CLOSE_BRACE
11638 || token
->type
== CPP_EOF
11639 || token
->type
== CPP_PRAGMA_EOL
11640 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11643 /* If we are in a compound statement and find 'else' then
11644 something went wrong. */
11645 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11647 if (parser
->in_statement
& IN_IF_STMT
)
11651 token
= cp_lexer_consume_token (parser
->lexer
);
11652 error_at (token
->location
, "%<else%> without a previous %<if%>");
11656 /* Parse the statement. */
11657 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11661 /* Return true if this is the C++20 version of range-based-for with
11665 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11669 /* Save tokens so that we can put them back. */
11670 cp_lexer_save_tokens (parser
->lexer
);
11672 /* There has to be an unnested ; followed by an unnested :. */
11673 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11674 /*recovering=*/false,
11676 /*consume_paren=*/false) != -1)
11679 /* We found the semicolon, eat it now. */
11680 cp_lexer_consume_token (parser
->lexer
);
11682 /* Now look for ':' that is not nested in () or {}. */
11683 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11684 /*recovering=*/false,
11686 /*consume_paren=*/false) == -1);
11689 /* Roll back the tokens we skipped. */
11690 cp_lexer_rollback_tokens (parser
->lexer
);
11695 /* Return true if we're looking at (init; cond), false otherwise. */
11698 cp_parser_init_statement_p (cp_parser
*parser
)
11700 /* Save tokens so that we can put them back. */
11701 cp_lexer_save_tokens (parser
->lexer
);
11703 /* Look for ';' that is not nested in () or {}. */
11704 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11705 /*recovering=*/false,
11707 /*consume_paren=*/false);
11709 /* Roll back the tokens we skipped. */
11710 cp_lexer_rollback_tokens (parser
->lexer
);
11715 /* Parse a selection-statement.
11717 selection-statement:
11718 if ( init-statement [opt] condition ) statement
11719 if ( init-statement [opt] condition ) statement else statement
11720 switch ( init-statement [opt] condition ) statement
11722 Returns the new IF_STMT or SWITCH_STMT.
11724 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11725 is a (possibly labeled) if statement which is not enclosed in
11726 braces and has an else clause. This is used to implement
11729 CHAIN is a vector of if-else-if conditions. This is used to implement
11730 -Wduplicated-cond. */
11733 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11738 token_indent_info guard_tinfo
;
11743 /* Peek at the next token. */
11744 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11745 guard_tinfo
= get_token_indent_info (token
);
11747 /* See what kind of keyword it is. */
11748 keyword
= token
->keyword
;
11758 if (keyword
== RID_IF
11759 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11763 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11764 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11765 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11766 "with %<-std=c++17%> or %<-std=gnu++17%>");
11769 /* Look for the `('. */
11770 matching_parens parens
;
11771 if (!parens
.require_open (parser
))
11773 cp_parser_skip_to_end_of_statement (parser
);
11774 return error_mark_node
;
11777 /* Begin the selection-statement. */
11778 if (keyword
== RID_IF
)
11780 statement
= begin_if_stmt ();
11781 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11784 statement
= begin_switch_stmt ();
11786 /* Parse the optional init-statement. */
11787 if (cp_parser_init_statement_p (parser
))
11790 if (cxx_dialect
< cxx17
)
11791 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11792 "init-statement in selection statements only available "
11793 "with %<-std=c++17%> or %<-std=gnu++17%>");
11794 cp_parser_init_statement (parser
, &decl
);
11797 /* Parse the condition. */
11798 condition
= cp_parser_condition (parser
);
11799 /* Look for the `)'. */
11800 if (!parens
.require_close (parser
))
11801 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11802 /*consume_paren=*/true);
11804 if (keyword
== RID_IF
)
11807 unsigned char in_statement
;
11809 /* Add the condition. */
11810 condition
= finish_if_stmt_cond (condition
, statement
);
11812 if (warn_duplicated_cond
)
11813 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11816 /* Parse the then-clause. */
11817 in_statement
= parser
->in_statement
;
11818 parser
->in_statement
|= IN_IF_STMT
;
11820 /* Outside a template, the non-selected branch of a constexpr
11821 if is a 'discarded statement', i.e. unevaluated. */
11822 bool was_discarded
= in_discarded_stmt
;
11823 bool discard_then
= (cx
&& !processing_template_decl
11824 && integer_zerop (condition
));
11827 in_discarded_stmt
= true;
11828 ++c_inhibit_evaluation_warnings
;
11831 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11834 parser
->in_statement
= in_statement
;
11836 finish_then_clause (statement
);
11840 THEN_CLAUSE (statement
) = NULL_TREE
;
11841 in_discarded_stmt
= was_discarded
;
11842 --c_inhibit_evaluation_warnings
;
11845 /* If the next token is `else', parse the else-clause. */
11846 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11849 bool discard_else
= (cx
&& !processing_template_decl
11850 && integer_nonzerop (condition
));
11853 in_discarded_stmt
= true;
11854 ++c_inhibit_evaluation_warnings
;
11858 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11859 /* Consume the `else' keyword. */
11860 cp_lexer_consume_token (parser
->lexer
);
11861 if (warn_duplicated_cond
)
11863 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11867 /* We've got "if (COND) else if (COND2)". Start
11868 the condition chain and add COND as the first
11870 chain
= new vec
<tree
> ();
11871 if (!CONSTANT_CLASS_P (condition
)
11872 && !TREE_SIDE_EFFECTS (condition
))
11874 /* Wrap it in a NOP_EXPR so that we can set the
11875 location of the condition. */
11876 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11878 SET_EXPR_LOCATION (e
, token
->location
);
11879 chain
->safe_push (e
);
11882 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11885 /* This is if-else without subsequent if. Zap the
11886 condition chain; we would have already warned at
11892 begin_else_clause (statement
);
11893 /* Parse the else-clause. */
11894 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11895 guard_tinfo
, chain
);
11897 finish_else_clause (statement
);
11899 /* If we are currently parsing a then-clause, then
11900 IF_P will not be NULL. We set it to true to
11901 indicate that this if statement has an else clause.
11902 This may trigger the Wparentheses warning below
11903 when we get back up to the parent if statement. */
11909 ELSE_CLAUSE (statement
) = NULL_TREE
;
11910 in_discarded_stmt
= was_discarded
;
11911 --c_inhibit_evaluation_warnings
;
11916 /* This if statement does not have an else clause. If
11917 NESTED_IF is true, then the then-clause has an if
11918 statement which does have an else clause. We warn
11919 about the potential ambiguity. */
11921 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11922 "suggest explicit braces to avoid ambiguous"
11924 if (warn_duplicated_cond
)
11926 /* We don't need the condition chain anymore. */
11932 /* Now we're all done with the if-statement. */
11933 finish_if_stmt (statement
);
11937 bool in_switch_statement_p
;
11938 unsigned char in_statement
;
11940 /* Add the condition. */
11941 finish_switch_cond (condition
, statement
);
11943 /* Parse the body of the switch-statement. */
11944 in_switch_statement_p
= parser
->in_switch_statement_p
;
11945 in_statement
= parser
->in_statement
;
11946 parser
->in_switch_statement_p
= true;
11947 parser
->in_statement
|= IN_SWITCH_STMT
;
11948 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11950 parser
->in_switch_statement_p
= in_switch_statement_p
;
11951 parser
->in_statement
= in_statement
;
11953 /* Now we're all done with the switch-statement. */
11954 finish_switch_stmt (statement
);
11962 cp_parser_error (parser
, "expected selection-statement");
11963 return error_mark_node
;
11967 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11968 If we have seen at least one decl-specifier, and the next token
11969 is not a parenthesis, then we must be looking at a declaration.
11970 (After "int (" we might be looking at a functional cast.) */
11973 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11974 bool any_specifiers_p
)
11976 if (any_specifiers_p
11977 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11978 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11979 && !cp_parser_error_occurred (parser
))
11980 cp_parser_commit_to_tentative_parse (parser
);
11983 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11984 The declarator shall not specify a function or an array. Returns
11985 TRUE if the declarator is valid, FALSE otherwise. */
11988 cp_parser_check_condition_declarator (cp_parser
* parser
,
11989 cp_declarator
*declarator
,
11992 if (declarator
== cp_error_declarator
11993 || function_declarator_p (declarator
)
11994 || declarator
->kind
== cdk_array
)
11996 if (declarator
== cp_error_declarator
)
11997 /* Already complained. */;
11998 else if (declarator
->kind
== cdk_array
)
11999 error_at (loc
, "condition declares an array");
12001 error_at (loc
, "condition declares a function");
12002 if (parser
->fully_implicit_function_template_p
)
12003 abort_fully_implicit_template (parser
);
12004 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
12005 /*or_comma=*/false,
12006 /*consume_paren=*/false);
12013 /* Parse a condition.
12017 type-specifier-seq declarator = initializer-clause
12018 type-specifier-seq declarator braced-init-list
12023 type-specifier-seq declarator asm-specification [opt]
12024 attributes [opt] = assignment-expression
12026 Returns the expression that should be tested. */
12029 cp_parser_condition (cp_parser
* parser
)
12031 cp_decl_specifier_seq type_specifiers
;
12032 const char *saved_message
;
12033 int declares_class_or_enum
;
12035 /* Try the declaration first. */
12036 cp_parser_parse_tentatively (parser
);
12037 /* New types are not allowed in the type-specifier-seq for a
12039 saved_message
= parser
->type_definition_forbidden_message
;
12040 parser
->type_definition_forbidden_message
12041 = G_("types may not be defined in conditions");
12042 /* Parse the type-specifier-seq. */
12043 cp_parser_decl_specifier_seq (parser
,
12044 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
12046 &declares_class_or_enum
);
12047 /* Restore the saved message. */
12048 parser
->type_definition_forbidden_message
= saved_message
;
12050 cp_parser_maybe_commit_to_declaration (parser
,
12051 type_specifiers
.any_specifiers_p
);
12053 /* If all is well, we might be looking at a declaration. */
12054 if (!cp_parser_error_occurred (parser
))
12057 tree asm_specification
;
12059 cp_declarator
*declarator
;
12060 tree initializer
= NULL_TREE
;
12061 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12063 /* Parse the declarator. */
12064 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12065 CP_PARSER_FLAGS_NONE
,
12066 /*ctor_dtor_or_conv_p=*/NULL
,
12067 /*parenthesized_p=*/NULL
,
12068 /*member_p=*/false,
12069 /*friend_p=*/false,
12070 /*static_p=*/false);
12071 /* Parse the attributes. */
12072 attributes
= cp_parser_attributes_opt (parser
);
12073 /* Parse the asm-specification. */
12074 asm_specification
= cp_parser_asm_specification_opt (parser
);
12075 /* If the next token is not an `=' or '{', then we might still be
12076 looking at an expression. For example:
12080 looks like a decl-specifier-seq and a declarator -- but then
12081 there is no `=', so this is an expression. */
12082 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12083 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12084 cp_parser_simulate_error (parser
);
12086 /* If we did see an `=' or '{', then we are looking at a declaration
12088 if (cp_parser_parse_definitely (parser
))
12091 bool non_constant_p
= false;
12092 int flags
= LOOKUP_ONLYCONVERTING
;
12094 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12095 return error_mark_node
;
12097 /* Create the declaration. */
12098 decl
= start_decl (declarator
, &type_specifiers
,
12099 /*initialized_p=*/true,
12100 attributes
, /*prefix_attributes=*/NULL_TREE
,
12103 /* Parse the initializer. */
12104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12106 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12107 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12110 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12112 /* Consume the `='. */
12113 cp_lexer_consume_token (parser
->lexer
);
12114 initializer
= cp_parser_initializer_clause (parser
,
12119 cp_parser_error (parser
, "expected initializer");
12120 initializer
= error_mark_node
;
12122 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12123 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12125 /* Process the initializer. */
12126 cp_finish_decl (decl
,
12127 initializer
, !non_constant_p
,
12132 pop_scope (pushed_scope
);
12134 return convert_from_reference (decl
);
12137 /* If we didn't even get past the declarator successfully, we are
12138 definitely not looking at a declaration. */
12140 cp_parser_abort_tentative_parse (parser
);
12142 /* Otherwise, we are looking at an expression. */
12143 return cp_parser_expression (parser
);
12146 /* Parses a for-statement or range-for-statement until the closing ')',
12150 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12152 tree init
, scope
, decl
;
12155 /* Begin the for-statement. */
12156 scope
= begin_for_scope (&init
);
12158 /* Parse the initialization. */
12159 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12162 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12165 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12169 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12170 unsigned short unroll
)
12172 /* Normal for loop */
12173 tree condition
= NULL_TREE
;
12174 tree expression
= NULL_TREE
;
12177 stmt
= begin_for_stmt (scope
, init
);
12178 /* The init-statement has already been parsed in
12179 cp_parser_init_statement, so no work is needed here. */
12180 finish_init_stmt (stmt
);
12182 /* If there's a condition, process it. */
12183 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12184 condition
= cp_parser_condition (parser
);
12187 cp_parser_error (parser
, "missing loop condition in loop with "
12188 "%<GCC ivdep%> pragma");
12189 condition
= error_mark_node
;
12193 cp_parser_error (parser
, "missing loop condition in loop with "
12194 "%<GCC unroll%> pragma");
12195 condition
= error_mark_node
;
12197 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12198 /* Look for the `;'. */
12199 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12201 /* If there's an expression, process it. */
12202 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12203 expression
= cp_parser_expression (parser
);
12204 finish_for_expr (expression
, stmt
);
12209 /* Tries to parse a range-based for-statement:
12212 decl-specifier-seq declarator : expression
12214 The decl-specifier-seq declarator and the `:' are already parsed by
12215 cp_parser_init_statement. If processing_template_decl it returns a
12216 newly created RANGE_FOR_STMT; if not, it is converted to a
12217 regular FOR_STMT. */
12220 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12221 bool ivdep
, unsigned short unroll
, bool is_omp
)
12223 tree stmt
, range_expr
;
12224 auto_vec
<cxx_binding
*, 16> bindings
;
12225 auto_vec
<tree
, 16> names
;
12226 tree decomp_first_name
= NULL_TREE
;
12227 unsigned int decomp_cnt
= 0;
12229 /* Get the range declaration momentarily out of the way so that
12230 the range expression doesn't clash with it. */
12231 if (range_decl
!= error_mark_node
)
12233 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12235 tree v
= DECL_VALUE_EXPR (range_decl
);
12236 /* For decomposition declaration get all of the corresponding
12237 declarations out of the way. */
12238 if (TREE_CODE (v
) == ARRAY_REF
12239 && VAR_P (TREE_OPERAND (v
, 0))
12240 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12242 tree d
= range_decl
;
12243 range_decl
= TREE_OPERAND (v
, 0);
12244 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12245 decomp_first_name
= d
;
12246 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12248 tree name
= DECL_NAME (d
);
12249 names
.safe_push (name
);
12250 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12251 IDENTIFIER_BINDING (name
)
12252 = IDENTIFIER_BINDING (name
)->previous
;
12256 if (names
.is_empty ())
12258 tree name
= DECL_NAME (range_decl
);
12259 names
.safe_push (name
);
12260 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12261 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12265 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12267 bool expr_non_constant_p
;
12268 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12271 range_expr
= cp_parser_expression (parser
);
12273 /* Put the range declaration(s) back into scope. */
12274 for (unsigned int i
= 0; i
< names
.length (); i
++)
12276 cxx_binding
*binding
= bindings
[i
];
12277 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12278 IDENTIFIER_BINDING (names
[i
]) = binding
;
12281 /* finish_omp_for has its own code for the following, so just
12282 return the range_expr instead. */
12286 /* If in template, STMT is converted to a normal for-statement
12287 at instantiation. If not, it is done just ahead. */
12288 if (processing_template_decl
)
12290 if (check_for_bare_parameter_packs (range_expr
))
12291 range_expr
= error_mark_node
;
12292 stmt
= begin_range_for_stmt (scope
, init
);
12294 RANGE_FOR_IVDEP (stmt
) = 1;
12296 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12297 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12298 if (!type_dependent_expression_p (range_expr
)
12299 /* do_auto_deduction doesn't mess with template init-lists. */
12300 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12301 do_range_for_auto_deduction (range_decl
, range_expr
);
12305 stmt
= begin_for_stmt (scope
, init
);
12306 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12307 decomp_first_name
, decomp_cnt
, ivdep
,
12313 /* Subroutine of cp_convert_range_for: given the initializer expression,
12314 builds up the range temporary. */
12317 build_range_temp (tree range_expr
)
12319 tree range_type
, range_temp
;
12321 /* Find out the type deduced by the declaration
12322 `auto &&__range = range_expr'. */
12323 range_type
= cp_build_reference_type (make_auto (), true);
12324 range_type
= do_auto_deduction (range_type
, range_expr
,
12325 type_uses_auto (range_type
));
12327 /* Create the __range variable. */
12328 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12330 TREE_USED (range_temp
) = 1;
12331 DECL_ARTIFICIAL (range_temp
) = 1;
12336 /* Used by cp_parser_range_for in template context: we aren't going to
12337 do a full conversion yet, but we still need to resolve auto in the
12338 type of the for-range-declaration if present. This is basically
12339 a shortcut version of cp_convert_range_for. */
12342 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12344 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12347 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12348 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12349 iter_type
= (cp_parser_perform_range_for_lookup
12350 (range_temp
, &begin_dummy
, &end_dummy
));
12353 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12355 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12357 tf_warning_or_error
);
12358 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12359 iter_decl
, auto_node
);
12364 /* Converts a range-based for-statement into a normal
12365 for-statement, as per the definition.
12367 for (RANGE_DECL : RANGE_EXPR)
12370 should be equivalent to:
12373 auto &&__range = RANGE_EXPR;
12374 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12378 RANGE_DECL = *__begin;
12383 If RANGE_EXPR is an array:
12384 BEGIN_EXPR = __range
12385 END_EXPR = __range + ARRAY_SIZE(__range)
12386 Else if RANGE_EXPR has a member 'begin' or 'end':
12387 BEGIN_EXPR = __range.begin()
12388 END_EXPR = __range.end()
12390 BEGIN_EXPR = begin(__range)
12391 END_EXPR = end(__range);
12393 If __range has a member 'begin' but not 'end', or vice versa, we must
12394 still use the second alternative (it will surely fail, however).
12395 When calling begin()/end() in the third alternative we must use
12396 argument dependent lookup, but always considering 'std' as an associated
12400 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12401 tree decomp_first_name
, unsigned int decomp_cnt
,
12402 bool ivdep
, unsigned short unroll
)
12405 tree iter_type
, begin_expr
, end_expr
;
12406 tree condition
, expression
;
12408 range_expr
= mark_lvalue_use (range_expr
);
12410 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12411 /* If an error happened previously do nothing or else a lot of
12412 unhelpful errors would be issued. */
12413 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12418 if (VAR_P (range_expr
)
12419 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12420 /* Can't bind a reference to an array of runtime bound. */
12421 range_temp
= range_expr
;
12424 range_temp
= build_range_temp (range_expr
);
12425 pushdecl (range_temp
);
12426 cp_finish_decl (range_temp
, range_expr
,
12427 /*is_constant_init*/false, NULL_TREE
,
12428 LOOKUP_ONLYCONVERTING
);
12429 range_temp
= convert_from_reference (range_temp
);
12431 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12432 &begin_expr
, &end_expr
);
12435 /* The new for initialization statement. */
12436 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12438 TREE_USED (begin
) = 1;
12439 DECL_ARTIFICIAL (begin
) = 1;
12441 cp_finish_decl (begin
, begin_expr
,
12442 /*is_constant_init*/false, NULL_TREE
,
12443 LOOKUP_ONLYCONVERTING
);
12445 if (cxx_dialect
>= cxx17
)
12446 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12447 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12448 TREE_USED (end
) = 1;
12449 DECL_ARTIFICIAL (end
) = 1;
12451 cp_finish_decl (end
, end_expr
,
12452 /*is_constant_init*/false, NULL_TREE
,
12453 LOOKUP_ONLYCONVERTING
);
12455 finish_init_stmt (statement
);
12457 /* The new for condition. */
12458 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12461 NULL
, tf_warning_or_error
);
12462 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12464 /* The new increment expression. */
12465 expression
= finish_unary_op_expr (input_location
,
12466 PREINCREMENT_EXPR
, begin
,
12467 tf_warning_or_error
);
12468 finish_for_expr (expression
, statement
);
12470 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12471 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12473 /* The declaration is initialized with *__begin inside the loop body. */
12474 cp_finish_decl (range_decl
,
12475 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12476 tf_warning_or_error
),
12477 /*is_constant_init*/false, NULL_TREE
,
12478 LOOKUP_ONLYCONVERTING
);
12479 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12480 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12485 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12486 We need to solve both at the same time because the method used
12487 depends on the existence of members begin or end.
12488 Returns the type deduced for the iterator expression. */
12491 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12493 if (error_operand_p (range
))
12495 *begin
= *end
= error_mark_node
;
12496 return error_mark_node
;
12499 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12501 error ("range-based %<for%> expression of type %qT "
12502 "has incomplete type", TREE_TYPE (range
));
12503 *begin
= *end
= error_mark_node
;
12504 return error_mark_node
;
12506 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12508 /* If RANGE is an array, we will use pointer arithmetic. */
12509 *begin
= decay_conversion (range
, tf_warning_or_error
);
12510 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12512 array_type_nelts_top (TREE_TYPE (range
)),
12514 return TREE_TYPE (*begin
);
12518 /* If it is not an array, we must do a bit of magic. */
12519 tree id_begin
, id_end
;
12520 tree member_begin
, member_end
;
12522 *begin
= *end
= error_mark_node
;
12524 id_begin
= get_identifier ("begin");
12525 id_end
= get_identifier ("end");
12526 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12527 /*protect=*/2, /*want_type=*/false,
12528 tf_warning_or_error
);
12529 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12530 /*protect=*/2, /*want_type=*/false,
12531 tf_warning_or_error
);
12533 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12535 /* Use the member functions. */
12536 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12537 *end
= cp_parser_range_for_member_function (range
, id_end
);
12541 /* Use global functions with ADL. */
12542 vec
<tree
, va_gc
> *vec
;
12543 vec
= make_tree_vector ();
12545 vec_safe_push (vec
, range
);
12547 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12548 tf_warning_or_error
);
12549 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12550 tf_warning_or_error
);
12551 member_end
= perform_koenig_lookup (id_end
, vec
,
12552 tf_warning_or_error
);
12553 *end
= finish_call_expr (member_end
, &vec
, false, true,
12554 tf_warning_or_error
);
12556 release_tree_vector (vec
);
12559 /* Last common checks. */
12560 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12562 /* If one of the expressions is an error do no more checks. */
12563 *begin
= *end
= error_mark_node
;
12564 return error_mark_node
;
12566 else if (type_dependent_expression_p (*begin
)
12567 || type_dependent_expression_p (*end
))
12568 /* Can happen, when, eg, in a template context, Koenig lookup
12569 can't resolve begin/end (c++/58503). */
12573 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12574 /* The unqualified type of the __begin and __end temporaries should
12575 be the same, as required by the multiple auto declaration. */
12576 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12578 if (cxx_dialect
>= cxx17
12579 && (build_x_binary_op (input_location
, NE_EXPR
,
12580 *begin
, ERROR_MARK
,
12583 != error_mark_node
))
12584 /* P0184R0 allows __begin and __end to have different types,
12585 but make sure they are comparable so we can give a better
12588 error ("inconsistent begin/end types in range-based %<for%> "
12589 "statement: %qT and %qT",
12590 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12597 /* Helper function for cp_parser_perform_range_for_lookup.
12598 Builds a tree for RANGE.IDENTIFIER(). */
12601 cp_parser_range_for_member_function (tree range
, tree identifier
)
12604 vec
<tree
, va_gc
> *vec
;
12606 member
= finish_class_member_access_expr (range
, identifier
,
12607 false, tf_warning_or_error
);
12608 if (member
== error_mark_node
)
12609 return error_mark_node
;
12611 vec
= make_tree_vector ();
12612 res
= finish_call_expr (member
, &vec
,
12613 /*disallow_virtual=*/false,
12614 /*koenig_p=*/false,
12615 tf_warning_or_error
);
12616 release_tree_vector (vec
);
12620 /* Parse an iteration-statement.
12622 iteration-statement:
12623 while ( condition ) statement
12624 do statement while ( expression ) ;
12625 for ( init-statement condition [opt] ; expression [opt] )
12628 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12631 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12632 unsigned short unroll
)
12637 unsigned char in_statement
;
12638 token_indent_info guard_tinfo
;
12640 /* Peek at the next token. */
12641 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12643 return error_mark_node
;
12645 guard_tinfo
= get_token_indent_info (token
);
12647 /* Remember whether or not we are already within an iteration
12649 in_statement
= parser
->in_statement
;
12651 /* See what kind of keyword it is. */
12652 keyword
= token
->keyword
;
12659 /* Begin the while-statement. */
12660 statement
= begin_while_stmt ();
12661 /* Look for the `('. */
12662 matching_parens parens
;
12663 parens
.require_open (parser
);
12664 /* Parse the condition. */
12665 condition
= cp_parser_condition (parser
);
12666 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12667 /* Look for the `)'. */
12668 parens
.require_close (parser
);
12669 /* Parse the dependent statement. */
12670 parser
->in_statement
= IN_ITERATION_STMT
;
12671 bool prev
= note_iteration_stmt_body_start ();
12672 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12673 note_iteration_stmt_body_end (prev
);
12674 parser
->in_statement
= in_statement
;
12675 /* We're done with the while-statement. */
12676 finish_while_stmt (statement
);
12684 /* Begin the do-statement. */
12685 statement
= begin_do_stmt ();
12686 /* Parse the body of the do-statement. */
12687 parser
->in_statement
= IN_ITERATION_STMT
;
12688 bool prev
= note_iteration_stmt_body_start ();
12689 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12690 note_iteration_stmt_body_end (prev
);
12691 parser
->in_statement
= in_statement
;
12692 finish_do_body (statement
);
12693 /* Look for the `while' keyword. */
12694 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12695 /* Look for the `('. */
12696 matching_parens parens
;
12697 parens
.require_open (parser
);
12698 /* Parse the expression. */
12699 expression
= cp_parser_expression (parser
);
12700 /* We're done with the do-statement. */
12701 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12702 /* Look for the `)'. */
12703 parens
.require_close (parser
);
12704 /* Look for the `;'. */
12705 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12711 /* Look for the `('. */
12712 matching_parens parens
;
12713 parens
.require_open (parser
);
12715 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12717 /* Look for the `)'. */
12718 parens
.require_close (parser
);
12720 /* Parse the body of the for-statement. */
12721 parser
->in_statement
= IN_ITERATION_STMT
;
12722 bool prev
= note_iteration_stmt_body_start ();
12723 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12724 note_iteration_stmt_body_end (prev
);
12725 parser
->in_statement
= in_statement
;
12727 /* We're done with the for-statement. */
12728 finish_for_stmt (statement
);
12733 cp_parser_error (parser
, "expected iteration-statement");
12734 statement
= error_mark_node
;
12741 /* Parse a init-statement or the declarator of a range-based-for.
12742 Returns true if a range-based-for declaration is seen.
12745 expression-statement
12746 simple-declaration */
12749 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12751 /* If the next token is a `;', then we have an empty
12752 expression-statement. Grammatically, this is also a
12753 simple-declaration, but an invalid one, because it does not
12754 declare anything. Therefore, if we did not handle this case
12755 specially, we would issue an error message about an invalid
12757 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12759 bool is_range_for
= false;
12760 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12762 /* Try to parse the init-statement. */
12763 if (cp_parser_range_based_for_with_init_p (parser
))
12766 cp_parser_parse_tentatively (parser
);
12767 /* Parse the declaration. */
12768 cp_parser_simple_declaration (parser
,
12769 /*function_definition_allowed_p=*/false,
12771 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12772 if (!cp_parser_parse_definitely (parser
))
12773 /* That didn't work, try to parse it as an expression-statement. */
12774 cp_parser_expression_statement (parser
, NULL_TREE
);
12776 if (cxx_dialect
< cxx2a
)
12778 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12779 "range-based %<for%> loops with initializer only "
12780 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12781 *decl
= error_mark_node
;
12785 /* A colon is used in range-based for. */
12786 parser
->colon_corrects_to_scope_p
= false;
12788 /* We're going to speculatively look for a declaration, falling back
12789 to an expression, if necessary. */
12790 cp_parser_parse_tentatively (parser
);
12791 /* Parse the declaration. */
12792 cp_parser_simple_declaration (parser
,
12793 /*function_definition_allowed_p=*/false,
12795 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12798 /* It is a range-for, consume the ':'. */
12799 cp_lexer_consume_token (parser
->lexer
);
12800 is_range_for
= true;
12801 if (cxx_dialect
< cxx11
)
12802 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12803 "range-based %<for%> loops only available with "
12804 "%<-std=c++11%> or %<-std=gnu++11%>");
12807 /* The ';' is not consumed yet because we told
12808 cp_parser_simple_declaration not to. */
12809 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12811 if (cp_parser_parse_definitely (parser
))
12812 return is_range_for
;
12813 /* If the tentative parse failed, then we shall need to look for an
12814 expression-statement. */
12816 /* If we are here, it is an expression-statement. */
12817 cp_parser_expression_statement (parser
, NULL_TREE
);
12821 /* Parse a jump-statement.
12826 return expression [opt] ;
12827 return braced-init-list ;
12833 goto * expression ;
12835 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12838 cp_parser_jump_statement (cp_parser
* parser
)
12840 tree statement
= error_mark_node
;
12843 unsigned char in_statement
;
12845 /* Peek at the next token. */
12846 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12848 return error_mark_node
;
12850 /* See what kind of keyword it is. */
12851 keyword
= token
->keyword
;
12855 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12856 switch (in_statement
)
12859 error_at (token
->location
, "break statement not within loop or switch");
12862 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12863 || in_statement
== IN_ITERATION_STMT
);
12864 statement
= finish_break_stmt ();
12865 if (in_statement
== IN_ITERATION_STMT
)
12866 break_maybe_infinite_loop ();
12869 error_at (token
->location
, "invalid exit from OpenMP structured block");
12872 error_at (token
->location
, "break statement used with OpenMP for loop");
12875 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12879 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12882 error_at (token
->location
, "continue statement not within a loop");
12884 /* Fall through. */
12885 case IN_ITERATION_STMT
:
12887 statement
= finish_continue_stmt ();
12890 error_at (token
->location
, "invalid exit from OpenMP structured block");
12893 gcc_unreachable ();
12895 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12901 bool expr_non_constant_p
;
12903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12905 cp_lexer_set_source_position (parser
->lexer
);
12906 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12907 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12909 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12910 expr
= cp_parser_expression (parser
);
12912 /* If the next token is a `;', then there is no
12915 /* Build the return-statement. */
12916 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12917 /* Don't deduce from a discarded return statement. */;
12919 statement
= finish_return_stmt (expr
);
12920 /* Look for the final `;'. */
12921 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12926 if (parser
->in_function_body
12927 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12929 error ("%<goto%> in %<constexpr%> function");
12930 cp_function_chain
->invalid_constexpr
= true;
12933 /* Create the goto-statement. */
12934 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12936 /* Issue a warning about this use of a GNU extension. */
12937 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12938 /* Consume the '*' token. */
12939 cp_lexer_consume_token (parser
->lexer
);
12940 /* Parse the dependent expression. */
12941 finish_goto_stmt (cp_parser_expression (parser
));
12944 finish_goto_stmt (cp_parser_identifier (parser
));
12945 /* Look for the final `;'. */
12946 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12950 cp_parser_error (parser
, "expected jump-statement");
12957 /* Parse a declaration-statement.
12959 declaration-statement:
12960 block-declaration */
12963 cp_parser_declaration_statement (cp_parser
* parser
)
12967 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12968 p
= obstack_alloc (&declarator_obstack
, 0);
12970 /* Parse the block-declaration. */
12971 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12973 /* Free any declarators allocated. */
12974 obstack_free (&declarator_obstack
, p
);
12977 /* Some dependent statements (like `if (cond) statement'), are
12978 implicitly in their own scope. In other words, if the statement is
12979 a single statement (as opposed to a compound-statement), it is
12980 none-the-less treated as if it were enclosed in braces. Any
12981 declarations appearing in the dependent statement are out of scope
12982 after control passes that point. This function parses a statement,
12983 but ensures that is in its own scope, even if it is not a
12984 compound-statement.
12986 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12987 is a (possibly labeled) if statement which is not enclosed in
12988 braces and has an else clause. This is used to implement
12991 CHAIN is a vector of if-else-if conditions. This is used to implement
12994 Returns the new statement. */
12997 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12998 const token_indent_info
&guard_tinfo
,
13002 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13003 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
13004 token_indent_info body_tinfo
13005 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13010 /* Mark if () ; with a special NOP_EXPR. */
13011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13013 cp_lexer_consume_token (parser
->lexer
);
13014 statement
= add_stmt (build_empty_stmt (body_loc
));
13016 if (guard_tinfo
.keyword
== RID_IF
13017 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
13018 warning_at (body_loc
, OPT_Wempty_body
,
13019 "suggest braces around empty body in an %<if%> statement");
13020 else if (guard_tinfo
.keyword
== RID_ELSE
)
13021 warning_at (body_loc
, OPT_Wempty_body
,
13022 "suggest braces around empty body in an %<else%> statement");
13024 /* if a compound is opened, we simply parse the statement directly. */
13025 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13026 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
13027 /* If the token is not a `{', then we must take special action. */
13030 /* Create a compound-statement. */
13031 statement
= begin_compound_stmt (0);
13032 /* Parse the dependent-statement. */
13033 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
13034 &body_loc_after_labels
);
13035 /* Finish the dummy compound-statement. */
13036 finish_compound_stmt (statement
);
13039 token_indent_info next_tinfo
13040 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13041 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13043 if (body_loc_after_labels
!= UNKNOWN_LOCATION
13044 && next_tinfo
.type
!= CPP_SEMICOLON
)
13045 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
13046 guard_tinfo
.location
, guard_tinfo
.keyword
);
13048 /* Return the statement. */
13052 /* For some dependent statements (like `while (cond) statement'), we
13053 have already created a scope. Therefore, even if the dependent
13054 statement is a compound-statement, we do not want to create another
13058 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
13059 const token_indent_info
&guard_tinfo
)
13061 /* If the token is a `{', then we must take special action. */
13062 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13064 token_indent_info body_tinfo
13065 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13066 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13068 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13069 &loc_after_labels
);
13070 token_indent_info next_tinfo
13071 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13072 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13074 if (loc_after_labels
!= UNKNOWN_LOCATION
13075 && next_tinfo
.type
!= CPP_SEMICOLON
)
13076 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13077 guard_tinfo
.location
,
13078 guard_tinfo
.keyword
);
13082 /* Avoid calling cp_parser_compound_statement, so that we
13083 don't create a new scope. Do everything else by hand. */
13084 matching_braces braces
;
13085 braces
.require_open (parser
);
13086 /* If the next keyword is `__label__' we have a label declaration. */
13087 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13088 cp_parser_label_declaration (parser
);
13089 /* Parse an (optional) statement-seq. */
13090 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13091 braces
.require_close (parser
);
13095 /* Declarations [gram.dcl.dcl] */
13097 /* Parse an optional declaration-sequence.
13101 declaration-seq declaration */
13104 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13108 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13110 if (token
->type
== CPP_CLOSE_BRACE
13111 || token
->type
== CPP_EOF
)
13114 cp_parser_toplevel_declaration (parser
);
13118 /* Parse a declaration.
13122 function-definition
13123 template-declaration
13124 explicit-instantiation
13125 explicit-specialization
13126 linkage-specification
13127 namespace-definition
13135 __extension__ declaration */
13138 cp_parser_declaration (cp_parser
* parser
)
13142 int saved_pedantic
;
13144 tree attributes
= NULL_TREE
;
13146 /* Check for the `__extension__' keyword. */
13147 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13149 /* Parse the qualified declaration. */
13150 cp_parser_declaration (parser
);
13151 /* Restore the PEDANTIC flag. */
13152 pedantic
= saved_pedantic
;
13157 /* Try to figure out what kind of declaration is present. */
13158 token1
= *cp_lexer_peek_token (parser
->lexer
);
13160 if (token1
.type
!= CPP_EOF
)
13161 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13164 token2
.type
= CPP_EOF
;
13165 token2
.keyword
= RID_MAX
;
13168 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13169 p
= obstack_alloc (&declarator_obstack
, 0);
13171 /* If the next token is `extern' and the following token is a string
13172 literal, then we have a linkage specification. */
13173 if (token1
.keyword
== RID_EXTERN
13174 && cp_parser_is_pure_string_literal (&token2
))
13175 cp_parser_linkage_specification (parser
);
13176 /* If the next token is `template', then we have either a template
13177 declaration, an explicit instantiation, or an explicit
13179 else if (token1
.keyword
== RID_TEMPLATE
)
13181 /* `template <>' indicates a template specialization. */
13182 if (token2
.type
== CPP_LESS
13183 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13184 cp_parser_explicit_specialization (parser
);
13185 /* `template <' indicates a template declaration. */
13186 else if (token2
.type
== CPP_LESS
)
13187 cp_parser_template_declaration (parser
, /*member_p=*/false);
13188 /* Anything else must be an explicit instantiation. */
13190 cp_parser_explicit_instantiation (parser
);
13192 /* If the next token is `export', then we have a template
13194 else if (token1
.keyword
== RID_EXPORT
)
13195 cp_parser_template_declaration (parser
, /*member_p=*/false);
13196 /* If the next token is `extern', 'static' or 'inline' and the one
13197 after that is `template', we have a GNU extended explicit
13198 instantiation directive. */
13199 else if (cp_parser_allow_gnu_extensions_p (parser
)
13200 && (token1
.keyword
== RID_EXTERN
13201 || token1
.keyword
== RID_STATIC
13202 || token1
.keyword
== RID_INLINE
)
13203 && token2
.keyword
== RID_TEMPLATE
)
13204 cp_parser_explicit_instantiation (parser
);
13205 /* If the next token is `namespace', check for a named or unnamed
13206 namespace definition. */
13207 else if (token1
.keyword
== RID_NAMESPACE
13208 && (/* A named namespace definition. */
13209 (token2
.type
== CPP_NAME
13210 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13212 || (token2
.type
== CPP_OPEN_SQUARE
13213 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13214 == CPP_OPEN_SQUARE
)
13215 /* An unnamed namespace definition. */
13216 || token2
.type
== CPP_OPEN_BRACE
13217 || token2
.keyword
== RID_ATTRIBUTE
))
13218 cp_parser_namespace_definition (parser
);
13219 /* An inline (associated) namespace definition. */
13220 else if (token1
.keyword
== RID_INLINE
13221 && token2
.keyword
== RID_NAMESPACE
)
13222 cp_parser_namespace_definition (parser
);
13223 /* Objective-C++ declaration/definition. */
13224 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13225 cp_parser_objc_declaration (parser
, NULL_TREE
);
13226 else if (c_dialect_objc ()
13227 && token1
.keyword
== RID_ATTRIBUTE
13228 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13229 cp_parser_objc_declaration (parser
, attributes
);
13230 /* At this point we may have a template declared by a concept
13232 else if (flag_concepts
13233 && cp_parser_template_declaration_after_export (parser
,
13234 /*member_p=*/false))
13237 /* Try to parse a block-declaration, or a function-definition. */
13238 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13240 /* Free any declarators allocated. */
13241 obstack_free (&declarator_obstack
, p
);
13244 /* Parse a namespace-scope declaration. */
13247 cp_parser_toplevel_declaration (cp_parser
* parser
)
13249 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13251 if (token
->type
== CPP_PRAGMA
)
13252 /* A top-level declaration can consist solely of a #pragma. A
13253 nested declaration cannot, so this is done here and not in
13254 cp_parser_declaration. (A #pragma at block scope is
13255 handled in cp_parser_statement.) */
13256 cp_parser_pragma (parser
, pragma_external
, NULL
);
13257 else if (token
->type
== CPP_SEMICOLON
)
13259 /* A declaration consisting of a single semicolon is
13260 invalid. Allow it unless we're being pedantic. */
13261 cp_lexer_consume_token (parser
->lexer
);
13262 if (!in_system_header_at (input_location
))
13263 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13266 /* Parse the declaration itself. */
13267 cp_parser_declaration (parser
);
13270 /* Parse a block-declaration.
13275 namespace-alias-definition
13282 __extension__ block-declaration
13287 static_assert-declaration
13289 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13290 part of a declaration-statement. */
13293 cp_parser_block_declaration (cp_parser
*parser
,
13297 int saved_pedantic
;
13299 /* Check for the `__extension__' keyword. */
13300 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13302 /* Parse the qualified declaration. */
13303 cp_parser_block_declaration (parser
, statement_p
);
13304 /* Restore the PEDANTIC flag. */
13305 pedantic
= saved_pedantic
;
13310 /* Peek at the next token to figure out which kind of declaration is
13312 token1
= cp_lexer_peek_token (parser
->lexer
);
13314 /* If the next keyword is `asm', we have an asm-definition. */
13315 if (token1
->keyword
== RID_ASM
)
13318 cp_parser_commit_to_tentative_parse (parser
);
13319 cp_parser_asm_definition (parser
);
13321 /* If the next keyword is `namespace', we have a
13322 namespace-alias-definition. */
13323 else if (token1
->keyword
== RID_NAMESPACE
)
13324 cp_parser_namespace_alias_definition (parser
);
13325 /* If the next keyword is `using', we have a
13326 using-declaration, a using-directive, or an alias-declaration. */
13327 else if (token1
->keyword
== RID_USING
)
13332 cp_parser_commit_to_tentative_parse (parser
);
13333 /* If the token after `using' is `namespace', then we have a
13334 using-directive. */
13335 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13336 if (token2
->keyword
== RID_NAMESPACE
)
13337 cp_parser_using_directive (parser
);
13338 /* If the second token after 'using' is '=', then we have an
13339 alias-declaration. */
13340 else if (cxx_dialect
>= cxx11
13341 && token2
->type
== CPP_NAME
13342 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13343 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13344 cp_parser_alias_declaration (parser
);
13345 /* Otherwise, it's a using-declaration. */
13347 cp_parser_using_declaration (parser
,
13348 /*access_declaration_p=*/false);
13350 /* If the next keyword is `__label__' we have a misplaced label
13352 else if (token1
->keyword
== RID_LABEL
)
13354 cp_lexer_consume_token (parser
->lexer
);
13355 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13356 cp_parser_skip_to_end_of_statement (parser
);
13357 /* If the next token is now a `;', consume it. */
13358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13359 cp_lexer_consume_token (parser
->lexer
);
13361 /* If the next token is `static_assert' we have a static assertion. */
13362 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13363 cp_parser_static_assert (parser
, /*member_p=*/false);
13364 /* Anything else must be a simple-declaration. */
13366 cp_parser_simple_declaration (parser
, !statement_p
,
13367 /*maybe_range_for_decl*/NULL
);
13370 /* Parse a simple-declaration.
13372 simple-declaration:
13373 decl-specifier-seq [opt] init-declarator-list [opt] ;
13374 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13375 brace-or-equal-initializer ;
13377 init-declarator-list:
13379 init-declarator-list , init-declarator
13381 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13382 function-definition as a simple-declaration.
13384 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13385 parsed declaration if it is an uninitialized single declarator not followed
13386 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13387 if present, will not be consumed. */
13390 cp_parser_simple_declaration (cp_parser
* parser
,
13391 bool function_definition_allowed_p
,
13392 tree
*maybe_range_for_decl
)
13394 cp_decl_specifier_seq decl_specifiers
;
13395 int declares_class_or_enum
;
13396 bool saw_declarator
;
13397 location_t comma_loc
= UNKNOWN_LOCATION
;
13398 location_t init_loc
= UNKNOWN_LOCATION
;
13400 if (maybe_range_for_decl
)
13401 *maybe_range_for_decl
= NULL_TREE
;
13403 /* Defer access checks until we know what is being declared; the
13404 checks for names appearing in the decl-specifier-seq should be
13405 done as if we were in the scope of the thing being declared. */
13406 push_deferring_access_checks (dk_deferred
);
13408 /* Parse the decl-specifier-seq. We have to keep track of whether
13409 or not the decl-specifier-seq declares a named class or
13410 enumeration type, since that is the only case in which the
13411 init-declarator-list is allowed to be empty.
13415 In a simple-declaration, the optional init-declarator-list can be
13416 omitted only when declaring a class or enumeration, that is when
13417 the decl-specifier-seq contains either a class-specifier, an
13418 elaborated-type-specifier, or an enum-specifier. */
13419 cp_parser_decl_specifier_seq (parser
,
13420 CP_PARSER_FLAGS_OPTIONAL
,
13422 &declares_class_or_enum
);
13423 /* We no longer need to defer access checks. */
13424 stop_deferring_access_checks ();
13426 /* In a block scope, a valid declaration must always have a
13427 decl-specifier-seq. By not trying to parse declarators, we can
13428 resolve the declaration/expression ambiguity more quickly. */
13429 if (!function_definition_allowed_p
13430 && !decl_specifiers
.any_specifiers_p
)
13432 cp_parser_error (parser
, "expected declaration");
13436 /* If the next two tokens are both identifiers, the code is
13437 erroneous. The usual cause of this situation is code like:
13441 where "T" should name a type -- but does not. */
13442 if (!decl_specifiers
.any_type_specifiers_p
13443 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13445 /* If parsing tentatively, we should commit; we really are
13446 looking at a declaration. */
13447 cp_parser_commit_to_tentative_parse (parser
);
13452 cp_parser_maybe_commit_to_declaration (parser
,
13453 decl_specifiers
.any_specifiers_p
);
13455 /* Look for C++17 decomposition declaration. */
13456 for (size_t n
= 1; ; n
++)
13457 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13458 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13460 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13461 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13462 && decl_specifiers
.any_specifiers_p
)
13465 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13466 maybe_range_for_decl
,
13469 /* The next token should be either a `,' or a `;'. */
13470 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13471 /* If it's a `;', we are done. */
13472 if (token
->type
== CPP_SEMICOLON
)
13474 else if (maybe_range_for_decl
)
13476 if (*maybe_range_for_decl
== NULL_TREE
)
13477 *maybe_range_for_decl
= error_mark_node
;
13480 /* Anything else is an error. */
13483 /* If we have already issued an error message we don't need
13484 to issue another one. */
13485 if ((decl
!= error_mark_node
13486 && DECL_INITIAL (decl
) != error_mark_node
)
13487 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13488 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13489 /* Skip tokens until we reach the end of the statement. */
13490 cp_parser_skip_to_end_of_statement (parser
);
13491 /* If the next token is now a `;', consume it. */
13492 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13493 cp_lexer_consume_token (parser
->lexer
);
13501 bool auto_specifier_p
;
13502 /* NULL_TREE if both variable and function declaration are allowed,
13503 error_mark_node if function declaration are not allowed and
13504 a FUNCTION_DECL that should be diagnosed if it is followed by
13505 variable declarations. */
13506 tree auto_function_declaration
;
13508 last_type
= NULL_TREE
;
13510 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13511 auto_function_declaration
= NULL_TREE
;
13513 /* Keep going until we hit the `;' at the end of the simple
13515 saw_declarator
= false;
13516 while (cp_lexer_next_token_is_not (parser
->lexer
,
13520 bool function_definition_p
;
13522 tree auto_result
= NULL_TREE
;
13524 if (saw_declarator
)
13526 /* If we are processing next declarator, comma is expected */
13527 token
= cp_lexer_peek_token (parser
->lexer
);
13528 gcc_assert (token
->type
== CPP_COMMA
);
13529 cp_lexer_consume_token (parser
->lexer
);
13530 if (maybe_range_for_decl
)
13532 *maybe_range_for_decl
= error_mark_node
;
13533 if (comma_loc
== UNKNOWN_LOCATION
)
13534 comma_loc
= token
->location
;
13538 saw_declarator
= true;
13540 /* Parse the init-declarator. */
13541 decl
= cp_parser_init_declarator (parser
,
13542 CP_PARSER_FLAGS_NONE
,
13545 function_definition_allowed_p
,
13546 /*member_p=*/false,
13547 declares_class_or_enum
,
13548 &function_definition_p
,
13549 maybe_range_for_decl
,
13552 /* If an error occurred while parsing tentatively, exit quickly.
13553 (That usually happens when in the body of a function; each
13554 statement is treated as a declaration-statement until proven
13556 if (cp_parser_error_occurred (parser
))
13559 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13561 /* If the init-declarator-list contains more than one
13562 init-declarator, they shall all form declarations of
13564 if (auto_function_declaration
== NULL_TREE
)
13565 auto_function_declaration
13566 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13567 else if (TREE_CODE (decl
) == FUNCTION_DECL
13568 || auto_function_declaration
!= error_mark_node
)
13570 error_at (decl_specifiers
.locations
[ds_type_spec
],
13571 "non-variable %qD in declaration with more than one "
13572 "declarator with placeholder type",
13573 TREE_CODE (decl
) == FUNCTION_DECL
13574 ? decl
: auto_function_declaration
);
13575 auto_function_declaration
= error_mark_node
;
13580 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13583 && last_type
!= error_mark_node
13584 && !same_type_p (auto_result
, last_type
))
13586 /* If the list of declarators contains more than one declarator,
13587 the type of each declared variable is determined as described
13588 above. If the type deduced for the template parameter U is not
13589 the same in each deduction, the program is ill-formed. */
13590 error_at (decl_specifiers
.locations
[ds_type_spec
],
13591 "inconsistent deduction for %qT: %qT and then %qT",
13592 decl_specifiers
.type
, last_type
, auto_result
);
13593 last_type
= error_mark_node
;
13596 last_type
= auto_result
;
13599 /* Handle function definitions specially. */
13600 if (function_definition_p
)
13602 /* If the next token is a `,', then we are probably
13603 processing something like:
13607 which is erroneous. */
13608 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13610 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13611 error_at (token
->location
,
13613 " declarations and function-definitions is forbidden");
13615 /* Otherwise, we're done with the list of declarators. */
13618 pop_deferring_access_checks ();
13622 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13623 *maybe_range_for_decl
= decl
;
13624 /* The next token should be either a `,' or a `;'. */
13625 token
= cp_lexer_peek_token (parser
->lexer
);
13626 /* If it's a `,', there are more declarators to come. */
13627 if (token
->type
== CPP_COMMA
)
13628 /* will be consumed next time around */;
13629 /* If it's a `;', we are done. */
13630 else if (token
->type
== CPP_SEMICOLON
)
13632 else if (maybe_range_for_decl
)
13634 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13635 permerror (decl_specifiers
.locations
[ds_type_spec
],
13636 "types may not be defined in a for-range-declaration");
13639 /* Anything else is an error. */
13642 /* If we have already issued an error message we don't need
13643 to issue another one. */
13644 if ((decl
!= error_mark_node
13645 && DECL_INITIAL (decl
) != error_mark_node
)
13646 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13647 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13648 /* Skip tokens until we reach the end of the statement. */
13649 cp_parser_skip_to_end_of_statement (parser
);
13650 /* If the next token is now a `;', consume it. */
13651 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13652 cp_lexer_consume_token (parser
->lexer
);
13655 /* After the first time around, a function-definition is not
13656 allowed -- even if it was OK at first. For example:
13661 function_definition_allowed_p
= false;
13664 /* Issue an error message if no declarators are present, and the
13665 decl-specifier-seq does not itself declare a class or
13666 enumeration: [dcl.dcl]/3. */
13667 if (!saw_declarator
)
13669 if (cp_parser_declares_only_class_p (parser
))
13671 if (!declares_class_or_enum
13672 && decl_specifiers
.type
13673 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13674 /* Ensure an error is issued anyway when finish_decltype_type,
13675 called via cp_parser_decl_specifier_seq, returns a class or
13676 an enumeration (c++/51786). */
13677 decl_specifiers
.type
= NULL_TREE
;
13678 shadow_tag (&decl_specifiers
);
13680 /* Perform any deferred access checks. */
13681 perform_deferred_access_checks (tf_warning_or_error
);
13684 /* Consume the `;'. */
13686 if (!maybe_range_for_decl
)
13687 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13688 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13690 if (init_loc
!= UNKNOWN_LOCATION
)
13691 error_at (init_loc
, "initializer in range-based %<for%> loop");
13692 if (comma_loc
!= UNKNOWN_LOCATION
)
13693 error_at (comma_loc
,
13694 "multiple declarations in range-based %<for%> loop");
13698 pop_deferring_access_checks ();
13701 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13702 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13706 cp_parser_decomposition_declaration (cp_parser
*parser
,
13707 cp_decl_specifier_seq
*decl_specifiers
,
13708 tree
*maybe_range_for_decl
,
13709 location_t
*init_loc
)
13711 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13712 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13713 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13715 /* Parse the identifier-list. */
13716 auto_vec
<cp_expr
, 10> v
;
13717 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13720 cp_expr e
= cp_parser_identifier (parser
);
13721 if (e
.get_value () == error_mark_node
)
13724 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13726 cp_lexer_consume_token (parser
->lexer
);
13729 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13730 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13732 end_loc
= UNKNOWN_LOCATION
;
13733 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13736 cp_lexer_consume_token (parser
->lexer
);
13739 cp_parser_skip_to_end_of_statement (parser
);
13740 return error_mark_node
;
13744 if (cxx_dialect
< cxx17
)
13745 pedwarn (loc
, 0, "structured bindings only available with "
13746 "%<-std=c++17%> or %<-std=gnu++17%>");
13749 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13750 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13751 declarator
->id_loc
= loc
;
13752 if (ref_qual
!= REF_QUAL_NONE
)
13753 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13754 ref_qual
== REF_QUAL_RVALUE
,
13756 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13757 NULL_TREE
, decl_specifiers
->attributes
,
13759 tree orig_decl
= decl
;
13763 cp_decl_specifier_seq decl_specs
;
13764 clear_decl_specs (&decl_specs
);
13765 decl_specs
.type
= make_auto ();
13767 FOR_EACH_VEC_ELT (v
, i
, e
)
13770 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13771 sfk_none
, e
.get_location ());
13774 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13775 declarator
->id_loc
= e
.get_location ();
13777 tree elt_pushed_scope
;
13778 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13779 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13780 if (decl2
== error_mark_node
)
13781 decl
= error_mark_node
;
13782 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13784 /* Ensure we've diagnosed redeclaration if we aren't creating
13786 gcc_assert (errorcount
);
13787 decl
= error_mark_node
;
13791 if (elt_pushed_scope
)
13792 pop_scope (elt_pushed_scope
);
13797 error_at (loc
, "empty structured binding declaration");
13798 decl
= error_mark_node
;
13801 if (maybe_range_for_decl
== NULL
13802 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13804 bool non_constant_p
= false, is_direct_init
= false;
13805 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13806 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13808 if (initializer
== NULL_TREE
13809 || (TREE_CODE (initializer
) == TREE_LIST
13810 && TREE_CHAIN (initializer
))
13812 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13813 && CONSTRUCTOR_NELTS (initializer
) != 1))
13815 error_at (loc
, "invalid initializer for structured binding "
13817 initializer
= error_mark_node
;
13820 if (decl
!= error_mark_node
)
13822 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13823 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13824 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13825 cp_finish_decomp (decl
, prev
, v
.length ());
13828 else if (decl
!= error_mark_node
)
13830 *maybe_range_for_decl
= prev
;
13831 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13832 the underlying DECL. */
13833 cp_finish_decomp (decl
, prev
, v
.length ());
13837 pop_scope (pushed_scope
);
13839 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13841 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13842 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13848 /* Parse a decl-specifier-seq.
13850 decl-specifier-seq:
13851 decl-specifier-seq [opt] decl-specifier
13852 decl-specifier attribute-specifier-seq [opt] (C++11)
13855 storage-class-specifier
13866 Concepts Extension:
13871 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13873 The parser flags FLAGS is used to control type-specifier parsing.
13875 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13878 1: one of the decl-specifiers is an elaborated-type-specifier
13879 (i.e., a type declaration)
13880 2: one of the decl-specifiers is an enum-specifier or a
13881 class-specifier (i.e., a type definition)
13886 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13887 cp_parser_flags flags
,
13888 cp_decl_specifier_seq
*decl_specs
,
13889 int* declares_class_or_enum
)
13891 bool constructor_possible_p
= !parser
->in_declarator_p
;
13892 bool found_decl_spec
= false;
13893 cp_token
*start_token
= NULL
;
13896 /* Clear DECL_SPECS. */
13897 clear_decl_specs (decl_specs
);
13899 /* Assume no class or enumeration type is declared. */
13900 *declares_class_or_enum
= 0;
13902 /* Keep reading specifiers until there are no more to read. */
13905 bool constructor_p
;
13909 /* Peek at the next token. */
13910 token
= cp_lexer_peek_token (parser
->lexer
);
13912 /* Save the first token of the decl spec list for error
13915 start_token
= token
;
13916 /* Handle attributes. */
13917 if (cp_next_tokens_can_be_attribute_p (parser
))
13919 /* Parse the attributes. */
13920 tree attrs
= cp_parser_attributes_opt (parser
);
13922 /* In a sequence of declaration specifiers, c++11 attributes
13923 appertain to the type that precede them. In that case
13926 The attribute-specifier-seq affects the type only for
13927 the declaration it appears in, not other declarations
13928 involving the same type.
13930 But for now let's force the user to position the
13931 attribute either at the beginning of the declaration or
13932 after the declarator-id, which would clearly mean that it
13933 applies to the declarator. */
13934 if (cxx11_attribute_p (attrs
))
13936 if (!found_decl_spec
)
13937 /* The c++11 attribute is at the beginning of the
13938 declaration. It appertains to the entity being
13942 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13944 /* This is an attribute following a
13945 class-specifier. */
13946 if (decl_specs
->type_definition_p
)
13947 warn_misplaced_attr_for_class_type (token
->location
,
13953 decl_specs
->std_attributes
13954 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13955 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13956 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13962 decl_specs
->attributes
13963 = attr_chainon (decl_specs
->attributes
, attrs
);
13964 if (decl_specs
->locations
[ds_attribute
] == 0)
13965 decl_specs
->locations
[ds_attribute
] = token
->location
;
13968 /* Assume we will find a decl-specifier keyword. */
13969 found_decl_spec
= true;
13970 /* If the next token is an appropriate keyword, we can simply
13971 add it to the list. */
13972 switch (token
->keyword
)
13978 if (!at_class_scope_p ())
13980 gcc_rich_location
richloc (token
->location
);
13981 richloc
.add_fixit_remove ();
13982 error_at (&richloc
, "%<friend%> used outside of class");
13983 cp_lexer_purge_token (parser
->lexer
);
13988 /* Consume the token. */
13989 cp_lexer_consume_token (parser
->lexer
);
13993 case RID_CONSTEXPR
:
13995 cp_lexer_consume_token (parser
->lexer
);
14000 cp_lexer_consume_token (parser
->lexer
);
14001 /* In C++20 a concept definition is just 'concept name = expr;'
14002 Support that syntax by pretending we've seen 'bool'. */
14003 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
14004 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
14006 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
14007 token
, /*type_definition*/false);
14008 decl_specs
->any_type_specifiers_p
= true;
14012 /* function-specifier:
14019 cp_parser_function_specifier_opt (parser
, decl_specs
);
14026 /* Consume the token. */
14027 cp_lexer_consume_token (parser
->lexer
);
14028 /* A constructor declarator cannot appear in a typedef. */
14029 constructor_possible_p
= false;
14030 /* The "typedef" keyword can only occur in a declaration; we
14031 may as well commit at this point. */
14032 cp_parser_commit_to_tentative_parse (parser
);
14034 if (decl_specs
->storage_class
!= sc_none
)
14035 decl_specs
->conflicting_specifiers_p
= true;
14038 /* storage-class-specifier:
14048 if (cxx_dialect
== cxx98
)
14050 /* Consume the token. */
14051 cp_lexer_consume_token (parser
->lexer
);
14053 /* Complain about `auto' as a storage specifier, if
14054 we're complaining about C++0x compatibility. */
14055 gcc_rich_location
richloc (token
->location
);
14056 richloc
.add_fixit_remove ();
14057 warning_at (&richloc
, OPT_Wc__11_compat
,
14058 "%<auto%> changes meaning in C++11; "
14059 "please remove it");
14061 /* Set the storage class anyway. */
14062 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
14066 /* C++0x auto type-specifier. */
14067 found_decl_spec
= false;
14074 /* Consume the token. */
14075 cp_lexer_consume_token (parser
->lexer
);
14076 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14080 /* Consume the token. */
14082 cp_lexer_consume_token (parser
->lexer
);
14086 /* We did not yet find a decl-specifier yet. */
14087 found_decl_spec
= false;
14091 if (found_decl_spec
14092 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14093 && token
->keyword
!= RID_CONSTEXPR
)
14094 error ("decl-specifier invalid in condition");
14096 if (found_decl_spec
14097 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14098 && token
->keyword
!= RID_MUTABLE
14099 && token
->keyword
!= RID_CONSTEXPR
)
14100 error_at (token
->location
, "%qD invalid in lambda",
14101 ridpointers
[token
->keyword
]);
14104 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14106 /* Constructors are a special case. The `S' in `S()' is not a
14107 decl-specifier; it is the beginning of the declarator. */
14109 = (!found_decl_spec
14110 && constructor_possible_p
14111 && (cp_parser_constructor_declarator_p
14112 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
14115 /* If we don't have a DECL_SPEC yet, then we must be looking at
14116 a type-specifier. */
14117 if (!found_decl_spec
&& !constructor_p
)
14119 int decl_spec_declares_class_or_enum
;
14120 bool is_cv_qualifier
;
14124 = cp_parser_type_specifier (parser
, flags
,
14126 /*is_declaration=*/true,
14127 &decl_spec_declares_class_or_enum
,
14129 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14131 /* If this type-specifier referenced a user-defined type
14132 (a typedef, class-name, etc.), then we can't allow any
14133 more such type-specifiers henceforth.
14137 The longest sequence of decl-specifiers that could
14138 possibly be a type name is taken as the
14139 decl-specifier-seq of a declaration. The sequence shall
14140 be self-consistent as described below.
14144 As a general rule, at most one type-specifier is allowed
14145 in the complete decl-specifier-seq of a declaration. The
14146 only exceptions are the following:
14148 -- const or volatile can be combined with any other
14151 -- signed or unsigned can be combined with char, long,
14159 void g (const int Pc);
14161 Here, Pc is *not* part of the decl-specifier seq; it's
14162 the declarator. Therefore, once we see a type-specifier
14163 (other than a cv-qualifier), we forbid any additional
14164 user-defined types. We *do* still allow things like `int
14165 int' to be considered a decl-specifier-seq, and issue the
14166 error message later. */
14167 if (type_spec
&& !is_cv_qualifier
)
14168 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14169 /* A constructor declarator cannot follow a type-specifier. */
14172 constructor_possible_p
= false;
14173 found_decl_spec
= true;
14174 if (!is_cv_qualifier
)
14175 decl_specs
->any_type_specifiers_p
= true;
14177 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14178 error_at (token
->location
, "type-specifier invalid in lambda");
14182 /* If we still do not have a DECL_SPEC, then there are no more
14183 decl-specifiers. */
14184 if (!found_decl_spec
)
14187 decl_specs
->any_specifiers_p
= true;
14188 /* After we see one decl-specifier, further decl-specifiers are
14189 always optional. */
14190 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14193 /* Don't allow a friend specifier with a class definition. */
14194 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14195 && (*declares_class_or_enum
& 2))
14196 error_at (decl_specs
->locations
[ds_friend
],
14197 "class definition may not be declared a friend");
14200 /* Parse an (optional) storage-class-specifier.
14202 storage-class-specifier:
14211 storage-class-specifier:
14214 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14217 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14219 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14222 if (cxx_dialect
!= cxx98
)
14224 /* Fall through for C++98. */
14225 gcc_fallthrough ();
14232 /* Consume the token. */
14233 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14240 /* Parse an (optional) function-specifier.
14242 function-specifier:
14248 explicit(constant-expression)
14250 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14251 Updates DECL_SPECS, if it is non-NULL. */
14254 cp_parser_function_specifier_opt (cp_parser
* parser
,
14255 cp_decl_specifier_seq
*decl_specs
)
14257 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14258 switch (token
->keyword
)
14261 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14265 /* 14.5.2.3 [temp.mem]
14267 A member function template shall not be virtual. */
14268 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14269 && current_class_type
)
14270 error_at (token
->location
, "templates may not be %<virtual%>");
14272 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14277 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14278 /* If we see '(', it's C++20 explicit(bool). */
14280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14282 matching_parens parens
;
14283 parens
.consume_open (parser
);
14285 /* New types are not allowed in an explicit-specifier. */
14286 const char *saved_message
14287 = parser
->type_definition_forbidden_message
;
14288 parser
->type_definition_forbidden_message
14289 = G_("types may not be defined in explicit-specifier");
14291 if (cxx_dialect
< cxx2a
)
14292 pedwarn (token
->location
, 0,
14293 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14294 "or %<-std=gnu++2a%>");
14296 /* Parse the constant-expression. */
14297 expr
= cp_parser_constant_expression (parser
);
14299 /* Restore the saved message. */
14300 parser
->type_definition_forbidden_message
= saved_message
;
14301 parens
.require_close (parser
);
14304 /* The explicit-specifier explicit without a constant-expression is
14305 equivalent to the explicit-specifier explicit(true). */
14306 expr
= boolean_true_node
;
14309 "the constant-expression, if supplied, shall be a contextually
14310 converted constant expression of type bool." */
14311 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14312 /* We could evaluate it -- mark the decl as appropriate. */
14313 if (expr
== boolean_true_node
)
14314 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14315 else if (expr
== boolean_false_node
)
14316 /* Don't mark the decl as explicit. */;
14317 else if (decl_specs
)
14318 /* The expression was value-dependent. Remember it so that we can
14319 substitute it later. */
14320 decl_specs
->explicit_specifier
= expr
;
14328 /* Consume the token. */
14329 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14332 /* Parse a linkage-specification.
14334 linkage-specification:
14335 extern string-literal { declaration-seq [opt] }
14336 extern string-literal declaration */
14339 cp_parser_linkage_specification (cp_parser
* parser
)
14343 /* Look for the `extern' keyword. */
14344 cp_token
*extern_token
14345 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14347 /* Look for the string-literal. */
14348 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14349 linkage
= cp_parser_string_literal (parser
, false, false);
14351 /* Transform the literal into an identifier. If the literal is a
14352 wide-character string, or contains embedded NULs, then we can't
14353 handle it as the user wants. */
14354 if (strlen (TREE_STRING_POINTER (linkage
))
14355 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14357 cp_parser_error (parser
, "invalid linkage-specification");
14358 /* Assume C++ linkage. */
14359 linkage
= lang_name_cplusplus
;
14362 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14364 /* We're now using the new linkage. */
14365 push_lang_context (linkage
);
14367 /* Preserve the location of the the innermost linkage specification,
14368 tracking the locations of nested specifications via a local. */
14369 location_t saved_location
14370 = parser
->innermost_linkage_specification_location
;
14371 /* Construct a location ranging from the start of the "extern" to
14372 the end of the string-literal, with the caret at the start, e.g.:
14376 parser
->innermost_linkage_specification_location
14377 = make_location (extern_token
->location
,
14378 extern_token
->location
,
14379 get_finish (string_token
->location
));
14381 /* If the next token is a `{', then we're using the first
14383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14385 cp_ensure_no_omp_declare_simd (parser
);
14386 cp_ensure_no_oacc_routine (parser
);
14388 /* Consume the `{' token. */
14389 matching_braces braces
;
14390 braces
.consume_open (parser
);
14391 /* Parse the declarations. */
14392 cp_parser_declaration_seq_opt (parser
);
14393 /* Look for the closing `}'. */
14394 braces
.require_close (parser
);
14396 /* Otherwise, there's just one declaration. */
14399 bool saved_in_unbraced_linkage_specification_p
;
14401 saved_in_unbraced_linkage_specification_p
14402 = parser
->in_unbraced_linkage_specification_p
;
14403 parser
->in_unbraced_linkage_specification_p
= true;
14404 cp_parser_declaration (parser
);
14405 parser
->in_unbraced_linkage_specification_p
14406 = saved_in_unbraced_linkage_specification_p
;
14409 /* We're done with the linkage-specification. */
14410 pop_lang_context ();
14412 /* Restore location of parent linkage specification, if any. */
14413 parser
->innermost_linkage_specification_location
= saved_location
;
14416 /* Parse a static_assert-declaration.
14418 static_assert-declaration:
14419 static_assert ( constant-expression , string-literal ) ;
14420 static_assert ( constant-expression ) ; (C++17)
14422 If MEMBER_P, this static_assert is a class member. */
14425 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14428 location_t token_loc
;
14432 /* Peek at the `static_assert' token so we can keep track of exactly
14433 where the static assertion started. */
14434 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14436 /* Look for the `static_assert' keyword. */
14437 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14441 /* We know we are in a static assertion; commit to any tentative
14443 if (cp_parser_parsing_tentatively (parser
))
14444 cp_parser_commit_to_tentative_parse (parser
);
14446 /* Parse the `(' starting the static assertion condition. */
14447 matching_parens parens
;
14448 parens
.require_open (parser
);
14450 /* Parse the constant-expression. Allow a non-constant expression
14451 here in order to give better diagnostics in finish_static_assert. */
14453 cp_parser_constant_expression (parser
,
14454 /*allow_non_constant_p=*/true,
14455 /*non_constant_p=*/&dummy
);
14457 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14459 if (cxx_dialect
< cxx17
)
14460 pedwarn (input_location
, OPT_Wpedantic
,
14461 "static_assert without a message "
14462 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14464 cp_lexer_consume_token (parser
->lexer
);
14465 message
= build_string (1, "");
14466 TREE_TYPE (message
) = char_array_type_node
;
14467 fix_string_type (message
);
14471 /* Parse the separating `,'. */
14472 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14474 /* Parse the string-literal message. */
14475 message
= cp_parser_string_literal (parser
,
14476 /*translate=*/false,
14479 /* A `)' completes the static assertion. */
14480 if (!parens
.require_close (parser
))
14481 cp_parser_skip_to_closing_parenthesis (parser
,
14482 /*recovering=*/true,
14483 /*or_comma=*/false,
14484 /*consume_paren=*/true);
14487 /* A semicolon terminates the declaration. */
14488 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14490 /* Get the location for the static assertion. Use that of the
14491 condition if available, otherwise, use that of the "static_assert"
14493 location_t assert_loc
= condition
.get_location ();
14494 if (assert_loc
== UNKNOWN_LOCATION
)
14495 assert_loc
= token_loc
;
14497 /* Complete the static assertion, which may mean either processing
14498 the static assert now or saving it for template instantiation. */
14499 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14502 /* Parse the expression in decltype ( expression ). */
14505 cp_parser_decltype_expr (cp_parser
*parser
,
14506 bool &id_expression_or_member_access_p
)
14508 cp_token
*id_expr_start_token
;
14511 /* Since we're going to preserve any side-effects from this parse, set up a
14512 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14513 in the expression. */
14514 tentative_firewall
firewall (parser
);
14516 /* First, try parsing an id-expression. */
14517 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14518 cp_parser_parse_tentatively (parser
);
14519 expr
= cp_parser_id_expression (parser
,
14520 /*template_keyword_p=*/false,
14521 /*check_dependency_p=*/true,
14522 /*template_p=*/NULL
,
14523 /*declarator_p=*/false,
14524 /*optional_p=*/false);
14526 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14528 bool non_integral_constant_expression_p
= false;
14529 tree id_expression
= expr
;
14531 const char *error_msg
;
14533 if (identifier_p (expr
))
14534 /* Lookup the name we got back from the id-expression. */
14535 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14536 id_expr_start_token
->location
);
14538 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14539 /* A template without args is not a complete id-expression. */
14540 expr
= error_mark_node
;
14543 && expr
!= error_mark_node
14544 && TREE_CODE (expr
) != TYPE_DECL
14545 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14546 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14547 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14549 /* Complete lookup of the id-expression. */
14550 expr
= (finish_id_expression
14551 (id_expression
, expr
, parser
->scope
, &idk
,
14552 /*integral_constant_expression_p=*/false,
14553 /*allow_non_integral_constant_expression_p=*/true,
14554 &non_integral_constant_expression_p
,
14555 /*template_p=*/false,
14557 /*address_p=*/false,
14558 /*template_arg_p=*/false,
14560 id_expr_start_token
->location
));
14562 if (expr
== error_mark_node
)
14563 /* We found an id-expression, but it was something that we
14564 should not have found. This is an error, not something
14565 we can recover from, so note that we found an
14566 id-expression and we'll recover as gracefully as
14568 id_expression_or_member_access_p
= true;
14572 && expr
!= error_mark_node
14573 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14574 /* We have an id-expression. */
14575 id_expression_or_member_access_p
= true;
14578 if (!id_expression_or_member_access_p
)
14580 /* Abort the id-expression parse. */
14581 cp_parser_abort_tentative_parse (parser
);
14583 /* Parsing tentatively, again. */
14584 cp_parser_parse_tentatively (parser
);
14586 /* Parse a class member access. */
14587 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14588 /*cast_p=*/false, /*decltype*/true,
14589 /*member_access_only_p=*/true, NULL
);
14592 && expr
!= error_mark_node
14593 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14594 /* We have an id-expression. */
14595 id_expression_or_member_access_p
= true;
14598 if (id_expression_or_member_access_p
)
14599 /* We have parsed the complete id-expression or member access. */
14600 cp_parser_parse_definitely (parser
);
14603 /* Abort our attempt to parse an id-expression or member access
14605 cp_parser_abort_tentative_parse (parser
);
14607 /* Commit to the tentative_firewall so we get syntax errors. */
14608 cp_parser_commit_to_tentative_parse (parser
);
14610 /* Parse a full expression. */
14611 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14612 /*decltype_p=*/true);
14618 /* Parse a `decltype' type. Returns the type.
14620 simple-type-specifier:
14621 decltype ( expression )
14623 decltype ( auto ) */
14626 cp_parser_decltype (cp_parser
*parser
)
14628 bool id_expression_or_member_access_p
= false;
14629 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14631 if (start_token
->type
== CPP_DECLTYPE
)
14633 /* Already parsed. */
14634 cp_lexer_consume_token (parser
->lexer
);
14635 return saved_checks_value (start_token
->u
.tree_check_value
);
14638 /* Look for the `decltype' token. */
14639 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14640 return error_mark_node
;
14642 /* Parse the opening `('. */
14643 matching_parens parens
;
14644 if (!parens
.require_open (parser
))
14645 return error_mark_node
;
14647 push_deferring_access_checks (dk_deferred
);
14649 tree expr
= NULL_TREE
;
14651 if (cxx_dialect
>= cxx14
14652 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14653 /* decltype (auto) */
14654 cp_lexer_consume_token (parser
->lexer
);
14657 /* decltype (expression) */
14659 /* Types cannot be defined in a `decltype' expression. Save away the
14660 old message and set the new one. */
14661 const char *saved_message
= parser
->type_definition_forbidden_message
;
14662 parser
->type_definition_forbidden_message
14663 = G_("types may not be defined in %<decltype%> expressions");
14665 /* The restrictions on constant-expressions do not apply inside
14666 decltype expressions. */
14667 bool saved_integral_constant_expression_p
14668 = parser
->integral_constant_expression_p
;
14669 bool saved_non_integral_constant_expression_p
14670 = parser
->non_integral_constant_expression_p
;
14671 parser
->integral_constant_expression_p
= false;
14673 /* Within a parenthesized expression, a `>' token is always
14674 the greater-than operator. */
14675 bool saved_greater_than_is_operator_p
14676 = parser
->greater_than_is_operator_p
;
14677 parser
->greater_than_is_operator_p
= true;
14679 /* Do not actually evaluate the expression. */
14680 ++cp_unevaluated_operand
;
14682 /* Do not warn about problems with the expression. */
14683 ++c_inhibit_evaluation_warnings
;
14685 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14686 STRIP_ANY_LOCATION_WRAPPER (expr
);
14688 /* Go back to evaluating expressions. */
14689 --cp_unevaluated_operand
;
14690 --c_inhibit_evaluation_warnings
;
14692 /* The `>' token might be the end of a template-id or
14693 template-parameter-list now. */
14694 parser
->greater_than_is_operator_p
14695 = saved_greater_than_is_operator_p
;
14697 /* Restore the old message and the integral constant expression
14699 parser
->type_definition_forbidden_message
= saved_message
;
14700 parser
->integral_constant_expression_p
14701 = saved_integral_constant_expression_p
;
14702 parser
->non_integral_constant_expression_p
14703 = saved_non_integral_constant_expression_p
;
14706 /* Parse to the closing `)'. */
14707 if (!parens
.require_close (parser
))
14709 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14710 /*consume_paren=*/true);
14711 pop_deferring_access_checks ();
14712 return error_mark_node
;
14718 expr
= make_decltype_auto ();
14719 AUTO_IS_DECLTYPE (expr
) = true;
14722 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14723 tf_warning_or_error
);
14725 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14727 start_token
->type
= CPP_DECLTYPE
;
14728 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14729 start_token
->u
.tree_check_value
->value
= expr
;
14730 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14731 start_token
->keyword
= RID_MAX
;
14732 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14734 pop_to_parent_deferring_access_checks ();
14739 /* Special member functions [gram.special] */
14741 /* Parse a conversion-function-id.
14743 conversion-function-id:
14744 operator conversion-type-id
14746 Returns an IDENTIFIER_NODE representing the operator. */
14749 cp_parser_conversion_function_id (cp_parser
* parser
)
14753 tree saved_qualifying_scope
;
14754 tree saved_object_scope
;
14755 tree pushed_scope
= NULL_TREE
;
14757 /* Look for the `operator' token. */
14758 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14759 return error_mark_node
;
14760 /* When we parse the conversion-type-id, the current scope will be
14761 reset. However, we need that information in able to look up the
14762 conversion function later, so we save it here. */
14763 saved_scope
= parser
->scope
;
14764 saved_qualifying_scope
= parser
->qualifying_scope
;
14765 saved_object_scope
= parser
->object_scope
;
14766 /* We must enter the scope of the class so that the names of
14767 entities declared within the class are available in the
14768 conversion-type-id. For example, consider:
14775 S::operator I() { ... }
14777 In order to see that `I' is a type-name in the definition, we
14778 must be in the scope of `S'. */
14780 pushed_scope
= push_scope (saved_scope
);
14781 /* Parse the conversion-type-id. */
14782 type
= cp_parser_conversion_type_id (parser
);
14783 /* Leave the scope of the class, if any. */
14785 pop_scope (pushed_scope
);
14786 /* Restore the saved scope. */
14787 parser
->scope
= saved_scope
;
14788 parser
->qualifying_scope
= saved_qualifying_scope
;
14789 parser
->object_scope
= saved_object_scope
;
14790 /* If the TYPE is invalid, indicate failure. */
14791 if (type
== error_mark_node
)
14792 return error_mark_node
;
14793 return make_conv_op_name (type
);
14796 /* Parse a conversion-type-id:
14798 conversion-type-id:
14799 type-specifier-seq conversion-declarator [opt]
14801 Returns the TYPE specified. */
14804 cp_parser_conversion_type_id (cp_parser
* parser
)
14807 cp_decl_specifier_seq type_specifiers
;
14808 cp_declarator
*declarator
;
14809 tree type_specified
;
14810 const char *saved_message
;
14812 /* Parse the attributes. */
14813 attributes
= cp_parser_attributes_opt (parser
);
14815 saved_message
= parser
->type_definition_forbidden_message
;
14816 parser
->type_definition_forbidden_message
14817 = G_("types may not be defined in a conversion-type-id");
14819 /* Parse the type-specifiers. */
14820 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
14821 /*is_declaration=*/false,
14822 /*is_trailing_return=*/false,
14825 parser
->type_definition_forbidden_message
= saved_message
;
14827 /* If that didn't work, stop. */
14828 if (type_specifiers
.type
== error_mark_node
)
14829 return error_mark_node
;
14830 /* Parse the conversion-declarator. */
14831 declarator
= cp_parser_conversion_declarator_opt (parser
);
14833 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14834 /*initialized=*/0, &attributes
);
14836 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14838 /* Don't give this error when parsing tentatively. This happens to
14839 work because we always parse this definitively once. */
14840 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14841 && type_uses_auto (type_specified
))
14843 if (cxx_dialect
< cxx14
)
14845 error ("invalid use of %<auto%> in conversion operator");
14846 return error_mark_node
;
14848 else if (template_parm_scope_p ())
14849 warning (0, "use of %<auto%> in member template "
14850 "conversion operator can never be deduced");
14853 return type_specified
;
14856 /* Parse an (optional) conversion-declarator.
14858 conversion-declarator:
14859 ptr-operator conversion-declarator [opt]
14863 static cp_declarator
*
14864 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14866 enum tree_code code
;
14867 tree class_type
, std_attributes
= NULL_TREE
;
14868 cp_cv_quals cv_quals
;
14870 /* We don't know if there's a ptr-operator next, or not. */
14871 cp_parser_parse_tentatively (parser
);
14872 /* Try the ptr-operator. */
14873 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14875 /* If it worked, look for more conversion-declarators. */
14876 if (cp_parser_parse_definitely (parser
))
14878 cp_declarator
*declarator
;
14880 /* Parse another optional declarator. */
14881 declarator
= cp_parser_conversion_declarator_opt (parser
);
14883 declarator
= cp_parser_make_indirect_declarator
14884 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14892 /* Parse an (optional) ctor-initializer.
14895 : mem-initializer-list */
14898 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14900 /* If the next token is not a `:', then there is no
14901 ctor-initializer. */
14902 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14904 /* Do default initialization of any bases and members. */
14905 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14906 finish_mem_initializers (NULL_TREE
);
14910 /* Consume the `:' token. */
14911 cp_lexer_consume_token (parser
->lexer
);
14912 /* And the mem-initializer-list. */
14913 cp_parser_mem_initializer_list (parser
);
14916 /* Parse a mem-initializer-list.
14918 mem-initializer-list:
14919 mem-initializer ... [opt]
14920 mem-initializer ... [opt] , mem-initializer-list */
14923 cp_parser_mem_initializer_list (cp_parser
* parser
)
14925 tree mem_initializer_list
= NULL_TREE
;
14926 tree target_ctor
= error_mark_node
;
14927 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14929 /* Let the semantic analysis code know that we are starting the
14930 mem-initializer-list. */
14931 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14932 error_at (token
->location
,
14933 "only constructors take member initializers");
14935 /* Loop through the list. */
14938 tree mem_initializer
;
14940 token
= cp_lexer_peek_token (parser
->lexer
);
14941 /* Parse the mem-initializer. */
14942 mem_initializer
= cp_parser_mem_initializer (parser
);
14943 /* If the next token is a `...', we're expanding member initializers. */
14944 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14946 || (mem_initializer
!= error_mark_node
14947 && check_for_bare_parameter_packs (TREE_PURPOSE
14948 (mem_initializer
))))
14950 /* Consume the `...'. */
14952 cp_lexer_consume_token (parser
->lexer
);
14954 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14955 can be expanded but members cannot. */
14956 if (mem_initializer
!= error_mark_node
14957 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14959 error_at (token
->location
,
14960 "cannot expand initializer for member %qD",
14961 TREE_PURPOSE (mem_initializer
));
14962 mem_initializer
= error_mark_node
;
14965 /* Construct the pack expansion type. */
14966 if (mem_initializer
!= error_mark_node
)
14967 mem_initializer
= make_pack_expansion (mem_initializer
);
14969 if (target_ctor
!= error_mark_node
14970 && mem_initializer
!= error_mark_node
)
14972 error ("mem-initializer for %qD follows constructor delegation",
14973 TREE_PURPOSE (mem_initializer
));
14974 mem_initializer
= error_mark_node
;
14976 /* Look for a target constructor. */
14977 if (mem_initializer
!= error_mark_node
14978 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14979 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14981 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14982 if (mem_initializer_list
)
14984 error ("constructor delegation follows mem-initializer for %qD",
14985 TREE_PURPOSE (mem_initializer_list
));
14986 mem_initializer
= error_mark_node
;
14988 target_ctor
= mem_initializer
;
14990 /* Add it to the list, unless it was erroneous. */
14991 if (mem_initializer
!= error_mark_node
)
14993 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14994 mem_initializer_list
= mem_initializer
;
14996 /* If the next token is not a `,', we're done. */
14997 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14999 /* Consume the `,' token. */
15000 cp_lexer_consume_token (parser
->lexer
);
15003 /* Perform semantic analysis. */
15004 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15005 finish_mem_initializers (mem_initializer_list
);
15008 /* Parse a mem-initializer.
15011 mem-initializer-id ( expression-list [opt] )
15012 mem-initializer-id braced-init-list
15017 ( expression-list [opt] )
15019 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15020 class) or FIELD_DECL (for a non-static data member) to initialize;
15021 the TREE_VALUE is the expression-list. An empty initialization
15022 list is represented by void_list_node. */
15025 cp_parser_mem_initializer (cp_parser
* parser
)
15027 tree mem_initializer_id
;
15028 tree expression_list
;
15030 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15032 /* Find out what is being initialized. */
15033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15035 permerror (token
->location
,
15036 "anachronistic old-style base class initializer");
15037 mem_initializer_id
= NULL_TREE
;
15041 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
15042 if (mem_initializer_id
== error_mark_node
)
15043 return mem_initializer_id
;
15045 member
= expand_member_init (mem_initializer_id
);
15046 if (member
&& !DECL_P (member
))
15047 in_base_initializer
= 1;
15049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15051 bool expr_non_constant_p
;
15052 cp_lexer_set_source_position (parser
->lexer
);
15053 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
15054 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
15055 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
15056 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
15060 vec
<tree
, va_gc
> *vec
;
15061 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
15063 /*allow_expansion_p=*/true,
15064 /*non_constant_p=*/NULL
,
15065 /*close_paren_loc=*/NULL
,
15066 /*wrap_locations_p=*/true);
15068 return error_mark_node
;
15069 expression_list
= build_tree_list_vec (vec
);
15070 release_tree_vector (vec
);
15073 if (expression_list
== error_mark_node
)
15074 return error_mark_node
;
15075 if (!expression_list
)
15076 expression_list
= void_type_node
;
15078 in_base_initializer
= 0;
15080 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
15083 /* Parse a mem-initializer-id.
15085 mem-initializer-id:
15086 :: [opt] nested-name-specifier [opt] class-name
15087 decltype-specifier (C++11)
15090 Returns a TYPE indicating the class to be initialized for the first
15091 production (and the second in C++11). Returns an IDENTIFIER_NODE
15092 indicating the data member to be initialized for the last production. */
15095 cp_parser_mem_initializer_id (cp_parser
* parser
)
15097 bool global_scope_p
;
15098 bool nested_name_specifier_p
;
15099 bool template_p
= false;
15102 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15104 /* `typename' is not allowed in this context ([temp.res]). */
15105 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15107 error_at (token
->location
,
15108 "keyword %<typename%> not allowed in this context (a qualified "
15109 "member initializer is implicitly a type)");
15110 cp_lexer_consume_token (parser
->lexer
);
15112 /* Look for the optional `::' operator. */
15114 = (cp_parser_global_scope_opt (parser
,
15115 /*current_scope_valid_p=*/false)
15117 /* Look for the optional nested-name-specifier. The simplest way to
15122 The keyword `typename' is not permitted in a base-specifier or
15123 mem-initializer; in these contexts a qualified name that
15124 depends on a template-parameter is implicitly assumed to be a
15127 is to assume that we have seen the `typename' keyword at this
15129 nested_name_specifier_p
15130 = (cp_parser_nested_name_specifier_opt (parser
,
15131 /*typename_keyword_p=*/true,
15132 /*check_dependency_p=*/true,
15134 /*is_declaration=*/true)
15136 if (nested_name_specifier_p
)
15137 template_p
= cp_parser_optional_template_keyword (parser
);
15138 /* If there is a `::' operator or a nested-name-specifier, then we
15139 are definitely looking for a class-name. */
15140 if (global_scope_p
|| nested_name_specifier_p
)
15141 return cp_parser_class_name (parser
,
15142 /*typename_keyword_p=*/true,
15143 /*template_keyword_p=*/template_p
,
15145 /*check_dependency_p=*/true,
15146 /*class_head_p=*/false,
15147 /*is_declaration=*/true);
15148 /* Otherwise, we could also be looking for an ordinary identifier. */
15149 cp_parser_parse_tentatively (parser
);
15150 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15151 /* Try a decltype-specifier. */
15152 id
= cp_parser_decltype (parser
);
15154 /* Otherwise, try a class-name. */
15155 id
= cp_parser_class_name (parser
,
15156 /*typename_keyword_p=*/true,
15157 /*template_keyword_p=*/false,
15159 /*check_dependency_p=*/true,
15160 /*class_head_p=*/false,
15161 /*is_declaration=*/true);
15162 /* If we found one, we're done. */
15163 if (cp_parser_parse_definitely (parser
))
15165 /* Otherwise, look for an ordinary identifier. */
15166 return cp_parser_identifier (parser
);
15169 /* Overloading [gram.over] */
15171 /* Parse an operator-function-id.
15173 operator-function-id:
15176 Returns an IDENTIFIER_NODE for the operator which is a
15177 human-readable spelling of the identifier, e.g., `operator +'. */
15180 cp_parser_operator_function_id (cp_parser
* parser
)
15182 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15183 /* Look for the `operator' keyword. */
15184 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15185 return error_mark_node
;
15186 /* And then the name of the operator itself. */
15187 return cp_parser_operator (parser
, start_loc
);
15190 /* Return an identifier node for a user-defined literal operator.
15191 The suffix identifier is chained to the operator name identifier. */
15194 cp_literal_operator_id (const char* name
)
15197 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15198 + strlen (name
) + 10);
15199 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15200 identifier
= get_identifier (buffer
);
15205 /* Parse an operator.
15208 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15209 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15210 || ++ -- , ->* -> () []
15217 Returns an IDENTIFIER_NODE for the operator which is a
15218 human-readable spelling of the identifier, e.g., `operator +'. */
15221 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15223 tree id
= NULL_TREE
;
15227 /* Peek at the next token. */
15228 token
= cp_lexer_peek_token (parser
->lexer
);
15230 location_t end_loc
= token
->location
;
15232 /* Figure out which operator we have. */
15233 enum tree_code op
= ERROR_MARK
;
15234 bool assop
= false;
15235 bool consumed
= false;
15236 switch (token
->type
)
15240 /* The keyword should be either `new' or `delete'. */
15241 if (token
->keyword
== RID_NEW
)
15243 else if (token
->keyword
== RID_DELETE
)
15248 /* Consume the `new' or `delete' token. */
15249 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15251 /* Peek at the next token. */
15252 token
= cp_lexer_peek_token (parser
->lexer
);
15253 /* If it's a `[' token then this is the array variant of the
15255 if (token
->type
== CPP_OPEN_SQUARE
)
15257 /* Consume the `[' token. */
15258 cp_lexer_consume_token (parser
->lexer
);
15259 /* Look for the `]' token. */
15260 if (cp_token
*close_token
15261 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15262 end_loc
= close_token
->location
;
15263 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15282 op
= TRUNC_DIV_EXPR
;
15286 op
= TRUNC_MOD_EXPR
;
15306 op
= TRUTH_NOT_EXPR
;
15339 op
= TRUNC_DIV_EXPR
;
15344 op
= TRUNC_MOD_EXPR
;
15370 case CPP_LSHIFT_EQ
:
15375 case CPP_RSHIFT_EQ
:
15392 case CPP_GREATER_EQ
:
15397 op
= TRUTH_ANDIF_EXPR
;
15401 op
= TRUTH_ORIF_EXPR
;
15404 case CPP_PLUS_PLUS
:
15405 op
= POSTINCREMENT_EXPR
;
15408 case CPP_MINUS_MINUS
:
15409 op
= PREDECREMENT_EXPR
;
15413 op
= COMPOUND_EXPR
;
15416 case CPP_DEREF_STAR
:
15421 op
= COMPONENT_REF
;
15424 case CPP_OPEN_PAREN
:
15426 /* Consume the `('. */
15427 matching_parens parens
;
15428 parens
.consume_open (parser
);
15429 /* Look for the matching `)'. */
15430 token
= parens
.require_close (parser
);
15432 end_loc
= token
->location
;
15438 case CPP_OPEN_SQUARE
:
15439 /* Consume the `['. */
15440 cp_lexer_consume_token (parser
->lexer
);
15441 /* Look for the matching `]'. */
15442 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15444 end_loc
= token
->location
;
15449 case CPP_UTF8STRING
:
15450 case CPP_UTF8STRING_USERDEF
:
15457 case CPP_STRING_USERDEF
:
15458 case CPP_WSTRING_USERDEF
:
15459 case CPP_STRING16_USERDEF
:
15460 case CPP_STRING32_USERDEF
:
15466 if (cxx_dialect
== cxx98
)
15467 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15469 /* Consume the string. */
15470 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15471 /*wide_ok=*/true, /*lookup_udlit=*/false);
15472 if (str
== error_mark_node
)
15473 return error_mark_node
;
15474 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15476 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15477 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15478 end_loc
= str
.get_location ();
15483 /* Look for the suffix identifier. */
15484 token
= cp_lexer_peek_token (parser
->lexer
);
15485 if (token
->type
== CPP_NAME
)
15487 id
= cp_parser_identifier (parser
);
15488 end_loc
= token
->location
;
15490 else if (token
->type
== CPP_KEYWORD
)
15492 error ("unexpected keyword;"
15493 " remove space between quotes and suffix identifier");
15494 return error_mark_node
;
15498 error ("expected suffix identifier");
15499 return error_mark_node
;
15502 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15503 (TREE_TYPE (TREE_TYPE (string_tree
))));
15504 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15507 error ("expected empty string after %<operator%> keyword");
15508 return error_mark_node
;
15510 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15513 error ("invalid encoding prefix in literal operator");
15514 return error_mark_node
;
15516 if (id
!= error_mark_node
)
15518 const char *name
= IDENTIFIER_POINTER (id
);
15519 id
= cp_literal_operator_id (name
);
15521 /* Generate a location of the form:
15522 "" _suffix_identifier
15523 ^~~~~~~~~~~~~~~~~~~~~
15524 with caret == start at the start token, finish at the end of the
15525 suffix identifier. */
15526 location_t finish_loc
15527 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15528 location_t combined_loc
15529 = make_location (start_loc
, start_loc
, finish_loc
);
15530 return cp_expr (id
, combined_loc
);
15534 /* Anything else is an error. */
15538 /* If we have selected an identifier, we need to consume the
15540 if (op
!= ERROR_MARK
)
15542 id
= ovl_op_identifier (assop
, op
);
15544 cp_lexer_consume_token (parser
->lexer
);
15546 /* Otherwise, no valid operator name was present. */
15549 cp_parser_error (parser
, "expected operator");
15550 id
= error_mark_node
;
15553 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15554 return cp_expr (id
, start_loc
);
15557 /* Parse a template-declaration.
15559 template-declaration:
15560 export [opt] template < template-parameter-list > declaration
15562 If MEMBER_P is TRUE, this template-declaration occurs within a
15565 The grammar rule given by the standard isn't correct. What
15566 is really meant is:
15568 template-declaration:
15569 export [opt] template-parameter-list-seq
15570 decl-specifier-seq [opt] init-declarator [opt] ;
15571 export [opt] template-parameter-list-seq
15572 function-definition
15574 template-parameter-list-seq:
15575 template-parameter-list-seq [opt]
15576 template < template-parameter-list >
15578 Concept Extensions:
15580 template-parameter-list-seq:
15581 template < template-parameter-list > requires-clause [opt]
15584 requires logical-or-expression */
15587 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15589 /* Check for `export'. */
15590 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15592 /* Consume the `export' token. */
15593 cp_lexer_consume_token (parser
->lexer
);
15594 /* Warn that we do not support `export'. */
15595 warning (0, "keyword %<export%> not implemented, and will be ignored");
15598 cp_parser_template_declaration_after_export (parser
, member_p
);
15601 /* Parse a template-parameter-list.
15603 template-parameter-list:
15605 template-parameter-list , template-parameter
15607 Returns a TREE_LIST. Each node represents a template parameter.
15608 The nodes are connected via their TREE_CHAINs. */
15611 cp_parser_template_parameter_list (cp_parser
* parser
)
15613 tree parameter_list
= NULL_TREE
;
15615 /* Don't create wrapper nodes within a template-parameter-list,
15616 since we don't want to have different types based on the
15617 spelling location of constants and decls within them. */
15618 auto_suppress_location_wrappers sentinel
;
15620 begin_template_parm_list ();
15622 /* The loop below parses the template parms. We first need to know
15623 the total number of template parms to be able to compute proper
15624 canonical types of each dependent type. So after the loop, when
15625 we know the total number of template parms,
15626 end_template_parm_list computes the proper canonical types and
15627 fixes up the dependent types accordingly. */
15632 bool is_parameter_pack
;
15633 location_t parm_loc
;
15635 /* Parse the template-parameter. */
15636 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15637 parameter
= cp_parser_template_parameter (parser
,
15639 &is_parameter_pack
);
15640 /* Add it to the list. */
15641 if (parameter
!= error_mark_node
)
15642 parameter_list
= process_template_parm (parameter_list
,
15646 is_parameter_pack
);
15649 tree err_parm
= build_tree_list (parameter
, parameter
);
15650 parameter_list
= chainon (parameter_list
, err_parm
);
15653 /* If the next token is not a `,', we're done. */
15654 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15656 /* Otherwise, consume the `,' token. */
15657 cp_lexer_consume_token (parser
->lexer
);
15660 return end_template_parm_list (parameter_list
);
15663 /* Parse a introduction-list.
15666 introduced-parameter
15667 introduction-list , introduced-parameter
15669 introduced-parameter:
15670 ...[opt] identifier
15672 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15673 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15674 WILDCARD_DECL will also have DECL_NAME set and token location in
15675 DECL_SOURCE_LOCATION. */
15678 cp_parser_introduction_list (cp_parser
*parser
)
15680 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15684 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15686 cp_lexer_consume_token (parser
->lexer
);
15688 tree identifier
= cp_parser_identifier (parser
);
15689 if (identifier
== error_mark_node
)
15692 /* Build placeholder. */
15693 tree parm
= build_nt (WILDCARD_DECL
);
15694 DECL_SOURCE_LOCATION (parm
)
15695 = cp_lexer_peek_token (parser
->lexer
)->location
;
15696 DECL_NAME (parm
) = identifier
;
15697 WILDCARD_PACK_P (parm
) = is_pack
;
15698 vec_safe_push (introduction_vec
, parm
);
15700 /* If the next token is not a `,', we're done. */
15701 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15703 /* Otherwise, consume the `,' token. */
15704 cp_lexer_consume_token (parser
->lexer
);
15707 /* Convert the vec into a TREE_VEC. */
15708 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15711 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15712 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15714 release_tree_vector (introduction_vec
);
15715 return introduction_list
;
15718 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15719 is an abstract declarator. */
15721 static inline cp_declarator
*
15722 get_id_declarator (cp_declarator
*declarator
)
15724 cp_declarator
*d
= declarator
;
15725 while (d
&& d
->kind
!= cdk_id
)
15730 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15731 is an abstract declarator. */
15734 get_unqualified_id (cp_declarator
*declarator
)
15736 declarator
= get_id_declarator (declarator
);
15738 return declarator
->u
.id
.unqualified_name
;
15743 /* Returns true if DECL represents a constrained-parameter. */
15746 is_constrained_parameter (tree decl
)
15749 && TREE_CODE (decl
) == TYPE_DECL
15750 && CONSTRAINED_PARM_CONCEPT (decl
)
15751 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15754 /* Returns true if PARM declares a constrained-parameter. */
15757 is_constrained_parameter (cp_parameter_declarator
*parm
)
15759 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15762 /* Check that the type parameter is only a declarator-id, and that its
15763 type is not cv-qualified. */
15766 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15767 cp_parameter_declarator
*parm
)
15769 if (!parm
->declarator
)
15772 if (parm
->declarator
->kind
!= cdk_id
)
15774 cp_parser_error (parser
, "invalid constrained type parameter");
15778 /* Don't allow cv-qualified type parameters. */
15779 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15780 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15782 cp_parser_error (parser
, "cv-qualified type parameter");
15789 /* Finish parsing/processing a template type parameter and checking
15790 various restrictions. */
15793 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15795 cp_parameter_declarator
* parmdecl
)
15797 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15798 return finish_template_type_parm (class_type_node
, id
);
15800 return error_mark_node
;
15804 finish_constrained_template_template_parm (tree proto
, tree id
)
15806 /* FIXME: This should probably be copied, and we may need to adjust
15807 the template parameter depths. */
15808 tree saved_parms
= current_template_parms
;
15809 begin_template_parm_list ();
15810 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15811 end_template_parm_list ();
15813 tree parm
= finish_template_template_parm (class_type_node
, id
);
15814 current_template_parms
= saved_parms
;
15819 /* Finish parsing/processing a template template parameter by borrowing
15820 the template parameter list from the prototype parameter. */
15823 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15826 cp_parameter_declarator
*parmdecl
)
15828 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15829 return error_mark_node
;
15830 return finish_constrained_template_template_parm (proto
, id
);
15833 /* Create a new non-type template parameter from the given PARM
15837 constrained_non_type_template_parm (bool *is_non_type
,
15838 cp_parameter_declarator
*parm
)
15840 *is_non_type
= true;
15841 cp_declarator
*decl
= parm
->declarator
;
15842 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15843 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15844 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15847 /* Build a constrained template parameter based on the PARMDECL
15848 declarator. The type of PARMDECL is the constrained type, which
15849 refers to the prototype template parameter that ultimately
15850 specifies the type of the declared parameter. */
15853 finish_constrained_parameter (cp_parser
*parser
,
15854 cp_parameter_declarator
*parmdecl
,
15856 bool *is_parameter_pack
)
15858 tree decl
= parmdecl
->decl_specifiers
.type
;
15859 tree id
= get_unqualified_id (parmdecl
->declarator
);
15860 tree def
= parmdecl
->default_argument
;
15861 tree proto
= DECL_INITIAL (decl
);
15863 /* A template parameter constrained by a variadic concept shall also
15864 be declared as a template parameter pack. */
15865 bool is_variadic
= template_parameter_pack_p (proto
);
15866 if (is_variadic
&& !*is_parameter_pack
)
15867 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15869 /* Build the parameter. Return an error if the declarator was invalid. */
15871 if (TREE_CODE (proto
) == TYPE_DECL
)
15872 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15873 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15874 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15877 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15878 if (parm
== error_mark_node
)
15879 return error_mark_node
;
15881 /* Finish the parameter decl and create a node attaching the
15882 default argument and constraint. */
15883 parm
= build_tree_list (def
, parm
);
15884 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15889 /* Returns true if the parsed type actually represents the declaration
15890 of a type template-parameter. */
15893 declares_constrained_type_template_parameter (tree type
)
15895 return (is_constrained_parameter (type
)
15896 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15900 /* Returns true if the parsed type actually represents the declaration of
15901 a template template-parameter. */
15904 declares_constrained_template_template_parameter (tree type
)
15906 return (is_constrained_parameter (type
)
15907 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15910 /* Parse a default argument for a type template-parameter.
15911 Note that diagnostics are handled in cp_parser_template_parameter. */
15914 cp_parser_default_type_template_argument (cp_parser
*parser
)
15916 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15918 /* Consume the `=' token. */
15919 cp_lexer_consume_token (parser
->lexer
);
15921 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15923 /* Parse the default-argument. */
15924 push_deferring_access_checks (dk_no_deferred
);
15925 tree default_argument
= cp_parser_type_id (parser
,
15926 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15928 pop_deferring_access_checks ();
15930 if (flag_concepts
&& type_uses_auto (default_argument
))
15932 error_at (token
->location
,
15933 "invalid use of %<auto%> in default template argument");
15934 return error_mark_node
;
15937 return default_argument
;
15940 /* Parse a default argument for a template template-parameter. */
15943 cp_parser_default_template_template_argument (cp_parser
*parser
)
15945 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15949 /* Consume the `='. */
15950 cp_lexer_consume_token (parser
->lexer
);
15951 /* Parse the id-expression. */
15952 push_deferring_access_checks (dk_no_deferred
);
15953 /* save token before parsing the id-expression, for error
15955 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15956 tree default_argument
15957 = cp_parser_id_expression (parser
,
15958 /*template_keyword_p=*/false,
15959 /*check_dependency_p=*/true,
15960 /*template_p=*/&is_template
,
15961 /*declarator_p=*/false,
15962 /*optional_p=*/false);
15963 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15964 /* If the id-expression was a template-id that refers to
15965 a template-class, we already have the declaration here,
15966 so no further lookup is needed. */
15969 /* Look up the name. */
15971 = cp_parser_lookup_name (parser
, default_argument
,
15973 /*is_template=*/is_template
,
15974 /*is_namespace=*/false,
15975 /*check_dependency=*/true,
15976 /*ambiguous_decls=*/NULL
,
15978 /* See if the default argument is valid. */
15979 default_argument
= check_template_template_default_arg (default_argument
);
15980 pop_deferring_access_checks ();
15981 return default_argument
;
15984 /* Parse a template-parameter.
15986 template-parameter:
15988 parameter-declaration
15990 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15991 the parameter. The TREE_PURPOSE is the default value, if any.
15992 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15993 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15994 set to true iff this parameter is a parameter pack. */
15997 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15998 bool *is_parameter_pack
)
16001 cp_parameter_declarator
*parameter_declarator
;
16004 /* Assume it is a type parameter or a template parameter. */
16005 *is_non_type
= false;
16006 /* Assume it not a parameter pack. */
16007 *is_parameter_pack
= false;
16008 /* Peek at the next token. */
16009 token
= cp_lexer_peek_token (parser
->lexer
);
16010 /* If it is `template', we have a type-parameter. */
16011 if (token
->keyword
== RID_TEMPLATE
)
16012 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16013 /* If it is `class' or `typename' we do not know yet whether it is a
16014 type parameter or a non-type parameter. Consider:
16016 template <typename T, typename T::X X> ...
16020 template <class C, class D*> ...
16022 Here, the first parameter is a type parameter, and the second is
16023 a non-type parameter. We can tell by looking at the token after
16024 the identifier -- if it is a `,', `=', or `>' then we have a type
16026 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
16028 /* Peek at the token after `class' or `typename'. */
16029 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16030 /* If it's an ellipsis, we have a template type parameter
16032 if (token
->type
== CPP_ELLIPSIS
)
16033 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16034 /* If it's an identifier, skip it. */
16035 if (token
->type
== CPP_NAME
)
16036 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
16037 /* Now, see if the token looks like the end of a template
16039 if (token
->type
== CPP_COMMA
16040 || token
->type
== CPP_EQ
16041 || token
->type
== CPP_GREATER
)
16042 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16045 /* Otherwise, it is a non-type parameter or a constrained parameter.
16049 When parsing a default template-argument for a non-type
16050 template-parameter, the first non-nested `>' is taken as the end
16051 of the template parameter-list rather than a greater-than
16053 parameter_declarator
16054 = cp_parser_parameter_declaration (parser
,
16055 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16056 /*template_parm_p=*/true,
16057 /*parenthesized_p=*/NULL
);
16059 if (!parameter_declarator
)
16060 return error_mark_node
;
16062 /* If the parameter declaration is marked as a parameter pack, set
16063 *IS_PARAMETER_PACK to notify the caller. */
16064 if (parameter_declarator
->template_parameter_pack_p
)
16065 *is_parameter_pack
= true;
16067 if (parameter_declarator
->default_argument
)
16069 /* Can happen in some cases of erroneous input (c++/34892). */
16070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16071 /* Consume the `...' for better error recovery. */
16072 cp_lexer_consume_token (parser
->lexer
);
16075 // The parameter may have been constrained.
16076 if (is_constrained_parameter (parameter_declarator
))
16077 return finish_constrained_parameter (parser
,
16078 parameter_declarator
,
16080 is_parameter_pack
);
16082 // Now we're sure that the parameter is a non-type parameter.
16083 *is_non_type
= true;
16085 parm
= grokdeclarator (parameter_declarator
->declarator
,
16086 ¶meter_declarator
->decl_specifiers
,
16087 TPARM
, /*initialized=*/0,
16088 /*attrlist=*/NULL
);
16089 if (parm
== error_mark_node
)
16090 return error_mark_node
;
16092 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16095 /* Parse a type-parameter.
16098 class identifier [opt]
16099 class identifier [opt] = type-id
16100 typename identifier [opt]
16101 typename identifier [opt] = type-id
16102 template < template-parameter-list > class identifier [opt]
16103 template < template-parameter-list > class identifier [opt]
16106 GNU Extension (variadic templates):
16109 class ... identifier [opt]
16110 typename ... identifier [opt]
16112 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16113 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16114 the declaration of the parameter.
16116 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16119 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16124 /* Look for a keyword to tell us what kind of parameter this is. */
16125 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16127 return error_mark_node
;
16129 switch (token
->keyword
)
16135 tree default_argument
;
16137 /* If the next token is an ellipsis, we have a template
16139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16141 /* Consume the `...' token. */
16142 cp_lexer_consume_token (parser
->lexer
);
16143 maybe_warn_variadic_templates ();
16145 *is_parameter_pack
= true;
16148 /* If the next token is an identifier, then it names the
16150 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16151 identifier
= cp_parser_identifier (parser
);
16153 identifier
= NULL_TREE
;
16155 /* Create the parameter. */
16156 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16158 /* If the next token is an `=', we have a default argument. */
16159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16162 = cp_parser_default_type_template_argument (parser
);
16164 /* Template parameter packs cannot have default
16166 if (*is_parameter_pack
)
16169 error_at (token
->location
,
16170 "template parameter pack %qD cannot have a "
16171 "default argument", identifier
);
16173 error_at (token
->location
,
16174 "template parameter packs cannot have "
16175 "default arguments");
16176 default_argument
= NULL_TREE
;
16178 else if (check_for_bare_parameter_packs (default_argument
))
16179 default_argument
= error_mark_node
;
16182 default_argument
= NULL_TREE
;
16184 /* Create the combined representation of the parameter and the
16185 default argument. */
16186 parameter
= build_tree_list (default_argument
, parameter
);
16193 tree default_argument
;
16195 /* Look for the `<'. */
16196 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16197 /* Parse the template-parameter-list. */
16198 cp_parser_template_parameter_list (parser
);
16199 /* Look for the `>'. */
16200 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16202 // If template requirements are present, parse them.
16205 tree reqs
= get_shorthand_constraints (current_template_parms
);
16206 if (tree r
= cp_parser_requires_clause_opt (parser
))
16207 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16208 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16211 /* Look for the `class' or 'typename' keywords. */
16212 cp_parser_type_parameter_key (parser
);
16213 /* If the next token is an ellipsis, we have a template
16215 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16217 /* Consume the `...' token. */
16218 cp_lexer_consume_token (parser
->lexer
);
16219 maybe_warn_variadic_templates ();
16221 *is_parameter_pack
= true;
16223 /* If the next token is an `=', then there is a
16224 default-argument. If the next token is a `>', we are at
16225 the end of the parameter-list. If the next token is a `,',
16226 then we are at the end of this parameter. */
16227 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16228 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16229 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16231 identifier
= cp_parser_identifier (parser
);
16232 /* Treat invalid names as if the parameter were nameless. */
16233 if (identifier
== error_mark_node
)
16234 identifier
= NULL_TREE
;
16237 identifier
= NULL_TREE
;
16239 /* Create the template parameter. */
16240 parameter
= finish_template_template_parm (class_type_node
,
16243 /* If the next token is an `=', then there is a
16244 default-argument. */
16245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16248 = cp_parser_default_template_template_argument (parser
);
16250 /* Template parameter packs cannot have default
16252 if (*is_parameter_pack
)
16255 error_at (token
->location
,
16256 "template parameter pack %qD cannot "
16257 "have a default argument",
16260 error_at (token
->location
, "template parameter packs cannot "
16261 "have default arguments");
16262 default_argument
= NULL_TREE
;
16266 default_argument
= NULL_TREE
;
16268 /* Create the combined representation of the parameter and the
16269 default argument. */
16270 parameter
= build_tree_list (default_argument
, parameter
);
16275 gcc_unreachable ();
16282 /* Parse a template-id.
16285 template-name < template-argument-list [opt] >
16287 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16288 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16289 returned. Otherwise, if the template-name names a function, or set
16290 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16291 names a class, returns a TYPE_DECL for the specialization.
16293 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16294 uninstantiated templates. */
16297 cp_parser_template_id (cp_parser
*parser
,
16298 bool template_keyword_p
,
16299 bool check_dependency_p
,
16300 enum tag_types tag_type
,
16301 bool is_declaration
)
16306 cp_token_position start_of_id
= 0;
16307 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16308 bool is_identifier
;
16310 /* If the next token corresponds to a template-id, there is no need
16312 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16313 if (token
->type
== CPP_TEMPLATE_ID
)
16315 cp_lexer_consume_token (parser
->lexer
);
16316 return saved_checks_value (token
->u
.tree_check_value
);
16319 /* Avoid performing name lookup if there is no possibility of
16320 finding a template-id. */
16321 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16322 || (token
->type
== CPP_NAME
16323 && !cp_parser_nth_token_starts_template_argument_list_p
16326 cp_parser_error (parser
, "expected template-id");
16327 return error_mark_node
;
16330 /* Remember where the template-id starts. */
16331 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16332 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16334 push_deferring_access_checks (dk_deferred
);
16336 /* Parse the template-name. */
16337 is_identifier
= false;
16338 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16339 check_dependency_p
,
16344 /* Push any access checks inside the firewall we're about to create. */
16345 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16346 pop_deferring_access_checks ();
16347 if (templ
== error_mark_node
|| is_identifier
)
16350 /* Since we're going to preserve any side-effects from this parse, set up a
16351 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16352 in the template arguments. */
16353 tentative_firewall
firewall (parser
);
16354 reopen_deferring_access_checks (checks
);
16356 /* If we find the sequence `[:' after a template-name, it's probably
16357 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16358 parse correctly the argument list. */
16359 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16360 == CPP_OPEN_SQUARE
)
16361 && next_token
->flags
& DIGRAPH
16362 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16364 && !(next_token_2
->flags
& PREV_WHITE
))
16366 cp_parser_parse_tentatively (parser
);
16367 /* Change `:' into `::'. */
16368 next_token_2
->type
= CPP_SCOPE
;
16369 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16371 cp_lexer_consume_token (parser
->lexer
);
16373 /* Parse the arguments. */
16374 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16375 if (!cp_parser_parse_definitely (parser
))
16377 /* If we couldn't parse an argument list, then we revert our changes
16378 and return simply an error. Maybe this is not a template-id
16380 next_token_2
->type
= CPP_COLON
;
16381 cp_parser_error (parser
, "expected %<<%>");
16382 pop_deferring_access_checks ();
16383 return error_mark_node
;
16385 /* Otherwise, emit an error about the invalid digraph, but continue
16386 parsing because we got our argument list. */
16387 if (permerror (next_token
->location
,
16388 "%<<::%> cannot begin a template-argument list"))
16390 static bool hint
= false;
16391 inform (next_token
->location
,
16392 "%<<:%> is an alternate spelling for %<[%>."
16393 " Insert whitespace between %<<%> and %<::%>");
16394 if (!hint
&& !flag_permissive
)
16396 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16397 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16398 "accept your code)");
16405 /* Look for the `<' that starts the template-argument-list. */
16406 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16408 pop_deferring_access_checks ();
16409 return error_mark_node
;
16411 /* Parse the arguments. */
16412 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16414 if ((cxx_dialect
> cxx17
)
16415 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16416 && !template_keyword_p
16417 && (cp_parser_error_occurred (parser
)
16418 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16420 /* This didn't go well. */
16421 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16423 /* C++2A says that "function-name < a;" is now ill-formed. */
16424 if (cp_parser_error_occurred (parser
))
16426 error_at (token
->location
, "invalid template-argument-list");
16427 inform (token
->location
, "function name as the left hand "
16428 "operand of %<<%> is ill-formed in C++2a; wrap the "
16429 "function name in %<()%>");
16432 /* We expect "f<targs>" to be followed by "(args)". */
16433 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16434 "expected %<(%> after template-argument-list");
16436 /* Purge all subsequent tokens. */
16437 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16440 cp_parser_simulate_error (parser
);
16441 pop_deferring_access_checks ();
16442 return error_mark_node
;
16446 /* Set the location to be of the form:
16447 template-name < template-argument-list [opt] >
16448 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16449 with caret == start at the start of the template-name,
16450 ranging until the closing '>'. */
16451 location_t finish_loc
16452 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16453 location_t combined_loc
16454 = make_location (token
->location
, token
->location
, finish_loc
);
16456 /* Check for concepts autos where they don't belong. We could
16457 identify types in some cases of idnetifier TEMPL, looking ahead
16458 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16459 types. We reject them in functions, but if what we have is an
16460 identifier, even with none_type we can't conclude it's NOT a
16461 type, we have to wait for template substitution. */
16462 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16463 template_id
= error_mark_node
;
16464 /* Build a representation of the specialization. */
16465 else if (identifier_p (templ
))
16466 template_id
= build_min_nt_loc (combined_loc
,
16469 else if (DECL_TYPE_TEMPLATE_P (templ
)
16470 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16472 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16473 template (rather than some instantiation thereof) only if
16474 is not nested within some other construct. For example, in
16475 "template <typename T> void f(T) { A<T>::", A<T> is just an
16476 instantiation of A. */
16477 bool entering_scope
16478 = (template_parm_scope_p ()
16479 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16481 = finish_template_type (templ
, arguments
, entering_scope
);
16483 /* A template-like identifier may be a partial concept id. */
16484 else if (flag_concepts
16485 && (template_id
= (cp_parser_maybe_partial_concept_id
16486 (parser
, templ
, arguments
))))
16487 return template_id
;
16488 else if (variable_template_p (templ
))
16490 template_id
= lookup_template_variable (templ
, arguments
);
16491 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16492 SET_EXPR_LOCATION (template_id
, combined_loc
);
16496 /* If it's not a class-template or a template-template, it should be
16497 a function-template. */
16498 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16499 || TREE_CODE (templ
) == OVERLOAD
16500 || TREE_CODE (templ
) == FUNCTION_DECL
16501 || BASELINK_P (templ
)));
16503 template_id
= lookup_template_function (templ
, arguments
);
16504 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16505 SET_EXPR_LOCATION (template_id
, combined_loc
);
16508 /* If parsing tentatively, replace the sequence of tokens that makes
16509 up the template-id with a CPP_TEMPLATE_ID token. That way,
16510 should we re-parse the token stream, we will not have to repeat
16511 the effort required to do the parse, nor will we issue duplicate
16512 error messages about problems during instantiation of the
16515 /* Don't do this if we had a parse error in a declarator; re-parsing
16516 might succeed if a name changes meaning (60361). */
16517 && !(cp_parser_error_occurred (parser
)
16518 && cp_parser_parsing_tentatively (parser
)
16519 && parser
->in_declarator_p
))
16521 /* Reset the contents of the START_OF_ID token. */
16522 token
->type
= CPP_TEMPLATE_ID
;
16523 token
->location
= combined_loc
;
16525 /* Retrieve any deferred checks. Do not pop this access checks yet
16526 so the memory will not be reclaimed during token replacing below. */
16527 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16528 token
->u
.tree_check_value
->value
= template_id
;
16529 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16530 token
->keyword
= RID_MAX
;
16532 /* Purge all subsequent tokens. */
16533 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16535 /* ??? Can we actually assume that, if template_id ==
16536 error_mark_node, we will have issued a diagnostic to the
16537 user, as opposed to simply marking the tentative parse as
16539 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16540 error_at (token
->location
, "parse error in template argument list");
16543 pop_to_parent_deferring_access_checks ();
16544 return template_id
;
16547 /* Parse a template-name.
16552 The standard should actually say:
16556 operator-function-id
16558 A defect report has been filed about this issue.
16560 A conversion-function-id cannot be a template name because they cannot
16561 be part of a template-id. In fact, looking at this code:
16563 a.operator K<int>()
16565 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16566 It is impossible to call a templated conversion-function-id with an
16567 explicit argument list, since the only allowed template parameter is
16568 the type to which it is converting.
16570 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16571 `template' keyword, in a construction like:
16575 In that case `f' is taken to be a template-name, even though there
16576 is no way of knowing for sure.
16578 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16579 name refers to a set of overloaded functions, at least one of which
16580 is a template, or an IDENTIFIER_NODE with the name of the template,
16581 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16582 names are looked up inside uninstantiated templates. */
16585 cp_parser_template_name (cp_parser
* parser
,
16586 bool template_keyword_p
,
16587 bool check_dependency_p
,
16588 bool is_declaration
,
16589 enum tag_types tag_type
,
16590 bool *is_identifier
)
16594 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16596 /* If the next token is `operator', then we have either an
16597 operator-function-id or a conversion-function-id. */
16598 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16600 /* We don't know whether we're looking at an
16601 operator-function-id or a conversion-function-id. */
16602 cp_parser_parse_tentatively (parser
);
16603 /* Try an operator-function-id. */
16604 identifier
= cp_parser_operator_function_id (parser
);
16605 /* If that didn't work, try a conversion-function-id. */
16606 if (!cp_parser_parse_definitely (parser
))
16608 cp_parser_error (parser
, "expected template-name");
16609 return error_mark_node
;
16612 /* Look for the identifier. */
16614 identifier
= cp_parser_identifier (parser
);
16616 /* If we didn't find an identifier, we don't have a template-id. */
16617 if (identifier
== error_mark_node
)
16618 return error_mark_node
;
16620 /* If the name immediately followed the `template' keyword, then it
16621 is a template-name. However, if the next token is not `<', then
16622 we do not treat it as a template-name, since it is not being used
16623 as part of a template-id. This enables us to handle constructs
16626 template <typename T> struct S { S(); };
16627 template <typename T> S<T>::S();
16629 correctly. We would treat `S' as a template -- if it were `S<T>'
16630 -- but we do not if there is no `<'. */
16632 if (processing_template_decl
16633 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16635 /* In a declaration, in a dependent context, we pretend that the
16636 "template" keyword was present in order to improve error
16637 recovery. For example, given:
16639 template <typename T> void f(T::X<int>);
16641 we want to treat "X<int>" as a template-id. */
16643 && !template_keyword_p
16644 && parser
->scope
&& TYPE_P (parser
->scope
)
16645 && check_dependency_p
16646 && dependent_scope_p (parser
->scope
)
16647 /* Do not do this for dtors (or ctors), since they never
16648 need the template keyword before their name. */
16649 && !constructor_name_p (identifier
, parser
->scope
))
16651 cp_token_position start
= 0;
16653 /* Explain what went wrong. */
16654 error_at (token
->location
, "non-template %qD used as template",
16656 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16657 parser
->scope
, identifier
);
16658 /* If parsing tentatively, find the location of the "<" token. */
16659 if (cp_parser_simulate_error (parser
))
16660 start
= cp_lexer_token_position (parser
->lexer
, true);
16661 /* Parse the template arguments so that we can issue error
16662 messages about them. */
16663 cp_lexer_consume_token (parser
->lexer
);
16664 cp_parser_enclosed_template_argument_list (parser
);
16665 /* Skip tokens until we find a good place from which to
16666 continue parsing. */
16667 cp_parser_skip_to_closing_parenthesis (parser
,
16668 /*recovering=*/true,
16670 /*consume_paren=*/false);
16671 /* If parsing tentatively, permanently remove the
16672 template argument list. That will prevent duplicate
16673 error messages from being issued about the missing
16674 "template" keyword. */
16676 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16678 *is_identifier
= true;
16679 parser
->context
->object_type
= NULL_TREE
;
16683 /* If the "template" keyword is present, then there is generally
16684 no point in doing name-lookup, so we just return IDENTIFIER.
16685 But, if the qualifying scope is non-dependent then we can
16686 (and must) do name-lookup normally. */
16687 if (template_keyword_p
)
16689 tree scope
= (parser
->scope
? parser
->scope
16690 : parser
->context
->object_type
);
16691 if (scope
&& TYPE_P (scope
)
16692 && (!CLASS_TYPE_P (scope
)
16693 || (check_dependency_p
&& dependent_type_p (scope
))))
16695 /* We're optimizing away the call to cp_parser_lookup_name, but
16696 we still need to do this. */
16697 parser
->context
->object_type
= NULL_TREE
;
16703 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16704 const bool scoped_p
= ((parser
->scope
? parser
->scope
16705 : parser
->context
->object_type
) != NULL_TREE
);
16707 /* Look up the name. */
16708 decl
= cp_parser_lookup_name (parser
, identifier
,
16710 /*is_template=*/true,
16711 /*is_namespace=*/false,
16712 check_dependency_p
,
16713 /*ambiguous_decls=*/NULL
,
16716 decl
= strip_using_decl (decl
);
16718 /* If DECL is a template, then the name was a template-name. */
16719 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16721 if (TREE_DEPRECATED (decl
)
16722 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16723 warn_deprecated_use (decl
, NULL_TREE
);
16727 /* The standard does not explicitly indicate whether a name that
16728 names a set of overloaded declarations, some of which are
16729 templates, is a template-name. However, such a name should
16730 be a template-name; otherwise, there is no way to form a
16731 template-id for the overloaded templates. */
16732 bool found
= false;
16734 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16735 !found
&& iter
; ++iter
)
16736 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16740 && (cxx_dialect
> cxx17
)
16742 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
16743 && tag_type
== none_type
)
16745 /* [temp.names] says "A name is also considered to refer to a template
16746 if it is an unqualified-id followed by a < and name lookup finds
16747 either one or more functions or finds nothing." */
16749 /* The "more functions" case. Just use the OVERLOAD as normally.
16750 We don't use is_overloaded_fn here to avoid considering
16752 if (TREE_CODE (decl
) == OVERLOAD
16753 /* Name lookup found one function. */
16754 || TREE_CODE (decl
) == FUNCTION_DECL
)
16756 /* Name lookup found nothing. */
16757 else if (decl
== error_mark_node
)
16763 /* The name does not name a template. */
16764 cp_parser_error (parser
, "expected template-name");
16765 return error_mark_node
;
16772 /* Parse a template-argument-list.
16774 template-argument-list:
16775 template-argument ... [opt]
16776 template-argument-list , template-argument ... [opt]
16778 Returns a TREE_VEC containing the arguments. */
16781 cp_parser_template_argument_list (cp_parser
* parser
)
16783 tree fixed_args
[10];
16784 unsigned n_args
= 0;
16785 unsigned alloced
= 10;
16786 tree
*arg_ary
= fixed_args
;
16788 bool saved_in_template_argument_list_p
;
16790 bool saved_non_ice_p
;
16792 /* Don't create location wrapper nodes within a template-argument-list. */
16793 auto_suppress_location_wrappers sentinel
;
16795 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16796 parser
->in_template_argument_list_p
= true;
16797 /* Even if the template-id appears in an integral
16798 constant-expression, the contents of the argument list do
16800 saved_ice_p
= parser
->integral_constant_expression_p
;
16801 parser
->integral_constant_expression_p
= false;
16802 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16803 parser
->non_integral_constant_expression_p
= false;
16805 /* Parse the arguments. */
16811 /* Consume the comma. */
16812 cp_lexer_consume_token (parser
->lexer
);
16814 /* Parse the template-argument. */
16815 argument
= cp_parser_template_argument (parser
);
16817 /* If the next token is an ellipsis, we're expanding a template
16819 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16821 if (argument
== error_mark_node
)
16823 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16824 error_at (token
->location
,
16825 "expected parameter pack before %<...%>");
16827 /* Consume the `...' token. */
16828 cp_lexer_consume_token (parser
->lexer
);
16830 /* Make the argument into a TYPE_PACK_EXPANSION or
16831 EXPR_PACK_EXPANSION. */
16832 argument
= make_pack_expansion (argument
);
16835 if (n_args
== alloced
)
16839 if (arg_ary
== fixed_args
)
16841 arg_ary
= XNEWVEC (tree
, alloced
);
16842 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16845 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16847 arg_ary
[n_args
++] = argument
;
16849 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16851 vec
= make_tree_vec (n_args
);
16854 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16856 if (arg_ary
!= fixed_args
)
16858 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16859 parser
->integral_constant_expression_p
= saved_ice_p
;
16860 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16862 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16866 /* Parse a template-argument.
16869 assignment-expression
16873 The representation is that of an assignment-expression, type-id, or
16874 id-expression -- except that the qualified id-expression is
16875 evaluated, so that the value returned is either a DECL or an
16878 Although the standard says "assignment-expression", it forbids
16879 throw-expressions or assignments in the template argument.
16880 Therefore, we use "conditional-expression" instead. */
16883 cp_parser_template_argument (cp_parser
* parser
)
16888 bool maybe_type_id
= false;
16889 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16890 location_t loc
= 0;
16893 /* There's really no way to know what we're looking at, so we just
16894 try each alternative in order.
16898 In a template-argument, an ambiguity between a type-id and an
16899 expression is resolved to a type-id, regardless of the form of
16900 the corresponding template-parameter.
16902 Therefore, we try a type-id first. */
16903 cp_parser_parse_tentatively (parser
);
16904 argument
= cp_parser_template_type_arg (parser
);
16905 /* If there was no error parsing the type-id but the next token is a
16906 '>>', our behavior depends on which dialect of C++ we're
16907 parsing. In C++98, we probably found a typo for '> >'. But there
16908 are type-id which are also valid expressions. For instance:
16910 struct X { int operator >> (int); };
16911 template <int V> struct Foo {};
16914 Here 'X()' is a valid type-id of a function type, but the user just
16915 wanted to write the expression "X() >> 5". Thus, we remember that we
16916 found a valid type-id, but we still try to parse the argument as an
16917 expression to see what happens.
16919 In C++0x, the '>>' will be considered two separate '>'
16921 if (!cp_parser_error_occurred (parser
)
16922 && cxx_dialect
== cxx98
16923 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16925 maybe_type_id
= true;
16926 cp_parser_abort_tentative_parse (parser
);
16930 /* If the next token isn't a `,' or a `>', then this argument wasn't
16931 really finished. This means that the argument is not a valid
16933 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16934 cp_parser_error (parser
, "expected template-argument");
16935 /* If that worked, we're done. */
16936 if (cp_parser_parse_definitely (parser
))
16939 /* We're still not sure what the argument will be. */
16940 cp_parser_parse_tentatively (parser
);
16941 /* Try a template. */
16942 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16943 argument
= cp_parser_id_expression (parser
,
16944 /*template_keyword_p=*/false,
16945 /*check_dependency_p=*/true,
16947 /*declarator_p=*/false,
16948 /*optional_p=*/false);
16949 /* If the next token isn't a `,' or a `>', then this argument wasn't
16950 really finished. */
16951 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16952 cp_parser_error (parser
, "expected template-argument");
16953 if (!cp_parser_error_occurred (parser
))
16955 /* Figure out what is being referred to. If the id-expression
16956 was for a class template specialization, then we will have a
16957 TYPE_DECL at this point. There is no need to do name lookup
16958 at this point in that case. */
16959 if (TREE_CODE (argument
) != TYPE_DECL
)
16960 argument
= cp_parser_lookup_name (parser
, argument
,
16962 /*is_template=*/template_p
,
16963 /*is_namespace=*/false,
16964 /*check_dependency=*/true,
16965 /*ambiguous_decls=*/NULL
,
16966 argument_start_token
->location
);
16967 /* Handle a constrained-type-specifier for a non-type template
16969 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16971 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16972 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16973 cp_parser_error (parser
, "expected template-name");
16975 if (cp_parser_parse_definitely (parser
))
16977 if (TREE_DEPRECATED (argument
))
16978 warn_deprecated_use (argument
, NULL_TREE
);
16981 /* It must be a non-type argument. In C++17 any constant-expression is
16983 if (cxx_dialect
> cxx14
)
16986 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16988 -- an integral constant-expression of integral or enumeration
16991 -- the name of a non-type template-parameter; or
16993 -- the name of an object or function with external linkage...
16995 -- the address of an object or function with external linkage...
16997 -- a pointer to member... */
16998 /* Look for a non-type template parameter. */
16999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17001 cp_parser_parse_tentatively (parser
);
17002 argument
= cp_parser_primary_expression (parser
,
17003 /*address_p=*/false,
17005 /*template_arg_p=*/true,
17007 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
17008 || !cp_parser_next_token_ends_template_argument_p (parser
))
17009 cp_parser_simulate_error (parser
);
17010 if (cp_parser_parse_definitely (parser
))
17014 /* If the next token is "&", the argument must be the address of an
17015 object or function with external linkage. */
17016 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
17019 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17020 cp_lexer_consume_token (parser
->lexer
);
17022 /* See if we might have an id-expression. */
17023 token
= cp_lexer_peek_token (parser
->lexer
);
17024 if (token
->type
== CPP_NAME
17025 || token
->keyword
== RID_OPERATOR
17026 || token
->type
== CPP_SCOPE
17027 || token
->type
== CPP_TEMPLATE_ID
17028 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
17030 cp_parser_parse_tentatively (parser
);
17031 argument
= cp_parser_primary_expression (parser
,
17034 /*template_arg_p=*/true,
17036 if (cp_parser_error_occurred (parser
)
17037 || !cp_parser_next_token_ends_template_argument_p (parser
))
17038 cp_parser_abort_tentative_parse (parser
);
17043 if (INDIRECT_REF_P (argument
))
17045 /* Strip the dereference temporarily. */
17046 gcc_assert (REFERENCE_REF_P (argument
));
17047 argument
= TREE_OPERAND (argument
, 0);
17050 /* If we're in a template, we represent a qualified-id referring
17051 to a static data member as a SCOPE_REF even if the scope isn't
17052 dependent so that we can check access control later. */
17054 if (TREE_CODE (probe
) == SCOPE_REF
)
17055 probe
= TREE_OPERAND (probe
, 1);
17058 /* A variable without external linkage might still be a
17059 valid constant-expression, so no error is issued here
17060 if the external-linkage check fails. */
17061 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
17062 cp_parser_simulate_error (parser
);
17064 else if (is_overloaded_fn (argument
))
17065 /* All overloaded functions are allowed; if the external
17066 linkage test does not pass, an error will be issued
17070 && (TREE_CODE (argument
) == OFFSET_REF
17071 || TREE_CODE (argument
) == SCOPE_REF
))
17072 /* A pointer-to-member. */
17074 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
17077 cp_parser_simulate_error (parser
);
17079 if (cp_parser_parse_definitely (parser
))
17082 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17083 tf_warning_or_error
);
17085 argument
= convert_from_reference (argument
);
17090 /* If the argument started with "&", there are no other valid
17091 alternatives at this point. */
17094 cp_parser_error (parser
, "invalid non-type template argument");
17095 return error_mark_node
;
17099 /* If the argument wasn't successfully parsed as a type-id followed
17100 by '>>', the argument can only be a constant expression now.
17101 Otherwise, we try parsing the constant-expression tentatively,
17102 because the argument could really be a type-id. */
17104 cp_parser_parse_tentatively (parser
);
17106 if (cxx_dialect
<= cxx14
)
17107 argument
= cp_parser_constant_expression (parser
);
17110 /* In C++20, we can encounter a braced-init-list. */
17111 if (cxx_dialect
>= cxx2a
17112 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17114 bool expr_non_constant_p
;
17115 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17118 /* With C++17 generalized non-type template arguments we need to handle
17119 lvalue constant expressions, too. */
17120 argument
= cp_parser_assignment_expression (parser
);
17121 require_potential_constant_expression (argument
);
17124 if (!maybe_type_id
)
17126 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17127 cp_parser_error (parser
, "expected template-argument");
17128 if (cp_parser_parse_definitely (parser
))
17130 /* We did our best to parse the argument as a non type-id, but that
17131 was the only alternative that matched (albeit with a '>' after
17132 it). We can assume it's just a typo from the user, and a
17133 diagnostic will then be issued. */
17134 return cp_parser_template_type_arg (parser
);
17137 /* Parse an explicit-instantiation.
17139 explicit-instantiation:
17140 template declaration
17142 Although the standard says `declaration', what it really means is:
17144 explicit-instantiation:
17145 template decl-specifier-seq [opt] declarator [opt] ;
17147 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17148 supposed to be allowed. A defect report has been filed about this
17153 explicit-instantiation:
17154 storage-class-specifier template
17155 decl-specifier-seq [opt] declarator [opt] ;
17156 function-specifier template
17157 decl-specifier-seq [opt] declarator [opt] ; */
17160 cp_parser_explicit_instantiation (cp_parser
* parser
)
17162 int declares_class_or_enum
;
17163 cp_decl_specifier_seq decl_specifiers
;
17164 tree extension_specifier
= NULL_TREE
;
17166 timevar_push (TV_TEMPLATE_INST
);
17168 /* Look for an (optional) storage-class-specifier or
17169 function-specifier. */
17170 if (cp_parser_allow_gnu_extensions_p (parser
))
17172 extension_specifier
17173 = cp_parser_storage_class_specifier_opt (parser
);
17174 if (!extension_specifier
)
17175 extension_specifier
17176 = cp_parser_function_specifier_opt (parser
,
17177 /*decl_specs=*/NULL
);
17180 /* Look for the `template' keyword. */
17181 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17182 /* Let the front end know that we are processing an explicit
17184 begin_explicit_instantiation ();
17185 /* [temp.explicit] says that we are supposed to ignore access
17186 control while processing explicit instantiation directives. */
17187 push_deferring_access_checks (dk_no_check
);
17188 /* Parse a decl-specifier-seq. */
17189 cp_parser_decl_specifier_seq (parser
,
17190 CP_PARSER_FLAGS_OPTIONAL
,
17192 &declares_class_or_enum
);
17193 /* If there was exactly one decl-specifier, and it declared a class,
17194 and there's no declarator, then we have an explicit type
17196 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17200 type
= check_tag_decl (&decl_specifiers
,
17201 /*explicit_type_instantiation_p=*/true);
17202 /* Turn access control back on for names used during
17203 template instantiation. */
17204 pop_deferring_access_checks ();
17206 do_type_instantiation (type
, extension_specifier
,
17207 /*complain=*/tf_error
);
17211 cp_declarator
*declarator
;
17214 /* Parse the declarator. */
17216 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17217 CP_PARSER_FLAGS_NONE
,
17218 /*ctor_dtor_or_conv_p=*/NULL
,
17219 /*parenthesized_p=*/NULL
,
17220 /*member_p=*/false,
17221 /*friend_p=*/false,
17222 /*static_p=*/false);
17223 if (declares_class_or_enum
& 2)
17224 cp_parser_check_for_definition_in_return_type (declarator
,
17225 decl_specifiers
.type
,
17226 decl_specifiers
.locations
[ds_type_spec
]);
17227 if (declarator
!= cp_error_declarator
)
17229 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17230 permerror (decl_specifiers
.locations
[ds_inline
],
17231 "explicit instantiation shall not use"
17232 " %<inline%> specifier");
17233 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17234 permerror (decl_specifiers
.locations
[ds_constexpr
],
17235 "explicit instantiation shall not use"
17236 " %<constexpr%> specifier");
17238 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17239 NORMAL
, 0, &decl_specifiers
.attributes
);
17240 /* Turn access control back on for names used during
17241 template instantiation. */
17242 pop_deferring_access_checks ();
17243 /* Do the explicit instantiation. */
17244 do_decl_instantiation (decl
, extension_specifier
);
17248 pop_deferring_access_checks ();
17249 /* Skip the body of the explicit instantiation. */
17250 cp_parser_skip_to_end_of_statement (parser
);
17253 /* We're done with the instantiation. */
17254 end_explicit_instantiation ();
17256 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17258 timevar_pop (TV_TEMPLATE_INST
);
17261 /* Parse an explicit-specialization.
17263 explicit-specialization:
17264 template < > declaration
17266 Although the standard says `declaration', what it really means is:
17268 explicit-specialization:
17269 template <> decl-specifier [opt] init-declarator [opt] ;
17270 template <> function-definition
17271 template <> explicit-specialization
17272 template <> template-declaration */
17275 cp_parser_explicit_specialization (cp_parser
* parser
)
17277 bool need_lang_pop
;
17278 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17280 /* Look for the `template' keyword. */
17281 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17282 /* Look for the `<'. */
17283 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17284 /* Look for the `>'. */
17285 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17286 /* We have processed another parameter list. */
17287 ++parser
->num_template_parameter_lists
;
17290 A template ... explicit specialization ... shall not have C
17292 if (current_lang_name
== lang_name_c
)
17294 error_at (token
->location
, "template specialization with C linkage");
17295 maybe_show_extern_c_location ();
17296 /* Give it C++ linkage to avoid confusing other parts of the
17298 push_lang_context (lang_name_cplusplus
);
17299 need_lang_pop
= true;
17302 need_lang_pop
= false;
17303 /* Let the front end know that we are beginning a specialization. */
17304 if (!begin_specialization ())
17306 end_specialization ();
17310 /* If the next keyword is `template', we need to figure out whether
17311 or not we're looking a template-declaration. */
17312 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17314 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17315 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17316 cp_parser_template_declaration_after_export (parser
,
17317 /*member_p=*/false);
17319 cp_parser_explicit_specialization (parser
);
17322 /* Parse the dependent declaration. */
17323 cp_parser_single_declaration (parser
,
17325 /*member_p=*/false,
17326 /*explicit_specialization_p=*/true,
17327 /*friend_p=*/NULL
);
17328 /* We're done with the specialization. */
17329 end_specialization ();
17330 /* For the erroneous case of a template with C linkage, we pushed an
17331 implicit C++ linkage scope; exit that scope now. */
17333 pop_lang_context ();
17334 /* We're done with this parameter list. */
17335 --parser
->num_template_parameter_lists
;
17338 /* Parse a type-specifier.
17341 simple-type-specifier
17344 elaborated-type-specifier
17352 Returns a representation of the type-specifier. For a
17353 class-specifier, enum-specifier, or elaborated-type-specifier, a
17354 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17356 The parser flags FLAGS is used to control type-specifier parsing.
17358 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17359 in a decl-specifier-seq.
17361 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17362 class-specifier, enum-specifier, or elaborated-type-specifier, then
17363 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17364 if a type is declared; 2 if it is defined. Otherwise, it is set to
17367 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17368 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17369 is set to FALSE. */
17372 cp_parser_type_specifier (cp_parser
* parser
,
17373 cp_parser_flags flags
,
17374 cp_decl_specifier_seq
*decl_specs
,
17375 bool is_declaration
,
17376 int* declares_class_or_enum
,
17377 bool* is_cv_qualifier
)
17379 tree type_spec
= NULL_TREE
;
17382 cp_decl_spec ds
= ds_last
;
17384 /* Assume this type-specifier does not declare a new type. */
17385 if (declares_class_or_enum
)
17386 *declares_class_or_enum
= 0;
17387 /* And that it does not specify a cv-qualifier. */
17388 if (is_cv_qualifier
)
17389 *is_cv_qualifier
= false;
17390 /* Peek at the next token. */
17391 token
= cp_lexer_peek_token (parser
->lexer
);
17393 /* If we're looking at a keyword, we can use that to guide the
17394 production we choose. */
17395 keyword
= token
->keyword
;
17399 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17400 goto elaborated_type_specifier
;
17402 /* Look for the enum-specifier. */
17403 type_spec
= cp_parser_enum_specifier (parser
);
17404 /* If that worked, we're done. */
17407 if (declares_class_or_enum
)
17408 *declares_class_or_enum
= 2;
17410 cp_parser_set_decl_spec_type (decl_specs
,
17413 /*type_definition_p=*/true);
17417 goto elaborated_type_specifier
;
17419 /* Any of these indicate either a class-specifier, or an
17420 elaborated-type-specifier. */
17424 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17425 goto elaborated_type_specifier
;
17427 /* Parse tentatively so that we can back up if we don't find a
17428 class-specifier. */
17429 cp_parser_parse_tentatively (parser
);
17430 /* Look for the class-specifier. */
17431 type_spec
= cp_parser_class_specifier (parser
);
17432 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17433 /* If that worked, we're done. */
17434 if (cp_parser_parse_definitely (parser
))
17436 if (declares_class_or_enum
)
17437 *declares_class_or_enum
= 2;
17439 cp_parser_set_decl_spec_type (decl_specs
,
17442 /*type_definition_p=*/true);
17446 /* Fall through. */
17447 elaborated_type_specifier
:
17448 /* We're declaring (not defining) a class or enum. */
17449 if (declares_class_or_enum
)
17450 *declares_class_or_enum
= 1;
17452 /* Fall through. */
17454 /* Look for an elaborated-type-specifier. */
17456 = (cp_parser_elaborated_type_specifier
17458 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17461 cp_parser_set_decl_spec_type (decl_specs
,
17464 /*type_definition_p=*/false);
17469 if (is_cv_qualifier
)
17470 *is_cv_qualifier
= true;
17475 if (is_cv_qualifier
)
17476 *is_cv_qualifier
= true;
17481 if (is_cv_qualifier
)
17482 *is_cv_qualifier
= true;
17486 /* The `__complex__' keyword is a GNU extension. */
17494 /* Handle simple keywords. */
17499 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17500 decl_specs
->any_specifiers_p
= true;
17502 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17505 /* If we do not already have a type-specifier, assume we are looking
17506 at a simple-type-specifier. */
17507 type_spec
= cp_parser_simple_type_specifier (parser
,
17511 /* If we didn't find a type-specifier, and a type-specifier was not
17512 optional in this context, issue an error message. */
17513 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17515 cp_parser_error (parser
, "expected type specifier");
17516 return error_mark_node
;
17522 /* Parse a simple-type-specifier.
17524 simple-type-specifier:
17525 :: [opt] nested-name-specifier [opt] type-name
17526 :: [opt] nested-name-specifier template template-id
17541 simple-type-specifier:
17543 decltype ( expression )
17546 __underlying_type ( type-id )
17550 nested-name-specifier(opt) template-name
17554 simple-type-specifier:
17556 __typeof__ unary-expression
17557 __typeof__ ( type-id )
17558 __typeof__ ( type-id ) { initializer-list , [opt] }
17560 Concepts Extension:
17562 simple-type-specifier:
17563 constrained-type-specifier
17565 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17566 appropriately updated. */
17569 cp_parser_simple_type_specifier (cp_parser
* parser
,
17570 cp_decl_specifier_seq
*decl_specs
,
17571 cp_parser_flags flags
)
17573 tree type
= NULL_TREE
;
17577 /* Peek at the next token. */
17578 token
= cp_lexer_peek_token (parser
->lexer
);
17580 /* If we're looking at a keyword, things are easy. */
17581 switch (token
->keyword
)
17585 decl_specs
->explicit_char_p
= true;
17586 type
= char_type_node
;
17589 type
= char8_type_node
;
17592 type
= char16_type_node
;
17595 type
= char32_type_node
;
17598 type
= wchar_type_node
;
17601 type
= boolean_type_node
;
17604 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17605 type
= short_integer_type_node
;
17609 decl_specs
->explicit_int_p
= true;
17610 type
= integer_type_node
;
17616 idx
= token
->keyword
- RID_INT_N_0
;
17617 if (! int_n_enabled_p
[idx
])
17621 decl_specs
->explicit_intN_p
= true;
17622 decl_specs
->int_n_idx
= idx
;
17624 type
= int_n_trees
[idx
].signed_type
;
17628 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17629 type
= long_integer_type_node
;
17632 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17633 type
= integer_type_node
;
17636 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17637 type
= unsigned_type_node
;
17640 type
= float_type_node
;
17643 type
= double_type_node
;
17646 type
= void_type_node
;
17650 maybe_warn_cpp0x (CPP0X_AUTO
);
17651 if (parser
->auto_is_implicit_function_template_parm_p
)
17653 /* The 'auto' might be the placeholder return type for a function decl
17654 with trailing return type. */
17655 bool have_trailing_return_fn_decl
= false;
17657 cp_parser_parse_tentatively (parser
);
17658 cp_lexer_consume_token (parser
->lexer
);
17659 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17660 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17661 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17662 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17666 cp_lexer_consume_token (parser
->lexer
);
17667 cp_parser_skip_to_closing_parenthesis (parser
,
17668 /*recovering*/false,
17670 /*consume_paren*/true);
17674 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17676 have_trailing_return_fn_decl
= true;
17680 cp_lexer_consume_token (parser
->lexer
);
17682 cp_parser_abort_tentative_parse (parser
);
17684 if (have_trailing_return_fn_decl
)
17686 type
= make_auto ();
17690 if (cxx_dialect
>= cxx14
)
17692 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17693 type
= TREE_TYPE (type
);
17696 type
= error_mark_node
;
17698 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17700 if (cxx_dialect
< cxx14
)
17701 error_at (token
->location
,
17702 "use of %<auto%> in lambda parameter declaration "
17703 "only available with "
17704 "%<-std=c++14%> or %<-std=gnu++14%>");
17706 else if (cxx_dialect
< cxx14
)
17707 error_at (token
->location
,
17708 "use of %<auto%> in parameter declaration "
17709 "only available with "
17710 "%<-std=c++14%> or %<-std=gnu++14%>");
17711 else if (!flag_concepts
)
17712 pedwarn (token
->location
, 0,
17713 "use of %<auto%> in parameter declaration "
17714 "only available with %<-fconcepts%>");
17717 type
= make_auto ();
17721 /* Since DR 743, decltype can either be a simple-type-specifier by
17722 itself or begin a nested-name-specifier. Parsing it will replace
17723 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17724 handling below decide what to do. */
17725 cp_parser_decltype (parser
);
17726 cp_lexer_set_token_position (parser
->lexer
, token
);
17730 /* Consume the `typeof' token. */
17731 cp_lexer_consume_token (parser
->lexer
);
17732 /* Parse the operand to `typeof'. */
17733 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17734 /* If it is not already a TYPE, take its type. */
17735 if (!TYPE_P (type
))
17736 type
= finish_typeof (type
);
17739 cp_parser_set_decl_spec_type (decl_specs
, type
,
17741 /*type_definition_p=*/false);
17745 case RID_UNDERLYING_TYPE
:
17746 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17748 cp_parser_set_decl_spec_type (decl_specs
, type
,
17750 /*type_definition_p=*/false);
17755 case RID_DIRECT_BASES
:
17756 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17758 cp_parser_set_decl_spec_type (decl_specs
, type
,
17760 /*type_definition_p=*/false);
17766 /* If token is an already-parsed decltype not followed by ::,
17767 it's a simple-type-specifier. */
17768 if (token
->type
== CPP_DECLTYPE
17769 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17771 type
= saved_checks_value (token
->u
.tree_check_value
);
17774 cp_parser_set_decl_spec_type (decl_specs
, type
,
17776 /*type_definition_p=*/false);
17777 /* Remember that we are handling a decltype in order to
17778 implement the resolution of DR 1510 when the argument
17779 isn't instantiation dependent. */
17780 decl_specs
->decltype_p
= true;
17782 cp_lexer_consume_token (parser
->lexer
);
17786 /* If the type-specifier was for a built-in type, we're done. */
17789 /* Record the type. */
17791 && (token
->keyword
!= RID_SIGNED
17792 && token
->keyword
!= RID_UNSIGNED
17793 && token
->keyword
!= RID_SHORT
17794 && token
->keyword
!= RID_LONG
))
17795 cp_parser_set_decl_spec_type (decl_specs
,
17798 /*type_definition_p=*/false);
17800 decl_specs
->any_specifiers_p
= true;
17802 /* Consume the token. */
17803 cp_lexer_consume_token (parser
->lexer
);
17805 if (type
== error_mark_node
)
17806 return error_mark_node
;
17808 /* There is no valid C++ program where a non-template type is
17809 followed by a "<". That usually indicates that the user thought
17810 that the type was a template. */
17811 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17814 return TYPE_NAME (type
);
17817 /* The type-specifier must be a user-defined type. */
17818 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17822 const bool typename_p
= (cxx_dialect
>= cxx2a
17823 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
17825 /* Don't gobble tokens or issue error messages if this is an
17826 optional type-specifier. */
17827 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17828 cp_parser_parse_tentatively (parser
);
17830 token
= cp_lexer_peek_token (parser
->lexer
);
17832 /* Look for the optional `::' operator. */
17834 = (cp_parser_global_scope_opt (parser
,
17835 /*current_scope_valid_p=*/false)
17837 /* Look for the nested-name specifier. */
17839 = (cp_parser_nested_name_specifier_opt (parser
,
17840 /*typename_keyword_p=*/false,
17841 /*check_dependency_p=*/true,
17843 /*is_declaration=*/false)
17845 /* If we have seen a nested-name-specifier, and the next token
17846 is `template', then we are using the template-id production. */
17848 && cp_parser_optional_template_keyword (parser
))
17850 /* Look for the template-id. */
17851 type
= cp_parser_template_id (parser
,
17852 /*template_keyword_p=*/true,
17853 /*check_dependency_p=*/true,
17855 /*is_declaration=*/false);
17856 /* If the template-id did not name a type, we are out of
17858 if (TREE_CODE (type
) != TYPE_DECL
)
17860 /* ...unless we pretend we have seen 'typename'. */
17862 type
= cp_parser_make_typename_type (parser
, type
,
17866 cp_parser_error (parser
, "expected template-id for type");
17871 /* Otherwise, look for a type-name. */
17873 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
17875 /* Keep track of all name-lookups performed in class scopes. */
17879 && TREE_CODE (type
) == TYPE_DECL
17880 && identifier_p (DECL_NAME (type
)))
17881 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17882 /* If it didn't work out, we don't have a TYPE. */
17883 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17884 && !cp_parser_parse_definitely (parser
))
17886 if (!type
&& cxx_dialect
>= cxx17
)
17888 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17889 cp_parser_parse_tentatively (parser
);
17891 cp_parser_global_scope_opt (parser
,
17892 /*current_scope_valid_p=*/false);
17893 cp_parser_nested_name_specifier_opt (parser
,
17894 /*typename_keyword_p=*/false,
17895 /*check_dependency_p=*/true,
17897 /*is_declaration=*/false);
17898 tree name
= cp_parser_identifier (parser
);
17899 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17900 && parser
->scope
!= error_mark_node
)
17902 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17904 /*is_template=*/false,
17905 /*is_namespace=*/false,
17906 /*check_dependency=*/true,
17907 /*ambiguous_decls=*/NULL
,
17909 if (tmpl
&& tmpl
!= error_mark_node
17910 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17911 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17912 type
= make_template_placeholder (tmpl
);
17915 type
= error_mark_node
;
17916 if (!cp_parser_simulate_error (parser
))
17917 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17918 NLE_TYPE
, token
->location
);
17922 type
= error_mark_node
;
17924 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17925 && !cp_parser_parse_definitely (parser
))
17928 if (type
&& decl_specs
)
17929 cp_parser_set_decl_spec_type (decl_specs
, type
,
17931 /*type_definition_p=*/false);
17934 /* If we didn't get a type-name, issue an error message. */
17935 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17937 cp_parser_error (parser
, "expected type-name");
17938 return error_mark_node
;
17941 if (type
&& type
!= error_mark_node
)
17943 /* See if TYPE is an Objective-C type, and if so, parse and
17944 accept any protocol references following it. Do this before
17945 the cp_parser_check_for_invalid_template_id() call, because
17946 Objective-C types can be followed by '<...>' which would
17947 enclose protocol names rather than template arguments, and so
17948 everything is fine. */
17949 if (c_dialect_objc () && !parser
->scope
17950 && (objc_is_id (type
) || objc_is_class_name (type
)))
17952 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17953 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17955 /* Clobber the "unqualified" type previously entered into
17956 DECL_SPECS with the new, improved protocol-qualified version. */
17958 decl_specs
->type
= qual_type
;
17963 /* There is no valid C++ program where a non-template type is
17964 followed by a "<". That usually indicates that the user
17965 thought that the type was a template. */
17966 cp_parser_check_for_invalid_template_id (parser
, type
,
17974 /* Parse a type-name.
17980 simple-template-id [in c++0x]
17997 Returns a TYPE_DECL for the type. */
18000 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
18004 /* We can't know yet whether it is a class-name or not. */
18005 cp_parser_parse_tentatively (parser
);
18006 /* Try a class-name. */
18007 type_decl
= cp_parser_class_name (parser
,
18008 typename_keyword_p
,
18009 /*template_keyword_p=*/false,
18011 /*check_dependency_p=*/true,
18012 /*class_head_p=*/false,
18013 /*is_declaration=*/false);
18014 /* If it's not a class-name, keep looking. */
18015 if (!cp_parser_parse_definitely (parser
))
18017 if (cxx_dialect
< cxx11
)
18018 /* It must be a typedef-name or an enum-name. */
18019 return cp_parser_nonclass_name (parser
);
18021 cp_parser_parse_tentatively (parser
);
18022 /* It is either a simple-template-id representing an
18023 instantiation of an alias template... */
18024 type_decl
= cp_parser_template_id (parser
,
18025 /*template_keyword_p=*/false,
18026 /*check_dependency_p=*/true,
18028 /*is_declaration=*/false);
18029 /* Note that this must be an instantiation of an alias template
18030 because [temp.names]/6 says:
18032 A template-id that names an alias template specialization
18035 Whereas [temp.names]/7 says:
18037 A simple-template-id that names a class template
18038 specialization is a class-name.
18040 With concepts, this could also be a partial-concept-id that
18041 declares a non-type template parameter. */
18042 if (type_decl
!= NULL_TREE
18043 && TREE_CODE (type_decl
) == TYPE_DECL
18044 && TYPE_DECL_ALIAS_P (type_decl
))
18045 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
18046 else if (is_constrained_parameter (type_decl
))
18047 /* Don't do anything. */ ;
18049 cp_parser_simulate_error (parser
);
18051 if (!cp_parser_parse_definitely (parser
))
18052 /* ... Or a typedef-name or an enum-name. */
18053 return cp_parser_nonclass_name (parser
);
18059 /* Check if DECL and ARGS can form a constrained-type-specifier.
18060 If ARGS is non-null, we try to form a concept check of the
18061 form DECL<?, ARGS> where ? is a wildcard that matches any
18062 kind of template argument. If ARGS is NULL, then we try to
18063 form a concept check of the form DECL<?>. */
18066 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
18067 tree decl
, tree args
)
18069 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
18071 /* If we a constrained-type-specifier cannot be deduced. */
18072 if (parser
->prevent_constrained_type_specifiers
)
18075 /* A constrained type specifier can only be found in an
18076 overload set or as a reference to a template declaration.
18078 FIXME: This might be masking a bug. It's possible that
18079 that the deduction below is causing template specializations
18080 to be formed with the wildcard as an argument. */
18081 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
18084 /* Try to build a call expression that evaluates the
18085 concept. This can fail if the overload set refers
18086 only to non-templates. */
18087 tree placeholder
= build_nt (WILDCARD_DECL
);
18088 tree check
= build_concept_check (decl
, placeholder
, args
);
18089 if (check
== error_mark_node
)
18092 /* Deduce the checked constraint and the prototype parameter.
18094 FIXME: In certain cases, failure to deduce should be a
18095 diagnosable error. */
18098 if (!deduce_constrained_parameter (check
, conc
, proto
))
18101 /* In template parameter scope, this results in a constrained
18102 parameter. Return a descriptor of that parm. */
18103 if (processing_template_parmlist
)
18104 return build_constrained_parameter (conc
, proto
, args
);
18106 /* In a parameter-declaration-clause, constrained-type
18107 specifiers result in invented template parameters. */
18108 if (parser
->auto_is_implicit_function_template_parm_p
)
18110 tree x
= build_constrained_parameter (conc
, proto
, args
);
18111 return synthesize_implicit_template_parm (parser
, x
);
18115 /* Otherwise, we're in a context where the constrained
18116 type name is deduced and the constraint applies
18117 after deduction. */
18118 return make_constrained_auto (conc
, args
);
18124 /* If DECL refers to a concept, return a TYPE_DECL representing
18125 the result of using the constrained type specifier in the
18126 current context. DECL refers to a concept if
18128 - it is an overload set containing a function concept taking a single
18131 - it is a variable concept taking a single type argument. */
18134 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
18137 && (TREE_CODE (decl
) == OVERLOAD
18138 || BASELINK_P (decl
)
18139 || variable_concept_p (decl
)))
18140 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
18145 /* Check if DECL and ARGS form a partial-concept-id. If so,
18146 assign ID to the resulting constrained placeholder.
18148 Returns true if the partial-concept-id designates a placeholder
18149 and false otherwise. Note that *id is set to NULL_TREE in
18153 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
18155 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
18158 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18170 Returns a TYPE_DECL for the type. */
18173 cp_parser_nonclass_name (cp_parser
* parser
)
18178 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18179 identifier
= cp_parser_identifier (parser
);
18180 if (identifier
== error_mark_node
)
18181 return error_mark_node
;
18183 /* Look up the type-name. */
18184 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18186 type_decl
= strip_using_decl (type_decl
);
18188 /* If we found an overload set, then it may refer to a concept-name. */
18189 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
18192 if (TREE_CODE (type_decl
) != TYPE_DECL
18193 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18195 /* See if this is an Objective-C type. */
18196 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18197 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18199 type_decl
= TYPE_NAME (type
);
18202 /* Issue an error if we did not find a type-name. */
18203 if (TREE_CODE (type_decl
) != TYPE_DECL
18204 /* In Objective-C, we have the complication that class names are
18205 normally type names and start declarations (eg, the
18206 "NSObject" in "NSObject *object;"), but can be used in an
18207 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18208 is an expression. So, a classname followed by a dot is not a
18209 valid type-name. */
18210 || (objc_is_class_name (TREE_TYPE (type_decl
))
18211 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18213 if (!cp_parser_simulate_error (parser
))
18214 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18215 NLE_TYPE
, token
->location
);
18216 return error_mark_node
;
18218 /* Remember that the name was used in the definition of the
18219 current class so that we can check later to see if the
18220 meaning would have been different after the class was
18221 entirely defined. */
18222 else if (type_decl
!= error_mark_node
18224 maybe_note_name_used_in_class (identifier
, type_decl
);
18229 /* Parse an elaborated-type-specifier. Note that the grammar given
18230 here incorporates the resolution to DR68.
18232 elaborated-type-specifier:
18233 class-key :: [opt] nested-name-specifier [opt] identifier
18234 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18235 enum-key :: [opt] nested-name-specifier [opt] identifier
18236 typename :: [opt] nested-name-specifier identifier
18237 typename :: [opt] nested-name-specifier template [opt]
18242 elaborated-type-specifier:
18243 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18244 class-key attributes :: [opt] nested-name-specifier [opt]
18245 template [opt] template-id
18246 enum attributes :: [opt] nested-name-specifier [opt] identifier
18248 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18249 declared `friend'. If IS_DECLARATION is TRUE, then this
18250 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18251 something is being declared.
18253 Returns the TYPE specified. */
18256 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18258 bool is_declaration
)
18260 enum tag_types tag_type
;
18262 tree type
= NULL_TREE
;
18263 tree attributes
= NULL_TREE
;
18265 cp_token
*token
= NULL
;
18267 /* See if we're looking at the `enum' keyword. */
18268 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18270 /* Consume the `enum' token. */
18271 cp_lexer_consume_token (parser
->lexer
);
18272 /* Remember that it's an enumeration type. */
18273 tag_type
= enum_type
;
18274 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18275 enums) is used here. */
18276 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18277 if (cp_parser_is_keyword (token
, RID_CLASS
)
18278 || cp_parser_is_keyword (token
, RID_STRUCT
))
18280 gcc_rich_location
richloc (token
->location
);
18281 richloc
.add_range (input_location
);
18282 richloc
.add_fixit_remove ();
18283 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18284 "a scoped enum must not use the %qD keyword",
18286 /* Consume the `struct' or `class' and parse it anyway. */
18287 cp_lexer_consume_token (parser
->lexer
);
18289 /* Parse the attributes. */
18290 attributes
= cp_parser_attributes_opt (parser
);
18292 /* Or, it might be `typename'. */
18293 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18296 /* Consume the `typename' token. */
18297 cp_lexer_consume_token (parser
->lexer
);
18298 /* Remember that it's a `typename' type. */
18299 tag_type
= typename_type
;
18301 /* Otherwise it must be a class-key. */
18304 tag_type
= cp_parser_class_key (parser
);
18305 if (tag_type
== none_type
)
18306 return error_mark_node
;
18307 /* Parse the attributes. */
18308 attributes
= cp_parser_attributes_opt (parser
);
18311 /* Look for the `::' operator. */
18312 globalscope
= cp_parser_global_scope_opt (parser
,
18313 /*current_scope_valid_p=*/false);
18314 /* Look for the nested-name-specifier. */
18315 tree nested_name_specifier
;
18316 if (tag_type
== typename_type
&& !globalscope
)
18318 nested_name_specifier
18319 = cp_parser_nested_name_specifier (parser
,
18320 /*typename_keyword_p=*/true,
18321 /*check_dependency_p=*/true,
18324 if (!nested_name_specifier
)
18325 return error_mark_node
;
18328 /* Even though `typename' is not present, the proposed resolution
18329 to Core Issue 180 says that in `class A<T>::B', `B' should be
18330 considered a type-name, even if `A<T>' is dependent. */
18331 nested_name_specifier
18332 = cp_parser_nested_name_specifier_opt (parser
,
18333 /*typename_keyword_p=*/true,
18334 /*check_dependency_p=*/true,
18337 /* For everything but enumeration types, consider a template-id.
18338 For an enumeration type, consider only a plain identifier. */
18339 if (tag_type
!= enum_type
)
18341 bool template_p
= false;
18344 /* Allow the `template' keyword. */
18345 template_p
= cp_parser_optional_template_keyword (parser
);
18346 /* If we didn't see `template', we don't know if there's a
18347 template-id or not. */
18349 cp_parser_parse_tentatively (parser
);
18350 /* The `template' keyword must follow a nested-name-specifier. */
18351 else if (!nested_name_specifier
)
18353 cp_parser_error (parser
, "%<template%> must follow a nested-"
18355 return error_mark_node
;
18358 /* Parse the template-id. */
18359 token
= cp_lexer_peek_token (parser
->lexer
);
18360 decl
= cp_parser_template_id (parser
, template_p
,
18361 /*check_dependency_p=*/true,
18364 /* If we didn't find a template-id, look for an ordinary
18366 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18368 /* We can get here when cp_parser_template_id, called by
18369 cp_parser_class_name with tag_type == none_type, succeeds
18370 and caches a BASELINK. Then, when called again here,
18371 instead of failing and returning an error_mark_node
18372 returns it (see template/typename17.C in C++11).
18373 ??? Could we diagnose this earlier? */
18374 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18376 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18377 type
= error_mark_node
;
18379 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18380 in effect, then we must assume that, upon instantiation, the
18381 template will correspond to a class. */
18382 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18383 && tag_type
== typename_type
)
18384 type
= make_typename_type (parser
->scope
, decl
,
18386 /*complain=*/tf_error
);
18387 /* If the `typename' keyword is in effect and DECL is not a type
18388 decl, then type is non existent. */
18389 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18391 else if (TREE_CODE (decl
) == TYPE_DECL
)
18393 type
= check_elaborated_type_specifier (tag_type
, decl
,
18394 /*allow_template_p=*/true);
18396 /* If the next token is a semicolon, this must be a specialization,
18397 instantiation, or friend declaration. Check the scope while we
18398 still know whether or not we had a nested-name-specifier. */
18399 if (type
!= error_mark_node
18400 && !nested_name_specifier
&& !is_friend
18401 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18402 check_unqualified_spec_or_inst (type
, token
->location
);
18404 else if (decl
== error_mark_node
)
18405 type
= error_mark_node
;
18410 token
= cp_lexer_peek_token (parser
->lexer
);
18411 identifier
= cp_parser_identifier (parser
);
18413 if (identifier
== error_mark_node
)
18415 parser
->scope
= NULL_TREE
;
18416 return error_mark_node
;
18419 /* For a `typename', we needn't call xref_tag. */
18420 if (tag_type
== typename_type
18421 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18422 return cp_parser_make_typename_type (parser
, identifier
,
18425 /* Template parameter lists apply only if we are not within a
18426 function parameter list. */
18427 bool template_parm_lists_apply
18428 = parser
->num_template_parameter_lists
;
18429 if (template_parm_lists_apply
)
18430 for (cp_binding_level
*s
= current_binding_level
;
18431 s
&& s
->kind
!= sk_template_parms
;
18432 s
= s
->level_chain
)
18433 if (s
->kind
== sk_function_parms
)
18434 template_parm_lists_apply
= false;
18436 /* Look up a qualified name in the usual way. */
18440 tree ambiguous_decls
;
18442 decl
= cp_parser_lookup_name (parser
, identifier
,
18444 /*is_template=*/false,
18445 /*is_namespace=*/false,
18446 /*check_dependency=*/true,
18450 /* If the lookup was ambiguous, an error will already have been
18452 if (ambiguous_decls
)
18453 return error_mark_node
;
18455 /* If we are parsing friend declaration, DECL may be a
18456 TEMPLATE_DECL tree node here. However, we need to check
18457 whether this TEMPLATE_DECL results in valid code. Consider
18458 the following example:
18461 template <class T> class C {};
18464 template <class T> friend class N::C; // #1, valid code
18466 template <class T> class Y {
18467 friend class N::C; // #2, invalid code
18470 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18471 name lookup of `N::C'. We see that friend declaration must
18472 be template for the code to be valid. Note that
18473 processing_template_decl does not work here since it is
18474 always 1 for the above two cases. */
18476 decl
= (cp_parser_maybe_treat_template_as_class
18477 (decl
, /*tag_name_p=*/is_friend
18478 && template_parm_lists_apply
));
18480 if (TREE_CODE (decl
) != TYPE_DECL
)
18482 cp_parser_diagnose_invalid_type_name (parser
,
18485 return error_mark_node
;
18488 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18490 bool allow_template
= (template_parm_lists_apply
18491 || DECL_SELF_REFERENCE_P (decl
));
18492 type
= check_elaborated_type_specifier (tag_type
, decl
,
18495 if (type
== error_mark_node
)
18496 return error_mark_node
;
18499 /* Forward declarations of nested types, such as
18504 are invalid unless all components preceding the final '::'
18505 are complete. If all enclosing types are complete, these
18506 declarations become merely pointless.
18508 Invalid forward declarations of nested types are errors
18509 caught elsewhere in parsing. Those that are pointless arrive
18512 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18513 && !is_friend
&& !processing_explicit_instantiation
)
18514 warning (0, "declaration %qD does not declare anything", decl
);
18516 type
= TREE_TYPE (decl
);
18520 /* An elaborated-type-specifier sometimes introduces a new type and
18521 sometimes names an existing type. Normally, the rule is that it
18522 introduces a new type only if there is not an existing type of
18523 the same name already in scope. For example, given:
18526 void f() { struct S s; }
18528 the `struct S' in the body of `f' is the same `struct S' as in
18529 the global scope; the existing definition is used. However, if
18530 there were no global declaration, this would introduce a new
18531 local class named `S'.
18533 An exception to this rule applies to the following code:
18535 namespace N { struct S; }
18537 Here, the elaborated-type-specifier names a new type
18538 unconditionally; even if there is already an `S' in the
18539 containing scope this declaration names a new type.
18540 This exception only applies if the elaborated-type-specifier
18541 forms the complete declaration:
18545 A declaration consisting solely of `class-key identifier ;' is
18546 either a redeclaration of the name in the current scope or a
18547 forward declaration of the identifier as a class name. It
18548 introduces the name into the current scope.
18550 We are in this situation precisely when the next token is a `;'.
18552 An exception to the exception is that a `friend' declaration does
18553 *not* name a new type; i.e., given:
18555 struct S { friend struct T; };
18557 `T' is not a new type in the scope of `S'.
18559 Also, `new struct S' or `sizeof (struct S)' never results in the
18560 definition of a new type; a new type can only be declared in a
18561 declaration context. */
18567 /* Friends have special name lookup rules. */
18568 ts
= ts_within_enclosing_non_class
;
18569 else if (is_declaration
18570 && cp_lexer_next_token_is (parser
->lexer
,
18572 /* This is a `class-key identifier ;' */
18578 (template_parm_lists_apply
18579 && (cp_parser_next_token_starts_class_definition_p (parser
)
18580 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18581 /* An unqualified name was used to reference this type, so
18582 there were no qualifying templates. */
18583 if (template_parm_lists_apply
18584 && !cp_parser_check_template_parameters (parser
,
18585 /*num_templates=*/0,
18586 /*template_id*/false,
18588 /*declarator=*/NULL
))
18589 return error_mark_node
;
18590 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18594 if (type
== error_mark_node
)
18595 return error_mark_node
;
18597 /* Allow attributes on forward declarations of classes. */
18600 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18601 warning (OPT_Wattributes
,
18602 "attributes ignored on uninstantiated type");
18603 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18604 && ! processing_explicit_instantiation
)
18605 warning (OPT_Wattributes
,
18606 "attributes ignored on template instantiation");
18607 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18608 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18610 warning (OPT_Wattributes
,
18611 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18614 if (tag_type
!= enum_type
)
18616 /* Indicate whether this class was declared as a `class' or as a
18618 if (CLASS_TYPE_P (type
))
18619 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18620 cp_parser_check_class_key (tag_type
, type
);
18623 /* A "<" cannot follow an elaborated type specifier. If that
18624 happens, the user was probably trying to form a template-id. */
18625 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18631 /* Parse an enum-specifier.
18634 enum-head { enumerator-list [opt] }
18635 enum-head { enumerator-list , } [C++0x]
18638 enum-key identifier [opt] enum-base [opt]
18639 enum-key nested-name-specifier identifier enum-base [opt]
18644 enum struct [C++0x]
18647 : type-specifier-seq
18649 opaque-enum-specifier:
18650 enum-key identifier enum-base [opt] ;
18653 enum-key attributes[opt] identifier [opt] enum-base [opt]
18654 { enumerator-list [opt] }attributes[opt]
18655 enum-key attributes[opt] identifier [opt] enum-base [opt]
18656 { enumerator-list, }attributes[opt] [C++0x]
18658 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18659 if the token stream isn't an enum-specifier after all. */
18662 cp_parser_enum_specifier (cp_parser
* parser
)
18665 tree type
= NULL_TREE
;
18667 tree nested_name_specifier
= NULL_TREE
;
18669 bool scoped_enum_p
= false;
18670 bool has_underlying_type
= false;
18671 bool nested_being_defined
= false;
18672 bool new_value_list
= false;
18673 bool is_new_type
= false;
18674 bool is_unnamed
= false;
18675 tree underlying_type
= NULL_TREE
;
18676 cp_token
*type_start_token
= NULL
;
18677 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18679 parser
->colon_corrects_to_scope_p
= false;
18681 /* Parse tentatively so that we can back up if we don't find a
18683 cp_parser_parse_tentatively (parser
);
18685 /* Caller guarantees that the current token is 'enum', an identifier
18686 possibly follows, and the token after that is an opening brace.
18687 If we don't have an identifier, fabricate an anonymous name for
18688 the enumeration being defined. */
18689 cp_lexer_consume_token (parser
->lexer
);
18691 /* Parse the "class" or "struct", which indicates a scoped
18692 enumeration type in C++0x. */
18693 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18694 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18696 if (cxx_dialect
< cxx11
)
18697 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18699 /* Consume the `struct' or `class' token. */
18700 cp_lexer_consume_token (parser
->lexer
);
18702 scoped_enum_p
= true;
18705 attributes
= cp_parser_attributes_opt (parser
);
18707 /* Clear the qualification. */
18708 parser
->scope
= NULL_TREE
;
18709 parser
->qualifying_scope
= NULL_TREE
;
18710 parser
->object_scope
= NULL_TREE
;
18712 /* Figure out in what scope the declaration is being placed. */
18713 prev_scope
= current_scope ();
18715 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18717 push_deferring_access_checks (dk_no_check
);
18718 nested_name_specifier
18719 = cp_parser_nested_name_specifier_opt (parser
,
18720 /*typename_keyword_p=*/true,
18721 /*check_dependency_p=*/false,
18723 /*is_declaration=*/false);
18725 if (nested_name_specifier
)
18729 identifier
= cp_parser_identifier (parser
);
18730 name
= cp_parser_lookup_name (parser
, identifier
,
18732 /*is_template=*/false,
18733 /*is_namespace=*/false,
18734 /*check_dependency=*/true,
18735 /*ambiguous_decls=*/NULL
,
18737 if (name
&& name
!= error_mark_node
)
18739 type
= TREE_TYPE (name
);
18740 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18742 /* Are template enums allowed in ISO? */
18743 if (template_parm_scope_p ())
18744 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18745 "%qD is an enumeration template", name
);
18746 /* ignore a typename reference, for it will be solved by name
18751 else if (nested_name_specifier
== error_mark_node
)
18752 /* We already issued an error. */;
18755 error_at (type_start_token
->location
,
18756 "%qD does not name an enumeration in %qT",
18757 identifier
, nested_name_specifier
);
18758 nested_name_specifier
= error_mark_node
;
18763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18764 identifier
= cp_parser_identifier (parser
);
18767 identifier
= make_anon_name ();
18770 error_at (type_start_token
->location
,
18771 "unnamed scoped enum is not allowed");
18774 pop_deferring_access_checks ();
18776 /* Check for the `:' that denotes a specified underlying type in C++0x.
18777 Note that a ':' could also indicate a bitfield width, however. */
18778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18780 cp_decl_specifier_seq type_specifiers
;
18782 /* Consume the `:'. */
18783 cp_lexer_consume_token (parser
->lexer
);
18785 /* Parse the type-specifier-seq. */
18786 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
18787 /*is_declaration=*/false,
18788 /*is_trailing_return=*/false,
18791 /* At this point this is surely not elaborated type specifier. */
18792 if (!cp_parser_parse_definitely (parser
))
18795 if (cxx_dialect
< cxx11
)
18796 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18798 has_underlying_type
= true;
18800 /* If that didn't work, stop. */
18801 if (type_specifiers
.type
!= error_mark_node
)
18803 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18804 /*initialized=*/0, NULL
);
18805 if (underlying_type
== error_mark_node
18806 || check_for_bare_parameter_packs (underlying_type
))
18807 underlying_type
= NULL_TREE
;
18811 /* Look for the `{' but don't consume it yet. */
18812 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18814 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18816 cp_parser_error (parser
, "expected %<{%>");
18817 if (has_underlying_type
)
18823 /* An opaque-enum-specifier must have a ';' here. */
18824 if ((scoped_enum_p
|| underlying_type
)
18825 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18827 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18828 if (has_underlying_type
)
18836 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18839 if (nested_name_specifier
)
18841 if (CLASS_TYPE_P (nested_name_specifier
))
18843 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18844 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18845 push_scope (nested_name_specifier
);
18847 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18849 push_nested_namespace (nested_name_specifier
);
18853 /* Issue an error message if type-definitions are forbidden here. */
18854 if (!cp_parser_check_type_definition (parser
))
18855 type
= error_mark_node
;
18857 /* Create the new type. We do this before consuming the opening
18858 brace so the enum will be recorded as being on the line of its
18859 tag (or the 'enum' keyword, if there is no tag). */
18860 type
= start_enum (identifier
, type
, underlying_type
,
18861 attributes
, scoped_enum_p
, &is_new_type
);
18863 /* If the next token is not '{' it is an opaque-enum-specifier or an
18864 elaborated-type-specifier. */
18865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18867 timevar_push (TV_PARSE_ENUM
);
18868 if (nested_name_specifier
18869 && nested_name_specifier
!= error_mark_node
)
18871 /* The following catches invalid code such as:
18872 enum class S<int>::E { A, B, C }; */
18873 if (!processing_specialization
18874 && CLASS_TYPE_P (nested_name_specifier
)
18875 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18876 error_at (type_start_token
->location
, "cannot add an enumerator "
18877 "list to a template instantiation");
18879 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18881 error_at (type_start_token
->location
,
18882 "%<%T::%E%> has not been declared",
18883 TYPE_CONTEXT (nested_name_specifier
),
18884 nested_name_specifier
);
18885 type
= error_mark_node
;
18887 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18888 && !CLASS_TYPE_P (nested_name_specifier
))
18890 error_at (type_start_token
->location
, "nested name specifier "
18891 "%qT for enum declaration does not name a class "
18892 "or namespace", nested_name_specifier
);
18893 type
= error_mark_node
;
18895 /* If that scope does not contain the scope in which the
18896 class was originally declared, the program is invalid. */
18897 else if (prev_scope
&& !is_ancestor (prev_scope
,
18898 nested_name_specifier
))
18900 if (at_namespace_scope_p ())
18901 error_at (type_start_token
->location
,
18902 "declaration of %qD in namespace %qD which does not "
18904 type
, prev_scope
, nested_name_specifier
);
18906 error_at (type_start_token
->location
,
18907 "declaration of %qD in %qD which does not "
18909 type
, prev_scope
, nested_name_specifier
);
18910 type
= error_mark_node
;
18912 /* If that scope is the scope where the declaration is being placed
18913 the program is invalid. */
18914 else if (CLASS_TYPE_P (nested_name_specifier
)
18915 && CLASS_TYPE_P (prev_scope
)
18916 && same_type_p (nested_name_specifier
, prev_scope
))
18918 permerror (type_start_token
->location
,
18919 "extra qualification not allowed");
18920 nested_name_specifier
= NULL_TREE
;
18925 begin_scope (sk_scoped_enum
, type
);
18927 /* Consume the opening brace. */
18928 matching_braces braces
;
18929 braces
.consume_open (parser
);
18931 if (type
== error_mark_node
)
18932 ; /* Nothing to add */
18933 else if (OPAQUE_ENUM_P (type
)
18934 || (cxx_dialect
> cxx98
&& processing_specialization
))
18936 new_value_list
= true;
18937 SET_OPAQUE_ENUM_P (type
, false);
18938 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18942 error_at (type_start_token
->location
,
18943 "multiple definition of %q#T", type
);
18944 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18945 "previous definition here");
18946 type
= error_mark_node
;
18949 if (type
== error_mark_node
)
18950 cp_parser_skip_to_end_of_block_or_statement (parser
);
18951 /* If the next token is not '}', then there are some enumerators. */
18952 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18954 if (is_unnamed
&& !scoped_enum_p
)
18955 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18956 "ISO C++ forbids empty unnamed enum");
18959 cp_parser_enumerator_list (parser
, type
);
18961 /* Consume the final '}'. */
18962 braces
.require_close (parser
);
18966 timevar_pop (TV_PARSE_ENUM
);
18970 /* If a ';' follows, then it is an opaque-enum-specifier
18971 and additional restrictions apply. */
18972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18975 error_at (type_start_token
->location
,
18976 "opaque-enum-specifier without name");
18977 else if (nested_name_specifier
)
18978 error_at (type_start_token
->location
,
18979 "opaque-enum-specifier must use a simple identifier");
18983 /* Look for trailing attributes to apply to this enumeration, and
18984 apply them if appropriate. */
18985 if (cp_parser_allow_gnu_extensions_p (parser
))
18987 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18988 cplus_decl_attributes (&type
,
18990 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18993 /* Finish up the enumeration. */
18994 if (type
!= error_mark_node
)
18996 if (new_value_list
)
18997 finish_enum_value_list (type
);
18999 finish_enum (type
);
19002 if (nested_name_specifier
)
19004 if (CLASS_TYPE_P (nested_name_specifier
))
19006 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
19007 pop_scope (nested_name_specifier
);
19009 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19011 pop_nested_namespace (nested_name_specifier
);
19015 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19019 /* Parse an enumerator-list. The enumerators all have the indicated
19023 enumerator-definition
19024 enumerator-list , enumerator-definition */
19027 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
19031 /* Parse an enumerator-definition. */
19032 cp_parser_enumerator_definition (parser
, type
);
19034 /* If the next token is not a ',', we've reached the end of
19036 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19038 /* Otherwise, consume the `,' and keep going. */
19039 cp_lexer_consume_token (parser
->lexer
);
19040 /* If the next token is a `}', there is a trailing comma. */
19041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19043 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
19044 pedwarn (input_location
, OPT_Wpedantic
,
19045 "comma at end of enumerator list");
19051 /* Parse an enumerator-definition. The enumerator has the indicated
19054 enumerator-definition:
19056 enumerator = constant-expression
19063 enumerator-definition:
19064 enumerator attributes [opt]
19065 enumerator attributes [opt] = constant-expression */
19068 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
19074 /* Save the input location because we are interested in the location
19075 of the identifier and not the location of the explicit value. */
19076 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19078 /* Look for the identifier. */
19079 identifier
= cp_parser_identifier (parser
);
19080 if (identifier
== error_mark_node
)
19083 /* Parse any specified attributes. */
19084 tree attrs
= cp_parser_attributes_opt (parser
);
19086 /* If the next token is an '=', then there is an explicit value. */
19087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19089 /* Consume the `=' token. */
19090 cp_lexer_consume_token (parser
->lexer
);
19091 /* Parse the value. */
19092 value
= cp_parser_constant_expression (parser
);
19097 /* If we are processing a template, make sure the initializer of the
19098 enumerator doesn't contain any bare template parameter pack. */
19099 if (check_for_bare_parameter_packs (value
))
19100 value
= error_mark_node
;
19102 /* Create the enumerator. */
19103 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19106 /* Parse a namespace-name.
19109 original-namespace-name
19112 Returns the NAMESPACE_DECL for the namespace. */
19115 cp_parser_namespace_name (cp_parser
* parser
)
19118 tree namespace_decl
;
19120 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19122 /* Get the name of the namespace. */
19123 identifier
= cp_parser_identifier (parser
);
19124 if (identifier
== error_mark_node
)
19125 return error_mark_node
;
19127 /* Look up the identifier in the currently active scope. Look only
19128 for namespaces, due to:
19130 [basic.lookup.udir]
19132 When looking up a namespace-name in a using-directive or alias
19133 definition, only namespace names are considered.
19137 [basic.lookup.qual]
19139 During the lookup of a name preceding the :: scope resolution
19140 operator, object, function, and enumerator names are ignored.
19142 (Note that cp_parser_qualifying_entity only calls this
19143 function if the token after the name is the scope resolution
19145 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19147 /*is_template=*/false,
19148 /*is_namespace=*/true,
19149 /*check_dependency=*/true,
19150 /*ambiguous_decls=*/NULL
,
19152 /* If it's not a namespace, issue an error. */
19153 if (namespace_decl
== error_mark_node
19154 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19156 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19158 auto_diagnostic_group d
;
19160 if (namespace_decl
== error_mark_node
19161 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19162 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19165 if (const char *suggestion
= hint
.suggestion ())
19167 gcc_rich_location
richloc (token
->location
);
19168 richloc
.add_fixit_replace (suggestion
);
19169 error_at (&richloc
,
19170 "%qD is not a namespace-name; did you mean %qs?",
19171 identifier
, suggestion
);
19174 error_at (token
->location
, "%qD is not a namespace-name",
19178 cp_parser_error (parser
, "expected namespace-name");
19179 namespace_decl
= error_mark_node
;
19182 return namespace_decl
;
19185 /* Parse a namespace-definition.
19187 namespace-definition:
19188 named-namespace-definition
19189 unnamed-namespace-definition
19191 named-namespace-definition:
19192 original-namespace-definition
19193 extension-namespace-definition
19195 original-namespace-definition:
19196 namespace identifier { namespace-body }
19198 extension-namespace-definition:
19199 namespace original-namespace-name { namespace-body }
19201 unnamed-namespace-definition:
19202 namespace { namespace-body } */
19205 cp_parser_namespace_definition (cp_parser
* parser
)
19208 int nested_definition_count
= 0;
19210 cp_ensure_no_omp_declare_simd (parser
);
19211 cp_ensure_no_oacc_routine (parser
);
19213 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19214 const bool topmost_inline_p
= is_inline
;
19218 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19219 cp_lexer_consume_token (parser
->lexer
);
19222 /* Look for the `namespace' keyword. */
19224 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19226 /* Parse any specified attributes before the identifier. */
19227 tree attribs
= cp_parser_attributes_opt (parser
);
19231 identifier
= NULL_TREE
;
19233 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19235 if (nested_inline_p
&& nested_definition_count
!= 0)
19237 if (cxx_dialect
< cxx2a
)
19238 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19239 OPT_Wpedantic
, "nested inline namespace definitions only "
19240 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19241 cp_lexer_consume_token (parser
->lexer
);
19244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19246 identifier
= cp_parser_identifier (parser
);
19248 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19249 pedwarn (input_location
, OPT_Wpedantic
,
19250 "standard attributes on namespaces must precede "
19251 "the namespace name");
19253 /* Parse any attributes specified after the identifier. */
19254 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19257 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19259 /* Don't forget that the innermost namespace might have been
19260 marked as inline. Use |= because we cannot overwrite
19261 IS_INLINE in case the outermost namespace is inline, but
19262 there are no nested inlines. */
19263 is_inline
|= nested_inline_p
;
19267 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19268 pedwarn (input_location
, OPT_Wpedantic
,
19269 "nested namespace definitions only available with "
19270 "%<-std=c++17%> or %<-std=gnu++17%>");
19272 /* Nested namespace names can create new namespaces (unlike
19273 other qualified-ids). */
19274 if (int count
= (identifier
19275 ? push_namespace (identifier
, nested_inline_p
)
19277 nested_definition_count
+= count
;
19279 cp_parser_error (parser
, "nested namespace name required");
19280 cp_lexer_consume_token (parser
->lexer
);
19283 if (nested_definition_count
&& !identifier
)
19284 cp_parser_error (parser
, "namespace name required");
19286 if (nested_definition_count
&& attribs
)
19287 error_at (token
->location
,
19288 "a nested namespace definition cannot have attributes");
19289 if (nested_definition_count
&& topmost_inline_p
)
19290 error_at (token
->location
,
19291 "a nested namespace definition cannot be inline");
19293 /* Start the namespace. */
19294 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19296 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19298 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19300 /* Look for the `{' to validate starting the namespace. */
19301 matching_braces braces
;
19302 if (braces
.require_open (parser
))
19304 /* Parse the body of the namespace. */
19305 cp_parser_namespace_body (parser
);
19307 /* Look for the final `}'. */
19308 braces
.require_close (parser
);
19311 if (has_visibility
)
19312 pop_visibility (1);
19314 /* Pop the nested namespace definitions. */
19315 while (nested_definition_count
--)
19319 /* Parse a namespace-body.
19322 declaration-seq [opt] */
19325 cp_parser_namespace_body (cp_parser
* parser
)
19327 cp_parser_declaration_seq_opt (parser
);
19330 /* Parse a namespace-alias-definition.
19332 namespace-alias-definition:
19333 namespace identifier = qualified-namespace-specifier ; */
19336 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19339 tree namespace_specifier
;
19341 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19343 /* Look for the `namespace' keyword. */
19344 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19345 /* Look for the identifier. */
19346 identifier
= cp_parser_identifier (parser
);
19347 if (identifier
== error_mark_node
)
19349 /* Look for the `=' token. */
19350 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19351 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19353 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19354 /* Skip the definition. */
19355 cp_lexer_consume_token (parser
->lexer
);
19356 if (cp_parser_skip_to_closing_brace (parser
))
19357 cp_lexer_consume_token (parser
->lexer
);
19360 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19361 /* Look for the qualified-namespace-specifier. */
19362 namespace_specifier
19363 = cp_parser_qualified_namespace_specifier (parser
);
19364 /* Look for the `;' token. */
19365 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19367 /* Register the alias in the symbol table. */
19368 do_namespace_alias (identifier
, namespace_specifier
);
19371 /* Parse a qualified-namespace-specifier.
19373 qualified-namespace-specifier:
19374 :: [opt] nested-name-specifier [opt] namespace-name
19376 Returns a NAMESPACE_DECL corresponding to the specified
19380 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19382 /* Look for the optional `::'. */
19383 cp_parser_global_scope_opt (parser
,
19384 /*current_scope_valid_p=*/false);
19386 /* Look for the optional nested-name-specifier. */
19387 cp_parser_nested_name_specifier_opt (parser
,
19388 /*typename_keyword_p=*/false,
19389 /*check_dependency_p=*/true,
19391 /*is_declaration=*/true);
19393 return cp_parser_namespace_name (parser
);
19396 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19397 access declaration.
19400 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19401 using :: unqualified-id ;
19403 access-declaration:
19409 cp_parser_using_declaration (cp_parser
* parser
,
19410 bool access_declaration_p
)
19413 bool typename_p
= false;
19414 bool global_scope_p
;
19418 int oldcount
= errorcount
;
19419 cp_token
*diag_token
= NULL
;
19421 if (access_declaration_p
)
19423 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19424 cp_parser_parse_tentatively (parser
);
19428 /* Look for the `using' keyword. */
19429 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19432 /* Peek at the next token. */
19433 token
= cp_lexer_peek_token (parser
->lexer
);
19434 /* See if it's `typename'. */
19435 if (token
->keyword
== RID_TYPENAME
)
19437 /* Remember that we've seen it. */
19439 /* Consume the `typename' token. */
19440 cp_lexer_consume_token (parser
->lexer
);
19444 /* Look for the optional global scope qualification. */
19446 = (cp_parser_global_scope_opt (parser
,
19447 /*current_scope_valid_p=*/false)
19450 /* If we saw `typename', or didn't see `::', then there must be a
19451 nested-name-specifier present. */
19452 if (typename_p
|| !global_scope_p
)
19454 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19455 /*check_dependency_p=*/true,
19457 /*is_declaration=*/true);
19458 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19460 cp_parser_skip_to_end_of_block_or_statement (parser
);
19464 /* Otherwise, we could be in either of the two productions. In that
19465 case, treat the nested-name-specifier as optional. */
19467 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19468 /*typename_keyword_p=*/false,
19469 /*check_dependency_p=*/true,
19471 /*is_declaration=*/true);
19473 qscope
= global_namespace
;
19474 else if (UNSCOPED_ENUM_P (qscope
)
19475 && !TYPE_FUNCTION_SCOPE_P (qscope
))
19476 qscope
= CP_TYPE_CONTEXT (qscope
);
19478 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19479 /* Something has already gone wrong; there's no need to parse
19480 further. Since an error has occurred, the return value of
19481 cp_parser_parse_definitely will be false, as required. */
19482 return cp_parser_parse_definitely (parser
);
19484 token
= cp_lexer_peek_token (parser
->lexer
);
19485 /* Parse the unqualified-id. */
19486 identifier
= cp_parser_unqualified_id (parser
,
19487 /*template_keyword_p=*/false,
19488 /*check_dependency_p=*/true,
19489 /*declarator_p=*/true,
19490 /*optional_p=*/false);
19492 if (access_declaration_p
)
19494 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19495 cp_parser_simulate_error (parser
);
19496 if (!cp_parser_parse_definitely (parser
))
19499 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19501 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19502 if (cxx_dialect
< cxx17
19503 && !in_system_header_at (ell
->location
))
19504 pedwarn (ell
->location
, 0,
19505 "pack expansion in using-declaration only available "
19506 "with %<-std=c++17%> or %<-std=gnu++17%>");
19507 qscope
= make_pack_expansion (qscope
);
19510 /* The function we call to handle a using-declaration is different
19511 depending on what scope we are in. */
19512 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19514 else if (!identifier_p (identifier
)
19515 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19516 /* [namespace.udecl]
19518 A using declaration shall not name a template-id. */
19519 error_at (token
->location
,
19520 "a template-id may not appear in a using-declaration");
19523 if (at_class_scope_p ())
19525 /* Create the USING_DECL. */
19526 decl
= do_class_using_decl (qscope
, identifier
);
19528 if (decl
&& typename_p
)
19529 USING_DECL_TYPENAME_P (decl
) = 1;
19531 if (check_for_bare_parameter_packs (decl
))
19533 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19537 /* Add it to the list of members in this class. */
19538 finish_member_declaration (decl
);
19542 decl
= cp_parser_lookup_name_simple (parser
,
19545 if (decl
== error_mark_node
)
19546 cp_parser_name_lookup_error (parser
, identifier
,
19549 else if (check_for_bare_parameter_packs (decl
))
19551 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19554 else if (!at_namespace_scope_p ())
19555 finish_local_using_decl (decl
, qscope
, identifier
);
19557 finish_namespace_using_decl (decl
, qscope
, identifier
);
19561 if (!access_declaration_p
19562 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19564 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19565 if (cxx_dialect
< cxx17
)
19566 pedwarn (comma
->location
, 0,
19567 "comma-separated list in using-declaration only available "
19568 "with %<-std=c++17%> or %<-std=gnu++17%>");
19572 /* Look for the final `;'. */
19573 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19575 if (access_declaration_p
&& errorcount
== oldcount
)
19576 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19577 "access declarations are deprecated "
19578 "in favour of using-declarations; "
19579 "suggestion: add the %<using%> keyword");
19584 /* Parse an alias-declaration.
19587 using identifier attribute-specifier-seq [opt] = type-id */
19590 cp_parser_alias_declaration (cp_parser
* parser
)
19592 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19593 location_t id_location
, type_location
;
19594 cp_declarator
*declarator
;
19595 cp_decl_specifier_seq decl_specs
;
19597 const char *saved_message
= NULL
;
19599 /* Look for the `using' keyword. */
19600 cp_token
*using_token
19601 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19602 if (using_token
== NULL
)
19603 return error_mark_node
;
19605 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19606 id
= cp_parser_identifier (parser
);
19607 if (id
== error_mark_node
)
19608 return error_mark_node
;
19610 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19611 attributes
= cp_parser_attributes_opt (parser
);
19612 if (attributes
== error_mark_node
)
19613 return error_mark_node
;
19615 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19617 if (cp_parser_error_occurred (parser
))
19618 return error_mark_node
;
19620 cp_parser_commit_to_tentative_parse (parser
);
19622 /* Now we are going to parse the type-id of the declaration. */
19627 "A type-specifier-seq shall not define a class or enumeration
19628 unless it appears in the type-id of an alias-declaration (7.1.3) that
19629 is not the declaration of a template-declaration."
19631 In other words, if we currently are in an alias template, the
19632 type-id should not define a type.
19634 So let's set parser->type_definition_forbidden_message in that
19635 case; cp_parser_check_type_definition (called by
19636 cp_parser_class_specifier) will then emit an error if a type is
19637 defined in the type-id. */
19638 if (parser
->num_template_parameter_lists
)
19640 saved_message
= parser
->type_definition_forbidden_message
;
19641 parser
->type_definition_forbidden_message
=
19642 G_("types may not be defined in alias template declarations");
19645 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
19648 /* Restore the error message if need be. */
19649 if (parser
->num_template_parameter_lists
)
19650 parser
->type_definition_forbidden_message
= saved_message
;
19652 if (type
== error_mark_node
19653 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19655 cp_parser_skip_to_end_of_block_or_statement (parser
);
19656 return error_mark_node
;
19659 /* A typedef-name can also be introduced by an alias-declaration. The
19660 identifier following the using keyword becomes a typedef-name. It has
19661 the same semantics as if it were introduced by the typedef
19662 specifier. In particular, it does not define a new type and it shall
19663 not appear in the type-id. */
19665 clear_decl_specs (&decl_specs
);
19666 decl_specs
.type
= type
;
19667 if (attributes
!= NULL_TREE
)
19669 decl_specs
.attributes
= attributes
;
19670 set_and_check_decl_spec_loc (&decl_specs
,
19674 set_and_check_decl_spec_loc (&decl_specs
,
19677 set_and_check_decl_spec_loc (&decl_specs
,
19680 decl_specs
.locations
[ds_type_spec
] = type_location
;
19682 if (parser
->num_template_parameter_lists
19683 && !cp_parser_check_template_parameters (parser
,
19684 /*num_templates=*/0,
19685 /*template_id*/false,
19687 /*declarator=*/NULL
))
19688 return error_mark_node
;
19690 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19692 member_p
= at_class_scope_p ();
19694 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19695 NULL_TREE
, attributes
);
19697 decl
= start_decl (declarator
, &decl_specs
, 0,
19698 attributes
, NULL_TREE
, &pushed_scope
);
19699 if (decl
== error_mark_node
)
19702 // Attach constraints to the alias declaration.
19703 if (flag_concepts
&& current_template_parms
)
19705 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19706 tree constr
= build_constraints (reqs
, NULL_TREE
);
19707 set_constraints (decl
, constr
);
19710 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19713 pop_scope (pushed_scope
);
19715 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19716 added into the symbol table; otherwise, return the TYPE_DECL. */
19717 if (DECL_LANG_SPECIFIC (decl
)
19718 && DECL_TEMPLATE_INFO (decl
)
19719 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19721 decl
= DECL_TI_TEMPLATE (decl
);
19723 check_member_template (decl
);
19729 /* Parse a using-directive.
19732 using namespace :: [opt] nested-name-specifier [opt]
19733 namespace-name ; */
19736 cp_parser_using_directive (cp_parser
* parser
)
19738 tree namespace_decl
;
19741 /* Look for the `using' keyword. */
19742 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19743 /* And the `namespace' keyword. */
19744 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19745 /* Look for the optional `::' operator. */
19746 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19747 /* And the optional nested-name-specifier. */
19748 cp_parser_nested_name_specifier_opt (parser
,
19749 /*typename_keyword_p=*/false,
19750 /*check_dependency_p=*/true,
19752 /*is_declaration=*/true);
19753 /* Get the namespace being used. */
19754 namespace_decl
= cp_parser_namespace_name (parser
);
19755 /* And any specified attributes. */
19756 attribs
= cp_parser_attributes_opt (parser
);
19758 /* Update the symbol table. */
19759 if (namespace_bindings_p ())
19760 finish_namespace_using_directive (namespace_decl
, attribs
);
19762 finish_local_using_directive (namespace_decl
, attribs
);
19764 /* Look for the final `;'. */
19765 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19768 /* Parse an asm-definition.
19775 asm-qualifier-list:
19777 asm-qualifier-list asm-qualifier
19780 asm ( string-literal ) ;
19785 asm asm-qualifier-list [opt] ( string-literal ) ;
19786 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19787 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19788 : asm-operand-list [opt] ) ;
19789 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19790 : asm-operand-list [opt]
19791 : asm-clobber-list [opt] ) ;
19792 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19793 : asm-clobber-list [opt]
19794 : asm-goto-list ) ;
19796 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19797 contains goto, and is the only allowed form in that case. No duplicates are
19798 allowed in an asm-qualifier-list. */
19801 cp_parser_asm_definition (cp_parser
* parser
)
19804 tree outputs
= NULL_TREE
;
19805 tree inputs
= NULL_TREE
;
19806 tree clobbers
= NULL_TREE
;
19807 tree labels
= NULL_TREE
;
19809 bool extended_p
= false;
19810 bool invalid_inputs_p
= false;
19811 bool invalid_outputs_p
= false;
19812 required_token missing
= RT_NONE
;
19814 /* Look for the `asm' keyword. */
19815 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19817 if (parser
->in_function_body
19818 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19820 error ("%<asm%> in %<constexpr%> function");
19821 cp_function_chain
->invalid_constexpr
= true;
19824 /* Handle the asm-qualifier-list. */
19825 location_t volatile_loc
= UNKNOWN_LOCATION
;
19826 location_t inline_loc
= UNKNOWN_LOCATION
;
19827 location_t goto_loc
= UNKNOWN_LOCATION
;
19828 location_t first_loc
= UNKNOWN_LOCATION
;
19830 if (cp_parser_allow_gnu_extensions_p (parser
))
19833 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19834 location_t loc
= token
->location
;
19835 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
19840 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19841 inform (volatile_loc
, "first seen here");
19845 if (!parser
->in_function_body
)
19846 warning_at (loc
, 0, "asm qualifier %qT ignored outside of "
19847 "function body", token
->u
.value
);
19848 volatile_loc
= loc
;
19850 cp_lexer_consume_token (parser
->lexer
);
19856 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19857 inform (inline_loc
, "first seen here");
19863 cp_lexer_consume_token (parser
->lexer
);
19869 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19870 inform (goto_loc
, "first seen here");
19876 cp_lexer_consume_token (parser
->lexer
);
19881 error_at (loc
, "%qT is not an asm qualifier", token
->u
.value
);
19882 cp_lexer_consume_token (parser
->lexer
);
19891 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
19892 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
19893 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
19895 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
19897 error_at (first_loc
, "asm qualifier outside of function body");
19898 inline_p
= goto_p
= false;
19901 /* Look for the opening `('. */
19902 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19904 /* Look for the string. */
19905 string
= cp_parser_string_literal (parser
, false, false);
19906 if (string
== error_mark_node
)
19908 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19909 /*consume_paren=*/true);
19913 /* If we're allowing GNU extensions, check for the extended assembly
19914 syntax. Unfortunately, the `:' tokens need not be separated by
19915 a space in C, and so, for compatibility, we tolerate that here
19916 too. Doing that means that we have to treat the `::' operator as
19918 if (cp_parser_allow_gnu_extensions_p (parser
)
19919 && parser
->in_function_body
19920 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19921 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19923 bool inputs_p
= false;
19924 bool clobbers_p
= false;
19925 bool labels_p
= false;
19927 /* The extended syntax was used. */
19930 /* Look for outputs. */
19931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19933 /* Consume the `:'. */
19934 cp_lexer_consume_token (parser
->lexer
);
19935 /* Parse the output-operands. */
19936 if (cp_lexer_next_token_is_not (parser
->lexer
,
19938 && cp_lexer_next_token_is_not (parser
->lexer
,
19940 && cp_lexer_next_token_is_not (parser
->lexer
,
19944 outputs
= cp_parser_asm_operand_list (parser
);
19945 if (outputs
== error_mark_node
)
19946 invalid_outputs_p
= true;
19949 /* If the next token is `::', there are no outputs, and the
19950 next token is the beginning of the inputs. */
19951 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19952 /* The inputs are coming next. */
19955 /* Look for inputs. */
19957 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19959 /* Consume the `:' or `::'. */
19960 cp_lexer_consume_token (parser
->lexer
);
19961 /* Parse the output-operands. */
19962 if (cp_lexer_next_token_is_not (parser
->lexer
,
19964 && cp_lexer_next_token_is_not (parser
->lexer
,
19966 && cp_lexer_next_token_is_not (parser
->lexer
,
19969 inputs
= cp_parser_asm_operand_list (parser
);
19970 if (inputs
== error_mark_node
)
19971 invalid_inputs_p
= true;
19974 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19975 /* The clobbers are coming next. */
19978 /* Look for clobbers. */
19980 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19983 /* Consume the `:' or `::'. */
19984 cp_lexer_consume_token (parser
->lexer
);
19985 /* Parse the clobbers. */
19986 if (cp_lexer_next_token_is_not (parser
->lexer
,
19988 && cp_lexer_next_token_is_not (parser
->lexer
,
19990 clobbers
= cp_parser_asm_clobber_list (parser
);
19992 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19993 /* The labels are coming next. */
19996 /* Look for labels. */
19998 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
20001 /* Consume the `:' or `::'. */
20002 cp_lexer_consume_token (parser
->lexer
);
20003 /* Parse the labels. */
20004 labels
= cp_parser_asm_label_list (parser
);
20007 if (goto_p
&& !labels_p
)
20008 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
20011 missing
= RT_COLON_SCOPE
;
20013 /* Look for the closing `)'. */
20014 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
20015 missing
? missing
: RT_CLOSE_PAREN
))
20016 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
20017 /*consume_paren=*/true);
20018 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20020 if (!invalid_inputs_p
&& !invalid_outputs_p
)
20022 /* Create the ASM_EXPR. */
20023 if (parser
->in_function_body
)
20025 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
20026 inputs
, clobbers
, labels
, inline_p
);
20027 /* If the extended syntax was not used, mark the ASM_EXPR. */
20030 tree temp
= asm_stmt
;
20031 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
20032 temp
= TREE_OPERAND (temp
, 0);
20034 ASM_INPUT_P (temp
) = 1;
20038 symtab
->finalize_toplevel_asm (string
);
20042 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20043 type that comes from the decl-specifier-seq. */
20046 strip_declarator_types (tree type
, cp_declarator
*declarator
)
20048 for (cp_declarator
*d
= declarator
; d
;)
20058 if (TYPE_PTRMEMFUNC_P (type
))
20059 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
20060 type
= TREE_TYPE (type
);
20068 /* Declarators [gram.dcl.decl] */
20070 /* Parse an init-declarator.
20073 declarator initializer [opt]
20078 declarator asm-specification [opt] attributes [opt] initializer [opt]
20080 function-definition:
20081 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20083 decl-specifier-seq [opt] declarator function-try-block
20087 function-definition:
20088 __extension__ function-definition
20092 function-definition:
20093 decl-specifier-seq [opt] declarator function-transaction-block
20095 The parser flags FLAGS is used to control type-specifier parsing.
20097 The DECL_SPECIFIERS apply to this declarator. Returns a
20098 representation of the entity declared. If MEMBER_P is TRUE, then
20099 this declarator appears in a class scope. The new DECL created by
20100 this declarator is returned.
20102 The CHECKS are access checks that should be performed once we know
20103 what entity is being declared (and, therefore, what classes have
20106 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20107 for a function-definition here as well. If the declarator is a
20108 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20109 be TRUE upon return. By that point, the function-definition will
20110 have been completely parsed.
20112 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20115 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20116 parsed declaration if it is an uninitialized single declarator not followed
20117 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20118 if present, will not be consumed. If returned, this declarator will be
20119 created with SD_INITIALIZED but will not call cp_finish_decl.
20121 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20122 and there is an initializer, the pointed location_t is set to the
20123 location of the '=' or `(', or '{' in C++11 token introducing the
20127 cp_parser_init_declarator (cp_parser
* parser
,
20128 cp_parser_flags flags
,
20129 cp_decl_specifier_seq
*decl_specifiers
,
20130 vec
<deferred_access_check
, va_gc
> *checks
,
20131 bool function_definition_allowed_p
,
20133 int declares_class_or_enum
,
20134 bool* function_definition_p
,
20135 tree
* maybe_range_for_decl
,
20136 location_t
* init_loc
,
20139 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20140 *attributes_start_token
= NULL
;
20141 cp_declarator
*declarator
;
20142 tree prefix_attributes
;
20143 tree attributes
= NULL
;
20144 tree asm_specification
;
20146 tree decl
= NULL_TREE
;
20148 int is_initialized
;
20149 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20150 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20152 enum cpp_ttype initialization_kind
;
20153 bool is_direct_init
= false;
20154 bool is_non_constant_init
;
20155 int ctor_dtor_or_conv_p
;
20156 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20157 tree pushed_scope
= NULL_TREE
;
20158 bool range_for_decl_p
= false;
20159 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20160 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20162 /* Gather the attributes that were provided with the
20163 decl-specifiers. */
20164 prefix_attributes
= decl_specifiers
->attributes
;
20166 /* Assume that this is not the declarator for a function
20168 if (function_definition_p
)
20169 *function_definition_p
= false;
20171 /* Default arguments are only permitted for function parameters. */
20172 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20173 parser
->default_arg_ok_p
= false;
20175 /* Defer access checks while parsing the declarator; we cannot know
20176 what names are accessible until we know what is being
20178 resume_deferring_access_checks ();
20180 token
= cp_lexer_peek_token (parser
->lexer
);
20182 /* Parse the declarator. */
20184 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20185 flags
, &ctor_dtor_or_conv_p
,
20186 /*parenthesized_p=*/NULL
,
20187 member_p
, friend_p
, /*static_p=*/false);
20188 /* Gather up the deferred checks. */
20189 stop_deferring_access_checks ();
20191 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20193 /* If the DECLARATOR was erroneous, there's no need to go
20195 if (declarator
== cp_error_declarator
)
20196 return error_mark_node
;
20198 /* Check that the number of template-parameter-lists is OK. */
20199 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20201 return error_mark_node
;
20203 if (declares_class_or_enum
& 2)
20204 cp_parser_check_for_definition_in_return_type (declarator
,
20205 decl_specifiers
->type
,
20206 decl_specifiers
->locations
[ds_type_spec
]);
20208 /* Figure out what scope the entity declared by the DECLARATOR is
20209 located in. `grokdeclarator' sometimes changes the scope, so
20210 we compute it now. */
20211 scope
= get_scope_of_declarator (declarator
);
20213 /* Perform any lookups in the declared type which were thought to be
20214 dependent, but are not in the scope of the declarator. */
20215 decl_specifiers
->type
20216 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20218 /* If we're allowing GNU extensions, look for an
20219 asm-specification. */
20220 if (cp_parser_allow_gnu_extensions_p (parser
))
20222 /* Look for an asm-specification. */
20223 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20224 asm_specification
= cp_parser_asm_specification_opt (parser
);
20227 asm_specification
= NULL_TREE
;
20229 /* Look for attributes. */
20230 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20231 attributes
= cp_parser_attributes_opt (parser
);
20233 /* Peek at the next token. */
20234 token
= cp_lexer_peek_token (parser
->lexer
);
20236 bool bogus_implicit_tmpl
= false;
20238 if (function_declarator_p (declarator
))
20240 /* Handle C++17 deduction guides. */
20241 if (!decl_specifiers
->type
20242 && ctor_dtor_or_conv_p
<= 0
20243 && cxx_dialect
>= cxx17
)
20245 cp_declarator
*id
= get_id_declarator (declarator
);
20246 tree name
= id
->u
.id
.unqualified_name
;
20247 parser
->scope
= id
->u
.id
.qualifying_scope
;
20248 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20250 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20251 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20253 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20254 id
->u
.id
.sfk
= sfk_deduction_guide
;
20255 ctor_dtor_or_conv_p
= 1;
20259 /* Check to see if the token indicates the start of a
20260 function-definition. */
20261 if (cp_parser_token_starts_function_definition_p (token
))
20263 if (!function_definition_allowed_p
)
20265 /* If a function-definition should not appear here, issue an
20267 cp_parser_error (parser
,
20268 "a function-definition is not allowed here");
20269 return error_mark_node
;
20272 location_t func_brace_location
20273 = cp_lexer_peek_token (parser
->lexer
)->location
;
20275 /* Neither attributes nor an asm-specification are allowed
20276 on a function-definition. */
20277 if (asm_specification
)
20278 error_at (asm_spec_start_token
->location
,
20279 "an asm-specification is not allowed "
20280 "on a function-definition");
20282 error_at (attributes_start_token
->location
,
20283 "attributes are not allowed "
20284 "on a function-definition");
20285 /* This is a function-definition. */
20286 *function_definition_p
= true;
20288 /* Parse the function definition. */
20290 decl
= cp_parser_save_member_function_body (parser
,
20293 prefix_attributes
);
20296 (cp_parser_function_definition_from_specifiers_and_declarator
20297 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20299 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20301 /* This is where the prologue starts... */
20302 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20303 = func_brace_location
;
20309 else if (parser
->fully_implicit_function_template_p
)
20311 /* A non-template declaration involving a function parameter list
20312 containing an implicit template parameter will be made into a
20313 template. If the resulting declaration is not going to be an
20314 actual function then finish the template scope here to prevent it.
20315 An error message will be issued once we have a decl to talk about.
20317 FIXME probably we should do type deduction rather than create an
20318 implicit template, but the standard currently doesn't allow it. */
20319 bogus_implicit_tmpl
= true;
20320 finish_fully_implicit_template (parser
, NULL_TREE
);
20325 Only in function declarations for constructors, destructors, type
20326 conversions, and deduction guides can the decl-specifier-seq be omitted.
20328 We explicitly postpone this check past the point where we handle
20329 function-definitions because we tolerate function-definitions
20330 that are missing their return types in some modes. */
20331 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20333 cp_parser_error (parser
,
20334 "expected constructor, destructor, or type conversion");
20335 return error_mark_node
;
20338 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20339 if (token
->type
== CPP_EQ
20340 || token
->type
== CPP_OPEN_PAREN
20341 || token
->type
== CPP_OPEN_BRACE
)
20343 is_initialized
= SD_INITIALIZED
;
20344 initialization_kind
= token
->type
;
20345 if (maybe_range_for_decl
)
20346 *maybe_range_for_decl
= error_mark_node
;
20347 tmp_init_loc
= token
->location
;
20348 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20349 *init_loc
= tmp_init_loc
;
20351 if (token
->type
== CPP_EQ
20352 && function_declarator_p (declarator
))
20354 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20355 if (t2
->keyword
== RID_DEFAULT
)
20356 is_initialized
= SD_DEFAULTED
;
20357 else if (t2
->keyword
== RID_DELETE
)
20358 is_initialized
= SD_DELETED
;
20363 /* If the init-declarator isn't initialized and isn't followed by a
20364 `,' or `;', it's not a valid init-declarator. */
20365 if (token
->type
!= CPP_COMMA
20366 && token
->type
!= CPP_SEMICOLON
)
20368 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20369 range_for_decl_p
= true;
20372 if (!maybe_range_for_decl
)
20373 cp_parser_error (parser
, "expected initializer");
20374 return error_mark_node
;
20377 is_initialized
= SD_UNINITIALIZED
;
20378 initialization_kind
= CPP_EOF
;
20381 /* Because start_decl has side-effects, we should only call it if we
20382 know we're going ahead. By this point, we know that we cannot
20383 possibly be looking at any other construct. */
20384 cp_parser_commit_to_tentative_parse (parser
);
20386 /* Enter the newly declared entry in the symbol table. If we're
20387 processing a declaration in a class-specifier, we wait until
20388 after processing the initializer. */
20391 if (parser
->in_unbraced_linkage_specification_p
)
20392 decl_specifiers
->storage_class
= sc_extern
;
20393 decl
= start_decl (declarator
, decl_specifiers
,
20394 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20395 attributes
, prefix_attributes
, &pushed_scope
);
20396 cp_finalize_omp_declare_simd (parser
, decl
);
20397 cp_finalize_oacc_routine (parser
, decl
, false);
20398 /* Adjust location of decl if declarator->id_loc is more appropriate:
20399 set, and decl wasn't merged with another decl, in which case its
20400 location would be different from input_location, and more accurate. */
20402 && declarator
->id_loc
!= UNKNOWN_LOCATION
20403 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20404 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20407 /* Enter the SCOPE. That way unqualified names appearing in the
20408 initializer will be looked up in SCOPE. */
20409 pushed_scope
= push_scope (scope
);
20411 /* Perform deferred access control checks, now that we know in which
20412 SCOPE the declared entity resides. */
20413 if (!member_p
&& decl
)
20415 tree saved_current_function_decl
= NULL_TREE
;
20417 /* If the entity being declared is a function, pretend that we
20418 are in its scope. If it is a `friend', it may have access to
20419 things that would not otherwise be accessible. */
20420 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20422 saved_current_function_decl
= current_function_decl
;
20423 current_function_decl
= decl
;
20426 /* Perform access checks for template parameters. */
20427 cp_parser_perform_template_parameter_access_checks (checks
);
20429 /* Perform the access control checks for the declarator and the
20430 decl-specifiers. */
20431 perform_deferred_access_checks (tf_warning_or_error
);
20433 /* Restore the saved value. */
20434 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20435 current_function_decl
= saved_current_function_decl
;
20438 /* Parse the initializer. */
20439 initializer
= NULL_TREE
;
20440 is_direct_init
= false;
20441 is_non_constant_init
= true;
20442 if (is_initialized
)
20444 if (function_declarator_p (declarator
))
20446 if (initialization_kind
== CPP_EQ
)
20447 initializer
= cp_parser_pure_specifier (parser
);
20450 /* If the declaration was erroneous, we don't really
20451 know what the user intended, so just silently
20452 consume the initializer. */
20453 if (decl
!= error_mark_node
)
20454 error_at (tmp_init_loc
, "initializer provided for function");
20455 cp_parser_skip_to_closing_parenthesis (parser
,
20456 /*recovering=*/true,
20457 /*or_comma=*/false,
20458 /*consume_paren=*/true);
20463 /* We want to record the extra mangling scope for in-class
20464 initializers of class members and initializers of static data
20465 member templates. The former involves deferring
20466 parsing of the initializer until end of class as with default
20467 arguments. So right here we only handle the latter. */
20468 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20469 start_lambda_scope (decl
);
20470 initializer
= cp_parser_initializer (parser
,
20472 &is_non_constant_init
);
20473 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20474 finish_lambda_scope ();
20475 if (initializer
== error_mark_node
)
20476 cp_parser_skip_to_end_of_statement (parser
);
20480 /* The old parser allows attributes to appear after a parenthesized
20481 initializer. Mark Mitchell proposed removing this functionality
20482 on the GCC mailing lists on 2002-08-13. This parser accepts the
20483 attributes -- but ignores them. Made a permerror in GCC 8. */
20484 if (cp_parser_allow_gnu_extensions_p (parser
)
20485 && initialization_kind
== CPP_OPEN_PAREN
20486 && cp_parser_attributes_opt (parser
)
20487 && permerror (input_location
,
20488 "attributes after parenthesized initializer ignored"))
20491 if (flag_permissive
&& !hint
)
20494 inform (input_location
,
20495 "this flexibility is deprecated and will be removed");
20499 /* And now complain about a non-function implicit template. */
20500 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20501 error_at (DECL_SOURCE_LOCATION (decl
),
20502 "non-function %qD declared as implicit template", decl
);
20504 /* For an in-class declaration, use `grokfield' to create the
20510 pop_scope (pushed_scope
);
20511 pushed_scope
= NULL_TREE
;
20513 decl
= grokfield (declarator
, decl_specifiers
,
20514 initializer
, !is_non_constant_init
,
20515 /*asmspec=*/NULL_TREE
,
20516 attr_chainon (attributes
, prefix_attributes
));
20517 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20518 cp_parser_save_default_args (parser
, decl
);
20519 cp_finalize_omp_declare_simd (parser
, decl
);
20520 cp_finalize_oacc_routine (parser
, decl
, false);
20523 /* Finish processing the declaration. But, skip member
20525 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20527 cp_finish_decl (decl
,
20528 initializer
, !is_non_constant_init
,
20530 /* If the initializer is in parentheses, then this is
20531 a direct-initialization, which means that an
20532 `explicit' constructor is OK. Otherwise, an
20533 `explicit' constructor cannot be used. */
20534 ((is_direct_init
|| !is_initialized
)
20535 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20537 else if ((cxx_dialect
!= cxx98
) && friend_p
20538 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20539 /* Core issue #226 (C++0x only): A default template-argument
20540 shall not be specified in a friend class template
20542 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20543 /*is_partial=*/false, /*is_friend_decl=*/1);
20545 if (!friend_p
&& pushed_scope
)
20546 pop_scope (pushed_scope
);
20548 if (function_declarator_p (declarator
)
20549 && parser
->fully_implicit_function_template_p
)
20552 decl
= finish_fully_implicit_template (parser
, decl
);
20554 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20557 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20558 && type_uses_auto (decl_specifiers
->type
))
20559 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20564 /* Parse a declarator.
20568 ptr-operator declarator
20570 abstract-declarator:
20571 ptr-operator abstract-declarator [opt]
20572 direct-abstract-declarator
20577 attributes [opt] direct-declarator
20578 attributes [opt] ptr-operator declarator
20580 abstract-declarator:
20581 attributes [opt] ptr-operator abstract-declarator [opt]
20582 attributes [opt] direct-abstract-declarator
20584 The parser flags FLAGS is used to control type-specifier parsing.
20586 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20587 detect constructors, destructors, deduction guides, or conversion operators.
20588 It is set to -1 if the declarator is a name, and +1 if it is a
20589 function. Otherwise it is set to zero. Usually you just want to
20590 test for >0, but internally the negative value is used.
20592 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20593 a decl-specifier-seq unless it declares a constructor, destructor,
20594 or conversion. It might seem that we could check this condition in
20595 semantic analysis, rather than parsing, but that makes it difficult
20596 to handle something like `f()'. We want to notice that there are
20597 no decl-specifiers, and therefore realize that this is an
20598 expression, not a declaration.)
20600 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20601 the declarator is a direct-declarator of the form "(...)".
20603 MEMBER_P is true iff this declarator is a member-declarator.
20605 FRIEND_P is true iff this declarator is a friend.
20607 STATIC_P is true iff the keyword static was seen. */
20609 static cp_declarator
*
20610 cp_parser_declarator (cp_parser
* parser
,
20611 cp_parser_declarator_kind dcl_kind
,
20612 cp_parser_flags flags
,
20613 int* ctor_dtor_or_conv_p
,
20614 bool* parenthesized_p
,
20615 bool member_p
, bool friend_p
, bool static_p
)
20617 cp_declarator
*declarator
;
20618 enum tree_code code
;
20619 cp_cv_quals cv_quals
;
20621 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20623 /* Assume this is not a constructor, destructor, or type-conversion
20625 if (ctor_dtor_or_conv_p
)
20626 *ctor_dtor_or_conv_p
= 0;
20628 if (cp_parser_allow_gnu_extensions_p (parser
))
20629 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20631 /* Check for the ptr-operator production. */
20632 cp_parser_parse_tentatively (parser
);
20633 /* Parse the ptr-operator. */
20634 code
= cp_parser_ptr_operator (parser
,
20639 /* If that worked, then we have a ptr-operator. */
20640 if (cp_parser_parse_definitely (parser
))
20642 /* If a ptr-operator was found, then this declarator was not
20644 if (parenthesized_p
)
20645 *parenthesized_p
= true;
20646 /* The dependent declarator is optional if we are parsing an
20647 abstract-declarator. */
20648 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20649 cp_parser_parse_tentatively (parser
);
20651 /* Parse the dependent declarator. */
20652 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20653 CP_PARSER_FLAGS_NONE
,
20654 /*ctor_dtor_or_conv_p=*/NULL
,
20655 /*parenthesized_p=*/NULL
,
20656 /*member_p=*/false,
20657 friend_p
, /*static_p=*/false);
20659 /* If we are parsing an abstract-declarator, we must handle the
20660 case where the dependent declarator is absent. */
20661 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20662 && !cp_parser_parse_definitely (parser
))
20665 declarator
= cp_parser_make_indirect_declarator
20666 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20668 /* Everything else is a direct-declarator. */
20671 if (parenthesized_p
)
20672 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20674 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20675 flags
, ctor_dtor_or_conv_p
,
20676 member_p
, friend_p
, static_p
);
20679 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20680 declarator
->attributes
= gnu_attributes
;
20684 /* Parse a direct-declarator or direct-abstract-declarator.
20688 direct-declarator ( parameter-declaration-clause )
20689 cv-qualifier-seq [opt]
20690 ref-qualifier [opt]
20691 exception-specification [opt]
20692 direct-declarator [ constant-expression [opt] ]
20695 direct-abstract-declarator:
20696 direct-abstract-declarator [opt]
20697 ( parameter-declaration-clause )
20698 cv-qualifier-seq [opt]
20699 ref-qualifier [opt]
20700 exception-specification [opt]
20701 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20702 ( abstract-declarator )
20704 Returns a representation of the declarator. DCL_KIND is
20705 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20706 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20707 we are parsing a direct-declarator. It is
20708 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20709 of ambiguity we prefer an abstract declarator, as per
20711 The parser flags FLAGS is used to control type-specifier parsing.
20712 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20713 as for cp_parser_declarator. */
20715 static cp_declarator
*
20716 cp_parser_direct_declarator (cp_parser
* parser
,
20717 cp_parser_declarator_kind dcl_kind
,
20718 cp_parser_flags flags
,
20719 int* ctor_dtor_or_conv_p
,
20720 bool member_p
, bool friend_p
, bool static_p
)
20723 cp_declarator
*declarator
= NULL
;
20724 tree scope
= NULL_TREE
;
20725 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20726 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20728 tree pushed_scope
= NULL_TREE
;
20729 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20733 /* Peek at the next token. */
20734 token
= cp_lexer_peek_token (parser
->lexer
);
20735 if (token
->type
== CPP_OPEN_PAREN
)
20737 /* This is either a parameter-declaration-clause, or a
20738 parenthesized declarator. When we know we are parsing a
20739 named declarator, it must be a parenthesized declarator
20740 if FIRST is true. For instance, `(int)' is a
20741 parameter-declaration-clause, with an omitted
20742 direct-abstract-declarator. But `((*))', is a
20743 parenthesized abstract declarator. Finally, when T is a
20744 template parameter `(T)' is a
20745 parameter-declaration-clause, and not a parenthesized
20748 We first try and parse a parameter-declaration-clause,
20749 and then try a nested declarator (if FIRST is true).
20751 It is not an error for it not to be a
20752 parameter-declaration-clause, even when FIRST is
20758 The first is the declaration of a function while the
20759 second is the definition of a variable, including its
20762 Having seen only the parenthesis, we cannot know which of
20763 these two alternatives should be selected. Even more
20764 complex are examples like:
20769 The former is a function-declaration; the latter is a
20770 variable initialization.
20772 Thus again, we try a parameter-declaration-clause, and if
20773 that fails, we back out and return. */
20775 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20778 bool is_declarator
= false;
20782 /* In a member-declarator, the only valid interpretation
20783 of a parenthesis is the start of a
20784 parameter-declaration-clause. (It is invalid to
20785 initialize a static data member with a parenthesized
20786 initializer; only the "=" form of initialization is
20789 cp_parser_parse_tentatively (parser
);
20791 /* Consume the `('. */
20792 matching_parens parens
;
20793 parens
.consume_open (parser
);
20796 /* If this is going to be an abstract declarator, we're
20797 in a declarator and we can't have default args. */
20798 parser
->default_arg_ok_p
= false;
20799 parser
->in_declarator_p
= true;
20802 begin_scope (sk_function_parms
, NULL_TREE
);
20804 /* Parse the parameter-declaration-clause. */
20806 = cp_parser_parameter_declaration_clause (parser
, flags
);
20808 /* Consume the `)'. */
20809 parens
.require_close (parser
);
20811 /* If all went well, parse the cv-qualifier-seq,
20812 ref-qualifier and the exception-specification. */
20813 if (member_p
|| cp_parser_parse_definitely (parser
))
20815 cp_cv_quals cv_quals
;
20816 cp_virt_specifiers virt_specifiers
;
20817 cp_ref_qualifier ref_qual
;
20818 tree exception_specification
;
20821 bool memfn
= (member_p
|| (pushed_scope
20822 && CLASS_TYPE_P (pushed_scope
)));
20823 unsigned char local_variables_forbidden_p
20824 = parser
->local_variables_forbidden_p
;
20825 /* 'this' is not allowed in static member functions. */
20826 if (static_p
|| friend_p
)
20827 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
20829 is_declarator
= true;
20831 if (ctor_dtor_or_conv_p
)
20832 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20835 /* Parse the cv-qualifier-seq. */
20836 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20837 /* Parse the ref-qualifier. */
20838 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20839 /* Parse the tx-qualifier. */
20840 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20841 /* And the exception-specification. */
20842 exception_specification
20843 = cp_parser_exception_specification_opt (parser
);
20845 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20847 /* In here, we handle cases where attribute is used after
20848 the function declaration. For example:
20849 void func (int x) __attribute__((vector(..))); */
20850 tree gnu_attrs
= NULL_TREE
;
20851 tree requires_clause
= NULL_TREE
;
20852 late_return
= (cp_parser_late_return_type_opt
20853 (parser
, declarator
, requires_clause
,
20854 memfn
? cv_quals
: -1));
20856 /* Parse the virt-specifier-seq. */
20857 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20859 /* Create the function-declarator. */
20860 declarator
= make_call_declarator (declarator
,
20866 exception_specification
,
20869 declarator
->std_attributes
= attrs
;
20870 declarator
->attributes
= gnu_attrs
;
20871 /* Any subsequent parameter lists are to do with
20872 return type, so are not those of the declared
20874 parser
->default_arg_ok_p
= false;
20876 /* Restore the state of local_variables_forbidden_p. */
20877 parser
->local_variables_forbidden_p
20878 = local_variables_forbidden_p
;
20881 /* Remove the function parms from scope. */
20882 pop_bindings_and_leave_scope ();
20885 /* Repeat the main loop. */
20889 /* If this is the first, we can try a parenthesized
20893 bool saved_in_type_id_in_expr_p
;
20895 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20896 parser
->in_declarator_p
= saved_in_declarator_p
;
20898 open_paren
= token
;
20899 /* Consume the `('. */
20900 matching_parens parens
;
20901 parens
.consume_open (parser
);
20902 /* Parse the nested declarator. */
20903 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20904 parser
->in_type_id_in_expr_p
= true;
20906 = cp_parser_declarator (parser
, dcl_kind
, flags
,
20907 ctor_dtor_or_conv_p
,
20908 /*parenthesized_p=*/NULL
,
20909 member_p
, friend_p
,
20910 /*static_p=*/false);
20911 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20913 /* Expect a `)'. */
20914 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20915 if (!parens
.require_close (parser
))
20916 declarator
= cp_error_declarator
;
20917 if (declarator
== cp_error_declarator
)
20920 goto handle_declarator
;
20922 /* Otherwise, we must be done. */
20926 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20927 && token
->type
== CPP_OPEN_SQUARE
20928 && !cp_next_tokens_can_be_attribute_p (parser
))
20930 /* Parse an array-declarator. */
20931 tree bounds
, attrs
;
20933 if (ctor_dtor_or_conv_p
)
20934 *ctor_dtor_or_conv_p
= 0;
20938 parser
->default_arg_ok_p
= false;
20939 parser
->in_declarator_p
= true;
20940 /* Consume the `['. */
20941 cp_lexer_consume_token (parser
->lexer
);
20942 /* Peek at the next token. */
20943 token
= cp_lexer_peek_token (parser
->lexer
);
20944 /* If the next token is `]', then there is no
20945 constant-expression. */
20946 if (token
->type
!= CPP_CLOSE_SQUARE
)
20948 bool non_constant_p
;
20950 = cp_parser_constant_expression (parser
,
20951 /*allow_non_constant=*/true,
20953 if (!non_constant_p
)
20955 else if (error_operand_p (bounds
))
20956 /* Already gave an error. */;
20957 else if (!parser
->in_function_body
20958 || current_binding_level
->kind
== sk_function_parms
)
20960 /* Normally, the array bound must be an integral constant
20961 expression. However, as an extension, we allow VLAs
20962 in function scopes as long as they aren't part of a
20963 parameter declaration. */
20964 cp_parser_error (parser
,
20965 "array bound is not an integer constant");
20966 bounds
= error_mark_node
;
20968 else if (processing_template_decl
20969 && !type_dependent_expression_p (bounds
))
20971 /* Remember this wasn't a constant-expression. */
20972 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20973 TREE_SIDE_EFFECTS (bounds
) = 1;
20977 bounds
= NULL_TREE
;
20978 /* Look for the closing `]'. */
20979 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20981 declarator
= cp_error_declarator
;
20985 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20986 declarator
= make_array_declarator (declarator
, bounds
);
20987 declarator
->std_attributes
= attrs
;
20989 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20992 tree qualifying_scope
;
20993 tree unqualified_name
;
20995 special_function_kind sfk
;
20997 bool pack_expansion_p
= false;
20998 cp_token
*declarator_id_start_token
;
21000 /* Parse a declarator-id */
21001 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
21004 cp_parser_parse_tentatively (parser
);
21006 /* If we see an ellipsis, we should be looking at a
21008 if (token
->type
== CPP_ELLIPSIS
)
21010 /* Consume the `...' */
21011 cp_lexer_consume_token (parser
->lexer
);
21013 pack_expansion_p
= true;
21017 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
21019 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
21020 qualifying_scope
= parser
->scope
;
21025 if (!unqualified_name
&& pack_expansion_p
)
21027 /* Check whether an error occurred. */
21028 okay
= !cp_parser_error_occurred (parser
);
21030 /* We already consumed the ellipsis to mark a
21031 parameter pack, but we have no way to report it,
21032 so abort the tentative parse. We will be exiting
21033 immediately anyway. */
21034 cp_parser_abort_tentative_parse (parser
);
21037 okay
= cp_parser_parse_definitely (parser
);
21040 unqualified_name
= error_mark_node
;
21041 else if (unqualified_name
21042 && (qualifying_scope
21043 || (!identifier_p (unqualified_name
))))
21045 cp_parser_error (parser
, "expected unqualified-id");
21046 unqualified_name
= error_mark_node
;
21050 if (!unqualified_name
)
21052 if (unqualified_name
== error_mark_node
)
21054 declarator
= cp_error_declarator
;
21055 pack_expansion_p
= false;
21056 declarator
->parameter_pack_p
= false;
21060 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21062 if (qualifying_scope
&& at_namespace_scope_p ()
21063 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
21065 /* In the declaration of a member of a template class
21066 outside of the class itself, the SCOPE will sometimes
21067 be a TYPENAME_TYPE. For example, given:
21069 template <typename T>
21070 int S<T>::R::i = 3;
21072 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21073 this context, we must resolve S<T>::R to an ordinary
21074 type, rather than a typename type.
21076 The reason we normally avoid resolving TYPENAME_TYPEs
21077 is that a specialization of `S' might render
21078 `S<T>::R' not a type. However, if `S' is
21079 specialized, then this `i' will not be used, so there
21080 is no harm in resolving the types here. */
21083 /* Resolve the TYPENAME_TYPE. */
21084 type
= resolve_typename_type (qualifying_scope
,
21085 /*only_current_p=*/false);
21086 /* If that failed, the declarator is invalid. */
21087 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21089 if (typedef_variant_p (type
))
21090 error_at (declarator_id_start_token
->location
,
21091 "cannot define member of dependent typedef "
21094 error_at (declarator_id_start_token
->location
,
21095 "%<%T::%E%> is not a type",
21096 TYPE_CONTEXT (qualifying_scope
),
21097 TYPE_IDENTIFIER (qualifying_scope
));
21099 qualifying_scope
= type
;
21104 if (unqualified_name
)
21108 if (qualifying_scope
21109 && CLASS_TYPE_P (qualifying_scope
))
21110 class_type
= qualifying_scope
;
21112 class_type
= current_class_type
;
21114 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21116 tree name_type
= TREE_TYPE (unqualified_name
);
21118 if (!class_type
|| !same_type_p (name_type
, class_type
))
21120 /* We do not attempt to print the declarator
21121 here because we do not have enough
21122 information about its original syntactic
21124 cp_parser_error (parser
, "invalid declarator");
21125 declarator
= cp_error_declarator
;
21128 else if (qualifying_scope
21129 && CLASSTYPE_USE_TEMPLATE (name_type
))
21131 error_at (declarator_id_start_token
->location
,
21132 "invalid use of constructor as a template");
21133 inform (declarator_id_start_token
->location
,
21134 "use %<%T::%D%> instead of %<%T::%D%> to "
21135 "name the constructor in a qualified name",
21137 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21138 class_type
, name_type
);
21139 declarator
= cp_error_declarator
;
21142 unqualified_name
= constructor_name (class_type
);
21147 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21148 sfk
= sfk_destructor
;
21149 else if (identifier_p (unqualified_name
)
21150 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21151 sfk
= sfk_conversion
;
21152 else if (/* There's no way to declare a constructor
21153 for an unnamed type, even if the type
21154 got a name for linkage purposes. */
21155 !TYPE_WAS_UNNAMED (class_type
)
21156 /* Handle correctly (c++/19200):
21170 friend void N::S();
21172 && (!friend_p
|| class_type
== qualifying_scope
)
21173 && constructor_name_p (unqualified_name
,
21175 sfk
= sfk_constructor
;
21176 else if (is_overloaded_fn (unqualified_name
)
21177 && DECL_CONSTRUCTOR_P (get_first_fn
21178 (unqualified_name
)))
21179 sfk
= sfk_constructor
;
21181 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21182 *ctor_dtor_or_conv_p
= -1;
21185 declarator
= make_id_declarator (qualifying_scope
,
21187 sfk
, token
->location
);
21188 declarator
->std_attributes
= attrs
;
21189 declarator
->parameter_pack_p
= pack_expansion_p
;
21191 if (pack_expansion_p
)
21192 maybe_warn_variadic_templates ();
21194 /* We're looking for this case in [temp.res]:
21195 A qualified-id is assumed to name a type if [...]
21196 - it is a decl-specifier of the decl-specifier-seq of a
21197 parameter-declaration in a declarator of a function or
21198 function template declaration, ... */
21199 if (cxx_dialect
>= cxx2a
21200 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
21201 && declarator
->kind
== cdk_id
21202 && !at_class_scope_p ()
21203 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21205 /* ...whose declarator-id is qualified. If it isn't, never
21206 assume the parameters to refer to types. */
21207 if (qualifying_scope
== NULL_TREE
)
21208 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21211 /* Now we have something like
21212 template <typename T> int C::x(S::p);
21213 which can be a function template declaration or a
21214 variable template definition. If name lookup for
21215 the declarator-id C::x finds one or more function
21216 templates, assume S::p to name a type. Otherwise,
21219 = cp_parser_lookup_name_simple (parser
, unqualified_name
,
21221 if (!is_overloaded_fn (decl
)
21223 template<typename T>
21224 A<T>::A(T::type) { } */
21225 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
21226 && constructor_name_p (unqualified_name
,
21227 qualifying_scope
)))
21228 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21233 handle_declarator
:;
21234 scope
= get_scope_of_declarator (declarator
);
21237 /* Any names that appear after the declarator-id for a
21238 member are looked up in the containing scope. */
21239 if (at_function_scope_p ())
21241 /* But declarations with qualified-ids can't appear in a
21243 cp_parser_error (parser
, "qualified-id in declaration");
21244 declarator
= cp_error_declarator
;
21247 pushed_scope
= push_scope (scope
);
21249 parser
->in_declarator_p
= true;
21250 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21251 || (declarator
&& declarator
->kind
== cdk_id
))
21252 /* Default args are only allowed on function
21254 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21256 parser
->default_arg_ok_p
= false;
21265 /* For an abstract declarator, we might wind up with nothing at this
21266 point. That's an error; the declarator is not optional. */
21268 cp_parser_error (parser
, "expected declarator");
21269 else if (open_paren
)
21271 /* Record overly parenthesized declarator so we can give a
21272 diagnostic about confusing decl/expr disambiguation. */
21273 if (declarator
->kind
== cdk_array
)
21275 /* If the open and close parens are on different lines, this
21276 is probably a formatting thing, so ignore. */
21277 expanded_location open
= expand_location (open_paren
->location
);
21278 expanded_location close
= expand_location (close_paren
->location
);
21279 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21283 declarator
->parenthesized
= open_paren
->location
;
21286 /* If we entered a scope, we must exit it now. */
21288 pop_scope (pushed_scope
);
21290 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21291 parser
->in_declarator_p
= saved_in_declarator_p
;
21296 /* Parse a ptr-operator.
21299 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21300 * cv-qualifier-seq [opt]
21302 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21303 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21308 & cv-qualifier-seq [opt]
21310 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21311 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21312 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21313 filled in with the TYPE containing the member. *CV_QUALS is
21314 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21315 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21316 Note that the tree codes returned by this function have nothing
21317 to do with the types of trees that will be eventually be created
21318 to represent the pointer or reference type being parsed. They are
21319 just constants with suggestive names. */
21320 static enum tree_code
21321 cp_parser_ptr_operator (cp_parser
* parser
,
21323 cp_cv_quals
*cv_quals
,
21326 enum tree_code code
= ERROR_MARK
;
21328 tree attrs
= NULL_TREE
;
21330 /* Assume that it's not a pointer-to-member. */
21332 /* And that there are no cv-qualifiers. */
21333 *cv_quals
= TYPE_UNQUALIFIED
;
21335 /* Peek at the next token. */
21336 token
= cp_lexer_peek_token (parser
->lexer
);
21338 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21339 if (token
->type
== CPP_MULT
)
21340 code
= INDIRECT_REF
;
21341 else if (token
->type
== CPP_AND
)
21343 else if ((cxx_dialect
!= cxx98
) &&
21344 token
->type
== CPP_AND_AND
) /* C++0x only */
21345 code
= NON_LVALUE_EXPR
;
21347 if (code
!= ERROR_MARK
)
21349 /* Consume the `*', `&' or `&&'. */
21350 cp_lexer_consume_token (parser
->lexer
);
21352 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21353 `&', if we are allowing GNU extensions. (The only qualifier
21354 that can legally appear after `&' is `restrict', but that is
21355 enforced during semantic analysis. */
21356 if (code
== INDIRECT_REF
21357 || cp_parser_allow_gnu_extensions_p (parser
))
21358 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21360 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21361 if (attributes
!= NULL
)
21362 *attributes
= attrs
;
21366 /* Try the pointer-to-member case. */
21367 cp_parser_parse_tentatively (parser
);
21368 /* Look for the optional `::' operator. */
21369 cp_parser_global_scope_opt (parser
,
21370 /*current_scope_valid_p=*/false);
21371 /* Look for the nested-name specifier. */
21372 token
= cp_lexer_peek_token (parser
->lexer
);
21373 cp_parser_nested_name_specifier (parser
,
21374 /*typename_keyword_p=*/false,
21375 /*check_dependency_p=*/true,
21377 /*is_declaration=*/false);
21378 /* If we found it, and the next token is a `*', then we are
21379 indeed looking at a pointer-to-member operator. */
21380 if (!cp_parser_error_occurred (parser
)
21381 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21383 /* Indicate that the `*' operator was used. */
21384 code
= INDIRECT_REF
;
21386 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21387 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21388 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21389 error_at (token
->location
, "cannot form pointer to member of "
21390 "non-class %q#T", parser
->scope
);
21393 /* The type of which the member is a member is given by the
21395 *type
= parser
->scope
;
21396 /* The next name will not be qualified. */
21397 parser
->scope
= NULL_TREE
;
21398 parser
->qualifying_scope
= NULL_TREE
;
21399 parser
->object_scope
= NULL_TREE
;
21400 /* Look for optional c++11 attributes. */
21401 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21402 if (attributes
!= NULL
)
21403 *attributes
= attrs
;
21404 /* Look for the optional cv-qualifier-seq. */
21405 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21408 /* If that didn't work we don't have a ptr-operator. */
21409 if (!cp_parser_parse_definitely (parser
))
21410 cp_parser_error (parser
, "expected ptr-operator");
21416 /* Parse an (optional) cv-qualifier-seq.
21419 cv-qualifier cv-qualifier-seq [opt]
21430 Returns a bitmask representing the cv-qualifiers. */
21433 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21435 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21440 cp_cv_quals cv_qualifier
;
21442 /* Peek at the next token. */
21443 token
= cp_lexer_peek_token (parser
->lexer
);
21444 /* See if it's a cv-qualifier. */
21445 switch (token
->keyword
)
21448 cv_qualifier
= TYPE_QUAL_CONST
;
21452 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21456 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21460 cv_qualifier
= TYPE_UNQUALIFIED
;
21467 if (cv_quals
& cv_qualifier
)
21469 gcc_rich_location
richloc (token
->location
);
21470 richloc
.add_fixit_remove ();
21471 error_at (&richloc
, "duplicate cv-qualifier");
21472 cp_lexer_purge_token (parser
->lexer
);
21476 cp_lexer_consume_token (parser
->lexer
);
21477 cv_quals
|= cv_qualifier
;
21484 /* Parse an (optional) ref-qualifier
21490 Returns cp_ref_qualifier representing ref-qualifier. */
21492 static cp_ref_qualifier
21493 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21495 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21497 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21498 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21503 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21504 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21506 switch (token
->type
)
21509 curr_ref_qual
= REF_QUAL_LVALUE
;
21513 curr_ref_qual
= REF_QUAL_RVALUE
;
21517 curr_ref_qual
= REF_QUAL_NONE
;
21521 if (!curr_ref_qual
)
21525 error_at (token
->location
, "multiple ref-qualifiers");
21526 cp_lexer_purge_token (parser
->lexer
);
21530 ref_qual
= curr_ref_qual
;
21531 cp_lexer_consume_token (parser
->lexer
);
21538 /* Parse an optional tx-qualifier.
21542 transaction_safe_dynamic */
21545 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21547 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21548 if (token
->type
== CPP_NAME
)
21550 tree name
= token
->u
.value
;
21551 const char *p
= IDENTIFIER_POINTER (name
);
21552 const int len
= strlen ("transaction_safe");
21553 if (!strncmp (p
, "transaction_safe", len
))
21557 || !strcmp (p
, "_dynamic"))
21559 cp_lexer_consume_token (parser
->lexer
);
21562 error ("%qE requires %<-fgnu-tm%>", name
);
21573 /* Parse an (optional) virt-specifier-seq.
21575 virt-specifier-seq:
21576 virt-specifier virt-specifier-seq [opt]
21582 Returns a bitmask representing the virt-specifiers. */
21584 static cp_virt_specifiers
21585 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21587 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21592 cp_virt_specifiers virt_specifier
;
21594 /* Peek at the next token. */
21595 token
= cp_lexer_peek_token (parser
->lexer
);
21596 /* See if it's a virt-specifier-qualifier. */
21597 if (token
->type
!= CPP_NAME
)
21599 if (id_equal (token
->u
.value
, "override"))
21601 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21602 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21604 else if (id_equal (token
->u
.value
, "final"))
21606 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21607 virt_specifier
= VIRT_SPEC_FINAL
;
21609 else if (id_equal (token
->u
.value
, "__final"))
21611 virt_specifier
= VIRT_SPEC_FINAL
;
21616 if (virt_specifiers
& virt_specifier
)
21618 gcc_rich_location
richloc (token
->location
);
21619 richloc
.add_fixit_remove ();
21620 error_at (&richloc
, "duplicate virt-specifier");
21621 cp_lexer_purge_token (parser
->lexer
);
21625 cp_lexer_consume_token (parser
->lexer
);
21626 virt_specifiers
|= virt_specifier
;
21629 return virt_specifiers
;
21632 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21633 is in scope even though it isn't real. */
21636 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21640 if (current_class_ptr
)
21642 /* We don't clear this between NSDMIs. Is it already what we want? */
21643 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21644 if (DECL_P (current_class_ptr
)
21645 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21646 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21647 && cp_type_quals (type
) == quals
)
21651 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21652 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21653 current_class_ptr
= NULL_TREE
;
21655 = cp_build_fold_indirect_ref (this_parm
);
21656 current_class_ptr
= this_parm
;
21659 /* Return true iff our current scope is a non-static data member
21663 parsing_nsdmi (void)
21665 /* We recognize NSDMI context by the context-less 'this' pointer set up
21666 by the function above. */
21667 if (current_class_ptr
21668 && TREE_CODE (current_class_ptr
) == PARM_DECL
21669 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21674 /* Parse a late-specified return type, if any. This is not a separate
21675 non-terminal, but part of a function declarator, which looks like
21677 -> trailing-type-specifier-seq abstract-declarator(opt)
21679 Returns the type indicated by the type-id.
21681 In addition to this, parse any queued up #pragma omp declare simd
21682 clauses, and #pragma acc routine clauses.
21684 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21688 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21689 tree
& requires_clause
, cp_cv_quals quals
)
21692 tree type
= NULL_TREE
;
21693 bool declare_simd_p
= (parser
->omp_declare_simd
21695 && declarator
->kind
== cdk_id
);
21697 bool oacc_routine_p
= (parser
->oacc_routine
21699 && declarator
->kind
== cdk_id
);
21701 /* Peek at the next token. */
21702 token
= cp_lexer_peek_token (parser
->lexer
);
21703 /* A late-specified return type is indicated by an initial '->'. */
21704 if (token
->type
!= CPP_DEREF
21705 && token
->keyword
!= RID_REQUIRES
21706 && !(token
->type
== CPP_NAME
21707 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21708 && !(declare_simd_p
|| oacc_routine_p
))
21711 tree save_ccp
= current_class_ptr
;
21712 tree save_ccr
= current_class_ref
;
21715 /* DR 1207: 'this' is in scope in the trailing return type. */
21716 inject_this_parameter (current_class_type
, quals
);
21719 if (token
->type
== CPP_DEREF
)
21721 /* Consume the ->. */
21722 cp_lexer_consume_token (parser
->lexer
);
21724 type
= cp_parser_trailing_type_id (parser
);
21727 /* Function declarations may be followed by a trailing
21728 requires-clause. */
21729 requires_clause
= cp_parser_requires_clause_opt (parser
);
21731 if (declare_simd_p
)
21732 declarator
->attributes
21733 = cp_parser_late_parsing_omp_declare_simd (parser
,
21734 declarator
->attributes
);
21735 if (oacc_routine_p
)
21736 declarator
->attributes
21737 = cp_parser_late_parsing_oacc_routine (parser
,
21738 declarator
->attributes
);
21742 current_class_ptr
= save_ccp
;
21743 current_class_ref
= save_ccr
;
21749 /* Parse a declarator-id.
21753 :: [opt] nested-name-specifier [opt] type-name
21755 In the `id-expression' case, the value returned is as for
21756 cp_parser_id_expression if the id-expression was an unqualified-id.
21757 If the id-expression was a qualified-id, then a SCOPE_REF is
21758 returned. The first operand is the scope (either a NAMESPACE_DECL
21759 or TREE_TYPE), but the second is still just a representation of an
21763 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21766 /* The expression must be an id-expression. Assume that qualified
21767 names are the names of types so that:
21770 int S<T>::R::i = 3;
21772 will work; we must treat `S<T>::R' as the name of a type.
21773 Similarly, assume that qualified names are templates, where
21777 int S<T>::R<T>::i = 3;
21780 id
= cp_parser_id_expression (parser
,
21781 /*template_keyword_p=*/false,
21782 /*check_dependency_p=*/false,
21783 /*template_p=*/NULL
,
21784 /*declarator_p=*/true,
21786 if (id
&& BASELINK_P (id
))
21787 id
= BASELINK_FUNCTIONS (id
);
21791 /* Parse a type-id.
21794 type-specifier-seq abstract-declarator [opt]
21796 The parser flags FLAGS is used to control type-specifier parsing.
21798 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21800 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21801 i.e. we've just seen "->".
21803 Returns the TYPE specified. */
21806 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
21807 bool is_template_arg
, bool is_trailing_return
,
21808 location_t
*type_location
)
21810 cp_decl_specifier_seq type_specifier_seq
;
21811 cp_declarator
*abstract_declarator
;
21813 /* Parse the type-specifier-seq. */
21814 cp_parser_type_specifier_seq (parser
, flags
,
21815 /*is_declaration=*/false,
21816 is_trailing_return
,
21817 &type_specifier_seq
);
21819 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21821 if (is_template_arg
&& type_specifier_seq
.type
21822 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21823 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21824 /* A bare template name as a template argument is a template template
21825 argument, not a placeholder, so fail parsing it as a type argument. */
21827 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21828 cp_parser_simulate_error (parser
);
21829 return error_mark_node
;
21831 if (type_specifier_seq
.type
== error_mark_node
)
21832 return error_mark_node
;
21834 /* There might or might not be an abstract declarator. */
21835 cp_parser_parse_tentatively (parser
);
21836 /* Look for the declarator. */
21837 abstract_declarator
21838 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
21839 CP_PARSER_FLAGS_NONE
, NULL
,
21840 /*parenthesized_p=*/NULL
,
21841 /*member_p=*/false,
21842 /*friend_p=*/false,
21843 /*static_p=*/false);
21844 /* Check to see if there really was a declarator. */
21845 if (!cp_parser_parse_definitely (parser
))
21846 abstract_declarator
= NULL
;
21848 if (type_specifier_seq
.type
21849 /* The concepts TS allows 'auto' as a type-id. */
21850 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21851 /* None of the valid uses of 'auto' in C++14 involve the type-id
21852 nonterminal, but it is valid in a trailing-return-type. */
21853 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21854 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21856 /* A type-id with type 'auto' is only ok if the abstract declarator
21857 is a function declarator with a late-specified return type.
21859 A type-id with 'auto' is also valid in a trailing-return-type
21860 in a compound-requirement. */
21861 if (abstract_declarator
21862 && abstract_declarator
->kind
== cdk_function
21863 && abstract_declarator
->u
.function
.late_return_type
)
21865 else if (parser
->in_result_type_constraint_p
)
21869 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21870 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21872 error_at (loc
, "missing template arguments after %qT",
21874 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21878 error_at (loc
, "invalid use of %qT", auto_node
);
21879 return error_mark_node
;
21883 return groktypename (&type_specifier_seq
, abstract_declarator
,
21887 /* Wrapper for cp_parser_type_id_1. */
21890 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
21891 location_t
*type_location
)
21893 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
21896 /* Wrapper for cp_parser_type_id_1. */
21899 cp_parser_template_type_arg (cp_parser
*parser
)
21902 const char *saved_message
= parser
->type_definition_forbidden_message
;
21903 parser
->type_definition_forbidden_message
21904 = G_("types may not be defined in template arguments");
21905 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
21906 parser
->type_definition_forbidden_message
= saved_message
;
21907 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21909 error ("invalid use of %<auto%> in template argument");
21910 r
= error_mark_node
;
21915 /* Wrapper for cp_parser_type_id_1. */
21918 cp_parser_trailing_type_id (cp_parser
*parser
)
21920 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21921 false, true, NULL
);
21924 /* Parse a type-specifier-seq.
21926 type-specifier-seq:
21927 type-specifier type-specifier-seq [opt]
21931 type-specifier-seq:
21932 attributes type-specifier-seq [opt]
21934 The parser flags FLAGS is used to control type-specifier parsing.
21936 If IS_DECLARATION is true, we are at the start of a "condition" or
21937 exception-declaration, so we might be followed by a declarator-id.
21939 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21940 i.e. we've just seen "->".
21942 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21945 cp_parser_type_specifier_seq (cp_parser
* parser
,
21946 cp_parser_flags flags
,
21947 bool is_declaration
,
21948 bool is_trailing_return
,
21949 cp_decl_specifier_seq
*type_specifier_seq
)
21951 bool seen_type_specifier
= false;
21952 cp_token
*start_token
= NULL
;
21954 /* Clear the TYPE_SPECIFIER_SEQ. */
21955 clear_decl_specs (type_specifier_seq
);
21957 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
21958 /* In the context of a trailing return type, enum E { } is an
21959 elaborated-type-specifier followed by a function-body, not an
21961 if (is_trailing_return
)
21962 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21964 /* Parse the type-specifiers and attributes. */
21967 tree type_specifier
;
21968 bool is_cv_qualifier
;
21970 /* Check for attributes first. */
21971 if (cp_next_tokens_can_be_attribute_p (parser
))
21973 type_specifier_seq
->attributes
21974 = attr_chainon (type_specifier_seq
->attributes
,
21975 cp_parser_attributes_opt (parser
));
21979 /* record the token of the beginning of the type specifier seq,
21980 for error reporting purposes*/
21982 start_token
= cp_lexer_peek_token (parser
->lexer
);
21984 /* Look for the type-specifier. */
21985 type_specifier
= cp_parser_type_specifier (parser
,
21987 type_specifier_seq
,
21988 /*is_declaration=*/false,
21991 if (!type_specifier
)
21993 /* If the first type-specifier could not be found, this is not a
21994 type-specifier-seq at all. */
21995 if (!seen_type_specifier
)
21997 /* Set in_declarator_p to avoid skipping to the semicolon. */
21998 int in_decl
= parser
->in_declarator_p
;
21999 parser
->in_declarator_p
= true;
22001 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
22002 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22003 cp_parser_error (parser
, "expected type-specifier");
22005 parser
->in_declarator_p
= in_decl
;
22007 type_specifier_seq
->type
= error_mark_node
;
22010 /* If subsequent type-specifiers could not be found, the
22011 type-specifier-seq is complete. */
22015 seen_type_specifier
= true;
22016 /* The standard says that a condition can be:
22018 type-specifier-seq declarator = assignment-expression
22025 we should treat the "S" as a declarator, not as a
22026 type-specifier. The standard doesn't say that explicitly for
22027 type-specifier-seq, but it does say that for
22028 decl-specifier-seq in an ordinary declaration. Perhaps it
22029 would be clearer just to allow a decl-specifier-seq here, and
22030 then add a semantic restriction that if any decl-specifiers
22031 that are not type-specifiers appear, the program is invalid. */
22032 if (is_declaration
&& !is_cv_qualifier
)
22033 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
22037 /* Return whether the function currently being declared has an associated
22038 template parameter list. */
22041 function_being_declared_is_template_p (cp_parser
* parser
)
22043 if (!current_template_parms
|| processing_template_parmlist
)
22046 if (parser
->implicit_template_scope
)
22049 if (at_class_scope_p ()
22050 && TYPE_BEING_DEFINED (current_class_type
))
22051 return parser
->num_template_parameter_lists
!= 0;
22053 return ((int) parser
->num_template_parameter_lists
> template_class_depth
22054 (current_class_type
));
22057 /* Parse a parameter-declaration-clause.
22059 parameter-declaration-clause:
22060 parameter-declaration-list [opt] ... [opt]
22061 parameter-declaration-list , ...
22063 The parser flags FLAGS is used to control type-specifier parsing.
22065 Returns a representation for the parameter declarations. A return
22066 value of NULL indicates a parameter-declaration-clause consisting
22067 only of an ellipsis. */
22070 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
22071 cp_parser_flags flags
)
22077 temp_override
<bool> cleanup
22078 (parser
->auto_is_implicit_function_template_parm_p
);
22080 if (!processing_specialization
22081 && !processing_template_parmlist
22082 && !processing_explicit_instantiation
22083 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22084 actual function or a random abstract declarator. */
22085 && parser
->default_arg_ok_p
)
22086 if (!current_function_decl
22087 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
22088 parser
->auto_is_implicit_function_template_parm_p
= true;
22090 /* Peek at the next token. */
22091 token
= cp_lexer_peek_token (parser
->lexer
);
22092 /* Check for trivial parameter-declaration-clauses. */
22093 if (token
->type
== CPP_ELLIPSIS
)
22095 /* Consume the `...' token. */
22096 cp_lexer_consume_token (parser
->lexer
);
22099 else if (token
->type
== CPP_CLOSE_PAREN
)
22100 /* There are no parameters. */
22101 return void_list_node
;
22102 /* Check for `(void)', too, which is a special case. */
22103 else if (token
->keyword
== RID_VOID
22104 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22105 == CPP_CLOSE_PAREN
))
22107 /* Consume the `void' token. */
22108 cp_lexer_consume_token (parser
->lexer
);
22109 /* There are no parameters. */
22110 return void_list_node
;
22113 /* Parse the parameter-declaration-list. */
22114 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
22115 /* If a parse error occurred while parsing the
22116 parameter-declaration-list, then the entire
22117 parameter-declaration-clause is erroneous. */
22118 if (parameters
== error_mark_node
)
22121 /* Peek at the next token. */
22122 token
= cp_lexer_peek_token (parser
->lexer
);
22123 /* If it's a `,', the clause should terminate with an ellipsis. */
22124 if (token
->type
== CPP_COMMA
)
22126 /* Consume the `,'. */
22127 cp_lexer_consume_token (parser
->lexer
);
22128 /* Expect an ellipsis. */
22130 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
22132 /* It might also be `...' if the optional trailing `,' was
22134 else if (token
->type
== CPP_ELLIPSIS
)
22136 /* Consume the `...' token. */
22137 cp_lexer_consume_token (parser
->lexer
);
22138 /* And remember that we saw it. */
22142 ellipsis_p
= false;
22144 /* Finish the parameter list. */
22146 parameters
= chainon (parameters
, void_list_node
);
22151 /* Parse a parameter-declaration-list.
22153 parameter-declaration-list:
22154 parameter-declaration
22155 parameter-declaration-list , parameter-declaration
22157 The parser flags FLAGS is used to control type-specifier parsing.
22159 Returns a representation of the parameter-declaration-list, as for
22160 cp_parser_parameter_declaration_clause. However, the
22161 `void_list_node' is never appended to the list. */
22164 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22166 tree parameters
= NULL_TREE
;
22167 tree
*tail
= ¶meters
;
22168 bool saved_in_unbraced_linkage_specification_p
;
22171 /* The special considerations that apply to a function within an
22172 unbraced linkage specifications do not apply to the parameters
22173 to the function. */
22174 saved_in_unbraced_linkage_specification_p
22175 = parser
->in_unbraced_linkage_specification_p
;
22176 parser
->in_unbraced_linkage_specification_p
= false;
22178 /* Look for more parameters. */
22181 cp_parameter_declarator
*parameter
;
22182 tree decl
= error_mark_node
;
22183 bool parenthesized_p
= false;
22185 /* Parse the parameter. */
22187 = cp_parser_parameter_declaration (parser
, flags
,
22188 /*template_parm_p=*/false,
22191 /* We don't know yet if the enclosing context is deprecated, so wait
22192 and warn in grokparms if appropriate. */
22193 deprecated_state
= DEPRECATED_SUPPRESS
;
22197 decl
= grokdeclarator (parameter
->declarator
,
22198 ¶meter
->decl_specifiers
,
22200 parameter
->default_argument
!= NULL_TREE
,
22201 ¶meter
->decl_specifiers
.attributes
);
22202 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22203 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22206 deprecated_state
= DEPRECATED_NORMAL
;
22208 /* If a parse error occurred parsing the parameter declaration,
22209 then the entire parameter-declaration-list is erroneous. */
22210 if (decl
== error_mark_node
)
22212 parameters
= error_mark_node
;
22216 if (parameter
->decl_specifiers
.attributes
)
22217 cplus_decl_attributes (&decl
,
22218 parameter
->decl_specifiers
.attributes
,
22220 if (DECL_NAME (decl
))
22221 decl
= pushdecl (decl
);
22223 if (decl
!= error_mark_node
)
22225 retrofit_lang_decl (decl
);
22226 DECL_PARM_INDEX (decl
) = ++index
;
22227 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22230 /* Add the new parameter to the list. */
22231 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22232 tail
= &TREE_CHAIN (*tail
);
22234 /* Peek at the next token. */
22235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22236 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22237 /* These are for Objective-C++ */
22238 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22239 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22240 /* The parameter-declaration-list is complete. */
22242 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22246 /* Peek at the next token. */
22247 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22248 /* If it's an ellipsis, then the list is complete. */
22249 if (token
->type
== CPP_ELLIPSIS
)
22251 /* Otherwise, there must be more parameters. Consume the
22253 cp_lexer_consume_token (parser
->lexer
);
22254 /* When parsing something like:
22256 int i(float f, double d)
22258 we can tell after seeing the declaration for "f" that we
22259 are not looking at an initialization of a variable "i",
22260 but rather at the declaration of a function "i".
22262 Due to the fact that the parsing of template arguments
22263 (as specified to a template-id) requires backtracking we
22264 cannot use this technique when inside a template argument
22266 if (!parser
->in_template_argument_list_p
22267 && !parser
->in_type_id_in_expr_p
22268 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22269 /* However, a parameter-declaration of the form
22270 "float(f)" (which is a valid declaration of a
22271 parameter "f") can also be interpreted as an
22272 expression (the conversion of "f" to "float"). */
22273 && !parenthesized_p
)
22274 cp_parser_commit_to_tentative_parse (parser
);
22278 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22279 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22280 cp_parser_skip_to_closing_parenthesis (parser
,
22281 /*recovering=*/true,
22282 /*or_comma=*/false,
22283 /*consume_paren=*/false);
22288 parser
->in_unbraced_linkage_specification_p
22289 = saved_in_unbraced_linkage_specification_p
;
22291 /* Reset implicit_template_scope if we are about to leave the function
22292 parameter list that introduced it. Note that for out-of-line member
22293 definitions, there will be one or more class scopes before we get to
22294 the template parameter scope. */
22296 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22297 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22299 while (maybe_its
->kind
== sk_class
)
22300 maybe_its
= maybe_its
->level_chain
;
22301 if (maybe_its
== its
)
22303 parser
->implicit_template_parms
= 0;
22304 parser
->implicit_template_scope
= 0;
22311 /* Parse a parameter declaration.
22313 parameter-declaration:
22314 decl-specifier-seq ... [opt] declarator
22315 decl-specifier-seq declarator = assignment-expression
22316 decl-specifier-seq ... [opt] abstract-declarator [opt]
22317 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22319 The parser flags FLAGS is used to control type-specifier parsing.
22321 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22322 declares a template parameter. (In that case, a non-nested `>'
22323 token encountered during the parsing of the assignment-expression
22324 is not interpreted as a greater-than operator.)
22326 Returns a representation of the parameter, or NULL if an error
22327 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22328 true iff the declarator is of the form "(p)". */
22330 static cp_parameter_declarator
*
22331 cp_parser_parameter_declaration (cp_parser
*parser
,
22332 cp_parser_flags flags
,
22333 bool template_parm_p
,
22334 bool *parenthesized_p
)
22336 int declares_class_or_enum
;
22337 cp_decl_specifier_seq decl_specifiers
;
22338 cp_declarator
*declarator
;
22339 tree default_argument
;
22340 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22341 const char *saved_message
;
22342 bool template_parameter_pack_p
= false;
22344 /* In a template parameter, `>' is not an operator.
22348 When parsing a default template-argument for a non-type
22349 template-parameter, the first non-nested `>' is taken as the end
22350 of the template parameter-list rather than a greater-than
22353 /* Type definitions may not appear in parameter types. */
22354 saved_message
= parser
->type_definition_forbidden_message
;
22355 parser
->type_definition_forbidden_message
22356 = G_("types may not be defined in parameter types");
22358 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22359 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22360 (current_template_parms
)) : 0);
22362 /* Parse the declaration-specifiers. */
22363 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22364 cp_parser_decl_specifier_seq (parser
,
22367 &declares_class_or_enum
);
22369 /* Complain about missing 'typename' or other invalid type names. */
22370 if (!decl_specifiers
.any_type_specifiers_p
22371 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22372 decl_specifiers
.type
= error_mark_node
;
22374 /* If an error occurred, there's no reason to attempt to parse the
22375 rest of the declaration. */
22376 if (cp_parser_error_occurred (parser
))
22378 parser
->type_definition_forbidden_message
= saved_message
;
22382 /* Peek at the next token. */
22383 token
= cp_lexer_peek_token (parser
->lexer
);
22385 /* If the next token is a `)', `,', `=', `>', or `...', then there
22386 is no declarator. However, when variadic templates are enabled,
22387 there may be a declarator following `...'. */
22388 if (token
->type
== CPP_CLOSE_PAREN
22389 || token
->type
== CPP_COMMA
22390 || token
->type
== CPP_EQ
22391 || token
->type
== CPP_GREATER
)
22394 if (parenthesized_p
)
22395 *parenthesized_p
= false;
22397 /* Otherwise, there should be a declarator. */
22400 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22401 parser
->default_arg_ok_p
= false;
22403 /* After seeing a decl-specifier-seq, if the next token is not a
22404 "(", there is no possibility that the code is a valid
22405 expression. Therefore, if parsing tentatively, we commit at
22407 if (!parser
->in_template_argument_list_p
22408 /* In an expression context, having seen:
22412 we cannot be sure whether we are looking at a
22413 function-type (taking a "char" as a parameter) or a cast
22414 of some object of type "char" to "int". */
22415 && !parser
->in_type_id_in_expr_p
22416 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22417 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22418 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22419 cp_parser_commit_to_tentative_parse (parser
);
22420 /* Parse the declarator. */
22421 declarator_token_start
= token
;
22422 declarator
= cp_parser_declarator (parser
,
22423 CP_PARSER_DECLARATOR_EITHER
,
22424 CP_PARSER_FLAGS_NONE
,
22425 /*ctor_dtor_or_conv_p=*/NULL
,
22427 /*member_p=*/false,
22428 /*friend_p=*/false,
22429 /*static_p=*/false);
22430 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22431 /* After the declarator, allow more attributes. */
22432 decl_specifiers
.attributes
22433 = attr_chainon (decl_specifiers
.attributes
,
22434 cp_parser_attributes_opt (parser
));
22436 /* If the declarator is a template parameter pack, remember that and
22437 clear the flag in the declarator itself so we don't get errors
22438 from grokdeclarator. */
22439 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22441 declarator
->parameter_pack_p
= false;
22442 template_parameter_pack_p
= true;
22446 /* If the next token is an ellipsis, and we have not seen a declarator
22447 name, and if either the type of the declarator contains parameter
22448 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22449 for, eg, abbreviated integral type names), then we actually have a
22450 parameter pack expansion expression. Otherwise, leave the ellipsis
22451 for a C-style variadic function. */
22452 token
= cp_lexer_peek_token (parser
->lexer
);
22454 /* If a function parameter pack was specified and an implicit template
22455 parameter was introduced during cp_parser_parameter_declaration,
22456 change any implicit parameters introduced into packs. */
22457 if (parser
->implicit_template_parms
22458 && ((token
->type
== CPP_ELLIPSIS
22459 && declarator_can_be_parameter_pack (declarator
))
22460 || (declarator
&& declarator
->parameter_pack_p
)))
22462 int latest_template_parm_idx
= TREE_VEC_LENGTH
22463 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22465 if (latest_template_parm_idx
!= template_parm_idx
)
22466 decl_specifiers
.type
= convert_generic_types_to_packs
22467 (decl_specifiers
.type
,
22468 template_parm_idx
, latest_template_parm_idx
);
22471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22473 tree type
= decl_specifiers
.type
;
22475 if (type
&& DECL_P (type
))
22476 type
= TREE_TYPE (type
);
22479 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22480 && (template_parm_p
|| uses_parameter_packs (type
)))
22481 || (!type
&& template_parm_p
))
22482 && declarator_can_be_parameter_pack (declarator
))
22484 /* Consume the `...'. */
22485 cp_lexer_consume_token (parser
->lexer
);
22486 maybe_warn_variadic_templates ();
22488 /* Build a pack expansion type */
22489 if (template_parm_p
)
22490 template_parameter_pack_p
= true;
22491 else if (declarator
)
22492 declarator
->parameter_pack_p
= true;
22494 decl_specifiers
.type
= make_pack_expansion (type
);
22498 /* The restriction on defining new types applies only to the type
22499 of the parameter, not to the default argument. */
22500 parser
->type_definition_forbidden_message
= saved_message
;
22502 /* If the next token is `=', then process a default argument. */
22503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22505 tree type
= decl_specifiers
.type
;
22506 token
= cp_lexer_peek_token (parser
->lexer
);
22507 /* If we are defining a class, then the tokens that make up the
22508 default argument must be saved and processed later. */
22509 if (!template_parm_p
&& at_class_scope_p ()
22510 && TYPE_BEING_DEFINED (current_class_type
)
22511 && !LAMBDA_TYPE_P (current_class_type
))
22512 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22514 // A constrained-type-specifier may declare a type template-parameter.
22515 else if (declares_constrained_type_template_parameter (type
))
22517 = cp_parser_default_type_template_argument (parser
);
22519 // A constrained-type-specifier may declare a template-template-parameter.
22520 else if (declares_constrained_template_template_parameter (type
))
22522 = cp_parser_default_template_template_argument (parser
);
22524 /* Outside of a class definition, we can just parse the
22525 assignment-expression. */
22528 = cp_parser_default_argument (parser
, template_parm_p
);
22530 if (!parser
->default_arg_ok_p
)
22532 permerror (token
->location
,
22533 "default arguments are only "
22534 "permitted for function parameters");
22536 else if ((declarator
&& declarator
->parameter_pack_p
)
22537 || template_parameter_pack_p
22538 || (decl_specifiers
.type
22539 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22541 /* Find the name of the parameter pack. */
22542 cp_declarator
*id_declarator
= declarator
;
22543 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22544 id_declarator
= id_declarator
->declarator
;
22546 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22547 error_at (declarator_token_start
->location
,
22549 ? G_("template parameter pack %qD "
22550 "cannot have a default argument")
22551 : G_("parameter pack %qD cannot have "
22552 "a default argument"),
22553 id_declarator
->u
.id
.unqualified_name
);
22555 error_at (declarator_token_start
->location
,
22557 ? G_("template parameter pack cannot have "
22558 "a default argument")
22559 : G_("parameter pack cannot have a "
22560 "default argument"));
22562 default_argument
= NULL_TREE
;
22566 default_argument
= NULL_TREE
;
22568 if (default_argument
)
22569 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
22571 /* Generate a location for the parameter, ranging from the start of the
22572 initial token to the end of the final token (using input_location for
22573 the latter, set up by cp_lexer_set_source_position_from_token when
22576 If we have a identifier, then use it for the caret location, e.g.
22578 extern int callee (int one, int (*two)(int, int), float three);
22579 ~~~~~~^~~~~~~~~~~~~~
22581 otherwise, reuse the start location for the caret location e.g.:
22583 extern int callee (int one, int (*)(int, int), float three);
22587 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22588 ? declarator
->id_loc
22589 : decl_spec_token_start
->location
);
22590 location_t param_loc
= make_location (caret_loc
,
22591 decl_spec_token_start
->location
,
22594 return make_parameter_declarator (&decl_specifiers
,
22598 template_parameter_pack_p
);
22601 /* Parse a default argument and return it.
22603 TEMPLATE_PARM_P is true if this is a default argument for a
22604 non-type template parameter. */
22606 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22608 tree default_argument
= NULL_TREE
;
22609 bool saved_greater_than_is_operator_p
;
22610 unsigned char saved_local_variables_forbidden_p
;
22611 bool non_constant_p
, is_direct_init
;
22613 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22615 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22616 parser
->greater_than_is_operator_p
= !template_parm_p
;
22617 /* Local variable names (and the `this' keyword) may not
22618 appear in a default argument. */
22619 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22620 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
22621 /* Parse the assignment-expression. */
22622 if (template_parm_p
)
22623 push_deferring_access_checks (dk_no_deferred
);
22624 tree saved_class_ptr
= NULL_TREE
;
22625 tree saved_class_ref
= NULL_TREE
;
22626 /* The "this" pointer is not valid in a default argument. */
22629 saved_class_ptr
= current_class_ptr
;
22630 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22631 saved_class_ref
= current_class_ref
;
22632 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22635 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22636 /* Restore the "this" pointer. */
22639 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22640 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22642 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22643 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22644 if (template_parm_p
)
22645 pop_deferring_access_checks ();
22646 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22647 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22649 return default_argument
;
22652 /* Parse a function-body.
22655 compound_statement */
22658 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22660 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22661 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22665 /* Parse a ctor-initializer-opt followed by a function-body. Return
22666 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22667 is true we are parsing a function-try-block. */
22670 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22671 bool in_function_try_block
)
22674 const bool check_body_p
22675 = (DECL_CONSTRUCTOR_P (current_function_decl
)
22676 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
22679 if (in_function_try_block
22680 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
22681 && cxx_dialect
< cxx2a
)
22683 if (DECL_CONSTRUCTOR_P (current_function_decl
))
22684 pedwarn (input_location
, 0,
22685 "function-try-block body of %<constexpr%> constructor only "
22686 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22688 pedwarn (input_location
, 0,
22689 "function-try-block body of %<constexpr%> function only "
22690 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22693 /* Begin the function body. */
22694 body
= begin_function_body ();
22695 /* Parse the optional ctor-initializer. */
22696 cp_parser_ctor_initializer_opt (parser
);
22698 /* If we're parsing a constexpr constructor definition, we need
22699 to check that the constructor body is indeed empty. However,
22700 before we get to cp_parser_function_body lot of junk has been
22701 generated, so we can't just check that we have an empty block.
22702 Rather we take a snapshot of the outermost block, and check whether
22703 cp_parser_function_body changed its state. */
22706 list
= cur_stmt_list
;
22707 if (STATEMENT_LIST_TAIL (list
))
22708 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22710 /* Parse the function-body. */
22711 cp_parser_function_body (parser
, in_function_try_block
);
22713 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22714 /* Finish the function body. */
22715 finish_function_body (body
);
22718 /* Parse an initializer.
22721 = initializer-clause
22722 ( expression-list )
22724 Returns an expression representing the initializer. If no
22725 initializer is present, NULL_TREE is returned.
22727 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22728 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22729 set to TRUE if there is no initializer present. If there is an
22730 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22731 is set to true; otherwise it is set to false. */
22734 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22735 bool* non_constant_p
, bool subexpression_p
)
22740 /* Peek at the next token. */
22741 token
= cp_lexer_peek_token (parser
->lexer
);
22743 /* Let our caller know whether or not this initializer was
22745 *is_direct_init
= (token
->type
!= CPP_EQ
);
22746 /* Assume that the initializer is constant. */
22747 *non_constant_p
= false;
22749 if (token
->type
== CPP_EQ
)
22751 /* Consume the `='. */
22752 cp_lexer_consume_token (parser
->lexer
);
22753 /* Parse the initializer-clause. */
22754 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22756 else if (token
->type
== CPP_OPEN_PAREN
)
22758 vec
<tree
, va_gc
> *vec
;
22759 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22761 /*allow_expansion_p=*/true,
22764 return error_mark_node
;
22765 init
= build_tree_list_vec (vec
);
22766 release_tree_vector (vec
);
22768 else if (token
->type
== CPP_OPEN_BRACE
)
22770 cp_lexer_set_source_position (parser
->lexer
);
22771 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22772 init
= cp_parser_braced_list (parser
, non_constant_p
);
22773 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22777 /* Anything else is an error. */
22778 cp_parser_error (parser
, "expected initializer");
22779 init
= error_mark_node
;
22782 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22783 init
= error_mark_node
;
22788 /* Parse an initializer-clause.
22790 initializer-clause:
22791 assignment-expression
22794 Returns an expression representing the initializer.
22796 If the `assignment-expression' production is used the value
22797 returned is simply a representation for the expression.
22799 Otherwise, calls cp_parser_braced_list. */
22802 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22804 cp_expr initializer
;
22806 /* Assume the expression is constant. */
22807 *non_constant_p
= false;
22809 /* If it is not a `{', then we are looking at an
22810 assignment-expression. */
22811 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22814 = cp_parser_constant_expression (parser
,
22815 /*allow_non_constant_p=*/true,
22819 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22821 return initializer
;
22824 /* Parse a brace-enclosed initializer list.
22827 { initializer-list , [opt] }
22828 { designated-initializer-list , [opt] }
22831 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22832 the elements of the initializer-list (or NULL, if the last
22833 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22834 NULL_TREE. There is no way to detect whether or not the optional
22835 trailing `,' was provided. NON_CONSTANT_P is as for
22836 cp_parser_initializer. */
22839 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22842 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22844 /* Consume the `{' token. */
22845 matching_braces braces
;
22846 braces
.require_open (parser
);
22847 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22848 initializer
= make_node (CONSTRUCTOR
);
22849 /* If it's not a `}', then there is a non-trivial initializer. */
22850 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22853 /* Parse the initializer list. */
22854 CONSTRUCTOR_ELTS (initializer
)
22855 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
22856 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
22857 /* A trailing `,' token is allowed. */
22858 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22859 cp_lexer_consume_token (parser
->lexer
);
22862 *non_constant_p
= false;
22863 /* Now, there should be a trailing `}'. */
22864 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22865 braces
.require_close (parser
);
22866 TREE_TYPE (initializer
) = init_list_type_node
;
22868 cp_expr
result (initializer
);
22869 /* Build a location of the form:
22872 with caret==start at the open brace, finish at the close brace. */
22873 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22874 result
.set_location (combined_loc
);
22878 /* Consume tokens up to, and including, the next non-nested closing `]'.
22879 Returns true iff we found a closing `]'. */
22882 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22884 unsigned square_depth
= 0;
22888 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22890 switch (token
->type
)
22892 case CPP_PRAGMA_EOL
:
22893 if (!parser
->lexer
->in_pragma
)
22897 /* If we've run out of tokens, then there is no closing `]'. */
22900 case CPP_OPEN_SQUARE
:
22904 case CPP_CLOSE_SQUARE
:
22905 if (!square_depth
--)
22907 cp_lexer_consume_token (parser
->lexer
);
22916 /* Consume the token. */
22917 cp_lexer_consume_token (parser
->lexer
);
22921 /* Return true if we are looking at an array-designator, false otherwise. */
22924 cp_parser_array_designator_p (cp_parser
*parser
)
22926 /* Consume the `['. */
22927 cp_lexer_consume_token (parser
->lexer
);
22929 cp_lexer_save_tokens (parser
->lexer
);
22931 /* Skip tokens until the next token is a closing square bracket.
22932 If we find the closing `]', and the next token is a `=', then
22933 we are looking at an array designator. */
22934 bool array_designator_p
22935 = (cp_parser_skip_to_closing_square_bracket (parser
)
22936 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22938 /* Roll back the tokens we skipped. */
22939 cp_lexer_rollback_tokens (parser
->lexer
);
22941 return array_designator_p
;
22944 /* Parse an initializer-list.
22947 initializer-clause ... [opt]
22948 initializer-list , initializer-clause ... [opt]
22952 designated-initializer-list:
22953 designated-initializer-clause
22954 designated-initializer-list , designated-initializer-clause
22956 designated-initializer-clause:
22957 designator brace-or-equal-initializer
22965 designation initializer-clause ...[opt]
22966 initializer-list , designation initializer-clause ...[opt]
22971 [ constant-expression ] =
22973 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22974 for the initializer. If the INDEX of the elt is non-NULL, it is the
22975 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22976 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22977 are any designators. */
22979 static vec
<constructor_elt
, va_gc
> *
22980 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
22983 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22984 bool first_p
= true;
22985 tree first_designator
= NULL_TREE
;
22987 /* Assume all of the expressions are constant. */
22988 *non_constant_p
= false;
22990 /* Parse the rest of the list. */
22996 bool clause_non_constant_p
;
22997 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22999 /* Handle the C++2A syntax, '. id ='. */
23000 if ((cxx_dialect
>= cxx2a
23001 || cp_parser_allow_gnu_extensions_p (parser
))
23002 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
23003 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
23004 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
23005 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
23006 == CPP_OPEN_BRACE
)))
23008 if (cxx_dialect
< cxx2a
)
23009 pedwarn (loc
, OPT_Wpedantic
,
23010 "C++ designated initializers only available with "
23011 "%<-std=c++2a%> or %<-std=gnu++2a%>");
23012 /* Consume the `.'. */
23013 cp_lexer_consume_token (parser
->lexer
);
23014 /* Consume the identifier. */
23015 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23017 /* Consume the `='. */
23018 cp_lexer_consume_token (parser
->lexer
);
23020 /* Also, if the next token is an identifier and the following one is a
23021 colon, we are looking at the GNU designated-initializer
23023 else if (cp_parser_allow_gnu_extensions_p (parser
)
23024 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
23025 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23028 /* Warn the user that they are using an extension. */
23029 pedwarn (loc
, OPT_Wpedantic
,
23030 "ISO C++ does not allow GNU designated initializers");
23031 /* Consume the identifier. */
23032 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23033 /* Consume the `:'. */
23034 cp_lexer_consume_token (parser
->lexer
);
23036 /* Also handle C99 array designators, '[ const ] ='. */
23037 else if (cp_parser_allow_gnu_extensions_p (parser
)
23038 && !c_dialect_objc ()
23039 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
23041 /* In C++11, [ could start a lambda-introducer. */
23042 bool non_const
= false;
23044 cp_parser_parse_tentatively (parser
);
23046 if (!cp_parser_array_designator_p (parser
))
23048 cp_parser_simulate_error (parser
);
23049 designator
= NULL_TREE
;
23053 designator
= cp_parser_constant_expression (parser
, true,
23055 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23056 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23059 if (!cp_parser_parse_definitely (parser
))
23060 designator
= NULL_TREE
;
23062 && (!require_potential_rvalue_constant_expression
23064 designator
= NULL_TREE
;
23066 /* Warn the user that they are using an extension. */
23067 pedwarn (loc
, OPT_Wpedantic
,
23068 "ISO C++ does not allow C99 designated initializers");
23071 designator
= NULL_TREE
;
23075 first_designator
= designator
;
23078 else if (cxx_dialect
>= cxx2a
23079 && first_designator
!= error_mark_node
23080 && (!first_designator
!= !designator
))
23082 error_at (loc
, "either all initializer clauses should be designated "
23083 "or none of them should be");
23084 first_designator
= error_mark_node
;
23086 else if (cxx_dialect
< cxx2a
&& !first_designator
)
23087 first_designator
= designator
;
23089 /* Parse the initializer. */
23090 initializer
= cp_parser_initializer_clause (parser
,
23091 &clause_non_constant_p
);
23092 /* If any clause is non-constant, so is the entire initializer. */
23093 if (clause_non_constant_p
)
23094 *non_constant_p
= true;
23096 /* If we have an ellipsis, this is an initializer pack
23098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23100 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23102 /* Consume the `...'. */
23103 cp_lexer_consume_token (parser
->lexer
);
23105 if (designator
&& cxx_dialect
>= cxx2a
)
23107 "%<...%> not allowed in designated initializer list");
23109 /* Turn the initializer into an initializer expansion. */
23110 initializer
= make_pack_expansion (initializer
);
23113 /* Add it to the vector. */
23114 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
23116 /* If the next token is not a comma, we have reached the end of
23118 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23121 /* Peek at the next token. */
23122 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23123 /* If the next token is a `}', then we're still done. An
23124 initializer-clause can have a trailing `,' after the
23125 initializer-list and before the closing `}'. */
23126 if (token
->type
== CPP_CLOSE_BRACE
)
23129 /* Consume the `,' token. */
23130 cp_lexer_consume_token (parser
->lexer
);
23133 /* The same identifier shall not appear in multiple designators
23134 of a designated-initializer-list. */
23135 if (first_designator
)
23138 tree designator
, val
;
23139 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23140 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23142 if (IDENTIFIER_MARKED (designator
))
23144 error_at (cp_expr_loc_or_loc (val
, input_location
),
23145 "%<.%s%> designator used multiple times in "
23146 "the same initializer list",
23147 IDENTIFIER_POINTER (designator
));
23148 (*v
)[i
].index
= error_mark_node
;
23151 IDENTIFIER_MARKED (designator
) = 1;
23153 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23154 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23155 IDENTIFIER_MARKED (designator
) = 0;
23158 *designated
= first_designator
!= NULL_TREE
;
23162 /* Classes [gram.class] */
23164 /* Parse a class-name.
23170 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23171 to indicate that names looked up in dependent types should be
23172 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23173 keyword has been used to indicate that the name that appears next
23174 is a template. TAG_TYPE indicates the explicit tag given before
23175 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23176 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23177 is the class being defined in a class-head. If ENUM_OK is TRUE,
23178 enum-names are also accepted.
23180 Returns the TYPE_DECL representing the class. */
23183 cp_parser_class_name (cp_parser
*parser
,
23184 bool typename_keyword_p
,
23185 bool template_keyword_p
,
23186 enum tag_types tag_type
,
23187 bool check_dependency_p
,
23189 bool is_declaration
,
23196 tree identifier
= NULL_TREE
;
23198 /* All class-names start with an identifier. */
23199 token
= cp_lexer_peek_token (parser
->lexer
);
23200 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23202 cp_parser_error (parser
, "expected class-name");
23203 return error_mark_node
;
23206 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23207 to a template-id, so we save it here. */
23208 scope
= parser
->scope
;
23209 if (scope
== error_mark_node
)
23210 return error_mark_node
;
23212 /* Any name names a type if we're following the `typename' keyword
23213 in a qualified name where the enclosing scope is type-dependent. */
23214 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
23215 && dependent_type_p (scope
));
23216 /* Handle the common case (an identifier, but not a template-id)
23218 if (token
->type
== CPP_NAME
23219 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
23221 cp_token
*identifier_token
;
23224 /* Look for the identifier. */
23225 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
23226 ambiguous_p
= identifier_token
->error_reported
;
23227 identifier
= cp_parser_identifier (parser
);
23228 /* If the next token isn't an identifier, we are certainly not
23229 looking at a class-name. */
23230 if (identifier
== error_mark_node
)
23231 decl
= error_mark_node
;
23232 /* If we know this is a type-name, there's no need to look it
23234 else if (typename_p
)
23238 tree ambiguous_decls
;
23239 /* If we already know that this lookup is ambiguous, then
23240 we've already issued an error message; there's no reason
23244 cp_parser_simulate_error (parser
);
23245 return error_mark_node
;
23247 /* If the next token is a `::', then the name must be a type
23250 [basic.lookup.qual]
23252 During the lookup for a name preceding the :: scope
23253 resolution operator, object, function, and enumerator
23254 names are ignored. */
23255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23256 tag_type
= scope_type
;
23257 /* Look up the name. */
23258 decl
= cp_parser_lookup_name (parser
, identifier
,
23260 /*is_template=*/false,
23261 /*is_namespace=*/false,
23262 check_dependency_p
,
23264 identifier_token
->location
);
23265 if (ambiguous_decls
)
23267 if (cp_parser_parsing_tentatively (parser
))
23268 cp_parser_simulate_error (parser
);
23269 return error_mark_node
;
23275 /* Try a template-id. */
23276 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23277 check_dependency_p
,
23280 if (decl
== error_mark_node
)
23281 return error_mark_node
;
23284 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23286 /* If this is a typename, create a TYPENAME_TYPE. */
23288 && decl
!= error_mark_node
23289 && !is_overloaded_fn (decl
))
23291 decl
= make_typename_type (scope
, decl
, typename_type
,
23292 /*complain=*/tf_error
);
23293 if (decl
!= error_mark_node
)
23294 decl
= TYPE_NAME (decl
);
23297 decl
= strip_using_decl (decl
);
23299 /* Check to see that it is really the name of a class. */
23300 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23301 && identifier_p (TREE_OPERAND (decl
, 0))
23302 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23303 /* Situations like this:
23305 template <typename T> struct A {
23306 typename T::template X<int>::I i;
23309 are problematic. Is `T::template X<int>' a class-name? The
23310 standard does not seem to be definitive, but there is no other
23311 valid interpretation of the following `::'. Therefore, those
23312 names are considered class-names. */
23314 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23315 if (decl
!= error_mark_node
)
23316 decl
= TYPE_NAME (decl
);
23318 else if (TREE_CODE (decl
) != TYPE_DECL
23319 || TREE_TYPE (decl
) == error_mark_node
23320 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23321 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23322 /* In Objective-C 2.0, a classname followed by '.' starts a
23323 dot-syntax expression, and it's not a type-name. */
23324 || (c_dialect_objc ()
23325 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23326 && objc_is_class_name (decl
)))
23327 decl
= error_mark_node
;
23329 if (decl
== error_mark_node
)
23330 cp_parser_error (parser
, "expected class-name");
23331 else if (identifier
&& !parser
->scope
)
23332 maybe_note_name_used_in_class (identifier
, decl
);
23337 /* Parse a class-specifier.
23340 class-head { member-specification [opt] }
23342 Returns the TREE_TYPE representing the class. */
23345 cp_parser_class_specifier_1 (cp_parser
* parser
)
23348 tree attributes
= NULL_TREE
;
23349 bool nested_name_specifier_p
;
23350 unsigned saved_num_template_parameter_lists
;
23351 bool saved_in_function_body
;
23352 unsigned char in_statement
;
23353 bool in_switch_statement_p
;
23354 bool saved_in_unbraced_linkage_specification_p
;
23355 tree old_scope
= NULL_TREE
;
23356 tree scope
= NULL_TREE
;
23357 cp_token
*closing_brace
;
23359 push_deferring_access_checks (dk_no_deferred
);
23361 /* Parse the class-head. */
23362 type
= cp_parser_class_head (parser
,
23363 &nested_name_specifier_p
);
23364 /* If the class-head was a semantic disaster, skip the entire body
23368 cp_parser_skip_to_end_of_block_or_statement (parser
);
23369 pop_deferring_access_checks ();
23370 return error_mark_node
;
23373 /* Look for the `{'. */
23374 matching_braces braces
;
23375 if (!braces
.require_open (parser
))
23377 pop_deferring_access_checks ();
23378 return error_mark_node
;
23381 cp_ensure_no_omp_declare_simd (parser
);
23382 cp_ensure_no_oacc_routine (parser
);
23384 /* Issue an error message if type-definitions are forbidden here. */
23385 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
23386 /* Remember that we are defining one more class. */
23387 ++parser
->num_classes_being_defined
;
23388 /* Inside the class, surrounding template-parameter-lists do not
23390 saved_num_template_parameter_lists
23391 = parser
->num_template_parameter_lists
;
23392 parser
->num_template_parameter_lists
= 0;
23393 /* We are not in a function body. */
23394 saved_in_function_body
= parser
->in_function_body
;
23395 parser
->in_function_body
= false;
23396 /* Or in a loop. */
23397 in_statement
= parser
->in_statement
;
23398 parser
->in_statement
= 0;
23399 /* Or in a switch. */
23400 in_switch_statement_p
= parser
->in_switch_statement_p
;
23401 parser
->in_switch_statement_p
= false;
23402 /* We are not immediately inside an extern "lang" block. */
23403 saved_in_unbraced_linkage_specification_p
23404 = parser
->in_unbraced_linkage_specification_p
;
23405 parser
->in_unbraced_linkage_specification_p
= false;
23407 // Associate constraints with the type.
23409 type
= associate_classtype_constraints (type
);
23411 /* Start the class. */
23412 if (nested_name_specifier_p
)
23414 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23415 old_scope
= push_inner_scope (scope
);
23417 type
= begin_class_definition (type
);
23419 if (type
== error_mark_node
)
23420 /* If the type is erroneous, skip the entire body of the class. */
23421 cp_parser_skip_to_closing_brace (parser
);
23423 /* Parse the member-specification. */
23424 cp_parser_member_specification_opt (parser
);
23426 /* Look for the trailing `}'. */
23427 closing_brace
= braces
.require_close (parser
);
23428 /* Look for trailing attributes to apply to this class. */
23429 if (cp_parser_allow_gnu_extensions_p (parser
))
23430 attributes
= cp_parser_gnu_attributes_opt (parser
);
23431 if (type
!= error_mark_node
)
23432 type
= finish_struct (type
, attributes
);
23433 if (nested_name_specifier_p
)
23434 pop_inner_scope (old_scope
, scope
);
23436 /* We've finished a type definition. Check for the common syntax
23437 error of forgetting a semicolon after the definition. We need to
23438 be careful, as we can't just check for not-a-semicolon and be done
23439 with it; the user might have typed:
23441 class X { } c = ...;
23442 class X { } *p = ...;
23444 and so forth. Instead, enumerate all the possible tokens that
23445 might follow this production; if we don't see one of them, then
23446 complain and silently insert the semicolon. */
23448 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23449 bool want_semicolon
= true;
23451 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23452 /* Don't try to parse c++11 attributes here. As per the
23453 grammar, that should be a task for
23454 cp_parser_decl_specifier_seq. */
23455 want_semicolon
= false;
23457 switch (token
->type
)
23460 case CPP_SEMICOLON
:
23463 case CPP_OPEN_PAREN
:
23464 case CPP_CLOSE_PAREN
:
23466 want_semicolon
= false;
23469 /* While it's legal for type qualifiers and storage class
23470 specifiers to follow type definitions in the grammar, only
23471 compiler testsuites contain code like that. Assume that if
23472 we see such code, then what we're really seeing is a case
23476 const <type> var = ...;
23481 static <type> func (...) ...
23483 i.e. the qualifier or specifier applies to the next
23484 declaration. To do so, however, we need to look ahead one
23485 more token to see if *that* token is a type specifier.
23487 This code could be improved to handle:
23490 static const <type> var = ...; */
23492 if (keyword_is_decl_specifier (token
->keyword
))
23494 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23496 /* Handling user-defined types here would be nice, but very
23499 = (lookahead
->type
== CPP_KEYWORD
23500 && keyword_begins_type_specifier (lookahead
->keyword
));
23507 /* If we don't have a type, then something is very wrong and we
23508 shouldn't try to do anything clever. Likewise for not seeing the
23510 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23512 /* Locate the closing brace. */
23513 cp_token_position prev
23514 = cp_lexer_previous_token_position (parser
->lexer
);
23515 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23516 location_t loc
= prev_token
->location
;
23518 /* We want to suggest insertion of a ';' immediately *after* the
23519 closing brace, so, if we can, offset the location by 1 column. */
23520 location_t next_loc
= loc
;
23521 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23522 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23524 rich_location
richloc (line_table
, next_loc
);
23526 /* If we successfully offset the location, suggest the fix-it. */
23527 if (next_loc
!= loc
)
23528 richloc
.add_fixit_insert_before (next_loc
, ";");
23530 if (CLASSTYPE_DECLARED_CLASS (type
))
23531 error_at (&richloc
,
23532 "expected %<;%> after class definition");
23533 else if (TREE_CODE (type
) == RECORD_TYPE
)
23534 error_at (&richloc
,
23535 "expected %<;%> after struct definition");
23536 else if (TREE_CODE (type
) == UNION_TYPE
)
23537 error_at (&richloc
,
23538 "expected %<;%> after union definition");
23540 gcc_unreachable ();
23542 /* Unget one token and smash it to look as though we encountered
23543 a semicolon in the input stream. */
23544 cp_lexer_set_token_position (parser
->lexer
, prev
);
23545 token
= cp_lexer_peek_token (parser
->lexer
);
23546 token
->type
= CPP_SEMICOLON
;
23547 token
->keyword
= RID_MAX
;
23551 /* If this class is not itself within the scope of another class,
23552 then we need to parse the bodies of all of the queued function
23553 definitions. Note that the queued functions defined in a class
23554 are not always processed immediately following the
23555 class-specifier for that class. Consider:
23558 struct B { void f() { sizeof (A); } };
23561 If `f' were processed before the processing of `A' were
23562 completed, there would be no way to compute the size of `A'.
23563 Note that the nesting we are interested in here is lexical --
23564 not the semantic nesting given by TYPE_CONTEXT. In particular,
23567 struct A { struct B; };
23568 struct A::B { void f() { } };
23570 there is no need to delay the parsing of `A::B::f'. */
23571 if (--parser
->num_classes_being_defined
== 0)
23574 tree class_type
= NULL_TREE
;
23575 tree pushed_scope
= NULL_TREE
;
23577 cp_default_arg_entry
*e
;
23578 tree save_ccp
, save_ccr
;
23580 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
23582 /* Skip default arguments, NSDMIs, etc, in order to improve
23583 error recovery (c++/71169, c++/71832). */
23584 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23585 vec_safe_truncate (unparsed_nsdmis
, 0);
23586 vec_safe_truncate (unparsed_classes
, 0);
23587 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23590 /* In a first pass, parse default arguments to the functions.
23591 Then, in a second pass, parse the bodies of the functions.
23592 This two-phased approach handles cases like:
23600 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23603 /* If there are default arguments that have not yet been processed,
23604 take care of them now. */
23605 if (class_type
!= e
->class_type
)
23608 pop_scope (pushed_scope
);
23609 class_type
= e
->class_type
;
23610 pushed_scope
= push_scope (class_type
);
23612 /* Make sure that any template parameters are in scope. */
23613 maybe_begin_member_template_processing (decl
);
23614 /* Parse the default argument expressions. */
23615 cp_parser_late_parsing_default_args (parser
, decl
);
23616 /* Remove any template parameters from the symbol table. */
23617 maybe_end_member_template_processing ();
23619 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23620 /* Now parse any NSDMIs. */
23621 save_ccp
= current_class_ptr
;
23622 save_ccr
= current_class_ref
;
23623 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23625 if (class_type
!= DECL_CONTEXT (decl
))
23628 pop_scope (pushed_scope
);
23629 class_type
= DECL_CONTEXT (decl
);
23630 pushed_scope
= push_scope (class_type
);
23632 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23633 cp_parser_late_parsing_nsdmi (parser
, decl
);
23635 vec_safe_truncate (unparsed_nsdmis
, 0);
23636 current_class_ptr
= save_ccp
;
23637 current_class_ref
= save_ccr
;
23639 pop_scope (pushed_scope
);
23641 /* Now do some post-NSDMI bookkeeping. */
23642 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23643 after_nsdmi_defaulted_late_checks (class_type
);
23644 vec_safe_truncate (unparsed_classes
, 0);
23645 after_nsdmi_defaulted_late_checks (type
);
23647 /* Now parse the body of the functions. */
23650 /* OpenMP UDRs need to be parsed before all other functions. */
23651 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23652 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23653 cp_parser_late_parsing_for_member (parser
, decl
);
23654 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23655 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23656 cp_parser_late_parsing_for_member (parser
, decl
);
23659 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23660 cp_parser_late_parsing_for_member (parser
, decl
);
23661 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23664 vec_safe_push (unparsed_classes
, type
);
23666 /* Put back any saved access checks. */
23667 pop_deferring_access_checks ();
23669 /* Restore saved state. */
23670 parser
->in_switch_statement_p
= in_switch_statement_p
;
23671 parser
->in_statement
= in_statement
;
23672 parser
->in_function_body
= saved_in_function_body
;
23673 parser
->num_template_parameter_lists
23674 = saved_num_template_parameter_lists
;
23675 parser
->in_unbraced_linkage_specification_p
23676 = saved_in_unbraced_linkage_specification_p
;
23682 cp_parser_class_specifier (cp_parser
* parser
)
23685 timevar_push (TV_PARSE_STRUCT
);
23686 ret
= cp_parser_class_specifier_1 (parser
);
23687 timevar_pop (TV_PARSE_STRUCT
);
23691 /* Parse a class-head.
23694 class-key identifier [opt] base-clause [opt]
23695 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23696 class-key nested-name-specifier [opt] template-id
23699 class-virt-specifier:
23703 class-key attributes identifier [opt] base-clause [opt]
23704 class-key attributes nested-name-specifier identifier base-clause [opt]
23705 class-key attributes nested-name-specifier [opt] template-id
23708 Upon return BASES is initialized to the list of base classes (or
23709 NULL, if there are none) in the same form returned by
23710 cp_parser_base_clause.
23712 Returns the TYPE of the indicated class. Sets
23713 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23714 involving a nested-name-specifier was used, and FALSE otherwise.
23716 Returns error_mark_node if this is not a class-head.
23718 Returns NULL_TREE if the class-head is syntactically valid, but
23719 semantically invalid in a way that means we should skip the entire
23720 body of the class. */
23723 cp_parser_class_head (cp_parser
* parser
,
23724 bool* nested_name_specifier_p
)
23726 tree nested_name_specifier
;
23727 enum tag_types class_key
;
23728 tree id
= NULL_TREE
;
23729 tree type
= NULL_TREE
;
23732 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23733 bool template_id_p
= false;
23734 bool qualified_p
= false;
23735 bool invalid_nested_name_p
= false;
23736 bool invalid_explicit_specialization_p
= false;
23737 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23738 tree pushed_scope
= NULL_TREE
;
23739 unsigned num_templates
;
23740 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23741 /* Assume no nested-name-specifier will be present. */
23742 *nested_name_specifier_p
= false;
23743 /* Assume no template parameter lists will be used in defining the
23746 parser
->colon_corrects_to_scope_p
= false;
23748 /* Look for the class-key. */
23749 class_key
= cp_parser_class_key (parser
);
23750 if (class_key
== none_type
)
23751 return error_mark_node
;
23753 location_t class_head_start_location
= input_location
;
23755 /* Parse the attributes. */
23756 attributes
= cp_parser_attributes_opt (parser
);
23758 /* If the next token is `::', that is invalid -- but sometimes
23759 people do try to write:
23763 Handle this gracefully by accepting the extra qualifier, and then
23764 issuing an error about it later if this really is a
23765 class-head. If it turns out just to be an elaborated type
23766 specifier, remain silent. */
23767 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23768 qualified_p
= true;
23770 push_deferring_access_checks (dk_no_check
);
23772 /* Determine the name of the class. Begin by looking for an
23773 optional nested-name-specifier. */
23774 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23775 nested_name_specifier
23776 = cp_parser_nested_name_specifier_opt (parser
,
23777 /*typename_keyword_p=*/false,
23778 /*check_dependency_p=*/false,
23780 /*is_declaration=*/false);
23781 /* If there was a nested-name-specifier, then there *must* be an
23784 cp_token
*bad_template_keyword
= NULL
;
23786 if (nested_name_specifier
)
23788 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23789 /* Although the grammar says `identifier', it really means
23790 `class-name' or `template-name'. You are only allowed to
23791 define a class that has already been declared with this
23794 The proposed resolution for Core Issue 180 says that wherever
23795 you see `class T::X' you should treat `X' as a type-name.
23797 It is OK to define an inaccessible class; for example:
23799 class A { class B; };
23802 We do not know if we will see a class-name, or a
23803 template-name. We look for a class-name first, in case the
23804 class-name is a template-id; if we looked for the
23805 template-name first we would stop after the template-name. */
23806 cp_parser_parse_tentatively (parser
);
23807 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23808 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23809 type
= cp_parser_class_name (parser
,
23810 /*typename_keyword_p=*/false,
23811 /*template_keyword_p=*/false,
23813 /*check_dependency_p=*/false,
23814 /*class_head_p=*/true,
23815 /*is_declaration=*/false);
23816 /* If that didn't work, ignore the nested-name-specifier. */
23817 if (!cp_parser_parse_definitely (parser
))
23819 invalid_nested_name_p
= true;
23820 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23821 id
= cp_parser_identifier (parser
);
23822 if (id
== error_mark_node
)
23825 /* If we could not find a corresponding TYPE, treat this
23826 declaration like an unqualified declaration. */
23827 if (type
== error_mark_node
)
23828 nested_name_specifier
= NULL_TREE
;
23829 /* Otherwise, count the number of templates used in TYPE and its
23830 containing scopes. */
23832 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23834 /* Otherwise, the identifier is optional. */
23837 /* We don't know whether what comes next is a template-id,
23838 an identifier, or nothing at all. */
23839 cp_parser_parse_tentatively (parser
);
23840 /* Check for a template-id. */
23841 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23842 id
= cp_parser_template_id (parser
,
23843 /*template_keyword_p=*/false,
23844 /*check_dependency_p=*/true,
23846 /*is_declaration=*/true);
23847 /* If that didn't work, it could still be an identifier. */
23848 if (!cp_parser_parse_definitely (parser
))
23850 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23852 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23853 id
= cp_parser_identifier (parser
);
23860 template_id_p
= true;
23865 pop_deferring_access_checks ();
23869 cp_parser_check_for_invalid_template_id (parser
, id
,
23871 type_start_token
->location
);
23873 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23875 /* If it's not a `:' or a `{' then we can't really be looking at a
23876 class-head, since a class-head only appears as part of a
23877 class-specifier. We have to detect this situation before calling
23878 xref_tag, since that has irreversible side-effects. */
23879 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23881 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23882 type
= error_mark_node
;
23886 /* At this point, we're going ahead with the class-specifier, even
23887 if some other problem occurs. */
23888 cp_parser_commit_to_tentative_parse (parser
);
23889 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23891 cp_parser_error (parser
,
23892 "cannot specify %<override%> for a class");
23893 type
= error_mark_node
;
23896 /* Issue the error about the overly-qualified name now. */
23899 cp_parser_error (parser
,
23900 "global qualification of class name is invalid");
23901 type
= error_mark_node
;
23904 else if (invalid_nested_name_p
)
23906 cp_parser_error (parser
,
23907 "qualified name does not name a class");
23908 type
= error_mark_node
;
23911 else if (nested_name_specifier
)
23915 if (bad_template_keyword
)
23916 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23917 keyword template shall not appear at the top level. */
23918 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23919 "keyword %<template%> not allowed in class-head-name");
23921 /* Reject typedef-names in class heads. */
23922 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23924 error_at (type_start_token
->location
,
23925 "invalid class name in declaration of %qD",
23931 /* Figure out in what scope the declaration is being placed. */
23932 scope
= current_scope ();
23933 /* If that scope does not contain the scope in which the
23934 class was originally declared, the program is invalid. */
23935 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23937 if (at_namespace_scope_p ())
23938 error_at (type_start_token
->location
,
23939 "declaration of %qD in namespace %qD which does not "
23941 type
, scope
, nested_name_specifier
);
23943 error_at (type_start_token
->location
,
23944 "declaration of %qD in %qD which does not enclose %qD",
23945 type
, scope
, nested_name_specifier
);
23951 A declarator-id shall not be qualified except for the
23952 definition of a ... nested class outside of its class
23953 ... [or] the definition or explicit instantiation of a
23954 class member of a namespace outside of its namespace. */
23955 if (scope
== nested_name_specifier
)
23957 permerror (nested_name_specifier_token_start
->location
,
23958 "extra qualification not allowed");
23959 nested_name_specifier
= NULL_TREE
;
23963 /* An explicit-specialization must be preceded by "template <>". If
23964 it is not, try to recover gracefully. */
23965 if (at_namespace_scope_p ()
23966 && parser
->num_template_parameter_lists
== 0
23967 && !processing_template_parmlist
23970 /* Build a location of this form:
23971 struct typename <ARGS>
23972 ^~~~~~~~~~~~~~~~~~~~~~
23973 with caret==start at the start token, and
23974 finishing at the end of the type. */
23975 location_t reported_loc
23976 = make_location (class_head_start_location
,
23977 class_head_start_location
,
23978 get_finish (type_start_token
->location
));
23979 rich_location
richloc (line_table
, reported_loc
);
23980 richloc
.add_fixit_insert_before (class_head_start_location
,
23982 error_at (&richloc
,
23983 "an explicit specialization must be preceded by"
23984 " %<template <>%>");
23985 invalid_explicit_specialization_p
= true;
23986 /* Take the same action that would have been taken by
23987 cp_parser_explicit_specialization. */
23988 ++parser
->num_template_parameter_lists
;
23989 begin_specialization ();
23991 /* There must be no "return" statements between this point and the
23992 end of this function; set "type "to the correct return value and
23993 use "goto done;" to return. */
23994 /* Make sure that the right number of template parameters were
23996 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23998 type_start_token
->location
,
23999 /*declarator=*/NULL
))
24001 /* If something went wrong, there is no point in even trying to
24002 process the class-definition. */
24007 /* Look up the type. */
24010 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
24011 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
24012 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
24014 error_at (type_start_token
->location
,
24015 "function template %qD redeclared as a class template", id
);
24016 type
= error_mark_node
;
24020 type
= TREE_TYPE (id
);
24021 type
= maybe_process_partial_specialization (type
);
24023 /* Check the scope while we still know whether or not we had a
24024 nested-name-specifier. */
24025 if (type
!= error_mark_node
)
24026 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
24028 if (nested_name_specifier
)
24029 pushed_scope
= push_scope (nested_name_specifier
);
24031 else if (nested_name_specifier
)
24037 template <typename T> struct S { struct T };
24038 template <typename T> struct S<T>::T { };
24040 we will get a TYPENAME_TYPE when processing the definition of
24041 `S::T'. We need to resolve it to the actual type before we
24042 try to define it. */
24043 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
24045 class_type
= resolve_typename_type (TREE_TYPE (type
),
24046 /*only_current_p=*/false);
24047 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
24048 type
= TYPE_NAME (class_type
);
24051 cp_parser_error (parser
, "could not resolve typename type");
24052 type
= error_mark_node
;
24056 if (maybe_process_partial_specialization (TREE_TYPE (type
))
24057 == error_mark_node
)
24063 class_type
= current_class_type
;
24064 /* Enter the scope indicated by the nested-name-specifier. */
24065 pushed_scope
= push_scope (nested_name_specifier
);
24066 /* Get the canonical version of this type. */
24067 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
24068 /* Call push_template_decl if it seems like we should be defining a
24069 template either from the template headers or the type we're
24070 defining, so that we diagnose both extra and missing headers. */
24071 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24072 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
24073 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
24075 type
= push_template_decl (type
);
24076 if (type
== error_mark_node
)
24083 type
= TREE_TYPE (type
);
24084 *nested_name_specifier_p
= true;
24086 else /* The name is not a nested name. */
24088 /* If the class was unnamed, create a dummy name. */
24090 id
= make_anon_name ();
24091 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
24092 ? ts_within_enclosing_non_class
24094 type
= xref_tag (class_key
, id
, tag_scope
,
24095 parser
->num_template_parameter_lists
);
24098 /* Indicate whether this class was declared as a `class' or as a
24100 if (TREE_CODE (type
) == RECORD_TYPE
)
24101 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
24102 cp_parser_check_class_key (class_key
, type
);
24104 /* If this type was already complete, and we see another definition,
24105 that's an error. Likewise if the type is already being defined:
24106 this can happen, eg, when it's defined from within an expression
24108 if (type
!= error_mark_node
24109 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
24111 error_at (type_start_token
->location
, "redefinition of %q#T",
24113 inform (location_of (type
), "previous definition of %q#T",
24118 else if (type
== error_mark_node
)
24123 /* Apply attributes now, before any use of the class as a template
24124 argument in its base list. */
24125 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
24126 fixup_attribute_variants (type
);
24129 /* We will have entered the scope containing the class; the names of
24130 base classes should be looked up in that context. For example:
24132 struct A { struct B {}; struct C; };
24133 struct A::C : B {};
24137 /* Get the list of base-classes, if there is one. */
24138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
24140 /* PR59482: enter the class scope so that base-specifiers are looked
24144 bases
= cp_parser_base_clause (parser
);
24145 /* PR59482: get out of the previously pushed class scope so that the
24146 subsequent pops pop the right thing. */
24153 /* If we're really defining a class, process the base classes.
24154 If they're invalid, fail. */
24155 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24156 xref_basetypes (type
, bases
);
24159 /* Leave the scope given by the nested-name-specifier. We will
24160 enter the class scope itself while processing the members. */
24162 pop_scope (pushed_scope
);
24164 if (invalid_explicit_specialization_p
)
24166 end_specialization ();
24167 --parser
->num_template_parameter_lists
;
24171 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
24172 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
24173 CLASSTYPE_FINAL (type
) = 1;
24175 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24179 /* Parse a class-key.
24186 Returns the kind of class-key specified, or none_type to indicate
24189 static enum tag_types
24190 cp_parser_class_key (cp_parser
* parser
)
24193 enum tag_types tag_type
;
24195 /* Look for the class-key. */
24196 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
24200 /* Check to see if the TOKEN is a class-key. */
24201 tag_type
= cp_parser_token_is_class_key (token
);
24203 cp_parser_error (parser
, "expected class-key");
24207 /* Parse a type-parameter-key.
24209 type-parameter-key:
24215 cp_parser_type_parameter_key (cp_parser
* parser
)
24217 /* Look for the type-parameter-key. */
24218 enum tag_types tag_type
= none_type
;
24219 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24220 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
24222 cp_lexer_consume_token (parser
->lexer
);
24223 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
24224 /* typename is not allowed in a template template parameter
24225 by the standard until C++17. */
24226 pedwarn (token
->location
, OPT_Wpedantic
,
24227 "ISO C++ forbids typename key in template template parameter;"
24228 " use %<-std=c++17%> or %<-std=gnu++17%>");
24231 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
24236 /* Parse an (optional) member-specification.
24238 member-specification:
24239 member-declaration member-specification [opt]
24240 access-specifier : member-specification [opt] */
24243 cp_parser_member_specification_opt (cp_parser
* parser
)
24250 /* Peek at the next token. */
24251 token
= cp_lexer_peek_token (parser
->lexer
);
24252 /* If it's a `}', or EOF then we've seen all the members. */
24253 if (token
->type
== CPP_CLOSE_BRACE
24254 || token
->type
== CPP_EOF
24255 || token
->type
== CPP_PRAGMA_EOL
)
24258 /* See if this token is a keyword. */
24259 keyword
= token
->keyword
;
24263 case RID_PROTECTED
:
24265 /* Consume the access-specifier. */
24266 cp_lexer_consume_token (parser
->lexer
);
24267 /* Remember which access-specifier is active. */
24268 current_access_specifier
= token
->u
.value
;
24269 /* Look for the `:'. */
24270 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24274 /* Accept #pragmas at class scope. */
24275 if (token
->type
== CPP_PRAGMA
)
24277 cp_parser_pragma (parser
, pragma_member
, NULL
);
24281 /* Otherwise, the next construction must be a
24282 member-declaration. */
24283 cp_parser_member_declaration (parser
);
24288 /* Parse a member-declaration.
24290 member-declaration:
24291 decl-specifier-seq [opt] member-declarator-list [opt] ;
24292 function-definition ; [opt]
24293 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24295 template-declaration
24298 member-declarator-list:
24300 member-declarator-list , member-declarator
24303 declarator pure-specifier [opt]
24304 declarator constant-initializer [opt]
24305 identifier [opt] : constant-expression
24309 member-declaration:
24310 __extension__ member-declaration
24313 declarator attributes [opt] pure-specifier [opt]
24314 declarator attributes [opt] constant-initializer [opt]
24315 identifier [opt] attributes [opt] : constant-expression
24319 member-declaration:
24320 static_assert-declaration */
24323 cp_parser_member_declaration (cp_parser
* parser
)
24325 cp_decl_specifier_seq decl_specifiers
;
24326 tree prefix_attributes
;
24328 int declares_class_or_enum
;
24330 cp_token
*token
= NULL
;
24331 cp_token
*decl_spec_token_start
= NULL
;
24332 cp_token
*initializer_token_start
= NULL
;
24333 int saved_pedantic
;
24334 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24336 /* Check for the `__extension__' keyword. */
24337 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24340 cp_parser_member_declaration (parser
);
24341 /* Restore the old value of the PEDANTIC flag. */
24342 pedantic
= saved_pedantic
;
24347 /* Check for a template-declaration. */
24348 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24350 /* An explicit specialization here is an error condition, and we
24351 expect the specialization handler to detect and report this. */
24352 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24353 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24354 cp_parser_explicit_specialization (parser
);
24356 cp_parser_template_declaration (parser
, /*member_p=*/true);
24360 /* Check for a template introduction. */
24361 else if (cp_parser_template_declaration_after_export (parser
, true))
24364 /* Check for a using-declaration. */
24365 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24367 if (cxx_dialect
< cxx11
)
24369 /* Parse the using-declaration. */
24370 cp_parser_using_declaration (parser
,
24371 /*access_declaration_p=*/false);
24377 bool alias_decl_expected
;
24378 cp_parser_parse_tentatively (parser
);
24379 decl
= cp_parser_alias_declaration (parser
);
24380 /* Note that if we actually see the '=' token after the
24381 identifier, cp_parser_alias_declaration commits the
24382 tentative parse. In that case, we really expect an
24383 alias-declaration. Otherwise, we expect a using
24385 alias_decl_expected
=
24386 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24387 cp_parser_parse_definitely (parser
);
24389 if (alias_decl_expected
)
24390 finish_member_declaration (decl
);
24392 cp_parser_using_declaration (parser
,
24393 /*access_declaration_p=*/false);
24398 /* Check for @defs. */
24399 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24402 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24403 ivar
= ivar_chains
;
24407 ivar
= TREE_CHAIN (member
);
24408 TREE_CHAIN (member
) = NULL_TREE
;
24409 finish_member_declaration (member
);
24414 /* If the next token is `static_assert' we have a static assertion. */
24415 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24417 cp_parser_static_assert (parser
, /*member_p=*/true);
24421 parser
->colon_corrects_to_scope_p
= false;
24423 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24426 /* Parse the decl-specifier-seq. */
24427 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24428 cp_parser_decl_specifier_seq (parser
,
24429 (CP_PARSER_FLAGS_OPTIONAL
24430 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
24432 &declares_class_or_enum
);
24433 /* Check for an invalid type-name. */
24434 if (!decl_specifiers
.any_type_specifiers_p
24435 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24437 /* If there is no declarator, then the decl-specifier-seq should
24439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24441 /* If there was no decl-specifier-seq, and the next token is a
24442 `;', then we have something like:
24448 Each member-declaration shall declare at least one member
24449 name of the class. */
24450 if (!decl_specifiers
.any_specifiers_p
)
24452 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24453 if (!in_system_header_at (token
->location
))
24455 gcc_rich_location
richloc (token
->location
);
24456 richloc
.add_fixit_remove ();
24457 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24464 /* See if this declaration is a friend. */
24465 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24466 /* If there were decl-specifiers, check to see if there was
24467 a class-declaration. */
24468 type
= check_tag_decl (&decl_specifiers
,
24469 /*explicit_type_instantiation_p=*/false);
24470 /* Nested classes have already been added to the class, but
24471 a `friend' needs to be explicitly registered. */
24474 /* If the `friend' keyword was present, the friend must
24475 be introduced with a class-key. */
24476 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24477 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24478 "in C++03 a class-key must be used "
24479 "when declaring a friend");
24482 template <typename T> struct A {
24483 friend struct A<T>::B;
24486 A<T>::B will be represented by a TYPENAME_TYPE, and
24487 therefore not recognized by check_tag_decl. */
24490 type
= decl_specifiers
.type
;
24491 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24492 type
= TREE_TYPE (type
);
24494 if (!type
|| !TYPE_P (type
))
24495 error_at (decl_spec_token_start
->location
,
24496 "friend declaration does not name a class or "
24499 make_friend_class (current_class_type
, type
,
24500 /*complain=*/true);
24502 /* If there is no TYPE, an error message will already have
24504 else if (!type
|| type
== error_mark_node
)
24506 /* An anonymous aggregate has to be handled specially; such
24507 a declaration really declares a data member (with a
24508 particular type), as opposed to a nested class. */
24509 else if (ANON_AGGR_TYPE_P (type
))
24512 if (decl_specifiers
.storage_class
!= sc_none
)
24513 error_at (decl_spec_token_start
->location
,
24514 "a storage class on an anonymous aggregate "
24515 "in class scope is not allowed");
24517 /* Remove constructors and such from TYPE, now that we
24518 know it is an anonymous aggregate. */
24519 fixup_anonymous_aggr (type
);
24520 /* And make the corresponding data member. */
24521 decl
= build_decl (decl_spec_token_start
->location
,
24522 FIELD_DECL
, NULL_TREE
, type
);
24523 /* Add it to the class. */
24524 finish_member_declaration (decl
);
24527 cp_parser_check_access_in_redeclaration
24529 decl_spec_token_start
->location
);
24534 bool assume_semicolon
= false;
24536 /* Clear attributes from the decl_specifiers but keep them
24537 around as prefix attributes that apply them to the entity
24539 prefix_attributes
= decl_specifiers
.attributes
;
24540 decl_specifiers
.attributes
= NULL_TREE
;
24542 /* See if these declarations will be friends. */
24543 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24545 /* Keep going until we hit the `;' at the end of the
24547 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24549 tree attributes
= NULL_TREE
;
24550 tree first_attribute
;
24552 bool named_bitfld
= false;
24554 /* Peek at the next token. */
24555 token
= cp_lexer_peek_token (parser
->lexer
);
24557 /* The following code wants to know early if it is a bit-field
24558 or some other declaration. Attributes can appear before
24559 the `:' token. Skip over them without consuming any tokens
24560 to peek if they are followed by `:'. */
24561 if (cp_next_tokens_can_be_attribute_p (parser
)
24562 || (token
->type
== CPP_NAME
24563 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24564 && (named_bitfld
= true)))
24567 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24568 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24571 /* Check for a bitfield declaration. */
24572 if (token
->type
== CPP_COLON
24573 || (token
->type
== CPP_NAME
24574 && token
== cp_lexer_peek_token (parser
->lexer
)
24575 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24576 && (named_bitfld
= true)))
24580 tree late_attributes
= NULL_TREE
;
24581 location_t id_location
24582 = cp_lexer_peek_token (parser
->lexer
)->location
;
24585 identifier
= cp_parser_identifier (parser
);
24587 identifier
= NULL_TREE
;
24589 /* Look for attributes that apply to the bitfield. */
24590 attributes
= cp_parser_attributes_opt (parser
);
24592 /* Consume the `:' token. */
24593 cp_lexer_consume_token (parser
->lexer
);
24595 /* Get the width of the bitfield. */
24596 width
= cp_parser_constant_expression (parser
, false, NULL
,
24597 cxx_dialect
>= cxx11
);
24599 /* In C++2A and as extension for C++11 and above we allow
24600 default member initializers for bit-fields. */
24601 initializer
= NULL_TREE
;
24602 if (cxx_dialect
>= cxx11
24603 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24604 || cp_lexer_next_token_is (parser
->lexer
,
24608 = cp_lexer_peek_token (parser
->lexer
)->location
;
24609 if (cxx_dialect
< cxx2a
24610 && !in_system_header_at (loc
)
24611 && identifier
!= NULL_TREE
)
24613 "default member initializers for bit-fields "
24614 "only available with %<-std=c++2a%> or "
24615 "%<-std=gnu++2a%>");
24617 initializer
= cp_parser_save_nsdmi (parser
);
24618 if (identifier
== NULL_TREE
)
24620 error_at (loc
, "default member initializer for "
24621 "unnamed bit-field");
24622 initializer
= NULL_TREE
;
24627 /* Look for attributes that apply to the bitfield after
24628 the `:' token and width. This is where GCC used to
24629 parse attributes in the past, pedwarn if there is
24630 a std attribute. */
24631 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24632 pedwarn (input_location
, OPT_Wpedantic
,
24633 "ISO C++ allows bit-field attributes only "
24634 "before the %<:%> token");
24636 late_attributes
= cp_parser_attributes_opt (parser
);
24639 attributes
= attr_chainon (attributes
, late_attributes
);
24641 /* Remember which attributes are prefix attributes and
24643 first_attribute
= attributes
;
24644 /* Combine the attributes. */
24645 attributes
= attr_chainon (prefix_attributes
, attributes
);
24647 /* Create the bitfield declaration. */
24648 decl
= grokbitfield (identifier
24649 ? make_id_declarator (NULL_TREE
,
24655 width
, initializer
,
24660 cp_declarator
*declarator
;
24661 tree asm_specification
;
24662 int ctor_dtor_or_conv_p
;
24663 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
24665 /* Parse the declarator. */
24667 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24668 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24669 &ctor_dtor_or_conv_p
,
24670 /*parenthesized_p=*/NULL
,
24672 friend_p
, static_p
);
24674 /* If something went wrong parsing the declarator, make sure
24675 that we at least consume some tokens. */
24676 if (declarator
== cp_error_declarator
)
24678 /* Skip to the end of the statement. */
24679 cp_parser_skip_to_end_of_statement (parser
);
24680 /* If the next token is not a semicolon, that is
24681 probably because we just skipped over the body of
24682 a function. So, we consume a semicolon if
24683 present, but do not issue an error message if it
24685 if (cp_lexer_next_token_is (parser
->lexer
,
24687 cp_lexer_consume_token (parser
->lexer
);
24691 if (declares_class_or_enum
& 2)
24692 cp_parser_check_for_definition_in_return_type
24693 (declarator
, decl_specifiers
.type
,
24694 decl_specifiers
.locations
[ds_type_spec
]);
24696 /* Look for an asm-specification. */
24697 asm_specification
= cp_parser_asm_specification_opt (parser
);
24698 /* Look for attributes that apply to the declaration. */
24699 attributes
= cp_parser_attributes_opt (parser
);
24700 /* Remember which attributes are prefix attributes and
24702 first_attribute
= attributes
;
24703 /* Combine the attributes. */
24704 attributes
= attr_chainon (prefix_attributes
, attributes
);
24706 /* If it's an `=', then we have a constant-initializer or a
24707 pure-specifier. It is not correct to parse the
24708 initializer before registering the member declaration
24709 since the member declaration should be in scope while
24710 its initializer is processed. However, the rest of the
24711 front end does not yet provide an interface that allows
24712 us to handle this correctly. */
24713 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24717 A pure-specifier shall be used only in the declaration of
24718 a virtual function.
24720 A member-declarator can contain a constant-initializer
24721 only if it declares a static member of integral or
24724 Therefore, if the DECLARATOR is for a function, we look
24725 for a pure-specifier; otherwise, we look for a
24726 constant-initializer. When we call `grokfield', it will
24727 perform more stringent semantics checks. */
24728 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24729 if (function_declarator_p (declarator
)
24730 || (decl_specifiers
.type
24731 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24732 && declarator
->kind
== cdk_id
24733 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24734 == FUNCTION_TYPE
)))
24735 initializer
= cp_parser_pure_specifier (parser
);
24736 else if (decl_specifiers
.storage_class
!= sc_static
)
24737 initializer
= cp_parser_save_nsdmi (parser
);
24738 else if (cxx_dialect
>= cxx11
)
24741 /* Don't require a constant rvalue in C++11, since we
24742 might want a reference constant. We'll enforce
24743 constancy later. */
24744 cp_lexer_consume_token (parser
->lexer
);
24745 /* Parse the initializer. */
24746 initializer
= cp_parser_initializer_clause (parser
,
24750 /* Parse the initializer. */
24751 initializer
= cp_parser_constant_initializer (parser
);
24753 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24754 && !function_declarator_p (declarator
))
24757 if (decl_specifiers
.storage_class
!= sc_static
)
24758 initializer
= cp_parser_save_nsdmi (parser
);
24760 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24762 /* Otherwise, there is no initializer. */
24764 initializer
= NULL_TREE
;
24766 /* See if we are probably looking at a function
24767 definition. We are certainly not looking at a
24768 member-declarator. Calling `grokfield' has
24769 side-effects, so we must not do it unless we are sure
24770 that we are looking at a member-declarator. */
24771 if (cp_parser_token_starts_function_definition_p
24772 (cp_lexer_peek_token (parser
->lexer
)))
24774 /* The grammar does not allow a pure-specifier to be
24775 used when a member function is defined. (It is
24776 possible that this fact is an oversight in the
24777 standard, since a pure function may be defined
24778 outside of the class-specifier. */
24779 if (initializer
&& initializer_token_start
)
24780 error_at (initializer_token_start
->location
,
24781 "pure-specifier on function-definition");
24782 decl
= cp_parser_save_member_function_body (parser
,
24786 if (parser
->fully_implicit_function_template_p
)
24787 decl
= finish_fully_implicit_template (parser
, decl
);
24788 /* If the member was not a friend, declare it here. */
24790 finish_member_declaration (decl
);
24791 /* Peek at the next token. */
24792 token
= cp_lexer_peek_token (parser
->lexer
);
24793 /* If the next token is a semicolon, consume it. */
24794 if (token
->type
== CPP_SEMICOLON
)
24796 location_t semicolon_loc
24797 = cp_lexer_consume_token (parser
->lexer
)->location
;
24798 gcc_rich_location
richloc (semicolon_loc
);
24799 richloc
.add_fixit_remove ();
24800 warning_at (&richloc
, OPT_Wextra_semi
,
24801 "extra %<;%> after in-class "
24802 "function definition");
24807 if (declarator
->kind
== cdk_function
)
24808 declarator
->id_loc
= token
->location
;
24809 /* Create the declaration. */
24810 decl
= grokfield (declarator
, &decl_specifiers
,
24811 initializer
, /*init_const_expr_p=*/true,
24812 asm_specification
, attributes
);
24813 if (parser
->fully_implicit_function_template_p
)
24816 finish_fully_implicit_template (parser
, 0);
24818 decl
= finish_fully_implicit_template (parser
, decl
);
24822 cp_finalize_omp_declare_simd (parser
, decl
);
24823 cp_finalize_oacc_routine (parser
, decl
, false);
24825 /* Reset PREFIX_ATTRIBUTES. */
24826 if (attributes
!= error_mark_node
)
24828 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24829 attributes
= TREE_CHAIN (attributes
);
24831 TREE_CHAIN (attributes
) = NULL_TREE
;
24834 /* If there is any qualification still in effect, clear it
24835 now; we will be starting fresh with the next declarator. */
24836 parser
->scope
= NULL_TREE
;
24837 parser
->qualifying_scope
= NULL_TREE
;
24838 parser
->object_scope
= NULL_TREE
;
24839 /* If it's a `,', then there are more declarators. */
24840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24842 cp_lexer_consume_token (parser
->lexer
);
24843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24845 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24846 gcc_rich_location
richloc (token
->location
);
24847 richloc
.add_fixit_remove ();
24848 error_at (&richloc
, "stray %<,%> at end of "
24849 "member declaration");
24852 /* If the next token isn't a `;', then we have a parse error. */
24853 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24856 /* The next token might be a ways away from where the
24857 actual semicolon is missing. Find the previous token
24858 and use that for our error position. */
24859 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24860 gcc_rich_location
richloc (token
->location
);
24861 richloc
.add_fixit_insert_after (";");
24862 error_at (&richloc
, "expected %<;%> at end of "
24863 "member declaration");
24865 /* Assume that the user meant to provide a semicolon. If
24866 we were to cp_parser_skip_to_end_of_statement, we might
24867 skip to a semicolon inside a member function definition
24868 and issue nonsensical error messages. */
24869 assume_semicolon
= true;
24874 /* Add DECL to the list of members. */
24876 /* Explicitly include, eg, NSDMIs, for better error
24877 recovery (c++/58650). */
24878 || !DECL_DECLARES_FUNCTION_P (decl
))
24879 finish_member_declaration (decl
);
24881 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24882 cp_parser_save_default_args (parser
, decl
);
24883 else if (TREE_CODE (decl
) == FIELD_DECL
24884 && DECL_INITIAL (decl
))
24885 /* Add DECL to the queue of NSDMI to be parsed later. */
24886 vec_safe_push (unparsed_nsdmis
, decl
);
24889 if (assume_semicolon
)
24894 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24896 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24899 /* Parse a pure-specifier.
24904 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24905 Otherwise, ERROR_MARK_NODE is returned. */
24908 cp_parser_pure_specifier (cp_parser
* parser
)
24912 /* Look for the `=' token. */
24913 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24914 return error_mark_node
;
24915 /* Look for the `0' token. */
24916 token
= cp_lexer_peek_token (parser
->lexer
);
24918 if (token
->type
== CPP_EOF
24919 || token
->type
== CPP_PRAGMA_EOL
)
24920 return error_mark_node
;
24922 cp_lexer_consume_token (parser
->lexer
);
24924 /* Accept = default or = delete in c++0x mode. */
24925 if (token
->keyword
== RID_DEFAULT
24926 || token
->keyword
== RID_DELETE
)
24928 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24929 return token
->u
.value
;
24932 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24933 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24935 cp_parser_error (parser
,
24936 "invalid pure specifier (only %<= 0%> is allowed)");
24937 cp_parser_skip_to_end_of_statement (parser
);
24938 return error_mark_node
;
24940 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24942 error_at (token
->location
, "templates may not be %<virtual%>");
24943 return error_mark_node
;
24946 return integer_zero_node
;
24949 /* Parse a constant-initializer.
24951 constant-initializer:
24952 = constant-expression
24954 Returns a representation of the constant-expression. */
24957 cp_parser_constant_initializer (cp_parser
* parser
)
24959 /* Look for the `=' token. */
24960 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24961 return error_mark_node
;
24963 /* It is invalid to write:
24965 struct S { static const int i = { 7 }; };
24968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24970 cp_parser_error (parser
,
24971 "a brace-enclosed initializer is not allowed here");
24972 /* Consume the opening brace. */
24973 matching_braces braces
;
24974 braces
.consume_open (parser
);
24975 /* Skip the initializer. */
24976 cp_parser_skip_to_closing_brace (parser
);
24977 /* Look for the trailing `}'. */
24978 braces
.require_close (parser
);
24980 return error_mark_node
;
24983 return cp_parser_constant_expression (parser
);
24986 /* Derived classes [gram.class.derived] */
24988 /* Parse a base-clause.
24991 : base-specifier-list
24993 base-specifier-list:
24994 base-specifier ... [opt]
24995 base-specifier-list , base-specifier ... [opt]
24997 Returns a TREE_LIST representing the base-classes, in the order in
24998 which they were declared. The representation of each node is as
24999 described by cp_parser_base_specifier.
25001 In the case that no bases are specified, this function will return
25002 NULL_TREE, not ERROR_MARK_NODE. */
25005 cp_parser_base_clause (cp_parser
* parser
)
25007 tree bases
= NULL_TREE
;
25009 /* Look for the `:' that begins the list. */
25010 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25012 /* Scan the base-specifier-list. */
25017 bool pack_expansion_p
= false;
25019 /* Look for the base-specifier. */
25020 base
= cp_parser_base_specifier (parser
);
25021 /* Look for the (optional) ellipsis. */
25022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25024 /* Consume the `...'. */
25025 cp_lexer_consume_token (parser
->lexer
);
25027 pack_expansion_p
= true;
25030 /* Add BASE to the front of the list. */
25031 if (base
&& base
!= error_mark_node
)
25033 if (pack_expansion_p
)
25034 /* Make this a pack expansion type. */
25035 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
25037 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
25039 TREE_CHAIN (base
) = bases
;
25043 /* Peek at the next token. */
25044 token
= cp_lexer_peek_token (parser
->lexer
);
25045 /* If it's not a comma, then the list is complete. */
25046 if (token
->type
!= CPP_COMMA
)
25048 /* Consume the `,'. */
25049 cp_lexer_consume_token (parser
->lexer
);
25052 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25053 base class had a qualified name. However, the next name that
25054 appears is certainly not qualified. */
25055 parser
->scope
= NULL_TREE
;
25056 parser
->qualifying_scope
= NULL_TREE
;
25057 parser
->object_scope
= NULL_TREE
;
25059 return nreverse (bases
);
25062 /* Parse a base-specifier.
25065 :: [opt] nested-name-specifier [opt] class-name
25066 virtual access-specifier [opt] :: [opt] nested-name-specifier
25068 access-specifier virtual [opt] :: [opt] nested-name-specifier
25071 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25072 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25073 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25074 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25077 cp_parser_base_specifier (cp_parser
* parser
)
25081 bool virtual_p
= false;
25082 bool duplicate_virtual_error_issued_p
= false;
25083 bool duplicate_access_error_issued_p
= false;
25084 bool class_scope_p
, template_p
;
25085 tree access
= access_default_node
;
25088 /* Process the optional `virtual' and `access-specifier'. */
25091 /* Peek at the next token. */
25092 token
= cp_lexer_peek_token (parser
->lexer
);
25093 /* Process `virtual'. */
25094 switch (token
->keyword
)
25097 /* If `virtual' appears more than once, issue an error. */
25098 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
25100 cp_parser_error (parser
,
25101 "%<virtual%> specified more than once in base-specifier");
25102 duplicate_virtual_error_issued_p
= true;
25107 /* Consume the `virtual' token. */
25108 cp_lexer_consume_token (parser
->lexer
);
25113 case RID_PROTECTED
:
25115 /* If more than one access specifier appears, issue an
25117 if (access
!= access_default_node
25118 && !duplicate_access_error_issued_p
)
25120 cp_parser_error (parser
,
25121 "more than one access specifier in base-specifier");
25122 duplicate_access_error_issued_p
= true;
25125 access
= ridpointers
[(int) token
->keyword
];
25127 /* Consume the access-specifier. */
25128 cp_lexer_consume_token (parser
->lexer
);
25137 /* It is not uncommon to see programs mechanically, erroneously, use
25138 the 'typename' keyword to denote (dependent) qualified types
25139 as base classes. */
25140 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25142 token
= cp_lexer_peek_token (parser
->lexer
);
25143 if (!processing_template_decl
)
25144 error_at (token
->location
,
25145 "keyword %<typename%> not allowed outside of templates");
25147 error_at (token
->location
,
25148 "keyword %<typename%> not allowed in this context "
25149 "(the base class is implicitly a type)");
25150 cp_lexer_consume_token (parser
->lexer
);
25153 /* Look for the optional `::' operator. */
25154 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
25155 /* Look for the nested-name-specifier. The simplest way to
25160 The keyword `typename' is not permitted in a base-specifier or
25161 mem-initializer; in these contexts a qualified name that
25162 depends on a template-parameter is implicitly assumed to be a
25165 is to pretend that we have seen the `typename' keyword at this
25167 cp_parser_nested_name_specifier_opt (parser
,
25168 /*typename_keyword_p=*/true,
25169 /*check_dependency_p=*/true,
25171 /*is_declaration=*/true);
25172 /* If the base class is given by a qualified name, assume that names
25173 we see are type names or templates, as appropriate. */
25174 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
25175 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
25178 && cp_lexer_next_token_is_decltype (parser
->lexer
))
25179 /* DR 950 allows decltype as a base-specifier. */
25180 type
= cp_parser_decltype (parser
);
25183 /* Otherwise, look for the class-name. */
25184 type
= cp_parser_class_name (parser
,
25188 /*check_dependency_p=*/true,
25189 /*class_head_p=*/false,
25190 /*is_declaration=*/true);
25191 type
= TREE_TYPE (type
);
25194 if (type
== error_mark_node
)
25195 return error_mark_node
;
25197 return finish_base_specifier (type
, access
, virtual_p
);
25200 /* Exception handling [gram.exception] */
25202 /* Parse an (optional) noexcept-specification.
25204 noexcept-specification:
25205 noexcept ( constant-expression ) [opt]
25207 If no noexcept-specification is present, returns NULL_TREE.
25208 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25209 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25210 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25211 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25212 in which case a boolean condition is returned instead. */
25215 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
25216 bool require_constexpr
,
25217 bool* consumed_expr
,
25221 const char *saved_message
;
25223 /* Peek at the next token. */
25224 token
= cp_lexer_peek_token (parser
->lexer
);
25226 /* Is it a noexcept-specification? */
25227 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
25230 cp_lexer_consume_token (parser
->lexer
);
25232 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
25234 matching_parens parens
;
25235 parens
.consume_open (parser
);
25237 tree save_ccp
= current_class_ptr
;
25238 tree save_ccr
= current_class_ref
;
25240 if (current_class_type
)
25241 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
25243 if (require_constexpr
)
25245 /* Types may not be defined in an exception-specification. */
25246 saved_message
= parser
->type_definition_forbidden_message
;
25247 parser
->type_definition_forbidden_message
25248 = G_("types may not be defined in an exception-specification");
25250 bool non_constant_p
;
25252 = cp_parser_constant_expression (parser
,
25253 /*allow_non_constant=*/true,
25256 && !require_potential_rvalue_constant_expression (expr
))
25259 return_cond
= true;
25262 /* Restore the saved message. */
25263 parser
->type_definition_forbidden_message
= saved_message
;
25267 expr
= cp_parser_expression (parser
);
25268 *consumed_expr
= true;
25271 parens
.require_close (parser
);
25273 current_class_ptr
= save_ccp
;
25274 current_class_ref
= save_ccr
;
25278 expr
= boolean_true_node
;
25279 if (!require_constexpr
)
25280 *consumed_expr
= false;
25283 /* We cannot build a noexcept-spec right away because this will check
25284 that expr is a constexpr. */
25286 return build_noexcept_spec (expr
, tf_warning_or_error
);
25294 /* Parse an (optional) exception-specification.
25296 exception-specification:
25297 throw ( type-id-list [opt] )
25299 Returns a TREE_LIST representing the exception-specification. The
25300 TREE_VALUE of each node is a type. */
25303 cp_parser_exception_specification_opt (cp_parser
* parser
)
25307 const char *saved_message
;
25309 /* Peek at the next token. */
25310 token
= cp_lexer_peek_token (parser
->lexer
);
25312 /* Is it a noexcept-specification? */
25313 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
25315 if (type_id_list
!= NULL_TREE
)
25316 return type_id_list
;
25318 /* If it's not `throw', then there's no exception-specification. */
25319 if (!cp_parser_is_keyword (token
, RID_THROW
))
25322 location_t loc
= token
->location
;
25324 /* Consume the `throw'. */
25325 cp_lexer_consume_token (parser
->lexer
);
25327 /* Look for the `('. */
25328 matching_parens parens
;
25329 parens
.require_open (parser
);
25331 /* Peek at the next token. */
25332 token
= cp_lexer_peek_token (parser
->lexer
);
25333 /* If it's not a `)', then there is a type-id-list. */
25334 if (token
->type
!= CPP_CLOSE_PAREN
)
25336 /* Types may not be defined in an exception-specification. */
25337 saved_message
= parser
->type_definition_forbidden_message
;
25338 parser
->type_definition_forbidden_message
25339 = G_("types may not be defined in an exception-specification");
25340 /* Parse the type-id-list. */
25341 type_id_list
= cp_parser_type_id_list (parser
);
25342 /* Restore the saved message. */
25343 parser
->type_definition_forbidden_message
= saved_message
;
25345 if (cxx_dialect
>= cxx17
)
25347 error_at (loc
, "ISO C++17 does not allow dynamic exception "
25349 type_id_list
= NULL_TREE
;
25351 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
25352 warning_at (loc
, OPT_Wdeprecated
,
25353 "dynamic exception specifications are deprecated in "
25356 /* In C++17, throw() is equivalent to noexcept (true). throw()
25357 is deprecated in C++11 and above as well, but is still widely used,
25358 so don't warn about it yet. */
25359 else if (cxx_dialect
>= cxx17
)
25360 type_id_list
= noexcept_true_spec
;
25362 type_id_list
= empty_except_spec
;
25364 /* Look for the `)'. */
25365 parens
.require_close (parser
);
25367 return type_id_list
;
25370 /* Parse an (optional) type-id-list.
25374 type-id-list , type-id ... [opt]
25376 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25377 in the order that the types were presented. */
25380 cp_parser_type_id_list (cp_parser
* parser
)
25382 tree types
= NULL_TREE
;
25389 token
= cp_lexer_peek_token (parser
->lexer
);
25391 /* Get the next type-id. */
25392 type
= cp_parser_type_id (parser
);
25393 /* Check for invalid 'auto'. */
25394 if (flag_concepts
&& type_uses_auto (type
))
25396 error_at (token
->location
,
25397 "invalid use of %<auto%> in exception-specification");
25398 type
= error_mark_node
;
25400 /* Parse the optional ellipsis. */
25401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25403 /* Consume the `...'. */
25404 cp_lexer_consume_token (parser
->lexer
);
25406 /* Turn the type into a pack expansion expression. */
25407 type
= make_pack_expansion (type
);
25409 /* Add it to the list. */
25410 types
= add_exception_specifier (types
, type
, /*complain=*/1);
25411 /* Peek at the next token. */
25412 token
= cp_lexer_peek_token (parser
->lexer
);
25413 /* If it is not a `,', we are done. */
25414 if (token
->type
!= CPP_COMMA
)
25416 /* Consume the `,'. */
25417 cp_lexer_consume_token (parser
->lexer
);
25420 return nreverse (types
);
25423 /* Parse a try-block.
25426 try compound-statement handler-seq */
25429 cp_parser_try_block (cp_parser
* parser
)
25433 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25434 if (parser
->in_function_body
25435 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
25436 && cxx_dialect
< cxx2a
)
25437 pedwarn (input_location
, 0,
25438 "%<try%> in %<constexpr%> function only "
25439 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25441 try_block
= begin_try_block ();
25442 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25443 finish_try_block (try_block
);
25444 cp_parser_handler_seq (parser
);
25445 finish_handler_sequence (try_block
);
25450 /* Parse a function-try-block.
25452 function-try-block:
25453 try ctor-initializer [opt] function-body handler-seq */
25456 cp_parser_function_try_block (cp_parser
* parser
)
25458 tree compound_stmt
;
25461 /* Look for the `try' keyword. */
25462 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25464 /* Let the rest of the front end know where we are. */
25465 try_block
= begin_function_try_block (&compound_stmt
);
25466 /* Parse the function-body. */
25467 cp_parser_ctor_initializer_opt_and_function_body
25468 (parser
, /*in_function_try_block=*/true);
25469 /* We're done with the `try' part. */
25470 finish_function_try_block (try_block
);
25471 /* Parse the handlers. */
25472 cp_parser_handler_seq (parser
);
25473 /* We're done with the handlers. */
25474 finish_function_handler_sequence (try_block
, compound_stmt
);
25477 /* Parse a handler-seq.
25480 handler handler-seq [opt] */
25483 cp_parser_handler_seq (cp_parser
* parser
)
25489 /* Parse the handler. */
25490 cp_parser_handler (parser
);
25491 /* Peek at the next token. */
25492 token
= cp_lexer_peek_token (parser
->lexer
);
25493 /* If it's not `catch' then there are no more handlers. */
25494 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25499 /* Parse a handler.
25502 catch ( exception-declaration ) compound-statement */
25505 cp_parser_handler (cp_parser
* parser
)
25510 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25511 handler
= begin_handler ();
25512 matching_parens parens
;
25513 parens
.require_open (parser
);
25514 declaration
= cp_parser_exception_declaration (parser
);
25515 finish_handler_parms (declaration
, handler
);
25516 parens
.require_close (parser
);
25517 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25518 finish_handler (handler
);
25521 /* Parse an exception-declaration.
25523 exception-declaration:
25524 type-specifier-seq declarator
25525 type-specifier-seq abstract-declarator
25529 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25530 ellipsis variant is used. */
25533 cp_parser_exception_declaration (cp_parser
* parser
)
25535 cp_decl_specifier_seq type_specifiers
;
25536 cp_declarator
*declarator
;
25537 const char *saved_message
;
25539 /* If it's an ellipsis, it's easy to handle. */
25540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25542 /* Consume the `...' token. */
25543 cp_lexer_consume_token (parser
->lexer
);
25547 /* Types may not be defined in exception-declarations. */
25548 saved_message
= parser
->type_definition_forbidden_message
;
25549 parser
->type_definition_forbidden_message
25550 = G_("types may not be defined in exception-declarations");
25552 /* Parse the type-specifier-seq. */
25553 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
25554 /*is_declaration=*/true,
25555 /*is_trailing_return=*/false,
25557 /* If it's a `)', then there is no declarator. */
25558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25561 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25562 CP_PARSER_FLAGS_NONE
,
25563 /*ctor_dtor_or_conv_p=*/NULL
,
25564 /*parenthesized_p=*/NULL
,
25565 /*member_p=*/false,
25566 /*friend_p=*/false,
25567 /*static_p=*/false);
25569 /* Restore the saved message. */
25570 parser
->type_definition_forbidden_message
= saved_message
;
25572 if (!type_specifiers
.any_specifiers_p
)
25573 return error_mark_node
;
25575 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25578 /* Parse a throw-expression.
25581 throw assignment-expression [opt]
25583 Returns a THROW_EXPR representing the throw-expression. */
25586 cp_parser_throw_expression (cp_parser
* parser
)
25591 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25592 token
= cp_lexer_peek_token (parser
->lexer
);
25593 /* Figure out whether or not there is an assignment-expression
25594 following the "throw" keyword. */
25595 if (token
->type
== CPP_COMMA
25596 || token
->type
== CPP_SEMICOLON
25597 || token
->type
== CPP_CLOSE_PAREN
25598 || token
->type
== CPP_CLOSE_SQUARE
25599 || token
->type
== CPP_CLOSE_BRACE
25600 || token
->type
== CPP_COLON
)
25601 expression
= NULL_TREE
;
25603 expression
= cp_parser_assignment_expression (parser
);
25605 return build_throw (expression
);
25608 /* GNU Extensions */
25610 /* Parse an (optional) asm-specification.
25613 asm ( string-literal )
25615 If the asm-specification is present, returns a STRING_CST
25616 corresponding to the string-literal. Otherwise, returns
25620 cp_parser_asm_specification_opt (cp_parser
* parser
)
25623 tree asm_specification
;
25625 /* Peek at the next token. */
25626 token
= cp_lexer_peek_token (parser
->lexer
);
25627 /* If the next token isn't the `asm' keyword, then there's no
25628 asm-specification. */
25629 if (!cp_parser_is_keyword (token
, RID_ASM
))
25632 /* Consume the `asm' token. */
25633 cp_lexer_consume_token (parser
->lexer
);
25634 /* Look for the `('. */
25635 matching_parens parens
;
25636 parens
.require_open (parser
);
25638 /* Look for the string-literal. */
25639 asm_specification
= cp_parser_string_literal (parser
, false, false);
25641 /* Look for the `)'. */
25642 parens
.require_close (parser
);
25644 return asm_specification
;
25647 /* Parse an asm-operand-list.
25651 asm-operand-list , asm-operand
25654 string-literal ( expression )
25655 [ string-literal ] string-literal ( expression )
25657 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25658 each node is the expression. The TREE_PURPOSE is itself a
25659 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25660 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25661 is a STRING_CST for the string literal before the parenthesis. Returns
25662 ERROR_MARK_NODE if any of the operands are invalid. */
25665 cp_parser_asm_operand_list (cp_parser
* parser
)
25667 tree asm_operands
= NULL_TREE
;
25668 bool invalid_operands
= false;
25672 tree string_literal
;
25676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25678 /* Consume the `[' token. */
25679 cp_lexer_consume_token (parser
->lexer
);
25680 /* Read the operand name. */
25681 name
= cp_parser_identifier (parser
);
25682 if (name
!= error_mark_node
)
25683 name
= build_string (IDENTIFIER_LENGTH (name
),
25684 IDENTIFIER_POINTER (name
));
25685 /* Look for the closing `]'. */
25686 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25690 /* Look for the string-literal. */
25691 string_literal
= cp_parser_string_literal (parser
, false, false);
25693 /* Look for the `('. */
25694 matching_parens parens
;
25695 parens
.require_open (parser
);
25696 /* Parse the expression. */
25697 expression
= cp_parser_expression (parser
);
25698 /* Look for the `)'. */
25699 parens
.require_close (parser
);
25701 if (name
== error_mark_node
25702 || string_literal
== error_mark_node
25703 || expression
== error_mark_node
)
25704 invalid_operands
= true;
25706 /* Add this operand to the list. */
25707 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25710 /* If the next token is not a `,', there are no more
25712 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25714 /* Consume the `,'. */
25715 cp_lexer_consume_token (parser
->lexer
);
25718 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25721 /* Parse an asm-clobber-list.
25725 asm-clobber-list , string-literal
25727 Returns a TREE_LIST, indicating the clobbers in the order that they
25728 appeared. The TREE_VALUE of each node is a STRING_CST. */
25731 cp_parser_asm_clobber_list (cp_parser
* parser
)
25733 tree clobbers
= NULL_TREE
;
25737 tree string_literal
;
25739 /* Look for the string literal. */
25740 string_literal
= cp_parser_string_literal (parser
, false, false);
25741 /* Add it to the list. */
25742 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25743 /* If the next token is not a `,', then the list is
25745 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25747 /* Consume the `,' token. */
25748 cp_lexer_consume_token (parser
->lexer
);
25754 /* Parse an asm-label-list.
25758 asm-label-list , identifier
25760 Returns a TREE_LIST, indicating the labels in the order that they
25761 appeared. The TREE_VALUE of each node is a label. */
25764 cp_parser_asm_label_list (cp_parser
* parser
)
25766 tree labels
= NULL_TREE
;
25770 tree identifier
, label
, name
;
25772 /* Look for the identifier. */
25773 identifier
= cp_parser_identifier (parser
);
25774 if (!error_operand_p (identifier
))
25776 label
= lookup_label (identifier
);
25777 if (TREE_CODE (label
) == LABEL_DECL
)
25779 TREE_USED (label
) = 1;
25780 check_goto (label
);
25781 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25782 IDENTIFIER_POINTER (identifier
));
25783 labels
= tree_cons (name
, label
, labels
);
25786 /* If the next token is not a `,', then the list is
25788 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25790 /* Consume the `,' token. */
25791 cp_lexer_consume_token (parser
->lexer
);
25794 return nreverse (labels
);
25797 /* Return TRUE iff the next tokens in the stream are possibly the
25798 beginning of a GNU extension attribute. */
25801 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25803 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25806 /* Return TRUE iff the next tokens in the stream are possibly the
25807 beginning of a standard C++-11 attribute specifier. */
25810 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25812 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25815 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25816 beginning of a standard C++-11 attribute specifier. */
25819 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25821 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25823 return (cxx_dialect
>= cxx11
25824 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25825 || (token
->type
== CPP_OPEN_SQUARE
25826 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25827 && token
->type
== CPP_OPEN_SQUARE
)));
25830 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25831 beginning of a GNU extension attribute. */
25834 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25836 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25838 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25841 /* Return true iff the next tokens can be the beginning of either a
25842 GNU attribute list, or a standard C++11 attribute sequence. */
25845 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25847 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25848 || cp_next_tokens_can_be_std_attribute_p (parser
));
25851 /* Return true iff the next Nth tokens can be the beginning of either
25852 a GNU attribute list, or a standard C++11 attribute sequence. */
25855 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25857 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25858 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25861 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25862 of GNU attributes, or return NULL. */
25865 cp_parser_attributes_opt (cp_parser
*parser
)
25867 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25868 return cp_parser_gnu_attributes_opt (parser
);
25869 return cp_parser_std_attribute_spec_seq (parser
);
25872 /* Parse an (optional) series of attributes.
25875 attributes attribute
25878 __attribute__ (( attribute-list [opt] ))
25880 The return value is as for cp_parser_gnu_attribute_list. */
25883 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25885 tree attributes
= NULL_TREE
;
25887 temp_override
<bool> cleanup
25888 (parser
->auto_is_implicit_function_template_parm_p
, false);
25893 tree attribute_list
;
25896 /* Peek at the next token. */
25897 token
= cp_lexer_peek_token (parser
->lexer
);
25898 /* If it's not `__attribute__', then we're done. */
25899 if (token
->keyword
!= RID_ATTRIBUTE
)
25902 /* Consume the `__attribute__' keyword. */
25903 cp_lexer_consume_token (parser
->lexer
);
25904 /* Look for the two `(' tokens. */
25905 matching_parens outer_parens
;
25906 if (!outer_parens
.require_open (parser
))
25908 matching_parens inner_parens
;
25909 if (!inner_parens
.require_open (parser
))
25912 /* Peek at the next token. */
25913 token
= cp_lexer_peek_token (parser
->lexer
);
25914 if (token
->type
!= CPP_CLOSE_PAREN
)
25915 /* Parse the attribute-list. */
25916 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25918 /* If the next token is a `)', then there is no attribute
25920 attribute_list
= NULL
;
25922 /* Look for the two `)' tokens. */
25923 if (!inner_parens
.require_close (parser
))
25925 if (!outer_parens
.require_close (parser
))
25928 cp_parser_skip_to_end_of_statement (parser
);
25930 /* Add these new attributes to the list. */
25931 attributes
= attr_chainon (attributes
, attribute_list
);
25937 /* Parse a GNU attribute-list.
25941 attribute-list , attribute
25945 identifier ( identifier )
25946 identifier ( identifier , expression-list )
25947 identifier ( expression-list )
25949 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25950 to an attribute. The TREE_PURPOSE of each node is the identifier
25951 indicating which attribute is in use. The TREE_VALUE represents
25952 the arguments, if any. */
25955 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25957 tree attribute_list
= NULL_TREE
;
25958 bool save_translate_strings_p
= parser
->translate_strings_p
;
25960 /* Don't create wrapper nodes within attributes: the
25961 handlers don't know how to handle them. */
25962 auto_suppress_location_wrappers sentinel
;
25964 parser
->translate_strings_p
= false;
25971 /* Look for the identifier. We also allow keywords here; for
25972 example `__attribute__ ((const))' is legal. */
25973 token
= cp_lexer_peek_token (parser
->lexer
);
25974 if (token
->type
== CPP_NAME
25975 || token
->type
== CPP_KEYWORD
)
25977 tree arguments
= NULL_TREE
;
25979 /* Consume the token, but save it since we need it for the
25980 SIMD enabled function parsing. */
25981 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25983 /* Save away the identifier that indicates which attribute
25985 identifier
= (token
->type
== CPP_KEYWORD
)
25986 /* For keywords, use the canonical spelling, not the
25987 parsed identifier. */
25988 ? ridpointers
[(int) token
->keyword
]
25989 : id_token
->u
.value
;
25991 identifier
= canonicalize_attr_name (identifier
);
25992 attribute
= build_tree_list (identifier
, NULL_TREE
);
25994 /* Peek at the next token. */
25995 token
= cp_lexer_peek_token (parser
->lexer
);
25996 /* If it's an `(', then parse the attribute arguments. */
25997 if (token
->type
== CPP_OPEN_PAREN
)
25999 vec
<tree
, va_gc
> *vec
;
26000 int attr_flag
= (attribute_takes_identifier_p (identifier
)
26001 ? id_attr
: normal_attr
);
26002 vec
= cp_parser_parenthesized_expression_list
26003 (parser
, attr_flag
, /*cast_p=*/false,
26004 /*allow_expansion_p=*/false,
26005 /*non_constant_p=*/NULL
);
26007 arguments
= error_mark_node
;
26010 arguments
= build_tree_list_vec (vec
);
26011 release_tree_vector (vec
);
26013 /* Save the arguments away. */
26014 TREE_VALUE (attribute
) = arguments
;
26017 if (arguments
!= error_mark_node
)
26019 /* Add this attribute to the list. */
26020 TREE_CHAIN (attribute
) = attribute_list
;
26021 attribute_list
= attribute
;
26024 token
= cp_lexer_peek_token (parser
->lexer
);
26026 /* Unless EXACTLY_ONE is set look for more attributes.
26027 If the next token isn't a `,', we're done. */
26028 if (exactly_one
|| token
->type
!= CPP_COMMA
)
26031 /* Consume the comma and keep going. */
26032 cp_lexer_consume_token (parser
->lexer
);
26034 parser
->translate_strings_p
= save_translate_strings_p
;
26036 /* We built up the list in reverse order. */
26037 return nreverse (attribute_list
);
26040 /* Parse a standard C++11 attribute.
26042 The returned representation is a TREE_LIST which TREE_PURPOSE is
26043 the scoped name of the attribute, and the TREE_VALUE is its
26046 Note that the scoped name of the attribute is itself a TREE_LIST
26047 which TREE_PURPOSE is the namespace of the attribute, and
26048 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26049 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26050 and which TREE_PURPOSE is directly the attribute name.
26052 Clients of the attribute code should use get_attribute_namespace
26053 and get_attribute_name to get the actual namespace and name of
26054 attributes, regardless of their being GNU or C++11 attributes.
26057 attribute-token attribute-argument-clause [opt]
26061 attribute-scoped-token
26063 attribute-scoped-token:
26064 attribute-namespace :: identifier
26066 attribute-namespace:
26069 attribute-argument-clause:
26070 ( balanced-token-seq )
26072 balanced-token-seq:
26073 balanced-token [opt]
26074 balanced-token-seq balanced-token
26077 ( balanced-token-seq )
26078 [ balanced-token-seq ]
26079 { balanced-token-seq }. */
26082 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
26084 tree attribute
, attr_id
= NULL_TREE
, arguments
;
26087 temp_override
<bool> cleanup
26088 (parser
->auto_is_implicit_function_template_parm_p
, false);
26090 /* First, parse name of the attribute, a.k.a attribute-token. */
26092 token
= cp_lexer_peek_token (parser
->lexer
);
26093 if (token
->type
== CPP_NAME
)
26094 attr_id
= token
->u
.value
;
26095 else if (token
->type
== CPP_KEYWORD
)
26096 attr_id
= ridpointers
[(int) token
->keyword
];
26097 else if (token
->flags
& NAMED_OP
)
26098 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26100 if (attr_id
== NULL_TREE
)
26103 cp_lexer_consume_token (parser
->lexer
);
26105 token
= cp_lexer_peek_token (parser
->lexer
);
26106 if (token
->type
== CPP_SCOPE
)
26108 /* We are seeing a scoped attribute token. */
26110 cp_lexer_consume_token (parser
->lexer
);
26112 error_at (token
->location
, "attribute using prefix used together "
26113 "with scoped attribute token");
26116 token
= cp_lexer_consume_token (parser
->lexer
);
26117 if (token
->type
== CPP_NAME
)
26118 attr_id
= token
->u
.value
;
26119 else if (token
->type
== CPP_KEYWORD
)
26120 attr_id
= ridpointers
[(int) token
->keyword
];
26121 else if (token
->flags
& NAMED_OP
)
26122 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26125 error_at (token
->location
,
26126 "expected an identifier for the attribute name");
26127 return error_mark_node
;
26130 attr_ns
= canonicalize_attr_name (attr_ns
);
26131 attr_id
= canonicalize_attr_name (attr_id
);
26132 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26134 token
= cp_lexer_peek_token (parser
->lexer
);
26138 attr_ns
= canonicalize_attr_name (attr_ns
);
26139 attr_id
= canonicalize_attr_name (attr_id
);
26140 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26145 attr_id
= canonicalize_attr_name (attr_id
);
26146 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
26148 /* C++11 noreturn attribute is equivalent to GNU's. */
26149 if (is_attribute_p ("noreturn", attr_id
))
26150 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26151 /* C++14 deprecated attribute is equivalent to GNU's. */
26152 else if (is_attribute_p ("deprecated", attr_id
))
26153 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26154 /* C++17 fallthrough attribute is equivalent to GNU's. */
26155 else if (is_attribute_p ("fallthrough", attr_id
))
26156 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26157 /* Transactional Memory TS optimize_for_synchronized attribute is
26158 equivalent to GNU transaction_callable. */
26159 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
26160 TREE_PURPOSE (attribute
)
26161 = get_identifier ("transaction_callable");
26162 /* Transactional Memory attributes are GNU attributes. */
26163 else if (tm_attr_to_mask (attr_id
))
26164 TREE_PURPOSE (attribute
) = attr_id
;
26167 /* Now parse the optional argument clause of the attribute. */
26169 if (token
->type
!= CPP_OPEN_PAREN
)
26173 vec
<tree
, va_gc
> *vec
;
26174 int attr_flag
= normal_attr
;
26176 if (attr_ns
== gnu_identifier
26177 && attribute_takes_identifier_p (attr_id
))
26178 /* A GNU attribute that takes an identifier in parameter. */
26179 attr_flag
= id_attr
;
26181 vec
= cp_parser_parenthesized_expression_list
26182 (parser
, attr_flag
, /*cast_p=*/false,
26183 /*allow_expansion_p=*/true,
26184 /*non_constant_p=*/NULL
);
26186 arguments
= error_mark_node
;
26189 arguments
= build_tree_list_vec (vec
);
26190 release_tree_vector (vec
);
26193 if (arguments
== error_mark_node
)
26194 attribute
= error_mark_node
;
26196 TREE_VALUE (attribute
) = arguments
;
26202 /* Check that the attribute ATTRIBUTE appears at most once in the
26203 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26204 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26205 isn't implemented yet in GCC. */
26208 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
26212 tree name
= get_attribute_name (attribute
);
26213 if (is_attribute_p ("noreturn", name
)
26214 && lookup_attribute ("noreturn", attributes
))
26215 error ("attribute %<noreturn%> can appear at most once "
26216 "in an attribute-list");
26217 else if (is_attribute_p ("deprecated", name
)
26218 && lookup_attribute ("deprecated", attributes
))
26219 error ("attribute %<deprecated%> can appear at most once "
26220 "in an attribute-list");
26224 /* Parse a list of standard C++-11 attributes.
26228 attribute-list , attribute[opt]
26230 attribute-list , attribute ...
26234 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
26236 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
26237 cp_token
*token
= NULL
;
26241 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
26242 if (attribute
== error_mark_node
)
26244 if (attribute
!= NULL_TREE
)
26246 cp_parser_check_std_attribute (attributes
, attribute
);
26247 TREE_CHAIN (attribute
) = attributes
;
26248 attributes
= attribute
;
26250 token
= cp_lexer_peek_token (parser
->lexer
);
26251 if (token
->type
== CPP_ELLIPSIS
)
26253 cp_lexer_consume_token (parser
->lexer
);
26254 if (attribute
== NULL_TREE
)
26255 error_at (token
->location
,
26256 "expected attribute before %<...%>");
26259 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
26260 if (pack
== error_mark_node
)
26261 return error_mark_node
;
26262 TREE_VALUE (attribute
) = pack
;
26264 token
= cp_lexer_peek_token (parser
->lexer
);
26266 if (token
->type
!= CPP_COMMA
)
26268 cp_lexer_consume_token (parser
->lexer
);
26270 attributes
= nreverse (attributes
);
26274 /* Parse a standard C++-11 attribute specifier.
26276 attribute-specifier:
26277 [ [ attribute-using-prefix [opt] attribute-list ] ]
26278 alignment-specifier
26280 attribute-using-prefix:
26281 using attribute-namespace :
26283 alignment-specifier:
26284 alignas ( type-id ... [opt] )
26285 alignas ( alignment-expression ... [opt] ). */
26288 cp_parser_std_attribute_spec (cp_parser
*parser
)
26290 tree attributes
= NULL_TREE
;
26291 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26293 if (token
->type
== CPP_OPEN_SQUARE
26294 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
26296 tree attr_ns
= NULL_TREE
;
26298 cp_lexer_consume_token (parser
->lexer
);
26299 cp_lexer_consume_token (parser
->lexer
);
26301 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26303 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26304 if (token
->type
== CPP_NAME
)
26305 attr_ns
= token
->u
.value
;
26306 else if (token
->type
== CPP_KEYWORD
)
26307 attr_ns
= ridpointers
[(int) token
->keyword
];
26308 else if (token
->flags
& NAMED_OP
)
26309 attr_ns
= get_identifier (cpp_type2name (token
->type
,
26312 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
26314 if (cxx_dialect
< cxx17
26315 && !in_system_header_at (input_location
))
26316 pedwarn (input_location
, 0,
26317 "attribute using prefix only available "
26318 "with %<-std=c++17%> or %<-std=gnu++17%>");
26320 cp_lexer_consume_token (parser
->lexer
);
26321 cp_lexer_consume_token (parser
->lexer
);
26322 cp_lexer_consume_token (parser
->lexer
);
26325 attr_ns
= NULL_TREE
;
26328 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
26330 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
26331 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
26332 cp_parser_skip_to_end_of_statement (parser
);
26334 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26335 when we are sure that we have actually parsed them. */
26336 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26342 /* Look for an alignment-specifier. */
26344 token
= cp_lexer_peek_token (parser
->lexer
);
26346 if (token
->type
!= CPP_KEYWORD
26347 || token
->keyword
!= RID_ALIGNAS
)
26350 cp_lexer_consume_token (parser
->lexer
);
26351 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26353 matching_parens parens
;
26354 if (!parens
.require_open (parser
))
26355 return error_mark_node
;
26357 cp_parser_parse_tentatively (parser
);
26358 alignas_expr
= cp_parser_type_id (parser
);
26360 if (!cp_parser_parse_definitely (parser
))
26362 alignas_expr
= cp_parser_assignment_expression (parser
);
26363 if (alignas_expr
== error_mark_node
)
26364 cp_parser_skip_to_end_of_statement (parser
);
26365 if (alignas_expr
== NULL_TREE
26366 || alignas_expr
== error_mark_node
)
26367 return alignas_expr
;
26370 alignas_expr
= cxx_alignas_expr (alignas_expr
);
26371 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
26373 /* Handle alignas (pack...). */
26374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26376 cp_lexer_consume_token (parser
->lexer
);
26377 alignas_expr
= make_pack_expansion (alignas_expr
);
26380 /* Something went wrong, so don't build the attribute. */
26381 if (alignas_expr
== error_mark_node
)
26382 return error_mark_node
;
26384 if (!parens
.require_close (parser
))
26385 return error_mark_node
;
26387 /* Build the C++-11 representation of an 'aligned'
26390 = build_tree_list (build_tree_list (gnu_identifier
,
26391 aligned_identifier
), alignas_expr
);
26397 /* Parse a standard C++-11 attribute-specifier-seq.
26399 attribute-specifier-seq:
26400 attribute-specifier-seq [opt] attribute-specifier
26404 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
26406 tree attr_specs
= NULL_TREE
;
26407 tree attr_last
= NULL_TREE
;
26409 /* Don't create wrapper nodes within attributes: the
26410 handlers don't know how to handle them. */
26411 auto_suppress_location_wrappers sentinel
;
26415 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
26416 if (attr_spec
== NULL_TREE
)
26418 if (attr_spec
== error_mark_node
)
26419 return error_mark_node
;
26422 TREE_CHAIN (attr_last
) = attr_spec
;
26424 attr_specs
= attr_last
= attr_spec
;
26425 attr_last
= tree_last (attr_last
);
26431 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26432 return index of the first token after balanced-token, or N on failure. */
26435 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26438 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26440 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26442 case CPP_PRAGMA_EOL
:
26443 if (!parser
->lexer
->in_pragma
)
26447 /* Ran out of tokens. */
26449 case CPP_OPEN_PAREN
:
26452 case CPP_OPEN_BRACE
:
26455 case CPP_OPEN_SQUARE
:
26458 case CPP_CLOSE_PAREN
:
26461 case CPP_CLOSE_BRACE
:
26464 case CPP_CLOSE_SQUARE
:
26470 while (nparens
|| nbraces
|| nsquares
);
26474 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26475 return index of the first token after the GNU attribute tokens, or N on
26479 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26483 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26484 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26485 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26488 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26491 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26498 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26499 next token), return index of the first token after the standard C++11
26500 attribute tokens, or N on failure. */
26503 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26507 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26508 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26510 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26513 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26517 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26518 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26520 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26531 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26532 as the next token), return index of the first token after the attribute
26533 tokens, or N on failure. */
26536 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26538 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26539 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26540 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26543 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26544 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26545 current value of the PEDANTIC flag, regardless of whether or not
26546 the `__extension__' keyword is present. The caller is responsible
26547 for restoring the value of the PEDANTIC flag. */
26550 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26552 /* Save the old value of the PEDANTIC flag. */
26553 *saved_pedantic
= pedantic
;
26555 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26557 /* Consume the `__extension__' token. */
26558 cp_lexer_consume_token (parser
->lexer
);
26559 /* We're not being pedantic while the `__extension__' keyword is
26569 /* Parse a label declaration.
26572 __label__ label-declarator-seq ;
26574 label-declarator-seq:
26575 identifier , label-declarator-seq
26579 cp_parser_label_declaration (cp_parser
* parser
)
26581 /* Look for the `__label__' keyword. */
26582 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26588 /* Look for an identifier. */
26589 identifier
= cp_parser_identifier (parser
);
26590 /* If we failed, stop. */
26591 if (identifier
== error_mark_node
)
26593 /* Declare it as a label. */
26594 finish_label_decl (identifier
);
26595 /* If the next token is a `;', stop. */
26596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26598 /* Look for the `,' separating the label declarations. */
26599 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26602 /* Look for the final `;'. */
26603 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26606 // -------------------------------------------------------------------------- //
26609 // Parse a requires clause.
26611 // requires-clause:
26612 // 'requires' logical-or-expression
26614 // The required logical-or-expression must be a constant expression. Note
26615 // that we don't check that the expression is constepxr here. We defer until
26616 // we analyze constraints and then, we only check atomic constraints.
26618 cp_parser_requires_clause (cp_parser
*parser
)
26620 // Parse the requires clause so that it is not automatically folded.
26621 ++processing_template_decl
;
26622 tree expr
= cp_parser_binary_expression (parser
, false, false,
26623 PREC_NOT_OPERATOR
, NULL
);
26624 if (check_for_bare_parameter_packs (expr
))
26625 expr
= error_mark_node
;
26626 --processing_template_decl
;
26630 // Optionally parse a requires clause:
26632 cp_parser_requires_clause_opt (cp_parser
*parser
)
26634 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26635 if (tok
->keyword
!= RID_REQUIRES
)
26637 if (!flag_concepts
&& tok
->type
== CPP_NAME
26638 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26640 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26641 "%<requires%> only available with %<-fconcepts%>");
26642 /* Parse and discard the requires-clause. */
26643 cp_lexer_consume_token (parser
->lexer
);
26644 cp_parser_requires_clause (parser
);
26648 cp_lexer_consume_token (parser
->lexer
);
26649 return cp_parser_requires_clause (parser
);
26653 /*---------------------------------------------------------------------------
26654 Requires expressions
26655 ---------------------------------------------------------------------------*/
26657 /* Parse a requires expression
26659 requirement-expression:
26660 'requires' requirement-parameter-list [opt] requirement-body */
26662 cp_parser_requires_expression (cp_parser
*parser
)
26664 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26665 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26667 /* A requires-expression shall appear only within a concept
26668 definition or a requires-clause.
26670 TODO: Implement this diagnostic correctly. */
26671 if (!processing_template_decl
)
26673 error_at (loc
, "a requires expression cannot appear outside a template");
26674 cp_parser_skip_to_end_of_statement (parser
);
26675 return error_mark_node
;
26680 /* Local parameters are delared as variables within the scope
26681 of the expression. They are not visible past the end of
26682 the expression. Expressions within the requires-expression
26683 are unevaluated. */
26684 struct scope_sentinel
26688 ++cp_unevaluated_operand
;
26689 begin_scope (sk_block
, NULL_TREE
);
26694 pop_bindings_and_leave_scope ();
26695 --cp_unevaluated_operand
;
26699 /* Parse the optional parameter list. */
26700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26702 parms
= cp_parser_requirement_parameter_list (parser
);
26703 if (parms
== error_mark_node
)
26704 return error_mark_node
;
26709 /* Parse the requirement body. */
26710 reqs
= cp_parser_requirement_body (parser
);
26711 if (reqs
== error_mark_node
)
26712 return error_mark_node
;
26715 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26717 grokparms (parms
, &parms
);
26718 return finish_requires_expr (parms
, reqs
);
26721 /* Parse a parameterized requirement.
26723 requirement-parameter-list:
26724 '(' parameter-declaration-clause ')' */
26726 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26728 matching_parens parens
;
26729 if (!parens
.require_open (parser
))
26730 return error_mark_node
;
26733 = cp_parser_parameter_declaration_clause (parser
, CP_PARSER_FLAGS_NONE
);
26735 if (!parens
.require_close (parser
))
26736 return error_mark_node
;
26741 /* Parse the body of a requirement.
26744 '{' requirement-list '}' */
26746 cp_parser_requirement_body (cp_parser
*parser
)
26748 matching_braces braces
;
26749 if (!braces
.require_open (parser
))
26750 return error_mark_node
;
26752 tree reqs
= cp_parser_requirement_list (parser
);
26754 if (!braces
.require_close (parser
))
26755 return error_mark_node
;
26760 /* Parse a list of requirements.
26764 requirement-list ';' requirement[opt] */
26766 cp_parser_requirement_list (cp_parser
*parser
)
26768 tree result
= NULL_TREE
;
26771 tree req
= cp_parser_requirement (parser
);
26772 if (req
== error_mark_node
)
26773 return error_mark_node
;
26775 result
= tree_cons (NULL_TREE
, req
, result
);
26777 /* If we see a semi-colon, consume it. */
26778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26779 cp_lexer_consume_token (parser
->lexer
);
26781 /* Stop processing at the end of the list. */
26782 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26786 /* Reverse the order of requirements so they are analyzed in
26787 declaration order. */
26788 return nreverse (result
);
26791 /* Parse a syntactic requirement or type requirement.
26795 compound-requirement
26797 nested-requirement */
26799 cp_parser_requirement (cp_parser
*parser
)
26801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26802 return cp_parser_compound_requirement (parser
);
26803 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26804 return cp_parser_type_requirement (parser
);
26805 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26806 return cp_parser_nested_requirement (parser
);
26808 return cp_parser_simple_requirement (parser
);
26811 /* Parse a simple requirement.
26813 simple-requirement:
26816 cp_parser_simple_requirement (cp_parser
*parser
)
26818 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26819 if (!expr
|| expr
== error_mark_node
)
26820 return error_mark_node
;
26822 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26823 return error_mark_node
;
26825 return finish_simple_requirement (expr
);
26828 /* Parse a type requirement
26831 nested-name-specifier [opt] required-type-name ';'
26833 required-type-name:
26835 'template' [opt] simple-template-id */
26837 cp_parser_type_requirement (cp_parser
*parser
)
26839 cp_lexer_consume_token (parser
->lexer
);
26841 // Save the scope before parsing name specifiers.
26842 tree saved_scope
= parser
->scope
;
26843 tree saved_object_scope
= parser
->object_scope
;
26844 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26845 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26846 cp_parser_nested_name_specifier_opt (parser
,
26847 /*typename_keyword_p=*/true,
26848 /*check_dependency_p=*/false,
26850 /*is_declaration=*/false);
26853 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26855 cp_lexer_consume_token (parser
->lexer
);
26856 type
= cp_parser_template_id (parser
,
26857 /*template_keyword_p=*/true,
26858 /*check_dependency=*/false,
26859 /*tag_type=*/none_type
,
26860 /*is_declaration=*/false);
26861 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26862 /*complain=*/tf_error
);
26865 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26867 if (TREE_CODE (type
) == TYPE_DECL
)
26868 type
= TREE_TYPE (type
);
26870 parser
->scope
= saved_scope
;
26871 parser
->object_scope
= saved_object_scope
;
26872 parser
->qualifying_scope
= saved_qualifying_scope
;
26874 if (type
== error_mark_node
)
26875 cp_parser_skip_to_end_of_statement (parser
);
26877 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26878 return error_mark_node
;
26879 if (type
== error_mark_node
)
26880 return error_mark_node
;
26882 return finish_type_requirement (type
);
26885 /* Parse a compound requirement
26887 compound-requirement:
26888 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26890 cp_parser_compound_requirement (cp_parser
*parser
)
26892 /* Parse an expression enclosed in '{ }'s. */
26893 matching_braces braces
;
26894 if (!braces
.require_open (parser
))
26895 return error_mark_node
;
26897 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26898 if (!expr
|| expr
== error_mark_node
)
26899 return error_mark_node
;
26901 if (!braces
.require_close (parser
))
26902 return error_mark_node
;
26904 /* Parse the optional noexcept. */
26905 bool noexcept_p
= false;
26906 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26908 cp_lexer_consume_token (parser
->lexer
);
26912 /* Parse the optional trailing return type. */
26913 tree type
= NULL_TREE
;
26914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26916 cp_lexer_consume_token (parser
->lexer
);
26917 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26918 parser
->in_result_type_constraint_p
= true;
26919 type
= cp_parser_trailing_type_id (parser
);
26920 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26921 if (type
== error_mark_node
)
26922 return error_mark_node
;
26925 return finish_compound_requirement (expr
, type
, noexcept_p
);
26928 /* Parse a nested requirement. This is the same as a requires clause.
26930 nested-requirement:
26933 cp_parser_nested_requirement (cp_parser
*parser
)
26935 cp_lexer_consume_token (parser
->lexer
);
26936 tree req
= cp_parser_requires_clause (parser
);
26937 if (req
== error_mark_node
)
26938 return error_mark_node
;
26939 return finish_nested_requirement (req
);
26942 /* Support Functions */
26944 /* Return the appropriate prefer_type argument for lookup_name_real based on
26945 tag_type and template_mem_access. */
26948 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26950 /* DR 141: When looking in the current enclosing context for a template-name
26951 after -> or ., only consider class templates. */
26952 if (template_mem_access
)
26956 case none_type
: return 0; // No preference.
26957 case scope_type
: return 1; // Type or namespace.
26958 default: return 2; // Type only.
26962 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26963 NAME should have one of the representations used for an
26964 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26965 is returned. If PARSER->SCOPE is a dependent type, then a
26966 SCOPE_REF is returned.
26968 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26969 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26970 was formed. Abstractly, such entities should not be passed to this
26971 function, because they do not need to be looked up, but it is
26972 simpler to check for this special case here, rather than at the
26975 In cases not explicitly covered above, this function returns a
26976 DECL, OVERLOAD, or baselink representing the result of the lookup.
26977 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26980 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26981 (e.g., "struct") that was used. In that case bindings that do not
26982 refer to types are ignored.
26984 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26987 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26990 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26993 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26994 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26995 NULL_TREE otherwise. */
26998 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26999 enum tag_types tag_type
,
27002 bool check_dependency
,
27003 tree
*ambiguous_decls
,
27004 location_t name_location
)
27007 tree object_type
= parser
->context
->object_type
;
27009 /* Assume that the lookup will be unambiguous. */
27010 if (ambiguous_decls
)
27011 *ambiguous_decls
= NULL_TREE
;
27013 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27014 no longer valid. Note that if we are parsing tentatively, and
27015 the parse fails, OBJECT_TYPE will be automatically restored. */
27016 parser
->context
->object_type
= NULL_TREE
;
27018 if (name
== error_mark_node
)
27019 return error_mark_node
;
27021 /* A template-id has already been resolved; there is no lookup to
27023 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
27025 if (BASELINK_P (name
))
27027 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
27028 == TEMPLATE_ID_EXPR
);
27032 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27033 it should already have been checked to make sure that the name
27034 used matches the type being destroyed. */
27035 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
27039 /* Figure out to which type this destructor applies. */
27041 type
= parser
->scope
;
27042 else if (object_type
)
27043 type
= object_type
;
27045 type
= current_class_type
;
27046 /* If that's not a class type, there is no destructor. */
27047 if (!type
|| !CLASS_TYPE_P (type
))
27048 return error_mark_node
;
27050 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
27051 lazily_declare_fn (sfk_destructor
, type
);
27053 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
27056 return error_mark_node
;
27059 /* By this point, the NAME should be an ordinary identifier. If
27060 the id-expression was a qualified name, the qualifying scope is
27061 stored in PARSER->SCOPE at this point. */
27062 gcc_assert (identifier_p (name
));
27064 /* Perform the lookup. */
27069 if (parser
->scope
== error_mark_node
)
27070 return error_mark_node
;
27072 /* If the SCOPE is dependent, the lookup must be deferred until
27073 the template is instantiated -- unless we are explicitly
27074 looking up names in uninstantiated templates. Even then, we
27075 cannot look up the name if the scope is not a class type; it
27076 might, for example, be a template type parameter. */
27077 dependent_p
= (TYPE_P (parser
->scope
)
27078 && dependent_scope_p (parser
->scope
));
27079 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
27081 /* Defer lookup. */
27082 decl
= error_mark_node
;
27085 tree pushed_scope
= NULL_TREE
;
27087 /* If PARSER->SCOPE is a dependent type, then it must be a
27088 class type, and we must not be checking dependencies;
27089 otherwise, we would have processed this lookup above. So
27090 that PARSER->SCOPE is not considered a dependent base by
27091 lookup_member, we must enter the scope here. */
27093 pushed_scope
= push_scope (parser
->scope
);
27095 /* If the PARSER->SCOPE is a template specialization, it
27096 may be instantiated during name lookup. In that case,
27097 errors may be issued. Even if we rollback the current
27098 tentative parse, those errors are valid. */
27099 decl
= lookup_qualified_name (parser
->scope
, name
,
27100 prefer_type_arg (tag_type
),
27101 /*complain=*/true);
27103 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27104 lookup result and the nested-name-specifier nominates a class C:
27105 * if the name specified after the nested-name-specifier, when
27106 looked up in C, is the injected-class-name of C (Clause 9), or
27107 * if the name specified after the nested-name-specifier is the
27108 same as the identifier or the simple-template-id's template-
27109 name in the last component of the nested-name-specifier,
27110 the name is instead considered to name the constructor of
27111 class C. [ Note: for example, the constructor is not an
27112 acceptable lookup result in an elaborated-type-specifier so
27113 the constructor would not be used in place of the
27114 injected-class-name. --end note ] Such a constructor name
27115 shall be used only in the declarator-id of a declaration that
27116 names a constructor or in a using-declaration. */
27117 if (tag_type
== none_type
27118 && DECL_SELF_REFERENCE_P (decl
)
27119 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
27120 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
27121 prefer_type_arg (tag_type
),
27122 /*complain=*/true);
27124 /* If we have a single function from a using decl, pull it out. */
27125 if (TREE_CODE (decl
) == OVERLOAD
27126 && !really_overloaded_fn (decl
))
27127 decl
= OVL_FUNCTION (decl
);
27130 pop_scope (pushed_scope
);
27133 /* If the scope is a dependent type and either we deferred lookup or
27134 we did lookup but didn't find the name, rememeber the name. */
27135 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
27136 && dependent_type_p (parser
->scope
))
27142 /* The resolution to Core Issue 180 says that `struct
27143 A::B' should be considered a type-name, even if `A'
27145 type
= make_typename_type (parser
->scope
, name
, tag_type
,
27146 /*complain=*/tf_error
);
27147 if (type
!= error_mark_node
)
27148 decl
= TYPE_NAME (type
);
27150 else if (is_template
27151 && (cp_parser_next_token_ends_template_argument_p (parser
)
27152 || cp_lexer_next_token_is (parser
->lexer
,
27154 decl
= make_unbound_class_template (parser
->scope
,
27156 /*complain=*/tf_error
);
27158 decl
= build_qualified_name (/*type=*/NULL_TREE
,
27159 parser
->scope
, name
,
27162 parser
->qualifying_scope
= parser
->scope
;
27163 parser
->object_scope
= NULL_TREE
;
27165 else if (object_type
)
27167 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27168 OBJECT_TYPE is not a class. */
27169 if (CLASS_TYPE_P (object_type
))
27170 /* If the OBJECT_TYPE is a template specialization, it may
27171 be instantiated during name lookup. In that case, errors
27172 may be issued. Even if we rollback the current tentative
27173 parse, those errors are valid. */
27174 decl
= lookup_member (object_type
,
27177 prefer_type_arg (tag_type
),
27178 tf_warning_or_error
);
27183 /* Look it up in the enclosing context. DR 141: When looking for a
27184 template-name after -> or ., only consider class templates. */
27185 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
27187 /*block_p=*/true, is_namespace
, 0);
27188 if (object_type
== unknown_type_node
)
27189 /* The object is type-dependent, so we can't look anything up; we used
27190 this to get the DR 141 behavior. */
27191 object_type
= NULL_TREE
;
27192 parser
->object_scope
= object_type
;
27193 parser
->qualifying_scope
= NULL_TREE
;
27197 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
27199 /*block_p=*/true, is_namespace
, 0);
27200 parser
->qualifying_scope
= NULL_TREE
;
27201 parser
->object_scope
= NULL_TREE
;
27204 /* If the lookup failed, let our caller know. */
27205 if (!decl
|| decl
== error_mark_node
)
27206 return error_mark_node
;
27208 /* Pull out the template from an injected-class-name (or multiple). */
27210 decl
= maybe_get_template_decl_from_type_decl (decl
);
27212 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27213 if (TREE_CODE (decl
) == TREE_LIST
)
27215 if (ambiguous_decls
)
27216 *ambiguous_decls
= decl
;
27217 /* The error message we have to print is too complicated for
27218 cp_parser_error, so we incorporate its actions directly. */
27219 if (!cp_parser_simulate_error (parser
))
27221 error_at (name_location
, "reference to %qD is ambiguous",
27223 print_candidates (decl
);
27225 return error_mark_node
;
27228 gcc_assert (DECL_P (decl
)
27229 || TREE_CODE (decl
) == OVERLOAD
27230 || TREE_CODE (decl
) == SCOPE_REF
27231 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
27232 || BASELINK_P (decl
));
27234 /* If we have resolved the name of a member declaration, check to
27235 see if the declaration is accessible. When the name resolves to
27236 set of overloaded functions, accessibility is checked when
27237 overload resolution is done.
27239 During an explicit instantiation, access is not checked at all,
27240 as per [temp.explicit]. */
27242 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
27244 maybe_record_typedef_use (decl
);
27246 return cp_expr (decl
, name_location
);
27249 /* Like cp_parser_lookup_name, but for use in the typical case where
27250 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27251 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27254 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
27256 return cp_parser_lookup_name (parser
, name
,
27258 /*is_template=*/false,
27259 /*is_namespace=*/false,
27260 /*check_dependency=*/true,
27261 /*ambiguous_decls=*/NULL
,
27265 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27266 the current context, return the TYPE_DECL. If TAG_NAME_P is
27267 true, the DECL indicates the class being defined in a class-head,
27268 or declared in an elaborated-type-specifier.
27270 Otherwise, return DECL. */
27273 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
27275 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27276 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27279 template <typename T> struct B;
27282 template <typename T> struct A::B {};
27284 Similarly, in an elaborated-type-specifier:
27286 namespace N { struct X{}; }
27289 template <typename T> friend struct N::X;
27292 However, if the DECL refers to a class type, and we are in
27293 the scope of the class, then the name lookup automatically
27294 finds the TYPE_DECL created by build_self_reference rather
27295 than a TEMPLATE_DECL. For example, in:
27297 template <class T> struct S {
27301 there is no need to handle such case. */
27303 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
27304 return DECL_TEMPLATE_RESULT (decl
);
27309 /* If too many, or too few, template-parameter lists apply to the
27310 declarator, issue an error message. Returns TRUE if all went well,
27311 and FALSE otherwise. */
27314 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
27315 cp_declarator
*declarator
,
27316 location_t declarator_location
)
27318 switch (declarator
->kind
)
27322 unsigned num_templates
= 0;
27323 tree scope
= declarator
->u
.id
.qualifying_scope
;
27324 bool template_id_p
= false;
27327 num_templates
= num_template_headers_for_class (scope
);
27328 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
27329 == TEMPLATE_ID_EXPR
)
27331 /* If the DECLARATOR has the form `X<y>' then it uses one
27332 additional level of template parameters. */
27334 template_id_p
= true;
27337 return cp_parser_check_template_parameters
27338 (parser
, num_templates
, template_id_p
, declarator_location
,
27345 case cdk_reference
:
27347 return (cp_parser_check_declarator_template_parameters
27348 (parser
, declarator
->declarator
, declarator_location
));
27355 gcc_unreachable ();
27360 /* NUM_TEMPLATES were used in the current declaration. If that is
27361 invalid, return FALSE and issue an error messages. Otherwise,
27362 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27363 declarator and we can print more accurate diagnostics. */
27366 cp_parser_check_template_parameters (cp_parser
* parser
,
27367 unsigned num_templates
,
27368 bool template_id_p
,
27369 location_t location
,
27370 cp_declarator
*declarator
)
27372 /* If there are the same number of template classes and parameter
27373 lists, that's OK. */
27374 if (parser
->num_template_parameter_lists
== num_templates
)
27376 /* If there are more, but only one more, and the name ends in an identifier,
27377 then we are declaring a primary template. That's OK too. */
27379 && parser
->num_template_parameter_lists
== num_templates
+ 1)
27381 /* If there are more template classes than parameter lists, we have
27384 template <class T> void S<T>::R<T>::f (); */
27385 if (parser
->num_template_parameter_lists
< num_templates
)
27387 if (declarator
&& !current_function_decl
)
27388 error_at (location
, "specializing member %<%T::%E%> "
27389 "requires %<template<>%> syntax",
27390 declarator
->u
.id
.qualifying_scope
,
27391 declarator
->u
.id
.unqualified_name
);
27392 else if (declarator
)
27393 error_at (location
, "invalid declaration of %<%T::%E%>",
27394 declarator
->u
.id
.qualifying_scope
,
27395 declarator
->u
.id
.unqualified_name
);
27397 error_at (location
, "too few template-parameter-lists");
27400 /* Otherwise, there are too many template parameter lists. We have
27403 template <class T> template <class U> void S::f(); */
27404 error_at (location
, "too many template-parameter-lists");
27408 /* Parse an optional `::' token indicating that the following name is
27409 from the global namespace. If so, PARSER->SCOPE is set to the
27410 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27411 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27412 Returns the new value of PARSER->SCOPE, if the `::' token is
27413 present, and NULL_TREE otherwise. */
27416 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
27420 /* Peek at the next token. */
27421 token
= cp_lexer_peek_token (parser
->lexer
);
27422 /* If we're looking at a `::' token then we're starting from the
27423 global namespace, not our current location. */
27424 if (token
->type
== CPP_SCOPE
)
27426 /* Consume the `::' token. */
27427 cp_lexer_consume_token (parser
->lexer
);
27428 /* Set the SCOPE so that we know where to start the lookup. */
27429 parser
->scope
= global_namespace
;
27430 parser
->qualifying_scope
= global_namespace
;
27431 parser
->object_scope
= NULL_TREE
;
27433 return parser
->scope
;
27435 else if (!current_scope_valid_p
)
27437 parser
->scope
= NULL_TREE
;
27438 parser
->qualifying_scope
= NULL_TREE
;
27439 parser
->object_scope
= NULL_TREE
;
27445 /* Returns TRUE if the upcoming token sequence is the start of a
27446 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27447 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27448 is used to control type-specifier parsing. */
27451 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
27454 bool constructor_p
;
27455 bool outside_class_specifier_p
;
27456 tree nested_name_specifier
;
27457 cp_token
*next_token
;
27459 /* The common case is that this is not a constructor declarator, so
27460 try to avoid doing lots of work if at all possible. It's not
27461 valid declare a constructor at function scope. */
27462 if (parser
->in_function_body
)
27464 /* And only certain tokens can begin a constructor declarator. */
27465 next_token
= cp_lexer_peek_token (parser
->lexer
);
27466 if (next_token
->type
!= CPP_NAME
27467 && next_token
->type
!= CPP_SCOPE
27468 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27469 && next_token
->type
!= CPP_TEMPLATE_ID
)
27472 /* Parse tentatively; we are going to roll back all of the tokens
27474 cp_parser_parse_tentatively (parser
);
27475 /* Assume that we are looking at a constructor declarator. */
27476 constructor_p
= true;
27478 /* Look for the optional `::' operator. */
27479 cp_parser_global_scope_opt (parser
,
27480 /*current_scope_valid_p=*/false);
27481 /* Look for the nested-name-specifier. */
27482 nested_name_specifier
27483 = (cp_parser_nested_name_specifier_opt (parser
,
27484 /*typename_keyword_p=*/false,
27485 /*check_dependency_p=*/false,
27487 /*is_declaration=*/false));
27489 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27490 if (nested_name_specifier
27491 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
27493 tree s
= resolve_typename_type (nested_name_specifier
,
27494 /*only_current_p=*/false);
27495 if (TREE_CODE (s
) != TYPENAME_TYPE
)
27496 nested_name_specifier
= s
;
27499 outside_class_specifier_p
= (!at_class_scope_p ()
27500 || !TYPE_BEING_DEFINED (current_class_type
)
27503 /* Outside of a class-specifier, there must be a
27504 nested-name-specifier. Except in C++17 mode, where we
27505 might be declaring a guiding declaration. */
27506 if (!nested_name_specifier
&& outside_class_specifier_p
27507 && cxx_dialect
< cxx17
)
27508 constructor_p
= false;
27509 else if (nested_name_specifier
== error_mark_node
)
27510 constructor_p
= false;
27512 /* If we have a class scope, this is easy; DR 147 says that S::S always
27513 names the constructor, and no other qualified name could. */
27514 if (constructor_p
&& nested_name_specifier
27515 && CLASS_TYPE_P (nested_name_specifier
))
27517 tree id
= cp_parser_unqualified_id (parser
,
27518 /*template_keyword_p=*/false,
27519 /*check_dependency_p=*/false,
27520 /*declarator_p=*/true,
27521 /*optional_p=*/false);
27522 if (is_overloaded_fn (id
))
27523 id
= DECL_NAME (get_first_fn (id
));
27524 if (!constructor_name_p (id
, nested_name_specifier
))
27525 constructor_p
= false;
27527 /* If we still think that this might be a constructor-declarator,
27528 look for a class-name. */
27529 else if (constructor_p
)
27533 template <typename T> struct S {
27537 we must recognize that the nested `S' names a class. */
27538 if (cxx_dialect
>= cxx17
)
27539 cp_parser_parse_tentatively (parser
);
27542 type_decl
= cp_parser_class_name (parser
,
27543 /*typename_keyword_p=*/false,
27544 /*template_keyword_p=*/false,
27546 /*check_dependency_p=*/false,
27547 /*class_head_p=*/false,
27548 /*is_declaration=*/false);
27550 if (cxx_dialect
>= cxx17
27551 && !cp_parser_parse_definitely (parser
))
27553 type_decl
= NULL_TREE
;
27554 tree tmpl
= cp_parser_template_name (parser
,
27555 /*template_keyword*/false,
27556 /*check_dependency_p*/false,
27557 /*is_declaration*/false,
27559 /*is_identifier*/NULL
);
27560 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27561 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27562 /* It's a deduction guide, return true. */;
27564 cp_parser_simulate_error (parser
);
27567 /* If there was no class-name, then this is not a constructor.
27568 Otherwise, if we are in a class-specifier and we aren't
27569 handling a friend declaration, check that its type matches
27570 current_class_type (c++/38313). Note: error_mark_node
27571 is left alone for error recovery purposes. */
27572 constructor_p
= (!cp_parser_error_occurred (parser
)
27573 && (outside_class_specifier_p
27574 || type_decl
== NULL_TREE
27575 || type_decl
== error_mark_node
27576 || same_type_p (current_class_type
,
27577 TREE_TYPE (type_decl
))));
27579 /* If we're still considering a constructor, we have to see a `(',
27580 to begin the parameter-declaration-clause, followed by either a
27581 `)', an `...', or a decl-specifier. We need to check for a
27582 type-specifier to avoid being fooled into thinking that:
27586 is a constructor. (It is actually a function named `f' that
27587 takes one parameter (of type `int') and returns a value of type
27590 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27591 constructor_p
= false;
27594 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27595 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27596 /* A parameter declaration begins with a decl-specifier,
27597 which is either the "attribute" keyword, a storage class
27598 specifier, or (usually) a type-specifier. */
27599 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27602 tree pushed_scope
= NULL_TREE
;
27603 unsigned saved_num_template_parameter_lists
;
27605 /* Names appearing in the type-specifier should be looked up
27606 in the scope of the class. */
27607 if (current_class_type
)
27609 else if (type_decl
)
27611 type
= TREE_TYPE (type_decl
);
27612 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27614 type
= resolve_typename_type (type
,
27615 /*only_current_p=*/false);
27616 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27618 cp_parser_abort_tentative_parse (parser
);
27622 pushed_scope
= push_scope (type
);
27625 /* Inside the constructor parameter list, surrounding
27626 template-parameter-lists do not apply. */
27627 saved_num_template_parameter_lists
27628 = parser
->num_template_parameter_lists
;
27629 parser
->num_template_parameter_lists
= 0;
27631 /* Look for the type-specifier. It's not optional, but its typename
27633 cp_parser_type_specifier (parser
,
27634 (flags
& ~CP_PARSER_FLAGS_OPTIONAL
),
27635 /*decl_specs=*/NULL
,
27636 /*is_declarator=*/true,
27637 /*declares_class_or_enum=*/NULL
,
27638 /*is_cv_qualifier=*/NULL
);
27640 parser
->num_template_parameter_lists
27641 = saved_num_template_parameter_lists
;
27643 /* Leave the scope of the class. */
27645 pop_scope (pushed_scope
);
27647 constructor_p
= !cp_parser_error_occurred (parser
);
27651 /* We did not really want to consume any tokens. */
27652 cp_parser_abort_tentative_parse (parser
);
27654 return constructor_p
;
27657 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27658 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27659 they must be performed once we are in the scope of the function.
27661 Returns the function defined. */
27664 cp_parser_function_definition_from_specifiers_and_declarator
27665 (cp_parser
* parser
,
27666 cp_decl_specifier_seq
*decl_specifiers
,
27668 const cp_declarator
*declarator
)
27673 /* Begin the function-definition. */
27674 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27676 /* The things we're about to see are not directly qualified by any
27677 template headers we've seen thus far. */
27678 reset_specialization ();
27680 /* If there were names looked up in the decl-specifier-seq that we
27681 did not check, check them now. We must wait until we are in the
27682 scope of the function to perform the checks, since the function
27683 might be a friend. */
27684 perform_deferred_access_checks (tf_warning_or_error
);
27688 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27689 parser
->omp_declare_simd
= NULL
;
27690 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27691 parser
->oacc_routine
= NULL
;
27696 /* Skip the entire function. */
27697 cp_parser_skip_to_end_of_block_or_statement (parser
);
27698 fn
= error_mark_node
;
27700 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27702 /* Seen already, skip it. An error message has already been output. */
27703 cp_parser_skip_to_end_of_block_or_statement (parser
);
27704 fn
= current_function_decl
;
27705 current_function_decl
= NULL_TREE
;
27706 /* If this is a function from a class, pop the nested class. */
27707 if (current_class_name
)
27708 pop_nested_class ();
27713 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27714 tv
= TV_PARSE_INLINE
;
27716 tv
= TV_PARSE_FUNC
;
27718 fn
= cp_parser_function_definition_after_declarator (parser
,
27719 /*inline_p=*/false);
27726 /* Parse the part of a function-definition that follows the
27727 declarator. INLINE_P is TRUE iff this function is an inline
27728 function defined within a class-specifier.
27730 Returns the function defined. */
27733 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27737 bool saved_in_unbraced_linkage_specification_p
;
27738 bool saved_in_function_body
;
27739 unsigned saved_num_template_parameter_lists
;
27741 bool fully_implicit_function_template_p
27742 = parser
->fully_implicit_function_template_p
;
27743 parser
->fully_implicit_function_template_p
= false;
27744 tree implicit_template_parms
27745 = parser
->implicit_template_parms
;
27746 parser
->implicit_template_parms
= 0;
27747 cp_binding_level
* implicit_template_scope
27748 = parser
->implicit_template_scope
;
27749 parser
->implicit_template_scope
= 0;
27751 saved_in_function_body
= parser
->in_function_body
;
27752 parser
->in_function_body
= true;
27753 /* If the next token is `return', then the code may be trying to
27754 make use of the "named return value" extension that G++ used to
27756 token
= cp_lexer_peek_token (parser
->lexer
);
27757 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27759 /* Consume the `return' keyword. */
27760 cp_lexer_consume_token (parser
->lexer
);
27761 /* Look for the identifier that indicates what value is to be
27763 cp_parser_identifier (parser
);
27764 /* Issue an error message. */
27765 error_at (token
->location
,
27766 "named return values are no longer supported");
27767 /* Skip tokens until we reach the start of the function body. */
27770 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27771 if (token
->type
== CPP_OPEN_BRACE
27772 || token
->type
== CPP_EOF
27773 || token
->type
== CPP_PRAGMA_EOL
)
27775 cp_lexer_consume_token (parser
->lexer
);
27778 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27779 anything declared inside `f'. */
27780 saved_in_unbraced_linkage_specification_p
27781 = parser
->in_unbraced_linkage_specification_p
;
27782 parser
->in_unbraced_linkage_specification_p
= false;
27783 /* Inside the function, surrounding template-parameter-lists do not
27785 saved_num_template_parameter_lists
27786 = parser
->num_template_parameter_lists
;
27787 parser
->num_template_parameter_lists
= 0;
27789 /* If the next token is `try', `__transaction_atomic', or
27790 `__transaction_relaxed`, then we are looking at either function-try-block
27791 or function-transaction-block. Note that all of these include the
27793 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27794 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27795 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27796 RID_TRANSACTION_RELAXED
))
27797 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27798 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27799 cp_parser_function_try_block (parser
);
27801 cp_parser_ctor_initializer_opt_and_function_body
27802 (parser
, /*in_function_try_block=*/false);
27804 /* Finish the function. */
27805 fn
= finish_function (inline_p
);
27806 /* Generate code for it, if necessary. */
27807 expand_or_defer_fn (fn
);
27808 /* Restore the saved values. */
27809 parser
->in_unbraced_linkage_specification_p
27810 = saved_in_unbraced_linkage_specification_p
;
27811 parser
->num_template_parameter_lists
27812 = saved_num_template_parameter_lists
;
27813 parser
->in_function_body
= saved_in_function_body
;
27815 parser
->fully_implicit_function_template_p
27816 = fully_implicit_function_template_p
;
27817 parser
->implicit_template_parms
27818 = implicit_template_parms
;
27819 parser
->implicit_template_scope
27820 = implicit_template_scope
;
27822 if (parser
->fully_implicit_function_template_p
)
27823 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27828 /* Parse a template-declaration body (following argument list). */
27831 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27832 tree parameter_list
,
27835 tree decl
= NULL_TREE
;
27836 bool friend_p
= false;
27838 /* We just processed one more parameter list. */
27839 ++parser
->num_template_parameter_lists
;
27841 /* Get the deferred access checks from the parameter list. These
27842 will be checked once we know what is being declared, as for a
27843 member template the checks must be performed in the scope of the
27844 class containing the member. */
27845 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27847 /* Tentatively parse for a new template parameter list, which can either be
27848 the template keyword or a template introduction. */
27849 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27851 else if (cxx_dialect
>= cxx11
27852 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27853 decl
= cp_parser_alias_declaration (parser
);
27856 /* There are no access checks when parsing a template, as we do not
27857 know if a specialization will be a friend. */
27858 push_deferring_access_checks (dk_no_check
);
27859 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27860 decl
= cp_parser_single_declaration (parser
,
27863 /*explicit_specialization_p=*/false,
27865 pop_deferring_access_checks ();
27867 /* If this is a member template declaration, let the front
27869 if (member_p
&& !friend_p
&& decl
)
27871 if (TREE_CODE (decl
) == TYPE_DECL
)
27872 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27874 decl
= finish_member_template_decl (decl
);
27876 else if (friend_p
&& decl
27877 && DECL_DECLARES_TYPE_P (decl
))
27878 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27879 /*complain=*/true);
27881 /* We are done with the current parameter list. */
27882 --parser
->num_template_parameter_lists
;
27884 pop_deferring_access_checks ();
27887 finish_template_decl (parameter_list
);
27889 /* Check the template arguments for a literal operator template. */
27891 && DECL_DECLARES_FUNCTION_P (decl
)
27892 && UDLIT_OPER_P (DECL_NAME (decl
)))
27895 if (parameter_list
== NULL_TREE
)
27899 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27900 if (num_parms
== 1)
27902 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27903 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27904 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27905 /* OK, C++20 string literal operator template. We don't need
27906 to warn in lower dialects here because we will have already
27907 warned about the template parameter. */;
27908 else if (TREE_TYPE (parm
) != char_type_node
27909 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27912 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27914 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27915 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27916 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27917 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27918 if (parm
== error_mark_node
27919 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27920 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27923 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27924 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27925 "ISO C++ did not adopt string literal operator templa"
27926 "tes taking an argument pack of characters");
27933 if (cxx_dialect
> cxx17
)
27934 error ("literal operator template %qD has invalid parameter list;"
27935 " Expected non-type template parameter pack <char...> "
27936 "or single non-type parameter of class type",
27939 error ("literal operator template %qD has invalid parameter list."
27940 " Expected non-type template parameter pack <char...>",
27945 /* Register member declarations. */
27946 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27947 finish_member_declaration (decl
);
27948 /* If DECL is a function template, we must return to parse it later.
27949 (Even though there is no definition, there might be default
27950 arguments that need handling.) */
27951 if (member_p
&& decl
27952 && DECL_DECLARES_FUNCTION_P (decl
))
27953 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27956 /* Parse a template introduction header for a template-declaration. Returns
27957 false if tentative parse fails. */
27960 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27962 cp_parser_parse_tentatively (parser
);
27964 tree saved_scope
= parser
->scope
;
27965 tree saved_object_scope
= parser
->object_scope
;
27966 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27968 /* Look for the optional `::' operator. */
27969 cp_parser_global_scope_opt (parser
,
27970 /*current_scope_valid_p=*/false);
27971 /* Look for the nested-name-specifier. */
27972 cp_parser_nested_name_specifier_opt (parser
,
27973 /*typename_keyword_p=*/false,
27974 /*check_dependency_p=*/true,
27976 /*is_declaration=*/false);
27978 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27979 tree concept_name
= cp_parser_identifier (parser
);
27981 /* Look up the concept for which we will be matching
27982 template parameters. */
27983 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27985 parser
->scope
= saved_scope
;
27986 parser
->object_scope
= saved_object_scope
;
27987 parser
->qualifying_scope
= saved_qualifying_scope
;
27989 if (concept_name
== error_mark_node
)
27990 cp_parser_simulate_error (parser
);
27992 /* Look for opening brace for introduction. */
27993 matching_braces braces
;
27994 braces
.require_open (parser
);
27996 if (!cp_parser_parse_definitely (parser
))
27999 push_deferring_access_checks (dk_deferred
);
28001 /* Build vector of placeholder parameters and grab
28002 matching identifiers. */
28003 tree introduction_list
= cp_parser_introduction_list (parser
);
28005 /* Look for closing brace for introduction. */
28006 if (!braces
.require_close (parser
))
28009 /* The introduction-list shall not be empty. */
28010 int nargs
= TREE_VEC_LENGTH (introduction_list
);
28013 /* In cp_parser_introduction_list we have already issued an error. */
28017 if (tmpl_decl
== error_mark_node
)
28019 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
28024 /* Build and associate the constraint. */
28025 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
28026 if (parms
&& parms
!= error_mark_node
)
28028 cp_parser_template_declaration_after_parameters (parser
, parms
,
28033 error_at (token
->location
, "no matching concept for template-introduction");
28037 /* Parse a normal template-declaration following the template keyword. */
28040 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
28042 tree parameter_list
;
28043 bool need_lang_pop
;
28044 location_t location
= input_location
;
28046 /* Look for the `<' token. */
28047 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
28049 if (at_class_scope_p () && current_function_decl
)
28051 /* 14.5.2.2 [temp.mem]
28053 A local class shall not have member templates. */
28054 error_at (location
,
28055 "invalid declaration of member template in local class");
28056 cp_parser_skip_to_end_of_block_or_statement (parser
);
28061 A template ... shall not have C linkage. */
28062 if (current_lang_name
== lang_name_c
)
28064 error_at (location
, "template with C linkage");
28065 maybe_show_extern_c_location ();
28066 /* Give it C++ linkage to avoid confusing other parts of the
28068 push_lang_context (lang_name_cplusplus
);
28069 need_lang_pop
= true;
28072 need_lang_pop
= false;
28074 /* We cannot perform access checks on the template parameter
28075 declarations until we know what is being declared, just as we
28076 cannot check the decl-specifier list. */
28077 push_deferring_access_checks (dk_deferred
);
28079 /* If the next token is `>', then we have an invalid
28080 specialization. Rather than complain about an invalid template
28081 parameter, issue an error message here. */
28082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
28084 cp_parser_error (parser
, "invalid explicit specialization");
28085 begin_specialization ();
28086 parameter_list
= NULL_TREE
;
28090 /* Parse the template parameters. */
28091 parameter_list
= cp_parser_template_parameter_list (parser
);
28094 /* Look for the `>'. */
28095 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28097 /* Manage template requirements */
28100 tree reqs
= get_shorthand_constraints (current_template_parms
);
28101 if (tree r
= cp_parser_requires_clause_opt (parser
))
28102 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
28103 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
28106 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
28109 /* For the erroneous case of a template with C linkage, we pushed an
28110 implicit C++ linkage scope; exit that scope now. */
28112 pop_lang_context ();
28115 /* Parse a template-declaration, assuming that the `export' (and
28116 `extern') keywords, if present, has already been scanned. MEMBER_P
28117 is as for cp_parser_template_declaration. */
28120 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
28122 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28124 cp_lexer_consume_token (parser
->lexer
);
28125 cp_parser_explicit_template_declaration (parser
, member_p
);
28128 else if (flag_concepts
)
28129 return cp_parser_template_introduction (parser
, member_p
);
28134 /* Perform the deferred access checks from a template-parameter-list.
28135 CHECKS is a TREE_LIST of access checks, as returned by
28136 get_deferred_access_checks. */
28139 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
28141 ++processing_template_parmlist
;
28142 perform_access_checks (checks
, tf_warning_or_error
);
28143 --processing_template_parmlist
;
28146 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28147 `function-definition' sequence that follows a template header.
28148 If MEMBER_P is true, this declaration appears in a class scope.
28150 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28151 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28154 cp_parser_single_declaration (cp_parser
* parser
,
28155 vec
<deferred_access_check
, va_gc
> *checks
,
28157 bool explicit_specialization_p
,
28160 int declares_class_or_enum
;
28161 tree decl
= NULL_TREE
;
28162 cp_decl_specifier_seq decl_specifiers
;
28163 bool function_definition_p
= false;
28164 cp_token
*decl_spec_token_start
;
28166 /* This function is only used when processing a template
28168 gcc_assert (innermost_scope_kind () == sk_template_parms
28169 || innermost_scope_kind () == sk_template_spec
);
28171 /* Defer access checks until we know what is being declared. */
28172 push_deferring_access_checks (dk_deferred
);
28174 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28176 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
28177 cp_parser_decl_specifier_seq (parser
,
28178 (CP_PARSER_FLAGS_OPTIONAL
28179 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
28181 &declares_class_or_enum
);
28183 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
28185 /* There are no template typedefs. */
28186 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
28188 error_at (decl_spec_token_start
->location
,
28189 "template declaration of %<typedef%>");
28190 decl
= error_mark_node
;
28193 /* Gather up the access checks that occurred the
28194 decl-specifier-seq. */
28195 stop_deferring_access_checks ();
28197 /* Check for the declaration of a template class. */
28198 if (declares_class_or_enum
)
28200 if (cp_parser_declares_only_class_p (parser
)
28201 || (declares_class_or_enum
& 2))
28203 // If this is a declaration, but not a definition, associate
28204 // any constraints with the type declaration. Constraints
28205 // are associated with definitions in cp_parser_class_specifier.
28206 if (declares_class_or_enum
== 1)
28207 associate_classtype_constraints (decl_specifiers
.type
);
28209 decl
= shadow_tag (&decl_specifiers
);
28214 friend template <typename T> struct A<T>::B;
28217 A<T>::B will be represented by a TYPENAME_TYPE, and
28218 therefore not recognized by shadow_tag. */
28219 if (friend_p
&& *friend_p
28221 && decl_specifiers
.type
28222 && TYPE_P (decl_specifiers
.type
))
28223 decl
= decl_specifiers
.type
;
28225 if (decl
&& decl
!= error_mark_node
)
28226 decl
= TYPE_NAME (decl
);
28228 decl
= error_mark_node
;
28230 /* Perform access checks for template parameters. */
28231 cp_parser_perform_template_parameter_access_checks (checks
);
28233 /* Give a helpful diagnostic for
28234 template <class T> struct A { } a;
28235 if we aren't already recovering from an error. */
28236 if (!cp_parser_declares_only_class_p (parser
)
28239 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28240 "a class template declaration must not declare "
28242 cp_parser_skip_to_end_of_block_or_statement (parser
);
28248 /* Complain about missing 'typename' or other invalid type names. */
28249 if (!decl_specifiers
.any_type_specifiers_p
28250 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
28252 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28253 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28254 the rest of this declaration. */
28255 decl
= error_mark_node
;
28259 /* If it's not a template class, try for a template function. If
28260 the next token is a `;', then this declaration does not declare
28261 anything. But, if there were errors in the decl-specifiers, then
28262 the error might well have come from an attempted class-specifier.
28263 In that case, there's no need to warn about a missing declarator. */
28265 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
28266 || decl_specifiers
.type
!= error_mark_node
))
28268 decl
= cp_parser_init_declarator (parser
,
28269 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
28272 /*function_definition_allowed_p=*/true,
28274 declares_class_or_enum
,
28275 &function_definition_p
,
28278 /* 7.1.1-1 [dcl.stc]
28280 A storage-class-specifier shall not be specified in an explicit
28281 specialization... */
28283 && explicit_specialization_p
28284 && decl_specifiers
.storage_class
!= sc_none
)
28286 error_at (decl_spec_token_start
->location
,
28287 "explicit template specialization cannot have a storage class");
28288 decl
= error_mark_node
;
28291 if (decl
&& VAR_P (decl
))
28292 check_template_variable (decl
);
28295 /* Look for a trailing `;' after the declaration. */
28296 if (!function_definition_p
28297 && (decl
== error_mark_node
28298 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
28299 cp_parser_skip_to_end_of_block_or_statement (parser
);
28302 pop_deferring_access_checks ();
28304 /* Clear any current qualification; whatever comes next is the start
28305 of something new. */
28306 parser
->scope
= NULL_TREE
;
28307 parser
->qualifying_scope
= NULL_TREE
;
28308 parser
->object_scope
= NULL_TREE
;
28313 /* Parse a cast-expression that is not the operand of a unary "&". */
28316 cp_parser_simple_cast_expression (cp_parser
*parser
)
28318 return cp_parser_cast_expression (parser
, /*address_p=*/false,
28319 /*cast_p=*/false, /*decltype*/false, NULL
);
28322 /* Parse a functional cast to TYPE. Returns an expression
28323 representing the cast. */
28326 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
28328 vec
<tree
, va_gc
> *vec
;
28329 tree expression_list
;
28333 location_t start_loc
= input_location
;
28336 type
= error_mark_node
;
28338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28340 cp_lexer_set_source_position (parser
->lexer
);
28341 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28342 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
28343 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
28344 if (TREE_CODE (type
) == TYPE_DECL
)
28345 type
= TREE_TYPE (type
);
28347 cast
= finish_compound_literal (type
, expression_list
,
28348 tf_warning_or_error
, fcl_functional
);
28349 /* Create a location of the form:
28352 with caret == start at the start of the type name,
28353 finishing at the closing brace. */
28354 location_t finish_loc
28355 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28356 location_t combined_loc
= make_location (start_loc
, start_loc
,
28358 cast
.set_location (combined_loc
);
28363 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28365 /*allow_expansion_p=*/true,
28366 /*non_constant_p=*/NULL
);
28368 expression_list
= error_mark_node
;
28371 expression_list
= build_tree_list_vec (vec
);
28372 release_tree_vector (vec
);
28375 cast
= build_functional_cast (type
, expression_list
,
28376 tf_warning_or_error
);
28377 /* [expr.const]/1: In an integral constant expression "only type
28378 conversions to integral or enumeration type can be used". */
28379 if (TREE_CODE (type
) == TYPE_DECL
)
28380 type
= TREE_TYPE (type
);
28381 if (cast
!= error_mark_node
28382 && !cast_valid_in_integral_constant_expression_p (type
)
28383 && cp_parser_non_integral_constant_expression (parser
,
28385 return error_mark_node
;
28387 /* Create a location of the form:
28390 with caret == start at the start of the type name,
28391 finishing at the closing paren. */
28392 location_t finish_loc
28393 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28394 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
28395 cast
.set_location (combined_loc
);
28399 /* Save the tokens that make up the body of a member function defined
28400 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28401 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28402 specifiers applied to the declaration. Returns the FUNCTION_DECL
28403 for the member function. */
28406 cp_parser_save_member_function_body (cp_parser
* parser
,
28407 cp_decl_specifier_seq
*decl_specifiers
,
28408 cp_declarator
*declarator
,
28414 bool function_try_block
= false;
28416 /* Create the FUNCTION_DECL. */
28417 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
28418 cp_finalize_omp_declare_simd (parser
, fn
);
28419 cp_finalize_oacc_routine (parser
, fn
, true);
28420 /* If something went badly wrong, bail out now. */
28421 if (fn
== error_mark_node
)
28423 /* If there's a function-body, skip it. */
28424 if (cp_parser_token_starts_function_definition_p
28425 (cp_lexer_peek_token (parser
->lexer
)))
28426 cp_parser_skip_to_end_of_block_or_statement (parser
);
28427 return error_mark_node
;
28430 /* Remember it, if there default args to post process. */
28431 cp_parser_save_default_args (parser
, fn
);
28433 /* Save away the tokens that make up the body of the
28435 first
= parser
->lexer
->next_token
;
28437 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
28438 cp_lexer_consume_token (parser
->lexer
);
28439 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
28440 RID_TRANSACTION_ATOMIC
))
28442 cp_lexer_consume_token (parser
->lexer
);
28443 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
28445 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
28446 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
28447 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
28448 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
28449 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
28451 cp_lexer_consume_token (parser
->lexer
);
28452 cp_lexer_consume_token (parser
->lexer
);
28453 cp_lexer_consume_token (parser
->lexer
);
28454 cp_lexer_consume_token (parser
->lexer
);
28455 cp_lexer_consume_token (parser
->lexer
);
28458 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28459 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
28461 cp_lexer_consume_token (parser
->lexer
);
28462 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28467 /* Handle function try blocks. */
28468 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28470 cp_lexer_consume_token (parser
->lexer
);
28471 function_try_block
= true;
28473 /* We can have braced-init-list mem-initializers before the fn body. */
28474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28476 cp_lexer_consume_token (parser
->lexer
);
28477 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28479 /* cache_group will stop after an un-nested { } pair, too. */
28480 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28483 /* variadic mem-inits have ... after the ')'. */
28484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28485 cp_lexer_consume_token (parser
->lexer
);
28488 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28489 /* Handle function try blocks. */
28490 if (function_try_block
)
28491 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28492 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28493 last
= parser
->lexer
->next_token
;
28495 /* Save away the inline definition; we will process it when the
28496 class is complete. */
28497 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28498 DECL_PENDING_INLINE_P (fn
) = 1;
28500 /* We need to know that this was defined in the class, so that
28501 friend templates are handled correctly. */
28502 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28504 /* Add FN to the queue of functions to be parsed later. */
28505 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28510 /* Save the tokens that make up the in-class initializer for a non-static
28511 data member. Returns a DEFAULT_ARG. */
28514 cp_parser_save_nsdmi (cp_parser
* parser
)
28516 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28519 /* Parse a template-argument-list, as well as the trailing ">" (but
28520 not the opening "<"). See cp_parser_template_argument_list for the
28524 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28528 tree saved_qualifying_scope
;
28529 tree saved_object_scope
;
28530 bool saved_greater_than_is_operator_p
;
28534 When parsing a template-id, the first non-nested `>' is taken as
28535 the end of the template-argument-list rather than a greater-than
28537 saved_greater_than_is_operator_p
28538 = parser
->greater_than_is_operator_p
;
28539 parser
->greater_than_is_operator_p
= false;
28540 /* Parsing the argument list may modify SCOPE, so we save it
28542 saved_scope
= parser
->scope
;
28543 saved_qualifying_scope
= parser
->qualifying_scope
;
28544 saved_object_scope
= parser
->object_scope
;
28545 /* We need to evaluate the template arguments, even though this
28546 template-id may be nested within a "sizeof". */
28548 /* Parse the template-argument-list itself. */
28549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28550 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28551 arguments
= NULL_TREE
;
28553 arguments
= cp_parser_template_argument_list (parser
);
28554 /* Look for the `>' that ends the template-argument-list. If we find
28555 a '>>' instead, it's probably just a typo. */
28556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28558 if (cxx_dialect
!= cxx98
)
28560 /* In C++0x, a `>>' in a template argument list or cast
28561 expression is considered to be two separate `>'
28562 tokens. So, change the current token to a `>', but don't
28563 consume it: it will be consumed later when the outer
28564 template argument list (or cast expression) is parsed.
28565 Note that this replacement of `>' for `>>' is necessary
28566 even if we are parsing tentatively: in the tentative
28567 case, after calling
28568 cp_parser_enclosed_template_argument_list we will always
28569 throw away all of the template arguments and the first
28570 closing `>', either because the template argument list
28571 was erroneous or because we are replacing those tokens
28572 with a CPP_TEMPLATE_ID token. The second `>' (which will
28573 not have been thrown away) is needed either to close an
28574 outer template argument list or to complete a new-style
28576 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28577 token
->type
= CPP_GREATER
;
28579 else if (!saved_greater_than_is_operator_p
)
28581 /* If we're in a nested template argument list, the '>>' has
28582 to be a typo for '> >'. We emit the error message, but we
28583 continue parsing and we push a '>' as next token, so that
28584 the argument list will be parsed correctly. Note that the
28585 global source location is still on the token before the
28586 '>>', so we need to say explicitly where we want it. */
28587 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28588 gcc_rich_location
richloc (token
->location
);
28589 richloc
.add_fixit_replace ("> >");
28590 error_at (&richloc
, "%<>>%> should be %<> >%> "
28591 "within a nested template argument list");
28593 token
->type
= CPP_GREATER
;
28597 /* If this is not a nested template argument list, the '>>'
28598 is a typo for '>'. Emit an error message and continue.
28599 Same deal about the token location, but here we can get it
28600 right by consuming the '>>' before issuing the diagnostic. */
28601 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28602 error_at (token
->location
,
28603 "spurious %<>>%>, use %<>%> to terminate "
28604 "a template argument list");
28608 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28609 /* The `>' token might be a greater-than operator again now. */
28610 parser
->greater_than_is_operator_p
28611 = saved_greater_than_is_operator_p
;
28612 /* Restore the SAVED_SCOPE. */
28613 parser
->scope
= saved_scope
;
28614 parser
->qualifying_scope
= saved_qualifying_scope
;
28615 parser
->object_scope
= saved_object_scope
;
28620 /* MEMBER_FUNCTION is a member function, or a friend. If default
28621 arguments, or the body of the function have not yet been parsed,
28625 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28627 timevar_push (TV_PARSE_INMETH
);
28628 /* If this member is a template, get the underlying
28630 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28631 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28633 /* There should not be any class definitions in progress at this
28634 point; the bodies of members are only parsed outside of all class
28636 gcc_assert (parser
->num_classes_being_defined
== 0);
28637 /* While we're parsing the member functions we might encounter more
28638 classes. We want to handle them right away, but we don't want
28639 them getting mixed up with functions that are currently in the
28641 push_unparsed_function_queues (parser
);
28643 /* Make sure that any template parameters are in scope. */
28644 maybe_begin_member_template_processing (member_function
);
28646 /* If the body of the function has not yet been parsed, parse it
28648 if (DECL_PENDING_INLINE_P (member_function
))
28650 tree function_scope
;
28651 cp_token_cache
*tokens
;
28653 /* The function is no longer pending; we are processing it. */
28654 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28655 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28656 DECL_PENDING_INLINE_P (member_function
) = 0;
28658 /* If this is a local class, enter the scope of the containing
28660 function_scope
= current_function_decl
;
28661 if (function_scope
)
28662 push_function_context ();
28664 /* Push the body of the function onto the lexer stack. */
28665 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28667 /* Let the front end know that we going to be defining this
28669 start_preparsed_function (member_function
, NULL_TREE
,
28670 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28672 /* Don't do access checking if it is a templated function. */
28673 if (processing_template_decl
)
28674 push_deferring_access_checks (dk_no_check
);
28676 /* #pragma omp declare reduction needs special parsing. */
28677 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28679 parser
->lexer
->in_pragma
= true;
28680 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28681 finish_function (/*inline_p=*/true);
28682 cp_check_omp_declare_reduction (member_function
);
28685 /* Now, parse the body of the function. */
28686 cp_parser_function_definition_after_declarator (parser
,
28687 /*inline_p=*/true);
28689 if (processing_template_decl
)
28690 pop_deferring_access_checks ();
28692 /* Leave the scope of the containing function. */
28693 if (function_scope
)
28694 pop_function_context ();
28695 cp_parser_pop_lexer (parser
);
28698 /* Remove any template parameters from the symbol table. */
28699 maybe_end_member_template_processing ();
28701 /* Restore the queue. */
28702 pop_unparsed_function_queues (parser
);
28703 timevar_pop (TV_PARSE_INMETH
);
28706 /* If DECL contains any default args, remember it on the unparsed
28707 functions queue. */
28710 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28714 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28716 probe
= TREE_CHAIN (probe
))
28717 if (TREE_PURPOSE (probe
))
28719 cp_default_arg_entry entry
= {current_class_type
, decl
};
28720 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28725 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28726 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28727 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28728 from the parameter-type-list. */
28731 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28732 tree default_arg
, tree parmtype
)
28734 cp_token_cache
*tokens
;
28738 if (default_arg
== error_mark_node
)
28739 return error_mark_node
;
28741 /* Push the saved tokens for the default argument onto the parser's
28743 tokens
= DEFARG_TOKENS (default_arg
);
28744 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28746 start_lambda_scope (decl
);
28748 /* Parse the default argument. */
28749 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28750 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28751 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28753 finish_lambda_scope ();
28755 if (parsed_arg
== error_mark_node
)
28756 cp_parser_skip_to_end_of_statement (parser
);
28758 if (!processing_template_decl
)
28760 /* In a non-template class, check conversions now. In a template,
28761 we'll wait and instantiate these as needed. */
28762 if (TREE_CODE (decl
) == PARM_DECL
)
28763 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28764 tf_warning_or_error
);
28765 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28766 parsed_arg
= error_mark_node
;
28768 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28771 /* If the token stream has not been completely used up, then
28772 there was extra junk after the end of the default
28774 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28776 if (TREE_CODE (decl
) == PARM_DECL
)
28777 cp_parser_error (parser
, "expected %<,%>");
28779 cp_parser_error (parser
, "expected %<;%>");
28782 /* Revert to the main lexer. */
28783 cp_parser_pop_lexer (parser
);
28788 /* FIELD is a non-static data member with an initializer which we saved for
28789 later; parse it now. */
28792 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28796 maybe_begin_member_template_processing (field
);
28798 push_unparsed_function_queues (parser
);
28799 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28800 DECL_INITIAL (field
),
28802 pop_unparsed_function_queues (parser
);
28804 maybe_end_member_template_processing ();
28806 DECL_INITIAL (field
) = def
;
28809 /* FN is a FUNCTION_DECL which may contains a parameter with an
28810 unparsed DEFAULT_ARG. Parse the default args now. This function
28811 assumes that the current scope is the scope in which the default
28812 argument should be processed. */
28815 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28817 unsigned char saved_local_variables_forbidden_p
;
28818 tree parm
, parmdecl
;
28820 /* While we're parsing the default args, we might (due to the
28821 statement expression extension) encounter more classes. We want
28822 to handle them right away, but we don't want them getting mixed
28823 up with default args that are currently in the queue. */
28824 push_unparsed_function_queues (parser
);
28826 /* Local variable names (and the `this' keyword) may not appear
28827 in a default argument. */
28828 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28829 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
28831 push_defarg_context (fn
);
28833 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28834 parmdecl
= DECL_ARGUMENTS (fn
);
28835 parm
&& parm
!= void_list_node
;
28836 parm
= TREE_CHAIN (parm
),
28837 parmdecl
= DECL_CHAIN (parmdecl
))
28839 tree default_arg
= TREE_PURPOSE (parm
);
28841 vec
<tree
, va_gc
> *insts
;
28848 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28849 /* This can happen for a friend declaration for a function
28850 already declared with default arguments. */
28854 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28856 TREE_VALUE (parm
));
28857 TREE_PURPOSE (parm
) = parsed_arg
;
28859 /* Update any instantiations we've already created. */
28860 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28861 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28862 TREE_PURPOSE (copy
) = parsed_arg
;
28865 pop_defarg_context ();
28867 /* Make sure no default arg is missing. */
28868 check_default_args (fn
);
28870 /* Restore the state of local_variables_forbidden_p. */
28871 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28873 /* Restore the queue. */
28874 pop_unparsed_function_queues (parser
);
28877 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28879 sizeof ... ( identifier )
28881 where the 'sizeof' token has already been consumed. */
28884 cp_parser_sizeof_pack (cp_parser
*parser
)
28886 /* Consume the `...'. */
28887 cp_lexer_consume_token (parser
->lexer
);
28888 maybe_warn_variadic_templates ();
28890 matching_parens parens
;
28891 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28893 parens
.consume_open (parser
);
28895 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28896 "%<sizeof...%> argument must be surrounded by parentheses");
28898 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28899 tree name
= cp_parser_identifier (parser
);
28900 if (name
== error_mark_node
)
28901 return error_mark_node
;
28902 /* The name is not qualified. */
28903 parser
->scope
= NULL_TREE
;
28904 parser
->qualifying_scope
= NULL_TREE
;
28905 parser
->object_scope
= NULL_TREE
;
28906 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28907 if (expr
== error_mark_node
)
28908 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28910 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28911 expr
= TREE_TYPE (expr
);
28912 else if (TREE_CODE (expr
) == CONST_DECL
)
28913 expr
= DECL_INITIAL (expr
);
28914 expr
= make_pack_expansion (expr
);
28915 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28918 parens
.require_close (parser
);
28923 /* Parse the operand of `sizeof' (or a similar operator). Returns
28924 either a TYPE or an expression, depending on the form of the
28925 input. The KEYWORD indicates which kind of expression we have
28929 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28931 tree expr
= NULL_TREE
;
28932 const char *saved_message
;
28933 const char *saved_message_arg
;
28934 bool saved_integral_constant_expression_p
;
28935 bool saved_non_integral_constant_expression_p
;
28937 /* If it's a `...', then we are computing the length of a parameter
28939 if (keyword
== RID_SIZEOF
28940 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28941 return cp_parser_sizeof_pack (parser
);
28943 /* Types cannot be defined in a `sizeof' expression. Save away the
28945 saved_message
= parser
->type_definition_forbidden_message
;
28946 saved_message_arg
= parser
->type_definition_forbidden_message_arg
;
28947 parser
->type_definition_forbidden_message
28948 = G_("types may not be defined in %qs expressions");
28949 parser
->type_definition_forbidden_message_arg
28950 = IDENTIFIER_POINTER (ridpointers
[keyword
]);
28952 /* The restrictions on constant-expressions do not apply inside
28953 sizeof expressions. */
28954 saved_integral_constant_expression_p
28955 = parser
->integral_constant_expression_p
;
28956 saved_non_integral_constant_expression_p
28957 = parser
->non_integral_constant_expression_p
;
28958 parser
->integral_constant_expression_p
= false;
28960 /* Do not actually evaluate the expression. */
28961 ++cp_unevaluated_operand
;
28962 ++c_inhibit_evaluation_warnings
;
28963 /* If it's a `(', then we might be looking at the type-id
28965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28967 tree type
= NULL_TREE
;
28969 /* We can't be sure yet whether we're looking at a type-id or an
28971 cp_parser_parse_tentatively (parser
);
28973 matching_parens parens
;
28974 parens
.consume_open (parser
);
28976 /* Note: as a GNU Extension, compound literals are considered
28977 postfix-expressions as they are in C99, so they are valid
28978 arguments to sizeof. See comment in cp_parser_cast_expression
28980 if (cp_parser_compound_literal_p (parser
))
28981 cp_parser_simulate_error (parser
);
28984 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28985 parser
->in_type_id_in_expr_p
= true;
28986 /* Look for the type-id. */
28987 type
= cp_parser_type_id (parser
);
28988 /* Look for the closing `)'. */
28989 parens
.require_close (parser
);
28990 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28993 /* If all went well, then we're done. */
28994 if (cp_parser_parse_definitely (parser
))
28998 /* If the type-id production did not work out, then we must be
28999 looking at the unary-expression production. */
29001 expr
= cp_parser_unary_expression (parser
);
29003 /* Go back to evaluating expressions. */
29004 --cp_unevaluated_operand
;
29005 --c_inhibit_evaluation_warnings
;
29007 /* And restore the old one. */
29008 parser
->type_definition_forbidden_message
= saved_message
;
29009 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
29010 parser
->integral_constant_expression_p
29011 = saved_integral_constant_expression_p
;
29012 parser
->non_integral_constant_expression_p
29013 = saved_non_integral_constant_expression_p
;
29018 /* If the current declaration has no declarator, return true. */
29021 cp_parser_declares_only_class_p (cp_parser
*parser
)
29023 /* If the next token is a `;' or a `,' then there is no
29025 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29026 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
29029 /* Update the DECL_SPECS to reflect the storage class indicated by
29033 cp_parser_set_storage_class (cp_parser
*parser
,
29034 cp_decl_specifier_seq
*decl_specs
,
29038 cp_storage_class storage_class
;
29040 if (parser
->in_unbraced_linkage_specification_p
)
29042 error_at (token
->location
, "invalid use of %qD in linkage specification",
29043 ridpointers
[keyword
]);
29046 else if (decl_specs
->storage_class
!= sc_none
)
29048 decl_specs
->conflicting_specifiers_p
= true;
29052 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
29053 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
29054 && decl_specs
->gnu_thread_keyword_p
)
29056 pedwarn (decl_specs
->locations
[ds_thread
], 0,
29057 "%<__thread%> before %qD", ridpointers
[keyword
]);
29063 storage_class
= sc_auto
;
29066 storage_class
= sc_register
;
29069 storage_class
= sc_static
;
29072 storage_class
= sc_extern
;
29075 storage_class
= sc_mutable
;
29078 gcc_unreachable ();
29080 decl_specs
->storage_class
= storage_class
;
29081 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
29083 /* A storage class specifier cannot be applied alongside a typedef
29084 specifier. If there is a typedef specifier present then set
29085 conflicting_specifiers_p which will trigger an error later
29086 on in grokdeclarator. */
29087 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
29088 decl_specs
->conflicting_specifiers_p
= true;
29091 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29092 is true, the type is a class or enum definition. */
29095 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
29098 bool type_definition_p
)
29100 decl_specs
->any_specifiers_p
= true;
29102 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29103 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29104 this is what happened. In system headers, we ignore these
29105 declarations so that G++ can work with system headers that are not
29107 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
29108 && !type_definition_p
29109 && (type_spec
== boolean_type_node
29110 || type_spec
== char8_type_node
29111 || type_spec
== char16_type_node
29112 || type_spec
== char32_type_node
29113 || type_spec
== wchar_type_node
)
29114 && (decl_specs
->type
29115 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
29116 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
29117 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
29118 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
29120 decl_specs
->redefined_builtin_type
= type_spec
;
29121 set_and_check_decl_spec_loc (decl_specs
,
29122 ds_redefined_builtin_type_spec
,
29124 if (!decl_specs
->type
)
29126 decl_specs
->type
= type_spec
;
29127 decl_specs
->type_definition_p
= false;
29128 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
29131 else if (decl_specs
->type
)
29132 decl_specs
->multiple_types_p
= true;
29135 decl_specs
->type
= type_spec
;
29136 decl_specs
->type_definition_p
= type_definition_p
;
29137 decl_specs
->redefined_builtin_type
= NULL_TREE
;
29138 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
29142 /* True iff TOKEN is the GNU keyword __thread. */
29145 token_is__thread (cp_token
*token
)
29147 gcc_assert (token
->keyword
== RID_THREAD
);
29148 return id_equal (token
->u
.value
, "__thread");
29151 /* Set the location for a declarator specifier and check if it is
29154 DECL_SPECS is the sequence of declarator specifiers onto which to
29157 DS is the single declarator specifier to set which location is to
29158 be set onto the existing sequence of declarators.
29160 LOCATION is the location for the declarator specifier to
29164 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
29165 cp_decl_spec ds
, cp_token
*token
)
29167 gcc_assert (ds
< ds_last
);
29169 if (decl_specs
== NULL
)
29172 location_t location
= token
->location
;
29174 if (decl_specs
->locations
[ds
] == 0)
29176 decl_specs
->locations
[ds
] = location
;
29177 if (ds
== ds_thread
)
29178 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
29184 if (decl_specs
->locations
[ds_long_long
] != 0)
29185 error_at (location
,
29186 "%<long long long%> is too long for GCC");
29189 decl_specs
->locations
[ds_long_long
] = location
;
29190 pedwarn_cxx98 (location
,
29192 "ISO C++ 1998 does not support %<long long%>");
29195 else if (ds
== ds_thread
)
29197 bool gnu
= token_is__thread (token
);
29198 gcc_rich_location
richloc (location
);
29199 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
29201 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
29202 error_at (&richloc
,
29203 "both %<__thread%> and %<thread_local%> specified");
29207 richloc
.add_fixit_remove ();
29208 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
29213 static const char *const decl_spec_names
[] = {
29230 gcc_rich_location
richloc (location
);
29231 richloc
.add_fixit_remove ();
29232 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
29237 /* Return true iff the declarator specifier DS is present in the
29238 sequence of declarator specifiers DECL_SPECS. */
29241 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
29244 gcc_assert (ds
< ds_last
);
29246 if (decl_specs
== NULL
)
29249 return decl_specs
->locations
[ds
] != 0;
29252 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29253 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29256 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
29258 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
29261 /* Issue an error message indicating that TOKEN_DESC was expected.
29262 If KEYWORD is true, it indicated this function is called by
29263 cp_parser_require_keword and the required token can only be
29264 a indicated keyword.
29266 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29267 within any error as the location of an "opening" token matching
29268 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29269 RT_CLOSE_PAREN). */
29272 cp_parser_required_error (cp_parser
*parser
,
29273 required_token token_desc
,
29275 location_t matching_location
)
29277 if (cp_parser_simulate_error (parser
))
29280 const char *gmsgid
= NULL
;
29281 switch (token_desc
)
29284 gmsgid
= G_("expected %<new%>");
29287 gmsgid
= G_("expected %<delete%>");
29290 gmsgid
= G_("expected %<return%>");
29293 gmsgid
= G_("expected %<while%>");
29296 gmsgid
= G_("expected %<extern%>");
29298 case RT_STATIC_ASSERT
:
29299 gmsgid
= G_("expected %<static_assert%>");
29302 gmsgid
= G_("expected %<decltype%>");
29305 gmsgid
= G_("expected %<operator%>");
29308 gmsgid
= G_("expected %<class%>");
29311 gmsgid
= G_("expected %<template%>");
29314 gmsgid
= G_("expected %<namespace%>");
29317 gmsgid
= G_("expected %<using%>");
29320 gmsgid
= G_("expected %<asm%>");
29323 gmsgid
= G_("expected %<try%>");
29326 gmsgid
= G_("expected %<catch%>");
29329 gmsgid
= G_("expected %<throw%>");
29332 gmsgid
= G_("expected %<__label__%>");
29335 gmsgid
= G_("expected %<@try%>");
29337 case RT_AT_SYNCHRONIZED
:
29338 gmsgid
= G_("expected %<@synchronized%>");
29341 gmsgid
= G_("expected %<@throw%>");
29343 case RT_TRANSACTION_ATOMIC
:
29344 gmsgid
= G_("expected %<__transaction_atomic%>");
29346 case RT_TRANSACTION_RELAXED
:
29347 gmsgid
= G_("expected %<__transaction_relaxed%>");
29353 if (!gmsgid
&& !keyword
)
29355 switch (token_desc
)
29358 gmsgid
= G_("expected %<;%>");
29360 case RT_OPEN_PAREN
:
29361 gmsgid
= G_("expected %<(%>");
29363 case RT_CLOSE_BRACE
:
29364 gmsgid
= G_("expected %<}%>");
29366 case RT_OPEN_BRACE
:
29367 gmsgid
= G_("expected %<{%>");
29369 case RT_CLOSE_SQUARE
:
29370 gmsgid
= G_("expected %<]%>");
29372 case RT_OPEN_SQUARE
:
29373 gmsgid
= G_("expected %<[%>");
29376 gmsgid
= G_("expected %<,%>");
29379 gmsgid
= G_("expected %<::%>");
29382 gmsgid
= G_("expected %<<%>");
29385 gmsgid
= G_("expected %<>%>");
29388 gmsgid
= G_("expected %<=%>");
29391 gmsgid
= G_("expected %<...%>");
29394 gmsgid
= G_("expected %<*%>");
29397 gmsgid
= G_("expected %<~%>");
29400 gmsgid
= G_("expected %<:%>");
29402 case RT_COLON_SCOPE
:
29403 gmsgid
= G_("expected %<:%> or %<::%>");
29405 case RT_CLOSE_PAREN
:
29406 gmsgid
= G_("expected %<)%>");
29408 case RT_COMMA_CLOSE_PAREN
:
29409 gmsgid
= G_("expected %<,%> or %<)%>");
29411 case RT_PRAGMA_EOL
:
29412 gmsgid
= G_("expected end of line");
29415 gmsgid
= G_("expected identifier");
29418 gmsgid
= G_("expected selection-statement");
29421 gmsgid
= G_("expected iteration-statement");
29424 gmsgid
= G_("expected jump-statement");
29427 gmsgid
= G_("expected class-key");
29429 case RT_CLASS_TYPENAME_TEMPLATE
:
29430 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
29433 gcc_unreachable ();
29438 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
29442 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29443 issue an error message indicating that TOKEN_DESC was expected.
29445 Returns the token consumed, if the token had the appropriate type.
29446 Otherwise, returns NULL.
29448 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29449 within any error as the location of an "opening" token matching
29450 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29451 RT_CLOSE_PAREN). */
29454 cp_parser_require (cp_parser
* parser
,
29455 enum cpp_ttype type
,
29456 required_token token_desc
,
29457 location_t matching_location
)
29459 if (cp_lexer_next_token_is (parser
->lexer
, type
))
29460 return cp_lexer_consume_token (parser
->lexer
);
29463 /* Output the MESSAGE -- unless we're parsing tentatively. */
29464 if (!cp_parser_simulate_error (parser
))
29465 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
29466 matching_location
);
29471 /* An error message is produced if the next token is not '>'.
29472 All further tokens are skipped until the desired token is
29473 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29476 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29478 /* Current level of '< ... >'. */
29479 unsigned level
= 0;
29480 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29481 unsigned nesting_depth
= 0;
29483 /* Are we ready, yet? If not, issue error message. */
29484 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29487 /* Skip tokens until the desired token is found. */
29490 /* Peek at the next token. */
29491 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29494 if (!nesting_depth
)
29499 if (cxx_dialect
== cxx98
)
29500 /* C++0x views the `>>' operator as two `>' tokens, but
29503 else if (!nesting_depth
&& level
-- == 0)
29505 /* We've hit a `>>' where the first `>' closes the
29506 template argument list, and the second `>' is
29507 spurious. Just consume the `>>' and stop; we've
29508 already produced at least one error. */
29509 cp_lexer_consume_token (parser
->lexer
);
29512 /* Fall through for C++0x, so we handle the second `>' in
29514 gcc_fallthrough ();
29517 if (!nesting_depth
&& level
-- == 0)
29519 /* We've reached the token we want, consume it and stop. */
29520 cp_lexer_consume_token (parser
->lexer
);
29525 case CPP_OPEN_PAREN
:
29526 case CPP_OPEN_SQUARE
:
29530 case CPP_CLOSE_PAREN
:
29531 case CPP_CLOSE_SQUARE
:
29532 if (nesting_depth
-- == 0)
29537 case CPP_PRAGMA_EOL
:
29538 case CPP_SEMICOLON
:
29539 case CPP_OPEN_BRACE
:
29540 case CPP_CLOSE_BRACE
:
29541 /* The '>' was probably forgotten, don't look further. */
29548 /* Consume this token. */
29549 cp_lexer_consume_token (parser
->lexer
);
29553 /* If the next token is the indicated keyword, consume it. Otherwise,
29554 issue an error message indicating that TOKEN_DESC was expected.
29556 Returns the token consumed, if the token had the appropriate type.
29557 Otherwise, returns NULL. */
29560 cp_parser_require_keyword (cp_parser
* parser
,
29562 required_token token_desc
)
29564 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29566 if (token
&& token
->keyword
!= keyword
)
29568 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29576 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29577 function-definition. */
29580 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29582 return (/* An ordinary function-body begins with an `{'. */
29583 token
->type
== CPP_OPEN_BRACE
29584 /* A ctor-initializer begins with a `:'. */
29585 || token
->type
== CPP_COLON
29586 /* A function-try-block begins with `try'. */
29587 || token
->keyword
== RID_TRY
29588 /* A function-transaction-block begins with `__transaction_atomic'
29589 or `__transaction_relaxed'. */
29590 || token
->keyword
== RID_TRANSACTION_ATOMIC
29591 || token
->keyword
== RID_TRANSACTION_RELAXED
29592 /* The named return value extension begins with `return'. */
29593 || token
->keyword
== RID_RETURN
);
29596 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29600 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29604 token
= cp_lexer_peek_token (parser
->lexer
);
29605 return (token
->type
== CPP_OPEN_BRACE
29606 || (token
->type
== CPP_COLON
29607 && !parser
->colon_doesnt_start_class_def_p
));
29610 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29611 C++0x) ending a template-argument. */
29614 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29618 token
= cp_lexer_peek_token (parser
->lexer
);
29619 return (token
->type
== CPP_COMMA
29620 || token
->type
== CPP_GREATER
29621 || token
->type
== CPP_ELLIPSIS
29622 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29625 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29626 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29629 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29634 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29635 if (token
->type
== CPP_LESS
)
29637 /* Check for the sequence `<::' in the original code. It would be lexed as
29638 `[:', where `[' is a digraph, and there is no whitespace before
29640 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29643 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29644 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29650 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29651 or none_type otherwise. */
29653 static enum tag_types
29654 cp_parser_token_is_class_key (cp_token
* token
)
29656 switch (token
->keyword
)
29661 return record_type
;
29670 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29671 or none_type otherwise or if the token is null. */
29673 static enum tag_types
29674 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29679 switch (token
->keyword
)
29684 return typename_type
;
29691 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29694 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29696 if (type
== error_mark_node
)
29698 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29700 if (permerror (input_location
, "%qs tag used in naming %q#T",
29701 class_key
== union_type
? "union"
29702 : class_key
== record_type
? "struct" : "class",
29704 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29705 "%q#T was previously declared here", type
);
29709 /* Issue an error message if DECL is redeclared with different
29710 access than its original declaration [class.access.spec/3].
29711 This applies to nested classes, nested class templates and
29712 enumerations [class.mem/1]. */
29715 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29718 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29719 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29722 if ((TREE_PRIVATE (decl
)
29723 != (current_access_specifier
== access_private_node
))
29724 || (TREE_PROTECTED (decl
)
29725 != (current_access_specifier
== access_protected_node
)))
29726 error_at (location
, "%qD redeclared with different access", decl
);
29729 /* Look for the `template' keyword, as a syntactic disambiguator.
29730 Return TRUE iff it is present, in which case it will be
29734 cp_parser_optional_template_keyword (cp_parser
*parser
)
29736 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29738 /* In C++98 the `template' keyword can only be used within templates;
29739 outside templates the parser can always figure out what is a
29740 template and what is not. In C++11, per the resolution of DR 468,
29741 `template' is allowed in cases where it is not strictly necessary. */
29742 if (!processing_template_decl
29743 && pedantic
&& cxx_dialect
== cxx98
)
29745 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29746 pedwarn (token
->location
, OPT_Wpedantic
,
29747 "in C++98 %<template%> (as a disambiguator) is only "
29748 "allowed within templates");
29749 /* If this part of the token stream is rescanned, the same
29750 error message would be generated. So, we purge the token
29751 from the stream. */
29752 cp_lexer_purge_token (parser
->lexer
);
29757 /* Consume the `template' keyword. */
29758 cp_lexer_consume_token (parser
->lexer
);
29765 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29766 set PARSER->SCOPE, and perform other related actions. */
29769 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29771 struct tree_check
*check_value
;
29773 /* Get the stored value. */
29774 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29775 /* Set the scope from the stored value. */
29776 parser
->scope
= saved_checks_value (check_value
);
29777 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29778 parser
->object_scope
= NULL_TREE
;
29781 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29782 encounter the end of a block before what we were looking for. */
29785 cp_parser_cache_group (cp_parser
*parser
,
29786 enum cpp_ttype end
,
29791 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29793 /* Abort a parenthesized expression if we encounter a semicolon. */
29794 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29795 && token
->type
== CPP_SEMICOLON
)
29797 /* If we've reached the end of the file, stop. */
29798 if (token
->type
== CPP_EOF
29799 || (end
!= CPP_PRAGMA_EOL
29800 && token
->type
== CPP_PRAGMA_EOL
))
29802 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29803 /* We've hit the end of an enclosing block, so there's been some
29804 kind of syntax error. */
29807 /* Consume the token. */
29808 cp_lexer_consume_token (parser
->lexer
);
29809 /* See if it starts a new group. */
29810 if (token
->type
== CPP_OPEN_BRACE
)
29812 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29813 /* In theory this should probably check end == '}', but
29814 cp_parser_save_member_function_body needs it to exit
29815 after either '}' or ')' when called with ')'. */
29819 else if (token
->type
== CPP_OPEN_PAREN
)
29821 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29822 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29825 else if (token
->type
== CPP_PRAGMA
)
29826 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29827 else if (token
->type
== end
)
29832 /* Like above, for caching a default argument or NSDMI. Both of these are
29833 terminated by a non-nested comma, but it can be unclear whether or not a
29834 comma is nested in a template argument list unless we do more parsing.
29835 In order to handle this ambiguity, when we encounter a ',' after a '<'
29836 we try to parse what follows as a parameter-declaration-list (in the
29837 case of a default argument) or a member-declarator (in the case of an
29838 NSDMI). If that succeeds, then we stop caching. */
29841 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29843 unsigned depth
= 0;
29844 int maybe_template_id
= 0;
29845 cp_token
*first_token
;
29847 tree default_argument
;
29849 /* Add tokens until we have processed the entire default
29850 argument. We add the range [first_token, token). */
29851 first_token
= cp_lexer_peek_token (parser
->lexer
);
29852 if (first_token
->type
== CPP_OPEN_BRACE
)
29854 /* For list-initialization, this is straightforward. */
29855 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29856 token
= cp_lexer_peek_token (parser
->lexer
);
29862 /* Peek at the next token. */
29863 token
= cp_lexer_peek_token (parser
->lexer
);
29864 /* What we do depends on what token we have. */
29865 switch (token
->type
)
29867 /* In valid code, a default argument must be
29868 immediately followed by a `,' `)', or `...'. */
29870 if (depth
== 0 && maybe_template_id
)
29872 /* If we've seen a '<', we might be in a
29873 template-argument-list. Until Core issue 325 is
29874 resolved, we don't know how this situation ought
29875 to be handled, so try to DTRT. We check whether
29876 what comes after the comma is a valid parameter
29877 declaration list. If it is, then the comma ends
29878 the default argument; otherwise the default
29879 argument continues. */
29880 bool error
= false;
29883 /* Set ITALP so cp_parser_parameter_declaration_list
29884 doesn't decide to commit to this parse. */
29885 bool saved_italp
= parser
->in_template_argument_list_p
;
29886 parser
->in_template_argument_list_p
= true;
29888 cp_parser_parse_tentatively (parser
);
29892 /* Parse declarators until we reach a non-comma or
29893 somthing that cannot be an initializer.
29894 Just checking whether we're looking at a single
29895 declarator is insufficient. Consider:
29896 int var = tuple<T,U>::x;
29897 The template parameter 'U' looks exactly like a
29901 int ctor_dtor_or_conv_p
;
29902 cp_lexer_consume_token (parser
->lexer
);
29903 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29904 CP_PARSER_FLAGS_NONE
,
29905 &ctor_dtor_or_conv_p
,
29906 /*parenthesized_p=*/NULL
,
29908 /*friend_p=*/false,
29909 /*static_p=*/false);
29910 peek
= cp_lexer_peek_token (parser
->lexer
);
29911 if (cp_parser_error_occurred (parser
))
29914 while (peek
->type
== CPP_COMMA
);
29915 /* If we met an '=' or ';' then the original comma
29916 was the end of the NSDMI. Otherwise assume
29917 we're still in the NSDMI. */
29918 error
= (peek
->type
!= CPP_EQ
29919 && peek
->type
!= CPP_SEMICOLON
);
29923 cp_lexer_consume_token (parser
->lexer
);
29924 begin_scope (sk_function_parms
, NULL_TREE
);
29925 tree t
= cp_parser_parameter_declaration_list
29926 (parser
, CP_PARSER_FLAGS_NONE
);
29927 if (t
== error_mark_node
)
29929 pop_bindings_and_leave_scope ();
29931 if (!cp_parser_error_occurred (parser
) && !error
)
29933 cp_parser_abort_tentative_parse (parser
);
29935 parser
->in_template_argument_list_p
= saved_italp
;
29939 case CPP_CLOSE_PAREN
:
29941 /* If we run into a non-nested `;', `}', or `]',
29942 then the code is invalid -- but the default
29943 argument is certainly over. */
29944 case CPP_SEMICOLON
:
29945 case CPP_CLOSE_BRACE
:
29946 case CPP_CLOSE_SQUARE
:
29948 /* Handle correctly int n = sizeof ... ( p ); */
29949 && token
->type
!= CPP_ELLIPSIS
)
29951 /* Update DEPTH, if necessary. */
29952 else if (token
->type
== CPP_CLOSE_PAREN
29953 || token
->type
== CPP_CLOSE_BRACE
29954 || token
->type
== CPP_CLOSE_SQUARE
)
29958 case CPP_OPEN_PAREN
:
29959 case CPP_OPEN_SQUARE
:
29960 case CPP_OPEN_BRACE
:
29966 /* This might be the comparison operator, or it might
29967 start a template argument list. */
29968 ++maybe_template_id
;
29972 if (cxx_dialect
== cxx98
)
29974 /* Fall through for C++0x, which treats the `>>'
29975 operator like two `>' tokens in certain
29977 gcc_fallthrough ();
29982 /* This might be an operator, or it might close a
29983 template argument list. But if a previous '<'
29984 started a template argument list, this will have
29985 closed it, so we can't be in one anymore. */
29986 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29987 if (maybe_template_id
< 0)
29988 maybe_template_id
= 0;
29992 /* If we run out of tokens, issue an error message. */
29994 case CPP_PRAGMA_EOL
:
29995 error_at (token
->location
, "file ends in default argument");
29996 return error_mark_node
;
30000 /* In these cases, we should look for template-ids.
30001 For example, if the default argument is
30002 `X<int, double>()', we need to do name lookup to
30003 figure out whether or not `X' is a template; if
30004 so, the `,' does not end the default argument.
30006 That is not yet done. */
30013 /* If we've reached the end, stop. */
30017 /* Add the token to the token block. */
30018 token
= cp_lexer_consume_token (parser
->lexer
);
30021 /* Create a DEFAULT_ARG to represent the unparsed default
30023 default_argument
= make_node (DEFAULT_ARG
);
30024 DEFARG_TOKENS (default_argument
)
30025 = cp_token_cache_new (first_token
, token
);
30026 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
30028 return default_argument
;
30031 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
30034 defarg_location (tree default_argument
)
30036 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
30037 location_t start
= tokens
->first
->location
;
30038 location_t end
= tokens
->last
->location
;
30039 return make_location (start
, start
, end
);
30042 /* Begin parsing tentatively. We always save tokens while parsing
30043 tentatively so that if the tentative parsing fails we can restore the
30047 cp_parser_parse_tentatively (cp_parser
* parser
)
30049 /* Enter a new parsing context. */
30050 parser
->context
= cp_parser_context_new (parser
->context
);
30051 /* Begin saving tokens. */
30052 cp_lexer_save_tokens (parser
->lexer
);
30053 /* In order to avoid repetitive access control error messages,
30054 access checks are queued up until we are no longer parsing
30056 push_deferring_access_checks (dk_deferred
);
30059 /* Commit to the currently active tentative parse. */
30062 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
30064 cp_parser_context
*context
;
30067 /* Mark all of the levels as committed. */
30068 lexer
= parser
->lexer
;
30069 for (context
= parser
->context
; context
->next
; context
= context
->next
)
30071 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
30073 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
30074 while (!cp_lexer_saving_tokens (lexer
))
30075 lexer
= lexer
->next
;
30076 cp_lexer_commit_tokens (lexer
);
30080 /* Commit to the topmost currently active tentative parse.
30082 Note that this function shouldn't be called when there are
30083 irreversible side-effects while in a tentative state. For
30084 example, we shouldn't create a permanent entry in the symbol
30085 table, or issue an error message that might not apply if the
30086 tentative parse is aborted. */
30089 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
30091 cp_parser_context
*context
= parser
->context
;
30092 cp_lexer
*lexer
= parser
->lexer
;
30096 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
30098 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
30100 while (!cp_lexer_saving_tokens (lexer
))
30101 lexer
= lexer
->next
;
30102 cp_lexer_commit_tokens (lexer
);
30106 /* Abort the currently active tentative parse. All consumed tokens
30107 will be rolled back, and no diagnostics will be issued. */
30110 cp_parser_abort_tentative_parse (cp_parser
* parser
)
30112 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
30113 || errorcount
> 0);
30114 cp_parser_simulate_error (parser
);
30115 /* Now, pretend that we want to see if the construct was
30116 successfully parsed. */
30117 cp_parser_parse_definitely (parser
);
30120 /* Stop parsing tentatively. If a parse error has occurred, restore the
30121 token stream. Otherwise, commit to the tokens we have consumed.
30122 Returns true if no error occurred; false otherwise. */
30125 cp_parser_parse_definitely (cp_parser
* parser
)
30127 bool error_occurred
;
30128 cp_parser_context
*context
;
30130 /* Remember whether or not an error occurred, since we are about to
30131 destroy that information. */
30132 error_occurred
= cp_parser_error_occurred (parser
);
30133 /* Remove the topmost context from the stack. */
30134 context
= parser
->context
;
30135 parser
->context
= context
->next
;
30136 /* If no parse errors occurred, commit to the tentative parse. */
30137 if (!error_occurred
)
30139 /* Commit to the tokens read tentatively, unless that was
30141 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
30142 cp_lexer_commit_tokens (parser
->lexer
);
30144 pop_to_parent_deferring_access_checks ();
30146 /* Otherwise, if errors occurred, roll back our state so that things
30147 are just as they were before we began the tentative parse. */
30150 cp_lexer_rollback_tokens (parser
->lexer
);
30151 pop_deferring_access_checks ();
30153 /* Add the context to the front of the free list. */
30154 context
->next
= cp_parser_context_free_list
;
30155 cp_parser_context_free_list
= context
;
30157 return !error_occurred
;
30160 /* Returns true if we are parsing tentatively and are not committed to
30161 this tentative parse. */
30164 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
30166 return (cp_parser_parsing_tentatively (parser
)
30167 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
30170 /* Returns nonzero iff an error has occurred during the most recent
30171 tentative parse. */
30174 cp_parser_error_occurred (cp_parser
* parser
)
30176 return (cp_parser_parsing_tentatively (parser
)
30177 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
30180 /* Returns nonzero if GNU extensions are allowed. */
30183 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
30185 return parser
->allow_gnu_extensions_p
;
30188 /* Objective-C++ Productions */
30191 /* Parse an Objective-C expression, which feeds into a primary-expression
30195 objc-message-expression
30196 objc-string-literal
30197 objc-encode-expression
30198 objc-protocol-expression
30199 objc-selector-expression
30201 Returns a tree representation of the expression. */
30204 cp_parser_objc_expression (cp_parser
* parser
)
30206 /* Try to figure out what kind of declaration is present. */
30207 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30211 case CPP_OPEN_SQUARE
:
30212 return cp_parser_objc_message_expression (parser
);
30214 case CPP_OBJC_STRING
:
30215 kwd
= cp_lexer_consume_token (parser
->lexer
);
30216 return objc_build_string_object (kwd
->u
.value
);
30219 switch (kwd
->keyword
)
30221 case RID_AT_ENCODE
:
30222 return cp_parser_objc_encode_expression (parser
);
30224 case RID_AT_PROTOCOL
:
30225 return cp_parser_objc_protocol_expression (parser
);
30227 case RID_AT_SELECTOR
:
30228 return cp_parser_objc_selector_expression (parser
);
30235 error_at (kwd
->location
,
30236 "misplaced %<@%D%> Objective-C++ construct",
30238 cp_parser_skip_to_end_of_block_or_statement (parser
);
30241 return error_mark_node
;
30244 /* Parse an Objective-C message expression.
30246 objc-message-expression:
30247 [ objc-message-receiver objc-message-args ]
30249 Returns a representation of an Objective-C message. */
30252 cp_parser_objc_message_expression (cp_parser
* parser
)
30254 tree receiver
, messageargs
;
30256 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30257 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
30258 receiver
= cp_parser_objc_message_receiver (parser
);
30259 messageargs
= cp_parser_objc_message_args (parser
);
30260 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30261 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30263 tree result
= objc_build_message_expr (receiver
, messageargs
);
30265 /* Construct a location e.g.
30268 ranging from the '[' to the ']', with the caret at the start. */
30269 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
30270 protected_set_expr_location (result
, combined_loc
);
30275 /* Parse an objc-message-receiver.
30277 objc-message-receiver:
30279 simple-type-specifier
30281 Returns a representation of the type or expression. */
30284 cp_parser_objc_message_receiver (cp_parser
* parser
)
30288 /* An Objective-C message receiver may be either (1) a type
30289 or (2) an expression. */
30290 cp_parser_parse_tentatively (parser
);
30291 rcv
= cp_parser_expression (parser
);
30293 /* If that worked out, fine. */
30294 if (cp_parser_parse_definitely (parser
))
30297 cp_parser_parse_tentatively (parser
);
30298 rcv
= cp_parser_simple_type_specifier (parser
,
30299 /*decl_specs=*/NULL
,
30300 CP_PARSER_FLAGS_NONE
);
30302 if (cp_parser_parse_definitely (parser
))
30303 return objc_get_class_reference (rcv
);
30305 cp_parser_error (parser
, "objective-c++ message receiver expected");
30306 return error_mark_node
;
30309 /* Parse the arguments and selectors comprising an Objective-C message.
30314 objc-selector-args , objc-comma-args
30316 objc-selector-args:
30317 objc-selector [opt] : assignment-expression
30318 objc-selector-args objc-selector [opt] : assignment-expression
30321 assignment-expression
30322 objc-comma-args , assignment-expression
30324 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30325 selector arguments and TREE_VALUE containing a list of comma
30329 cp_parser_objc_message_args (cp_parser
* parser
)
30331 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
30332 bool maybe_unary_selector_p
= true;
30333 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30335 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30337 tree selector
= NULL_TREE
, arg
;
30339 if (token
->type
!= CPP_COLON
)
30340 selector
= cp_parser_objc_selector (parser
);
30342 /* Detect if we have a unary selector. */
30343 if (maybe_unary_selector_p
30344 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30345 return build_tree_list (selector
, NULL_TREE
);
30347 maybe_unary_selector_p
= false;
30348 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30349 arg
= cp_parser_assignment_expression (parser
);
30352 = chainon (sel_args
,
30353 build_tree_list (selector
, arg
));
30355 token
= cp_lexer_peek_token (parser
->lexer
);
30358 /* Handle non-selector arguments, if any. */
30359 while (token
->type
== CPP_COMMA
)
30363 cp_lexer_consume_token (parser
->lexer
);
30364 arg
= cp_parser_assignment_expression (parser
);
30367 = chainon (addl_args
,
30368 build_tree_list (NULL_TREE
, arg
));
30370 token
= cp_lexer_peek_token (parser
->lexer
);
30373 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
30375 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
30376 return build_tree_list (error_mark_node
, error_mark_node
);
30379 return build_tree_list (sel_args
, addl_args
);
30382 /* Parse an Objective-C encode expression.
30384 objc-encode-expression:
30385 @encode objc-typename
30387 Returns an encoded representation of the type argument. */
30390 cp_parser_objc_encode_expression (cp_parser
* parser
)
30394 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30396 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
30397 matching_parens parens
;
30398 parens
.require_open (parser
);
30399 token
= cp_lexer_peek_token (parser
->lexer
);
30400 type
= complete_type (cp_parser_type_id (parser
));
30401 parens
.require_close (parser
);
30405 error_at (token
->location
,
30406 "%<@encode%> must specify a type as an argument");
30407 return error_mark_node
;
30410 /* This happens if we find @encode(T) (where T is a template
30411 typename or something dependent on a template typename) when
30412 parsing a template. In that case, we can't compile it
30413 immediately, but we rather create an AT_ENCODE_EXPR which will
30414 need to be instantiated when the template is used.
30416 if (dependent_type_p (type
))
30418 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
30419 TREE_READONLY (value
) = 1;
30424 /* Build a location of the form:
30427 with caret==start at the @ token, finishing at the close paren. */
30428 location_t combined_loc
30429 = make_location (start_loc
, start_loc
,
30430 cp_lexer_previous_token (parser
->lexer
)->location
);
30432 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
30435 /* Parse an Objective-C @defs expression. */
30438 cp_parser_objc_defs_expression (cp_parser
*parser
)
30442 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
30443 matching_parens parens
;
30444 parens
.require_open (parser
);
30445 name
= cp_parser_identifier (parser
);
30446 parens
.require_close (parser
);
30448 return objc_get_class_ivars (name
);
30451 /* Parse an Objective-C protocol expression.
30453 objc-protocol-expression:
30454 @protocol ( identifier )
30456 Returns a representation of the protocol expression. */
30459 cp_parser_objc_protocol_expression (cp_parser
* parser
)
30462 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30464 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30465 matching_parens parens
;
30466 parens
.require_open (parser
);
30467 proto
= cp_parser_identifier (parser
);
30468 parens
.require_close (parser
);
30470 /* Build a location of the form:
30473 with caret==start at the @ token, finishing at the close paren. */
30474 location_t combined_loc
30475 = make_location (start_loc
, start_loc
,
30476 cp_lexer_previous_token (parser
->lexer
)->location
);
30477 tree result
= objc_build_protocol_expr (proto
);
30478 protected_set_expr_location (result
, combined_loc
);
30482 /* Parse an Objective-C selector expression.
30484 objc-selector-expression:
30485 @selector ( objc-method-signature )
30487 objc-method-signature:
30493 objc-selector-seq objc-selector :
30495 Returns a representation of the method selector. */
30498 cp_parser_objc_selector_expression (cp_parser
* parser
)
30500 tree sel_seq
= NULL_TREE
;
30501 bool maybe_unary_selector_p
= true;
30503 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30505 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30506 matching_parens parens
;
30507 parens
.require_open (parser
);
30508 token
= cp_lexer_peek_token (parser
->lexer
);
30510 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30511 || token
->type
== CPP_SCOPE
)
30513 tree selector
= NULL_TREE
;
30515 if (token
->type
!= CPP_COLON
30516 || token
->type
== CPP_SCOPE
)
30517 selector
= cp_parser_objc_selector (parser
);
30519 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30520 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30522 /* Detect if we have a unary selector. */
30523 if (maybe_unary_selector_p
)
30525 sel_seq
= selector
;
30526 goto finish_selector
;
30530 cp_parser_error (parser
, "expected %<:%>");
30533 maybe_unary_selector_p
= false;
30534 token
= cp_lexer_consume_token (parser
->lexer
);
30536 if (token
->type
== CPP_SCOPE
)
30539 = chainon (sel_seq
,
30540 build_tree_list (selector
, NULL_TREE
));
30542 = chainon (sel_seq
,
30543 build_tree_list (NULL_TREE
, NULL_TREE
));
30547 = chainon (sel_seq
,
30548 build_tree_list (selector
, NULL_TREE
));
30550 token
= cp_lexer_peek_token (parser
->lexer
);
30554 parens
.require_close (parser
);
30557 /* Build a location of the form:
30560 with caret==start at the @ token, finishing at the close paren. */
30561 location_t combined_loc
30562 = make_location (loc
, loc
,
30563 cp_lexer_previous_token (parser
->lexer
)->location
);
30564 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30565 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30566 protected_set_expr_location (result
, combined_loc
);
30570 /* Parse a list of identifiers.
30572 objc-identifier-list:
30574 objc-identifier-list , identifier
30576 Returns a TREE_LIST of identifier nodes. */
30579 cp_parser_objc_identifier_list (cp_parser
* parser
)
30585 identifier
= cp_parser_identifier (parser
);
30586 if (identifier
== error_mark_node
)
30587 return error_mark_node
;
30589 list
= build_tree_list (NULL_TREE
, identifier
);
30590 sep
= cp_lexer_peek_token (parser
->lexer
);
30592 while (sep
->type
== CPP_COMMA
)
30594 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30595 identifier
= cp_parser_identifier (parser
);
30596 if (identifier
== error_mark_node
)
30599 list
= chainon (list
, build_tree_list (NULL_TREE
,
30601 sep
= cp_lexer_peek_token (parser
->lexer
);
30607 /* Parse an Objective-C alias declaration.
30609 objc-alias-declaration:
30610 @compatibility_alias identifier identifier ;
30612 This function registers the alias mapping with the Objective-C front end.
30613 It returns nothing. */
30616 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30620 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30621 alias
= cp_parser_identifier (parser
);
30622 orig
= cp_parser_identifier (parser
);
30623 objc_declare_alias (alias
, orig
);
30624 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30627 /* Parse an Objective-C class forward-declaration.
30629 objc-class-declaration:
30630 @class objc-identifier-list ;
30632 The function registers the forward declarations with the Objective-C
30633 front end. It returns nothing. */
30636 cp_parser_objc_class_declaration (cp_parser
* parser
)
30638 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30643 id
= cp_parser_identifier (parser
);
30644 if (id
== error_mark_node
)
30647 objc_declare_class (id
);
30649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30650 cp_lexer_consume_token (parser
->lexer
);
30654 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30657 /* Parse a list of Objective-C protocol references.
30659 objc-protocol-refs-opt:
30660 objc-protocol-refs [opt]
30662 objc-protocol-refs:
30663 < objc-identifier-list >
30665 Returns a TREE_LIST of identifiers, if any. */
30668 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30670 tree protorefs
= NULL_TREE
;
30672 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30674 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30675 protorefs
= cp_parser_objc_identifier_list (parser
);
30676 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30682 /* Parse a Objective-C visibility specification. */
30685 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30687 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30689 switch (vis
->keyword
)
30691 case RID_AT_PRIVATE
:
30692 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30694 case RID_AT_PROTECTED
:
30695 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30697 case RID_AT_PUBLIC
:
30698 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30700 case RID_AT_PACKAGE
:
30701 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30707 /* Eat '@private'/'@protected'/'@public'. */
30708 cp_lexer_consume_token (parser
->lexer
);
30711 /* Parse an Objective-C method type. Return 'true' if it is a class
30712 (+) method, and 'false' if it is an instance (-) method. */
30715 cp_parser_objc_method_type (cp_parser
* parser
)
30717 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30723 /* Parse an Objective-C protocol qualifier. */
30726 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30728 tree quals
= NULL_TREE
, node
;
30729 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30731 node
= token
->u
.value
;
30733 while (node
&& identifier_p (node
)
30734 && (node
== ridpointers
[(int) RID_IN
]
30735 || node
== ridpointers
[(int) RID_OUT
]
30736 || node
== ridpointers
[(int) RID_INOUT
]
30737 || node
== ridpointers
[(int) RID_BYCOPY
]
30738 || node
== ridpointers
[(int) RID_BYREF
]
30739 || node
== ridpointers
[(int) RID_ONEWAY
]))
30741 quals
= tree_cons (NULL_TREE
, node
, quals
);
30742 cp_lexer_consume_token (parser
->lexer
);
30743 token
= cp_lexer_peek_token (parser
->lexer
);
30744 node
= token
->u
.value
;
30750 /* Parse an Objective-C typename. */
30753 cp_parser_objc_typename (cp_parser
* parser
)
30755 tree type_name
= NULL_TREE
;
30757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30759 tree proto_quals
, cp_type
= NULL_TREE
;
30761 matching_parens parens
;
30762 parens
.consume_open (parser
); /* Eat '('. */
30763 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30765 /* An ObjC type name may consist of just protocol qualifiers, in which
30766 case the type shall default to 'id'. */
30767 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30769 cp_type
= cp_parser_type_id (parser
);
30771 /* If the type could not be parsed, an error has already
30772 been produced. For error recovery, behave as if it had
30773 not been specified, which will use the default type
30775 if (cp_type
== error_mark_node
)
30777 cp_type
= NULL_TREE
;
30778 /* We need to skip to the closing parenthesis as
30779 cp_parser_type_id() does not seem to do it for
30781 cp_parser_skip_to_closing_parenthesis (parser
,
30782 /*recovering=*/true,
30783 /*or_comma=*/false,
30784 /*consume_paren=*/false);
30788 parens
.require_close (parser
);
30789 type_name
= build_tree_list (proto_quals
, cp_type
);
30795 /* Check to see if TYPE refers to an Objective-C selector name. */
30798 cp_parser_objc_selector_p (enum cpp_ttype type
)
30800 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30801 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30802 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30803 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30804 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30807 /* Parse an Objective-C selector. */
30810 cp_parser_objc_selector (cp_parser
* parser
)
30812 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30814 if (!cp_parser_objc_selector_p (token
->type
))
30816 error_at (token
->location
, "invalid Objective-C++ selector name");
30817 return error_mark_node
;
30820 /* C++ operator names are allowed to appear in ObjC selectors. */
30821 switch (token
->type
)
30823 case CPP_AND_AND
: return get_identifier ("and");
30824 case CPP_AND_EQ
: return get_identifier ("and_eq");
30825 case CPP_AND
: return get_identifier ("bitand");
30826 case CPP_OR
: return get_identifier ("bitor");
30827 case CPP_COMPL
: return get_identifier ("compl");
30828 case CPP_NOT
: return get_identifier ("not");
30829 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30830 case CPP_OR_OR
: return get_identifier ("or");
30831 case CPP_OR_EQ
: return get_identifier ("or_eq");
30832 case CPP_XOR
: return get_identifier ("xor");
30833 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30834 default: return token
->u
.value
;
30838 /* Parse an Objective-C params list. */
30841 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30843 tree params
= NULL_TREE
;
30844 bool maybe_unary_selector_p
= true;
30845 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30847 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30849 tree selector
= NULL_TREE
, type_name
, identifier
;
30850 tree parm_attr
= NULL_TREE
;
30852 if (token
->keyword
== RID_ATTRIBUTE
)
30855 if (token
->type
!= CPP_COLON
)
30856 selector
= cp_parser_objc_selector (parser
);
30858 /* Detect if we have a unary selector. */
30859 if (maybe_unary_selector_p
30860 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30862 params
= selector
; /* Might be followed by attributes. */
30866 maybe_unary_selector_p
= false;
30867 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30869 /* Something went quite wrong. There should be a colon
30870 here, but there is not. Stop parsing parameters. */
30873 type_name
= cp_parser_objc_typename (parser
);
30874 /* New ObjC allows attributes on parameters too. */
30875 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30876 parm_attr
= cp_parser_attributes_opt (parser
);
30877 identifier
= cp_parser_identifier (parser
);
30881 objc_build_keyword_decl (selector
,
30886 token
= cp_lexer_peek_token (parser
->lexer
);
30889 if (params
== NULL_TREE
)
30891 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30892 return error_mark_node
;
30895 /* We allow tail attributes for the method. */
30896 if (token
->keyword
== RID_ATTRIBUTE
)
30898 *attributes
= cp_parser_attributes_opt (parser
);
30899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30900 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30902 cp_parser_error (parser
,
30903 "method attributes must be specified at the end");
30904 return error_mark_node
;
30907 if (params
== NULL_TREE
)
30909 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30910 return error_mark_node
;
30915 /* Parse the non-keyword Objective-C params. */
30918 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30921 tree params
= make_node (TREE_LIST
);
30922 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30923 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30925 while (token
->type
== CPP_COMMA
)
30927 cp_parameter_declarator
*parmdecl
;
30930 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30931 token
= cp_lexer_peek_token (parser
->lexer
);
30933 if (token
->type
== CPP_ELLIPSIS
)
30935 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30937 token
= cp_lexer_peek_token (parser
->lexer
);
30941 /* TODO: parse attributes for tail parameters. */
30942 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
30944 parm
= grokdeclarator (parmdecl
->declarator
,
30945 &parmdecl
->decl_specifiers
,
30946 PARM
, /*initialized=*/0,
30947 /*attrlist=*/NULL
);
30949 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30950 token
= cp_lexer_peek_token (parser
->lexer
);
30953 /* We allow tail attributes for the method. */
30954 if (token
->keyword
== RID_ATTRIBUTE
)
30956 if (*attributes
== NULL_TREE
)
30958 *attributes
= cp_parser_attributes_opt (parser
);
30959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30960 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30964 /* We have an error, but parse the attributes, so that we can
30966 *attributes
= cp_parser_attributes_opt (parser
);
30968 cp_parser_error (parser
,
30969 "method attributes must be specified at the end");
30970 return error_mark_node
;
30976 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30979 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30981 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30983 /* If the next token is `extern' and the following token is a string
30984 literal, then we have a linkage specification. */
30985 if (token
->keyword
== RID_EXTERN
30986 && cp_parser_is_pure_string_literal
30987 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30988 cp_parser_linkage_specification (parser
);
30989 /* Handle #pragma, if any. */
30990 else if (token
->type
== CPP_PRAGMA
)
30991 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30992 /* Allow stray semicolons. */
30993 else if (token
->type
== CPP_SEMICOLON
)
30994 cp_lexer_consume_token (parser
->lexer
);
30995 /* Mark methods as optional or required, when building protocols. */
30996 else if (token
->keyword
== RID_AT_OPTIONAL
)
30998 cp_lexer_consume_token (parser
->lexer
);
30999 objc_set_method_opt (true);
31001 else if (token
->keyword
== RID_AT_REQUIRED
)
31003 cp_lexer_consume_token (parser
->lexer
);
31004 objc_set_method_opt (false);
31006 else if (token
->keyword
== RID_NAMESPACE
)
31007 cp_parser_namespace_definition (parser
);
31008 /* Other stray characters must generate errors. */
31009 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
31011 cp_lexer_consume_token (parser
->lexer
);
31012 error ("stray %qs between Objective-C++ methods",
31013 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
31015 /* Finally, try to parse a block-declaration, or a function-definition. */
31017 cp_parser_block_declaration (parser
, /*statement_p=*/false);
31020 /* Parse a method signature. */
31023 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
31025 tree rettype
, kwdparms
, optparms
;
31026 bool ellipsis
= false;
31027 bool is_class_method
;
31029 is_class_method
= cp_parser_objc_method_type (parser
);
31030 rettype
= cp_parser_objc_typename (parser
);
31031 *attributes
= NULL_TREE
;
31032 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
31033 if (kwdparms
== error_mark_node
)
31034 return error_mark_node
;
31035 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
31036 if (optparms
== error_mark_node
)
31037 return error_mark_node
;
31039 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
31043 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
31046 cp_lexer_save_tokens (parser
->lexer
);
31047 tattr
= cp_parser_attributes_opt (parser
);
31048 gcc_assert (tattr
) ;
31050 /* If the attributes are followed by a method introducer, this is not allowed.
31051 Dump the attributes and flag the situation. */
31052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
31053 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
31056 /* Otherwise, the attributes introduce some interstitial code, possibly so
31057 rewind to allow that check. */
31058 cp_lexer_rollback_tokens (parser
->lexer
);
31062 /* Parse an Objective-C method prototype list. */
31065 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
31067 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31069 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31071 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
31073 tree attributes
, sig
;
31074 bool is_class_method
;
31075 if (token
->type
== CPP_PLUS
)
31076 is_class_method
= true;
31078 is_class_method
= false;
31079 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
31080 if (sig
== error_mark_node
)
31082 cp_parser_skip_to_end_of_block_or_statement (parser
);
31083 token
= cp_lexer_peek_token (parser
->lexer
);
31086 objc_add_method_declaration (is_class_method
, sig
, attributes
);
31087 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31089 else if (token
->keyword
== RID_AT_PROPERTY
)
31090 cp_parser_objc_at_property_declaration (parser
);
31091 else if (token
->keyword
== RID_ATTRIBUTE
31092 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31093 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31095 "prefix attributes are ignored for methods");
31097 /* Allow for interspersed non-ObjC++ code. */
31098 cp_parser_objc_interstitial_code (parser
);
31100 token
= cp_lexer_peek_token (parser
->lexer
);
31103 if (token
->type
!= CPP_EOF
)
31104 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31106 cp_parser_error (parser
, "expected %<@end%>");
31108 objc_finish_interface ();
31111 /* Parse an Objective-C method definition list. */
31114 cp_parser_objc_method_definition_list (cp_parser
* parser
)
31116 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31118 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31122 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
31125 tree sig
, attribute
;
31126 bool is_class_method
;
31127 if (token
->type
== CPP_PLUS
)
31128 is_class_method
= true;
31130 is_class_method
= false;
31131 push_deferring_access_checks (dk_deferred
);
31132 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
31133 if (sig
== error_mark_node
)
31135 cp_parser_skip_to_end_of_block_or_statement (parser
);
31136 token
= cp_lexer_peek_token (parser
->lexer
);
31139 objc_start_method_definition (is_class_method
, sig
, attribute
,
31142 /* For historical reasons, we accept an optional semicolon. */
31143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31144 cp_lexer_consume_token (parser
->lexer
);
31146 ptk
= cp_lexer_peek_token (parser
->lexer
);
31147 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
31148 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
31150 perform_deferred_access_checks (tf_warning_or_error
);
31151 stop_deferring_access_checks ();
31152 meth
= cp_parser_function_definition_after_declarator (parser
,
31154 pop_deferring_access_checks ();
31155 objc_finish_method_definition (meth
);
31158 /* The following case will be removed once @synthesize is
31159 completely implemented. */
31160 else if (token
->keyword
== RID_AT_PROPERTY
)
31161 cp_parser_objc_at_property_declaration (parser
);
31162 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
31163 cp_parser_objc_at_synthesize_declaration (parser
);
31164 else if (token
->keyword
== RID_AT_DYNAMIC
)
31165 cp_parser_objc_at_dynamic_declaration (parser
);
31166 else if (token
->keyword
== RID_ATTRIBUTE
31167 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31168 warning_at (token
->location
, OPT_Wattributes
,
31169 "prefix attributes are ignored for methods");
31171 /* Allow for interspersed non-ObjC++ code. */
31172 cp_parser_objc_interstitial_code (parser
);
31174 token
= cp_lexer_peek_token (parser
->lexer
);
31177 if (token
->type
!= CPP_EOF
)
31178 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31180 cp_parser_error (parser
, "expected %<@end%>");
31182 objc_finish_implementation ();
31185 /* Parse Objective-C ivars. */
31188 cp_parser_objc_class_ivars (cp_parser
* parser
)
31190 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31192 if (token
->type
!= CPP_OPEN_BRACE
)
31193 return; /* No ivars specified. */
31195 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
31196 token
= cp_lexer_peek_token (parser
->lexer
);
31198 while (token
->type
!= CPP_CLOSE_BRACE
31199 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31201 cp_decl_specifier_seq declspecs
;
31202 int decl_class_or_enum_p
;
31203 tree prefix_attributes
;
31205 cp_parser_objc_visibility_spec (parser
);
31207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
31210 cp_parser_decl_specifier_seq (parser
,
31211 CP_PARSER_FLAGS_OPTIONAL
,
31213 &decl_class_or_enum_p
);
31215 /* auto, register, static, extern, mutable. */
31216 if (declspecs
.storage_class
!= sc_none
)
31218 cp_parser_error (parser
, "invalid type for instance variable");
31219 declspecs
.storage_class
= sc_none
;
31222 /* thread_local. */
31223 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31225 cp_parser_error (parser
, "invalid type for instance variable");
31226 declspecs
.locations
[ds_thread
] = 0;
31230 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31232 cp_parser_error (parser
, "invalid type for instance variable");
31233 declspecs
.locations
[ds_typedef
] = 0;
31236 prefix_attributes
= declspecs
.attributes
;
31237 declspecs
.attributes
= NULL_TREE
;
31239 /* Keep going until we hit the `;' at the end of the
31241 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31243 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
31244 cp_declarator
*declarator
= NULL
;
31245 int ctor_dtor_or_conv_p
;
31247 /* Check for a (possibly unnamed) bitfield declaration. */
31248 token
= cp_lexer_peek_token (parser
->lexer
);
31249 if (token
->type
== CPP_COLON
)
31252 if (token
->type
== CPP_NAME
31253 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31256 /* Get the name of the bitfield. */
31257 declarator
= make_id_declarator (NULL_TREE
,
31258 cp_parser_identifier (parser
),
31259 sfk_none
, token
->location
);
31262 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31263 /* Get the width of the bitfield. */
31265 = cp_parser_constant_expression (parser
);
31269 /* Parse the declarator. */
31271 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31272 CP_PARSER_FLAGS_NONE
,
31273 &ctor_dtor_or_conv_p
,
31274 /*parenthesized_p=*/NULL
,
31275 /*member_p=*/false,
31276 /*friend_p=*/false,
31277 /*static_p=*/false);
31280 /* Look for attributes that apply to the ivar. */
31281 attributes
= cp_parser_attributes_opt (parser
);
31282 /* Remember which attributes are prefix attributes and
31284 first_attribute
= attributes
;
31285 /* Combine the attributes. */
31286 attributes
= attr_chainon (prefix_attributes
, attributes
);
31289 /* Create the bitfield declaration. */
31290 decl
= grokbitfield (declarator
, &declspecs
,
31291 width
, NULL_TREE
, attributes
);
31293 decl
= grokfield (declarator
, &declspecs
,
31294 NULL_TREE
, /*init_const_expr_p=*/false,
31295 NULL_TREE
, attributes
);
31297 /* Add the instance variable. */
31298 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
31299 objc_add_instance_variable (decl
);
31301 /* Reset PREFIX_ATTRIBUTES. */
31302 if (attributes
!= error_mark_node
)
31304 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31305 attributes
= TREE_CHAIN (attributes
);
31307 TREE_CHAIN (attributes
) = NULL_TREE
;
31310 token
= cp_lexer_peek_token (parser
->lexer
);
31312 if (token
->type
== CPP_COMMA
)
31314 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31320 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31321 token
= cp_lexer_peek_token (parser
->lexer
);
31324 if (token
->keyword
== RID_AT_END
)
31325 cp_parser_error (parser
, "expected %<}%>");
31327 /* Do not consume the RID_AT_END, so it will be read again as terminating
31328 the @interface of @implementation. */
31329 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31330 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
31332 /* For historical reasons, we accept an optional semicolon. */
31333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31334 cp_lexer_consume_token (parser
->lexer
);
31337 /* Parse an Objective-C protocol declaration. */
31340 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
31342 tree proto
, protorefs
;
31345 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
31346 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31348 tok
= cp_lexer_peek_token (parser
->lexer
);
31349 error_at (tok
->location
, "identifier expected after %<@protocol%>");
31350 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31354 /* See if we have a forward declaration or a definition. */
31355 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
31357 /* Try a forward declaration first. */
31358 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
31364 id
= cp_parser_identifier (parser
);
31365 if (id
== error_mark_node
)
31368 objc_declare_protocol (id
, attributes
);
31370 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31371 cp_lexer_consume_token (parser
->lexer
);
31375 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31378 /* Ok, we got a full-fledged definition (or at least should). */
31381 proto
= cp_parser_identifier (parser
);
31382 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
31383 objc_start_protocol (proto
, protorefs
, attributes
);
31384 cp_parser_objc_method_prototype_list (parser
);
31388 /* Parse an Objective-C superclass or category. */
31391 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
31394 tree
*categ
, bool *is_class_extension
)
31396 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
31398 *super
= *categ
= NULL_TREE
;
31399 *is_class_extension
= false;
31400 if (next
->type
== CPP_COLON
)
31402 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31403 *super
= cp_parser_identifier (parser
);
31405 else if (next
->type
== CPP_OPEN_PAREN
)
31407 matching_parens parens
;
31408 parens
.consume_open (parser
); /* Eat '('. */
31410 /* If there is no category name, and this is an @interface, we
31411 have a class extension. */
31412 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31414 *categ
= NULL_TREE
;
31415 *is_class_extension
= true;
31418 *categ
= cp_parser_identifier (parser
);
31420 parens
.require_close (parser
);
31424 /* Parse an Objective-C class interface. */
31427 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
31429 tree name
, super
, categ
, protos
;
31430 bool is_class_extension
;
31432 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
31433 name
= cp_parser_identifier (parser
);
31434 if (name
== error_mark_node
)
31436 /* It's hard to recover because even if valid @interface stuff
31437 is to follow, we can't compile it (or validate it) if we
31438 don't even know which class it refers to. Let's assume this
31439 was a stray '@interface' token in the stream and skip it.
31443 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
31444 &is_class_extension
);
31445 protos
= cp_parser_objc_protocol_refs_opt (parser
);
31447 /* We have either a class or a category on our hands. */
31448 if (categ
|| is_class_extension
)
31449 objc_start_category_interface (name
, categ
, protos
, attributes
);
31452 objc_start_class_interface (name
, super
, protos
, attributes
);
31453 /* Handle instance variable declarations, if any. */
31454 cp_parser_objc_class_ivars (parser
);
31455 objc_continue_interface ();
31458 cp_parser_objc_method_prototype_list (parser
);
31461 /* Parse an Objective-C class implementation. */
31464 cp_parser_objc_class_implementation (cp_parser
* parser
)
31466 tree name
, super
, categ
;
31467 bool is_class_extension
;
31469 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
31470 name
= cp_parser_identifier (parser
);
31471 if (name
== error_mark_node
)
31473 /* It's hard to recover because even if valid @implementation
31474 stuff is to follow, we can't compile it (or validate it) if
31475 we don't even know which class it refers to. Let's assume
31476 this was a stray '@implementation' token in the stream and
31481 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31482 &is_class_extension
);
31484 /* We have either a class or a category on our hands. */
31486 objc_start_category_implementation (name
, categ
);
31489 objc_start_class_implementation (name
, super
);
31490 /* Handle instance variable declarations, if any. */
31491 cp_parser_objc_class_ivars (parser
);
31492 objc_continue_implementation ();
31495 cp_parser_objc_method_definition_list (parser
);
31498 /* Consume the @end token and finish off the implementation. */
31501 cp_parser_objc_end_implementation (cp_parser
* parser
)
31503 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31504 objc_finish_implementation ();
31507 /* Parse an Objective-C declaration. */
31510 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31512 /* Try to figure out what kind of declaration is present. */
31513 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31516 switch (kwd
->keyword
)
31521 error_at (kwd
->location
, "attributes may not be specified before"
31522 " the %<@%D%> Objective-C++ keyword",
31526 case RID_AT_IMPLEMENTATION
:
31527 warning_at (kwd
->location
, OPT_Wattributes
,
31528 "prefix attributes are ignored before %<@%D%>",
31535 switch (kwd
->keyword
)
31538 cp_parser_objc_alias_declaration (parser
);
31541 cp_parser_objc_class_declaration (parser
);
31543 case RID_AT_PROTOCOL
:
31544 cp_parser_objc_protocol_declaration (parser
, attributes
);
31546 case RID_AT_INTERFACE
:
31547 cp_parser_objc_class_interface (parser
, attributes
);
31549 case RID_AT_IMPLEMENTATION
:
31550 cp_parser_objc_class_implementation (parser
);
31553 cp_parser_objc_end_implementation (parser
);
31556 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31558 cp_parser_skip_to_end_of_block_or_statement (parser
);
31562 /* Parse an Objective-C try-catch-finally statement.
31564 objc-try-catch-finally-stmt:
31565 @try compound-statement objc-catch-clause-seq [opt]
31566 objc-finally-clause [opt]
31568 objc-catch-clause-seq:
31569 objc-catch-clause objc-catch-clause-seq [opt]
31572 @catch ( objc-exception-declaration ) compound-statement
31574 objc-finally-clause:
31575 @finally compound-statement
31577 objc-exception-declaration:
31578 parameter-declaration
31581 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31585 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31586 for C. Keep them in sync. */
31589 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31591 location_t location
;
31594 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31595 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31596 objc_maybe_warn_exceptions (location
);
31597 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31598 node, lest it get absorbed into the surrounding block. */
31599 stmt
= push_stmt_list ();
31600 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31601 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31603 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31605 cp_parameter_declarator
*parm
;
31606 tree parameter_declaration
= error_mark_node
;
31607 bool seen_open_paren
= false;
31608 matching_parens parens
;
31610 cp_lexer_consume_token (parser
->lexer
);
31611 if (parens
.require_open (parser
))
31612 seen_open_paren
= true;
31613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31615 /* We have "@catch (...)" (where the '...' are literally
31616 what is in the code). Skip the '...'.
31617 parameter_declaration is set to NULL_TREE, and
31618 objc_being_catch_clauses() knows that that means
31620 cp_lexer_consume_token (parser
->lexer
);
31621 parameter_declaration
= NULL_TREE
;
31625 /* We have "@catch (NSException *exception)" or something
31626 like that. Parse the parameter declaration. */
31627 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
31630 parameter_declaration
= error_mark_node
;
31632 parameter_declaration
= grokdeclarator (parm
->declarator
,
31633 &parm
->decl_specifiers
,
31634 PARM
, /*initialized=*/0,
31635 /*attrlist=*/NULL
);
31637 if (seen_open_paren
)
31638 parens
.require_close (parser
);
31641 /* If there was no open parenthesis, we are recovering from
31642 an error, and we are trying to figure out what mistake
31643 the user has made. */
31645 /* If there is an immediate closing parenthesis, the user
31646 probably forgot the opening one (ie, they typed "@catch
31647 NSException *e)". Parse the closing parenthesis and keep
31649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31650 cp_lexer_consume_token (parser
->lexer
);
31652 /* If these is no immediate closing parenthesis, the user
31653 probably doesn't know that parenthesis are required at
31654 all (ie, they typed "@catch NSException *e"). So, just
31655 forget about the closing parenthesis and keep going. */
31657 objc_begin_catch_clause (parameter_declaration
);
31658 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31659 objc_finish_catch_clause ();
31661 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31663 cp_lexer_consume_token (parser
->lexer
);
31664 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31665 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31666 node, lest it get absorbed into the surrounding block. */
31667 stmt
= push_stmt_list ();
31668 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31669 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31672 return objc_finish_try_stmt ();
31675 /* Parse an Objective-C synchronized statement.
31677 objc-synchronized-stmt:
31678 @synchronized ( expression ) compound-statement
31680 Returns NULL_TREE. */
31683 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31685 location_t location
;
31688 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31690 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31691 objc_maybe_warn_exceptions (location
);
31692 matching_parens parens
;
31693 parens
.require_open (parser
);
31694 lock
= cp_parser_expression (parser
);
31695 parens
.require_close (parser
);
31697 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31698 node, lest it get absorbed into the surrounding block. */
31699 stmt
= push_stmt_list ();
31700 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31702 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31705 /* Parse an Objective-C throw statement.
31708 @throw assignment-expression [opt] ;
31710 Returns a constructed '@throw' statement. */
31713 cp_parser_objc_throw_statement (cp_parser
*parser
)
31715 tree expr
= NULL_TREE
;
31716 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31718 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31720 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31721 expr
= cp_parser_expression (parser
);
31723 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31725 return objc_build_throw_stmt (loc
, expr
);
31728 /* Parse an Objective-C statement. */
31731 cp_parser_objc_statement (cp_parser
* parser
)
31733 /* Try to figure out what kind of declaration is present. */
31734 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31736 switch (kwd
->keyword
)
31739 return cp_parser_objc_try_catch_finally_statement (parser
);
31740 case RID_AT_SYNCHRONIZED
:
31741 return cp_parser_objc_synchronized_statement (parser
);
31743 return cp_parser_objc_throw_statement (parser
);
31745 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31747 cp_parser_skip_to_end_of_block_or_statement (parser
);
31750 return error_mark_node
;
31753 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31754 look ahead to see if an objc keyword follows the attributes. This
31755 is to detect the use of prefix attributes on ObjC @interface and
31759 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31761 cp_lexer_save_tokens (parser
->lexer
);
31762 *attrib
= cp_parser_attributes_opt (parser
);
31763 gcc_assert (*attrib
);
31764 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31766 cp_lexer_commit_tokens (parser
->lexer
);
31769 cp_lexer_rollback_tokens (parser
->lexer
);
31773 /* This routine is a minimal replacement for
31774 c_parser_struct_declaration () used when parsing the list of
31775 types/names or ObjC++ properties. For example, when parsing the
31778 @property (readonly) int a, b, c;
31780 this function is responsible for parsing "int a, int b, int c" and
31781 returning the declarations as CHAIN of DECLs.
31783 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31784 similar parsing. */
31786 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31788 tree decls
= NULL_TREE
;
31789 cp_decl_specifier_seq declspecs
;
31790 int decl_class_or_enum_p
;
31791 tree prefix_attributes
;
31793 cp_parser_decl_specifier_seq (parser
,
31794 CP_PARSER_FLAGS_NONE
,
31796 &decl_class_or_enum_p
);
31798 if (declspecs
.type
== error_mark_node
)
31799 return error_mark_node
;
31801 /* auto, register, static, extern, mutable. */
31802 if (declspecs
.storage_class
!= sc_none
)
31804 cp_parser_error (parser
, "invalid type for property");
31805 declspecs
.storage_class
= sc_none
;
31808 /* thread_local. */
31809 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31811 cp_parser_error (parser
, "invalid type for property");
31812 declspecs
.locations
[ds_thread
] = 0;
31816 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31818 cp_parser_error (parser
, "invalid type for property");
31819 declspecs
.locations
[ds_typedef
] = 0;
31822 prefix_attributes
= declspecs
.attributes
;
31823 declspecs
.attributes
= NULL_TREE
;
31825 /* Keep going until we hit the `;' at the end of the declaration. */
31826 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31828 tree attributes
, first_attribute
, decl
;
31829 cp_declarator
*declarator
;
31832 /* Parse the declarator. */
31833 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31834 CP_PARSER_FLAGS_NONE
,
31835 NULL
, NULL
, false, false, false);
31837 /* Look for attributes that apply to the ivar. */
31838 attributes
= cp_parser_attributes_opt (parser
);
31839 /* Remember which attributes are prefix attributes and
31841 first_attribute
= attributes
;
31842 /* Combine the attributes. */
31843 attributes
= attr_chainon (prefix_attributes
, attributes
);
31845 decl
= grokfield (declarator
, &declspecs
,
31846 NULL_TREE
, /*init_const_expr_p=*/false,
31847 NULL_TREE
, attributes
);
31849 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31850 return error_mark_node
;
31852 /* Reset PREFIX_ATTRIBUTES. */
31853 if (attributes
!= error_mark_node
)
31855 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31856 attributes
= TREE_CHAIN (attributes
);
31858 TREE_CHAIN (attributes
) = NULL_TREE
;
31861 DECL_CHAIN (decl
) = decls
;
31864 token
= cp_lexer_peek_token (parser
->lexer
);
31865 if (token
->type
== CPP_COMMA
)
31867 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31876 /* Parse an Objective-C @property declaration. The syntax is:
31878 objc-property-declaration:
31879 '@property' objc-property-attributes[opt] struct-declaration ;
31881 objc-property-attributes:
31882 '(' objc-property-attribute-list ')'
31884 objc-property-attribute-list:
31885 objc-property-attribute
31886 objc-property-attribute-list, objc-property-attribute
31888 objc-property-attribute
31889 'getter' = identifier
31890 'setter' = identifier
31899 @property NSString *name;
31900 @property (readonly) id object;
31901 @property (retain, nonatomic, getter=getTheName) id name;
31902 @property int a, b, c;
31904 PS: This function is identical to
31905 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31907 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31909 /* The following variables hold the attributes of the properties as
31910 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31911 seen. When we see an attribute, we set them to 'true' (if they
31912 are boolean properties) or to the identifier (if they have an
31913 argument, ie, for getter and setter). Note that here we only
31914 parse the list of attributes, check the syntax and accumulate the
31915 attributes that we find. objc_add_property_declaration() will
31916 then process the information. */
31917 bool property_assign
= false;
31918 bool property_copy
= false;
31919 tree property_getter_ident
= NULL_TREE
;
31920 bool property_nonatomic
= false;
31921 bool property_readonly
= false;
31922 bool property_readwrite
= false;
31923 bool property_retain
= false;
31924 tree property_setter_ident
= NULL_TREE
;
31926 /* 'properties' is the list of properties that we read. Usually a
31927 single one, but maybe more (eg, in "@property int a, b, c;" there
31932 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31934 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31936 /* Parse the optional attribute list... */
31937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31940 matching_parens parens
;
31941 parens
.consume_open (parser
);
31945 bool syntax_error
= false;
31946 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31949 if (token
->type
!= CPP_NAME
)
31951 cp_parser_error (parser
, "expected identifier");
31954 keyword
= C_RID_CODE (token
->u
.value
);
31955 cp_lexer_consume_token (parser
->lexer
);
31958 case RID_ASSIGN
: property_assign
= true; break;
31959 case RID_COPY
: property_copy
= true; break;
31960 case RID_NONATOMIC
: property_nonatomic
= true; break;
31961 case RID_READONLY
: property_readonly
= true; break;
31962 case RID_READWRITE
: property_readwrite
= true; break;
31963 case RID_RETAIN
: property_retain
= true; break;
31967 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31969 if (keyword
== RID_GETTER
)
31970 cp_parser_error (parser
,
31971 "missing %<=%> (after %<getter%> attribute)");
31973 cp_parser_error (parser
,
31974 "missing %<=%> (after %<setter%> attribute)");
31975 syntax_error
= true;
31978 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31979 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31981 cp_parser_error (parser
, "expected identifier");
31982 syntax_error
= true;
31985 if (keyword
== RID_SETTER
)
31987 if (property_setter_ident
!= NULL_TREE
)
31989 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31990 cp_lexer_consume_token (parser
->lexer
);
31993 property_setter_ident
= cp_parser_objc_selector (parser
);
31994 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31995 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31997 cp_lexer_consume_token (parser
->lexer
);
32001 if (property_getter_ident
!= NULL_TREE
)
32003 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
32004 cp_lexer_consume_token (parser
->lexer
);
32007 property_getter_ident
= cp_parser_objc_selector (parser
);
32011 cp_parser_error (parser
, "unknown property attribute");
32012 syntax_error
= true;
32019 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32020 cp_lexer_consume_token (parser
->lexer
);
32025 /* FIXME: "@property (setter, assign);" will generate a spurious
32026 "error: expected ‘)’ before ‘,’ token". This is because
32027 cp_parser_require, unlike the C counterpart, will produce an
32028 error even if we are in error recovery. */
32029 if (!parens
.require_close (parser
))
32031 cp_parser_skip_to_closing_parenthesis (parser
,
32032 /*recovering=*/true,
32033 /*or_comma=*/false,
32034 /*consume_paren=*/true);
32038 /* ... and the property declaration(s). */
32039 properties
= cp_parser_objc_struct_declaration (parser
);
32041 if (properties
== error_mark_node
)
32043 cp_parser_skip_to_end_of_statement (parser
);
32044 /* If the next token is now a `;', consume it. */
32045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32046 cp_lexer_consume_token (parser
->lexer
);
32050 if (properties
== NULL_TREE
)
32051 cp_parser_error (parser
, "expected identifier");
32054 /* Comma-separated properties are chained together in
32055 reverse order; add them one by one. */
32056 properties
= nreverse (properties
);
32058 for (; properties
; properties
= TREE_CHAIN (properties
))
32059 objc_add_property_declaration (loc
, copy_node (properties
),
32060 property_readonly
, property_readwrite
,
32061 property_assign
, property_retain
,
32062 property_copy
, property_nonatomic
,
32063 property_getter_ident
, property_setter_ident
);
32066 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32069 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32071 objc-synthesize-declaration:
32072 @synthesize objc-synthesize-identifier-list ;
32074 objc-synthesize-identifier-list:
32075 objc-synthesize-identifier
32076 objc-synthesize-identifier-list, objc-synthesize-identifier
32078 objc-synthesize-identifier
32080 identifier = identifier
32083 @synthesize MyProperty;
32084 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32086 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32087 for C. Keep them in sync.
32090 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
32092 tree list
= NULL_TREE
;
32094 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32096 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
32099 tree property
, ivar
;
32100 property
= cp_parser_identifier (parser
);
32101 if (property
== error_mark_node
)
32103 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
32108 cp_lexer_consume_token (parser
->lexer
);
32109 ivar
= cp_parser_identifier (parser
);
32110 if (ivar
== error_mark_node
)
32112 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32118 list
= chainon (list
, build_tree_list (ivar
, property
));
32119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32120 cp_lexer_consume_token (parser
->lexer
);
32124 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32125 objc_add_synthesize_declaration (loc
, list
);
32128 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32130 objc-dynamic-declaration:
32131 @dynamic identifier-list ;
32134 @dynamic MyProperty;
32135 @dynamic MyProperty, AnotherProperty;
32137 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32138 for C. Keep them in sync.
32141 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
32143 tree list
= NULL_TREE
;
32145 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32147 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
32151 property
= cp_parser_identifier (parser
);
32152 if (property
== error_mark_node
)
32154 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32157 list
= chainon (list
, build_tree_list (NULL
, property
));
32158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32159 cp_lexer_consume_token (parser
->lexer
);
32163 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32164 objc_add_dynamic_declaration (loc
, list
);
32168 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32170 /* Returns name of the next clause.
32171 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32172 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32173 returned and the token is consumed. */
32175 static pragma_omp_clause
32176 cp_parser_omp_clause_name (cp_parser
*parser
)
32178 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
32180 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32181 result
= PRAGMA_OACC_CLAUSE_AUTO
;
32182 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
32183 result
= PRAGMA_OMP_CLAUSE_IF
;
32184 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
32185 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
32186 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
32187 result
= PRAGMA_OACC_CLAUSE_DELETE
;
32188 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
32189 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
32190 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
32191 result
= PRAGMA_OMP_CLAUSE_FOR
;
32192 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32194 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32195 const char *p
= IDENTIFIER_POINTER (id
);
32200 if (!strcmp ("aligned", p
))
32201 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
32202 else if (!strcmp ("async", p
))
32203 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
32206 if (!strcmp ("collapse", p
))
32207 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
32208 else if (!strcmp ("copy", p
))
32209 result
= PRAGMA_OACC_CLAUSE_COPY
;
32210 else if (!strcmp ("copyin", p
))
32211 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
32212 else if (!strcmp ("copyout", p
))
32213 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32214 else if (!strcmp ("copyprivate", p
))
32215 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
32216 else if (!strcmp ("create", p
))
32217 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32220 if (!strcmp ("defaultmap", p
))
32221 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
32222 else if (!strcmp ("depend", p
))
32223 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
32224 else if (!strcmp ("device", p
))
32225 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
32226 else if (!strcmp ("deviceptr", p
))
32227 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
32228 else if (!strcmp ("device_resident", p
))
32229 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
32230 else if (!strcmp ("dist_schedule", p
))
32231 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
32234 if (!strcmp ("final", p
))
32235 result
= PRAGMA_OMP_CLAUSE_FINAL
;
32236 else if (!strcmp ("finalize", p
))
32237 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
32238 else if (!strcmp ("firstprivate", p
))
32239 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
32240 else if (!strcmp ("from", p
))
32241 result
= PRAGMA_OMP_CLAUSE_FROM
;
32244 if (!strcmp ("gang", p
))
32245 result
= PRAGMA_OACC_CLAUSE_GANG
;
32246 else if (!strcmp ("grainsize", p
))
32247 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
32250 if (!strcmp ("hint", p
))
32251 result
= PRAGMA_OMP_CLAUSE_HINT
;
32252 else if (!strcmp ("host", p
))
32253 result
= PRAGMA_OACC_CLAUSE_HOST
;
32256 if (!strcmp ("if_present", p
))
32257 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
32258 else if (!strcmp ("in_reduction", p
))
32259 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
32260 else if (!strcmp ("inbranch", p
))
32261 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
32262 else if (!strcmp ("independent", p
))
32263 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
32264 else if (!strcmp ("is_device_ptr", p
))
32265 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
32268 if (!strcmp ("lastprivate", p
))
32269 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
32270 else if (!strcmp ("linear", p
))
32271 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
32272 else if (!strcmp ("link", p
))
32273 result
= PRAGMA_OMP_CLAUSE_LINK
;
32276 if (!strcmp ("map", p
))
32277 result
= PRAGMA_OMP_CLAUSE_MAP
;
32278 else if (!strcmp ("mergeable", p
))
32279 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
32282 if (!strcmp ("nogroup", p
))
32283 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
32284 else if (!strcmp ("nontemporal", p
))
32285 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
32286 else if (!strcmp ("notinbranch", p
))
32287 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
32288 else if (!strcmp ("nowait", p
))
32289 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
32290 else if (!strcmp ("num_gangs", p
))
32291 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
32292 else if (!strcmp ("num_tasks", p
))
32293 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
32294 else if (!strcmp ("num_teams", p
))
32295 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
32296 else if (!strcmp ("num_threads", p
))
32297 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
32298 else if (!strcmp ("num_workers", p
))
32299 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
32302 if (!strcmp ("ordered", p
))
32303 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
32306 if (!strcmp ("parallel", p
))
32307 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
32308 else if (!strcmp ("present", p
))
32309 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
32310 else if (!strcmp ("present_or_copy", p
)
32311 || !strcmp ("pcopy", p
))
32312 result
= PRAGMA_OACC_CLAUSE_COPY
;
32313 else if (!strcmp ("present_or_copyin", p
)
32314 || !strcmp ("pcopyin", p
))
32315 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
32316 else if (!strcmp ("present_or_copyout", p
)
32317 || !strcmp ("pcopyout", p
))
32318 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32319 else if (!strcmp ("present_or_create", p
)
32320 || !strcmp ("pcreate", p
))
32321 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32322 else if (!strcmp ("priority", p
))
32323 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
32324 else if (!strcmp ("proc_bind", p
))
32325 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
32328 if (!strcmp ("reduction", p
))
32329 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
32332 if (!strcmp ("safelen", p
))
32333 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
32334 else if (!strcmp ("schedule", p
))
32335 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
32336 else if (!strcmp ("sections", p
))
32337 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
32338 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
32339 result
= PRAGMA_OACC_CLAUSE_HOST
;
32340 else if (!strcmp ("seq", p
))
32341 result
= PRAGMA_OACC_CLAUSE_SEQ
;
32342 else if (!strcmp ("shared", p
))
32343 result
= PRAGMA_OMP_CLAUSE_SHARED
;
32344 else if (!strcmp ("simd", p
))
32345 result
= PRAGMA_OMP_CLAUSE_SIMD
;
32346 else if (!strcmp ("simdlen", p
))
32347 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
32350 if (!strcmp ("task_reduction", p
))
32351 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
32352 else if (!strcmp ("taskgroup", p
))
32353 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
32354 else if (!strcmp ("thread_limit", p
))
32355 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
32356 else if (!strcmp ("threads", p
))
32357 result
= PRAGMA_OMP_CLAUSE_THREADS
;
32358 else if (!strcmp ("tile", p
))
32359 result
= PRAGMA_OACC_CLAUSE_TILE
;
32360 else if (!strcmp ("to", p
))
32361 result
= PRAGMA_OMP_CLAUSE_TO
;
32364 if (!strcmp ("uniform", p
))
32365 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
32366 else if (!strcmp ("untied", p
))
32367 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
32368 else if (!strcmp ("use_device", p
))
32369 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
32370 else if (!strcmp ("use_device_ptr", p
))
32371 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
32374 if (!strcmp ("vector", p
))
32375 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
32376 else if (!strcmp ("vector_length", p
))
32377 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
32380 if (!strcmp ("wait", p
))
32381 result
= PRAGMA_OACC_CLAUSE_WAIT
;
32382 else if (!strcmp ("worker", p
))
32383 result
= PRAGMA_OACC_CLAUSE_WORKER
;
32388 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
32389 cp_lexer_consume_token (parser
->lexer
);
32394 /* Validate that a clause of the given type does not already exist. */
32397 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
32398 const char *name
, location_t location
)
32402 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32403 if (OMP_CLAUSE_CODE (c
) == code
)
32405 error_at (location
, "too many %qs clauses", name
);
32413 variable-list , identifier
32415 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32416 colon). An opening parenthesis will have been consumed by the caller.
32418 If KIND is nonzero, create the appropriate node and install the decl
32419 in OMP_CLAUSE_DECL and add the node to the head of the list.
32421 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32422 return the list created.
32424 COLON can be NULL if only closing parenthesis should end the list,
32425 or pointer to bool which will receive false if the list is terminated
32426 by closing parenthesis or true if the list is terminated by colon. */
32429 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
32430 tree list
, bool *colon
)
32433 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32436 parser
->colon_corrects_to_scope_p
= false;
32443 if (kind
== OMP_CLAUSE_DEPEND
)
32444 cp_parser_parse_tentatively (parser
);
32445 token
= cp_lexer_peek_token (parser
->lexer
);
32447 && current_class_ptr
32448 && cp_parser_is_keyword (token
, RID_THIS
))
32450 decl
= finish_this_expr ();
32451 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
32452 || CONVERT_EXPR_P (decl
))
32453 decl
= TREE_OPERAND (decl
, 0);
32454 cp_lexer_consume_token (parser
->lexer
);
32458 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32459 /*check_dependency_p=*/true,
32460 /*template_p=*/NULL
,
32461 /*declarator_p=*/false,
32462 /*optional_p=*/false);
32463 if (name
== error_mark_node
)
32465 if (kind
== OMP_CLAUSE_DEPEND
32466 && cp_parser_simulate_error (parser
))
32467 goto depend_lvalue
;
32471 if (identifier_p (name
))
32472 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32475 if (decl
== error_mark_node
)
32477 if (kind
== OMP_CLAUSE_DEPEND
32478 && cp_parser_simulate_error (parser
))
32479 goto depend_lvalue
;
32480 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32484 if (decl
== error_mark_node
)
32486 else if (kind
!= 0)
32490 case OMP_CLAUSE__CACHE_
:
32491 /* The OpenACC cache directive explicitly only allows "array
32492 elements or subarrays". */
32493 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32495 error_at (token
->location
, "expected %<[%>");
32496 decl
= error_mark_node
;
32500 case OMP_CLAUSE_MAP
:
32501 case OMP_CLAUSE_FROM
:
32502 case OMP_CLAUSE_TO
:
32503 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32506 = cp_lexer_peek_token (parser
->lexer
)->location
;
32507 cp_id_kind idk
= CP_ID_KIND_NONE
;
32508 cp_lexer_consume_token (parser
->lexer
);
32509 decl
= convert_from_reference (decl
);
32511 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32516 case OMP_CLAUSE_DEPEND
:
32517 case OMP_CLAUSE_REDUCTION
:
32518 case OMP_CLAUSE_IN_REDUCTION
:
32519 case OMP_CLAUSE_TASK_REDUCTION
:
32520 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32522 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32524 parser
->colon_corrects_to_scope_p
= false;
32525 cp_lexer_consume_token (parser
->lexer
);
32526 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32527 low_bound
= cp_parser_expression (parser
);
32529 parser
->colon_corrects_to_scope_p
32530 = saved_colon_corrects_to_scope_p
;
32531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32532 length
= integer_one_node
;
32535 /* Look for `:'. */
32536 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32538 if (kind
== OMP_CLAUSE_DEPEND
32539 && cp_parser_simulate_error (parser
))
32540 goto depend_lvalue
;
32543 if (kind
== OMP_CLAUSE_DEPEND
)
32544 cp_parser_commit_to_tentative_parse (parser
);
32545 if (!cp_lexer_next_token_is (parser
->lexer
,
32547 length
= cp_parser_expression (parser
);
32549 /* Look for the closing `]'. */
32550 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32553 if (kind
== OMP_CLAUSE_DEPEND
32554 && cp_parser_simulate_error (parser
))
32555 goto depend_lvalue
;
32559 decl
= tree_cons (low_bound
, length
, decl
);
32566 if (kind
== OMP_CLAUSE_DEPEND
)
32568 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32569 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32570 && cp_parser_simulate_error (parser
))
32573 cp_parser_abort_tentative_parse (parser
);
32574 decl
= cp_parser_assignment_expression (parser
, NULL
,
32578 cp_parser_parse_definitely (parser
);
32581 tree u
= build_omp_clause (token
->location
, kind
);
32582 OMP_CLAUSE_DECL (u
) = decl
;
32583 OMP_CLAUSE_CHAIN (u
) = list
;
32587 list
= tree_cons (decl
, NULL_TREE
, list
);
32590 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32592 cp_lexer_consume_token (parser
->lexer
);
32596 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32598 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32601 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32605 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32609 /* Try to resync to an unnested comma. Copied from
32610 cp_parser_parenthesized_expression_list. */
32613 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32614 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32615 /*recovering=*/true,
32617 /*consume_paren=*/true);
32625 /* Similarly, but expect leading and trailing parenthesis. This is a very
32626 common case for omp clauses. */
32629 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32631 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32632 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32637 copy ( variable-list )
32638 copyin ( variable-list )
32639 copyout ( variable-list )
32640 create ( variable-list )
32641 delete ( variable-list )
32642 present ( variable-list ) */
32645 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32648 enum gomp_map_kind kind
;
32651 case PRAGMA_OACC_CLAUSE_COPY
:
32652 kind
= GOMP_MAP_TOFROM
;
32654 case PRAGMA_OACC_CLAUSE_COPYIN
:
32655 kind
= GOMP_MAP_TO
;
32657 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32658 kind
= GOMP_MAP_FROM
;
32660 case PRAGMA_OACC_CLAUSE_CREATE
:
32661 kind
= GOMP_MAP_ALLOC
;
32663 case PRAGMA_OACC_CLAUSE_DELETE
:
32664 kind
= GOMP_MAP_RELEASE
;
32666 case PRAGMA_OACC_CLAUSE_DEVICE
:
32667 kind
= GOMP_MAP_FORCE_TO
;
32669 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32670 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32672 case PRAGMA_OACC_CLAUSE_HOST
:
32673 kind
= GOMP_MAP_FORCE_FROM
;
32675 case PRAGMA_OACC_CLAUSE_LINK
:
32676 kind
= GOMP_MAP_LINK
;
32678 case PRAGMA_OACC_CLAUSE_PRESENT
:
32679 kind
= GOMP_MAP_FORCE_PRESENT
;
32682 gcc_unreachable ();
32685 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32687 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32688 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32694 deviceptr ( variable-list ) */
32697 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32699 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32702 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32703 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32704 variable-list must only allow for pointer variables. */
32705 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32706 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32708 tree v
= TREE_PURPOSE (t
);
32709 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32710 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32711 OMP_CLAUSE_DECL (u
) = v
;
32712 OMP_CLAUSE_CHAIN (u
) = list
;
32727 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
32730 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
32732 tree c
= build_omp_clause (loc
, code
);
32733 OMP_CLAUSE_CHAIN (c
) = list
;
32739 num_gangs ( expression )
32740 num_workers ( expression )
32741 vector_length ( expression ) */
32744 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32745 const char *str
, tree list
)
32747 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32749 matching_parens parens
;
32750 if (!parens
.require_open (parser
))
32753 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32755 if (t
== error_mark_node
32756 || !parens
.require_close (parser
))
32758 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32759 /*or_comma=*/false,
32760 /*consume_paren=*/true);
32764 check_no_duplicate_clause (list
, code
, str
, loc
);
32766 tree c
= build_omp_clause (loc
, code
);
32767 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32768 OMP_CLAUSE_CHAIN (c
) = list
;
32774 gang [( gang-arg-list )]
32775 worker [( [num:] int-expr )]
32776 vector [( [length:] int-expr )]
32778 where gang-arg is one of:
32783 and size-expr may be:
32790 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
32791 omp_clause_code kind
,
32792 const char *str
, tree list
)
32794 const char *id
= "num";
32795 cp_lexer
*lexer
= parser
->lexer
;
32796 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32798 if (kind
== OMP_CLAUSE_VECTOR
)
32801 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32803 matching_parens parens
;
32804 parens
.consume_open (parser
);
32808 cp_token
*next
= cp_lexer_peek_token (lexer
);
32811 /* Gang static argument. */
32812 if (kind
== OMP_CLAUSE_GANG
32813 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32815 cp_lexer_consume_token (lexer
);
32817 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32818 goto cleanup_error
;
32821 if (ops
[idx
] != NULL
)
32823 cp_parser_error (parser
, "too many %<static%> arguments");
32824 goto cleanup_error
;
32827 /* Check for the '*' argument. */
32828 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32829 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32830 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32833 cp_lexer_consume_token (lexer
);
32834 ops
[idx
] = integer_minus_one_node
;
32836 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32838 cp_lexer_consume_token (lexer
);
32844 /* Worker num: argument and vector length: arguments. */
32845 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32846 && id_equal (next
->u
.value
, id
)
32847 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32849 cp_lexer_consume_token (lexer
); /* id */
32850 cp_lexer_consume_token (lexer
); /* ':' */
32853 /* Now collect the actual argument. */
32854 if (ops
[idx
] != NULL_TREE
)
32856 cp_parser_error (parser
, "unexpected argument");
32857 goto cleanup_error
;
32860 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32862 if (expr
== error_mark_node
)
32863 goto cleanup_error
;
32865 mark_exp_read (expr
);
32868 if (kind
== OMP_CLAUSE_GANG
32869 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32871 cp_lexer_consume_token (lexer
);
32878 if (!parens
.require_close (parser
))
32879 goto cleanup_error
;
32882 check_no_duplicate_clause (list
, kind
, str
, loc
);
32884 c
= build_omp_clause (loc
, kind
);
32887 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32889 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32890 OMP_CLAUSE_CHAIN (c
) = list
;
32895 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32900 tile ( size-expr-list ) */
32903 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32905 tree c
, expr
= error_mark_node
;
32906 tree tile
= NULL_TREE
;
32908 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32909 so, but the spec authors never considered such a case and have
32910 differing opinions on what it might mean, including 'not
32912 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32913 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32916 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32921 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32925 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32926 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32928 cp_lexer_consume_token (parser
->lexer
);
32929 expr
= integer_zero_node
;
32932 expr
= cp_parser_constant_expression (parser
);
32934 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32936 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32938 /* Consume the trailing ')'. */
32939 cp_lexer_consume_token (parser
->lexer
);
32941 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32942 tile
= nreverse (tile
);
32943 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32944 OMP_CLAUSE_CHAIN (c
) = list
;
32949 Parse wait clause or directive parameters. */
32952 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32954 vec
<tree
, va_gc
> *args
;
32957 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32959 /*allow_expansion_p=*/true,
32960 /*non_constant_p=*/NULL
);
32962 if (args
== NULL
|| args
->length () == 0)
32966 cp_parser_error (parser
, "expected integer expression list");
32967 release_tree_vector (args
);
32972 args_tree
= build_tree_list_vec (args
);
32974 release_tree_vector (args
);
32976 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32978 tree targ
= TREE_VALUE (t
);
32980 if (targ
!= error_mark_node
)
32982 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32983 error ("%<wait%> expression must be integral");
32986 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32988 targ
= mark_rvalue_use (targ
);
32989 OMP_CLAUSE_DECL (c
) = targ
;
32990 OMP_CLAUSE_CHAIN (c
) = list
;
33000 wait [( int-expr-list )] */
33003 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
33005 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33007 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33008 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
33011 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
33013 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33014 OMP_CLAUSE_CHAIN (c
) = list
;
33022 collapse ( constant-expression ) */
33025 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
33031 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33032 matching_parens parens
;
33033 if (!parens
.require_open (parser
))
33036 num
= cp_parser_constant_expression (parser
);
33038 if (!parens
.require_close (parser
))
33039 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33040 /*or_comma=*/false,
33041 /*consume_paren=*/true);
33043 if (num
== error_mark_node
)
33045 num
= fold_non_dependent_expr (num
);
33046 if (!tree_fits_shwi_p (num
)
33047 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33048 || (n
= tree_to_shwi (num
)) <= 0
33051 error_at (loc
, "collapse argument needs positive constant integer expression");
33055 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
33056 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
33057 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
33058 OMP_CLAUSE_CHAIN (c
) = list
;
33059 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
33065 default ( none | shared )
33068 default ( none | present ) */
33071 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
33072 location_t location
, bool is_oacc
)
33074 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
33077 matching_parens parens
;
33078 if (!parens
.require_open (parser
))
33080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33082 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33083 const char *p
= IDENTIFIER_POINTER (id
);
33088 if (strcmp ("none", p
) != 0)
33090 kind
= OMP_CLAUSE_DEFAULT_NONE
;
33094 if (strcmp ("present", p
) != 0 || !is_oacc
)
33096 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
33100 if (strcmp ("shared", p
) != 0 || is_oacc
)
33102 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
33109 cp_lexer_consume_token (parser
->lexer
);
33115 cp_parser_error (parser
, "expected %<none%> or %<present%>");
33117 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
33120 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
33121 || !parens
.require_close (parser
))
33122 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33123 /*or_comma=*/false,
33124 /*consume_paren=*/true);
33126 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
33129 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
33130 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
33131 OMP_CLAUSE_CHAIN (c
) = list
;
33132 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
33138 final ( expression ) */
33141 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
33145 matching_parens parens
;
33146 if (!parens
.require_open (parser
))
33149 t
= cp_parser_assignment_expression (parser
);
33151 if (t
== error_mark_node
33152 || !parens
.require_close (parser
))
33153 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33154 /*or_comma=*/false,
33155 /*consume_paren=*/true);
33157 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
33159 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
33160 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
33161 OMP_CLAUSE_CHAIN (c
) = list
;
33170 if ( directive-name-modifier : expression )
33172 directive-name-modifier:
33173 parallel | task | taskloop | target data | target | target update
33174 | target enter data | target exit data
33177 directive-name-modifier:
33178 ... | simd | cancel */
33181 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
33185 enum tree_code if_modifier
= ERROR_MARK
;
33187 matching_parens parens
;
33188 if (!parens
.require_open (parser
))
33191 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33193 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33194 const char *p
= IDENTIFIER_POINTER (id
);
33197 if (strcmp ("cancel", p
) == 0)
33198 if_modifier
= VOID_CST
;
33199 else if (strcmp ("parallel", p
) == 0)
33200 if_modifier
= OMP_PARALLEL
;
33201 else if (strcmp ("simd", p
) == 0)
33202 if_modifier
= OMP_SIMD
;
33203 else if (strcmp ("task", p
) == 0)
33204 if_modifier
= OMP_TASK
;
33205 else if (strcmp ("taskloop", p
) == 0)
33206 if_modifier
= OMP_TASKLOOP
;
33207 else if (strcmp ("target", p
) == 0)
33209 if_modifier
= OMP_TARGET
;
33210 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
33212 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
33213 p
= IDENTIFIER_POINTER (id
);
33214 if (strcmp ("data", p
) == 0)
33215 if_modifier
= OMP_TARGET_DATA
;
33216 else if (strcmp ("update", p
) == 0)
33217 if_modifier
= OMP_TARGET_UPDATE
;
33218 else if (strcmp ("enter", p
) == 0)
33219 if_modifier
= OMP_TARGET_ENTER_DATA
;
33220 else if (strcmp ("exit", p
) == 0)
33221 if_modifier
= OMP_TARGET_EXIT_DATA
;
33222 if (if_modifier
!= OMP_TARGET
)
33227 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
33228 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
33230 if_modifier
= ERROR_MARK
;
33232 if (if_modifier
== OMP_TARGET_ENTER_DATA
33233 || if_modifier
== OMP_TARGET_EXIT_DATA
)
33235 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
33237 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
33238 p
= IDENTIFIER_POINTER (id
);
33239 if (strcmp ("data", p
) == 0)
33245 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
33246 error_at (loc
, "expected %<data%>");
33247 if_modifier
= ERROR_MARK
;
33252 if (if_modifier
!= ERROR_MARK
)
33254 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
33257 cp_lexer_consume_token (parser
->lexer
);
33264 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
33265 error_at (loc
, "expected %<:%>");
33267 if_modifier
= ERROR_MARK
;
33272 t
= cp_parser_assignment_expression (parser
);
33274 if (t
== error_mark_node
33275 || !parens
.require_close (parser
))
33276 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33280 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33281 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
33283 if (if_modifier
!= ERROR_MARK
33284 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33286 const char *p
= NULL
;
33287 switch (if_modifier
)
33289 case VOID_CST
: p
= "cancel"; break;
33290 case OMP_PARALLEL
: p
= "parallel"; break;
33291 case OMP_SIMD
: p
= "simd"; break;
33292 case OMP_TASK
: p
= "task"; break;
33293 case OMP_TASKLOOP
: p
= "taskloop"; break;
33294 case OMP_TARGET_DATA
: p
= "target data"; break;
33295 case OMP_TARGET
: p
= "target"; break;
33296 case OMP_TARGET_UPDATE
: p
= "target update"; break;
33297 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
33298 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
33299 default: gcc_unreachable ();
33301 error_at (location
, "too many %<if%> clauses with %qs modifier",
33305 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33308 error_at (location
, "too many %<if%> clauses");
33310 error_at (location
, "too many %<if%> clauses without modifier");
33313 else if (if_modifier
== ERROR_MARK
33314 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
33316 error_at (location
, "if any %<if%> clause has modifier, then all "
33317 "%<if%> clauses have to use modifier");
33322 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
33323 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
33324 OMP_CLAUSE_IF_EXPR (c
) = t
;
33325 OMP_CLAUSE_CHAIN (c
) = list
;
33334 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
33335 tree list
, location_t location
)
33339 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
33342 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
33343 OMP_CLAUSE_CHAIN (c
) = list
;
33351 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
33352 tree list
, location_t location
)
33356 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
33358 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
33359 OMP_CLAUSE_CHAIN (c
) = list
;
33364 num_threads ( expression ) */
33367 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
33368 location_t location
)
33372 matching_parens parens
;
33373 if (!parens
.require_open (parser
))
33376 t
= cp_parser_assignment_expression (parser
);
33378 if (t
== error_mark_node
33379 || !parens
.require_close (parser
))
33380 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33381 /*or_comma=*/false,
33382 /*consume_paren=*/true);
33384 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
33385 "num_threads", location
);
33387 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
33388 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
33389 OMP_CLAUSE_CHAIN (c
) = list
;
33395 num_tasks ( expression ) */
33398 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
33399 location_t location
)
33403 matching_parens parens
;
33404 if (!parens
.require_open (parser
))
33407 t
= cp_parser_assignment_expression (parser
);
33409 if (t
== error_mark_node
33410 || !parens
.require_close (parser
))
33411 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33412 /*or_comma=*/false,
33413 /*consume_paren=*/true);
33415 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
33416 "num_tasks", location
);
33418 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
33419 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
33420 OMP_CLAUSE_CHAIN (c
) = list
;
33426 grainsize ( expression ) */
33429 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
33430 location_t location
)
33434 matching_parens parens
;
33435 if (!parens
.require_open (parser
))
33438 t
= cp_parser_assignment_expression (parser
);
33440 if (t
== error_mark_node
33441 || !parens
.require_close (parser
))
33442 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33443 /*or_comma=*/false,
33444 /*consume_paren=*/true);
33446 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
33447 "grainsize", location
);
33449 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
33450 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
33451 OMP_CLAUSE_CHAIN (c
) = list
;
33457 priority ( expression ) */
33460 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
33461 location_t location
)
33465 matching_parens parens
;
33466 if (!parens
.require_open (parser
))
33469 t
= cp_parser_assignment_expression (parser
);
33471 if (t
== error_mark_node
33472 || !parens
.require_close (parser
))
33473 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33474 /*or_comma=*/false,
33475 /*consume_paren=*/true);
33477 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
33478 "priority", location
);
33480 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
33481 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
33482 OMP_CLAUSE_CHAIN (c
) = list
;
33488 hint ( expression ) */
33491 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33495 matching_parens parens
;
33496 if (!parens
.require_open (parser
))
33499 t
= cp_parser_assignment_expression (parser
);
33501 if (t
== error_mark_node
33502 || !parens
.require_close (parser
))
33503 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33504 /*or_comma=*/false,
33505 /*consume_paren=*/true);
33507 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33509 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33510 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33511 OMP_CLAUSE_CHAIN (c
) = list
;
33517 defaultmap ( tofrom : scalar )
33520 defaultmap ( implicit-behavior [ : variable-category ] ) */
33523 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33524 location_t location
)
33528 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33529 enum omp_clause_defaultmap_kind category
33530 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33532 matching_parens parens
;
33533 if (!parens
.require_open (parser
))
33536 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33538 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33541 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33542 "%<tofrom%>, %<firstprivate%>, %<none%> "
33548 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33549 p
= IDENTIFIER_POINTER (id
);
33555 if (strcmp ("alloc", p
) == 0)
33556 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33558 goto invalid_behavior
;
33562 if (strcmp ("default", p
) == 0)
33563 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33565 goto invalid_behavior
;
33569 if (strcmp ("firstprivate", p
) == 0)
33570 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33571 else if (strcmp ("from", p
) == 0)
33572 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33574 goto invalid_behavior
;
33578 if (strcmp ("none", p
) == 0)
33579 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33581 goto invalid_behavior
;
33585 if (strcmp ("tofrom", p
) == 0)
33586 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33587 else if (strcmp ("to", p
) == 0)
33588 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33590 goto invalid_behavior
;
33594 goto invalid_behavior
;
33596 cp_lexer_consume_token (parser
->lexer
);
33598 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33600 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33603 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33606 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33610 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33611 p
= IDENTIFIER_POINTER (id
);
33616 if (strcmp ("aggregate", p
) == 0)
33617 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33619 goto invalid_category
;
33623 if (strcmp ("pointer", p
) == 0)
33624 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33626 goto invalid_category
;
33630 if (strcmp ("scalar", p
) == 0)
33631 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33633 goto invalid_category
;
33637 goto invalid_category
;
33640 cp_lexer_consume_token (parser
->lexer
);
33642 if (!parens
.require_close (parser
))
33645 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33646 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33647 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33648 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33649 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33650 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33652 enum omp_clause_defaultmap_kind cat
= category
;
33653 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33654 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33655 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33659 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33662 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33665 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33668 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33672 gcc_unreachable ();
33675 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33678 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33683 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33684 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33685 OMP_CLAUSE_CHAIN (c
) = list
;
33689 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33690 /*or_comma=*/false,
33691 /*consume_paren=*/true);
33699 ordered ( constant-expression ) */
33702 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33703 tree list
, location_t location
)
33705 tree c
, num
= NULL_TREE
;
33708 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33709 "ordered", location
);
33711 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33713 matching_parens parens
;
33714 parens
.consume_open (parser
);
33716 num
= cp_parser_constant_expression (parser
);
33718 if (!parens
.require_close (parser
))
33719 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33720 /*or_comma=*/false,
33721 /*consume_paren=*/true);
33723 if (num
== error_mark_node
)
33725 num
= fold_non_dependent_expr (num
);
33726 if (!tree_fits_shwi_p (num
)
33727 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33728 || (n
= tree_to_shwi (num
)) <= 0
33731 error_at (location
,
33732 "ordered argument needs positive constant integer "
33738 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33739 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33740 OMP_CLAUSE_CHAIN (c
) = list
;
33745 reduction ( reduction-operator : variable-list )
33747 reduction-operator:
33748 One of: + * - & ^ | && ||
33752 reduction-operator:
33753 One of: + * - & ^ | && || min max
33757 reduction-operator:
33758 One of: + * - & ^ | && ||
33762 reduction ( reduction-modifier, reduction-operator : variable-list )
33763 in_reduction ( reduction-operator : variable-list )
33764 task_reduction ( reduction-operator : variable-list ) */
33767 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33768 bool is_omp
, tree list
)
33770 enum tree_code code
= ERROR_MARK
;
33771 tree nlist
, c
, id
= NULL_TREE
;
33773 bool inscan
= false;
33775 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33778 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33780 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33781 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33783 cp_lexer_consume_token (parser
->lexer
);
33784 cp_lexer_consume_token (parser
->lexer
);
33786 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33787 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33789 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33790 const char *p
= IDENTIFIER_POINTER (id
);
33791 if (strcmp (p
, "task") == 0)
33793 else if (strcmp (p
, "inscan") == 0)
33796 sorry ("%<inscan%> modifier on %<reduction%> clause "
33797 "not supported yet");
33799 if (task
|| inscan
)
33801 cp_lexer_consume_token (parser
->lexer
);
33802 cp_lexer_consume_token (parser
->lexer
);
33807 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33809 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33810 case CPP_MULT
: code
= MULT_EXPR
; break;
33811 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33812 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33813 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33814 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33815 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33816 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33820 if (code
!= ERROR_MARK
)
33821 cp_lexer_consume_token (parser
->lexer
);
33824 bool saved_colon_corrects_to_scope_p
;
33825 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33826 parser
->colon_corrects_to_scope_p
= false;
33827 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33828 /*check_dependency_p=*/true,
33829 /*template_p=*/NULL
,
33830 /*declarator_p=*/false,
33831 /*optional_p=*/false);
33832 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33833 if (identifier_p (id
))
33835 const char *p
= IDENTIFIER_POINTER (id
);
33837 if (strcmp (p
, "min") == 0)
33839 else if (strcmp (p
, "max") == 0)
33841 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33843 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33845 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33847 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33848 code
= BIT_AND_EXPR
;
33849 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33850 code
= BIT_IOR_EXPR
;
33851 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33852 code
= BIT_XOR_EXPR
;
33853 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33854 code
= TRUTH_ANDIF_EXPR
;
33855 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33856 code
= TRUTH_ORIF_EXPR
;
33857 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33858 tree scope
= parser
->scope
;
33860 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33861 parser
->scope
= NULL_TREE
;
33862 parser
->qualifying_scope
= NULL_TREE
;
33863 parser
->object_scope
= NULL_TREE
;
33867 error ("invalid reduction-identifier");
33869 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33870 /*or_comma=*/false,
33871 /*consume_paren=*/true);
33876 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33879 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33881 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33883 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33885 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33887 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33888 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33895 schedule ( schedule-kind )
33896 schedule ( schedule-kind , expression )
33899 static | dynamic | guided | runtime | auto
33902 schedule ( schedule-modifier : schedule-kind )
33903 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33911 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33914 int modifiers
= 0, nmodifiers
= 0;
33916 matching_parens parens
;
33917 if (!parens
.require_open (parser
))
33920 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33922 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33924 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33925 const char *p
= IDENTIFIER_POINTER (id
);
33926 if (strcmp ("simd", p
) == 0)
33927 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33928 else if (strcmp ("monotonic", p
) == 0)
33929 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33930 else if (strcmp ("nonmonotonic", p
) == 0)
33931 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33934 cp_lexer_consume_token (parser
->lexer
);
33935 if (nmodifiers
++ == 0
33936 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33937 cp_lexer_consume_token (parser
->lexer
);
33940 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33945 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33947 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33948 const char *p
= IDENTIFIER_POINTER (id
);
33953 if (strcmp ("dynamic", p
) != 0)
33955 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33959 if (strcmp ("guided", p
) != 0)
33961 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33965 if (strcmp ("runtime", p
) != 0)
33967 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33974 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33975 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33976 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33977 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33980 cp_lexer_consume_token (parser
->lexer
);
33982 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33983 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33984 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33985 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33987 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33992 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33995 cp_lexer_consume_token (parser
->lexer
);
33997 token
= cp_lexer_peek_token (parser
->lexer
);
33998 t
= cp_parser_assignment_expression (parser
);
34000 if (t
== error_mark_node
)
34002 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
34003 error_at (token
->location
, "schedule %<runtime%> does not take "
34004 "a %<chunk_size%> parameter");
34005 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
34006 error_at (token
->location
, "schedule %<auto%> does not take "
34007 "a %<chunk_size%> parameter");
34009 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
34011 if (!parens
.require_close (parser
))
34014 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34017 OMP_CLAUSE_SCHEDULE_KIND (c
)
34018 = (enum omp_clause_schedule_kind
)
34019 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
34021 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
34022 OMP_CLAUSE_CHAIN (c
) = list
;
34026 cp_parser_error (parser
, "invalid schedule kind");
34028 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34029 /*or_comma=*/false,
34030 /*consume_paren=*/true);
34038 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
34039 tree list
, location_t location
)
34043 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
34045 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
34046 OMP_CLAUSE_CHAIN (c
) = list
;
34055 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
34056 tree list
, location_t location
)
34058 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
34059 tree c
= build_omp_clause (location
, code
);
34060 OMP_CLAUSE_CHAIN (c
) = list
;
34071 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
34072 enum omp_clause_code code
,
34073 tree list
, location_t location
)
34075 tree c
= build_omp_clause (location
, code
);
34076 OMP_CLAUSE_CHAIN (c
) = list
;
34084 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
34085 tree list
, location_t location
)
34087 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
34088 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
34089 OMP_CLAUSE_CHAIN (c
) = list
;
34098 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
34099 enum omp_clause_code code
,
34100 tree list
, location_t location
)
34102 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
34103 tree c
= build_omp_clause (location
, code
);
34104 OMP_CLAUSE_CHAIN (c
) = list
;
34109 num_teams ( expression ) */
34112 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
34113 location_t location
)
34117 matching_parens parens
;
34118 if (!parens
.require_open (parser
))
34121 t
= cp_parser_assignment_expression (parser
);
34123 if (t
== error_mark_node
34124 || !parens
.require_close (parser
))
34125 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34126 /*or_comma=*/false,
34127 /*consume_paren=*/true);
34129 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
34130 "num_teams", location
);
34132 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
34133 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
34134 OMP_CLAUSE_CHAIN (c
) = list
;
34140 thread_limit ( expression ) */
34143 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
34144 location_t location
)
34148 matching_parens parens
;
34149 if (!parens
.require_open (parser
))
34152 t
= cp_parser_assignment_expression (parser
);
34154 if (t
== error_mark_node
34155 || !parens
.require_close (parser
))
34156 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34157 /*or_comma=*/false,
34158 /*consume_paren=*/true);
34160 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
34161 "thread_limit", location
);
34163 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
34164 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
34165 OMP_CLAUSE_CHAIN (c
) = list
;
34171 aligned ( variable-list )
34172 aligned ( variable-list : constant-expression ) */
34175 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
34177 tree nlist
, c
, alignment
= NULL_TREE
;
34180 matching_parens parens
;
34181 if (!parens
.require_open (parser
))
34184 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
34189 alignment
= cp_parser_constant_expression (parser
);
34191 if (!parens
.require_close (parser
))
34192 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34193 /*or_comma=*/false,
34194 /*consume_paren=*/true);
34196 if (alignment
== error_mark_node
)
34197 alignment
= NULL_TREE
;
34200 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34201 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
34207 lastprivate ( variable-list )
34210 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34213 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
34215 bool conditional
= false;
34217 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34221 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
34223 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34224 const char *p
= IDENTIFIER_POINTER (id
);
34226 if (strcmp ("conditional", p
) == 0)
34228 conditional
= true;
34229 cp_lexer_consume_token (parser
->lexer
);
34230 cp_lexer_consume_token (parser
->lexer
);
34234 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
34238 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34239 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
34244 linear ( variable-list )
34245 linear ( variable-list : expression )
34248 linear ( modifier ( variable-list ) )
34249 linear ( modifier ( variable-list ) : expression ) */
34252 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
34255 tree nlist
, c
, step
= integer_one_node
;
34257 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34259 matching_parens parens
;
34260 if (!parens
.require_open (parser
))
34263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34265 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34266 const char *p
= IDENTIFIER_POINTER (id
);
34268 if (strcmp ("ref", p
) == 0)
34269 kind
= OMP_CLAUSE_LINEAR_REF
;
34270 else if (strcmp ("val", p
) == 0)
34271 kind
= OMP_CLAUSE_LINEAR_VAL
;
34272 else if (strcmp ("uval", p
) == 0)
34273 kind
= OMP_CLAUSE_LINEAR_UVAL
;
34274 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
34275 cp_lexer_consume_token (parser
->lexer
);
34277 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34280 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
34281 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
34285 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
34286 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
34288 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34289 else if (!parens
.require_close (parser
))
34290 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34291 /*or_comma=*/false,
34292 /*consume_paren=*/true);
34299 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34300 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
34302 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34303 cp_parser_parse_tentatively (parser
);
34304 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
34305 /*check_dependency_p=*/true,
34306 /*template_p=*/NULL
,
34307 /*declarator_p=*/false,
34308 /*optional_p=*/false);
34309 if (step
!= error_mark_node
)
34310 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
34311 if (step
== error_mark_node
)
34314 cp_parser_abort_tentative_parse (parser
);
34316 else if (!cp_parser_parse_definitely (parser
))
34320 step
= cp_parser_assignment_expression (parser
);
34322 if (!parens
.require_close (parser
))
34323 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34324 /*or_comma=*/false,
34325 /*consume_paren=*/true);
34327 if (step
== error_mark_node
)
34331 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34333 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
34334 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
34341 safelen ( constant-expression ) */
34344 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
34345 location_t location
)
34349 matching_parens parens
;
34350 if (!parens
.require_open (parser
))
34353 t
= cp_parser_constant_expression (parser
);
34355 if (t
== error_mark_node
34356 || !parens
.require_close (parser
))
34357 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34358 /*or_comma=*/false,
34359 /*consume_paren=*/true);
34361 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
34363 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
34364 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
34365 OMP_CLAUSE_CHAIN (c
) = list
;
34371 simdlen ( constant-expression ) */
34374 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
34375 location_t location
)
34379 matching_parens parens
;
34380 if (!parens
.require_open (parser
))
34383 t
= cp_parser_constant_expression (parser
);
34385 if (t
== error_mark_node
34386 || !parens
.require_close (parser
))
34387 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34388 /*or_comma=*/false,
34389 /*consume_paren=*/true);
34391 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
34393 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
34394 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
34395 OMP_CLAUSE_CHAIN (c
) = list
;
34402 identifier [+/- integer]
34403 vec , identifier [+/- integer]
34407 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
34412 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34414 cp_parser_error (parser
, "expected identifier");
34418 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34420 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34421 tree t
, identifier
= cp_parser_identifier (parser
);
34422 tree addend
= NULL
;
34424 if (identifier
== error_mark_node
)
34425 t
= error_mark_node
;
34428 t
= cp_parser_lookup_name_simple
34429 (parser
, identifier
,
34430 cp_lexer_peek_token (parser
->lexer
)->location
);
34431 if (t
== error_mark_node
)
34432 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
34437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
34439 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
34441 addend
= integer_zero_node
;
34442 goto add_to_vector
;
34444 cp_lexer_consume_token (parser
->lexer
);
34446 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
34448 cp_parser_error (parser
, "expected integer");
34452 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34453 if (TREE_CODE (addend
) != INTEGER_CST
)
34455 cp_parser_error (parser
, "expected integer");
34458 cp_lexer_consume_token (parser
->lexer
);
34461 if (t
!= error_mark_node
)
34463 vec
= tree_cons (addend
, t
, vec
);
34465 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
34468 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34471 cp_lexer_consume_token (parser
->lexer
);
34474 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
34476 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
34477 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
34478 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
34479 OMP_CLAUSE_CHAIN (u
) = list
;
34486 iterators ( iterators-definition )
34488 iterators-definition:
34490 iterator-specifier , iterators-definition
34492 iterator-specifier:
34493 identifier = range-specification
34494 iterator-type identifier = range-specification
34496 range-specification:
34498 begin : end : step */
34501 cp_parser_omp_iterators (cp_parser
*parser
)
34503 tree ret
= NULL_TREE
, *last
= &ret
;
34504 cp_lexer_consume_token (parser
->lexer
);
34506 matching_parens parens
;
34507 if (!parens
.require_open (parser
))
34508 return error_mark_node
;
34510 bool saved_colon_corrects_to_scope_p
34511 = parser
->colon_corrects_to_scope_p
;
34512 bool saved_colon_doesnt_start_class_def_p
34513 = parser
->colon_doesnt_start_class_def_p
;
34518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34519 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34520 iter_type
= integer_type_node
;
34523 const char *saved_message
34524 = parser
->type_definition_forbidden_message
;
34525 parser
->type_definition_forbidden_message
34526 = G_("types may not be defined in iterator type");
34528 iter_type
= cp_parser_type_id (parser
);
34530 parser
->type_definition_forbidden_message
= saved_message
;
34533 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34534 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34536 cp_parser_error (parser
, "expected identifier");
34540 tree id
= cp_parser_identifier (parser
);
34541 if (id
== error_mark_node
)
34544 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34547 parser
->colon_corrects_to_scope_p
= false;
34548 parser
->colon_doesnt_start_class_def_p
= true;
34549 tree begin
= cp_parser_assignment_expression (parser
);
34551 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34554 tree end
= cp_parser_assignment_expression (parser
);
34556 tree step
= integer_one_node
;
34557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34559 cp_lexer_consume_token (parser
->lexer
);
34560 step
= cp_parser_assignment_expression (parser
);
34563 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34564 DECL_ARTIFICIAL (iter_var
) = 1;
34565 DECL_CONTEXT (iter_var
) = current_function_decl
;
34566 pushdecl (iter_var
);
34568 *last
= make_tree_vec (6);
34569 TREE_VEC_ELT (*last
, 0) = iter_var
;
34570 TREE_VEC_ELT (*last
, 1) = begin
;
34571 TREE_VEC_ELT (*last
, 2) = end
;
34572 TREE_VEC_ELT (*last
, 3) = step
;
34573 last
= &TREE_CHAIN (*last
);
34575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34577 cp_lexer_consume_token (parser
->lexer
);
34584 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34585 parser
->colon_doesnt_start_class_def_p
34586 = saved_colon_doesnt_start_class_def_p
;
34588 if (!parens
.require_close (parser
))
34589 cp_parser_skip_to_closing_parenthesis (parser
,
34590 /*recovering=*/true,
34591 /*or_comma=*/false,
34592 /*consume_paren=*/true);
34594 return ret
? ret
: error_mark_node
;
34598 depend ( depend-kind : variable-list )
34606 depend ( sink : vec )
34609 depend ( depend-modifier , depend-kind: variable-list )
34612 in | out | inout | mutexinoutset | depobj
34615 iterator ( iterators-definition ) */
34618 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34620 tree nlist
, c
, iterators
= NULL_TREE
;
34621 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34623 matching_parens parens
;
34624 if (!parens
.require_open (parser
))
34629 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34632 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34633 const char *p
= IDENTIFIER_POINTER (id
);
34635 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34637 begin_scope (sk_omp
, NULL
);
34638 iterators
= cp_parser_omp_iterators (parser
);
34639 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34642 if (strcmp ("in", p
) == 0)
34643 kind
= OMP_CLAUSE_DEPEND_IN
;
34644 else if (strcmp ("inout", p
) == 0)
34645 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34646 else if (strcmp ("mutexinoutset", p
) == 0)
34647 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34648 else if (strcmp ("out", p
) == 0)
34649 kind
= OMP_CLAUSE_DEPEND_OUT
;
34650 else if (strcmp ("depobj", p
) == 0)
34651 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34652 else if (strcmp ("sink", p
) == 0)
34653 kind
= OMP_CLAUSE_DEPEND_SINK
;
34654 else if (strcmp ("source", p
) == 0)
34655 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34662 cp_lexer_consume_token (parser
->lexer
);
34665 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34667 poplevel (0, 1, 0);
34668 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34669 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34670 iterators
= NULL_TREE
;
34673 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34675 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34676 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34677 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34678 OMP_CLAUSE_CHAIN (c
) = list
;
34679 if (!parens
.require_close (parser
))
34680 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34681 /*or_comma=*/false,
34682 /*consume_paren=*/true);
34686 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34689 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34690 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34693 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34698 tree block
= poplevel (1, 1, 0);
34699 if (iterators
== error_mark_node
)
34700 iterators
= NULL_TREE
;
34702 TREE_VEC_ELT (iterators
, 5) = block
;
34705 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34707 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34709 OMP_CLAUSE_DECL (c
)
34710 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34716 cp_parser_error (parser
, "invalid depend kind");
34719 poplevel (0, 1, 0);
34720 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34721 /*or_comma=*/false,
34722 /*consume_paren=*/true);
34727 map ( map-kind : variable-list )
34728 map ( variable-list )
34731 alloc | to | from | tofrom
34735 alloc | to | from | tofrom | release | delete
34737 map ( always [,] map-kind: variable-list ) */
34740 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34743 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34744 bool always
= false;
34746 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34751 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34752 const char *p
= IDENTIFIER_POINTER (id
);
34754 if (strcmp ("always", p
) == 0)
34757 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34759 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34760 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34762 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34766 cp_lexer_consume_token (parser
->lexer
);
34768 cp_lexer_consume_token (parser
->lexer
);
34773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34774 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34776 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34777 const char *p
= IDENTIFIER_POINTER (id
);
34779 if (strcmp ("alloc", p
) == 0)
34780 kind
= GOMP_MAP_ALLOC
;
34781 else if (strcmp ("to", p
) == 0)
34782 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34783 else if (strcmp ("from", p
) == 0)
34784 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34785 else if (strcmp ("tofrom", p
) == 0)
34786 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34787 else if (strcmp ("release", p
) == 0)
34788 kind
= GOMP_MAP_RELEASE
;
34791 cp_parser_error (parser
, "invalid map kind");
34792 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34793 /*or_comma=*/false,
34794 /*consume_paren=*/true);
34797 cp_lexer_consume_token (parser
->lexer
);
34798 cp_lexer_consume_token (parser
->lexer
);
34800 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34801 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34803 kind
= GOMP_MAP_DELETE
;
34804 cp_lexer_consume_token (parser
->lexer
);
34805 cp_lexer_consume_token (parser
->lexer
);
34808 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34811 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34812 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34818 device ( expression ) */
34821 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34822 location_t location
)
34826 matching_parens parens
;
34827 if (!parens
.require_open (parser
))
34830 t
= cp_parser_assignment_expression (parser
);
34832 if (t
== error_mark_node
34833 || !parens
.require_close (parser
))
34834 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34835 /*or_comma=*/false,
34836 /*consume_paren=*/true);
34838 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34839 "device", location
);
34841 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34842 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34843 OMP_CLAUSE_CHAIN (c
) = list
;
34849 dist_schedule ( static )
34850 dist_schedule ( static , expression ) */
34853 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34854 location_t location
)
34858 matching_parens parens
;
34859 if (!parens
.require_open (parser
))
34862 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34864 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34866 cp_lexer_consume_token (parser
->lexer
);
34868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34870 cp_lexer_consume_token (parser
->lexer
);
34872 t
= cp_parser_assignment_expression (parser
);
34874 if (t
== error_mark_node
)
34876 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34878 if (!parens
.require_close (parser
))
34881 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34884 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34886 OMP_CLAUSE_CHAIN (c
) = list
;
34890 cp_parser_error (parser
, "invalid dist_schedule kind");
34892 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34893 /*or_comma=*/false,
34894 /*consume_paren=*/true);
34899 proc_bind ( proc-bind-kind )
34902 master | close | spread */
34905 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34906 location_t location
)
34909 enum omp_clause_proc_bind_kind kind
;
34911 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34916 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34917 const char *p
= IDENTIFIER_POINTER (id
);
34919 if (strcmp ("master", p
) == 0)
34920 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34921 else if (strcmp ("close", p
) == 0)
34922 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34923 else if (strcmp ("spread", p
) == 0)
34924 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34931 cp_lexer_consume_token (parser
->lexer
);
34932 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34935 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34936 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34938 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34939 OMP_CLAUSE_CHAIN (c
) = list
;
34943 cp_parser_error (parser
, "invalid depend kind");
34945 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34946 /*or_comma=*/false,
34947 /*consume_paren=*/true);
34952 async [( int-expr )] */
34955 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34958 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34960 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34962 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34964 matching_parens parens
;
34965 parens
.consume_open (parser
);
34967 t
= cp_parser_expression (parser
);
34968 if (t
== error_mark_node
34969 || !parens
.require_close (parser
))
34970 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34971 /*or_comma=*/false,
34972 /*consume_paren=*/true);
34975 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34977 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34978 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34979 OMP_CLAUSE_CHAIN (c
) = list
;
34985 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34986 is a bitmask in MASK. Return the list of clauses found. */
34989 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34990 const char *where
, cp_token
*pragma_tok
,
34991 bool finish_p
= true)
34993 tree clauses
= NULL
;
34996 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34999 pragma_omp_clause c_kind
;
35000 omp_clause_code code
;
35001 const char *c_name
;
35002 tree prev
= clauses
;
35004 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35005 cp_lexer_consume_token (parser
->lexer
);
35007 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
35008 c_kind
= cp_parser_omp_clause_name (parser
);
35012 case PRAGMA_OACC_CLAUSE_ASYNC
:
35013 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
35016 case PRAGMA_OACC_CLAUSE_AUTO
:
35017 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
35021 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
35022 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
35023 c_name
= "collapse";
35025 case PRAGMA_OACC_CLAUSE_COPY
:
35026 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35029 case PRAGMA_OACC_CLAUSE_COPYIN
:
35030 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35033 case PRAGMA_OACC_CLAUSE_COPYOUT
:
35034 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35035 c_name
= "copyout";
35037 case PRAGMA_OACC_CLAUSE_CREATE
:
35038 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35041 case PRAGMA_OACC_CLAUSE_DELETE
:
35042 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35045 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35046 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
35047 c_name
= "default";
35049 case PRAGMA_OACC_CLAUSE_DEVICE
:
35050 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35053 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
35054 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
35055 c_name
= "deviceptr";
35057 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
35058 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35059 c_name
= "device_resident";
35061 case PRAGMA_OACC_CLAUSE_FINALIZE
:
35062 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
35064 c_name
= "finalize";
35066 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
35067 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35069 c_name
= "firstprivate";
35071 case PRAGMA_OACC_CLAUSE_GANG
:
35073 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
35076 case PRAGMA_OACC_CLAUSE_HOST
:
35077 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35080 case PRAGMA_OACC_CLAUSE_IF
:
35081 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
35084 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
35085 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
35087 c_name
= "if_present";
35089 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
35090 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
35092 c_name
= "independent";
35094 case PRAGMA_OACC_CLAUSE_LINK
:
35095 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35098 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
35099 code
= OMP_CLAUSE_NUM_GANGS
;
35100 c_name
= "num_gangs";
35101 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35104 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
35105 c_name
= "num_workers";
35106 code
= OMP_CLAUSE_NUM_WORKERS
;
35107 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35110 case PRAGMA_OACC_CLAUSE_PRESENT
:
35111 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35112 c_name
= "present";
35114 case PRAGMA_OACC_CLAUSE_PRIVATE
:
35115 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35117 c_name
= "private";
35119 case PRAGMA_OACC_CLAUSE_REDUCTION
:
35121 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35123 c_name
= "reduction";
35125 case PRAGMA_OACC_CLAUSE_SEQ
:
35126 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
35130 case PRAGMA_OACC_CLAUSE_TILE
:
35131 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
35134 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
35135 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35137 c_name
= "use_device";
35139 case PRAGMA_OACC_CLAUSE_VECTOR
:
35141 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
35145 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
35146 c_name
= "vector_length";
35147 code
= OMP_CLAUSE_VECTOR_LENGTH
;
35148 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35151 case PRAGMA_OACC_CLAUSE_WAIT
:
35152 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
35155 case PRAGMA_OACC_CLAUSE_WORKER
:
35157 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
35162 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
35168 if (((mask
>> c_kind
) & 1) == 0)
35170 /* Remove the invalid clause(s) from the list to avoid
35171 confusing the rest of the compiler. */
35173 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
35178 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35181 return finish_omp_clauses (clauses
, C_ORT_ACC
);
35186 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35187 is a bitmask in MASK. Return the list of clauses found; the result
35188 of clause default goes in *pdefault. */
35191 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
35192 const char *where
, cp_token
*pragma_tok
,
35193 bool finish_p
= true)
35195 tree clauses
= NULL
;
35197 cp_token
*token
= NULL
;
35199 /* Don't create location wrapper nodes within OpenMP clauses. */
35200 auto_suppress_location_wrappers sentinel
;
35202 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35204 pragma_omp_clause c_kind
;
35205 const char *c_name
;
35206 tree prev
= clauses
;
35208 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35209 cp_lexer_consume_token (parser
->lexer
);
35211 token
= cp_lexer_peek_token (parser
->lexer
);
35212 c_kind
= cp_parser_omp_clause_name (parser
);
35216 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
35217 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
35219 c_name
= "collapse";
35221 case PRAGMA_OMP_CLAUSE_COPYIN
:
35222 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
35225 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
35226 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
35228 c_name
= "copyprivate";
35230 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35231 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
35232 token
->location
, false);
35233 c_name
= "default";
35235 case PRAGMA_OMP_CLAUSE_FINAL
:
35236 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
35239 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
35240 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35242 c_name
= "firstprivate";
35244 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
35245 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
35247 c_name
= "grainsize";
35249 case PRAGMA_OMP_CLAUSE_HINT
:
35250 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
35254 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
35255 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
35257 c_name
= "defaultmap";
35259 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
35260 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35262 c_name
= "use_device_ptr";
35264 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
35265 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
35267 c_name
= "is_device_ptr";
35269 case PRAGMA_OMP_CLAUSE_IF
:
35270 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
35274 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
35276 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
35278 c_name
= "in_reduction";
35280 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
35281 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
35282 c_name
= "lastprivate";
35284 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
35285 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
35287 c_name
= "mergeable";
35289 case PRAGMA_OMP_CLAUSE_NOWAIT
:
35290 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
35293 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
35294 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
35296 c_name
= "num_tasks";
35298 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
35299 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
35301 c_name
= "num_threads";
35303 case PRAGMA_OMP_CLAUSE_ORDERED
:
35304 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
35306 c_name
= "ordered";
35308 case PRAGMA_OMP_CLAUSE_PRIORITY
:
35309 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
35311 c_name
= "priority";
35313 case PRAGMA_OMP_CLAUSE_PRIVATE
:
35314 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35316 c_name
= "private";
35318 case PRAGMA_OMP_CLAUSE_REDUCTION
:
35320 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35322 c_name
= "reduction";
35324 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
35325 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
35327 c_name
= "schedule";
35329 case PRAGMA_OMP_CLAUSE_SHARED
:
35330 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
35334 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
35336 = cp_parser_omp_clause_reduction (parser
,
35337 OMP_CLAUSE_TASK_REDUCTION
,
35339 c_name
= "task_reduction";
35341 case PRAGMA_OMP_CLAUSE_UNTIED
:
35342 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
35346 case PRAGMA_OMP_CLAUSE_INBRANCH
:
35347 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
35348 clauses
, token
->location
);
35349 c_name
= "inbranch";
35351 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
35352 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
35354 c_name
= "nontemporal";
35356 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
35357 clauses
= cp_parser_omp_clause_branch (parser
,
35358 OMP_CLAUSE_NOTINBRANCH
,
35359 clauses
, token
->location
);
35360 c_name
= "notinbranch";
35362 case PRAGMA_OMP_CLAUSE_PARALLEL
:
35363 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
35364 clauses
, token
->location
);
35365 c_name
= "parallel";
35369 error_at (token
->location
, "%qs must be the first clause of %qs",
35374 case PRAGMA_OMP_CLAUSE_FOR
:
35375 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
35376 clauses
, token
->location
);
35379 goto clause_not_first
;
35381 case PRAGMA_OMP_CLAUSE_SECTIONS
:
35382 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
35383 clauses
, token
->location
);
35384 c_name
= "sections";
35386 goto clause_not_first
;
35388 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
35389 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
35390 clauses
, token
->location
);
35391 c_name
= "taskgroup";
35393 goto clause_not_first
;
35395 case PRAGMA_OMP_CLAUSE_LINK
:
35396 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
35399 case PRAGMA_OMP_CLAUSE_TO
:
35400 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
35401 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35404 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
35407 case PRAGMA_OMP_CLAUSE_FROM
:
35408 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
35411 case PRAGMA_OMP_CLAUSE_UNIFORM
:
35412 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
35414 c_name
= "uniform";
35416 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
35417 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
35419 c_name
= "num_teams";
35421 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
35422 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
35424 c_name
= "thread_limit";
35426 case PRAGMA_OMP_CLAUSE_ALIGNED
:
35427 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
35428 c_name
= "aligned";
35430 case PRAGMA_OMP_CLAUSE_LINEAR
:
35432 bool declare_simd
= false;
35433 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
35434 declare_simd
= true;
35435 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
35439 case PRAGMA_OMP_CLAUSE_DEPEND
:
35440 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
35444 case PRAGMA_OMP_CLAUSE_MAP
:
35445 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
35448 case PRAGMA_OMP_CLAUSE_DEVICE
:
35449 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
35453 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
35454 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
35456 c_name
= "dist_schedule";
35458 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
35459 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
35461 c_name
= "proc_bind";
35463 case PRAGMA_OMP_CLAUSE_SAFELEN
:
35464 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
35466 c_name
= "safelen";
35468 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
35469 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
35471 c_name
= "simdlen";
35473 case PRAGMA_OMP_CLAUSE_NOGROUP
:
35474 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
35476 c_name
= "nogroup";
35478 case PRAGMA_OMP_CLAUSE_THREADS
:
35480 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
35481 clauses
, token
->location
);
35482 c_name
= "threads";
35484 case PRAGMA_OMP_CLAUSE_SIMD
:
35486 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
35487 clauses
, token
->location
);
35491 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35497 if (((mask
>> c_kind
) & 1) == 0)
35499 /* Remove the invalid clause(s) from the list to avoid
35500 confusing the rest of the compiler. */
35502 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35506 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35509 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35510 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35512 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35521 In practice, we're also interested in adding the statement to an
35522 outer node. So it is convenient if we work around the fact that
35523 cp_parser_statement calls add_stmt. */
35526 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35528 unsigned save
= parser
->in_statement
;
35530 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35531 This preserves the "not within loop or switch" style error messages
35532 for nonsense cases like
35538 if (parser
->in_statement
)
35539 parser
->in_statement
= IN_OMP_BLOCK
;
35545 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35547 parser
->in_statement
= save
;
35551 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35553 tree stmt
= begin_omp_structured_block ();
35554 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35556 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35558 cp_parser_end_omp_structured_block (parser
, save
);
35559 return finish_omp_structured_block (stmt
);
35563 # pragma omp atomic new-line
35567 x binop= expr | x++ | ++x | x-- | --x
35569 +, *, -, /, &, ^, |, <<, >>
35571 where x is an lvalue expression with scalar type.
35574 # pragma omp atomic new-line
35577 # pragma omp atomic read new-line
35580 # pragma omp atomic write new-line
35583 # pragma omp atomic update new-line
35586 # pragma omp atomic capture new-line
35589 # pragma omp atomic capture new-line
35597 expression-stmt | x = x binop expr
35599 v = expression-stmt
35601 { v = x; update-stmt; } | { update-stmt; v = x; }
35605 expression-stmt | x = x binop expr | x = expr binop x
35609 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35611 where x and v are lvalue expressions with scalar type. */
35614 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35616 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35617 tree rhs1
= NULL_TREE
, orig_lhs
;
35618 location_t loc
= pragma_tok
->location
;
35619 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35620 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35621 bool structured_block
= false;
35623 tree clauses
= NULL_TREE
;
35625 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35627 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35628 cp_lexer_consume_token (parser
->lexer
);
35632 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35634 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35635 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35636 const char *p
= IDENTIFIER_POINTER (id
);
35637 enum tree_code new_code
= ERROR_MARK
;
35638 enum omp_memory_order new_memory_order
35639 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35641 if (!strcmp (p
, "read"))
35642 new_code
= OMP_ATOMIC_READ
;
35643 else if (!strcmp (p
, "write"))
35644 new_code
= NOP_EXPR
;
35645 else if (!strcmp (p
, "update"))
35646 new_code
= OMP_ATOMIC
;
35647 else if (!strcmp (p
, "capture"))
35648 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35649 else if (!strcmp (p
, "seq_cst"))
35650 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35651 else if (!strcmp (p
, "acq_rel"))
35652 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35653 else if (!strcmp (p
, "release"))
35654 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35655 else if (!strcmp (p
, "acquire"))
35656 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35657 else if (!strcmp (p
, "relaxed"))
35658 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35659 else if (!strcmp (p
, "hint"))
35661 cp_lexer_consume_token (parser
->lexer
);
35662 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35668 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35669 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35670 "%<release%>, %<relaxed%> or %<hint%> clause");
35674 if (new_code
!= ERROR_MARK
)
35676 if (code
!= ERROR_MARK
)
35677 error_at (cloc
, "too many atomic clauses");
35681 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35683 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35684 error_at (cloc
, "too many memory order clauses");
35686 memory_order
= new_memory_order
;
35688 cp_lexer_consume_token (parser
->lexer
);
35694 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35696 if (code
== ERROR_MARK
)
35698 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35701 = (enum omp_requires
) (omp_requires_mask
35702 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35703 switch ((enum omp_memory_order
)
35704 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35706 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35707 case OMP_MEMORY_ORDER_RELAXED
:
35708 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35710 case OMP_MEMORY_ORDER_SEQ_CST
:
35711 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35713 case OMP_MEMORY_ORDER_ACQ_REL
:
35716 case OMP_ATOMIC_READ
:
35717 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35719 case NOP_EXPR
: /* atomic write */
35721 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35724 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35729 gcc_unreachable ();
35735 case OMP_ATOMIC_READ
:
35736 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35737 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35739 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35740 "%<acq_rel%> or %<release%> clauses");
35741 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35744 case NOP_EXPR
: /* atomic write */
35745 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35746 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35748 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35749 "%<acq_rel%> or %<acquire%> clauses");
35750 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35754 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35755 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35757 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35758 "%<acq_rel%> or %<acquire%> clauses");
35759 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35768 case OMP_ATOMIC_READ
:
35769 case NOP_EXPR
: /* atomic write */
35770 v
= cp_parser_unary_expression (parser
);
35771 if (v
== error_mark_node
)
35773 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35775 if (code
== NOP_EXPR
)
35776 lhs
= cp_parser_expression (parser
);
35778 lhs
= cp_parser_unary_expression (parser
);
35779 if (lhs
== error_mark_node
)
35781 if (code
== NOP_EXPR
)
35783 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35791 case OMP_ATOMIC_CAPTURE_NEW
:
35792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35794 cp_lexer_consume_token (parser
->lexer
);
35795 structured_block
= true;
35799 v
= cp_parser_unary_expression (parser
);
35800 if (v
== error_mark_node
)
35802 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35810 lhs
= cp_parser_unary_expression (parser
);
35812 switch (TREE_CODE (lhs
))
35817 case POSTINCREMENT_EXPR
:
35818 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35819 code
= OMP_ATOMIC_CAPTURE_OLD
;
35821 case PREINCREMENT_EXPR
:
35822 lhs
= TREE_OPERAND (lhs
, 0);
35823 opcode
= PLUS_EXPR
;
35824 rhs
= integer_one_node
;
35827 case POSTDECREMENT_EXPR
:
35828 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35829 code
= OMP_ATOMIC_CAPTURE_OLD
;
35831 case PREDECREMENT_EXPR
:
35832 lhs
= TREE_OPERAND (lhs
, 0);
35833 opcode
= MINUS_EXPR
;
35834 rhs
= integer_one_node
;
35837 case COMPOUND_EXPR
:
35838 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35839 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35840 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35841 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35842 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35843 (TREE_OPERAND (lhs
, 1), 0), 0)))
35845 /* Undo effects of boolean_increment for post {in,de}crement. */
35846 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35849 if (TREE_CODE (lhs
) == MODIFY_EXPR
35850 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35852 /* Undo effects of boolean_increment. */
35853 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35855 /* This is pre or post increment. */
35856 rhs
= TREE_OPERAND (lhs
, 1);
35857 lhs
= TREE_OPERAND (lhs
, 0);
35859 if (code
== OMP_ATOMIC_CAPTURE_NEW
35860 && !structured_block
35861 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35862 code
= OMP_ATOMIC_CAPTURE_OLD
;
35868 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35871 opcode
= MULT_EXPR
;
35874 opcode
= TRUNC_DIV_EXPR
;
35877 opcode
= PLUS_EXPR
;
35880 opcode
= MINUS_EXPR
;
35882 case CPP_LSHIFT_EQ
:
35883 opcode
= LSHIFT_EXPR
;
35885 case CPP_RSHIFT_EQ
:
35886 opcode
= RSHIFT_EXPR
;
35889 opcode
= BIT_AND_EXPR
;
35892 opcode
= BIT_IOR_EXPR
;
35895 opcode
= BIT_XOR_EXPR
;
35898 enum cp_parser_prec oprec
;
35900 cp_lexer_consume_token (parser
->lexer
);
35901 cp_parser_parse_tentatively (parser
);
35902 rhs1
= cp_parser_simple_cast_expression (parser
);
35903 if (rhs1
== error_mark_node
)
35905 cp_parser_abort_tentative_parse (parser
);
35906 cp_parser_simple_cast_expression (parser
);
35909 token
= cp_lexer_peek_token (parser
->lexer
);
35910 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35912 cp_parser_abort_tentative_parse (parser
);
35913 cp_parser_parse_tentatively (parser
);
35914 rhs
= cp_parser_binary_expression (parser
, false, true,
35915 PREC_NOT_OPERATOR
, NULL
);
35916 if (rhs
== error_mark_node
)
35918 cp_parser_abort_tentative_parse (parser
);
35919 cp_parser_binary_expression (parser
, false, true,
35920 PREC_NOT_OPERATOR
, NULL
);
35923 switch (TREE_CODE (rhs
))
35926 case TRUNC_DIV_EXPR
:
35935 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35937 if (cp_parser_parse_definitely (parser
))
35939 opcode
= TREE_CODE (rhs
);
35940 rhs1
= TREE_OPERAND (rhs
, 0);
35941 rhs
= TREE_OPERAND (rhs
, 1);
35951 cp_parser_abort_tentative_parse (parser
);
35952 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35954 rhs
= cp_parser_expression (parser
);
35955 if (rhs
== error_mark_node
)
35961 cp_parser_error (parser
,
35962 "invalid form of %<#pragma omp atomic%>");
35965 if (!cp_parser_parse_definitely (parser
))
35967 switch (token
->type
)
35969 case CPP_SEMICOLON
:
35970 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35972 code
= OMP_ATOMIC_CAPTURE_OLD
;
35977 cp_lexer_consume_token (parser
->lexer
);
35980 else if (structured_block
)
35987 cp_parser_error (parser
,
35988 "invalid form of %<#pragma omp atomic%>");
35991 opcode
= MULT_EXPR
;
35994 opcode
= TRUNC_DIV_EXPR
;
35997 opcode
= PLUS_EXPR
;
36000 opcode
= MINUS_EXPR
;
36003 opcode
= LSHIFT_EXPR
;
36006 opcode
= RSHIFT_EXPR
;
36009 opcode
= BIT_AND_EXPR
;
36012 opcode
= BIT_IOR_EXPR
;
36015 opcode
= BIT_XOR_EXPR
;
36018 cp_parser_error (parser
,
36019 "invalid operator for %<#pragma omp atomic%>");
36022 oprec
= TOKEN_PRECEDENCE (token
);
36023 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
36024 if (commutative_tree_code (opcode
))
36025 oprec
= (enum cp_parser_prec
) (oprec
- 1);
36026 cp_lexer_consume_token (parser
->lexer
);
36027 rhs
= cp_parser_binary_expression (parser
, false, false,
36029 if (rhs
== error_mark_node
)
36034 cp_parser_error (parser
,
36035 "invalid operator for %<#pragma omp atomic%>");
36038 cp_lexer_consume_token (parser
->lexer
);
36040 rhs
= cp_parser_expression (parser
);
36041 if (rhs
== error_mark_node
)
36046 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
36048 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
36050 v
= cp_parser_unary_expression (parser
);
36051 if (v
== error_mark_node
)
36053 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36055 lhs1
= cp_parser_unary_expression (parser
);
36056 if (lhs1
== error_mark_node
)
36059 if (structured_block
)
36061 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36062 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
36065 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
36066 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
36067 rhs1
, clauses
, memory_order
);
36068 if (!structured_block
)
36069 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36073 cp_parser_skip_to_end_of_block_or_statement (parser
);
36074 if (structured_block
)
36076 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36077 cp_lexer_consume_token (parser
->lexer
);
36078 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
36080 cp_parser_skip_to_end_of_block_or_statement (parser
);
36081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36082 cp_lexer_consume_token (parser
->lexer
);
36089 # pragma omp barrier new-line */
36092 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
36094 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36095 finish_omp_barrier ();
36099 # pragma omp critical [(name)] new-line
36103 # pragma omp critical [(name) [hint(expression)]] new-line
36104 structured-block */
36106 #define OMP_CRITICAL_CLAUSE_MASK \
36107 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36110 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36112 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
36114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36116 matching_parens parens
;
36117 parens
.consume_open (parser
);
36119 name
= cp_parser_identifier (parser
);
36121 if (name
== error_mark_node
36122 || !parens
.require_close (parser
))
36123 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36124 /*or_comma=*/false,
36125 /*consume_paren=*/true);
36126 if (name
== error_mark_node
)
36129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
36130 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
36131 cp_lexer_consume_token (parser
->lexer
);
36133 clauses
= cp_parser_omp_all_clauses (parser
,
36134 OMP_CRITICAL_CLAUSE_MASK
,
36135 "#pragma omp critical", pragma_tok
);
36138 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36140 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
36141 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
36145 # pragma omp depobj ( depobj ) depobj-clause new-line
36148 depend (dependence-type : locator)
36150 update (dependence-type)
36159 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
36161 location_t loc
= pragma_tok
->location
;
36162 matching_parens parens
;
36163 if (!parens
.require_open (parser
))
36165 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36169 tree depobj
= cp_parser_assignment_expression (parser
);
36171 if (!parens
.require_close (parser
))
36172 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36173 /*or_comma=*/false,
36174 /*consume_paren=*/true);
36176 tree clause
= NULL_TREE
;
36177 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
36178 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36179 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36181 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36182 const char *p
= IDENTIFIER_POINTER (id
);
36184 cp_lexer_consume_token (parser
->lexer
);
36185 if (!strcmp ("depend", p
))
36187 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
36189 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
36191 clause
= error_mark_node
;
36193 else if (!strcmp ("destroy", p
))
36194 kind
= OMP_CLAUSE_DEPEND_LAST
;
36195 else if (!strcmp ("update", p
))
36197 matching_parens c_parens
;
36198 if (c_parens
.require_open (parser
))
36201 = cp_lexer_peek_token (parser
->lexer
)->location
;
36202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36204 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36205 const char *p2
= IDENTIFIER_POINTER (id2
);
36207 cp_lexer_consume_token (parser
->lexer
);
36208 if (!strcmp ("in", p2
))
36209 kind
= OMP_CLAUSE_DEPEND_IN
;
36210 else if (!strcmp ("out", p2
))
36211 kind
= OMP_CLAUSE_DEPEND_OUT
;
36212 else if (!strcmp ("inout", p2
))
36213 kind
= OMP_CLAUSE_DEPEND_INOUT
;
36214 else if (!strcmp ("mutexinoutset", p2
))
36215 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
36217 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36219 clause
= error_mark_node
;
36220 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
36221 "%<mutexinoutset%>");
36223 if (!c_parens
.require_close (parser
))
36224 cp_parser_skip_to_closing_parenthesis (parser
,
36225 /*recovering=*/true,
36226 /*or_comma=*/false,
36227 /*consume_paren=*/true);
36230 clause
= error_mark_node
;
36233 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36235 clause
= error_mark_node
;
36236 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
36238 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36240 finish_omp_depobj (loc
, depobj
, kind
, clause
);
36245 # pragma omp flush flush-vars[opt] new-line
36251 # pragma omp flush memory-order-clause new-line */
36254 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
36256 enum memmodel mo
= MEMMODEL_LAST
;
36257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36259 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36260 const char *p
= IDENTIFIER_POINTER (id
);
36261 if (!strcmp (p
, "acq_rel"))
36262 mo
= MEMMODEL_ACQ_REL
;
36263 else if (!strcmp (p
, "release"))
36264 mo
= MEMMODEL_RELEASE
;
36265 else if (!strcmp (p
, "acquire"))
36266 mo
= MEMMODEL_ACQUIRE
;
36268 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36269 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36270 cp_lexer_consume_token (parser
->lexer
);
36272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36274 if (mo
!= MEMMODEL_LAST
)
36275 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36276 "%<flush%> list specified together with memory order "
36278 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36280 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36282 finish_omp_flush (mo
);
36285 /* Helper function, to parse omp for increment expression. */
36288 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
36290 tree cond
= cp_parser_binary_expression (parser
, false, true,
36291 PREC_NOT_OPERATOR
, NULL
);
36292 if (cond
== error_mark_node
36293 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36295 cp_parser_skip_to_end_of_statement (parser
);
36296 return error_mark_node
;
36299 switch (TREE_CODE (cond
))
36307 if (code
!= OACC_LOOP
)
36309 gcc_fallthrough ();
36311 return error_mark_node
;
36314 /* If decl is an iterator, preserve LHS and RHS of the relational
36315 expr until finish_omp_for. */
36317 && (type_dependent_expression_p (decl
)
36318 || CLASS_TYPE_P (TREE_TYPE (decl
))))
36321 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
36323 TREE_OPERAND (cond
, 0), ERROR_MARK
,
36324 TREE_OPERAND (cond
, 1), ERROR_MARK
,
36325 /*overload=*/NULL
, tf_warning_or_error
);
36328 /* Helper function, to parse omp for increment expression. */
36331 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
36333 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36339 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36341 op
= (token
->type
== CPP_PLUS_PLUS
36342 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
36343 cp_lexer_consume_token (parser
->lexer
);
36344 lhs
= cp_parser_simple_cast_expression (parser
);
36346 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36347 return error_mark_node
;
36348 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36351 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
36353 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36354 return error_mark_node
;
36356 token
= cp_lexer_peek_token (parser
->lexer
);
36357 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36359 op
= (token
->type
== CPP_PLUS_PLUS
36360 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
36361 cp_lexer_consume_token (parser
->lexer
);
36362 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36365 op
= cp_parser_assignment_operator_opt (parser
);
36366 if (op
== ERROR_MARK
)
36367 return error_mark_node
;
36369 if (op
!= NOP_EXPR
)
36371 rhs
= cp_parser_assignment_expression (parser
);
36372 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
36373 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36376 lhs
= cp_parser_binary_expression (parser
, false, false,
36377 PREC_ADDITIVE_EXPRESSION
, NULL
);
36378 token
= cp_lexer_peek_token (parser
->lexer
);
36379 decl_first
= (lhs
== decl
36380 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
36383 if (token
->type
!= CPP_PLUS
36384 && token
->type
!= CPP_MINUS
)
36385 return error_mark_node
;
36389 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
36390 cp_lexer_consume_token (parser
->lexer
);
36391 rhs
= cp_parser_binary_expression (parser
, false, false,
36392 PREC_ADDITIVE_EXPRESSION
, NULL
);
36393 token
= cp_lexer_peek_token (parser
->lexer
);
36394 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
36396 if (lhs
== NULL_TREE
)
36398 if (op
== PLUS_EXPR
)
36401 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
36402 tf_warning_or_error
);
36405 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
36406 ERROR_MARK
, NULL
, tf_warning_or_error
);
36409 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
36414 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
36415 || op
== MINUS_EXPR
)
36416 return error_mark_node
;
36417 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
36420 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
36422 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36425 /* Parse the initialization statement of an OpenMP for loop.
36427 Return true if the resulting construct should have an
36428 OMP_CLAUSE_PRIVATE added to it. */
36431 cp_parser_omp_for_loop_init (cp_parser
*parser
,
36432 tree
&this_pre_body
,
36433 vec
<tree
, va_gc
> *&for_block
,
36439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36442 tree add_private_clause
= NULL_TREE
;
36444 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36448 integer-type var = lb
36449 random-access-iterator-type var = lb
36450 pointer-type var = lb
36452 cp_decl_specifier_seq type_specifiers
;
36454 /* First, try to parse as an initialized declaration. See
36455 cp_parser_condition, from whence the bulk of this is copied. */
36457 cp_parser_parse_tentatively (parser
);
36458 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
36459 /*is_declaration=*/true,
36460 /*is_trailing_return=*/false,
36462 if (cp_parser_parse_definitely (parser
))
36464 /* If parsing a type specifier seq succeeded, then this
36465 MUST be a initialized declaration. */
36466 tree asm_specification
, attributes
;
36467 cp_declarator
*declarator
;
36469 declarator
= cp_parser_declarator (parser
,
36470 CP_PARSER_DECLARATOR_NAMED
,
36471 CP_PARSER_FLAGS_NONE
,
36472 /*ctor_dtor_or_conv_p=*/NULL
,
36473 /*parenthesized_p=*/NULL
,
36474 /*member_p=*/false,
36475 /*friend_p=*/false,
36476 /*static_p=*/false);
36477 attributes
= cp_parser_attributes_opt (parser
);
36478 asm_specification
= cp_parser_asm_specification_opt (parser
);
36480 if (declarator
== cp_error_declarator
)
36481 cp_parser_skip_to_end_of_statement (parser
);
36485 tree pushed_scope
, auto_node
;
36487 decl
= start_decl (declarator
, &type_specifiers
,
36488 SD_INITIALIZED
, attributes
,
36489 /*prefix_attributes=*/NULL_TREE
,
36492 auto_node
= type_uses_auto (TREE_TYPE (decl
));
36493 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36495 if (cp_lexer_next_token_is (parser
->lexer
,
36497 error ("parenthesized initialization is not allowed in "
36498 "OpenMP %<for%> loop");
36500 /* Trigger an error. */
36501 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36503 init
= error_mark_node
;
36504 cp_parser_skip_to_end_of_statement (parser
);
36506 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36507 || type_dependent_expression_p (decl
)
36510 bool is_direct_init
, is_non_constant_init
;
36512 init
= cp_parser_initializer (parser
,
36514 &is_non_constant_init
);
36519 = do_auto_deduction (TREE_TYPE (decl
), init
,
36522 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36523 && !type_dependent_expression_p (decl
))
36527 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36529 LOOKUP_ONLYCONVERTING
);
36531 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36533 vec_safe_push (for_block
, this_pre_body
);
36538 init
= pop_stmt_list (this_pre_body
);
36539 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36541 tree_stmt_iterator i
= tsi_start (init
);
36542 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36543 while (!tsi_end_p (i
))
36545 tree t
= tsi_stmt (i
);
36546 if (TREE_CODE (t
) == DECL_EXPR
36547 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36550 vec_safe_push (for_block
, t
);
36555 if (tsi_one_before_end_p (i
))
36557 tree t
= tsi_stmt (i
);
36559 free_stmt_list (init
);
36564 this_pre_body
= NULL_TREE
;
36569 cp_lexer_consume_token (parser
->lexer
);
36570 init
= cp_parser_assignment_expression (parser
);
36573 if (TYPE_REF_P (TREE_TYPE (decl
)))
36574 init
= error_mark_node
;
36576 cp_finish_decl (decl
, NULL_TREE
,
36577 /*init_const_expr_p=*/false,
36579 LOOKUP_ONLYCONVERTING
);
36583 pop_scope (pushed_scope
);
36589 /* If parsing a type specifier sequence failed, then
36590 this MUST be a simple expression. */
36591 cp_parser_parse_tentatively (parser
);
36592 decl
= cp_parser_primary_expression (parser
, false, false,
36594 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36595 if (!cp_parser_error_occurred (parser
)
36597 && (TREE_CODE (decl
) == COMPONENT_REF
36598 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36600 cp_parser_abort_tentative_parse (parser
);
36601 cp_parser_parse_tentatively (parser
);
36602 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36603 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36604 /*check_dependency_p=*/true,
36605 /*template_p=*/NULL
,
36606 /*declarator_p=*/false,
36607 /*optional_p=*/false);
36608 if (name
!= error_mark_node
36609 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36611 decl
= cp_parser_lookup_name_simple (parser
, name
,
36613 if (TREE_CODE (decl
) == FIELD_DECL
)
36614 add_private_clause
= omp_privatize_field (decl
, false);
36616 cp_parser_abort_tentative_parse (parser
);
36617 cp_parser_parse_tentatively (parser
);
36618 decl
= cp_parser_primary_expression (parser
, false, false,
36621 if (!cp_parser_error_occurred (parser
)
36624 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36628 cp_parser_parse_definitely (parser
);
36629 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36630 rhs
= cp_parser_assignment_expression (parser
);
36632 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36635 tf_warning_or_error
));
36636 if (!add_private_clause
)
36637 add_private_clause
= decl
;
36642 cp_parser_abort_tentative_parse (parser
);
36643 init
= cp_parser_expression (parser
);
36646 if (TREE_CODE (init
) == MODIFY_EXPR
36647 || TREE_CODE (init
) == MODOP_EXPR
)
36648 real_decl
= TREE_OPERAND (init
, 0);
36652 return add_private_clause
;
36655 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36658 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36659 tree
&decl
, tree
&orig_decl
, tree
&init
,
36660 tree
&orig_init
, tree
&cond
, tree
&incr
)
36662 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36663 tree iter_type
, begin_expr
, end_expr
;
36665 if (processing_template_decl
)
36667 if (check_for_bare_parameter_packs (init
))
36668 init
= error_mark_node
;
36669 if (!type_dependent_expression_p (init
)
36670 /* do_auto_deduction doesn't mess with template init-lists. */
36671 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36674 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36676 tree v
= DECL_VALUE_EXPR (decl
);
36677 if (TREE_CODE (v
) == ARRAY_REF
36678 && VAR_P (TREE_OPERAND (v
, 0))
36679 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36680 d
= TREE_OPERAND (v
, 0);
36682 do_range_for_auto_deduction (d
, init
);
36684 cond
= global_namespace
;
36688 this_pre_body
= pop_stmt_list (this_pre_body
);
36692 init
= mark_lvalue_use (init
);
36694 if (decl
== error_mark_node
|| init
== error_mark_node
)
36695 /* If an error happened previously do nothing or else a lot of
36696 unhelpful errors would be issued. */
36697 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36703 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36704 /* Can't bind a reference to an array of runtime bound. */
36708 range_temp
= build_range_temp (init
);
36709 DECL_NAME (range_temp
) = NULL_TREE
;
36710 pushdecl (range_temp
);
36711 cp_finish_decl (range_temp
, init
,
36712 /*is_constant_init*/false, NULL_TREE
,
36713 LOOKUP_ONLYCONVERTING
);
36714 range_temp_decl
= range_temp
;
36715 range_temp
= convert_from_reference (range_temp
);
36717 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36718 &begin_expr
, &end_expr
);
36721 tree end_iter_type
= iter_type
;
36722 if (cxx_dialect
>= cxx17
)
36723 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36724 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36725 TREE_USED (end
) = 1;
36726 DECL_ARTIFICIAL (end
) = 1;
36728 cp_finish_decl (end
, end_expr
,
36729 /*is_constant_init*/false, NULL_TREE
,
36730 LOOKUP_ONLYCONVERTING
);
36732 /* The new for initialization statement. */
36733 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36734 TREE_USED (begin
) = 1;
36735 DECL_ARTIFICIAL (begin
) = 1;
36738 if (CLASS_TYPE_P (iter_type
))
36743 begin_expr
= NULL_TREE
;
36745 cp_finish_decl (begin
, begin_expr
,
36746 /*is_constant_init*/false, NULL_TREE
,
36747 LOOKUP_ONLYCONVERTING
);
36749 /* The new for condition. */
36750 if (CLASS_TYPE_P (iter_type
))
36751 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36753 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36756 NULL
, tf_warning_or_error
);
36758 /* The new increment expression. */
36759 if (CLASS_TYPE_P (iter_type
))
36760 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36762 incr
= finish_unary_op_expr (input_location
,
36763 PREINCREMENT_EXPR
, begin
,
36764 tf_warning_or_error
);
36770 vec_safe_push (for_block
, this_pre_body
);
36771 this_pre_body
= NULL_TREE
;
36774 tree decomp_first_name
= NULL_TREE
;
36775 unsigned decomp_cnt
= 0;
36776 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36778 tree v
= DECL_VALUE_EXPR (orig_decl
);
36779 if (TREE_CODE (v
) == ARRAY_REF
36780 && VAR_P (TREE_OPERAND (v
, 0))
36781 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36783 tree d
= orig_decl
;
36784 orig_decl
= TREE_OPERAND (v
, 0);
36785 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36786 decomp_first_name
= d
;
36790 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36793 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36795 if (!error_operand_p (t
))
36796 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36800 tree v
= make_tree_vec (decomp_cnt
+ 3);
36801 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36802 TREE_VEC_ELT (v
, 1) = end
;
36803 TREE_VEC_ELT (v
, 2) = orig_decl
;
36804 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36806 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36807 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36809 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36812 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36813 inside of the collapsed body. */
36816 cp_finish_omp_range_for (tree orig
, tree begin
)
36818 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36819 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36820 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36821 tree decomp_first_name
= NULL_TREE
;
36822 unsigned int decomp_cnt
= 0;
36824 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36826 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36827 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36828 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36831 /* The declaration is initialized with *__begin inside the loop body. */
36832 cp_finish_decl (decl
,
36833 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36834 tf_warning_or_error
),
36835 /*is_constant_init*/false, NULL_TREE
,
36836 LOOKUP_ONLYCONVERTING
);
36837 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36838 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36841 /* Parse the restricted form of the for statement allowed by OpenMP. */
36844 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36845 tree
*cclauses
, bool *if_p
)
36847 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36849 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36850 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36851 location_t loc_first
;
36852 bool collapse_err
= false;
36853 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36854 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36855 auto_vec
<tree
, 4> orig_inits
;
36856 bool tiling
= false;
36858 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36859 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36860 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36861 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36864 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36866 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36867 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36870 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36873 if (ordered
&& ordered
< collapse
)
36875 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36876 "%<ordered%> clause parameter is less than %<collapse%>");
36877 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36878 = build_int_cst (NULL_TREE
, collapse
);
36879 ordered
= collapse
;
36883 for (tree
*pc
= &clauses
; *pc
; )
36884 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36886 error_at (OMP_CLAUSE_LOCATION (*pc
),
36887 "%<linear%> clause may not be specified together "
36888 "with %<ordered%> clause with a parameter");
36889 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36892 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36895 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36896 count
= ordered
? ordered
: collapse
;
36898 declv
= make_tree_vec (count
);
36899 initv
= make_tree_vec (count
);
36900 condv
= make_tree_vec (count
);
36901 incrv
= make_tree_vec (count
);
36902 orig_declv
= NULL_TREE
;
36904 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36906 for (i
= 0; i
< count
; i
++)
36908 int bracecount
= 0;
36909 tree add_private_clause
= NULL_TREE
;
36912 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36915 cp_parser_error (parser
, "for statement expected");
36918 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36920 /* Don't create location wrapper nodes within an OpenMP "for"
36922 auto_suppress_location_wrappers sentinel
;
36924 matching_parens parens
;
36925 if (!parens
.require_open (parser
))
36928 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36929 this_pre_body
= push_stmt_list ();
36931 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36933 /* Save tokens so that we can put them back. */
36934 cp_lexer_save_tokens (parser
->lexer
);
36936 /* Look for ':' that is not nested in () or {}. */
36938 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36939 /*recovering=*/false,
36944 /* Roll back the tokens we skipped. */
36945 cp_lexer_rollback_tokens (parser
->lexer
);
36949 bool saved_colon_corrects_to_scope_p
36950 = parser
->colon_corrects_to_scope_p
;
36952 /* A colon is used in range-based for. */
36953 parser
->colon_corrects_to_scope_p
= false;
36955 /* Parse the declaration. */
36956 cp_parser_simple_declaration (parser
,
36957 /*function_definition_allowed_p=*/
36959 parser
->colon_corrects_to_scope_p
36960 = saved_colon_corrects_to_scope_p
;
36962 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36964 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36967 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36968 orig_decl
, init
, orig_init
,
36975 pre_body
= push_stmt_list ();
36977 add_stmt (this_pre_body
);
36978 pre_body
= pop_stmt_list (pre_body
);
36981 pre_body
= this_pre_body
;
36985 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36986 "%<ordered%> clause with parameter on "
36987 "range-based %<for%> loop");
36989 goto parse_close_paren
;
36994 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36995 init
, orig_init
, decl
, real_decl
);
36997 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
37000 this_pre_body
= pop_stmt_list (this_pre_body
);
37004 pre_body
= push_stmt_list ();
37006 add_stmt (this_pre_body
);
37007 pre_body
= pop_stmt_list (pre_body
);
37010 pre_body
= this_pre_body
;
37015 if (cclauses
!= NULL
37016 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
37017 && real_decl
!= NULL_TREE
)
37020 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
37021 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
37022 && OMP_CLAUSE_DECL (*c
) == real_decl
)
37024 error_at (loc
, "iteration variable %qD"
37025 " should not be firstprivate", real_decl
);
37026 *c
= OMP_CLAUSE_CHAIN (*c
);
37028 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
37029 && OMP_CLAUSE_DECL (*c
) == real_decl
)
37031 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
37033 *c
= OMP_CLAUSE_CHAIN (*c
);
37034 if (code
== OMP_SIMD
)
37036 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37037 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
37041 OMP_CLAUSE_CHAIN (l
) = clauses
;
37044 add_private_clause
= NULL_TREE
;
37048 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
37049 && OMP_CLAUSE_DECL (*c
) == real_decl
)
37050 add_private_clause
= NULL_TREE
;
37051 c
= &OMP_CLAUSE_CHAIN (*c
);
37055 if (add_private_clause
)
37058 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37060 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
37061 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
37062 && OMP_CLAUSE_DECL (c
) == decl
)
37064 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
37065 && OMP_CLAUSE_DECL (c
) == decl
)
37066 error_at (loc
, "iteration variable %qD "
37067 "should not be firstprivate",
37069 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
37070 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
37071 && OMP_CLAUSE_DECL (c
) == decl
)
37072 error_at (loc
, "iteration variable %qD should not be reduction",
37077 if (code
!= OMP_SIMD
)
37078 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
37079 else if (collapse
== 1)
37080 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
37082 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
37083 OMP_CLAUSE_DECL (c
) = add_private_clause
;
37084 c
= finish_omp_clauses (c
, C_ORT_OMP
);
37087 OMP_CLAUSE_CHAIN (c
) = clauses
;
37089 /* For linear, signal that we need to fill up
37090 the so far unknown linear step. */
37091 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
37092 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
37098 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
37099 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
37100 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
37103 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
37105 /* If decl is an iterator, preserve the operator on decl
37106 until finish_omp_for. */
37108 && ((processing_template_decl
37109 && (TREE_TYPE (real_decl
) == NULL_TREE
37110 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
37111 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
37112 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
37114 incr
= cp_parser_expression (parser
);
37115 if (!EXPR_HAS_LOCATION (incr
))
37116 protected_set_expr_location (incr
, input_location
);
37120 if (!parens
.require_close (parser
))
37121 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37122 /*or_comma=*/false,
37123 /*consume_paren=*/true);
37125 TREE_VEC_ELT (declv
, i
) = decl
;
37126 TREE_VEC_ELT (initv
, i
) = init
;
37127 TREE_VEC_ELT (condv
, i
) = cond
;
37128 TREE_VEC_ELT (incrv
, i
) = incr
;
37131 orig_inits
.safe_grow_cleared (i
+ 1);
37132 orig_inits
[i
] = orig_init
;
37137 orig_declv
= copy_node (declv
);
37138 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
37140 else if (orig_declv
)
37141 TREE_VEC_ELT (orig_declv
, i
) = decl
;
37143 if (i
== count
- 1)
37146 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37147 in between the collapsed for loops to be still considered perfectly
37148 nested. Hopefully the final version clarifies this.
37149 For now handle (multiple) {'s and empty statements. */
37150 cp_parser_parse_tentatively (parser
);
37153 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37155 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
37157 cp_lexer_consume_token (parser
->lexer
);
37160 else if (bracecount
37161 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37162 cp_lexer_consume_token (parser
->lexer
);
37165 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37166 error_at (loc
, "not enough for loops to collapse");
37167 collapse_err
= true;
37168 cp_parser_abort_tentative_parse (parser
);
37176 cp_parser_parse_definitely (parser
);
37177 nbraces
+= bracecount
;
37184 /* Note that we saved the original contents of this flag when we entered
37185 the structured block, and so we don't need to re-save it here. */
37186 parser
->in_statement
= IN_OMP_FOR
;
37188 /* Note that the grammar doesn't call for a structured block here,
37189 though the loop as a whole is a structured block. */
37192 body
= begin_omp_structured_block ();
37193 for (i
= 0; i
< count
; i
++)
37194 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
37195 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
37196 TREE_VEC_ELT (declv
, i
));
37199 body
= push_stmt_list ();
37200 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37202 body
= finish_omp_structured_block (body
);
37204 body
= pop_stmt_list (body
);
37206 if (declv
== NULL_TREE
)
37209 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
37210 incrv
, body
, pre_body
, &orig_inits
, clauses
);
37214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
37216 cp_lexer_consume_token (parser
->lexer
);
37219 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37220 cp_lexer_consume_token (parser
->lexer
);
37225 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37226 "collapsed loops not perfectly nested");
37228 collapse_err
= true;
37229 cp_parser_statement_seq_opt (parser
, NULL
);
37230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
37235 while (!for_block
->is_empty ())
37237 tree t
= for_block
->pop ();
37238 if (TREE_CODE (t
) == STATEMENT_LIST
)
37239 add_stmt (pop_stmt_list (t
));
37243 release_tree_vector (for_block
);
37248 /* Helper function for OpenMP parsing, split clauses and call
37249 finish_omp_clauses on each of the set of clauses afterwards. */
37252 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
37253 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
37256 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
37257 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
37259 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
37263 #pragma omp simd simd-clause[optseq] new-line
37266 #define OMP_SIMD_CLAUSE_MASK \
37267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37279 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37280 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37283 tree clauses
, sb
, ret
;
37285 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37287 strcat (p_name
, " simd");
37288 mask
|= OMP_SIMD_CLAUSE_MASK
;
37290 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37294 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
37295 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
37296 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
37297 OMP_CLAUSE_ORDERED
);
37298 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
37300 error_at (OMP_CLAUSE_LOCATION (c
),
37301 "%<ordered%> clause with parameter may not be specified "
37302 "on %qs construct", p_name
);
37303 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
37307 keep_next_level (true);
37308 sb
= begin_omp_structured_block ();
37309 save
= cp_parser_begin_omp_structured_block (parser
);
37311 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
37313 cp_parser_end_omp_structured_block (parser
, save
);
37314 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37320 #pragma omp for for-clause[optseq] new-line
37324 #pragma omp for simd for-simd-clause[optseq] new-line
37327 #define OMP_FOR_CLAUSE_MASK \
37328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37339 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
37340 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37343 tree clauses
, sb
, ret
;
37345 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37347 strcat (p_name
, " for");
37348 mask
|= OMP_FOR_CLAUSE_MASK
;
37349 /* parallel for{, simd} disallows nowait clause, but for
37350 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37351 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
37352 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37353 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37354 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37355 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
37357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37359 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37360 const char *p
= IDENTIFIER_POINTER (id
);
37362 if (strcmp (p
, "simd") == 0)
37364 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37365 if (cclauses
== NULL
)
37366 cclauses
= cclauses_buf
;
37368 cp_lexer_consume_token (parser
->lexer
);
37369 if (!flag_openmp
) /* flag_openmp_simd */
37370 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37372 sb
= begin_omp_structured_block ();
37373 save
= cp_parser_begin_omp_structured_block (parser
);
37374 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37376 cp_parser_end_omp_structured_block (parser
, save
);
37377 tree body
= finish_omp_structured_block (sb
);
37380 ret
= make_node (OMP_FOR
);
37381 TREE_TYPE (ret
) = void_type_node
;
37382 OMP_FOR_BODY (ret
) = body
;
37383 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37384 SET_EXPR_LOCATION (ret
, loc
);
37389 if (!flag_openmp
) /* flag_openmp_simd */
37391 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37395 /* Composite distribute parallel for disallows linear clause. */
37396 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37397 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
37399 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37403 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
37404 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37407 keep_next_level (true);
37408 sb
= begin_omp_structured_block ();
37409 save
= cp_parser_begin_omp_structured_block (parser
);
37411 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
37413 cp_parser_end_omp_structured_block (parser
, save
);
37414 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37419 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
37420 omp_clause_mask
, tree
*, bool *);
37423 # pragma omp master new-line
37424 structured-block */
37427 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
37428 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37431 tree clauses
, sb
, ret
;
37433 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37435 strcat (p_name
, " master");
37437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37439 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37440 const char *p
= IDENTIFIER_POINTER (id
);
37442 if (strcmp (p
, "taskloop") == 0)
37444 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37445 if (cclauses
== NULL
)
37446 cclauses
= cclauses_buf
;
37448 cp_lexer_consume_token (parser
->lexer
);
37449 if (!flag_openmp
) /* flag_openmp_simd */
37450 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37452 sb
= begin_omp_structured_block ();
37453 save
= cp_parser_begin_omp_structured_block (parser
);
37454 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37456 cp_parser_end_omp_structured_block (parser
, save
);
37457 tree body
= finish_omp_structured_block (sb
);
37460 return c_finish_omp_master (loc
, body
);
37463 if (!flag_openmp
) /* flag_openmp_simd */
37465 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37471 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37473 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
37476 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37478 return c_finish_omp_master (loc
,
37479 cp_parser_omp_structured_block (parser
, if_p
));
37483 # pragma omp ordered new-line
37487 # pragma omp ordered ordered-clauses new-line
37488 structured-block */
37490 #define OMP_ORDERED_CLAUSE_MASK \
37491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37494 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37495 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37498 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37499 enum pragma_context context
, bool *if_p
)
37501 location_t loc
= pragma_tok
->location
;
37503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37505 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37506 const char *p
= IDENTIFIER_POINTER (id
);
37508 if (strcmp (p
, "depend") == 0)
37510 if (!flag_openmp
) /* flag_openmp_simd */
37512 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37515 if (context
== pragma_stmt
)
37517 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37518 "%<depend%> clause may only be used in compound "
37520 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37524 = cp_parser_omp_all_clauses (parser
,
37525 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37526 "#pragma omp ordered", pragma_tok
);
37527 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37533 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37534 "#pragma omp ordered", pragma_tok
);
37536 if (!flag_openmp
/* flag_openmp_simd */
37537 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37540 c_finish_omp_ordered (loc
, clauses
,
37541 cp_parser_omp_structured_block (parser
, if_p
));
37548 { section-sequence }
37551 section-directive[opt] structured-block
37552 section-sequence section-directive structured-block */
37555 cp_parser_omp_sections_scope (cp_parser
*parser
)
37557 tree stmt
, substmt
;
37558 bool error_suppress
= false;
37561 matching_braces braces
;
37562 if (!braces
.require_open (parser
))
37565 stmt
= push_stmt_list ();
37567 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37568 != PRAGMA_OMP_SECTION
)
37570 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37571 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37572 add_stmt (substmt
);
37577 tok
= cp_lexer_peek_token (parser
->lexer
);
37578 if (tok
->type
== CPP_CLOSE_BRACE
)
37580 if (tok
->type
== CPP_EOF
)
37583 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37585 cp_lexer_consume_token (parser
->lexer
);
37586 cp_parser_require_pragma_eol (parser
, tok
);
37587 error_suppress
= false;
37589 else if (!error_suppress
)
37591 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37592 error_suppress
= true;
37595 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37596 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37597 add_stmt (substmt
);
37599 braces
.require_close (parser
);
37601 substmt
= pop_stmt_list (stmt
);
37603 stmt
= make_node (OMP_SECTIONS
);
37604 TREE_TYPE (stmt
) = void_type_node
;
37605 OMP_SECTIONS_BODY (stmt
) = substmt
;
37612 # pragma omp sections sections-clause[optseq] newline
37615 #define OMP_SECTIONS_CLAUSE_MASK \
37616 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37623 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37624 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37627 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37629 strcat (p_name
, " sections");
37630 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37632 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37634 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37638 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37639 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37642 ret
= cp_parser_omp_sections_scope (parser
);
37644 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37650 # pragma omp parallel parallel-clause[optseq] new-line
37652 # pragma omp parallel for parallel-for-clause[optseq] new-line
37654 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37658 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37659 structured-block */
37661 #define OMP_PARALLEL_CLAUSE_MASK \
37662 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37673 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37674 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37677 tree stmt
, clauses
, block
;
37679 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37681 strcat (p_name
, " parallel");
37682 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37683 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37684 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37685 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37686 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37688 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37690 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37691 if (cclauses
== NULL
)
37692 cclauses
= cclauses_buf
;
37694 cp_lexer_consume_token (parser
->lexer
);
37695 if (!flag_openmp
) /* flag_openmp_simd */
37696 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37698 block
= begin_omp_parallel ();
37699 save
= cp_parser_begin_omp_structured_block (parser
);
37700 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37702 cp_parser_end_omp_structured_block (parser
, save
);
37703 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37705 if (ret
== NULL_TREE
)
37707 OMP_PARALLEL_COMBINED (stmt
) = 1;
37710 /* When combined with distribute, parallel has to be followed by for.
37711 #pragma omp target parallel is allowed though. */
37713 && (mask
& (OMP_CLAUSE_MASK_1
37714 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37716 error_at (loc
, "expected %<for%> after %qs", p_name
);
37717 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37720 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37722 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37723 const char *p
= IDENTIFIER_POINTER (id
);
37724 if (strcmp (p
, "master") == 0)
37726 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37727 cclauses
= cclauses_buf
;
37729 cp_lexer_consume_token (parser
->lexer
);
37730 block
= begin_omp_parallel ();
37731 save
= cp_parser_begin_omp_structured_block (parser
);
37732 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37734 cp_parser_end_omp_structured_block (parser
, save
);
37735 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37737 OMP_PARALLEL_COMBINED (stmt
) = 1;
37738 if (ret
== NULL_TREE
)
37742 else if (!flag_openmp
) /* flag_openmp_simd */
37744 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37747 else if (strcmp (p
, "sections") == 0)
37749 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37750 cclauses
= cclauses_buf
;
37752 cp_lexer_consume_token (parser
->lexer
);
37753 block
= begin_omp_parallel ();
37754 save
= cp_parser_begin_omp_structured_block (parser
);
37755 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37756 cp_parser_end_omp_structured_block (parser
, save
);
37757 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37759 OMP_PARALLEL_COMBINED (stmt
) = 1;
37763 else if (!flag_openmp
) /* flag_openmp_simd */
37765 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37769 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37773 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37774 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37777 block
= begin_omp_parallel ();
37778 save
= cp_parser_begin_omp_structured_block (parser
);
37779 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37780 cp_parser_end_omp_structured_block (parser
, save
);
37781 stmt
= finish_omp_parallel (clauses
, block
);
37786 # pragma omp single single-clause[optseq] new-line
37787 structured-block */
37789 #define OMP_SINGLE_CLAUSE_MASK \
37790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37796 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37798 tree stmt
= make_node (OMP_SINGLE
);
37799 TREE_TYPE (stmt
) = void_type_node
;
37800 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37802 OMP_SINGLE_CLAUSES (stmt
)
37803 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37804 "#pragma omp single", pragma_tok
);
37805 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37807 return add_stmt (stmt
);
37811 # pragma omp task task-clause[optseq] new-line
37812 structured-block */
37814 #define OMP_TASK_CLAUSE_MASK \
37815 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37828 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37830 tree clauses
, block
;
37833 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37834 "#pragma omp task", pragma_tok
);
37835 block
= begin_omp_task ();
37836 save
= cp_parser_begin_omp_structured_block (parser
);
37837 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37838 cp_parser_end_omp_structured_block (parser
, save
);
37839 return finish_omp_task (clauses
, block
);
37843 # pragma omp taskwait new-line
37846 # pragma omp taskwait taskwait-clause[opt] new-line */
37848 #define OMP_TASKWAIT_CLAUSE_MASK \
37849 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37852 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37855 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37856 "#pragma omp taskwait", pragma_tok
);
37860 tree stmt
= make_node (OMP_TASK
);
37861 TREE_TYPE (stmt
) = void_node
;
37862 OMP_TASK_CLAUSES (stmt
) = clauses
;
37863 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37864 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37868 finish_omp_taskwait ();
37872 # pragma omp taskyield new-line */
37875 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37877 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37878 finish_omp_taskyield ();
37882 # pragma omp taskgroup new-line
37886 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37888 #define OMP_TASKGROUP_CLAUSE_MASK \
37889 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37892 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37895 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37896 "#pragma omp taskgroup", pragma_tok
);
37897 return c_finish_omp_taskgroup (input_location
,
37898 cp_parser_omp_structured_block (parser
,
37905 # pragma omp threadprivate (variable-list) */
37908 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37912 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37913 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37915 finish_omp_threadprivate (vars
);
37919 # pragma omp cancel cancel-clause[optseq] new-line */
37921 #define OMP_CANCEL_CLAUSE_MASK \
37922 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37929 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37931 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37932 "#pragma omp cancel", pragma_tok
);
37933 finish_omp_cancel (clauses
);
37937 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37939 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37946 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37947 enum pragma_context context
)
37950 bool point_seen
= false;
37952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37954 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37955 const char *p
= IDENTIFIER_POINTER (id
);
37957 if (strcmp (p
, "point") == 0)
37959 cp_lexer_consume_token (parser
->lexer
);
37965 cp_parser_error (parser
, "expected %<point%>");
37966 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37970 if (context
!= pragma_compound
)
37972 if (context
== pragma_stmt
)
37973 error_at (pragma_tok
->location
,
37974 "%<#pragma %s%> may only be used in compound statements",
37975 "omp cancellation point");
37977 cp_parser_error (parser
, "expected declaration specifiers");
37978 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37982 clauses
= cp_parser_omp_all_clauses (parser
,
37983 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37984 "#pragma omp cancellation point",
37986 finish_omp_cancellation_point (clauses
);
37990 #pragma omp distribute distribute-clause[optseq] new-line
37993 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37994 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
38001 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
38002 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38005 tree clauses
, sb
, ret
;
38007 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38009 strcat (p_name
, " distribute");
38010 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
38012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38014 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38015 const char *p
= IDENTIFIER_POINTER (id
);
38017 bool parallel
= false;
38019 if (strcmp (p
, "simd") == 0)
38022 parallel
= strcmp (p
, "parallel") == 0;
38023 if (parallel
|| simd
)
38025 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38026 if (cclauses
== NULL
)
38027 cclauses
= cclauses_buf
;
38028 cp_lexer_consume_token (parser
->lexer
);
38029 if (!flag_openmp
) /* flag_openmp_simd */
38032 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38035 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
38038 sb
= begin_omp_structured_block ();
38039 save
= cp_parser_begin_omp_structured_block (parser
);
38041 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38044 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
38046 cp_parser_end_omp_structured_block (parser
, save
);
38047 tree body
= finish_omp_structured_block (sb
);
38050 ret
= make_node (OMP_DISTRIBUTE
);
38051 TREE_TYPE (ret
) = void_type_node
;
38052 OMP_FOR_BODY (ret
) = body
;
38053 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
38054 SET_EXPR_LOCATION (ret
, loc
);
38059 if (!flag_openmp
) /* flag_openmp_simd */
38061 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38065 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38069 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
38070 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
38073 keep_next_level (true);
38074 sb
= begin_omp_structured_block ();
38075 save
= cp_parser_begin_omp_structured_block (parser
);
38077 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
38079 cp_parser_end_omp_structured_block (parser
, save
);
38080 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
38086 # pragma omp teams teams-clause[optseq] new-line
38087 structured-block */
38089 #define OMP_TEAMS_CLAUSE_MASK \
38090 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38099 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
38100 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38103 tree clauses
, sb
, ret
;
38105 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38107 strcat (p_name
, " teams");
38108 mask
|= OMP_TEAMS_CLAUSE_MASK
;
38110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38112 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38113 const char *p
= IDENTIFIER_POINTER (id
);
38114 if (strcmp (p
, "distribute") == 0)
38116 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38117 if (cclauses
== NULL
)
38118 cclauses
= cclauses_buf
;
38120 cp_lexer_consume_token (parser
->lexer
);
38121 if (!flag_openmp
) /* flag_openmp_simd */
38122 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
38124 keep_next_level (true);
38125 sb
= begin_omp_structured_block ();
38126 save
= cp_parser_begin_omp_structured_block (parser
);
38127 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
38129 cp_parser_end_omp_structured_block (parser
, save
);
38130 tree body
= finish_omp_structured_block (sb
);
38133 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38134 ret
= make_node (OMP_TEAMS
);
38135 TREE_TYPE (ret
) = void_type_node
;
38136 OMP_TEAMS_CLAUSES (ret
) = clauses
;
38137 OMP_TEAMS_BODY (ret
) = body
;
38138 OMP_TEAMS_COMBINED (ret
) = 1;
38139 SET_EXPR_LOCATION (ret
, loc
);
38140 return add_stmt (ret
);
38143 if (!flag_openmp
) /* flag_openmp_simd */
38145 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38149 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38153 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
38154 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38157 tree stmt
= make_node (OMP_TEAMS
);
38158 TREE_TYPE (stmt
) = void_type_node
;
38159 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
38160 keep_next_level (true);
38161 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38162 SET_EXPR_LOCATION (stmt
, loc
);
38164 return add_stmt (stmt
);
38168 # pragma omp target data target-data-clause[optseq] new-line
38169 structured-block */
38171 #define OMP_TARGET_DATA_CLAUSE_MASK \
38172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38178 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38181 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
38182 "#pragma omp target data", pragma_tok
);
38184 for (tree
*pc
= &clauses
; *pc
;)
38186 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38187 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38190 case GOMP_MAP_ALWAYS_TO
:
38191 case GOMP_MAP_FROM
:
38192 case GOMP_MAP_ALWAYS_FROM
:
38193 case GOMP_MAP_TOFROM
:
38194 case GOMP_MAP_ALWAYS_TOFROM
:
38195 case GOMP_MAP_ALLOC
:
38198 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38199 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38200 case GOMP_MAP_ALWAYS_POINTER
:
38204 error_at (OMP_CLAUSE_LOCATION (*pc
),
38205 "%<#pragma omp target data%> with map-type other "
38206 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38207 "on %<map%> clause");
38208 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38211 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
38213 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38219 error_at (pragma_tok
->location
,
38220 "%<#pragma omp target data%> must contain at least "
38221 "one %<map%> or %<use_device_ptr%> clause");
38225 tree stmt
= make_node (OMP_TARGET_DATA
);
38226 TREE_TYPE (stmt
) = void_type_node
;
38227 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
38229 keep_next_level (true);
38230 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38232 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38233 return add_stmt (stmt
);
38237 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38238 structured-block */
38240 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38241 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38248 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38249 enum pragma_context context
)
38251 bool data_seen
= false;
38252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38254 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38255 const char *p
= IDENTIFIER_POINTER (id
);
38257 if (strcmp (p
, "data") == 0)
38259 cp_lexer_consume_token (parser
->lexer
);
38265 cp_parser_error (parser
, "expected %<data%>");
38266 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38270 if (context
== pragma_stmt
)
38272 error_at (pragma_tok
->location
,
38273 "%<#pragma %s%> may only be used in compound statements",
38274 "omp target enter data");
38275 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38280 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
38281 "#pragma omp target enter data", pragma_tok
);
38283 for (tree
*pc
= &clauses
; *pc
;)
38285 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38286 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38289 case GOMP_MAP_ALWAYS_TO
:
38290 case GOMP_MAP_ALLOC
:
38293 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38294 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38295 case GOMP_MAP_ALWAYS_POINTER
:
38299 error_at (OMP_CLAUSE_LOCATION (*pc
),
38300 "%<#pragma omp target enter data%> with map-type other "
38301 "than %<to%> or %<alloc%> on %<map%> clause");
38302 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38305 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38311 error_at (pragma_tok
->location
,
38312 "%<#pragma omp target enter data%> must contain at least "
38313 "one %<map%> clause");
38317 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
38318 TREE_TYPE (stmt
) = void_type_node
;
38319 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
38320 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38321 return add_stmt (stmt
);
38325 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38326 structured-block */
38328 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38329 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38336 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38337 enum pragma_context context
)
38339 bool data_seen
= false;
38340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38342 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38343 const char *p
= IDENTIFIER_POINTER (id
);
38345 if (strcmp (p
, "data") == 0)
38347 cp_lexer_consume_token (parser
->lexer
);
38353 cp_parser_error (parser
, "expected %<data%>");
38354 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38358 if (context
== pragma_stmt
)
38360 error_at (pragma_tok
->location
,
38361 "%<#pragma %s%> may only be used in compound statements",
38362 "omp target exit data");
38363 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38368 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
38369 "#pragma omp target exit data", pragma_tok
);
38371 for (tree
*pc
= &clauses
; *pc
;)
38373 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38374 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38376 case GOMP_MAP_FROM
:
38377 case GOMP_MAP_ALWAYS_FROM
:
38378 case GOMP_MAP_RELEASE
:
38379 case GOMP_MAP_DELETE
:
38382 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38383 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38384 case GOMP_MAP_ALWAYS_POINTER
:
38388 error_at (OMP_CLAUSE_LOCATION (*pc
),
38389 "%<#pragma omp target exit data%> with map-type other "
38390 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38392 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38395 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38401 error_at (pragma_tok
->location
,
38402 "%<#pragma omp target exit data%> must contain at least "
38403 "one %<map%> clause");
38407 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
38408 TREE_TYPE (stmt
) = void_type_node
;
38409 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
38410 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38411 return add_stmt (stmt
);
38415 # pragma omp target update target-update-clause[optseq] new-line */
38417 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38426 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
38427 enum pragma_context context
)
38429 if (context
== pragma_stmt
)
38431 error_at (pragma_tok
->location
,
38432 "%<#pragma %s%> may only be used in compound statements",
38433 "omp target update");
38434 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38439 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
38440 "#pragma omp target update", pragma_tok
);
38441 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
38442 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
38444 error_at (pragma_tok
->location
,
38445 "%<#pragma omp target update%> must contain at least one "
38446 "%<from%> or %<to%> clauses");
38450 tree stmt
= make_node (OMP_TARGET_UPDATE
);
38451 TREE_TYPE (stmt
) = void_type_node
;
38452 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
38453 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38459 # pragma omp target target-clause[optseq] new-line
38460 structured-block */
38462 #define OMP_TARGET_CLAUSE_MASK \
38463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38474 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
38475 enum pragma_context context
, bool *if_p
)
38477 tree
*pc
= NULL
, stmt
;
38481 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
38483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38485 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38486 const char *p
= IDENTIFIER_POINTER (id
);
38487 enum tree_code ccode
= ERROR_MARK
;
38489 if (strcmp (p
, "teams") == 0)
38491 else if (strcmp (p
, "parallel") == 0)
38492 ccode
= OMP_PARALLEL
;
38493 else if (strcmp (p
, "simd") == 0)
38495 if (ccode
!= ERROR_MARK
)
38497 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38498 char p_name
[sizeof ("#pragma omp target teams distribute "
38499 "parallel for simd")];
38501 cp_lexer_consume_token (parser
->lexer
);
38502 strcpy (p_name
, "#pragma omp target");
38503 if (!flag_openmp
) /* flag_openmp_simd */
38509 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38510 OMP_TARGET_CLAUSE_MASK
,
38514 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38515 OMP_TARGET_CLAUSE_MASK
,
38519 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38520 OMP_TARGET_CLAUSE_MASK
,
38524 gcc_unreachable ();
38526 return stmt
!= NULL_TREE
;
38528 keep_next_level (true);
38529 tree sb
= begin_omp_structured_block (), ret
;
38530 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38534 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38535 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38539 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38540 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38544 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38545 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38549 gcc_unreachable ();
38551 cp_parser_end_omp_structured_block (parser
, save
);
38552 tree body
= finish_omp_structured_block (sb
);
38553 if (ret
== NULL_TREE
)
38555 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38557 /* For combined target teams, ensure the num_teams and
38558 thread_limit clause expressions are evaluated on the host,
38559 before entering the target construct. */
38561 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38562 c
; c
= OMP_CLAUSE_CHAIN (c
))
38563 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38564 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38565 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38567 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38568 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38569 if (expr
== error_mark_node
)
38571 tree tmp
= TARGET_EXPR_SLOT (expr
);
38573 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38574 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38575 OMP_CLAUSE_FIRSTPRIVATE
);
38576 OMP_CLAUSE_DECL (tc
) = tmp
;
38577 OMP_CLAUSE_CHAIN (tc
)
38578 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38579 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38582 tree stmt
= make_node (OMP_TARGET
);
38583 TREE_TYPE (stmt
) = void_type_node
;
38584 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38585 OMP_TARGET_BODY (stmt
) = body
;
38586 OMP_TARGET_COMBINED (stmt
) = 1;
38587 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38589 pc
= &OMP_TARGET_CLAUSES (stmt
);
38590 goto check_clauses
;
38592 else if (!flag_openmp
) /* flag_openmp_simd */
38594 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38597 else if (strcmp (p
, "data") == 0)
38599 cp_lexer_consume_token (parser
->lexer
);
38600 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38603 else if (strcmp (p
, "enter") == 0)
38605 cp_lexer_consume_token (parser
->lexer
);
38606 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38609 else if (strcmp (p
, "exit") == 0)
38611 cp_lexer_consume_token (parser
->lexer
);
38612 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38615 else if (strcmp (p
, "update") == 0)
38617 cp_lexer_consume_token (parser
->lexer
);
38618 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38621 if (!flag_openmp
) /* flag_openmp_simd */
38623 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38627 stmt
= make_node (OMP_TARGET
);
38628 TREE_TYPE (stmt
) = void_type_node
;
38630 OMP_TARGET_CLAUSES (stmt
)
38631 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38632 "#pragma omp target", pragma_tok
);
38633 pc
= &OMP_TARGET_CLAUSES (stmt
);
38634 keep_next_level (true);
38635 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38637 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38643 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38644 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38647 case GOMP_MAP_ALWAYS_TO
:
38648 case GOMP_MAP_FROM
:
38649 case GOMP_MAP_ALWAYS_FROM
:
38650 case GOMP_MAP_TOFROM
:
38651 case GOMP_MAP_ALWAYS_TOFROM
:
38652 case GOMP_MAP_ALLOC
:
38653 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38654 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38655 case GOMP_MAP_ALWAYS_POINTER
:
38658 error_at (OMP_CLAUSE_LOCATION (*pc
),
38659 "%<#pragma omp target%> with map-type other "
38660 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38661 "on %<map%> clause");
38662 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38665 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38671 # pragma acc cache (variable-list) new-line
38675 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38677 tree stmt
, clauses
;
38679 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38680 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38682 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38684 stmt
= make_node (OACC_CACHE
);
38685 TREE_TYPE (stmt
) = void_type_node
;
38686 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38687 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38694 # pragma acc data oacc-data-clause[optseq] new-line
38695 structured-block */
38697 #define OACC_DATA_CLAUSE_MASK \
38698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38707 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38709 tree stmt
, clauses
, block
;
38712 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38713 "#pragma acc data", pragma_tok
);
38715 block
= begin_omp_parallel ();
38716 save
= cp_parser_begin_omp_structured_block (parser
);
38717 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38718 cp_parser_end_omp_structured_block (parser
, save
);
38719 stmt
= finish_oacc_data (clauses
, block
);
38724 # pragma acc host_data <clauses> new-line
38725 structured-block */
38727 #define OACC_HOST_DATA_CLAUSE_MASK \
38728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38731 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38733 tree stmt
, clauses
, block
;
38736 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38737 "#pragma acc host_data", pragma_tok
);
38739 block
= begin_omp_parallel ();
38740 save
= cp_parser_begin_omp_structured_block (parser
);
38741 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38742 cp_parser_end_omp_structured_block (parser
, save
);
38743 stmt
= finish_oacc_host_data (clauses
, block
);
38748 # pragma acc declare oacc-data-clause[optseq] new-line
38751 #define OACC_DECLARE_CLAUSE_MASK \
38752 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38762 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38764 tree clauses
, stmt
;
38765 bool error
= false;
38767 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38768 "#pragma acc declare", pragma_tok
, true);
38771 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38773 error_at (pragma_tok
->location
,
38774 "no valid clauses specified in %<#pragma acc declare%>");
38778 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38780 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38781 tree decl
= OMP_CLAUSE_DECL (t
);
38782 if (!DECL_P (decl
))
38784 error_at (loc
, "array section in %<#pragma acc declare%>");
38788 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38789 switch (OMP_CLAUSE_MAP_KIND (t
))
38791 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38792 case GOMP_MAP_ALLOC
:
38794 case GOMP_MAP_FORCE_DEVICEPTR
:
38795 case GOMP_MAP_DEVICE_RESIDENT
:
38798 case GOMP_MAP_LINK
:
38799 if (!global_bindings_p ()
38800 && (TREE_STATIC (decl
)
38801 || !DECL_EXTERNAL (decl
)))
38804 "%qD must be a global variable in "
38805 "%<#pragma acc declare link%>",
38813 if (global_bindings_p ())
38815 error_at (loc
, "invalid OpenACC clause at file scope");
38819 if (DECL_EXTERNAL (decl
))
38822 "invalid use of %<extern%> variable %qD "
38823 "in %<#pragma acc declare%>", decl
);
38827 else if (TREE_PUBLIC (decl
))
38830 "invalid use of %<global%> variable %qD "
38831 "in %<#pragma acc declare%>", decl
);
38838 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38839 || lookup_attribute ("omp declare target link",
38840 DECL_ATTRIBUTES (decl
)))
38842 error_at (loc
, "variable %qD used more than once with "
38843 "%<#pragma acc declare%>", decl
);
38852 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38853 id
= get_identifier ("omp declare target link");
38855 id
= get_identifier ("omp declare target");
38857 DECL_ATTRIBUTES (decl
)
38858 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38859 if (global_bindings_p ())
38861 symtab_node
*node
= symtab_node::get (decl
);
38864 node
->offloadable
= 1;
38865 if (ENABLE_OFFLOADING
)
38867 g
->have_offload
= true;
38868 if (is_a
<varpool_node
*> (node
))
38869 vec_safe_push (offload_vars
, decl
);
38876 if (error
|| global_bindings_p ())
38879 stmt
= make_node (OACC_DECLARE
);
38880 TREE_TYPE (stmt
) = void_type_node
;
38881 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38882 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38890 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38894 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38896 LOC is the location of the #pragma token.
38899 #define OACC_ENTER_DATA_CLAUSE_MASK \
38900 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38906 #define OACC_EXIT_DATA_CLAUSE_MASK \
38907 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38915 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38918 location_t loc
= pragma_tok
->location
;
38919 tree stmt
, clauses
;
38920 const char *p
= "";
38922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38923 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38925 if (strcmp (p
, "data") != 0)
38927 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38928 enter
? "enter" : "exit");
38929 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38933 cp_lexer_consume_token (parser
->lexer
);
38936 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38937 "#pragma acc enter data", pragma_tok
);
38939 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38940 "#pragma acc exit data", pragma_tok
);
38942 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38944 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38945 enter
? "enter" : "exit");
38949 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38950 TREE_TYPE (stmt
) = void_type_node
;
38951 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38952 SET_EXPR_LOCATION (stmt
, loc
);
38958 # pragma acc loop oacc-loop-clause[optseq] new-line
38959 structured-block */
38961 #define OACC_LOOP_CLAUSE_MASK \
38962 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38974 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38975 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38977 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38979 strcat (p_name
, " loop");
38980 mask
|= OACC_LOOP_CLAUSE_MASK
;
38982 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38986 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38988 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38990 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38993 tree block
= begin_omp_structured_block ();
38994 int save
= cp_parser_begin_omp_structured_block (parser
);
38995 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38996 cp_parser_end_omp_structured_block (parser
, save
);
38997 add_stmt (finish_omp_structured_block (block
));
39003 # pragma acc kernels oacc-kernels-clause[optseq] new-line
39008 # pragma acc parallel oacc-parallel-clause[optseq] new-line
39012 #define OACC_KERNELS_CLAUSE_MASK \
39013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39027 #define OACC_PARALLEL_CLAUSE_MASK \
39028 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39046 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
39047 char *p_name
, bool *if_p
)
39049 omp_clause_mask mask
;
39050 enum tree_code code
;
39051 switch (cp_parser_pragma_kind (pragma_tok
))
39053 case PRAGMA_OACC_KERNELS
:
39054 strcat (p_name
, " kernels");
39055 mask
= OACC_KERNELS_CLAUSE_MASK
;
39056 code
= OACC_KERNELS
;
39058 case PRAGMA_OACC_PARALLEL
:
39059 strcat (p_name
, " parallel");
39060 mask
= OACC_PARALLEL_CLAUSE_MASK
;
39061 code
= OACC_PARALLEL
;
39064 gcc_unreachable ();
39067 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39070 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
39071 if (strcmp (p
, "loop") == 0)
39073 cp_lexer_consume_token (parser
->lexer
);
39074 tree block
= begin_omp_parallel ();
39076 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
39078 protected_set_expr_location (stmt
, pragma_tok
->location
);
39079 return finish_omp_construct (code
, block
, clauses
);
39083 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
39085 tree block
= begin_omp_parallel ();
39086 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
39087 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39088 cp_parser_end_omp_structured_block (parser
, save
);
39089 return finish_omp_construct (code
, block
, clauses
);
39093 # pragma acc update oacc-update-clause[optseq] new-line
39096 #define OACC_UPDATE_CLAUSE_MASK \
39097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39105 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
39107 tree stmt
, clauses
;
39109 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
39110 "#pragma acc update", pragma_tok
);
39112 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
39114 error_at (pragma_tok
->location
,
39115 "%<#pragma acc update%> must contain at least one "
39116 "%<device%> or %<host%> or %<self%> clause");
39120 stmt
= make_node (OACC_UPDATE
);
39121 TREE_TYPE (stmt
) = void_type_node
;
39122 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
39123 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39129 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39131 LOC is the location of the #pragma token.
39134 #define OACC_WAIT_CLAUSE_MASK \
39135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39138 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
39140 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
39141 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39143 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
39144 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
39146 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
39147 "#pragma acc wait", pragma_tok
);
39149 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
39150 stmt
= finish_expr_stmt (stmt
);
39156 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39158 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39167 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
39168 enum pragma_context context
)
39170 bool first_p
= parser
->omp_declare_simd
== NULL
;
39171 cp_omp_declare_simd_data data
;
39174 data
.error_seen
= false;
39175 data
.fndecl_seen
= false;
39176 data
.tokens
= vNULL
;
39177 data
.clauses
= NULL_TREE
;
39178 /* It is safe to take the address of a local variable; it will only be
39179 used while this scope is live. */
39180 parser
->omp_declare_simd
= &data
;
39183 /* Store away all pragma tokens. */
39184 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39185 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39186 cp_lexer_consume_token (parser
->lexer
);
39187 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39188 parser
->omp_declare_simd
->error_seen
= true;
39189 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39190 struct cp_token_cache
*cp
39191 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39192 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
39196 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39197 cp_parser_pragma (parser
, context
, NULL
);
39200 case pragma_external
:
39201 cp_parser_declaration (parser
);
39203 case pragma_member
:
39204 cp_parser_member_declaration (parser
);
39206 case pragma_objc_icode
:
39207 cp_parser_block_declaration (parser
, /*statement_p=*/false);
39210 cp_parser_declaration_statement (parser
);
39213 if (parser
->omp_declare_simd
39214 && !parser
->omp_declare_simd
->error_seen
39215 && !parser
->omp_declare_simd
->fndecl_seen
)
39216 error_at (pragma_tok
->location
,
39217 "%<#pragma omp declare simd%> not immediately followed by "
39218 "function declaration or definition");
39219 data
.tokens
.release ();
39220 parser
->omp_declare_simd
= NULL
;
39224 /* Finalize #pragma omp declare simd clauses after direct declarator has
39225 been parsed, and put that into "omp declare simd" attribute. */
39228 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
39230 struct cp_token_cache
*ce
;
39231 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
39234 if (!data
->error_seen
&& data
->fndecl_seen
)
39236 error ("%<#pragma omp declare simd%> not immediately followed by "
39237 "a single function declaration or definition");
39238 data
->error_seen
= true;
39240 if (data
->error_seen
)
39243 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
39247 cp_parser_push_lexer_for_tokens (parser
, ce
);
39248 parser
->lexer
->in_pragma
= true;
39249 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39250 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39251 cp_lexer_consume_token (parser
->lexer
);
39252 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
39253 "#pragma omp declare simd", pragma_tok
);
39254 cp_parser_pop_lexer (parser
);
39256 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
39257 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
39258 TREE_CHAIN (c
) = attrs
;
39259 if (processing_template_decl
)
39260 ATTR_IS_DEPENDENT (c
) = 1;
39264 data
->fndecl_seen
= true;
39270 # pragma omp declare target new-line
39271 declarations and definitions
39272 # pragma omp end declare target new-line
39275 # pragma omp declare target ( extended-list ) new-line
39277 # pragma omp declare target declare-target-clauses[seq] new-line */
39279 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39284 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39286 tree clauses
= NULL_TREE
;
39287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39289 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
39290 "#pragma omp declare target", pragma_tok
);
39291 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39293 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
39295 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39296 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39300 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39301 scope_chain
->omp_declare_target_attribute
++;
39304 if (scope_chain
->omp_declare_target_attribute
)
39305 error_at (pragma_tok
->location
,
39306 "%<#pragma omp declare target%> with clauses in between "
39307 "%<#pragma omp declare target%> without clauses and "
39308 "%<#pragma omp end declare target%>");
39309 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39311 tree t
= OMP_CLAUSE_DECL (c
), id
;
39312 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
39313 tree at2
= lookup_attribute ("omp declare target link",
39314 DECL_ATTRIBUTES (t
));
39315 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
39317 id
= get_identifier ("omp declare target link");
39318 std::swap (at1
, at2
);
39321 id
= get_identifier ("omp declare target");
39324 error_at (OMP_CLAUSE_LOCATION (c
),
39325 "%qD specified both in declare target %<link%> and %<to%>"
39331 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
39332 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
39335 symtab_node
*node
= symtab_node::get (t
);
39338 node
->offloadable
= 1;
39339 if (ENABLE_OFFLOADING
)
39341 g
->have_offload
= true;
39342 if (is_a
<varpool_node
*> (node
))
39343 vec_safe_push (offload_vars
, t
);
39351 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39353 const char *p
= "";
39354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39356 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39357 p
= IDENTIFIER_POINTER (id
);
39359 if (strcmp (p
, "declare") == 0)
39361 cp_lexer_consume_token (parser
->lexer
);
39363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39365 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39366 p
= IDENTIFIER_POINTER (id
);
39368 if (strcmp (p
, "target") == 0)
39369 cp_lexer_consume_token (parser
->lexer
);
39372 cp_parser_error (parser
, "expected %<target%>");
39373 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39379 cp_parser_error (parser
, "expected %<declare%>");
39380 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39383 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39384 if (!scope_chain
->omp_declare_target_attribute
)
39385 error_at (pragma_tok
->location
,
39386 "%<#pragma omp end declare target%> without corresponding "
39387 "%<#pragma omp declare target%>");
39389 scope_chain
->omp_declare_target_attribute
--;
39392 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39393 expression and optional initializer clause of
39394 #pragma omp declare reduction. We store the expression(s) as
39395 either 3, 6 or 7 special statements inside of the artificial function's
39396 body. The first two statements are DECL_EXPRs for the artificial
39397 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39398 expression that uses those variables.
39399 If there was any INITIALIZER clause, this is followed by further statements,
39400 the fourth and fifth statements are DECL_EXPRs for the artificial
39401 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39402 constructor variant (first token after open paren is not omp_priv),
39403 then the sixth statement is a statement with the function call expression
39404 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39405 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39406 to initialize the OMP_PRIV artificial variable and there is seventh
39407 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39410 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
39412 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
39413 gcc_assert (TYPE_REF_P (type
));
39414 type
= TREE_TYPE (type
);
39415 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
39416 DECL_ARTIFICIAL (omp_out
) = 1;
39417 pushdecl (omp_out
);
39418 add_decl_expr (omp_out
);
39419 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
39420 DECL_ARTIFICIAL (omp_in
) = 1;
39422 add_decl_expr (omp_in
);
39424 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
39426 keep_next_level (true);
39427 tree block
= begin_omp_structured_block ();
39428 combiner
= cp_parser_expression (parser
);
39429 finish_expr_stmt (combiner
);
39430 block
= finish_omp_structured_block (block
);
39433 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
39436 const char *p
= "";
39437 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39439 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39440 p
= IDENTIFIER_POINTER (id
);
39443 if (strcmp (p
, "initializer") == 0)
39445 cp_lexer_consume_token (parser
->lexer
);
39446 matching_parens parens
;
39447 if (!parens
.require_open (parser
))
39451 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39453 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39454 p
= IDENTIFIER_POINTER (id
);
39457 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
39458 DECL_ARTIFICIAL (omp_priv
) = 1;
39459 pushdecl (omp_priv
);
39460 add_decl_expr (omp_priv
);
39461 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
39462 DECL_ARTIFICIAL (omp_orig
) = 1;
39463 pushdecl (omp_orig
);
39464 add_decl_expr (omp_orig
);
39466 keep_next_level (true);
39467 block
= begin_omp_structured_block ();
39470 if (strcmp (p
, "omp_priv") == 0)
39472 bool is_direct_init
, is_non_constant_init
;
39474 cp_lexer_consume_token (parser
->lexer
);
39475 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
39477 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39478 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
39480 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
39481 == CPP_CLOSE_PAREN
))
39483 finish_omp_structured_block (block
);
39484 error ("invalid initializer clause");
39487 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
39488 &is_non_constant_init
);
39489 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
39490 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
39494 cp_parser_parse_tentatively (parser
);
39495 /* Don't create location wrapper nodes here. */
39496 auto_suppress_location_wrappers sentinel
;
39497 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39498 /*check_dependency_p=*/true,
39499 /*template_p=*/NULL
,
39500 /*declarator_p=*/false,
39501 /*optional_p=*/false);
39502 vec
<tree
, va_gc
> *args
;
39503 if (fn_name
== error_mark_node
39504 || cp_parser_error_occurred (parser
)
39505 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39506 || ((args
= cp_parser_parenthesized_expression_list
39507 (parser
, non_attr
, /*cast_p=*/false,
39508 /*allow_expansion_p=*/true,
39509 /*non_constant_p=*/NULL
)),
39510 cp_parser_error_occurred (parser
)))
39512 finish_omp_structured_block (block
);
39513 cp_parser_abort_tentative_parse (parser
);
39514 cp_parser_error (parser
, "expected id-expression (arguments)");
39519 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39520 if (arg
== omp_priv
39521 || (TREE_CODE (arg
) == ADDR_EXPR
39522 && TREE_OPERAND (arg
, 0) == omp_priv
))
39524 cp_parser_abort_tentative_parse (parser
);
39525 if (arg
== NULL_TREE
)
39526 error ("one of the initializer call arguments should be %<omp_priv%>"
39527 " or %<&omp_priv%>");
39528 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39530 finish_expr_stmt (initializer
);
39533 block
= finish_omp_structured_block (block
);
39534 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39538 add_decl_expr (omp_orig
);
39540 if (!parens
.require_close (parser
))
39544 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39545 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39552 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39553 initializer-clause[opt] new-line
39555 initializer-clause:
39556 initializer (omp_priv initializer)
39557 initializer (function-name (argument-list)) */
39560 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39561 enum pragma_context
)
39563 auto_vec
<tree
> types
;
39564 enum tree_code reduc_code
= ERROR_MARK
;
39565 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39567 cp_token
*first_token
;
39568 cp_token_cache
*cp
;
39572 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39573 p
= obstack_alloc (&declarator_obstack
, 0);
39575 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39578 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39581 reduc_code
= PLUS_EXPR
;
39584 reduc_code
= MULT_EXPR
;
39587 reduc_code
= MINUS_EXPR
;
39590 reduc_code
= BIT_AND_EXPR
;
39593 reduc_code
= BIT_XOR_EXPR
;
39596 reduc_code
= BIT_IOR_EXPR
;
39599 reduc_code
= TRUTH_ANDIF_EXPR
;
39602 reduc_code
= TRUTH_ORIF_EXPR
;
39605 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39608 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39609 "%<|%>, %<&&%>, %<||%> or identifier");
39613 if (reduc_code
!= ERROR_MARK
)
39614 cp_lexer_consume_token (parser
->lexer
);
39616 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39617 if (reduc_id
== error_mark_node
)
39620 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39623 /* Types may not be defined in declare reduction type list. */
39624 const char *saved_message
;
39625 saved_message
= parser
->type_definition_forbidden_message
;
39626 parser
->type_definition_forbidden_message
39627 = G_("types may not be defined in declare reduction type list");
39628 bool saved_colon_corrects_to_scope_p
;
39629 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39630 parser
->colon_corrects_to_scope_p
= false;
39631 bool saved_colon_doesnt_start_class_def_p
;
39632 saved_colon_doesnt_start_class_def_p
39633 = parser
->colon_doesnt_start_class_def_p
;
39634 parser
->colon_doesnt_start_class_def_p
= true;
39638 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39639 type
= cp_parser_type_id (parser
);
39640 if (type
== error_mark_node
)
39642 else if (ARITHMETIC_TYPE_P (type
)
39643 && (orig_reduc_id
== NULL_TREE
39644 || (TREE_CODE (type
) != COMPLEX_TYPE
39645 && (id_equal (orig_reduc_id
, "min")
39646 || id_equal (orig_reduc_id
, "max")))))
39647 error_at (loc
, "predeclared arithmetic type %qT in "
39648 "%<#pragma omp declare reduction%>", type
);
39649 else if (TREE_CODE (type
) == FUNCTION_TYPE
39650 || TREE_CODE (type
) == METHOD_TYPE
39651 || TREE_CODE (type
) == ARRAY_TYPE
)
39652 error_at (loc
, "function or array type %qT in "
39653 "%<#pragma omp declare reduction%>", type
);
39654 else if (TYPE_REF_P (type
))
39655 error_at (loc
, "reference type %qT in "
39656 "%<#pragma omp declare reduction%>", type
);
39657 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39658 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39659 "%<#pragma omp declare reduction%>", type
);
39661 types
.safe_push (type
);
39663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39664 cp_lexer_consume_token (parser
->lexer
);
39669 /* Restore the saved message. */
39670 parser
->type_definition_forbidden_message
= saved_message
;
39671 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39672 parser
->colon_doesnt_start_class_def_p
39673 = saved_colon_doesnt_start_class_def_p
;
39675 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39676 || types
.is_empty ())
39679 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39683 first_token
= cp_lexer_peek_token (parser
->lexer
);
39686 FOR_EACH_VEC_ELT (types
, i
, type
)
39689 = build_function_type_list (void_type_node
,
39690 cp_build_reference_type (type
, false),
39692 tree this_reduc_id
= reduc_id
;
39693 if (!dependent_type_p (type
))
39694 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39695 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39696 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39697 DECL_ARTIFICIAL (fndecl
) = 1;
39698 DECL_EXTERNAL (fndecl
) = 1;
39699 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39700 DECL_IGNORED_P (fndecl
) = 1;
39701 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39702 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39703 DECL_ATTRIBUTES (fndecl
)
39704 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39705 DECL_ATTRIBUTES (fndecl
));
39706 if (processing_template_decl
)
39707 fndecl
= push_template_decl (fndecl
);
39708 bool block_scope
= false;
39709 tree block
= NULL_TREE
;
39710 if (current_function_decl
)
39712 block_scope
= true;
39713 DECL_CONTEXT (fndecl
) = global_namespace
;
39714 if (!processing_template_decl
)
39717 else if (current_class_type
)
39721 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39722 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39723 cp_lexer_consume_token (parser
->lexer
);
39724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39726 cp
= cp_token_cache_new (first_token
,
39727 cp_lexer_peek_nth_token (parser
->lexer
,
39730 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39731 finish_member_declaration (fndecl
);
39732 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39733 DECL_PENDING_INLINE_P (fndecl
) = 1;
39734 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39739 DECL_CONTEXT (fndecl
) = current_namespace
;
39743 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39745 block
= begin_omp_structured_block ();
39748 cp_parser_push_lexer_for_tokens (parser
, cp
);
39749 parser
->lexer
->in_pragma
= true;
39751 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39754 finish_function (/*inline_p=*/false);
39756 DECL_CONTEXT (fndecl
) = current_function_decl
;
39758 cp_parser_pop_lexer (parser
);
39762 cp_parser_pop_lexer (parser
);
39764 finish_function (/*inline_p=*/false);
39767 DECL_CONTEXT (fndecl
) = current_function_decl
;
39768 block
= finish_omp_structured_block (block
);
39769 if (TREE_CODE (block
) == BIND_EXPR
)
39770 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39771 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39772 DECL_SAVED_TREE (fndecl
) = block
;
39773 if (processing_template_decl
)
39774 add_decl_expr (fndecl
);
39776 cp_check_omp_declare_reduction (fndecl
);
39777 if (cp
== NULL
&& types
.length () > 1)
39778 cp
= cp_token_cache_new (first_token
,
39779 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39780 if (errs
!= errorcount
)
39784 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39787 /* Free any declarators allocated. */
39788 obstack_free (&declarator_obstack
, p
);
39792 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39793 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39794 initializer-clause[opt] new-line
39795 #pragma omp declare target new-line */
39798 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39799 enum pragma_context context
)
39801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39803 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39804 const char *p
= IDENTIFIER_POINTER (id
);
39806 if (strcmp (p
, "simd") == 0)
39808 cp_lexer_consume_token (parser
->lexer
);
39809 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39813 cp_ensure_no_omp_declare_simd (parser
);
39814 if (strcmp (p
, "reduction") == 0)
39816 cp_lexer_consume_token (parser
->lexer
);
39817 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39821 if (!flag_openmp
) /* flag_openmp_simd */
39823 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39826 if (strcmp (p
, "target") == 0)
39828 cp_lexer_consume_token (parser
->lexer
);
39829 cp_parser_omp_declare_target (parser
, pragma_tok
);
39833 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39835 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39840 #pragma omp requires clauses[optseq] new-line */
39843 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39846 enum omp_requires new_req
= (enum omp_requires
) 0;
39848 location_t loc
= pragma_tok
->location
;
39849 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39851 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39852 cp_lexer_consume_token (parser
->lexer
);
39856 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39858 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39859 const char *p
= IDENTIFIER_POINTER (id
);
39860 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39861 enum omp_requires this_req
= (enum omp_requires
) 0;
39863 if (!strcmp (p
, "unified_address"))
39864 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39865 else if (!strcmp (p
, "unified_shared_memory"))
39866 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39867 else if (!strcmp (p
, "dynamic_allocators"))
39868 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39869 else if (!strcmp (p
, "reverse_offload"))
39870 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39871 else if (!strcmp (p
, "atomic_default_mem_order"))
39873 cp_lexer_consume_token (parser
->lexer
);
39875 matching_parens parens
;
39876 if (parens
.require_open (parser
))
39878 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39880 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39881 p
= IDENTIFIER_POINTER (id
);
39883 if (!strcmp (p
, "seq_cst"))
39885 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39886 else if (!strcmp (p
, "relaxed"))
39888 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39889 else if (!strcmp (p
, "acq_rel"))
39891 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39895 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39896 "expected %<seq_cst%>, %<relaxed%> or "
39898 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39900 cp_lexer_consume_token (parser
->lexer
);
39903 cp_lexer_consume_token (parser
->lexer
);
39905 if (!parens
.require_close (parser
))
39906 cp_parser_skip_to_closing_parenthesis (parser
,
39907 /*recovering=*/true,
39908 /*or_comma=*/false,
39914 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39922 error_at (cloc
, "expected %<unified_address%>, "
39923 "%<unified_shared_memory%>, "
39924 "%<dynamic_allocators%>, "
39925 "%<reverse_offload%> "
39926 "or %<atomic_default_mem_order%> clause");
39927 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39931 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39932 "supported yet", p
);
39934 cp_lexer_consume_token (parser
->lexer
);
39937 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39939 if ((this_req
& new_req
) != 0)
39940 error_at (cloc
, "too many %qs clauses", p
);
39941 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39942 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39943 error_at (cloc
, "%qs clause used lexically after first "
39944 "target construct or offloading API", p
);
39946 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39948 error_at (cloc
, "too many %qs clauses",
39949 "atomic_default_mem_order");
39950 this_req
= (enum omp_requires
) 0;
39952 else if ((omp_requires_mask
39953 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39955 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39956 " clause in a single compilation unit");
39958 = (enum omp_requires
)
39960 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39962 else if ((omp_requires_mask
39963 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39964 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39965 "lexically after first %<atomic%> construct "
39966 "without memory order clause");
39967 new_req
= (enum omp_requires
) (new_req
| this_req
);
39969 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39975 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39978 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39984 #pragma omp taskloop taskloop-clause[optseq] new-line
39987 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39990 #define OMP_TASKLOOP_CLAUSE_MASK \
39991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
40004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40009 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
40010 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40013 tree clauses
, sb
, ret
;
40015 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40017 strcat (p_name
, " taskloop");
40018 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
40019 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
40021 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
40022 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
40024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40026 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40027 const char *p
= IDENTIFIER_POINTER (id
);
40029 if (strcmp (p
, "simd") == 0)
40031 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40032 if (cclauses
== NULL
)
40033 cclauses
= cclauses_buf
;
40035 cp_lexer_consume_token (parser
->lexer
);
40036 if (!flag_openmp
) /* flag_openmp_simd */
40037 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40039 sb
= begin_omp_structured_block ();
40040 save
= cp_parser_begin_omp_structured_block (parser
);
40041 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40043 cp_parser_end_omp_structured_block (parser
, save
);
40044 tree body
= finish_omp_structured_block (sb
);
40047 ret
= make_node (OMP_TASKLOOP
);
40048 TREE_TYPE (ret
) = void_type_node
;
40049 OMP_FOR_BODY (ret
) = body
;
40050 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
40051 SET_EXPR_LOCATION (ret
, loc
);
40056 if (!flag_openmp
) /* flag_openmp_simd */
40058 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40062 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40066 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
40067 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
40070 keep_next_level (true);
40071 sb
= begin_omp_structured_block ();
40072 save
= cp_parser_begin_omp_structured_block (parser
);
40074 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
40077 cp_parser_end_omp_structured_block (parser
, save
);
40078 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
40085 # pragma acc routine oacc-routine-clause[optseq] new-line
40086 function-definition
40088 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40091 #define OACC_ROUTINE_CLAUSE_MASK \
40092 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40098 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40099 component, which must resolve to a declared namespace-scope
40100 function. The clauses are either processed directly (for a named
40101 function), or defered until the immediatley following declaration
40105 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
40106 enum pragma_context context
)
40108 gcc_checking_assert (context
== pragma_external
);
40109 /* The checking for "another pragma following this one" in the "no optional
40110 '( name )'" case makes sure that we dont re-enter. */
40111 gcc_checking_assert (parser
->oacc_routine
== NULL
);
40113 cp_oacc_routine_data data
;
40114 data
.error_seen
= false;
40115 data
.fndecl_seen
= false;
40116 data
.tokens
= vNULL
;
40117 data
.clauses
= NULL_TREE
;
40118 data
.loc
= pragma_tok
->location
;
40119 /* It is safe to take the address of a local variable; it will only be
40120 used while this scope is live. */
40121 parser
->oacc_routine
= &data
;
40123 /* Look for optional '( name )'. */
40124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
40126 matching_parens parens
;
40127 parens
.consume_open (parser
); /* '(' */
40129 /* We parse the name as an id-expression. If it resolves to
40130 anything other than a non-overloaded function at namespace
40131 scope, it's an error. */
40132 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40133 tree name
= cp_parser_id_expression (parser
,
40134 /*template_keyword_p=*/false,
40135 /*check_dependency_p=*/false,
40136 /*template_p=*/NULL
,
40137 /*declarator_p=*/false,
40138 /*optional_p=*/false);
40139 tree decl
= (identifier_p (name
)
40140 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
40142 if (name
!= error_mark_node
&& decl
== error_mark_node
)
40143 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
40145 if (decl
== error_mark_node
40146 || !parens
.require_close (parser
))
40148 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40149 parser
->oacc_routine
= NULL
;
40154 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40155 "#pragma acc routine",
40156 cp_lexer_peek_token (parser
->lexer
));
40158 if (decl
&& is_overloaded_fn (decl
)
40159 && (TREE_CODE (decl
) != FUNCTION_DECL
40160 || DECL_FUNCTION_TEMPLATE_P (decl
)))
40162 error_at (name_loc
,
40163 "%<#pragma acc routine%> names a set of overloads");
40164 parser
->oacc_routine
= NULL
;
40168 /* Perhaps we should use the same rule as declarations in different
40170 if (!DECL_NAMESPACE_SCOPE_P (decl
))
40172 error_at (name_loc
,
40173 "%qD does not refer to a namespace scope function", decl
);
40174 parser
->oacc_routine
= NULL
;
40178 if (TREE_CODE (decl
) != FUNCTION_DECL
)
40180 error_at (name_loc
, "%qD does not refer to a function", decl
);
40181 parser
->oacc_routine
= NULL
;
40185 cp_finalize_oacc_routine (parser
, decl
, false);
40186 parser
->oacc_routine
= NULL
;
40188 else /* No optional '( name )'. */
40190 /* Store away all pragma tokens. */
40191 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
40192 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
40193 cp_lexer_consume_token (parser
->lexer
);
40194 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40195 parser
->oacc_routine
->error_seen
= true;
40196 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40197 struct cp_token_cache
*cp
40198 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
40199 parser
->oacc_routine
->tokens
.safe_push (cp
);
40201 /* Emit a helpful diagnostic if there's another pragma following this
40203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
40205 cp_ensure_no_oacc_routine (parser
);
40206 data
.tokens
.release ();
40207 /* ..., and then just keep going. */
40211 /* We only have to consider the pragma_external case here. */
40212 cp_parser_declaration (parser
);
40213 if (parser
->oacc_routine
40214 && !parser
->oacc_routine
->fndecl_seen
)
40215 cp_ensure_no_oacc_routine (parser
);
40217 parser
->oacc_routine
= NULL
;
40218 data
.tokens
.release ();
40222 /* Finalize #pragma acc routine clauses after direct declarator has
40226 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
40228 struct cp_token_cache
*ce
;
40229 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
40231 if (!data
->error_seen
&& data
->fndecl_seen
)
40233 error_at (data
->loc
,
40234 "%<#pragma acc routine%> not immediately followed by "
40235 "a single function declaration or definition");
40236 data
->error_seen
= true;
40238 if (data
->error_seen
)
40241 gcc_checking_assert (data
->tokens
.length () == 1);
40242 ce
= data
->tokens
[0];
40244 cp_parser_push_lexer_for_tokens (parser
, ce
);
40245 parser
->lexer
->in_pragma
= true;
40246 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
40248 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40249 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
40250 parser
->oacc_routine
->clauses
40251 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40252 "#pragma acc routine", pragma_tok
);
40253 cp_parser_pop_lexer (parser
);
40254 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40260 /* Apply any saved OpenACC routine clauses to a just-parsed
40264 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
40266 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
40268 /* Keep going if we're in error reporting mode. */
40269 if (parser
->oacc_routine
->error_seen
40270 || fndecl
== error_mark_node
)
40273 if (parser
->oacc_routine
->fndecl_seen
)
40275 error_at (parser
->oacc_routine
->loc
,
40276 "%<#pragma acc routine%> not immediately followed by"
40277 " a single function declaration or definition");
40278 parser
->oacc_routine
= NULL
;
40281 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
40283 cp_ensure_no_oacc_routine (parser
);
40287 if (oacc_get_fn_attrib (fndecl
))
40289 error_at (parser
->oacc_routine
->loc
,
40290 "%<#pragma acc routine%> already applied to %qD", fndecl
);
40291 parser
->oacc_routine
= NULL
;
40295 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
40297 error_at (parser
->oacc_routine
->loc
,
40299 ? G_("%<#pragma acc routine%> must be applied before use")
40300 : G_("%<#pragma acc routine%> must be applied before "
40302 parser
->oacc_routine
= NULL
;
40306 /* Process the routine's dimension clauses. */
40307 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
40308 oacc_replace_fn_attrib (fndecl
, dims
);
40310 /* Add an "omp declare target" attribute. */
40311 DECL_ATTRIBUTES (fndecl
)
40312 = tree_cons (get_identifier ("omp declare target"),
40313 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
40315 /* Don't unset parser->oacc_routine here: we may still need it to
40316 diagnose wrong usage. But, remember that we've used this "#pragma acc
40318 parser
->oacc_routine
->fndecl_seen
= true;
40322 /* Main entry point to OpenMP statement pragmas. */
40325 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40328 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
40329 omp_clause_mask
mask (0);
40331 switch (cp_parser_pragma_kind (pragma_tok
))
40333 case PRAGMA_OACC_ATOMIC
:
40334 cp_parser_omp_atomic (parser
, pragma_tok
);
40336 case PRAGMA_OACC_CACHE
:
40337 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
40339 case PRAGMA_OACC_DATA
:
40340 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
40342 case PRAGMA_OACC_ENTER_DATA
:
40343 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
40345 case PRAGMA_OACC_EXIT_DATA
:
40346 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
40348 case PRAGMA_OACC_HOST_DATA
:
40349 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
40351 case PRAGMA_OACC_KERNELS
:
40352 case PRAGMA_OACC_PARALLEL
:
40353 strcpy (p_name
, "#pragma acc");
40354 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
40357 case PRAGMA_OACC_LOOP
:
40358 strcpy (p_name
, "#pragma acc");
40359 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40362 case PRAGMA_OACC_UPDATE
:
40363 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
40365 case PRAGMA_OACC_WAIT
:
40366 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
40368 case PRAGMA_OMP_ATOMIC
:
40369 cp_parser_omp_atomic (parser
, pragma_tok
);
40371 case PRAGMA_OMP_CRITICAL
:
40372 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
40374 case PRAGMA_OMP_DISTRIBUTE
:
40375 strcpy (p_name
, "#pragma omp");
40376 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
40379 case PRAGMA_OMP_FOR
:
40380 strcpy (p_name
, "#pragma omp");
40381 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
40384 case PRAGMA_OMP_MASTER
:
40385 strcpy (p_name
, "#pragma omp");
40386 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
40389 case PRAGMA_OMP_PARALLEL
:
40390 strcpy (p_name
, "#pragma omp");
40391 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
40394 case PRAGMA_OMP_SECTIONS
:
40395 strcpy (p_name
, "#pragma omp");
40396 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
40398 case PRAGMA_OMP_SIMD
:
40399 strcpy (p_name
, "#pragma omp");
40400 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
40403 case PRAGMA_OMP_SINGLE
:
40404 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
40406 case PRAGMA_OMP_TASK
:
40407 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
40409 case PRAGMA_OMP_TASKGROUP
:
40410 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
40412 case PRAGMA_OMP_TASKLOOP
:
40413 strcpy (p_name
, "#pragma omp");
40414 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40417 case PRAGMA_OMP_TEAMS
:
40418 strcpy (p_name
, "#pragma omp");
40419 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
40423 gcc_unreachable ();
40426 protected_set_expr_location (stmt
, pragma_tok
->location
);
40429 /* Transactional Memory parsing routines. */
40431 /* Parse a transaction attribute.
40437 We use this instead of cp_parser_attributes_opt for transactions to avoid
40438 the pedwarn in C++98 mode. */
40441 cp_parser_txn_attribute_opt (cp_parser
*parser
)
40444 tree attr_name
, attr
= NULL
;
40446 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
40447 return cp_parser_attributes_opt (parser
);
40449 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
40451 cp_lexer_consume_token (parser
->lexer
);
40452 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
40455 token
= cp_lexer_peek_token (parser
->lexer
);
40456 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
40458 token
= cp_lexer_consume_token (parser
->lexer
);
40460 attr_name
= (token
->type
== CPP_KEYWORD
40461 /* For keywords, use the canonical spelling,
40462 not the parsed identifier. */
40463 ? ridpointers
[(int) token
->keyword
]
40465 attr
= build_tree_list (attr_name
, NULL_TREE
);
40468 cp_parser_error (parser
, "expected identifier");
40470 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40472 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40476 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40478 transaction-statement:
40479 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40481 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40485 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
40487 unsigned char old_in
= parser
->in_transaction
;
40488 unsigned char this_in
= 1, new_in
;
40489 enum rid keyword
= token
->keyword
;
40490 tree stmt
, attrs
, noex
;
40492 cp_lexer_consume_token (parser
->lexer
);
40494 if (keyword
== RID_TRANSACTION_RELAXED
40495 || keyword
== RID_SYNCHRONIZED
)
40496 this_in
|= TM_STMT_ATTR_RELAXED
;
40499 attrs
= cp_parser_txn_attribute_opt (parser
);
40501 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40504 /* Parse a noexcept specification. */
40505 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40506 noex
= boolean_true_node
;
40507 else if (keyword
== RID_ATOMIC_CANCEL
)
40509 /* cancel-and-throw is unimplemented. */
40510 sorry ("atomic_cancel");
40514 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40516 /* Keep track if we're in the lexical scope of an outer transaction. */
40517 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40519 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40521 parser
->in_transaction
= new_in
;
40522 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40523 parser
->in_transaction
= old_in
;
40525 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40530 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40532 transaction-expression:
40533 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40534 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40538 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40540 unsigned char old_in
= parser
->in_transaction
;
40541 unsigned char this_in
= 1;
40545 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40547 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40548 || keyword
== RID_TRANSACTION_RELAXED
);
40552 keyword
== RID_TRANSACTION_RELAXED
40553 ? G_("%<__transaction_relaxed%> without transactional memory "
40555 : G_("%<__transaction_atomic%> without transactional memory "
40556 "support enabled"));
40558 token
= cp_parser_require_keyword (parser
, keyword
,
40559 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40560 : RT_TRANSACTION_RELAXED
));
40561 gcc_assert (token
!= NULL
);
40563 if (keyword
== RID_TRANSACTION_RELAXED
)
40564 this_in
|= TM_STMT_ATTR_RELAXED
;
40566 /* Set this early. This might mean that we allow transaction_cancel in
40567 an expression that we find out later actually has to be a constexpr.
40568 However, we expect that cxx_constant_value will be able to deal with
40569 this; also, if the noexcept has no constexpr, then what we parse next
40570 really is a transaction's body. */
40571 parser
->in_transaction
= this_in
;
40573 /* Parse a noexcept specification. */
40574 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40577 if (!noex
|| !noex_expr
40578 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40580 matching_parens parens
;
40581 parens
.require_open (parser
);
40583 expr
= cp_parser_expression (parser
);
40584 expr
= finish_parenthesized_expr (expr
);
40586 parens
.require_close (parser
);
40590 /* The only expression that is available got parsed for the noexcept
40591 already. noexcept is true then. */
40593 noex
= boolean_true_node
;
40596 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40597 parser
->in_transaction
= old_in
;
40599 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40600 return error_mark_node
;
40602 return (flag_tm
? expr
: error_mark_node
);
40605 /* Parse a function-transaction-block.
40607 function-transaction-block:
40608 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40610 __transaction_atomic txn-attribute[opt] function-try-block
40611 __transaction_relaxed ctor-initializer[opt] function-body
40612 __transaction_relaxed function-try-block
40616 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40618 unsigned char old_in
= parser
->in_transaction
;
40619 unsigned char new_in
= 1;
40620 tree compound_stmt
, stmt
, attrs
;
40623 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40624 || keyword
== RID_TRANSACTION_RELAXED
);
40625 token
= cp_parser_require_keyword (parser
, keyword
,
40626 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40627 : RT_TRANSACTION_RELAXED
));
40628 gcc_assert (token
!= NULL
);
40630 if (keyword
== RID_TRANSACTION_RELAXED
)
40631 new_in
|= TM_STMT_ATTR_RELAXED
;
40634 attrs
= cp_parser_txn_attribute_opt (parser
);
40636 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40639 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40641 parser
->in_transaction
= new_in
;
40643 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40644 cp_parser_function_try_block (parser
);
40646 cp_parser_ctor_initializer_opt_and_function_body
40647 (parser
, /*in_function_try_block=*/false);
40649 parser
->in_transaction
= old_in
;
40651 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40654 /* Parse a __transaction_cancel statement.
40657 __transaction_cancel txn-attribute[opt] ;
40658 __transaction_cancel txn-attribute[opt] throw-expression ;
40660 ??? Cancel and throw is not yet implemented. */
40663 cp_parser_transaction_cancel (cp_parser
*parser
)
40666 bool is_outer
= false;
40669 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40670 RT_TRANSACTION_CANCEL
);
40671 gcc_assert (token
!= NULL
);
40673 attrs
= cp_parser_txn_attribute_opt (parser
);
40675 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40677 /* ??? Parse cancel-and-throw here. */
40679 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40683 error_at (token
->location
, "%<__transaction_cancel%> without "
40684 "transactional memory support enabled");
40685 return error_mark_node
;
40687 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40689 error_at (token
->location
, "%<__transaction_cancel%> within a "
40690 "%<__transaction_relaxed%>");
40691 return error_mark_node
;
40695 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40696 && !is_tm_may_cancel_outer (current_function_decl
))
40698 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40699 "within outer %<__transaction_atomic%>");
40700 error_at (token
->location
,
40701 " or a %<transaction_may_cancel_outer%> function");
40702 return error_mark_node
;
40705 else if (parser
->in_transaction
== 0)
40707 error_at (token
->location
, "%<__transaction_cancel%> not within "
40708 "%<__transaction_atomic%>");
40709 return error_mark_node
;
40712 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40720 static GTY (()) cp_parser
*the_parser
;
40723 /* Special handling for the first token or line in the file. The first
40724 thing in the file might be #pragma GCC pch_preprocess, which loads a
40725 PCH file, which is a GC collection point. So we need to handle this
40726 first pragma without benefit of an existing lexer structure.
40728 Always returns one token to the caller in *FIRST_TOKEN. This is
40729 either the true first token of the file, or the first token after
40730 the initial pragma. */
40733 cp_parser_initial_pragma (cp_token
*first_token
)
40737 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40738 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40741 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40742 if (first_token
->type
== CPP_STRING
)
40744 name
= first_token
->u
.value
;
40746 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40747 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40748 error_at (first_token
->location
,
40749 "junk at end of %<#pragma GCC pch_preprocess%>");
40752 error_at (first_token
->location
, "expected string literal");
40754 /* Skip to the end of the pragma. */
40755 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40756 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40758 /* Now actually load the PCH file. */
40760 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40762 /* Read one more token to return to our caller. We have to do this
40763 after reading the PCH file in, since its pointers have to be
40765 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40768 /* Parse a pragma GCC ivdep. */
40771 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40773 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40777 /* Parse a pragma GCC unroll. */
40779 static unsigned short
40780 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40782 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40783 tree expr
= cp_parser_constant_expression (parser
);
40784 unsigned short unroll
;
40785 expr
= maybe_constant_value (expr
);
40786 HOST_WIDE_INT lunroll
= 0;
40787 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40788 || TREE_CODE (expr
) != INTEGER_CST
40789 || (lunroll
= tree_to_shwi (expr
)) < 0
40790 || lunroll
>= USHRT_MAX
)
40792 error_at (location
, "%<#pragma GCC unroll%> requires an"
40793 " assignment-expression that evaluates to a non-negative"
40794 " integral constant less than %u", USHRT_MAX
);
40799 unroll
= (unsigned short)lunroll
;
40803 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40807 /* Normal parsing of a pragma token. Here we can (and must) use the
40811 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40813 cp_token
*pragma_tok
;
40818 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40819 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40820 parser
->lexer
->in_pragma
= true;
40822 id
= cp_parser_pragma_kind (pragma_tok
);
40823 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40824 cp_ensure_no_omp_declare_simd (parser
);
40827 case PRAGMA_GCC_PCH_PREPROCESS
:
40828 error_at (pragma_tok
->location
,
40829 "%<#pragma GCC pch_preprocess%> must be first");
40832 case PRAGMA_OMP_BARRIER
:
40835 case pragma_compound
:
40836 cp_parser_omp_barrier (parser
, pragma_tok
);
40839 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40840 "used in compound statements", "omp barrier");
40847 case PRAGMA_OMP_DEPOBJ
:
40850 case pragma_compound
:
40851 cp_parser_omp_depobj (parser
, pragma_tok
);
40854 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40855 "used in compound statements", "omp depobj");
40862 case PRAGMA_OMP_FLUSH
:
40865 case pragma_compound
:
40866 cp_parser_omp_flush (parser
, pragma_tok
);
40869 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40870 "used in compound statements", "omp flush");
40877 case PRAGMA_OMP_TASKWAIT
:
40880 case pragma_compound
:
40881 cp_parser_omp_taskwait (parser
, pragma_tok
);
40884 error_at (pragma_tok
->location
,
40885 "%<#pragma %s%> may only be used in compound statements",
40893 case PRAGMA_OMP_TASKYIELD
:
40896 case pragma_compound
:
40897 cp_parser_omp_taskyield (parser
, pragma_tok
);
40900 error_at (pragma_tok
->location
,
40901 "%<#pragma %s%> may only be used in compound statements",
40909 case PRAGMA_OMP_CANCEL
:
40912 case pragma_compound
:
40913 cp_parser_omp_cancel (parser
, pragma_tok
);
40916 error_at (pragma_tok
->location
,
40917 "%<#pragma %s%> may only be used in compound statements",
40925 case PRAGMA_OMP_CANCELLATION_POINT
:
40926 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40929 case PRAGMA_OMP_THREADPRIVATE
:
40930 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40933 case PRAGMA_OMP_DECLARE
:
40934 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40936 case PRAGMA_OACC_DECLARE
:
40937 cp_parser_oacc_declare (parser
, pragma_tok
);
40940 case PRAGMA_OACC_ENTER_DATA
:
40941 if (context
== pragma_stmt
)
40943 error_at (pragma_tok
->location
,
40944 "%<#pragma %s%> may only be used in compound statements",
40948 else if (context
!= pragma_compound
)
40950 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40953 case PRAGMA_OACC_EXIT_DATA
:
40954 if (context
== pragma_stmt
)
40956 error_at (pragma_tok
->location
,
40957 "%<#pragma %s%> may only be used in compound statements",
40961 else if (context
!= pragma_compound
)
40963 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40966 case PRAGMA_OACC_ROUTINE
:
40967 if (context
!= pragma_external
)
40969 error_at (pragma_tok
->location
,
40970 "%<#pragma acc routine%> must be at file scope");
40973 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40976 case PRAGMA_OACC_UPDATE
:
40977 if (context
== pragma_stmt
)
40979 error_at (pragma_tok
->location
,
40980 "%<#pragma %s%> may only be used in compound statements",
40984 else if (context
!= pragma_compound
)
40986 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40989 case PRAGMA_OACC_WAIT
:
40990 if (context
== pragma_stmt
)
40992 error_at (pragma_tok
->location
,
40993 "%<#pragma %s%> may only be used in compound statements",
40997 else if (context
!= pragma_compound
)
40999 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
41002 case PRAGMA_OACC_ATOMIC
:
41003 case PRAGMA_OACC_CACHE
:
41004 case PRAGMA_OACC_DATA
:
41005 case PRAGMA_OACC_HOST_DATA
:
41006 case PRAGMA_OACC_KERNELS
:
41007 case PRAGMA_OACC_PARALLEL
:
41008 case PRAGMA_OACC_LOOP
:
41009 case PRAGMA_OMP_ATOMIC
:
41010 case PRAGMA_OMP_CRITICAL
:
41011 case PRAGMA_OMP_DISTRIBUTE
:
41012 case PRAGMA_OMP_FOR
:
41013 case PRAGMA_OMP_MASTER
:
41014 case PRAGMA_OMP_PARALLEL
:
41015 case PRAGMA_OMP_SECTIONS
:
41016 case PRAGMA_OMP_SIMD
:
41017 case PRAGMA_OMP_SINGLE
:
41018 case PRAGMA_OMP_TASK
:
41019 case PRAGMA_OMP_TASKGROUP
:
41020 case PRAGMA_OMP_TASKLOOP
:
41021 case PRAGMA_OMP_TEAMS
:
41022 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41024 stmt
= push_omp_privatization_clauses (false);
41025 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
41026 pop_omp_privatization_clauses (stmt
);
41029 case PRAGMA_OMP_REQUIRES
:
41030 return cp_parser_omp_requires (parser
, pragma_tok
);
41032 case PRAGMA_OMP_ORDERED
:
41033 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41035 stmt
= push_omp_privatization_clauses (false);
41036 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
41037 pop_omp_privatization_clauses (stmt
);
41040 case PRAGMA_OMP_TARGET
:
41041 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41043 stmt
= push_omp_privatization_clauses (false);
41044 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
41045 pop_omp_privatization_clauses (stmt
);
41048 case PRAGMA_OMP_END_DECLARE_TARGET
:
41049 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
41052 case PRAGMA_OMP_SECTION
:
41053 error_at (pragma_tok
->location
,
41054 "%<#pragma omp section%> may only be used in "
41055 "%<#pragma omp sections%> construct");
41060 if (context
== pragma_external
)
41062 error_at (pragma_tok
->location
,
41063 "%<#pragma GCC ivdep%> must be inside a function");
41066 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
41067 unsigned short unroll
;
41068 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41069 if (tok
->type
== CPP_PRAGMA
41070 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
41072 tok
= cp_lexer_consume_token (parser
->lexer
);
41073 unroll
= cp_parser_pragma_unroll (parser
, tok
);
41074 tok
= cp_lexer_peek_token (the_parser
->lexer
);
41078 if (tok
->type
!= CPP_KEYWORD
41079 || (tok
->keyword
!= RID_FOR
41080 && tok
->keyword
!= RID_WHILE
41081 && tok
->keyword
!= RID_DO
))
41083 cp_parser_error (parser
, "for, while or do statement expected");
41086 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
41090 case PRAGMA_UNROLL
:
41092 if (context
== pragma_external
)
41094 error_at (pragma_tok
->location
,
41095 "%<#pragma GCC unroll%> must be inside a function");
41098 const unsigned short unroll
41099 = cp_parser_pragma_unroll (parser
, pragma_tok
);
41101 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41102 if (tok
->type
== CPP_PRAGMA
41103 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
41105 tok
= cp_lexer_consume_token (parser
->lexer
);
41106 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
41107 tok
= cp_lexer_peek_token (the_parser
->lexer
);
41111 if (tok
->type
!= CPP_KEYWORD
41112 || (tok
->keyword
!= RID_FOR
41113 && tok
->keyword
!= RID_WHILE
41114 && tok
->keyword
!= RID_DO
))
41116 cp_parser_error (parser
, "for, while or do statement expected");
41119 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
41124 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
41125 c_invoke_pragma_handler (id
);
41129 cp_parser_error (parser
, "expected declaration specifiers");
41133 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41137 /* The interface the pragma parsers have to the lexer. */
41140 pragma_lex (tree
*value
, location_t
*loc
)
41142 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41143 enum cpp_ttype ret
= tok
->type
;
41145 *value
= tok
->u
.value
;
41147 *loc
= tok
->location
;
41149 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
41151 else if (ret
== CPP_STRING
)
41152 *value
= cp_parser_string_literal (the_parser
, false, false);
41155 if (ret
== CPP_KEYWORD
)
41157 cp_lexer_consume_token (the_parser
->lexer
);
41164 /* External interface. */
41166 /* Parse one entire translation unit. */
41169 c_parse_file (void)
41171 static bool already_called
= false;
41173 if (already_called
)
41174 fatal_error (input_location
,
41175 "inter-module optimizations not implemented for C++");
41176 already_called
= true;
41178 the_parser
= cp_parser_new ();
41179 push_deferring_access_checks (flag_access_control
41180 ? dk_no_deferred
: dk_no_check
);
41181 cp_parser_translation_unit (the_parser
);
41184 finish_translation_unit ();
41187 /* Create an identifier for a generic parameter type (a synthesized
41188 template parameter implied by `auto' or a concept identifier). */
41190 static GTY(()) int generic_parm_count
;
41192 make_generic_type_name ()
41195 sprintf (buf
, "auto:%d", ++generic_parm_count
);
41196 return get_identifier (buf
);
41199 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41200 (creating a new template parameter list if necessary). Returns the newly
41201 created template type parm. */
41204 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
41206 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
41208 /* Before committing to modifying any scope, if we're in an
41209 implicit template scope, and we're trying to synthesize a
41210 constrained parameter, try to find a previous parameter with
41211 the same name. This is the same-type rule for abbreviated
41212 function templates.
41214 NOTE: We can generate implicit parameters when tentatively
41215 parsing a nested name specifier, only to reject that parse
41216 later. However, matching the same template-id as part of a
41217 direct-declarator should generate an identical template
41218 parameter, so this rule will merge them. */
41219 if (parser
->implicit_template_scope
&& constr
)
41221 tree t
= parser
->implicit_template_parms
;
41224 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
41226 tree d
= TREE_VALUE (t
);
41227 if (TREE_CODE (d
) == PARM_DECL
)
41228 /* Return the TEMPLATE_PARM_INDEX. */
41229 d
= DECL_INITIAL (d
);
41232 t
= TREE_CHAIN (t
);
41236 /* We are either continuing a function template that already contains implicit
41237 template parameters, creating a new fully-implicit function template, or
41238 extending an existing explicit function template with implicit template
41241 cp_binding_level
*const entry_scope
= current_binding_level
;
41243 bool become_template
= false;
41244 cp_binding_level
*parent_scope
= 0;
41246 if (parser
->implicit_template_scope
)
41248 gcc_assert (parser
->implicit_template_parms
);
41250 current_binding_level
= parser
->implicit_template_scope
;
41254 /* Roll back to the existing template parameter scope (in the case of
41255 extending an explicit function template) or introduce a new template
41256 parameter scope ahead of the function parameter scope (or class scope
41257 in the case of out-of-line member definitions). The function scope is
41258 added back after template parameter synthesis below. */
41260 cp_binding_level
*scope
= entry_scope
;
41262 while (scope
->kind
== sk_function_parms
)
41264 parent_scope
= scope
;
41265 scope
= scope
->level_chain
;
41267 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
41269 /* If not defining a class, then any class scope is a scope level in
41270 an out-of-line member definition. In this case simply wind back
41271 beyond the first such scope to inject the template parameter list.
41272 Otherwise wind back to the class being defined. The latter can
41273 occur in class member friend declarations such as:
41279 friend void A::foo (auto);
41282 The template parameter list synthesized for the friend declaration
41283 must be injected in the scope of 'B'. This can also occur in
41284 erroneous cases such as:
41290 void B::foo (auto) {}
41293 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41294 but, nevertheless, the template parameter list synthesized for the
41295 declarator should be injected into the scope of 'A' as if the
41296 ill-formed template was specified explicitly. */
41298 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
41300 parent_scope
= scope
;
41301 scope
= scope
->level_chain
;
41305 current_binding_level
= scope
;
41307 if (scope
->kind
!= sk_template_parms
41308 || !function_being_declared_is_template_p (parser
))
41310 /* Introduce a new template parameter list for implicit template
41313 become_template
= true;
41315 parser
->implicit_template_scope
41316 = begin_scope (sk_template_parms
, NULL
);
41318 ++processing_template_decl
;
41320 parser
->fully_implicit_function_template_p
= true;
41321 ++parser
->num_template_parameter_lists
;
41325 /* Synthesize implicit template parameters at the end of the explicit
41326 template parameter list. */
41328 gcc_assert (current_template_parms
);
41330 parser
->implicit_template_scope
= scope
;
41332 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41333 parser
->implicit_template_parms
41334 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
41338 /* Synthesize a new template parameter and track the current template
41339 parameter chain with implicit_template_parms. */
41341 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
41342 tree synth_id
= make_generic_type_name ();
41343 tree synth_tmpl_parm
;
41344 bool non_type
= false;
41346 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
41348 = finish_template_type_parm (class_type_node
, synth_id
);
41349 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
41351 = finish_constrained_template_template_parm (proto
, synth_id
);
41354 synth_tmpl_parm
= copy_decl (proto
);
41355 DECL_NAME (synth_tmpl_parm
) = synth_id
;
41359 // Attach the constraint to the parm before processing.
41360 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
41361 TREE_TYPE (node
) = constr
;
41363 = process_template_parm (parser
->implicit_template_parms
,
41366 /*non_type=*/non_type
,
41367 /*param_pack=*/false);
41369 // Chain the new parameter to the list of implicit parameters.
41370 if (parser
->implicit_template_parms
)
41371 parser
->implicit_template_parms
41372 = TREE_CHAIN (parser
->implicit_template_parms
);
41374 parser
->implicit_template_parms
= new_parm
;
41376 tree new_decl
= get_local_decls ();
41378 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41379 new_decl
= DECL_INITIAL (new_decl
);
41381 /* If creating a fully implicit function template, start the new implicit
41382 template parameter list with this synthesized type, otherwise grow the
41383 current template parameter list. */
41385 if (become_template
)
41387 parent_scope
->level_chain
= current_binding_level
;
41389 tree new_parms
= make_tree_vec (1);
41390 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
41391 current_template_parms
= tree_cons (size_int (processing_template_decl
),
41392 new_parms
, current_template_parms
);
41396 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41397 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
41398 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
41399 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
41402 // If the new parameter was constrained, we need to add that to the
41403 // constraints in the template parameter list.
41404 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
41406 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
41407 reqs
= conjoin_constraints (reqs
, req
);
41408 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
41411 current_binding_level
= entry_scope
;
41416 /* Finish the declaration of a fully implicit function template. Such a
41417 template has no explicit template parameter list so has not been through the
41418 normal template head and tail processing. synthesize_implicit_template_parm
41419 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41420 provided if the declaration is a class member such that its template
41421 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41422 form is returned. Otherwise NULL_TREE is returned. */
41425 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
41427 gcc_assert (parser
->fully_implicit_function_template_p
);
41429 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
41430 && DECL_VIRTUAL_P (member_decl_opt
))
41432 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
41433 "implicit templates may not be %<virtual%>");
41434 DECL_VIRTUAL_P (member_decl_opt
) = false;
41437 if (member_decl_opt
)
41438 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
41439 end_template_decl ();
41441 parser
->fully_implicit_function_template_p
= false;
41442 parser
->implicit_template_parms
= 0;
41443 parser
->implicit_template_scope
= 0;
41444 --parser
->num_template_parameter_lists
;
41446 return member_decl_opt
;
41449 /* Like finish_fully_implicit_template, but to be used in error
41450 recovery, rearranging scopes so that we restore the state we had
41451 before synthesize_implicit_template_parm inserted the implement
41452 template parms scope. */
41455 abort_fully_implicit_template (cp_parser
*parser
)
41457 cp_binding_level
*return_to_scope
= current_binding_level
;
41459 if (parser
->implicit_template_scope
41460 && return_to_scope
!= parser
->implicit_template_scope
)
41462 cp_binding_level
*child
= return_to_scope
;
41463 for (cp_binding_level
*scope
= child
->level_chain
;
41464 scope
!= parser
->implicit_template_scope
;
41465 scope
= child
->level_chain
)
41467 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
41468 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
41469 current_binding_level
= parser
->implicit_template_scope
;
41472 return_to_scope
= return_to_scope
->level_chain
;
41474 finish_fully_implicit_template (parser
, NULL
);
41476 gcc_assert (current_binding_level
== return_to_scope
);
41479 /* Helper function for diagnostics that have complained about things
41480 being used with 'extern "C"' linkage.
41482 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41485 maybe_show_extern_c_location (void)
41487 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
41488 inform (the_parser
->innermost_linkage_specification_location
,
41489 "%<extern \"C\"%> linkage started here");
41492 #include "gt-cp-parser.h"