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\n",
574 parser
->type_definition_forbidden_message
);
575 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
576 fprintf (file
, "Number of class definitions in progress: %u\n",
577 parser
->num_classes_being_defined
);
578 fprintf (file
, "Number of template parameter lists for the current "
579 "declaration: %u\n", parser
->num_template_parameter_lists
);
580 cp_debug_parser_tokens (file
, parser
, window_size
);
581 token
= parser
->lexer
->next_token
;
582 fprintf (file
, "Next token to parse:\n");
583 fprintf (file
, "\tToken: ");
584 cp_lexer_print_token (file
, token
);
585 eloc
= expand_location (token
->location
);
586 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
587 fprintf (file
, "\tLine: %d\n", eloc
.line
);
588 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
592 debug (cp_parser
&ref
)
594 cp_debug_parser (stderr
, &ref
);
598 debug (cp_parser
*ptr
)
603 fprintf (stderr
, "<nil>\n");
606 /* Allocate memory for a new lexer object and return it. */
609 cp_lexer_alloc (void)
613 c_common_no_more_pch ();
615 /* Allocate the memory. */
616 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
618 /* Initially we are not debugging. */
619 lexer
->debugging_p
= false;
621 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
623 /* Create the buffer. */
624 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
634 cp_lexer_new_main (void)
639 /* It's possible that parsing the first pragma will load a PCH file,
640 which is a GC collection point. So we have to do that before
641 allocating any memory. */
642 cp_parser_initial_pragma (&token
);
644 lexer
= cp_lexer_alloc ();
646 /* Put the first token in the buffer. */
647 lexer
->buffer
->quick_push (token
);
649 /* Get the remaining tokens from the preprocessor. */
650 while (token
.type
!= CPP_EOF
)
652 cp_lexer_get_preprocessor_token (lexer
, &token
);
653 vec_safe_push (lexer
->buffer
, token
);
656 lexer
->last_token
= lexer
->buffer
->address ()
657 + lexer
->buffer
->length ()
659 lexer
->next_token
= lexer
->buffer
->length ()
660 ? lexer
->buffer
->address ()
663 /* Subsequent preprocessor diagnostics should use compiler
664 diagnostic functions to get the compiler source location. */
667 gcc_assert (!lexer
->next_token
->purged_p
);
671 /* Create a new lexer whose token stream is primed with the tokens in
672 CACHE. When these tokens are exhausted, no new tokens will be read. */
675 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
677 cp_token
*first
= cache
->first
;
678 cp_token
*last
= cache
->last
;
679 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
681 /* We do not own the buffer. */
682 lexer
->buffer
= NULL
;
683 lexer
->next_token
= first
== last
? &eof_token
: first
;
684 lexer
->last_token
= last
;
686 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
688 /* Initially we are not debugging. */
689 lexer
->debugging_p
= false;
691 gcc_assert (!lexer
->next_token
->purged_p
);
695 /* Frees all resources associated with LEXER. */
698 cp_lexer_destroy (cp_lexer
*lexer
)
700 vec_free (lexer
->buffer
);
701 lexer
->saved_tokens
.release ();
705 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
706 be used. The point of this flag is to help the compiler to fold away calls
707 to cp_lexer_debugging_p within this source file at compile time, when the
708 lexer is not being debugged. */
710 #define LEXER_DEBUGGING_ENABLED_P false
712 /* Returns nonzero if debugging information should be output. */
715 cp_lexer_debugging_p (cp_lexer
*lexer
)
717 if (!LEXER_DEBUGGING_ENABLED_P
)
720 return lexer
->debugging_p
;
724 static inline cp_token_position
725 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
727 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
729 return lexer
->next_token
- previous_p
;
732 static inline cp_token
*
733 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
739 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
741 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
744 static inline cp_token_position
745 cp_lexer_previous_token_position (cp_lexer
*lexer
)
747 if (lexer
->next_token
== &eof_token
)
748 return lexer
->last_token
- 1;
750 return cp_lexer_token_position (lexer
, true);
753 static inline cp_token
*
754 cp_lexer_previous_token (cp_lexer
*lexer
)
756 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
758 /* Skip past purged tokens. */
761 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
765 return cp_lexer_token_at (lexer
, tp
);
768 /* nonzero if we are presently saving tokens. */
771 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
773 return lexer
->saved_tokens
.length () != 0;
776 /* Store the next token from the preprocessor in *TOKEN. Return true
777 if we reach EOF. If LEXER is NULL, assume we are handling an
778 initial #pragma pch_preprocess, and thus want the lexer to return
779 processed strings. */
782 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
784 static int is_extern_c
= 0;
786 /* Get a new token from the preprocessor. */
788 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
789 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
790 token
->keyword
= RID_MAX
;
791 token
->purged_p
= false;
792 token
->error_reported
= false;
794 /* On some systems, some header files are surrounded by an
795 implicit extern "C" block. Set a flag in the token if it
796 comes from such a header. */
797 is_extern_c
+= pending_lang_change
;
798 pending_lang_change
= 0;
799 token
->implicit_extern_c
= is_extern_c
> 0;
801 /* Check to see if this token is a keyword. */
802 if (token
->type
== CPP_NAME
)
804 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
806 /* Mark this token as a keyword. */
807 token
->type
= CPP_KEYWORD
;
808 /* Record which keyword. */
809 token
->keyword
= C_RID_CODE (token
->u
.value
);
813 if (warn_cxx11_compat
814 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
815 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
817 /* Warn about the C++0x keyword (but still treat it as
819 warning (OPT_Wc__11_compat
,
820 "identifier %qE is a keyword in C++11",
823 /* Clear out the C_RID_CODE so we don't warn about this
824 particular identifier-turned-keyword again. */
825 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
828 token
->keyword
= RID_MAX
;
831 else if (token
->type
== CPP_AT_NAME
)
833 /* This only happens in Objective-C++; it must be a keyword. */
834 token
->type
= CPP_KEYWORD
;
835 switch (C_RID_CODE (token
->u
.value
))
837 /* Replace 'class' with '@class', 'private' with '@private',
838 etc. This prevents confusion with the C++ keyword
839 'class', and makes the tokens consistent with other
840 Objective-C 'AT' keywords. For example '@class' is
841 reported as RID_AT_CLASS which is consistent with
842 '@synchronized', which is reported as
845 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
846 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
847 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
848 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
849 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
850 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
851 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
852 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
853 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
858 /* Update the globals input_location and the input file stack from TOKEN. */
860 cp_lexer_set_source_position_from_token (cp_token
*token
)
862 if (token
->type
!= CPP_EOF
)
864 input_location
= token
->location
;
868 /* Update the globals input_location and the input file stack from LEXER. */
870 cp_lexer_set_source_position (cp_lexer
*lexer
)
872 cp_token
*token
= cp_lexer_peek_token (lexer
);
873 cp_lexer_set_source_position_from_token (token
);
876 /* Return a pointer to the next token in the token stream, but do not
879 static inline cp_token
*
880 cp_lexer_peek_token (cp_lexer
*lexer
)
882 if (cp_lexer_debugging_p (lexer
))
884 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
885 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
886 putc ('\n', cp_lexer_debug_stream
);
888 return lexer
->next_token
;
891 /* Return true if the next token has the indicated TYPE. */
894 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
896 return cp_lexer_peek_token (lexer
)->type
== type
;
899 /* Return true if the next token does not have the indicated TYPE. */
902 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
904 return !cp_lexer_next_token_is (lexer
, type
);
907 /* Return true if the next token is the indicated KEYWORD. */
910 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
912 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
916 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
918 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
922 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
924 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
927 /* Return true if KEYWORD can start a decl-specifier. */
930 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
934 /* auto specifier: storage-class-specifier in C++,
935 simple-type-specifier in C++0x. */
937 /* Storage classes. */
943 /* Elaborated type specifiers. */
949 /* Simple type specifiers. */
964 /* GNU extensions. */
967 /* C++0x extensions. */
969 case RID_UNDERLYING_TYPE
:
974 if (keyword
>= RID_FIRST_INT_N
975 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
976 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
982 /* Return true if the next token is a keyword for a decl-specifier. */
985 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
989 token
= cp_lexer_peek_token (lexer
);
990 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
993 /* Returns TRUE iff the token T begins a decltype type. */
996 token_is_decltype (cp_token
*t
)
998 return (t
->keyword
== RID_DECLTYPE
999 || t
->type
== CPP_DECLTYPE
);
1002 /* Returns TRUE iff the next token begins a decltype type. */
1005 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1007 cp_token
*t
= cp_lexer_peek_token (lexer
);
1008 return token_is_decltype (t
);
1011 /* Called when processing a token with tree_check_value; perform or defer the
1012 associated checks and return the value. */
1015 saved_checks_value (struct tree_check
*check_value
)
1017 /* Perform any access checks that were deferred. */
1018 vec
<deferred_access_check
, va_gc
> *checks
;
1019 deferred_access_check
*chk
;
1020 checks
= check_value
->checks
;
1024 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1025 perform_or_defer_access_check (chk
->binfo
,
1027 chk
->diag_decl
, tf_warning_or_error
);
1029 /* Return the stored value. */
1030 return check_value
->value
;
1033 /* Return a pointer to the Nth token in the token stream. If N is 1,
1034 then this is precisely equivalent to cp_lexer_peek_token (except
1035 that it is not inline). One would like to disallow that case, but
1036 there is one case (cp_parser_nth_token_starts_template_id) where
1037 the caller passes a variable for N and it might be 1. */
1040 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1044 /* N is 1-based, not zero-based. */
1047 if (cp_lexer_debugging_p (lexer
))
1048 fprintf (cp_lexer_debug_stream
,
1049 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1052 token
= lexer
->next_token
;
1053 gcc_assert (!n
|| token
!= &eof_token
);
1057 if (token
== lexer
->last_token
)
1063 if (!token
->purged_p
)
1067 if (cp_lexer_debugging_p (lexer
))
1069 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1070 putc ('\n', cp_lexer_debug_stream
);
1076 /* Return the next token, and advance the lexer's next_token pointer
1077 to point to the next non-purged token. */
1080 cp_lexer_consume_token (cp_lexer
* lexer
)
1082 cp_token
*token
= lexer
->next_token
;
1084 gcc_assert (token
!= &eof_token
);
1085 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1089 lexer
->next_token
++;
1090 if (lexer
->next_token
== lexer
->last_token
)
1092 lexer
->next_token
= &eof_token
;
1097 while (lexer
->next_token
->purged_p
);
1099 cp_lexer_set_source_position_from_token (token
);
1101 /* Provide debugging output. */
1102 if (cp_lexer_debugging_p (lexer
))
1104 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1105 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1106 putc ('\n', cp_lexer_debug_stream
);
1112 /* Permanently remove the next token from the token stream, and
1113 advance the next_token pointer to refer to the next non-purged
1117 cp_lexer_purge_token (cp_lexer
*lexer
)
1119 cp_token
*tok
= lexer
->next_token
;
1121 gcc_assert (tok
!= &eof_token
);
1122 tok
->purged_p
= true;
1123 tok
->location
= UNKNOWN_LOCATION
;
1124 tok
->u
.value
= NULL_TREE
;
1125 tok
->keyword
= RID_MAX
;
1130 if (tok
== lexer
->last_token
)
1136 while (tok
->purged_p
);
1137 lexer
->next_token
= tok
;
1140 /* Permanently remove all tokens after TOK, up to, but not
1141 including, the token that will be returned next by
1142 cp_lexer_peek_token. */
1145 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1147 cp_token
*peek
= lexer
->next_token
;
1149 if (peek
== &eof_token
)
1150 peek
= lexer
->last_token
;
1152 gcc_assert (tok
< peek
);
1154 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1156 tok
->purged_p
= true;
1157 tok
->location
= UNKNOWN_LOCATION
;
1158 tok
->u
.value
= NULL_TREE
;
1159 tok
->keyword
= RID_MAX
;
1163 /* Begin saving tokens. All tokens consumed after this point will be
1167 cp_lexer_save_tokens (cp_lexer
* lexer
)
1169 /* Provide debugging output. */
1170 if (cp_lexer_debugging_p (lexer
))
1171 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1173 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1176 /* Commit to the portion of the token stream most recently saved. */
1179 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer
))
1183 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1185 lexer
->saved_tokens
.pop ();
1188 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1189 to the token stream. Stop saving tokens. */
1192 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1194 /* Provide debugging output. */
1195 if (cp_lexer_debugging_p (lexer
))
1196 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1198 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1201 /* RAII wrapper around the above functions, with sanity checking. Creating
1202 a variable saves tokens, which are committed when the variable is
1203 destroyed unless they are explicitly rolled back by calling the rollback
1206 struct saved_token_sentinel
1211 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1213 len
= lexer
->saved_tokens
.length ();
1214 cp_lexer_save_tokens (lexer
);
1218 cp_lexer_rollback_tokens (lexer
);
1221 ~saved_token_sentinel()
1224 cp_lexer_commit_tokens (lexer
);
1225 gcc_assert (lexer
->saved_tokens
.length () == len
);
1229 /* Print a representation of the TOKEN on the STREAM. */
1232 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1234 /* We don't use cpp_type2name here because the parser defines
1235 a few tokens of its own. */
1236 static const char *const token_names
[] = {
1237 /* cpplib-defined token types */
1238 #define OP(e, s) #e,
1239 #define TK(e, s) #e,
1243 /* C++ parser token types - see "Manifest constants", above. */
1246 "NESTED_NAME_SPECIFIER",
1249 /* For some tokens, print the associated data. */
1250 switch (token
->type
)
1253 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1254 For example, `struct' is mapped to an INTEGER_CST. */
1255 if (!identifier_p (token
->u
.value
))
1259 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1266 case CPP_UTF8STRING
:
1267 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1271 print_generic_expr (stream
, token
->u
.value
);
1275 /* If we have a name for the token, print it out. Otherwise, we
1276 simply give the numeric code. */
1277 if (token
->type
< ARRAY_SIZE(token_names
))
1278 fputs (token_names
[token
->type
], stream
);
1280 fprintf (stream
, "[%d]", token
->type
);
1286 debug (cp_token
&ref
)
1288 cp_lexer_print_token (stderr
, &ref
);
1289 fprintf (stderr
, "\n");
1293 debug (cp_token
*ptr
)
1298 fprintf (stderr
, "<nil>\n");
1302 /* Start emitting debugging information. */
1305 cp_lexer_start_debugging (cp_lexer
* lexer
)
1307 if (!LEXER_DEBUGGING_ENABLED_P
)
1308 fatal_error (input_location
,
1309 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1311 lexer
->debugging_p
= true;
1312 cp_lexer_debug_stream
= stderr
;
1315 /* Stop emitting debugging information. */
1318 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1320 if (!LEXER_DEBUGGING_ENABLED_P
)
1321 fatal_error (input_location
,
1322 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1324 lexer
->debugging_p
= false;
1325 cp_lexer_debug_stream
= NULL
;
1328 /* Create a new cp_token_cache, representing a range of tokens. */
1330 static cp_token_cache
*
1331 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1333 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1334 cache
->first
= first
;
1339 /* Diagnose if #pragma omp declare simd isn't followed immediately
1340 by function declaration or definition. */
1343 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1345 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1347 error ("%<#pragma omp declare simd%> not immediately followed by "
1348 "function declaration or definition");
1349 parser
->omp_declare_simd
= NULL
;
1353 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1354 and put that into "omp declare simd" attribute. */
1357 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1359 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1361 if (fndecl
== error_mark_node
)
1363 parser
->omp_declare_simd
= NULL
;
1366 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1368 cp_ensure_no_omp_declare_simd (parser
);
1374 /* Diagnose if #pragma acc routine isn't followed immediately by function
1375 declaration or definition. */
1378 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1380 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1382 error_at (parser
->oacc_routine
->loc
,
1383 "%<#pragma acc routine%> not immediately followed by "
1384 "function declaration or definition");
1385 parser
->oacc_routine
= NULL
;
1389 /* Decl-specifiers. */
1391 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1394 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1396 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1401 /* Nothing other than the parser should be creating declarators;
1402 declarators are a semi-syntactic representation of C++ entities.
1403 Other parts of the front end that need to create entities (like
1404 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1406 static cp_declarator
*make_call_declarator
1407 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1408 static cp_declarator
*make_array_declarator
1409 (cp_declarator
*, tree
);
1410 static cp_declarator
*make_pointer_declarator
1411 (cp_cv_quals
, cp_declarator
*, tree
);
1412 static cp_declarator
*make_reference_declarator
1413 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1414 static cp_declarator
*make_ptrmem_declarator
1415 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1417 /* An erroneous declarator. */
1418 static cp_declarator
*cp_error_declarator
;
1420 /* The obstack on which declarators and related data structures are
1422 static struct obstack declarator_obstack
;
1424 /* Alloc BYTES from the declarator memory pool. */
1426 static inline void *
1427 alloc_declarator (size_t bytes
)
1429 return obstack_alloc (&declarator_obstack
, bytes
);
1432 /* Allocate a declarator of the indicated KIND. Clear fields that are
1433 common to all declarators. */
1435 static cp_declarator
*
1436 make_declarator (cp_declarator_kind kind
)
1438 cp_declarator
*declarator
;
1440 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1441 declarator
->kind
= kind
;
1442 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1443 declarator
->attributes
= NULL_TREE
;
1444 declarator
->std_attributes
= NULL_TREE
;
1445 declarator
->declarator
= NULL
;
1446 declarator
->parameter_pack_p
= false;
1447 declarator
->id_loc
= UNKNOWN_LOCATION
;
1452 /* Make a declarator for a generalized identifier. If
1453 QUALIFYING_SCOPE is non-NULL, the identifier is
1454 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1455 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1458 static cp_declarator
*
1459 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1460 special_function_kind sfk
, location_t id_location
)
1462 cp_declarator
*declarator
;
1464 /* It is valid to write:
1466 class C { void f(); };
1470 The standard is not clear about whether `typedef const C D' is
1471 legal; as of 2002-09-15 the committee is considering that
1472 question. EDG 3.0 allows that syntax. Therefore, we do as
1474 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1475 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1477 gcc_assert (identifier_p (unqualified_name
)
1478 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1479 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1481 declarator
= make_declarator (cdk_id
);
1482 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1483 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1484 declarator
->u
.id
.sfk
= sfk
;
1485 declarator
->id_loc
= id_location
;
1490 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1491 of modifiers such as const or volatile to apply to the pointer
1492 type, represented as identifiers. ATTRIBUTES represent the attributes that
1493 appertain to the pointer or reference. */
1496 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1499 cp_declarator
*declarator
;
1501 declarator
= make_declarator (cdk_pointer
);
1502 declarator
->declarator
= target
;
1503 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1504 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1507 declarator
->id_loc
= target
->id_loc
;
1508 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1509 target
->parameter_pack_p
= false;
1512 declarator
->parameter_pack_p
= false;
1514 declarator
->std_attributes
= attributes
;
1519 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1520 represent the attributes that appertain to the pointer or
1524 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1525 bool rvalue_ref
, tree attributes
)
1527 cp_declarator
*declarator
;
1529 declarator
= make_declarator (cdk_reference
);
1530 declarator
->declarator
= target
;
1531 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1532 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1535 declarator
->id_loc
= target
->id_loc
;
1536 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1537 target
->parameter_pack_p
= false;
1540 declarator
->parameter_pack_p
= false;
1542 declarator
->std_attributes
= attributes
;
1547 /* Like make_pointer_declarator -- but for a pointer to a non-static
1548 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1549 appertain to the pointer or reference. */
1552 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1553 cp_declarator
*pointee
,
1556 cp_declarator
*declarator
;
1558 declarator
= make_declarator (cdk_ptrmem
);
1559 declarator
->declarator
= pointee
;
1560 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1561 declarator
->u
.pointer
.class_type
= class_type
;
1565 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1566 pointee
->parameter_pack_p
= false;
1569 declarator
->parameter_pack_p
= false;
1571 declarator
->std_attributes
= attributes
;
1576 /* Make a declarator for the function given by TARGET, with the
1577 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1578 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1579 indicates what exceptions can be thrown. */
1582 make_call_declarator (cp_declarator
*target
,
1584 cp_cv_quals cv_qualifiers
,
1585 cp_virt_specifiers virt_specifiers
,
1586 cp_ref_qualifier ref_qualifier
,
1588 tree exception_specification
,
1589 tree late_return_type
,
1590 tree requires_clause
)
1592 cp_declarator
*declarator
;
1594 declarator
= make_declarator (cdk_function
);
1595 declarator
->declarator
= target
;
1596 declarator
->u
.function
.parameters
= parms
;
1597 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1598 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1599 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1600 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1601 declarator
->u
.function
.exception_specification
= exception_specification
;
1602 declarator
->u
.function
.late_return_type
= late_return_type
;
1603 declarator
->u
.function
.requires_clause
= requires_clause
;
1606 declarator
->id_loc
= target
->id_loc
;
1607 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1608 target
->parameter_pack_p
= false;
1611 declarator
->parameter_pack_p
= false;
1616 /* Make a declarator for an array of BOUNDS elements, each of which is
1617 defined by ELEMENT. */
1620 make_array_declarator (cp_declarator
*element
, tree bounds
)
1622 cp_declarator
*declarator
;
1624 declarator
= make_declarator (cdk_array
);
1625 declarator
->declarator
= element
;
1626 declarator
->u
.array
.bounds
= bounds
;
1629 declarator
->id_loc
= element
->id_loc
;
1630 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1631 element
->parameter_pack_p
= false;
1634 declarator
->parameter_pack_p
= false;
1639 /* Determine whether the declarator we've seen so far can be a
1640 parameter pack, when followed by an ellipsis. */
1642 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1644 if (declarator
&& declarator
->parameter_pack_p
)
1645 /* We already saw an ellipsis. */
1648 /* Search for a declarator name, or any other declarator that goes
1649 after the point where the ellipsis could appear in a parameter
1650 pack. If we find any of these, then this declarator cannot be
1651 made into a parameter pack. */
1653 while (declarator
&& !found
)
1655 switch ((int)declarator
->kind
)
1667 declarator
= declarator
->declarator
;
1675 cp_parameter_declarator
*no_parameters
;
1677 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1678 DECLARATOR and DEFAULT_ARGUMENT. */
1680 cp_parameter_declarator
*
1681 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1682 cp_declarator
*declarator
,
1683 tree default_argument
,
1685 bool template_parameter_pack_p
= false)
1687 cp_parameter_declarator
*parameter
;
1689 parameter
= ((cp_parameter_declarator
*)
1690 alloc_declarator (sizeof (cp_parameter_declarator
)));
1691 parameter
->next
= NULL
;
1692 if (decl_specifiers
)
1693 parameter
->decl_specifiers
= *decl_specifiers
;
1695 clear_decl_specs (¶meter
->decl_specifiers
);
1696 parameter
->declarator
= declarator
;
1697 parameter
->default_argument
= default_argument
;
1698 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1699 parameter
->loc
= loc
;
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1707 function_declarator_p (const cp_declarator
*declarator
)
1711 if (declarator
->kind
== cdk_function
1712 && declarator
->declarator
->kind
== cdk_id
)
1714 if (declarator
->kind
== cdk_id
1715 || declarator
->kind
== cdk_decomp
1716 || declarator
->kind
== cdk_error
)
1718 declarator
= declarator
->declarator
;
1728 A cp_parser parses the token stream as specified by the C++
1729 grammar. Its job is purely parsing, not semantic analysis. For
1730 example, the parser breaks the token stream into declarators,
1731 expressions, statements, and other similar syntactic constructs.
1732 It does not check that the types of the expressions on either side
1733 of an assignment-statement are compatible, or that a function is
1734 not declared with a parameter of type `void'.
1736 The parser invokes routines elsewhere in the compiler to perform
1737 semantic analysis and to build up the abstract syntax tree for the
1740 The parser (and the template instantiation code, which is, in a
1741 way, a close relative of parsing) are the only parts of the
1742 compiler that should be calling push_scope and pop_scope, or
1743 related functions. The parser (and template instantiation code)
1744 keeps track of what scope is presently active; everything else
1745 should simply honor that. (The code that generates static
1746 initializers may also need to set the scope, in order to check
1747 access control correctly when emitting the initializers.)
1752 The parser is of the standard recursive-descent variety. Upcoming
1753 tokens in the token stream are examined in order to determine which
1754 production to use when parsing a non-terminal. Some C++ constructs
1755 require arbitrary look ahead to disambiguate. For example, it is
1756 impossible, in the general case, to tell whether a statement is an
1757 expression or declaration without scanning the entire statement.
1758 Therefore, the parser is capable of "parsing tentatively." When the
1759 parser is not sure what construct comes next, it enters this mode.
1760 Then, while we attempt to parse the construct, the parser queues up
1761 error messages, rather than issuing them immediately, and saves the
1762 tokens it consumes. If the construct is parsed successfully, the
1763 parser "commits", i.e., it issues any queued error messages and
1764 the tokens that were being preserved are permanently discarded.
1765 If, however, the construct is not parsed successfully, the parser
1766 rolls back its state completely so that it can resume parsing using
1767 a different alternative.
1772 The performance of the parser could probably be improved substantially.
1773 We could often eliminate the need to parse tentatively by looking ahead
1774 a little bit. In some places, this approach might not entirely eliminate
1775 the need to parse tentatively, but it might still speed up the average
1778 /* Flags that are passed to some parsing functions. These values can
1779 be bitwise-ored together. */
1784 CP_PARSER_FLAGS_NONE
= 0x0,
1785 /* The construct is optional. If it is not present, then no error
1786 should be issued. */
1787 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1788 /* When parsing a type-specifier, treat user-defined type-names
1789 as non-type identifiers. */
1790 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1791 /* When parsing a type-specifier, do not try to parse a class-specifier
1792 or enum-specifier. */
1793 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1794 /* When parsing a decl-specifier-seq, only allow type-specifier or
1796 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1797 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1798 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1799 /* When parsing a decl-specifier-seq, allow missing typename. */
1800 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20
1803 /* This type is used for parameters and variables which hold
1804 combinations of the above flags. */
1805 typedef int cp_parser_flags
;
1807 /* The different kinds of declarators we want to parse. */
1809 enum cp_parser_declarator_kind
1811 /* We want an abstract declarator. */
1812 CP_PARSER_DECLARATOR_ABSTRACT
,
1813 /* We want a named declarator. */
1814 CP_PARSER_DECLARATOR_NAMED
,
1815 /* We don't mind, but the name must be an unqualified-id. */
1816 CP_PARSER_DECLARATOR_EITHER
1819 /* The precedence values used to parse binary expressions. The minimum value
1820 of PREC must be 1, because zero is reserved to quickly discriminate
1821 binary operators from other tokens. */
1826 PREC_LOGICAL_OR_EXPRESSION
,
1827 PREC_LOGICAL_AND_EXPRESSION
,
1828 PREC_INCLUSIVE_OR_EXPRESSION
,
1829 PREC_EXCLUSIVE_OR_EXPRESSION
,
1830 PREC_AND_EXPRESSION
,
1831 PREC_EQUALITY_EXPRESSION
,
1832 PREC_RELATIONAL_EXPRESSION
,
1833 PREC_SHIFT_EXPRESSION
,
1834 PREC_ADDITIVE_EXPRESSION
,
1835 PREC_MULTIPLICATIVE_EXPRESSION
,
1837 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1840 /* A mapping from a token type to a corresponding tree node type, with a
1841 precedence value. */
1843 struct cp_parser_binary_operations_map_node
1845 /* The token type. */
1846 enum cpp_ttype token_type
;
1847 /* The corresponding tree code. */
1848 enum tree_code tree_type
;
1849 /* The precedence of this operator. */
1850 enum cp_parser_prec prec
;
1853 struct cp_parser_expression_stack_entry
1855 /* Left hand side of the binary operation we are currently
1858 /* Original tree code for left hand side, if it was a binary
1859 expression itself (used for -Wparentheses). */
1860 enum tree_code lhs_type
;
1861 /* Tree code for the binary operation we are parsing. */
1862 enum tree_code tree_type
;
1863 /* Precedence of the binary operation we are parsing. */
1864 enum cp_parser_prec prec
;
1865 /* Location of the binary operation we are parsing. */
1869 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1870 entries because precedence levels on the stack are monotonically
1872 typedef struct cp_parser_expression_stack_entry
1873 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1877 /* Constructors and destructors. */
1879 static cp_parser_context
*cp_parser_context_new
1880 (cp_parser_context
*);
1882 /* Class variables. */
1884 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1886 /* The operator-precedence table used by cp_parser_binary_expression.
1887 Transformed into an associative array (binops_by_token) by
1890 static const cp_parser_binary_operations_map_node binops
[] = {
1891 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1892 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1894 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1895 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1896 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1899 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1902 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1905 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1906 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1910 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1914 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1916 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1918 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1920 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1923 /* The same as binops, but initialized by cp_parser_new so that
1924 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1926 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1928 /* Constructors and destructors. */
1930 /* Construct a new context. The context below this one on the stack
1931 is given by NEXT. */
1933 static cp_parser_context
*
1934 cp_parser_context_new (cp_parser_context
* next
)
1936 cp_parser_context
*context
;
1938 /* Allocate the storage. */
1939 if (cp_parser_context_free_list
!= NULL
)
1941 /* Pull the first entry from the free list. */
1942 context
= cp_parser_context_free_list
;
1943 cp_parser_context_free_list
= context
->next
;
1944 memset (context
, 0, sizeof (*context
));
1947 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1949 /* No errors have occurred yet in this context. */
1950 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1951 /* If this is not the bottommost context, copy information that we
1952 need from the previous context. */
1955 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1956 expression, then we are parsing one in this context, too. */
1957 context
->object_type
= next
->object_type
;
1958 /* Thread the stack. */
1959 context
->next
= next
;
1965 /* Managing the unparsed function queues. */
1967 #define unparsed_funs_with_default_args \
1968 parser->unparsed_queues->last ().funs_with_default_args
1969 #define unparsed_funs_with_definitions \
1970 parser->unparsed_queues->last ().funs_with_definitions
1971 #define unparsed_nsdmis \
1972 parser->unparsed_queues->last ().nsdmis
1973 #define unparsed_classes \
1974 parser->unparsed_queues->last ().classes
1977 push_unparsed_function_queues (cp_parser
*parser
)
1979 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1980 vec_safe_push (parser
->unparsed_queues
, e
);
1984 pop_unparsed_function_queues (cp_parser
*parser
)
1986 release_tree_vector (unparsed_funs_with_definitions
);
1987 parser
->unparsed_queues
->pop ();
1992 /* Constructors and destructors. */
1994 static cp_parser
*cp_parser_new
1997 /* Routines to parse various constructs.
1999 Those that return `tree' will return the error_mark_node (rather
2000 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2001 Sometimes, they will return an ordinary node if error-recovery was
2002 attempted, even though a parse error occurred. So, to check
2003 whether or not a parse error occurred, you should always use
2004 cp_parser_error_occurred. If the construct is optional (indicated
2005 either by an `_opt' in the name of the function that does the
2006 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2007 the construct is not present. */
2009 /* Lexical conventions [gram.lex] */
2011 static cp_expr cp_parser_identifier
2013 static cp_expr cp_parser_string_literal
2014 (cp_parser
*, bool, bool, bool);
2015 static cp_expr cp_parser_userdef_char_literal
2017 static tree cp_parser_userdef_string_literal
2019 static cp_expr cp_parser_userdef_numeric_literal
2022 /* Basic concepts [gram.basic] */
2024 static void cp_parser_translation_unit (cp_parser
*);
2026 /* Expressions [gram.expr] */
2028 static cp_expr cp_parser_primary_expression
2029 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2030 static cp_expr cp_parser_id_expression
2031 (cp_parser
*, bool, bool, bool *, bool, bool);
2032 static cp_expr cp_parser_unqualified_id
2033 (cp_parser
*, bool, bool, bool, bool);
2034 static tree cp_parser_nested_name_specifier_opt
2035 (cp_parser
*, bool, bool, bool, bool, bool = false);
2036 static tree cp_parser_nested_name_specifier
2037 (cp_parser
*, bool, bool, bool, bool);
2038 static tree cp_parser_qualifying_entity
2039 (cp_parser
*, bool, bool, bool, bool, bool);
2040 static cp_expr cp_parser_postfix_expression
2041 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2042 static tree cp_parser_postfix_open_square_expression
2043 (cp_parser
*, tree
, bool, bool);
2044 static tree cp_parser_postfix_dot_deref_expression
2045 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2046 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2047 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2049 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2050 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2051 static void cp_parser_pseudo_destructor_name
2052 (cp_parser
*, tree
, tree
*, tree
*);
2053 static cp_expr cp_parser_unary_expression
2054 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2055 static enum tree_code cp_parser_unary_operator
2057 static tree cp_parser_has_attribute_expression
2059 static tree cp_parser_new_expression
2061 static vec
<tree
, va_gc
> *cp_parser_new_placement
2063 static tree cp_parser_new_type_id
2064 (cp_parser
*, tree
*);
2065 static cp_declarator
*cp_parser_new_declarator_opt
2067 static cp_declarator
*cp_parser_direct_new_declarator
2069 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2071 static tree cp_parser_delete_expression
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser
*, cp_expr
);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2083 static cp_expr cp_parser_expression
2084 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2089 static cp_expr cp_parser_lambda_expression
2091 static void cp_parser_lambda_introducer
2092 (cp_parser
*, tree
);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser
*, tree
);
2095 static void cp_parser_lambda_body
2096 (cp_parser
*, tree
);
2098 /* Statements [gram.stmt.stmt] */
2100 static void cp_parser_statement
2101 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser
*, tree
);
2104 static tree cp_parser_expression_statement
2105 (cp_parser
*, tree
);
2106 static tree cp_parser_compound_statement
2107 (cp_parser
*, tree
, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser
*, tree
);
2110 static tree cp_parser_selection_statement
2111 (cp_parser
*, bool *, vec
<tree
> *);
2112 static tree cp_parser_condition
2114 static tree cp_parser_iteration_statement
2115 (cp_parser
*, bool *, bool, unsigned short);
2116 static bool cp_parser_init_statement
2117 (cp_parser
*, tree
*decl
);
2118 static tree cp_parser_for
2119 (cp_parser
*, bool, unsigned short);
2120 static tree cp_parser_c_for
2121 (cp_parser
*, tree
, tree
, bool, unsigned short);
2122 static tree cp_parser_range_for
2123 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2124 static void do_range_for_auto_deduction
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree
, tree
*, tree
*);
2128 static tree cp_parser_range_for_member_function
2130 static tree cp_parser_jump_statement
2132 static void cp_parser_declaration_statement
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser
*, bool *, const token_indent_info
&);
2140 /* Declarations [gram.dcl.dcl] */
2142 static void cp_parser_declaration_seq_opt
2144 static void cp_parser_declaration
2146 static void cp_parser_toplevel_declaration
2148 static void cp_parser_block_declaration
2149 (cp_parser
*, bool);
2150 static void cp_parser_simple_declaration
2151 (cp_parser
*, bool, tree
*);
2152 static void cp_parser_decl_specifier_seq
2153 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2154 static tree cp_parser_storage_class_specifier_opt
2156 static tree cp_parser_function_specifier_opt
2157 (cp_parser
*, cp_decl_specifier_seq
*);
2158 static tree cp_parser_type_specifier
2159 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2161 static tree cp_parser_simple_type_specifier
2162 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2163 static tree cp_parser_type_name
2164 (cp_parser
*, bool);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser
* parser
);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser
*, bool, bool);
2169 static tree cp_parser_enum_specifier
2171 static void cp_parser_enumerator_list
2172 (cp_parser
*, tree
);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser
*, tree
);
2175 static tree cp_parser_namespace_name
2177 static void cp_parser_namespace_definition
2179 static void cp_parser_namespace_body
2181 static tree cp_parser_qualified_namespace_specifier
2183 static void cp_parser_namespace_alias_definition
2185 static bool cp_parser_using_declaration
2186 (cp_parser
*, bool);
2187 static void cp_parser_using_directive
2189 static tree cp_parser_alias_declaration
2191 static void cp_parser_asm_definition
2193 static void cp_parser_linkage_specification
2195 static void cp_parser_static_assert
2196 (cp_parser
*, bool);
2197 static tree cp_parser_decltype
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2202 /* Declarators [gram.dcl.decl] */
2204 static tree cp_parser_init_declarator
2205 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2206 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2207 location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2211 static cp_declarator
*cp_parser_direct_declarator
2212 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2214 static enum tree_code cp_parser_ptr_operator
2215 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2216 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 static tree cp_parser_tx_qualifier_opt
2224 static tree cp_parser_late_return_type_opt
2225 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2226 static tree cp_parser_declarator_id
2227 (cp_parser
*, bool);
2228 static tree cp_parser_type_id
2229 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2230 static tree cp_parser_template_type_arg
2232 static tree
cp_parser_trailing_type_id (cp_parser
*);
2233 static tree cp_parser_type_id_1
2234 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2235 static void cp_parser_type_specifier_seq
2236 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2237 static tree cp_parser_parameter_declaration_clause
2238 (cp_parser
*, cp_parser_flags
);
2239 static tree cp_parser_parameter_declaration_list
2240 (cp_parser
*, cp_parser_flags
);
2241 static cp_parameter_declarator
*cp_parser_parameter_declaration
2242 (cp_parser
*, cp_parser_flags
, bool, bool *);
2243 static tree cp_parser_default_argument
2244 (cp_parser
*, bool);
2245 static void cp_parser_function_body
2246 (cp_parser
*, bool);
2247 static tree cp_parser_initializer
2248 (cp_parser
*, bool *, bool *, bool = false);
2249 static cp_expr cp_parser_initializer_clause
2250 (cp_parser
*, bool *);
2251 static cp_expr cp_parser_braced_list
2252 (cp_parser
*, bool*);
2253 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2254 (cp_parser
*, bool *, bool *);
2256 static void cp_parser_ctor_initializer_opt_and_function_body
2257 (cp_parser
*, bool);
2259 static tree cp_parser_late_parsing_omp_declare_simd
2260 (cp_parser
*, tree
);
2262 static tree cp_parser_late_parsing_oacc_routine
2263 (cp_parser
*, tree
);
2265 static tree synthesize_implicit_template_parm
2266 (cp_parser
*, tree
);
2267 static tree finish_fully_implicit_template
2268 (cp_parser
*, tree
);
2269 static void abort_fully_implicit_template
2272 /* Classes [gram.class] */
2274 static tree cp_parser_class_name
2275 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2278 static tree cp_parser_class_head
2279 (cp_parser
*, bool *);
2280 static enum tag_types cp_parser_class_key
2282 static void cp_parser_type_parameter_key
2283 (cp_parser
* parser
);
2284 static void cp_parser_member_specification_opt
2286 static void cp_parser_member_declaration
2288 static tree cp_parser_pure_specifier
2290 static tree cp_parser_constant_initializer
2293 /* Derived classes [gram.class.derived] */
2295 static tree cp_parser_base_clause
2297 static tree cp_parser_base_specifier
2300 /* Special member functions [gram.special] */
2302 static tree cp_parser_conversion_function_id
2304 static tree cp_parser_conversion_type_id
2306 static cp_declarator
*cp_parser_conversion_declarator_opt
2308 static void cp_parser_ctor_initializer_opt
2310 static void cp_parser_mem_initializer_list
2312 static tree cp_parser_mem_initializer
2314 static tree cp_parser_mem_initializer_id
2317 /* Overloading [gram.over] */
2319 static cp_expr cp_parser_operator_function_id
2321 static cp_expr cp_parser_operator
2322 (cp_parser
*, location_t
);
2324 /* Templates [gram.temp] */
2326 static void cp_parser_template_declaration
2327 (cp_parser
*, bool);
2328 static tree cp_parser_template_parameter_list
2330 static tree cp_parser_template_parameter
2331 (cp_parser
*, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser
*, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser
*, bool, bool, enum tag_types
, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2338 static tree cp_parser_template_argument_list
2340 static tree cp_parser_template_argument
2342 static void cp_parser_explicit_instantiation
2344 static void cp_parser_explicit_specialization
2347 /* Exception handling [gram.exception] */
2349 static tree cp_parser_try_block
2351 static void cp_parser_function_try_block
2353 static void cp_parser_handler_seq
2355 static void cp_parser_handler
2357 static tree cp_parser_exception_declaration
2359 static tree cp_parser_throw_expression
2361 static tree cp_parser_exception_specification_opt
2363 static tree cp_parser_type_id_list
2366 /* GNU Extensions */
2368 static tree cp_parser_asm_specification_opt
2370 static tree cp_parser_asm_operand_list
2372 static tree cp_parser_asm_clobber_list
2374 static tree cp_parser_asm_label_list
2376 static bool cp_next_tokens_can_be_attribute_p
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2380 static bool cp_next_tokens_can_be_std_attribute_p
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser
*, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser
*, size_t);
2388 static tree cp_parser_attributes_opt
2390 static tree cp_parser_gnu_attributes_opt
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser
*, bool = false);
2394 static tree cp_parser_std_attribute
2395 (cp_parser
*, tree
);
2396 static tree cp_parser_std_attribute_spec
2398 static tree cp_parser_std_attribute_spec_seq
2400 static size_t cp_parser_skip_attributes_opt
2401 (cp_parser
*, size_t);
2402 static bool cp_parser_extension_opt
2403 (cp_parser
*, int *);
2404 static void cp_parser_label_declaration
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2411 static tree cp_parser_requires_clause_opt
2413 static tree cp_parser_requires_expression
2415 static tree cp_parser_requirement_parameter_list
2417 static tree cp_parser_requirement_body
2419 static tree cp_parser_requirement_list
2421 static tree cp_parser_requirement
2423 static tree cp_parser_simple_requirement
2425 static tree cp_parser_compound_requirement
2427 static tree cp_parser_type_requirement
2429 static tree cp_parser_nested_requirement
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser
*, cp_token
*);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser
*, enum rid
);
2438 static void cp_parser_function_transaction
2439 (cp_parser
*, enum rid
);
2440 static tree cp_parser_transaction_cancel
2443 enum pragma_context
{
2450 static bool cp_parser_pragma
2451 (cp_parser
*, enum pragma_context
, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2457 static tree cp_parser_objc_message_args
2459 static tree cp_parser_objc_message_expression
2461 static cp_expr cp_parser_objc_encode_expression
2463 static tree cp_parser_objc_defs_expression
2465 static tree cp_parser_objc_protocol_expression
2467 static tree cp_parser_objc_selector_expression
2469 static cp_expr cp_parser_objc_expression
2471 static bool cp_parser_objc_selector_p
2473 static tree cp_parser_objc_selector
2475 static tree cp_parser_objc_protocol_refs_opt
2477 static void cp_parser_objc_declaration
2478 (cp_parser
*, tree
);
2479 static tree cp_parser_objc_statement
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser
*, tree
*);
2483 static void cp_parser_objc_at_property_declaration
2485 static void cp_parser_objc_at_synthesize_declaration
2487 static void cp_parser_objc_at_dynamic_declaration
2489 static tree cp_parser_objc_struct_declaration
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser
*, tree
, location_t
);
2498 static tree cp_parser_maybe_treat_template_as_class
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser
*, cp_declarator
*, location_t
);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2504 static cp_expr cp_parser_simple_cast_expression
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser
*, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser
*, cp_parser_flags
, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser
*, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser
*, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec
<deferred_access_check
, va_gc
> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser
*, tree
);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2524 static tree cp_parser_save_nsdmi
2526 static tree cp_parser_enclosed_template_argument_list
2528 static void cp_parser_save_default_args
2529 (cp_parser
*, tree
);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser
*, tree
);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser
*, tree
, tree
, tree
);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser
*, tree
);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser
*, tree
);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser
*, enum rid
);
2540 static cp_expr cp_parser_trait_expr
2541 (cp_parser
*, enum rid
);
2542 static bool cp_parser_declares_only_class_p
2544 static void cp_parser_set_storage_class
2545 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq
*decl_specs
,
2550 cp_decl_spec ds
, cp_token
*);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq
*);
2553 static void cp_parser_required_error
2554 (cp_parser
*, required_token
, bool, location_t
);
2555 static cp_token
*cp_parser_require
2556 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2557 static cp_token
*cp_parser_require_keyword
2558 (cp_parser
*, enum rid
, required_token
);
2559 static bool cp_parser_token_starts_function_definition_p
2561 static bool cp_parser_next_token_starts_class_definition_p
2563 static bool cp_parser_next_token_ends_template_argument_p
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser
*, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2571 static void cp_parser_check_class_key
2572 (enum tag_types
, tree type
);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type
, location_t location
);
2575 static bool cp_parser_optional_template_keyword
2577 static void cp_parser_pre_parsed_nested_name_specifier
2579 static bool cp_parser_cache_group
2580 (cp_parser
*, enum cpp_ttype
, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser
*parser
, bool nsdmi
);
2583 static void cp_parser_parse_tentatively
2585 static void cp_parser_commit_to_tentative_parse
2587 static void cp_parser_commit_to_topmost_tentative_parse
2589 static void cp_parser_abort_tentative_parse
2591 static bool cp_parser_parse_definitely
2593 static inline bool cp_parser_parsing_tentatively
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2597 static void cp_parser_error
2598 (cp_parser
*, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2601 static bool cp_parser_simulate_error
2603 static bool cp_parser_check_type_definition
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator
*, tree
, location_t type_location
);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser
*, non_integral_constant
);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser
*, tree
, location_t
);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser
*, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2621 static void cp_parser_skip_to_end_of_block_or_statement
2623 static bool cp_parser_skip_to_closing_brace
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser
*, cp_token
*);
2629 static bool cp_parser_error_occurred
2631 static bool cp_parser_allow_gnu_extensions_p
2633 static bool cp_parser_is_pure_string_literal
2635 static bool cp_parser_is_string_literal
2637 static bool cp_parser_is_keyword
2638 (cp_token
*, enum rid
);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser
*, tree
, location_t location
);
2641 static cp_declarator
* cp_parser_make_indirect_declarator
2642 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2643 static bool cp_parser_compound_literal_p
2645 static bool cp_parser_array_designator_p
2647 static bool cp_parser_init_statement_p
2649 static bool cp_parser_skip_to_closing_square_bracket
2652 /* Concept-related syntactic transformations */
2654 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2655 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand
;
2664 ++c_inhibit_evaluation_warnings
;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings
;
2670 --cp_unevaluated_operand
;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2679 cp_parser_parsing_tentatively (cp_parser
* parser
)
2681 return parser
->context
->next
!= NULL
;
2684 /* Returns nonzero if TOKEN is a string literal. */
2687 cp_parser_is_pure_string_literal (cp_token
* token
)
2689 return (token
->type
== CPP_STRING
||
2690 token
->type
== CPP_STRING16
||
2691 token
->type
== CPP_STRING32
||
2692 token
->type
== CPP_WSTRING
||
2693 token
->type
== CPP_UTF8STRING
);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2700 cp_parser_is_string_literal (cp_token
* token
)
2702 return (cp_parser_is_pure_string_literal (token
) ||
2703 token
->type
== CPP_STRING_USERDEF
||
2704 token
->type
== CPP_STRING16_USERDEF
||
2705 token
->type
== CPP_STRING32_USERDEF
||
2706 token
->type
== CPP_WSTRING_USERDEF
||
2707 token
->type
== CPP_UTF8STRING_USERDEF
);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2713 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2715 return token
->keyword
== keyword
;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token
*token
)
2724 if (token
->type
!= CPP_PRAGMA
)
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2742 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2743 location_t
*out_loc
)
2745 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2746 if (token2
->type
!= tok1_kind
)
2748 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2749 if (token3
->type
!= tok1_kind
)
2751 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2752 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2755 /* It must be at the start of the line. */
2756 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2757 if (LOCATION_COLUMN (start_loc
) != 1)
2760 /* We have a conflict marker. Construct a location of the form:
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc
= get_finish (token4
->location
);
2765 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2770 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2774 get_matching_symbol (required_token token_desc
)
2781 case RT_CLOSE_BRACE
:
2783 case RT_CLOSE_PAREN
:
2788 /* Attempt to convert TOKEN_DESC from a required_token to an
2789 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2791 static enum cpp_ttype
2792 get_required_cpp_ttype (required_token token_desc
)
2797 return CPP_SEMICOLON
;
2799 return CPP_OPEN_PAREN
;
2800 case RT_CLOSE_BRACE
:
2801 return CPP_CLOSE_BRACE
;
2803 return CPP_OPEN_BRACE
;
2804 case RT_CLOSE_SQUARE
:
2805 return CPP_CLOSE_SQUARE
;
2806 case RT_OPEN_SQUARE
:
2807 return CPP_OPEN_SQUARE
;
2812 case RT_CLOSE_PAREN
:
2813 return CPP_CLOSE_PAREN
;
2816 /* Use CPP_EOF as a "no completions possible" code. */
2822 /* Subroutine of cp_parser_error and cp_parser_required_error.
2824 Issue a diagnostic of the form
2825 FILE:LINE: MESSAGE before TOKEN
2826 where TOKEN is the next token in the input stream. MESSAGE
2827 (specified by the caller) is usually of the form "expected
2830 This bypasses the check for tentative passing, and potentially
2831 adds material needed by cp_parser_required_error.
2833 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2834 suggesting insertion of the missing token.
2836 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2837 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2841 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2842 required_token missing_token_desc
,
2843 location_t matching_location
)
2845 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2846 /* This diagnostic makes more sense if it is tagged to the line
2847 of the token we just peeked at. */
2848 cp_lexer_set_source_position_from_token (token
);
2850 if (token
->type
== CPP_PRAGMA
)
2852 error_at (token
->location
,
2853 "%<#pragma%> is not allowed here");
2854 cp_parser_skip_to_pragma_eol (parser
, token
);
2858 /* If this is actually a conflict marker, report it as such. */
2859 if (token
->type
== CPP_LSHIFT
2860 || token
->type
== CPP_RSHIFT
2861 || token
->type
== CPP_EQ_EQ
)
2864 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2866 error_at (loc
, "version control conflict marker in file");
2867 expanded_location token_exploc
= expand_location (token
->location
);
2868 /* Consume tokens until the end of the source line. */
2871 cp_lexer_consume_token (parser
->lexer
);
2872 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2875 expanded_location next_exploc
= expand_location (next
->location
);
2876 if (next_exploc
.file
!= token_exploc
.file
)
2878 if (next_exploc
.line
!= token_exploc
.line
)
2885 gcc_rich_location
richloc (input_location
);
2887 bool added_matching_location
= false;
2889 if (missing_token_desc
!= RT_NONE
)
2891 /* Potentially supply a fix-it hint, suggesting to add the
2892 missing token immediately after the *previous* token.
2893 This may move the primary location within richloc. */
2894 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2895 location_t prev_token_loc
2896 = cp_lexer_previous_token (parser
->lexer
)->location
;
2897 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2899 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2900 Attempt to consolidate diagnostics by printing it as a
2901 secondary range within the main diagnostic. */
2902 if (matching_location
!= UNKNOWN_LOCATION
)
2903 added_matching_location
2904 = richloc
.add_location_if_nearby (matching_location
);
2907 /* Actually emit the error. */
2908 c_parse_error (gmsgid
,
2909 /* Because c_parser_error does not understand
2910 CPP_KEYWORD, keywords are treated like
2912 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2913 token
->u
.value
, token
->flags
, &richloc
);
2915 if (missing_token_desc
!= RT_NONE
)
2917 /* If we weren't able to consolidate matching_location, then
2918 print it as a secondary diagnostic. */
2919 if (matching_location
!= UNKNOWN_LOCATION
2920 && !added_matching_location
)
2921 inform (matching_location
, "to match this %qs",
2922 get_matching_symbol (missing_token_desc
));
2926 /* If not parsing tentatively, issue a diagnostic of the form
2927 FILE:LINE: MESSAGE before TOKEN
2928 where TOKEN is the next token in the input stream. MESSAGE
2929 (specified by the caller) is usually of the form "expected
2933 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2935 if (!cp_parser_simulate_error (parser
))
2936 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2939 /* Issue an error about name-lookup failing. NAME is the
2940 IDENTIFIER_NODE DECL is the result of
2941 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2942 the thing that we hoped to find. */
2945 cp_parser_name_lookup_error (cp_parser
* parser
,
2948 name_lookup_error desired
,
2949 location_t location
)
2951 /* If name lookup completely failed, tell the user that NAME was not
2953 if (decl
== error_mark_node
)
2955 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2956 error_at (location
, "%<%E::%E%> has not been declared",
2957 parser
->scope
, name
);
2958 else if (parser
->scope
== global_namespace
)
2959 error_at (location
, "%<::%E%> has not been declared", name
);
2960 else if (parser
->object_scope
2961 && !CLASS_TYPE_P (parser
->object_scope
))
2962 error_at (location
, "request for member %qE in non-class type %qT",
2963 name
, parser
->object_scope
);
2964 else if (parser
->object_scope
)
2965 error_at (location
, "%<%T::%E%> has not been declared",
2966 parser
->object_scope
, name
);
2968 error_at (location
, "%qE has not been declared", name
);
2970 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2975 error_at (location
, "%<%E::%E%> is not a type",
2976 parser
->scope
, name
);
2979 error_at (location
, "%<%E::%E%> is not a class or namespace",
2980 parser
->scope
, name
);
2984 "%<%E::%E%> is not a class, namespace, or enumeration",
2985 parser
->scope
, name
);
2992 else if (parser
->scope
== global_namespace
)
2997 error_at (location
, "%<::%E%> is not a type", name
);
3000 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3004 "%<::%E%> is not a class, namespace, or enumeration",
3016 error_at (location
, "%qE is not a type", name
);
3019 error_at (location
, "%qE is not a class or namespace", name
);
3023 "%qE is not a class, namespace, or enumeration", name
);
3031 /* If we are parsing tentatively, remember that an error has occurred
3032 during this tentative parse. Returns true if the error was
3033 simulated; false if a message should be issued by the caller. */
3036 cp_parser_simulate_error (cp_parser
* parser
)
3038 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3040 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3046 /* This function is called when a type is defined. If type
3047 definitions are forbidden at this point, an error message is
3051 cp_parser_check_type_definition (cp_parser
* parser
)
3053 /* If types are forbidden here, issue a message. */
3054 if (parser
->type_definition_forbidden_message
)
3056 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3057 in the message need to be interpreted. */
3058 error (parser
->type_definition_forbidden_message
);
3064 /* This function is called when the DECLARATOR is processed. The TYPE
3065 was a type defined in the decl-specifiers. If it is invalid to
3066 define a type in the decl-specifiers for DECLARATOR, an error is
3067 issued. TYPE_LOCATION is the location of TYPE and is used
3068 for error reporting. */
3071 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3072 tree type
, location_t type_location
)
3074 /* [dcl.fct] forbids type definitions in return types.
3075 Unfortunately, it's not easy to know whether or not we are
3076 processing a return type until after the fact. */
3078 && (declarator
->kind
== cdk_pointer
3079 || declarator
->kind
== cdk_reference
3080 || declarator
->kind
== cdk_ptrmem
))
3081 declarator
= declarator
->declarator
;
3083 && declarator
->kind
== cdk_function
)
3085 error_at (type_location
,
3086 "new types may not be defined in a return type");
3087 inform (type_location
,
3088 "(perhaps a semicolon is missing after the definition of %qT)",
3093 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3094 "<" in any valid C++ program. If the next token is indeed "<",
3095 issue a message warning the user about what appears to be an
3096 invalid attempt to form a template-id. LOCATION is the location
3097 of the type-specifier (TYPE) */
3100 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3102 enum tag_types tag_type
,
3103 location_t location
)
3105 cp_token_position start
= 0;
3107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3109 if (TREE_CODE (type
) == TYPE_DECL
)
3110 type
= TREE_TYPE (type
);
3111 if (TYPE_P (type
) && !template_placeholder_p (type
))
3112 error_at (location
, "%qT is not a template", type
);
3113 else if (identifier_p (type
))
3115 if (tag_type
!= none_type
)
3116 error_at (location
, "%qE is not a class template", type
);
3118 error_at (location
, "%qE is not a template", type
);
3121 error_at (location
, "invalid template-id");
3122 /* Remember the location of the invalid "<". */
3123 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3124 start
= cp_lexer_token_position (parser
->lexer
, true);
3125 /* Consume the "<". */
3126 cp_lexer_consume_token (parser
->lexer
);
3127 /* Parse the template arguments. */
3128 cp_parser_enclosed_template_argument_list (parser
);
3129 /* Permanently remove the invalid template arguments so that
3130 this error message is not issued again. */
3132 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3136 /* If parsing an integral constant-expression, issue an error message
3137 about the fact that THING appeared and return true. Otherwise,
3138 return false. In either case, set
3139 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3142 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3143 non_integral_constant thing
)
3145 parser
->non_integral_constant_expression_p
= true;
3146 if (parser
->integral_constant_expression_p
)
3148 if (!parser
->allow_non_integral_constant_expression_p
)
3150 const char *msg
= NULL
;
3154 pedwarn (input_location
, OPT_Wpedantic
,
3155 "ISO C++ forbids using a floating-point literal "
3156 "in a constant-expression");
3159 error ("a cast to a type other than an integral or "
3160 "enumeration type cannot appear in a "
3161 "constant-expression");
3164 error ("%<typeid%> operator "
3165 "cannot appear in a constant-expression");
3168 error ("non-constant compound literals "
3169 "cannot appear in a constant-expression");
3172 error ("a function call "
3173 "cannot appear in a constant-expression");
3176 error ("an increment "
3177 "cannot appear in a constant-expression");
3180 error ("an decrement "
3181 "cannot appear in a constant-expression");
3184 error ("an array reference "
3185 "cannot appear in a constant-expression");
3187 case NIC_ADDR_LABEL
:
3188 error ("the address of a label "
3189 "cannot appear in a constant-expression");
3191 case NIC_OVERLOADED
:
3192 error ("calls to overloaded operators "
3193 "cannot appear in a constant-expression");
3195 case NIC_ASSIGNMENT
:
3196 error ("an assignment cannot appear in a constant-expression");
3199 error ("a comma operator "
3200 "cannot appear in a constant-expression");
3202 case NIC_CONSTRUCTOR
:
3203 error ("a call to a constructor "
3204 "cannot appear in a constant-expression");
3206 case NIC_TRANSACTION
:
3207 error ("a transaction expression "
3208 "cannot appear in a constant-expression");
3214 msg
= "__FUNCTION__";
3216 case NIC_PRETTY_FUNC
:
3217 msg
= "__PRETTY_FUNCTION__";
3237 case NIC_PREINCREMENT
:
3240 case NIC_PREDECREMENT
:
3253 error ("%qs cannot appear in a constant-expression", msg
);
3260 /* Emit a diagnostic for an invalid type name. This function commits
3261 to the current active tentative parse, if any. (Otherwise, the
3262 problematic construct might be encountered again later, resulting
3263 in duplicate error messages.) LOCATION is the location of ID. */
3266 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3267 location_t location
)
3269 tree decl
, ambiguous_decls
;
3270 cp_parser_commit_to_tentative_parse (parser
);
3271 /* Try to lookup the identifier. */
3272 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3273 /*is_template=*/false,
3274 /*is_namespace=*/false,
3275 /*check_dependency=*/true,
3276 &ambiguous_decls
, location
);
3277 if (ambiguous_decls
)
3278 /* If the lookup was ambiguous, an error will already have
3281 /* If the lookup found a template-name, it means that the user forgot
3282 to specify an argument list. Emit a useful error message. */
3283 if (DECL_TYPE_TEMPLATE_P (decl
))
3285 auto_diagnostic_group d
;
3287 "invalid use of template-name %qE without an argument list",
3289 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3290 inform (location
, "class template argument deduction is only available "
3291 "with %<-std=c++17%> or %<-std=gnu++17%>");
3292 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3294 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3295 error_at (location
, "invalid use of destructor %qD as a type", id
);
3296 else if (TREE_CODE (decl
) == TYPE_DECL
)
3297 /* Something like 'unsigned A a;' */
3298 error_at (location
, "invalid combination of multiple type-specifiers");
3299 else if (!parser
->scope
)
3301 /* Issue an error message. */
3302 auto_diagnostic_group d
;
3304 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3305 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3306 if (const char *suggestion
= hint
.suggestion ())
3308 gcc_rich_location
richloc (location
);
3309 richloc
.add_fixit_replace (suggestion
);
3311 "%qE does not name a type; did you mean %qs?",
3315 error_at (location
, "%qE does not name a type", id
);
3316 /* If we're in a template class, it's possible that the user was
3317 referring to a type from a base class. For example:
3319 template <typename T> struct A { typedef T X; };
3320 template <typename T> struct B : public A<T> { X x; };
3322 The user should have said "typename A<T>::X". */
3323 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3324 inform (location
, "C++11 %<constexpr%> only available with "
3325 "%<-std=c++11%> or %<-std=gnu++11%>");
3326 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3327 inform (location
, "C++11 %<noexcept%> only available with "
3328 "%<-std=c++11%> or %<-std=gnu++11%>");
3329 else if (cxx_dialect
< cxx11
3330 && TREE_CODE (id
) == IDENTIFIER_NODE
3331 && id_equal (id
, "thread_local"))
3332 inform (location
, "C++11 %<thread_local%> only available with "
3333 "%<-std=c++11%> or %<-std=gnu++11%>");
3334 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3335 inform (location
, "%<concept%> only available with %<-fconcepts%>");
3336 else if (processing_template_decl
&& current_class_type
3337 && TYPE_BINFO (current_class_type
))
3341 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3345 tree base_type
= BINFO_TYPE (b
);
3346 if (CLASS_TYPE_P (base_type
)
3347 && dependent_type_p (base_type
))
3350 /* Go from a particular instantiation of the
3351 template (which will have an empty TYPE_FIELDs),
3352 to the main version. */
3353 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3354 for (field
= TYPE_FIELDS (base_type
);
3356 field
= DECL_CHAIN (field
))
3357 if (TREE_CODE (field
) == TYPE_DECL
3358 && DECL_NAME (field
) == id
)
3361 "(perhaps %<typename %T::%E%> was intended)",
3362 BINFO_TYPE (b
), id
);
3371 /* Here we diagnose qualified-ids where the scope is actually correct,
3372 but the identifier does not resolve to a valid type name. */
3373 else if (parser
->scope
!= error_mark_node
)
3375 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3377 auto_diagnostic_group d
;
3379 if (decl
== error_mark_node
)
3380 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3382 const char *suggestion
= hint
.suggestion ();
3383 gcc_rich_location
richloc (location_of (id
));
3385 richloc
.add_fixit_replace (suggestion
);
3386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3390 "%qE in namespace %qE does not name a template"
3391 " type; did you mean %qs?",
3392 id
, parser
->scope
, suggestion
);
3395 "%qE in namespace %qE does not name a template type",
3398 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3402 "%qE in namespace %qE does not name a template"
3403 " type; did you mean %qs?",
3404 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3407 "%qE in namespace %qE does not name a template"
3409 TREE_OPERAND (id
, 0), parser
->scope
);
3415 "%qE in namespace %qE does not name a type"
3416 "; did you mean %qs?",
3417 id
, parser
->scope
, suggestion
);
3420 "%qE in namespace %qE does not name a type",
3424 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3426 else if (CLASS_TYPE_P (parser
->scope
)
3427 && constructor_name_p (id
, parser
->scope
))
3430 auto_diagnostic_group d
;
3431 error_at (location
, "%<%T::%E%> names the constructor, not"
3432 " the type", parser
->scope
, id
);
3433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3434 error_at (location
, "and %qT has no template constructors",
3437 else if (TYPE_P (parser
->scope
)
3438 && dependent_scope_p (parser
->scope
))
3440 gcc_rich_location
richloc (location
);
3441 richloc
.add_fixit_insert_before ("typename ");
3442 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3444 "need %<typename%> before %<%T::%D::%E%> because "
3445 "%<%T::%D%> is a dependent scope",
3446 TYPE_CONTEXT (parser
->scope
),
3447 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3449 TYPE_CONTEXT (parser
->scope
),
3450 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3452 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3453 "%qT is a dependent scope",
3454 parser
->scope
, id
, parser
->scope
);
3456 else if (TYPE_P (parser
->scope
))
3458 auto_diagnostic_group d
;
3459 if (!COMPLETE_TYPE_P (parser
->scope
))
3460 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3462 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3463 error_at (location_of (id
),
3464 "%qE in %q#T does not name a template type",
3466 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3467 error_at (location_of (id
),
3468 "%qE in %q#T does not name a template type",
3469 TREE_OPERAND (id
, 0), parser
->scope
);
3471 error_at (location_of (id
),
3472 "%qE in %q#T does not name a type",
3475 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3482 /* Check for a common situation where a type-name should be present,
3483 but is not, and issue a sensible error message. Returns true if an
3484 invalid type-name was detected.
3486 The situation handled by this function are variable declarations of the
3487 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3488 Usually, `ID' should name a type, but if we got here it means that it
3489 does not. We try to emit the best possible error message depending on
3490 how exactly the id-expression looks like. */
3493 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3496 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3498 /* Avoid duplicate error about ambiguous lookup. */
3499 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3501 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3502 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3506 cp_parser_parse_tentatively (parser
);
3507 id
= cp_parser_id_expression (parser
,
3508 /*template_keyword_p=*/false,
3509 /*check_dependency_p=*/true,
3510 /*template_p=*/NULL
,
3511 /*declarator_p=*/false,
3512 /*optional_p=*/false);
3513 /* If the next token is a (, this is a function with no explicit return
3514 type, i.e. constructor, destructor or conversion op. */
3515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3516 || TREE_CODE (id
) == TYPE_DECL
)
3518 cp_parser_abort_tentative_parse (parser
);
3521 if (!cp_parser_parse_definitely (parser
))
3524 /* Emit a diagnostic for the invalid type. */
3525 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3527 /* If we aren't in the middle of a declarator (i.e. in a
3528 parameter-declaration-clause), skip to the end of the declaration;
3529 there's no point in trying to process it. */
3530 if (!parser
->in_declarator_p
)
3531 cp_parser_skip_to_end_of_block_or_statement (parser
);
3535 /* Consume tokens up to, and including, the next non-nested closing `)'.
3536 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3537 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3538 found an unnested token of that type. */
3541 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3546 unsigned paren_depth
= 0;
3547 unsigned brace_depth
= 0;
3548 unsigned square_depth
= 0;
3549 unsigned condop_depth
= 0;
3551 if (recovering
&& or_ttype
== CPP_EOF
3552 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3557 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3559 /* Have we found what we're looking for before the closing paren? */
3560 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3561 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3564 switch (token
->type
)
3566 case CPP_PRAGMA_EOL
:
3567 if (!parser
->lexer
->in_pragma
)
3571 /* If we've run out of tokens, then there is no closing `)'. */
3574 /* This is good for lambda expression capture-lists. */
3575 case CPP_OPEN_SQUARE
:
3578 case CPP_CLOSE_SQUARE
:
3579 if (!square_depth
--)
3584 /* This matches the processing in skip_to_end_of_statement. */
3589 case CPP_OPEN_BRACE
:
3592 case CPP_CLOSE_BRACE
:
3597 case CPP_OPEN_PAREN
:
3602 case CPP_CLOSE_PAREN
:
3603 if (!brace_depth
&& !paren_depth
--)
3606 cp_lexer_consume_token (parser
->lexer
);
3612 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3617 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3625 /* Consume the token. */
3626 cp_lexer_consume_token (parser
->lexer
);
3630 /* Consume tokens up to, and including, the next non-nested closing `)'.
3631 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3632 are doing error recovery. Returns -1 if OR_COMMA is true and we
3633 found an unnested token of that type. */
3636 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3641 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3642 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3643 ttype
, consume_paren
);
3646 /* Consume tokens until we reach the end of the current statement.
3647 Normally, that will be just before consuming a `;'. However, if a
3648 non-nested `}' comes first, then we stop before consuming that. */
3651 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3653 unsigned nesting_depth
= 0;
3655 /* Unwind generic function template scope if necessary. */
3656 if (parser
->fully_implicit_function_template_p
)
3657 abort_fully_implicit_template (parser
);
3661 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3663 switch (token
->type
)
3665 case CPP_PRAGMA_EOL
:
3666 if (!parser
->lexer
->in_pragma
)
3670 /* If we've run out of tokens, stop. */
3674 /* If the next token is a `;', we have reached the end of the
3680 case CPP_CLOSE_BRACE
:
3681 /* If this is a non-nested '}', stop before consuming it.
3682 That way, when confronted with something like:
3686 we stop before consuming the closing '}', even though we
3687 have not yet reached a `;'. */
3688 if (nesting_depth
== 0)
3691 /* If it is the closing '}' for a block that we have
3692 scanned, stop -- but only after consuming the token.
3698 we will stop after the body of the erroneously declared
3699 function, but before consuming the following `typedef'
3701 if (--nesting_depth
== 0)
3703 cp_lexer_consume_token (parser
->lexer
);
3708 case CPP_OPEN_BRACE
:
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser
->lexer
);
3721 /* This function is called at the end of a statement or declaration.
3722 If the next token is a semicolon, it is consumed; otherwise, error
3723 recovery is attempted. */
3726 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3728 /* Look for the trailing `;'. */
3729 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3731 /* If there is additional (erroneous) input, skip to the end of
3733 cp_parser_skip_to_end_of_statement (parser
);
3734 /* If the next token is now a `;', consume it. */
3735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3736 cp_lexer_consume_token (parser
->lexer
);
3740 /* Skip tokens until we have consumed an entire block, or until we
3741 have consumed a non-nested `;'. */
3744 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3746 int nesting_depth
= 0;
3748 /* Unwind generic function template scope if necessary. */
3749 if (parser
->fully_implicit_function_template_p
)
3750 abort_fully_implicit_template (parser
);
3752 while (nesting_depth
>= 0)
3754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3756 switch (token
->type
)
3758 case CPP_PRAGMA_EOL
:
3759 if (!parser
->lexer
->in_pragma
)
3763 /* If we've run out of tokens, stop. */
3767 /* Stop if this is an unnested ';'. */
3772 case CPP_CLOSE_BRACE
:
3773 /* Stop if this is an unnested '}', or closes the outermost
3776 if (nesting_depth
< 0)
3782 case CPP_OPEN_BRACE
:
3791 /* Consume the token. */
3792 cp_lexer_consume_token (parser
->lexer
);
3796 /* Skip tokens until a non-nested closing curly brace is the next
3797 token, or there are no more tokens. Return true in the first case,
3801 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3803 unsigned nesting_depth
= 0;
3807 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3809 switch (token
->type
)
3811 case CPP_PRAGMA_EOL
:
3812 if (!parser
->lexer
->in_pragma
)
3816 /* If we've run out of tokens, stop. */
3819 case CPP_CLOSE_BRACE
:
3820 /* If the next token is a non-nested `}', then we have reached
3821 the end of the current block. */
3822 if (nesting_depth
-- == 0)
3826 case CPP_OPEN_BRACE
:
3827 /* If it the next token is a `{', then we are entering a new
3828 block. Consume the entire block. */
3836 /* Consume the token. */
3837 cp_lexer_consume_token (parser
->lexer
);
3841 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3842 parameter is the PRAGMA token, allowing us to purge the entire pragma
3846 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3850 parser
->lexer
->in_pragma
= false;
3853 token
= cp_lexer_consume_token (parser
->lexer
);
3854 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3856 /* Ensure that the pragma is not parsed again. */
3857 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3860 /* Require pragma end of line, resyncing with it as necessary. The
3861 arguments are as for cp_parser_skip_to_pragma_eol. */
3864 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3866 parser
->lexer
->in_pragma
= false;
3867 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3868 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3871 /* This is a simple wrapper around make_typename_type. When the id is
3872 an unresolved identifier node, we can provide a superior diagnostic
3873 using cp_parser_diagnose_invalid_type_name. */
3876 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3877 location_t id_location
)
3880 if (identifier_p (id
))
3882 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3883 /*complain=*/tf_none
);
3884 if (result
== error_mark_node
)
3885 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3888 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3891 /* This is a wrapper around the
3892 make_{pointer,ptrmem,reference}_declarator functions that decides
3893 which one to call based on the CODE and CLASS_TYPE arguments. The
3894 CODE argument should be one of the values returned by
3895 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3896 appertain to the pointer or reference. */
3898 static cp_declarator
*
3899 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3900 cp_cv_quals cv_qualifiers
,
3901 cp_declarator
*target
,
3904 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3905 return cp_error_declarator
;
3907 if (code
== INDIRECT_REF
)
3908 if (class_type
== NULL_TREE
)
3909 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3911 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3912 target
, attributes
);
3913 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3914 return make_reference_declarator (cv_qualifiers
, target
,
3916 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3917 return make_reference_declarator (cv_qualifiers
, target
,
3922 /* Create a new C++ parser. */
3925 cp_parser_new (void)
3931 /* cp_lexer_new_main is called before doing GC allocation because
3932 cp_lexer_new_main might load a PCH file. */
3933 lexer
= cp_lexer_new_main ();
3935 /* Initialize the binops_by_token so that we can get the tree
3936 directly from the token. */
3937 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3938 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3940 parser
= ggc_cleared_alloc
<cp_parser
> ();
3941 parser
->lexer
= lexer
;
3942 parser
->context
= cp_parser_context_new (NULL
);
3944 /* For now, we always accept GNU extensions. */
3945 parser
->allow_gnu_extensions_p
= 1;
3947 /* The `>' token is a greater-than operator, not the end of a
3949 parser
->greater_than_is_operator_p
= true;
3951 parser
->default_arg_ok_p
= true;
3953 /* We are not parsing a constant-expression. */
3954 parser
->integral_constant_expression_p
= false;
3955 parser
->allow_non_integral_constant_expression_p
= false;
3956 parser
->non_integral_constant_expression_p
= false;
3958 /* Local variable names are not forbidden. */
3959 parser
->local_variables_forbidden_p
= 0;
3961 /* We are not processing an `extern "C"' declaration. */
3962 parser
->in_unbraced_linkage_specification_p
= false;
3964 /* We are not processing a declarator. */
3965 parser
->in_declarator_p
= false;
3967 /* We are not processing a template-argument-list. */
3968 parser
->in_template_argument_list_p
= false;
3970 /* We are not in an iteration statement. */
3971 parser
->in_statement
= 0;
3973 /* We are not in a switch statement. */
3974 parser
->in_switch_statement_p
= false;
3976 /* We are not parsing a type-id inside an expression. */
3977 parser
->in_type_id_in_expr_p
= false;
3979 /* String literals should be translated to the execution character set. */
3980 parser
->translate_strings_p
= true;
3982 /* We are not parsing a function body. */
3983 parser
->in_function_body
= false;
3985 /* We can correct until told otherwise. */
3986 parser
->colon_corrects_to_scope_p
= true;
3988 /* The unparsed function queue is empty. */
3989 push_unparsed_function_queues (parser
);
3991 /* There are no classes being defined. */
3992 parser
->num_classes_being_defined
= 0;
3994 /* No template parameters apply. */
3995 parser
->num_template_parameter_lists
= 0;
3997 /* Special parsing data structures. */
3998 parser
->omp_declare_simd
= NULL
;
3999 parser
->oacc_routine
= NULL
;
4001 /* Not declaring an implicit function template. */
4002 parser
->auto_is_implicit_function_template_parm_p
= false;
4003 parser
->fully_implicit_function_template_p
= false;
4004 parser
->implicit_template_parms
= 0;
4005 parser
->implicit_template_scope
= 0;
4007 /* Allow constrained-type-specifiers. */
4008 parser
->prevent_constrained_type_specifiers
= 0;
4010 /* We haven't yet seen an 'extern "C"'. */
4011 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4016 /* Create a cp_lexer structure which will emit the tokens in CACHE
4017 and push it onto the parser's lexer stack. This is used for delayed
4018 parsing of in-class method bodies and default arguments, and should
4019 not be confused with tentative parsing. */
4021 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4023 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4024 lexer
->next
= parser
->lexer
;
4025 parser
->lexer
= lexer
;
4027 /* Move the current source position to that of the first token in the
4029 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4032 /* Pop the top lexer off the parser stack. This is never used for the
4033 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4035 cp_parser_pop_lexer (cp_parser
*parser
)
4037 cp_lexer
*lexer
= parser
->lexer
;
4038 parser
->lexer
= lexer
->next
;
4039 cp_lexer_destroy (lexer
);
4041 /* Put the current source position back where it was before this
4042 lexer was pushed. */
4043 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4046 /* Lexical conventions [gram.lex] */
4048 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4052 cp_parser_identifier (cp_parser
* parser
)
4056 /* Look for the identifier. */
4057 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4058 /* Return the value. */
4060 return cp_expr (token
->u
.value
, token
->location
);
4062 return error_mark_node
;
4065 /* Parse a sequence of adjacent string constants. Returns a
4066 TREE_STRING representing the combined, nul-terminated string
4067 constant. If TRANSLATE is true, translate the string to the
4068 execution character set. If WIDE_OK is true, a wide string is
4071 C++98 [lex.string] says that if a narrow string literal token is
4072 adjacent to a wide string literal token, the behavior is undefined.
4073 However, C99 6.4.5p4 says that this results in a wide string literal.
4074 We follow C99 here, for consistency with the C front end.
4076 This code is largely lifted from lex_string() in c-lex.c.
4078 FUTURE: ObjC++ will need to handle @-strings here. */
4080 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4081 bool lookup_udlit
= true)
4085 struct obstack str_ob
;
4086 struct obstack loc_ob
;
4087 cpp_string str
, istr
, *strs
;
4089 enum cpp_ttype type
, curr_type
;
4090 int have_suffix_p
= 0;
4092 tree suffix_id
= NULL_TREE
;
4093 bool curr_tok_is_userdef_p
= false;
4095 tok
= cp_lexer_peek_token (parser
->lexer
);
4096 if (!cp_parser_is_string_literal (tok
))
4098 cp_parser_error (parser
, "expected string-literal");
4099 return error_mark_node
;
4102 location_t loc
= tok
->location
;
4104 if (cpp_userdef_string_p (tok
->type
))
4106 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4107 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4108 curr_tok_is_userdef_p
= true;
4112 string_tree
= tok
->u
.value
;
4113 curr_type
= tok
->type
;
4117 /* Try to avoid the overhead of creating and destroying an obstack
4118 for the common case of just one string. */
4119 if (!cp_parser_is_string_literal
4120 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4122 cp_lexer_consume_token (parser
->lexer
);
4124 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4125 str
.len
= TREE_STRING_LENGTH (string_tree
);
4128 if (curr_tok_is_userdef_p
)
4130 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4132 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4135 curr_type
= tok
->type
;
4141 location_t last_tok_loc
= tok
->location
;
4142 gcc_obstack_init (&str_ob
);
4143 gcc_obstack_init (&loc_ob
);
4148 cp_lexer_consume_token (parser
->lexer
);
4150 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4151 str
.len
= TREE_STRING_LENGTH (string_tree
);
4153 if (curr_tok_is_userdef_p
)
4155 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4156 if (have_suffix_p
== 0)
4158 suffix_id
= curr_suffix_id
;
4161 else if (have_suffix_p
== 1
4162 && curr_suffix_id
!= suffix_id
)
4164 error ("inconsistent user-defined literal suffixes"
4165 " %qD and %qD in string literal",
4166 suffix_id
, curr_suffix_id
);
4169 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4172 curr_type
= tok
->type
;
4174 if (type
!= curr_type
)
4176 if (type
== CPP_STRING
)
4178 else if (curr_type
!= CPP_STRING
)
4180 rich_location
rich_loc (line_table
, tok
->location
);
4181 rich_loc
.add_range (last_tok_loc
);
4182 error_at (&rich_loc
,
4183 "unsupported non-standard concatenation "
4184 "of string literals");
4188 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4189 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4191 last_tok_loc
= tok
->location
;
4193 tok
= cp_lexer_peek_token (parser
->lexer
);
4194 if (cpp_userdef_string_p (tok
->type
))
4196 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4197 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4198 curr_tok_is_userdef_p
= true;
4202 string_tree
= tok
->u
.value
;
4203 curr_type
= tok
->type
;
4204 curr_tok_is_userdef_p
= false;
4207 while (cp_parser_is_string_literal (tok
));
4209 /* A string literal built by concatenation has its caret=start at
4210 the start of the initial string, and its finish at the finish of
4211 the final string literal. */
4212 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4214 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4217 if (type
!= CPP_STRING
&& !wide_ok
)
4219 cp_parser_error (parser
, "a wide string is invalid in this context");
4223 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4224 (parse_in
, strs
, count
, &istr
, type
))
4226 value
= build_string (istr
.len
, (const char *)istr
.text
);
4227 free (CONST_CAST (unsigned char *, istr
.text
));
4230 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4231 gcc_assert (g_string_concat_db
);
4232 g_string_concat_db
->record_string_concatenation (count
, locs
);
4239 TREE_TYPE (value
) = char_array_type_node
;
4241 case CPP_UTF8STRING
:
4243 TREE_TYPE (value
) = char8_array_type_node
;
4245 TREE_TYPE (value
) = char_array_type_node
;
4248 TREE_TYPE (value
) = char16_array_type_node
;
4251 TREE_TYPE (value
) = char32_array_type_node
;
4254 TREE_TYPE (value
) = wchar_array_type_node
;
4258 value
= fix_string_type (value
);
4262 tree literal
= build_userdef_literal (suffix_id
, value
,
4263 OT_NONE
, NULL_TREE
);
4265 value
= cp_parser_userdef_string_literal (literal
);
4271 /* cpp_interpret_string has issued an error. */
4272 value
= error_mark_node
;
4276 obstack_free (&str_ob
, 0);
4277 obstack_free (&loc_ob
, 0);
4280 return cp_expr (value
, loc
);
4283 /* Look up a literal operator with the name and the exact arguments. */
4286 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4288 tree decl
= lookup_name (name
);
4289 if (!decl
|| !is_overloaded_fn (decl
))
4290 return error_mark_node
;
4292 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4296 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4302 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4303 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4305 tree tparm
= TREE_VALUE (parmtypes
);
4306 tree targ
= TREE_TYPE ((*args
)[ix
]);
4307 bool ptr
= TYPE_PTR_P (tparm
);
4308 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4309 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4311 || !same_type_p (TREE_TYPE (tparm
),
4317 && ix
== vec_safe_length (args
)
4318 /* May be this should be sufficient_parms_p instead,
4319 depending on how exactly should user-defined literals
4320 work in presence of default arguments on the literal
4321 operator parameters. */
4322 && parmtypes
== void_list_node
)
4327 return error_mark_node
;
4330 /* Parse a user-defined char constant. Returns a call to a user-defined
4331 literal operator taking the character as an argument. */
4334 cp_parser_userdef_char_literal (cp_parser
*parser
)
4336 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4337 tree literal
= token
->u
.value
;
4338 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4339 tree value
= USERDEF_LITERAL_VALUE (literal
);
4340 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4343 /* Build up a call to the user-defined operator */
4344 /* Lookup the name we got back from the id-expression. */
4345 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4346 vec_safe_push (args
, value
);
4347 decl
= lookup_literal_operator (name
, args
);
4348 if (!decl
|| decl
== error_mark_node
)
4350 error ("unable to find character literal operator %qD with %qT argument",
4351 name
, TREE_TYPE (value
));
4352 release_tree_vector (args
);
4353 return error_mark_node
;
4355 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4356 release_tree_vector (args
);
4360 /* A subroutine of cp_parser_userdef_numeric_literal to
4361 create a char... template parameter pack from a string node. */
4364 make_char_string_pack (tree value
)
4367 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4368 const char *str
= TREE_STRING_POINTER (value
);
4369 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4370 tree argvec
= make_tree_vec (1);
4372 /* Fill in CHARVEC with all of the parameters. */
4373 charvec
= make_tree_vec (len
);
4374 for (i
= 0; i
< len
; ++i
)
4376 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4377 cpp_string in
= { 3, s
};
4378 cpp_string out
= { 0, 0 };
4379 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4381 gcc_assert (out
.len
== 2);
4382 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4386 /* Build the argument packs. */
4387 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4389 TREE_VEC_ELT (argvec
, 0) = argpack
;
4394 /* A subroutine of cp_parser_userdef_numeric_literal to
4395 create a char... template parameter pack from a string node. */
4398 make_string_pack (tree value
)
4401 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4402 const unsigned char *str
4403 = (const unsigned char *) TREE_STRING_POINTER (value
);
4404 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4405 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4406 tree argvec
= make_tree_vec (2);
4408 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4409 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4411 /* First template parm is character type. */
4412 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4414 /* Fill in CHARVEC with all of the parameters. */
4415 charvec
= make_tree_vec (len
);
4416 for (int i
= 0; i
< len
; ++i
)
4417 TREE_VEC_ELT (charvec
, i
)
4418 = double_int_to_tree (str_char_type_node
,
4419 double_int::from_buffer (str
+ i
* sz
, sz
));
4421 /* Build the argument packs. */
4422 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4424 TREE_VEC_ELT (argvec
, 1) = argpack
;
4429 /* Parse a user-defined numeric constant. returns a call to a user-defined
4430 literal operator. */
4433 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4435 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4436 tree literal
= token
->u
.value
;
4437 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4438 tree value
= USERDEF_LITERAL_VALUE (literal
);
4439 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4440 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4441 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4443 vec
<tree
, va_gc
> *args
;
4445 /* Look for a literal operator taking the exact type of numeric argument
4446 as the literal value. */
4447 args
= make_tree_vector ();
4448 vec_safe_push (args
, value
);
4449 decl
= lookup_literal_operator (name
, args
);
4450 if (decl
&& decl
!= error_mark_node
)
4452 result
= finish_call_expr (decl
, &args
, false, true,
4453 tf_warning_or_error
);
4455 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4457 warning_at (token
->location
, OPT_Woverflow
,
4458 "integer literal exceeds range of %qT type",
4459 long_long_unsigned_type_node
);
4464 warning_at (token
->location
, OPT_Woverflow
,
4465 "floating literal exceeds range of %qT type",
4466 long_double_type_node
);
4467 else if (overflow
< 0)
4468 warning_at (token
->location
, OPT_Woverflow
,
4469 "floating literal truncated to zero");
4472 release_tree_vector (args
);
4475 release_tree_vector (args
);
4477 /* If the numeric argument didn't work, look for a raw literal
4478 operator taking a const char* argument consisting of the number
4479 in string format. */
4480 args
= make_tree_vector ();
4481 vec_safe_push (args
, num_string
);
4482 decl
= lookup_literal_operator (name
, args
);
4483 if (decl
&& decl
!= error_mark_node
)
4485 result
= finish_call_expr (decl
, &args
, false, true,
4486 tf_warning_or_error
);
4487 release_tree_vector (args
);
4490 release_tree_vector (args
);
4492 /* If the raw literal didn't work, look for a non-type template
4493 function with parameter pack char.... Call the function with
4494 template parameter characters representing the number. */
4495 args
= make_tree_vector ();
4496 decl
= lookup_literal_operator (name
, args
);
4497 if (decl
&& decl
!= error_mark_node
)
4499 tree tmpl_args
= make_char_string_pack (num_string
);
4500 if (tmpl_args
== NULL_TREE
)
4502 error ("failed to translate literal to execution character set %qT",
4504 return error_mark_node
;
4506 decl
= lookup_template_function (decl
, tmpl_args
);
4507 result
= finish_call_expr (decl
, &args
, false, true,
4508 tf_warning_or_error
);
4509 release_tree_vector (args
);
4513 release_tree_vector (args
);
4515 /* In C++14 the standard library defines complex number suffixes that
4516 conflict with GNU extensions. Prefer them if <complex> is #included. */
4517 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4518 bool i14
= (cxx_dialect
> cxx11
4519 && (id_equal (suffix_id
, "i")
4520 || id_equal (suffix_id
, "if")
4521 || id_equal (suffix_id
, "il")));
4522 diagnostic_t kind
= DK_ERROR
;
4527 tree cxlit
= lookup_qualified_name (std_node
,
4528 get_identifier ("complex_literals"),
4530 if (cxlit
== error_mark_node
)
4532 /* No <complex>, so pedwarn and use GNU semantics. */
4534 opt
= OPT_Wpedantic
;
4539 = emit_diagnostic (kind
, input_location
, opt
,
4540 "unable to find numeric literal operator %qD", name
);
4543 /* Don't inform either. */;
4546 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4547 "(from <complex>) to enable the C++14 user-defined literal "
4550 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4551 "GNU built-in suffix");
4554 inform (token
->location
, "use %<-fext-numeric-literals%> "
4555 "to enable more built-in suffixes");
4557 if (kind
== DK_ERROR
)
4558 value
= error_mark_node
;
4561 /* Use the built-in semantics. */
4563 if (id_equal (suffix_id
, "i"))
4565 if (TREE_CODE (value
) == INTEGER_CST
)
4566 type
= integer_type_node
;
4568 type
= double_type_node
;
4570 else if (id_equal (suffix_id
, "if"))
4571 type
= float_type_node
;
4572 else /* if (id_equal (suffix_id, "il")) */
4573 type
= long_double_type_node
;
4575 value
= build_complex (build_complex_type (type
),
4576 fold_convert (type
, integer_zero_node
),
4577 fold_convert (type
, value
));
4580 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4581 /* Avoid repeated diagnostics. */
4582 token
->u
.value
= value
;
4586 /* Parse a user-defined string constant. Returns a call to a user-defined
4587 literal operator taking a character pointer and the length of the string
4591 cp_parser_userdef_string_literal (tree literal
)
4593 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4594 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4595 tree value
= USERDEF_LITERAL_VALUE (literal
);
4596 int len
= TREE_STRING_LENGTH (value
)
4597 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4600 /* Build up a call to the user-defined operator. */
4601 /* Lookup the name we got back from the id-expression. */
4602 releasing_vec rargs
;
4603 vec
<tree
, va_gc
> *&args
= rargs
.get_ref();
4604 vec_safe_push (args
, value
);
4605 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4606 decl
= lookup_literal_operator (name
, args
);
4608 if (decl
&& decl
!= error_mark_node
)
4609 return finish_call_expr (decl
, &args
, false, true,
4610 tf_warning_or_error
);
4612 /* Look for a suitable template function, either (C++20) with a single
4613 parameter of class type, or (N3599) with typename parameter CharT and
4614 parameter pack CharT... */
4616 decl
= lookup_literal_operator (name
, args
);
4617 if (decl
&& decl
!= error_mark_node
)
4619 /* Use resolve_nondeduced_context to try to choose one form of template
4621 tree tmpl_args
= make_tree_vec (1);
4622 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4623 decl
= lookup_template_function (decl
, tmpl_args
);
4624 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4629 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4630 res
= resolve_nondeduced_context (decl
, tf_none
);
4634 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4635 TREE_OPERAND (decl
, 1) = tmpl_args
;
4636 return finish_call_expr (decl
, &args
, false, true,
4637 tf_warning_or_error
);
4640 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4641 name
, TREE_TYPE (value
), size_type_node
);
4642 return error_mark_node
;
4646 /* Basic concepts [gram.basic] */
4648 /* Parse a translation-unit.
4651 declaration-seq [opt] */
4654 cp_parser_translation_unit (cp_parser
* parser
)
4656 gcc_checking_assert (!cp_error_declarator
);
4658 /* Create the declarator obstack. */
4659 gcc_obstack_init (&declarator_obstack
);
4660 /* Create the error declarator. */
4661 cp_error_declarator
= make_declarator (cdk_error
);
4662 /* Create the empty parameter list. */
4663 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4665 /* Remember where the base of the declarator obstack lies. */
4666 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4668 bool implicit_extern_c
= false;
4672 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4674 /* If we're entering or exiting a region that's implicitly
4675 extern "C", modify the lang context appropriately. */
4676 if (implicit_extern_c
4677 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4679 implicit_extern_c
= !implicit_extern_c
;
4680 if (implicit_extern_c
)
4681 push_lang_context (lang_name_c
);
4683 pop_lang_context ();
4686 if (token
->type
== CPP_EOF
)
4689 if (token
->type
== CPP_CLOSE_BRACE
)
4691 cp_parser_error (parser
, "expected declaration");
4692 cp_lexer_consume_token (parser
->lexer
);
4693 /* If the next token is now a `;', consume it. */
4694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4695 cp_lexer_consume_token (parser
->lexer
);
4698 cp_parser_toplevel_declaration (parser
);
4701 /* Get rid of the token array; we don't need it any more. */
4702 cp_lexer_destroy (parser
->lexer
);
4703 parser
->lexer
= NULL
;
4705 /* The EOF should have reset this. */
4706 gcc_checking_assert (!implicit_extern_c
);
4708 /* Make sure the declarator obstack was fully cleaned up. */
4709 gcc_assert (obstack_next_free (&declarator_obstack
)
4710 == declarator_obstack_base
);
4713 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4714 decltype context. */
4716 static inline tsubst_flags_t
4717 complain_flags (bool decltype_p
)
4719 tsubst_flags_t complain
= tf_warning_or_error
;
4721 complain
|= tf_decltype
;
4725 /* We're about to parse a collection of statements. If we're currently
4726 parsing tentatively, set up a firewall so that any nested
4727 cp_parser_commit_to_tentative_parse won't affect the current context. */
4729 static cp_token_position
4730 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4732 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4735 cp_parser_parse_tentatively (parser
);
4736 cp_parser_commit_to_topmost_tentative_parse (parser
);
4737 return cp_lexer_token_position (parser
->lexer
, false);
4740 /* We've finished parsing the collection of statements. Wrap up the
4741 firewall and replace the relevant tokens with the parsed form. */
4744 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4750 /* Finish the firewall level. */
4751 cp_parser_parse_definitely (parser
);
4752 /* And remember the result of the parse for when we try again. */
4753 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4754 token
->type
= CPP_PREPARSED_EXPR
;
4755 token
->u
.value
= expr
;
4756 token
->keyword
= RID_MAX
;
4757 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4760 /* Like the above functions, but let the user modify the tokens. Used by
4761 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4762 later parses, so it makes sense to localize the effects of
4763 cp_parser_commit_to_tentative_parse. */
4765 struct tentative_firewall
4770 tentative_firewall (cp_parser
*p
): parser(p
)
4772 /* If we're currently parsing tentatively, start a committed level as a
4773 firewall and then an inner tentative parse. */
4774 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4776 cp_parser_parse_tentatively (parser
);
4777 cp_parser_commit_to_topmost_tentative_parse (parser
);
4778 cp_parser_parse_tentatively (parser
);
4782 ~tentative_firewall()
4786 /* Finish the inner tentative parse and the firewall, propagating any
4787 uncommitted error state to the outer tentative parse. */
4788 bool err
= cp_parser_error_occurred (parser
);
4789 cp_parser_parse_definitely (parser
);
4790 cp_parser_parse_definitely (parser
);
4792 cp_parser_simulate_error (parser
);
4797 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4798 This class is for tracking such a matching pair of symbols.
4799 In particular, it tracks the location of the first token,
4800 so that if the second token is missing, we can highlight the
4801 location of the first token when notifying the user about the
4804 template <typename traits_t
>
4808 /* token_pair's ctor. */
4809 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4811 /* If the next token is the opening symbol for this pair, consume it and
4813 Otherwise, issue an error and return false.
4814 In either case, record the location of the opening token. */
4816 bool require_open (cp_parser
*parser
)
4818 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4819 return cp_parser_require (parser
, traits_t::open_token_type
,
4820 traits_t::required_token_open
);
4823 /* Consume the next token from PARSER, recording its location as
4824 that of the opening token within the pair. */
4826 cp_token
* consume_open (cp_parser
*parser
)
4828 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4829 gcc_assert (tok
->type
== traits_t::open_token_type
);
4830 m_open_loc
= tok
->location
;
4834 /* If the next token is the closing symbol for this pair, consume it
4836 Otherwise, issue an error, highlighting the location of the
4837 corresponding opening token, and return NULL. */
4839 cp_token
*require_close (cp_parser
*parser
) const
4841 return cp_parser_require (parser
, traits_t::close_token_type
,
4842 traits_t::required_token_close
,
4847 location_t m_open_loc
;
4850 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4852 struct matching_paren_traits
4854 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4855 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4856 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4857 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4860 /* "matching_parens" is a token_pair<T> class for tracking matching
4861 pairs of parentheses. */
4863 typedef token_pair
<matching_paren_traits
> matching_parens
;
4865 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4867 struct matching_brace_traits
4869 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4870 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4871 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4872 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4875 /* "matching_braces" is a token_pair<T> class for tracking matching
4878 typedef token_pair
<matching_brace_traits
> matching_braces
;
4881 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4882 enclosing parentheses. */
4885 cp_parser_statement_expr (cp_parser
*parser
)
4887 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4889 /* Consume the '('. */
4890 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4891 matching_parens parens
;
4892 parens
.consume_open (parser
);
4893 /* Start the statement-expression. */
4894 tree expr
= begin_stmt_expr ();
4895 /* Parse the compound-statement. */
4896 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4898 expr
= finish_stmt_expr (expr
, false);
4899 /* Consume the ')'. */
4900 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4901 if (!parens
.require_close (parser
))
4902 cp_parser_skip_to_end_of_statement (parser
);
4904 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4905 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4906 return cp_expr (expr
, combined_loc
);
4909 /* Expressions [gram.expr] */
4911 /* Parse a fold-operator.
4914 - * / % ^ & | = < > << >>
4915 = -= *= /= %= ^= &= |= <<= >>=
4916 == != <= >= && || , .* ->*
4918 This returns the tree code corresponding to the matched operator
4919 as an int. When the current token matches a compound assignment
4920 opertor, the resulting tree code is the negative value of the
4921 non-assignment operator. */
4924 cp_parser_fold_operator (cp_token
*token
)
4926 switch (token
->type
)
4928 case CPP_PLUS
: return PLUS_EXPR
;
4929 case CPP_MINUS
: return MINUS_EXPR
;
4930 case CPP_MULT
: return MULT_EXPR
;
4931 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4932 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4933 case CPP_XOR
: return BIT_XOR_EXPR
;
4934 case CPP_AND
: return BIT_AND_EXPR
;
4935 case CPP_OR
: return BIT_IOR_EXPR
;
4936 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4937 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4939 case CPP_EQ
: return -NOP_EXPR
;
4940 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4941 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4942 case CPP_MULT_EQ
: return -MULT_EXPR
;
4943 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4944 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4945 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4946 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4947 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4948 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4949 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4951 case CPP_EQ_EQ
: return EQ_EXPR
;
4952 case CPP_NOT_EQ
: return NE_EXPR
;
4953 case CPP_LESS
: return LT_EXPR
;
4954 case CPP_GREATER
: return GT_EXPR
;
4955 case CPP_LESS_EQ
: return LE_EXPR
;
4956 case CPP_GREATER_EQ
: return GE_EXPR
;
4958 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4959 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4961 case CPP_COMMA
: return COMPOUND_EXPR
;
4963 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4964 case CPP_DEREF_STAR
: return MEMBER_REF
;
4966 default: return ERROR_MARK
;
4970 /* Returns true if CODE indicates a binary expression, which is not allowed in
4971 the LHS of a fold-expression. More codes will need to be added to use this
4972 function in other contexts. */
4975 is_binary_op (tree_code code
)
4980 case POINTER_PLUS_EXPR
:
4983 case TRUNC_DIV_EXPR
:
4984 case TRUNC_MOD_EXPR
:
5000 case TRUTH_ANDIF_EXPR
:
5001 case TRUTH_ORIF_EXPR
:
5014 /* If the next token is a suitable fold operator, consume it and return as
5015 the function above. */
5018 cp_parser_fold_operator (cp_parser
*parser
)
5020 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5021 int code
= cp_parser_fold_operator (token
);
5022 if (code
!= ERROR_MARK
)
5023 cp_lexer_consume_token (parser
->lexer
);
5027 /* Parse a fold-expression.
5030 ( ... folding-operator cast-expression)
5031 ( cast-expression folding-operator ... )
5032 ( cast-expression folding operator ... folding-operator cast-expression)
5034 Note that the '(' and ')' are matched in primary expression. */
5037 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5044 cp_lexer_consume_token (parser
->lexer
);
5045 int op
= cp_parser_fold_operator (parser
);
5046 if (op
== ERROR_MARK
)
5048 cp_parser_error (parser
, "expected binary operator");
5049 return error_mark_node
;
5052 tree expr
= cp_parser_cast_expression (parser
, false, false,
5054 if (expr
== error_mark_node
)
5055 return error_mark_node
;
5056 return finish_left_unary_fold_expr (expr
, op
);
5059 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5060 int op
= cp_parser_fold_operator (parser
);
5061 if (op
== ERROR_MARK
)
5063 cp_parser_error (parser
, "expected binary operator");
5064 return error_mark_node
;
5067 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5069 cp_parser_error (parser
, "expected ...");
5070 return error_mark_node
;
5072 cp_lexer_consume_token (parser
->lexer
);
5074 /* The operands of a fold-expression are cast-expressions, so binary or
5075 conditional expressions are not allowed. We check this here to avoid
5076 tentative parsing. */
5077 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5078 /* OK, the expression was parenthesized. */;
5079 else if (is_binary_op (TREE_CODE (expr1
)))
5080 error_at (location_of (expr1
),
5081 "binary expression in operand of fold-expression");
5082 else if (TREE_CODE (expr1
) == COND_EXPR
5083 || (REFERENCE_REF_P (expr1
)
5084 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5085 error_at (location_of (expr1
),
5086 "conditional expression in operand of fold-expression");
5089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5090 return finish_right_unary_fold_expr (expr1
, op
);
5092 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5094 cp_parser_error (parser
, "mismatched operator in fold-expression");
5095 return error_mark_node
;
5097 cp_lexer_consume_token (parser
->lexer
);
5099 // Binary left or right fold.
5100 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5101 if (expr2
== error_mark_node
)
5102 return error_mark_node
;
5103 return finish_binary_fold_expr (expr1
, expr2
, op
);
5106 /* Parse a primary-expression.
5113 lambda-expression (C++11)
5118 ( compound-statement )
5119 __builtin_va_arg ( assignment-expression , type-id )
5120 __builtin_offsetof ( type-id , offsetof-expression )
5123 __has_nothrow_assign ( type-id )
5124 __has_nothrow_constructor ( type-id )
5125 __has_nothrow_copy ( type-id )
5126 __has_trivial_assign ( type-id )
5127 __has_trivial_constructor ( type-id )
5128 __has_trivial_copy ( type-id )
5129 __has_trivial_destructor ( type-id )
5130 __has_virtual_destructor ( type-id )
5131 __is_abstract ( type-id )
5132 __is_base_of ( type-id , type-id )
5133 __is_class ( type-id )
5134 __is_empty ( type-id )
5135 __is_enum ( type-id )
5136 __is_final ( type-id )
5137 __is_literal_type ( type-id )
5138 __is_pod ( type-id )
5139 __is_polymorphic ( type-id )
5140 __is_std_layout ( type-id )
5141 __is_trivial ( type-id )
5142 __is_union ( type-id )
5144 Objective-C++ Extension:
5152 ADDRESS_P is true iff this expression was immediately preceded by
5153 "&" and therefore might denote a pointer-to-member. CAST_P is true
5154 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5155 true iff this expression is a template argument.
5157 Returns a representation of the expression. Upon return, *IDK
5158 indicates what kind of id-expression (if any) was present. */
5161 cp_parser_primary_expression (cp_parser
*parser
,
5164 bool template_arg_p
,
5168 cp_token
*token
= NULL
;
5170 /* Assume the primary expression is not an id-expression. */
5171 *idk
= CP_ID_KIND_NONE
;
5173 /* Peek at the next token. */
5174 token
= cp_lexer_peek_token (parser
->lexer
);
5175 switch ((int) token
->type
)
5184 user-defined-literal */
5191 case CPP_PREPARSED_EXPR
:
5192 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5193 return cp_parser_userdef_numeric_literal (parser
);
5194 token
= cp_lexer_consume_token (parser
->lexer
);
5195 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5197 error_at (token
->location
,
5198 "fixed-point types not supported in C++");
5199 return error_mark_node
;
5201 /* Floating-point literals are only allowed in an integral
5202 constant expression if they are cast to an integral or
5203 enumeration type. */
5204 if (TREE_CODE (token
->u
.value
) == REAL_CST
5205 && parser
->integral_constant_expression_p
5208 /* CAST_P will be set even in invalid code like "int(2.7 +
5209 ...)". Therefore, we have to check that the next token
5210 is sure to end the cast. */
5213 cp_token
*next_token
;
5215 next_token
= cp_lexer_peek_token (parser
->lexer
);
5216 if (/* The comma at the end of an
5217 enumerator-definition. */
5218 next_token
->type
!= CPP_COMMA
5219 /* The curly brace at the end of an enum-specifier. */
5220 && next_token
->type
!= CPP_CLOSE_BRACE
5221 /* The end of a statement. */
5222 && next_token
->type
!= CPP_SEMICOLON
5223 /* The end of the cast-expression. */
5224 && next_token
->type
!= CPP_CLOSE_PAREN
5225 /* The end of an array bound. */
5226 && next_token
->type
!= CPP_CLOSE_SQUARE
5227 /* The closing ">" in a template-argument-list. */
5228 && (next_token
->type
!= CPP_GREATER
5229 || parser
->greater_than_is_operator_p
)
5230 /* C++0x only: A ">>" treated like two ">" tokens,
5231 in a template-argument-list. */
5232 && (next_token
->type
!= CPP_RSHIFT
5233 || (cxx_dialect
== cxx98
)
5234 || parser
->greater_than_is_operator_p
))
5238 /* If we are within a cast, then the constraint that the
5239 cast is to an integral or enumeration type will be
5240 checked at that point. If we are not within a cast, then
5241 this code is invalid. */
5243 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5245 return (cp_expr (token
->u
.value
, token
->location
)
5246 .maybe_add_location_wrapper ());
5248 case CPP_CHAR_USERDEF
:
5249 case CPP_CHAR16_USERDEF
:
5250 case CPP_CHAR32_USERDEF
:
5251 case CPP_WCHAR_USERDEF
:
5252 case CPP_UTF8CHAR_USERDEF
:
5253 return cp_parser_userdef_char_literal (parser
);
5259 case CPP_UTF8STRING
:
5260 case CPP_STRING_USERDEF
:
5261 case CPP_STRING16_USERDEF
:
5262 case CPP_STRING32_USERDEF
:
5263 case CPP_WSTRING_USERDEF
:
5264 case CPP_UTF8STRING_USERDEF
:
5265 /* ??? Should wide strings be allowed when parser->translate_strings_p
5266 is false (i.e. in attributes)? If not, we can kill the third
5267 argument to cp_parser_string_literal. */
5268 return (cp_parser_string_literal (parser
,
5269 parser
->translate_strings_p
,
5271 .maybe_add_location_wrapper ());
5273 case CPP_OPEN_PAREN
:
5274 /* If we see `( { ' then we are looking at the beginning of
5275 a GNU statement-expression. */
5276 if (cp_parser_allow_gnu_extensions_p (parser
)
5277 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5279 /* Statement-expressions are not allowed by the standard. */
5280 pedwarn (token
->location
, OPT_Wpedantic
,
5281 "ISO C++ forbids braced-groups within expressions");
5283 /* And they're not allowed outside of a function-body; you
5284 cannot, for example, write:
5286 int i = ({ int j = 3; j + 1; });
5288 at class or namespace scope. */
5289 if (!parser
->in_function_body
5290 || parser
->in_template_argument_list_p
)
5292 error_at (token
->location
,
5293 "statement-expressions are not allowed outside "
5294 "functions nor in template-argument lists");
5295 cp_parser_skip_to_end_of_block_or_statement (parser
);
5296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5297 cp_lexer_consume_token (parser
->lexer
);
5298 return error_mark_node
;
5301 return cp_parser_statement_expr (parser
);
5303 /* Otherwise it's a normal parenthesized expression. */
5306 bool saved_greater_than_is_operator_p
;
5308 location_t open_paren_loc
= token
->location
;
5310 /* Consume the `('. */
5311 matching_parens parens
;
5312 parens
.consume_open (parser
);
5313 /* Within a parenthesized expression, a `>' token is always
5314 the greater-than operator. */
5315 saved_greater_than_is_operator_p
5316 = parser
->greater_than_is_operator_p
;
5317 parser
->greater_than_is_operator_p
= true;
5319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5320 /* Left fold expression. */
5323 /* Parse the parenthesized expression. */
5324 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5326 token
= cp_lexer_peek_token (parser
->lexer
);
5327 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5329 expr
= cp_parser_fold_expression (parser
, expr
);
5330 if (expr
!= error_mark_node
5331 && cxx_dialect
< cxx17
5332 && !in_system_header_at (input_location
))
5333 pedwarn (input_location
, 0, "fold-expressions only available "
5334 "with %<-std=c++17%> or %<-std=gnu++17%>");
5337 /* Let the front end know that this expression was
5338 enclosed in parentheses. This matters in case, for
5339 example, the expression is of the form `A::B', since
5340 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5342 expr
= finish_parenthesized_expr (expr
);
5344 /* DR 705: Wrapping an unqualified name in parentheses
5345 suppresses arg-dependent lookup. We want to pass back
5346 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5347 (c++/37862), but none of the others. */
5348 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5349 *idk
= CP_ID_KIND_NONE
;
5351 /* The `>' token might be the end of a template-id or
5352 template-parameter-list now. */
5353 parser
->greater_than_is_operator_p
5354 = saved_greater_than_is_operator_p
;
5356 /* Consume the `)'. */
5357 token
= cp_lexer_peek_token (parser
->lexer
);
5358 location_t close_paren_loc
= token
->location
;
5359 expr
.set_range (open_paren_loc
, close_paren_loc
);
5360 if (!parens
.require_close (parser
)
5361 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5362 cp_parser_skip_to_end_of_statement (parser
);
5367 case CPP_OPEN_SQUARE
:
5369 if (c_dialect_objc ())
5371 /* We might have an Objective-C++ message. */
5372 cp_parser_parse_tentatively (parser
);
5373 tree msg
= cp_parser_objc_message_expression (parser
);
5374 /* If that works out, we're done ... */
5375 if (cp_parser_parse_definitely (parser
))
5377 /* ... else, fall though to see if it's a lambda. */
5379 cp_expr lam
= cp_parser_lambda_expression (parser
);
5380 /* Don't warn about a failed tentative parse. */
5381 if (cp_parser_error_occurred (parser
))
5382 return error_mark_node
;
5383 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5387 case CPP_OBJC_STRING
:
5388 if (c_dialect_objc ())
5389 /* We have an Objective-C++ string literal. */
5390 return cp_parser_objc_expression (parser
);
5391 cp_parser_error (parser
, "expected primary-expression");
5392 return error_mark_node
;
5395 switch (token
->keyword
)
5397 /* These two are the boolean literals. */
5399 cp_lexer_consume_token (parser
->lexer
);
5400 return cp_expr (boolean_true_node
, token
->location
);
5402 cp_lexer_consume_token (parser
->lexer
);
5403 return cp_expr (boolean_false_node
, token
->location
);
5405 /* The `__null' literal. */
5407 cp_lexer_consume_token (parser
->lexer
);
5408 return cp_expr (null_node
, token
->location
);
5410 /* The `nullptr' literal. */
5412 cp_lexer_consume_token (parser
->lexer
);
5413 return cp_expr (nullptr_node
, token
->location
);
5415 /* Recognize the `this' keyword. */
5417 cp_lexer_consume_token (parser
->lexer
);
5418 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5420 error_at (token
->location
,
5421 "%<this%> may not be used in this context");
5422 return error_mark_node
;
5424 /* Pointers cannot appear in constant-expressions. */
5425 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5426 return error_mark_node
;
5427 return cp_expr (finish_this_expr (), token
->location
);
5429 /* The `operator' keyword can be the beginning of an
5434 case RID_FUNCTION_NAME
:
5435 case RID_PRETTY_FUNCTION_NAME
:
5436 case RID_C99_FUNCTION_NAME
:
5438 non_integral_constant name
;
5440 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5441 __func__ are the names of variables -- but they are
5442 treated specially. Therefore, they are handled here,
5443 rather than relying on the generic id-expression logic
5444 below. Grammatically, these names are id-expressions.
5446 Consume the token. */
5447 token
= cp_lexer_consume_token (parser
->lexer
);
5449 switch (token
->keyword
)
5451 case RID_FUNCTION_NAME
:
5452 name
= NIC_FUNC_NAME
;
5454 case RID_PRETTY_FUNCTION_NAME
:
5455 name
= NIC_PRETTY_FUNC
;
5457 case RID_C99_FUNCTION_NAME
:
5458 name
= NIC_C99_FUNC
;
5464 if (cp_parser_non_integral_constant_expression (parser
, name
))
5465 return error_mark_node
;
5467 /* Look up the name. */
5468 return finish_fname (token
->u
.value
);
5475 location_t type_location
;
5476 location_t start_loc
5477 = cp_lexer_peek_token (parser
->lexer
)->location
;
5478 /* The `__builtin_va_arg' construct is used to handle
5479 `va_arg'. Consume the `__builtin_va_arg' token. */
5480 cp_lexer_consume_token (parser
->lexer
);
5481 /* Look for the opening `('. */
5482 matching_parens parens
;
5483 parens
.require_open (parser
);
5484 /* Now, parse the assignment-expression. */
5485 expression
= cp_parser_assignment_expression (parser
);
5486 /* Look for the `,'. */
5487 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5488 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5489 /* Parse the type-id. */
5491 type_id_in_expr_sentinel
s (parser
);
5492 type
= cp_parser_type_id (parser
);
5494 /* Look for the closing `)'. */
5495 location_t finish_loc
5496 = cp_lexer_peek_token (parser
->lexer
)->location
;
5497 parens
.require_close (parser
);
5498 /* Using `va_arg' in a constant-expression is not
5500 if (cp_parser_non_integral_constant_expression (parser
,
5502 return error_mark_node
;
5503 /* Construct a location of the form:
5504 __builtin_va_arg (v, int)
5505 ~~~~~~~~~~~~~~~~~~~~~^~~~
5506 with the caret at the type, ranging from the start of the
5507 "__builtin_va_arg" token to the close paren. */
5508 location_t combined_loc
5509 = make_location (type_location
, start_loc
, finish_loc
);
5510 return build_x_va_arg (combined_loc
, expression
, type
);
5514 return cp_parser_builtin_offsetof (parser
);
5516 case RID_HAS_NOTHROW_ASSIGN
:
5517 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5518 case RID_HAS_NOTHROW_COPY
:
5519 case RID_HAS_TRIVIAL_ASSIGN
:
5520 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5521 case RID_HAS_TRIVIAL_COPY
:
5522 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5523 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5524 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5525 case RID_IS_ABSTRACT
:
5526 case RID_IS_AGGREGATE
:
5527 case RID_IS_BASE_OF
:
5532 case RID_IS_LITERAL_TYPE
:
5534 case RID_IS_POLYMORPHIC
:
5535 case RID_IS_SAME_AS
:
5536 case RID_IS_STD_LAYOUT
:
5537 case RID_IS_TRIVIAL
:
5538 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5539 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5540 case RID_IS_TRIVIALLY_COPYABLE
:
5542 case RID_IS_ASSIGNABLE
:
5543 case RID_IS_CONSTRUCTIBLE
:
5544 return cp_parser_trait_expr (parser
, token
->keyword
);
5548 return cp_parser_requires_expression (parser
);
5550 /* Objective-C++ expressions. */
5552 case RID_AT_PROTOCOL
:
5553 case RID_AT_SELECTOR
:
5554 return cp_parser_objc_expression (parser
);
5557 if (parser
->in_function_body
5558 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5561 error_at (token
->location
,
5562 "a template declaration cannot appear at block scope");
5563 cp_parser_skip_to_end_of_block_or_statement (parser
);
5564 return error_mark_node
;
5568 cp_parser_error (parser
, "expected primary-expression");
5569 return error_mark_node
;
5572 /* An id-expression can start with either an identifier, a
5573 `::' as the beginning of a qualified-id, or the "operator"
5577 case CPP_TEMPLATE_ID
:
5578 case CPP_NESTED_NAME_SPECIFIER
:
5581 cp_expr id_expression
;
5583 const char *error_msg
;
5586 cp_token
*id_expr_token
;
5588 /* Parse the id-expression. */
5590 = cp_parser_id_expression (parser
,
5591 /*template_keyword_p=*/false,
5592 /*check_dependency_p=*/true,
5594 /*declarator_p=*/false,
5595 /*optional_p=*/false);
5596 if (id_expression
== error_mark_node
)
5597 return error_mark_node
;
5598 id_expr_token
= token
;
5599 token
= cp_lexer_peek_token (parser
->lexer
);
5600 done
= (token
->type
!= CPP_OPEN_SQUARE
5601 && token
->type
!= CPP_OPEN_PAREN
5602 && token
->type
!= CPP_DOT
5603 && token
->type
!= CPP_DEREF
5604 && token
->type
!= CPP_PLUS_PLUS
5605 && token
->type
!= CPP_MINUS_MINUS
);
5606 /* If we have a template-id, then no further lookup is
5607 required. If the template-id was for a template-class, we
5608 will sometimes have a TYPE_DECL at this point. */
5609 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5610 || TREE_CODE (id_expression
) == TYPE_DECL
)
5611 decl
= id_expression
;
5612 /* Look up the name. */
5615 tree ambiguous_decls
;
5617 /* If we already know that this lookup is ambiguous, then
5618 we've already issued an error message; there's no reason
5620 if (id_expr_token
->type
== CPP_NAME
5621 && id_expr_token
->error_reported
)
5623 cp_parser_simulate_error (parser
);
5624 return error_mark_node
;
5627 decl
= cp_parser_lookup_name (parser
, id_expression
,
5630 /*is_namespace=*/false,
5631 /*check_dependency=*/true,
5633 id_expression
.get_location ());
5634 /* If the lookup was ambiguous, an error will already have
5636 if (ambiguous_decls
)
5637 return error_mark_node
;
5639 /* In Objective-C++, we may have an Objective-C 2.0
5640 dot-syntax for classes here. */
5641 if (c_dialect_objc ()
5642 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5643 && TREE_CODE (decl
) == TYPE_DECL
5644 && objc_is_class_name (decl
))
5647 cp_lexer_consume_token (parser
->lexer
);
5648 component
= cp_parser_identifier (parser
);
5649 if (component
== error_mark_node
)
5650 return error_mark_node
;
5652 tree result
= objc_build_class_component_ref (id_expression
,
5654 /* Build a location of the form:
5657 with caret at the start of the component name (at
5658 input_location), ranging from the start of the id_expression
5659 to the end of the component name. */
5660 location_t combined_loc
5661 = make_location (input_location
, id_expression
.get_start (),
5662 get_finish (input_location
));
5663 protected_set_expr_location (result
, combined_loc
);
5667 /* In Objective-C++, an instance variable (ivar) may be preferred
5668 to whatever cp_parser_lookup_name() found.
5669 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5670 rest of c-family, we have to do a little extra work to preserve
5671 any location information in cp_expr "decl". Given that
5672 objc_lookup_ivar is implemented in "c-family" and "objc", we
5673 have a trip through the pure "tree" type, rather than cp_expr.
5674 Naively copying it back to "decl" would implicitly give the
5675 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5676 store an EXPR_LOCATION. Hence we only update "decl" (and
5677 hence its location_t) if we get back a different tree node. */
5678 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5680 if (decl_tree
!= decl
.get_value ())
5681 decl
= cp_expr (decl_tree
);
5683 /* If name lookup gives us a SCOPE_REF, then the
5684 qualifying scope was dependent. */
5685 if (TREE_CODE (decl
) == SCOPE_REF
)
5687 /* At this point, we do not know if DECL is a valid
5688 integral constant expression. We assume that it is
5689 in fact such an expression, so that code like:
5691 template <int N> struct A {
5695 is accepted. At template-instantiation time, we
5696 will check that B<N>::i is actually a constant. */
5699 /* Check to see if DECL is a local variable in a context
5700 where that is forbidden. */
5701 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5702 && local_variable_p (decl
))
5704 error_at (id_expression
.get_location (),
5705 "local variable %qD may not appear in this context",
5707 return error_mark_node
;
5711 decl
= (finish_id_expression
5712 (id_expression
, decl
, parser
->scope
,
5714 parser
->integral_constant_expression_p
,
5715 parser
->allow_non_integral_constant_expression_p
,
5716 &parser
->non_integral_constant_expression_p
,
5717 template_p
, done
, address_p
,
5720 id_expression
.get_location ()));
5722 cp_parser_error (parser
, error_msg
);
5723 /* Build a location for an id-expression of the form:
5729 i.e. from the start of the first token to the end of the final
5730 token, with the caret at the start of the unqualified-id. */
5731 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5732 location_t start_loc
= get_start (id_expr_token
->location
);
5733 location_t finish_loc
= get_finish (id_expression
.get_location ());
5734 location_t combined_loc
5735 = make_location (caret_loc
, start_loc
, finish_loc
);
5737 decl
.set_location (combined_loc
);
5741 /* Anything else is an error. */
5743 cp_parser_error (parser
, "expected primary-expression");
5744 return error_mark_node
;
5748 static inline cp_expr
5749 cp_parser_primary_expression (cp_parser
*parser
,
5752 bool template_arg_p
,
5755 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5756 /*decltype*/false, idk
);
5759 /* Parse an id-expression.
5766 :: [opt] nested-name-specifier template [opt] unqualified-id
5768 :: operator-function-id
5771 Return a representation of the unqualified portion of the
5772 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5773 a `::' or nested-name-specifier.
5775 Often, if the id-expression was a qualified-id, the caller will
5776 want to make a SCOPE_REF to represent the qualified-id. This
5777 function does not do this in order to avoid wastefully creating
5778 SCOPE_REFs when they are not required.
5780 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5783 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5784 uninstantiated templates.
5786 If *TEMPLATE_P is non-NULL, it is set to true iff the
5787 `template' keyword is used to explicitly indicate that the entity
5788 named is a template.
5790 If DECLARATOR_P is true, the id-expression is appearing as part of
5791 a declarator, rather than as part of an expression. */
5794 cp_parser_id_expression (cp_parser
*parser
,
5795 bool template_keyword_p
,
5796 bool check_dependency_p
,
5801 bool global_scope_p
;
5802 bool nested_name_specifier_p
;
5804 /* Assume the `template' keyword was not used. */
5806 *template_p
= template_keyword_p
;
5808 /* Look for the optional `::' operator. */
5810 = (!template_keyword_p
5811 && (cp_parser_global_scope_opt (parser
,
5812 /*current_scope_valid_p=*/false)
5815 /* Look for the optional nested-name-specifier. */
5816 nested_name_specifier_p
5817 = (cp_parser_nested_name_specifier_opt (parser
,
5818 /*typename_keyword_p=*/false,
5825 /* If there is a nested-name-specifier, then we are looking at
5826 the first qualified-id production. */
5827 if (nested_name_specifier_p
)
5830 tree saved_object_scope
;
5831 tree saved_qualifying_scope
;
5832 cp_expr unqualified_id
;
5835 /* See if the next token is the `template' keyword. */
5837 template_p
= &is_template
;
5838 *template_p
= cp_parser_optional_template_keyword (parser
);
5839 /* Name lookup we do during the processing of the
5840 unqualified-id might obliterate SCOPE. */
5841 saved_scope
= parser
->scope
;
5842 saved_object_scope
= parser
->object_scope
;
5843 saved_qualifying_scope
= parser
->qualifying_scope
;
5844 /* Process the final unqualified-id. */
5845 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5848 /*optional_p=*/false);
5849 /* Restore the SAVED_SCOPE for our caller. */
5850 parser
->scope
= saved_scope
;
5851 parser
->object_scope
= saved_object_scope
;
5852 parser
->qualifying_scope
= saved_qualifying_scope
;
5854 return unqualified_id
;
5856 /* Otherwise, if we are in global scope, then we are looking at one
5857 of the other qualified-id productions. */
5858 else if (global_scope_p
)
5863 /* Peek at the next token. */
5864 token
= cp_lexer_peek_token (parser
->lexer
);
5866 /* If it's an identifier, and the next token is not a "<", then
5867 we can avoid the template-id case. This is an optimization
5868 for this common case. */
5869 if (token
->type
== CPP_NAME
5870 && !cp_parser_nth_token_starts_template_argument_list_p
5872 return cp_parser_identifier (parser
);
5874 cp_parser_parse_tentatively (parser
);
5875 /* Try a template-id. */
5876 id
= cp_parser_template_id (parser
,
5877 /*template_keyword_p=*/false,
5878 /*check_dependency_p=*/true,
5881 /* If that worked, we're done. */
5882 if (cp_parser_parse_definitely (parser
))
5885 /* Peek at the next token. (Changes in the token buffer may
5886 have invalidated the pointer obtained above.) */
5887 token
= cp_lexer_peek_token (parser
->lexer
);
5889 switch (token
->type
)
5892 return cp_parser_identifier (parser
);
5895 if (token
->keyword
== RID_OPERATOR
)
5896 return cp_parser_operator_function_id (parser
);
5900 cp_parser_error (parser
, "expected id-expression");
5901 return error_mark_node
;
5905 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5906 /*check_dependency_p=*/true,
5911 /* Parse an unqualified-id.
5915 operator-function-id
5916 conversion-function-id
5920 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5921 keyword, in a construct like `A::template ...'.
5923 Returns a representation of unqualified-id. For the `identifier'
5924 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5925 production a BIT_NOT_EXPR is returned; the operand of the
5926 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5927 other productions, see the documentation accompanying the
5928 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5929 names are looked up in uninstantiated templates. If DECLARATOR_P
5930 is true, the unqualified-id is appearing as part of a declarator,
5931 rather than as part of an expression. */
5934 cp_parser_unqualified_id (cp_parser
* parser
,
5935 bool template_keyword_p
,
5936 bool check_dependency_p
,
5942 /* Peek at the next token. */
5943 token
= cp_lexer_peek_token (parser
->lexer
);
5945 switch ((int) token
->type
)
5951 /* We don't know yet whether or not this will be a
5953 cp_parser_parse_tentatively (parser
);
5954 /* Try a template-id. */
5955 id
= cp_parser_template_id (parser
, template_keyword_p
,
5959 /* If it worked, we're done. */
5960 if (cp_parser_parse_definitely (parser
))
5962 /* Otherwise, it's an ordinary identifier. */
5963 return cp_parser_identifier (parser
);
5966 case CPP_TEMPLATE_ID
:
5967 return cp_parser_template_id (parser
, template_keyword_p
,
5975 tree qualifying_scope
;
5979 location_t tilde_loc
= token
->location
;
5981 /* Consume the `~' token. */
5982 cp_lexer_consume_token (parser
->lexer
);
5983 /* Parse the class-name. The standard, as written, seems to
5986 template <typename T> struct S { ~S (); };
5987 template <typename T> S<T>::~S() {}
5989 is invalid, since `~' must be followed by a class-name, but
5990 `S<T>' is dependent, and so not known to be a class.
5991 That's not right; we need to look in uninstantiated
5992 templates. A further complication arises from:
5994 template <typename T> void f(T t) {
5998 Here, it is not possible to look up `T' in the scope of `T'
5999 itself. We must look in both the current scope, and the
6000 scope of the containing complete expression.
6002 Yet another issue is:
6011 The standard does not seem to say that the `S' in `~S'
6012 should refer to the type `S' and not the data member
6015 /* DR 244 says that we look up the name after the "~" in the
6016 same scope as we looked up the qualifying name. That idea
6017 isn't fully worked out; it's more complicated than that. */
6018 scope
= parser
->scope
;
6019 object_scope
= parser
->object_scope
;
6020 qualifying_scope
= parser
->qualifying_scope
;
6022 /* Check for invalid scopes. */
6023 if (scope
== error_mark_node
)
6025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6026 cp_lexer_consume_token (parser
->lexer
);
6027 return error_mark_node
;
6029 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6031 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6032 error_at (token
->location
,
6033 "scope %qT before %<~%> is not a class-name",
6035 cp_parser_simulate_error (parser
);
6036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6037 cp_lexer_consume_token (parser
->lexer
);
6038 return error_mark_node
;
6040 gcc_assert (!scope
|| TYPE_P (scope
));
6042 token
= cp_lexer_peek_token (parser
->lexer
);
6044 /* Create a location with caret == start at the tilde,
6045 finishing at the end of the peeked token, e.g:
6049 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6051 /* If the name is of the form "X::~X" it's OK even if X is a
6055 && token
->type
== CPP_NAME
6056 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6058 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6059 || (CLASS_TYPE_P (scope
)
6060 && constructor_name_p (token
->u
.value
, scope
))))
6062 cp_lexer_consume_token (parser
->lexer
);
6063 return cp_expr (build_nt (BIT_NOT_EXPR
, scope
), loc
);
6066 /* ~auto means the destructor of whatever the object is. */
6067 if (cp_parser_is_keyword (token
, RID_AUTO
))
6069 if (cxx_dialect
< cxx14
)
6071 "%<~auto%> only available with "
6072 "%<-std=c++14%> or %<-std=gnu++14%>");
6073 cp_lexer_consume_token (parser
->lexer
);
6074 return cp_expr (build_nt (BIT_NOT_EXPR
, make_auto (), loc
));
6077 /* If there was an explicit qualification (S::~T), first look
6078 in the scope given by the qualification (i.e., S).
6080 Note: in the calls to cp_parser_class_name below we pass
6081 typename_type so that lookup finds the injected-class-name
6082 rather than the constructor. */
6084 type_decl
= NULL_TREE
;
6087 cp_parser_parse_tentatively (parser
);
6088 type_decl
= cp_parser_class_name (parser
,
6089 /*typename_keyword_p=*/false,
6090 /*template_keyword_p=*/false,
6092 /*check_dependency=*/false,
6093 /*class_head_p=*/false,
6095 if (cp_parser_parse_definitely (parser
))
6098 /* In "N::S::~S", look in "N" as well. */
6099 if (!done
&& scope
&& qualifying_scope
)
6101 cp_parser_parse_tentatively (parser
);
6102 parser
->scope
= qualifying_scope
;
6103 parser
->object_scope
= NULL_TREE
;
6104 parser
->qualifying_scope
= NULL_TREE
;
6106 = cp_parser_class_name (parser
,
6107 /*typename_keyword_p=*/false,
6108 /*template_keyword_p=*/false,
6110 /*check_dependency=*/false,
6111 /*class_head_p=*/false,
6113 if (cp_parser_parse_definitely (parser
))
6116 /* In "p->S::~T", look in the scope given by "*p" as well. */
6117 else if (!done
&& object_scope
)
6119 cp_parser_parse_tentatively (parser
);
6120 parser
->scope
= object_scope
;
6121 parser
->object_scope
= NULL_TREE
;
6122 parser
->qualifying_scope
= NULL_TREE
;
6124 = cp_parser_class_name (parser
,
6125 /*typename_keyword_p=*/false,
6126 /*template_keyword_p=*/false,
6128 /*check_dependency=*/false,
6129 /*class_head_p=*/false,
6131 if (cp_parser_parse_definitely (parser
))
6134 /* Look in the surrounding context. */
6137 parser
->scope
= NULL_TREE
;
6138 parser
->object_scope
= NULL_TREE
;
6139 parser
->qualifying_scope
= NULL_TREE
;
6140 if (processing_template_decl
)
6141 cp_parser_parse_tentatively (parser
);
6143 = cp_parser_class_name (parser
,
6144 /*typename_keyword_p=*/false,
6145 /*template_keyword_p=*/false,
6147 /*check_dependency=*/false,
6148 /*class_head_p=*/false,
6150 if (processing_template_decl
6151 && ! cp_parser_parse_definitely (parser
))
6153 /* We couldn't find a type with this name. If we're parsing
6154 tentatively, fail and try something else. */
6155 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6157 cp_parser_simulate_error (parser
);
6158 return error_mark_node
;
6160 /* Otherwise, accept it and check for a match at instantiation
6162 type_decl
= cp_parser_identifier (parser
);
6163 if (type_decl
!= error_mark_node
)
6164 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6165 return cp_expr (type_decl
, loc
);
6168 /* If an error occurred, assume that the name of the
6169 destructor is the same as the name of the qualifying
6170 class. That allows us to keep parsing after running
6171 into ill-formed destructor names. */
6172 if (type_decl
== error_mark_node
&& scope
)
6173 return build_nt (BIT_NOT_EXPR
, scope
);
6174 else if (type_decl
== error_mark_node
)
6175 return error_mark_node
;
6177 /* Check that destructor name and scope match. */
6178 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6180 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6182 "declaration of %<~%T%> as member of %qT",
6184 cp_parser_simulate_error (parser
);
6185 return error_mark_node
;
6190 A typedef-name that names a class shall not be used as the
6191 identifier in the declarator for a destructor declaration. */
6193 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6194 && !DECL_SELF_REFERENCE_P (type_decl
)
6195 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6197 "typedef-name %qD used as destructor declarator",
6200 return cp_expr (build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
), loc
));
6204 if (token
->keyword
== RID_OPERATOR
)
6208 /* This could be a template-id, so we try that first. */
6209 cp_parser_parse_tentatively (parser
);
6210 /* Try a template-id. */
6211 id
= cp_parser_template_id (parser
, template_keyword_p
,
6212 /*check_dependency_p=*/true,
6215 /* If that worked, we're done. */
6216 if (cp_parser_parse_definitely (parser
))
6218 /* We still don't know whether we're looking at an
6219 operator-function-id or a conversion-function-id. */
6220 cp_parser_parse_tentatively (parser
);
6221 /* Try an operator-function-id. */
6222 id
= cp_parser_operator_function_id (parser
);
6223 /* If that didn't work, try a conversion-function-id. */
6224 if (!cp_parser_parse_definitely (parser
))
6225 id
= cp_parser_conversion_function_id (parser
);
6234 cp_parser_error (parser
, "expected unqualified-id");
6235 return error_mark_node
;
6239 /* Parse an (optional) nested-name-specifier.
6241 nested-name-specifier: [C++98]
6242 class-or-namespace-name :: nested-name-specifier [opt]
6243 class-or-namespace-name :: template nested-name-specifier [opt]
6245 nested-name-specifier: [C++0x]
6248 nested-name-specifier identifier ::
6249 nested-name-specifier template [opt] simple-template-id ::
6251 PARSER->SCOPE should be set appropriately before this function is
6252 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6253 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6256 Sets PARSER->SCOPE to the class (TYPE) or namespace
6257 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6258 it unchanged if there is no nested-name-specifier. Returns the new
6259 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6261 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6262 part of a declaration and/or decl-specifier. */
6265 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6266 bool typename_keyword_p
,
6267 bool check_dependency_p
,
6269 bool is_declaration
,
6270 bool template_keyword_p
/* = false */)
6272 bool success
= false;
6273 cp_token_position start
= 0;
6276 /* Remember where the nested-name-specifier starts. */
6277 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6279 start
= cp_lexer_token_position (parser
->lexer
, false);
6280 push_deferring_access_checks (dk_deferred
);
6287 tree saved_qualifying_scope
;
6289 /* Spot cases that cannot be the beginning of a
6290 nested-name-specifier. */
6291 token
= cp_lexer_peek_token (parser
->lexer
);
6293 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6294 the already parsed nested-name-specifier. */
6295 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6297 /* Grab the nested-name-specifier and continue the loop. */
6298 cp_parser_pre_parsed_nested_name_specifier (parser
);
6299 /* If we originally encountered this nested-name-specifier
6300 with IS_DECLARATION set to false, we will not have
6301 resolved TYPENAME_TYPEs, so we must do so here. */
6303 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6305 new_scope
= resolve_typename_type (parser
->scope
,
6306 /*only_current_p=*/false);
6307 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6308 parser
->scope
= new_scope
;
6314 /* Spot cases that cannot be the beginning of a
6315 nested-name-specifier. On the second and subsequent times
6316 through the loop, we look for the `template' keyword. */
6317 if (success
&& token
->keyword
== RID_TEMPLATE
)
6319 /* A template-id can start a nested-name-specifier. */
6320 else if (token
->type
== CPP_TEMPLATE_ID
)
6322 /* DR 743: decltype can be used in a nested-name-specifier. */
6323 else if (token_is_decltype (token
))
6327 /* If the next token is not an identifier, then it is
6328 definitely not a type-name or namespace-name. */
6329 if (token
->type
!= CPP_NAME
)
6331 /* If the following token is neither a `<' (to begin a
6332 template-id), nor a `::', then we are not looking at a
6333 nested-name-specifier. */
6334 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6336 if (token
->type
== CPP_COLON
6337 && parser
->colon_corrects_to_scope_p
6338 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6340 gcc_rich_location
richloc (token
->location
);
6341 richloc
.add_fixit_replace ("::");
6343 "found %<:%> in nested-name-specifier, "
6345 token
->type
= CPP_SCOPE
;
6348 if (token
->type
!= CPP_SCOPE
6349 && !cp_parser_nth_token_starts_template_argument_list_p
6354 /* The nested-name-specifier is optional, so we parse
6356 cp_parser_parse_tentatively (parser
);
6358 /* Look for the optional `template' keyword, if this isn't the
6359 first time through the loop. */
6361 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6363 /* Save the old scope since the name lookup we are about to do
6364 might destroy it. */
6365 old_scope
= parser
->scope
;
6366 saved_qualifying_scope
= parser
->qualifying_scope
;
6367 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6368 look up names in "X<T>::I" in order to determine that "Y" is
6369 a template. So, if we have a typename at this point, we make
6370 an effort to look through it. */
6372 && !typename_keyword_p
6374 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6375 parser
->scope
= resolve_typename_type (parser
->scope
,
6376 /*only_current_p=*/false);
6377 /* Parse the qualifying entity. */
6379 = cp_parser_qualifying_entity (parser
,
6385 /* Look for the `::' token. */
6386 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6388 /* If we found what we wanted, we keep going; otherwise, we're
6390 if (!cp_parser_parse_definitely (parser
))
6392 bool error_p
= false;
6394 /* Restore the OLD_SCOPE since it was valid before the
6395 failed attempt at finding the last
6396 class-or-namespace-name. */
6397 parser
->scope
= old_scope
;
6398 parser
->qualifying_scope
= saved_qualifying_scope
;
6400 /* If the next token is a decltype, and the one after that is a
6401 `::', then the decltype has failed to resolve to a class or
6402 enumeration type. Give this error even when parsing
6403 tentatively since it can't possibly be valid--and we're going
6404 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6405 won't get another chance.*/
6406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6407 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6410 token
= cp_lexer_consume_token (parser
->lexer
);
6411 error_at (token
->location
, "decltype evaluates to %qT, "
6412 "which is not a class or enumeration type",
6413 token
->u
.tree_check_value
->value
);
6414 parser
->scope
= error_mark_node
;
6418 cp_lexer_consume_token (parser
->lexer
);
6421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6422 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6424 /* If we have a non-type template-id followed by ::, it can't
6425 possibly be valid. */
6426 token
= cp_lexer_peek_token (parser
->lexer
);
6427 tree tid
= token
->u
.tree_check_value
->value
;
6428 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6429 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6431 tree tmpl
= NULL_TREE
;
6432 if (is_overloaded_fn (tid
))
6434 tree fns
= get_fns (tid
);
6435 if (OVL_SINGLE_P (fns
))
6436 tmpl
= OVL_FIRST (fns
);
6437 error_at (token
->location
, "function template-id %qD "
6438 "in nested-name-specifier", tid
);
6442 /* Variable template. */
6443 tmpl
= TREE_OPERAND (tid
, 0);
6444 gcc_assert (variable_template_p (tmpl
));
6445 error_at (token
->location
, "variable template-id %qD "
6446 "in nested-name-specifier", tid
);
6449 inform (DECL_SOURCE_LOCATION (tmpl
),
6450 "%qD declared here", tmpl
);
6452 parser
->scope
= error_mark_node
;
6456 cp_lexer_consume_token (parser
->lexer
);
6457 cp_lexer_consume_token (parser
->lexer
);
6461 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6463 /* If the next token is an identifier, and the one after
6464 that is a `::', then any valid interpretation would have
6465 found a class-or-namespace-name. */
6466 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6467 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6469 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6472 token
= cp_lexer_consume_token (parser
->lexer
);
6475 if (!token
->error_reported
)
6478 tree ambiguous_decls
;
6480 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6482 /*is_template=*/false,
6483 /*is_namespace=*/false,
6484 /*check_dependency=*/true,
6487 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6488 error_at (token
->location
,
6489 "%qD used without template arguments",
6491 else if (ambiguous_decls
)
6493 // cp_parser_lookup_name has the same diagnostic,
6494 // thus make sure to emit it at most once.
6495 if (cp_parser_uncommitted_to_tentative_parse_p
6498 error_at (token
->location
,
6499 "reference to %qD is ambiguous",
6501 print_candidates (ambiguous_decls
);
6503 decl
= error_mark_node
;
6507 if (cxx_dialect
!= cxx98
)
6508 cp_parser_name_lookup_error
6509 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6512 cp_parser_name_lookup_error
6513 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6517 parser
->scope
= error_mark_node
;
6519 /* Treat this as a successful nested-name-specifier
6524 If the name found is not a class-name (clause
6525 _class_) or namespace-name (_namespace.def_), the
6526 program is ill-formed. */
6529 cp_lexer_consume_token (parser
->lexer
);
6533 /* We've found one valid nested-name-specifier. */
6535 /* Name lookup always gives us a DECL. */
6536 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6537 new_scope
= TREE_TYPE (new_scope
);
6538 /* Uses of "template" must be followed by actual templates. */
6539 if (template_keyword_p
6540 && !(CLASS_TYPE_P (new_scope
)
6541 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6542 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6543 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6544 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6545 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6546 == TEMPLATE_ID_EXPR
)))
6547 permerror (input_location
, TYPE_P (new_scope
)
6548 ? G_("%qT is not a template")
6549 : G_("%qD is not a template"),
6551 /* If it is a class scope, try to complete it; we are about to
6552 be looking up names inside the class. */
6553 if (TYPE_P (new_scope
)
6554 /* Since checking types for dependency can be expensive,
6555 avoid doing it if the type is already complete. */
6556 && !COMPLETE_TYPE_P (new_scope
)
6557 /* Do not try to complete dependent types. */
6558 && !dependent_type_p (new_scope
))
6560 new_scope
= complete_type (new_scope
);
6561 /* If it is a typedef to current class, use the current
6562 class instead, as the typedef won't have any names inside
6564 if (!COMPLETE_TYPE_P (new_scope
)
6565 && currently_open_class (new_scope
))
6566 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6568 /* Make sure we look in the right scope the next time through
6570 parser
->scope
= new_scope
;
6573 /* If parsing tentatively, replace the sequence of tokens that makes
6574 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6575 token. That way, should we re-parse the token stream, we will
6576 not have to repeat the effort required to do the parse, nor will
6577 we issue duplicate error messages. */
6578 if (success
&& start
)
6582 token
= cp_lexer_token_at (parser
->lexer
, start
);
6583 /* Reset the contents of the START token. */
6584 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6585 /* Retrieve any deferred checks. Do not pop this access checks yet
6586 so the memory will not be reclaimed during token replacing below. */
6587 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6588 token
->u
.tree_check_value
->value
= parser
->scope
;
6589 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6590 token
->u
.tree_check_value
->qualifying_scope
=
6591 parser
->qualifying_scope
;
6592 token
->keyword
= RID_MAX
;
6594 /* Purge all subsequent tokens. */
6595 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6599 pop_to_parent_deferring_access_checks ();
6601 return success
? parser
->scope
: NULL_TREE
;
6604 /* Parse a nested-name-specifier. See
6605 cp_parser_nested_name_specifier_opt for details. This function
6606 behaves identically, except that it will an issue an error if no
6607 nested-name-specifier is present. */
6610 cp_parser_nested_name_specifier (cp_parser
*parser
,
6611 bool typename_keyword_p
,
6612 bool check_dependency_p
,
6614 bool is_declaration
)
6618 /* Look for the nested-name-specifier. */
6619 scope
= cp_parser_nested_name_specifier_opt (parser
,
6624 /* If it was not present, issue an error message. */
6627 cp_parser_error (parser
, "expected nested-name-specifier");
6628 parser
->scope
= NULL_TREE
;
6634 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6635 this is either a class-name or a namespace-name (which corresponds
6636 to the class-or-namespace-name production in the grammar). For
6637 C++0x, it can also be a type-name that refers to an enumeration
6638 type or a simple-template-id.
6640 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6641 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6642 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6643 TYPE_P is TRUE iff the next name should be taken as a class-name,
6644 even the same name is declared to be another entity in the same
6647 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6648 specified by the class-or-namespace-name. If neither is found the
6649 ERROR_MARK_NODE is returned. */
6652 cp_parser_qualifying_entity (cp_parser
*parser
,
6653 bool typename_keyword_p
,
6654 bool template_keyword_p
,
6655 bool check_dependency_p
,
6657 bool is_declaration
)
6660 tree saved_qualifying_scope
;
6661 tree saved_object_scope
;
6664 bool successful_parse_p
;
6666 /* DR 743: decltype can appear in a nested-name-specifier. */
6667 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6669 scope
= cp_parser_decltype (parser
);
6670 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6671 && !MAYBE_CLASS_TYPE_P (scope
))
6673 cp_parser_simulate_error (parser
);
6674 return error_mark_node
;
6676 if (TYPE_NAME (scope
))
6677 scope
= TYPE_NAME (scope
);
6681 /* Before we try to parse the class-name, we must save away the
6682 current PARSER->SCOPE since cp_parser_class_name will destroy
6684 saved_scope
= parser
->scope
;
6685 saved_qualifying_scope
= parser
->qualifying_scope
;
6686 saved_object_scope
= parser
->object_scope
;
6687 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6688 there is no need to look for a namespace-name. */
6689 only_class_p
= template_keyword_p
6690 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6692 cp_parser_parse_tentatively (parser
);
6693 scope
= cp_parser_class_name (parser
,
6696 type_p
? class_type
: none_type
,
6698 /*class_head_p=*/false,
6700 /*enum_ok=*/cxx_dialect
> cxx98
);
6701 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6702 /* If that didn't work, try for a namespace-name. */
6703 if (!only_class_p
&& !successful_parse_p
)
6705 /* Restore the saved scope. */
6706 parser
->scope
= saved_scope
;
6707 parser
->qualifying_scope
= saved_qualifying_scope
;
6708 parser
->object_scope
= saved_object_scope
;
6709 /* If we are not looking at an identifier followed by the scope
6710 resolution operator, then this is not part of a
6711 nested-name-specifier. (Note that this function is only used
6712 to parse the components of a nested-name-specifier.) */
6713 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6714 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6715 return error_mark_node
;
6716 scope
= cp_parser_namespace_name (parser
);
6722 /* Return true if we are looking at a compound-literal, false otherwise. */
6725 cp_parser_compound_literal_p (cp_parser
*parser
)
6727 cp_lexer_save_tokens (parser
->lexer
);
6729 /* Skip tokens until the next token is a closing parenthesis.
6730 If we find the closing `)', and the next token is a `{', then
6731 we are looking at a compound-literal. */
6732 bool compound_literal_p
6733 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6734 /*consume_paren=*/true)
6735 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6737 /* Roll back the tokens we skipped. */
6738 cp_lexer_rollback_tokens (parser
->lexer
);
6740 return compound_literal_p
;
6743 /* Return true if EXPR is the integer constant zero or a complex constant
6744 of zero, without any folding, but ignoring location wrappers. */
6747 literal_integer_zerop (const_tree expr
)
6749 return (location_wrapper_p (expr
)
6750 && integer_zerop (TREE_OPERAND (expr
, 0)));
6753 /* Parse a postfix-expression.
6757 postfix-expression [ expression ]
6758 postfix-expression ( expression-list [opt] )
6759 simple-type-specifier ( expression-list [opt] )
6760 typename :: [opt] nested-name-specifier identifier
6761 ( expression-list [opt] )
6762 typename :: [opt] nested-name-specifier template [opt] template-id
6763 ( expression-list [opt] )
6764 postfix-expression . template [opt] id-expression
6765 postfix-expression -> template [opt] id-expression
6766 postfix-expression . pseudo-destructor-name
6767 postfix-expression -> pseudo-destructor-name
6768 postfix-expression ++
6769 postfix-expression --
6770 dynamic_cast < type-id > ( expression )
6771 static_cast < type-id > ( expression )
6772 reinterpret_cast < type-id > ( expression )
6773 const_cast < type-id > ( expression )
6774 typeid ( expression )
6780 ( type-id ) { initializer-list , [opt] }
6782 This extension is a GNU version of the C99 compound-literal
6783 construct. (The C99 grammar uses `type-name' instead of `type-id',
6784 but they are essentially the same concept.)
6786 If ADDRESS_P is true, the postfix expression is the operand of the
6787 `&' operator. CAST_P is true if this expression is the target of a
6790 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6791 class member access expressions [expr.ref].
6793 Returns a representation of the expression. */
6796 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6797 bool member_access_only_p
, bool decltype_p
,
6798 cp_id_kind
* pidk_return
)
6803 cp_id_kind idk
= CP_ID_KIND_NONE
;
6804 cp_expr postfix_expression
= NULL_TREE
;
6805 bool is_member_access
= false;
6807 /* Peek at the next token. */
6808 token
= cp_lexer_peek_token (parser
->lexer
);
6809 loc
= token
->location
;
6810 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6812 /* Some of the productions are determined by keywords. */
6813 keyword
= token
->keyword
;
6823 const char *saved_message
;
6824 bool saved_in_type_id_in_expr_p
;
6826 /* All of these can be handled in the same way from the point
6827 of view of parsing. Begin by consuming the token
6828 identifying the cast. */
6829 cp_lexer_consume_token (parser
->lexer
);
6831 /* New types cannot be defined in the cast. */
6832 saved_message
= parser
->type_definition_forbidden_message
;
6833 parser
->type_definition_forbidden_message
6834 = G_("types may not be defined in casts");
6836 /* Look for the opening `<'. */
6837 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6838 /* Parse the type to which we are casting. */
6839 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6840 parser
->in_type_id_in_expr_p
= true;
6841 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
6843 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6844 /* Look for the closing `>'. */
6845 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6846 /* Restore the old message. */
6847 parser
->type_definition_forbidden_message
= saved_message
;
6849 bool saved_greater_than_is_operator_p
6850 = parser
->greater_than_is_operator_p
;
6851 parser
->greater_than_is_operator_p
= true;
6853 /* And the expression which is being cast. */
6854 matching_parens parens
;
6855 parens
.require_open (parser
);
6856 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6857 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6859 location_t end_loc
= close_paren
?
6860 close_paren
->location
: UNKNOWN_LOCATION
;
6862 parser
->greater_than_is_operator_p
6863 = saved_greater_than_is_operator_p
;
6865 /* Only type conversions to integral or enumeration types
6866 can be used in constant-expressions. */
6867 if (!cast_valid_in_integral_constant_expression_p (type
)
6868 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6870 postfix_expression
= error_mark_node
;
6878 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6882 = build_static_cast (type
, expression
, tf_warning_or_error
);
6886 = build_reinterpret_cast (type
, expression
,
6887 tf_warning_or_error
);
6891 = build_const_cast (type
, expression
, tf_warning_or_error
);
6897 /* Construct a location e.g. :
6898 reinterpret_cast <int *> (expr)
6899 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6900 ranging from the start of the "*_cast" token to the final closing
6901 paren, with the caret at the start. */
6902 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6903 postfix_expression
.set_location (cp_cast_loc
);
6910 const char *saved_message
;
6911 bool saved_in_type_id_in_expr_p
;
6913 /* Consume the `typeid' token. */
6914 cp_lexer_consume_token (parser
->lexer
);
6915 /* Look for the `(' token. */
6916 matching_parens parens
;
6917 parens
.require_open (parser
);
6918 /* Types cannot be defined in a `typeid' expression. */
6919 saved_message
= parser
->type_definition_forbidden_message
;
6920 parser
->type_definition_forbidden_message
6921 = G_("types may not be defined in a %<typeid%> expression");
6922 /* We can't be sure yet whether we're looking at a type-id or an
6924 cp_parser_parse_tentatively (parser
);
6925 /* Try a type-id first. */
6926 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6927 parser
->in_type_id_in_expr_p
= true;
6928 type
= cp_parser_type_id (parser
);
6929 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6930 /* Look for the `)' token. Otherwise, we can't be sure that
6931 we're not looking at an expression: consider `typeid (int
6932 (3))', for example. */
6933 cp_token
*close_paren
= parens
.require_close (parser
);
6934 /* If all went well, simply lookup the type-id. */
6935 if (cp_parser_parse_definitely (parser
))
6936 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6937 /* Otherwise, fall back to the expression variant. */
6942 /* Look for an expression. */
6943 expression
= cp_parser_expression (parser
, & idk
);
6944 /* Compute its typeid. */
6945 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6946 /* Look for the `)' token. */
6947 close_paren
= parens
.require_close (parser
);
6949 /* Restore the saved message. */
6950 parser
->type_definition_forbidden_message
= saved_message
;
6951 /* `typeid' may not appear in an integral constant expression. */
6952 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6953 postfix_expression
= error_mark_node
;
6955 /* Construct a location e.g. :
6958 ranging from the start of the "typeid" token to the final closing
6959 paren, with the caret at the start. */
6962 location_t typeid_loc
6963 = make_location (start_loc
, start_loc
, close_paren
->location
);
6964 postfix_expression
.set_location (typeid_loc
);
6965 postfix_expression
.maybe_add_location_wrapper ();
6973 /* The syntax permitted here is the same permitted for an
6974 elaborated-type-specifier. */
6975 ++parser
->prevent_constrained_type_specifiers
;
6976 type
= cp_parser_elaborated_type_specifier (parser
,
6977 /*is_friend=*/false,
6978 /*is_declaration=*/false);
6979 --parser
->prevent_constrained_type_specifiers
;
6980 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6985 case RID_BUILTIN_SHUFFLE
:
6986 case RID_BUILTIN_LAUNDER
:
6988 vec
<tree
, va_gc
> *vec
;
6992 cp_lexer_consume_token (parser
->lexer
);
6993 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6994 /*cast_p=*/false, /*allow_expansion_p=*/true,
6995 /*non_constant_p=*/NULL
);
6998 postfix_expression
= error_mark_node
;
7002 FOR_EACH_VEC_ELT (*vec
, i
, p
)
7008 if (vec
->length () == 1)
7010 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7013 error_at (loc
, "wrong number of arguments to "
7014 "%<__builtin_addressof%>");
7015 postfix_expression
= error_mark_node
;
7019 case RID_BUILTIN_LAUNDER
:
7020 if (vec
->length () == 1)
7021 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7022 tf_warning_or_error
);
7025 error_at (loc
, "wrong number of arguments to "
7026 "%<__builtin_launder%>");
7027 postfix_expression
= error_mark_node
;
7031 case RID_BUILTIN_SHUFFLE
:
7032 if (vec
->length () == 2)
7034 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7035 (*vec
)[1], tf_warning_or_error
);
7036 else if (vec
->length () == 3)
7038 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7039 (*vec
)[2], tf_warning_or_error
);
7042 error_at (loc
, "wrong number of arguments to "
7043 "%<__builtin_shuffle%>");
7044 postfix_expression
= error_mark_node
;
7054 case RID_BUILTIN_CONVERTVECTOR
:
7058 /* Consume the `__builtin_convertvector' token. */
7059 cp_lexer_consume_token (parser
->lexer
);
7060 /* Look for the opening `('. */
7061 matching_parens parens
;
7062 parens
.require_open (parser
);
7063 /* Now, parse the assignment-expression. */
7064 expression
= cp_parser_assignment_expression (parser
);
7065 /* Look for the `,'. */
7066 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7067 location_t type_location
7068 = cp_lexer_peek_token (parser
->lexer
)->location
;
7069 /* Parse the type-id. */
7071 type_id_in_expr_sentinel
s (parser
);
7072 type
= cp_parser_type_id (parser
);
7074 /* Look for the closing `)'. */
7075 parens
.require_close (parser
);
7076 return cp_build_vec_convert (expression
, type_location
, type
,
7077 tf_warning_or_error
);
7084 /* If the next thing is a simple-type-specifier, we may be
7085 looking at a functional cast. We could also be looking at
7086 an id-expression. So, we try the functional cast, and if
7087 that doesn't work we fall back to the primary-expression. */
7088 cp_parser_parse_tentatively (parser
);
7089 /* Look for the simple-type-specifier. */
7090 ++parser
->prevent_constrained_type_specifiers
;
7091 type
= cp_parser_simple_type_specifier (parser
,
7092 /*decl_specs=*/NULL
,
7093 CP_PARSER_FLAGS_NONE
);
7094 --parser
->prevent_constrained_type_specifiers
;
7095 /* Parse the cast itself. */
7096 if (!cp_parser_error_occurred (parser
))
7098 = cp_parser_functional_cast (parser
, type
);
7099 /* If that worked, we're done. */
7100 if (cp_parser_parse_definitely (parser
))
7103 /* If the functional-cast didn't work out, try a
7104 compound-literal. */
7105 if (cp_parser_allow_gnu_extensions_p (parser
)
7106 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7108 cp_expr initializer
= NULL_TREE
;
7110 cp_parser_parse_tentatively (parser
);
7112 matching_parens parens
;
7113 parens
.consume_open (parser
);
7115 /* Avoid calling cp_parser_type_id pointlessly, see comment
7116 in cp_parser_cast_expression about c++/29234. */
7117 if (!cp_parser_compound_literal_p (parser
))
7118 cp_parser_simulate_error (parser
);
7121 /* Parse the type. */
7122 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7123 parser
->in_type_id_in_expr_p
= true;
7124 type
= cp_parser_type_id (parser
);
7125 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7126 parens
.require_close (parser
);
7129 /* If things aren't going well, there's no need to
7131 if (!cp_parser_error_occurred (parser
))
7133 bool non_constant_p
;
7134 /* Parse the brace-enclosed initializer list. */
7135 initializer
= cp_parser_braced_list (parser
,
7138 /* If that worked, we're definitely looking at a
7139 compound-literal expression. */
7140 if (cp_parser_parse_definitely (parser
))
7142 /* Warn the user that a compound literal is not
7143 allowed in standard C++. */
7144 pedwarn (input_location
, OPT_Wpedantic
,
7145 "ISO C++ forbids compound-literals");
7146 /* For simplicity, we disallow compound literals in
7147 constant-expressions. We could
7148 allow compound literals of integer type, whose
7149 initializer was a constant, in constant
7150 expressions. Permitting that usage, as a further
7151 extension, would not change the meaning of any
7152 currently accepted programs. (Of course, as
7153 compound literals are not part of ISO C++, the
7154 standard has nothing to say.) */
7155 if (cp_parser_non_integral_constant_expression (parser
,
7158 postfix_expression
= error_mark_node
;
7161 /* Form the representation of the compound-literal. */
7163 = finish_compound_literal (type
, initializer
,
7164 tf_warning_or_error
, fcl_c99
);
7165 postfix_expression
.set_location (initializer
.get_location ());
7170 /* It must be a primary-expression. */
7172 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7173 /*template_arg_p=*/false,
7180 /* Note that we don't need to worry about calling build_cplus_new on a
7181 class-valued CALL_EXPR in decltype when it isn't the end of the
7182 postfix-expression; unary_complex_lvalue will take care of that for
7185 /* Keep looping until the postfix-expression is complete. */
7188 if (idk
== CP_ID_KIND_UNQUALIFIED
7189 && identifier_p (postfix_expression
)
7190 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7191 /* It is not a Koenig lookup function call. */
7193 = unqualified_name_lookup_error (postfix_expression
);
7195 /* Peek at the next token. */
7196 token
= cp_lexer_peek_token (parser
->lexer
);
7198 switch (token
->type
)
7200 case CPP_OPEN_SQUARE
:
7201 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7203 cp_parser_error (parser
,
7204 "two consecutive %<[%> shall "
7205 "only introduce an attribute");
7206 return error_mark_node
;
7209 = cp_parser_postfix_open_square_expression (parser
,
7213 postfix_expression
.set_range (start_loc
,
7214 postfix_expression
.get_location ());
7216 idk
= CP_ID_KIND_NONE
;
7217 is_member_access
= false;
7220 case CPP_OPEN_PAREN
:
7221 /* postfix-expression ( expression-list [opt] ) */
7224 bool is_builtin_constant_p
;
7225 bool saved_integral_constant_expression_p
= false;
7226 bool saved_non_integral_constant_expression_p
= false;
7227 tsubst_flags_t complain
= complain_flags (decltype_p
);
7228 vec
<tree
, va_gc
> *args
;
7229 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7231 is_member_access
= false;
7233 tree stripped_expression
7234 = tree_strip_any_location_wrapper (postfix_expression
);
7235 is_builtin_constant_p
7236 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7237 if (is_builtin_constant_p
)
7239 /* The whole point of __builtin_constant_p is to allow
7240 non-constant expressions to appear as arguments. */
7241 saved_integral_constant_expression_p
7242 = parser
->integral_constant_expression_p
;
7243 saved_non_integral_constant_expression_p
7244 = parser
->non_integral_constant_expression_p
;
7245 parser
->integral_constant_expression_p
= false;
7247 args
= (cp_parser_parenthesized_expression_list
7249 /*cast_p=*/false, /*allow_expansion_p=*/true,
7250 /*non_constant_p=*/NULL
,
7251 /*close_paren_loc=*/&close_paren_loc
,
7252 /*wrap_locations_p=*/true));
7253 if (is_builtin_constant_p
)
7255 parser
->integral_constant_expression_p
7256 = saved_integral_constant_expression_p
;
7257 parser
->non_integral_constant_expression_p
7258 = saved_non_integral_constant_expression_p
;
7263 postfix_expression
= error_mark_node
;
7267 /* Function calls are not permitted in
7268 constant-expressions. */
7269 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7270 && cp_parser_non_integral_constant_expression (parser
,
7273 postfix_expression
= error_mark_node
;
7274 release_tree_vector (args
);
7279 if (idk
== CP_ID_KIND_UNQUALIFIED
7280 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7282 if (identifier_p (postfix_expression
)
7283 /* In C++2A, we may need to perform ADL for a template
7285 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7286 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7288 if (!args
->is_empty ())
7291 if (!any_type_dependent_arguments_p (args
))
7293 = perform_koenig_lookup (postfix_expression
, args
,
7298 = unqualified_fn_lookup_error (postfix_expression
);
7300 /* We do not perform argument-dependent lookup if
7301 normal lookup finds a non-function, in accordance
7302 with the expected resolution of DR 218. */
7303 else if (!args
->is_empty ()
7304 && is_overloaded_fn (postfix_expression
))
7306 /* We only need to look at the first function,
7307 because all the fns share the attribute we're
7308 concerned with (all member fns or all local
7310 tree fn
= get_first_fn (postfix_expression
);
7311 fn
= STRIP_TEMPLATE (fn
);
7313 /* Do not do argument dependent lookup if regular
7314 lookup finds a member function or a block-scope
7315 function declaration. [basic.lookup.argdep]/3 */
7316 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7317 || DECL_FUNCTION_MEMBER_P (fn
)
7318 || DECL_LOCAL_FUNCTION_P (fn
)))
7321 if (!any_type_dependent_arguments_p (args
))
7323 = perform_koenig_lookup (postfix_expression
, args
,
7329 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7331 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7332 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7334 if (processing_template_decl
7335 && (type_dependent_object_expression_p (instance
)
7336 || (!BASELINK_P (fn
)
7337 && TREE_CODE (fn
) != FIELD_DECL
)
7338 || type_dependent_expression_p (fn
)
7339 || any_type_dependent_arguments_p (args
)))
7341 maybe_generic_this_capture (instance
, fn
);
7343 = build_min_nt_call_vec (postfix_expression
, args
);
7344 release_tree_vector (args
);
7348 if (BASELINK_P (fn
))
7351 = (build_new_method_call
7352 (instance
, fn
, &args
, NULL_TREE
,
7353 (idk
== CP_ID_KIND_QUALIFIED
7354 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7361 = finish_call_expr (postfix_expression
, &args
,
7362 /*disallow_virtual=*/false,
7366 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7367 || TREE_CODE (postfix_expression
) == MEMBER_REF
7368 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7369 postfix_expression
= (build_offset_ref_call_from_tree
7370 (postfix_expression
, &args
,
7372 else if (idk
== CP_ID_KIND_QUALIFIED
)
7373 /* A call to a static class member, or a namespace-scope
7376 = finish_call_expr (postfix_expression
, &args
,
7377 /*disallow_virtual=*/true,
7381 /* All other function calls. */
7383 = finish_call_expr (postfix_expression
, &args
,
7384 /*disallow_virtual=*/false,
7388 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7390 location_t combined_loc
= make_location (token
->location
,
7393 postfix_expression
.set_location (combined_loc
);
7396 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7397 idk
= CP_ID_KIND_NONE
;
7399 release_tree_vector (args
);
7405 /* postfix-expression . template [opt] id-expression
7406 postfix-expression . pseudo-destructor-name
7407 postfix-expression -> template [opt] id-expression
7408 postfix-expression -> pseudo-destructor-name */
7410 /* Consume the `.' or `->' operator. */
7411 cp_lexer_consume_token (parser
->lexer
);
7414 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7418 is_member_access
= true;
7422 /* postfix-expression ++ */
7423 /* Consume the `++' token. */
7424 cp_lexer_consume_token (parser
->lexer
);
7425 /* Generate a representation for the complete expression. */
7427 = finish_increment_expr (postfix_expression
,
7428 POSTINCREMENT_EXPR
);
7429 /* Increments may not appear in constant-expressions. */
7430 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7431 postfix_expression
= error_mark_node
;
7432 idk
= CP_ID_KIND_NONE
;
7433 is_member_access
= false;
7436 case CPP_MINUS_MINUS
:
7437 /* postfix-expression -- */
7438 /* Consume the `--' token. */
7439 cp_lexer_consume_token (parser
->lexer
);
7440 /* Generate a representation for the complete expression. */
7442 = finish_increment_expr (postfix_expression
,
7443 POSTDECREMENT_EXPR
);
7444 /* Decrements may not appear in constant-expressions. */
7445 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7446 postfix_expression
= error_mark_node
;
7447 idk
= CP_ID_KIND_NONE
;
7448 is_member_access
= false;
7452 if (pidk_return
!= NULL
)
7453 * pidk_return
= idk
;
7454 if (member_access_only_p
)
7455 return is_member_access
7456 ? postfix_expression
7457 : cp_expr (error_mark_node
);
7459 return postfix_expression
;
7463 /* We should never get here. */
7465 return error_mark_node
;
7468 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7469 by cp_parser_builtin_offsetof. We're looking for
7471 postfix-expression [ expression ]
7472 postfix-expression [ braced-init-list ] (C++11)
7474 FOR_OFFSETOF is set if we're being called in that context, which
7475 changes how we deal with integer constant expressions. */
7478 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7479 tree postfix_expression
,
7483 tree index
= NULL_TREE
;
7484 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7485 bool saved_greater_than_is_operator_p
;
7487 /* Consume the `[' token. */
7488 cp_lexer_consume_token (parser
->lexer
);
7490 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7491 parser
->greater_than_is_operator_p
= true;
7493 /* Parse the index expression. */
7494 /* ??? For offsetof, there is a question of what to allow here. If
7495 offsetof is not being used in an integral constant expression context,
7496 then we *could* get the right answer by computing the value at runtime.
7497 If we are in an integral constant expression context, then we might
7498 could accept any constant expression; hard to say without analysis.
7499 Rather than open the barn door too wide right away, allow only integer
7500 constant expressions here. */
7502 index
= cp_parser_constant_expression (parser
);
7505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7507 bool expr_nonconst_p
;
7508 cp_lexer_set_source_position (parser
->lexer
);
7509 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7510 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7513 index
= cp_parser_expression (parser
);
7516 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7518 /* Look for the closing `]'. */
7519 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7521 /* Build the ARRAY_REF. */
7522 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7525 /* When not doing offsetof, array references are not permitted in
7526 constant-expressions. */
7528 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7529 postfix_expression
= error_mark_node
;
7531 return postfix_expression
;
7534 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7535 dereference of incomplete type, returns true if error_mark_node should
7536 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7537 and *DEPENDENT_P. */
7540 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7543 /* In a template, be permissive by treating an object expression
7544 of incomplete type as dependent (after a pedwarn). */
7545 diagnostic_t kind
= (processing_template_decl
7546 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7548 switch (TREE_CODE (*postfix_expression
))
7551 case REINTERPRET_CAST_EXPR
:
7552 case CONST_CAST_EXPR
:
7553 case STATIC_CAST_EXPR
:
7554 case DYNAMIC_CAST_EXPR
:
7555 case IMPLICIT_CONV_EXPR
:
7556 case VIEW_CONVERT_EXPR
:
7557 case NON_LVALUE_EXPR
:
7561 /* Don't emit any diagnostic for OVERLOADs. */
7565 /* Avoid clobbering e.g. DECLs. */
7566 if (!EXPR_P (*postfix_expression
))
7571 if (kind
== DK_IGNORED
)
7574 location_t exploc
= location_of (*postfix_expression
);
7575 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7576 if (!MAYBE_CLASS_TYPE_P (*scope
))
7578 if (kind
== DK_ERROR
)
7579 *scope
= *postfix_expression
= error_mark_node
;
7580 else if (processing_template_decl
)
7582 *dependent_p
= true;
7583 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7588 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7589 by cp_parser_builtin_offsetof. We're looking for
7591 postfix-expression . template [opt] id-expression
7592 postfix-expression . pseudo-destructor-name
7593 postfix-expression -> template [opt] id-expression
7594 postfix-expression -> pseudo-destructor-name
7596 FOR_OFFSETOF is set if we're being called in that context. That sorta
7597 limits what of the above we'll actually accept, but nevermind.
7598 TOKEN_TYPE is the "." or "->" token, which will already have been
7599 removed from the stream. */
7602 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7603 enum cpp_ttype token_type
,
7604 cp_expr postfix_expression
,
7605 bool for_offsetof
, cp_id_kind
*idk
,
7606 location_t location
)
7610 bool pseudo_destructor_p
;
7611 tree scope
= NULL_TREE
;
7612 location_t start_loc
= postfix_expression
.get_start ();
7614 /* If this is a `->' operator, dereference the pointer. */
7615 if (token_type
== CPP_DEREF
)
7616 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7617 tf_warning_or_error
);
7618 /* Check to see whether or not the expression is type-dependent and
7619 not the current instantiation. */
7620 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7621 /* The identifier following the `->' or `.' is not qualified. */
7622 parser
->scope
= NULL_TREE
;
7623 parser
->qualifying_scope
= NULL_TREE
;
7624 parser
->object_scope
= NULL_TREE
;
7625 *idk
= CP_ID_KIND_NONE
;
7627 /* Enter the scope corresponding to the type of the object
7628 given by the POSTFIX_EXPRESSION. */
7631 scope
= TREE_TYPE (postfix_expression
);
7632 /* According to the standard, no expression should ever have
7633 reference type. Unfortunately, we do not currently match
7634 the standard in this respect in that our internal representation
7635 of an expression may have reference type even when the standard
7636 says it does not. Therefore, we have to manually obtain the
7637 underlying type here. */
7638 scope
= non_reference (scope
);
7639 /* The type of the POSTFIX_EXPRESSION must be complete. */
7640 /* Unlike the object expression in other contexts, *this is not
7641 required to be of complete type for purposes of class member
7642 access (5.2.5) outside the member function body. */
7643 if (postfix_expression
!= current_class_ref
7644 && scope
!= error_mark_node
7645 && !currently_open_class (scope
))
7647 scope
= complete_type (scope
);
7648 if (!COMPLETE_TYPE_P (scope
)
7649 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7651 return error_mark_node
;
7656 /* Let the name lookup machinery know that we are processing a
7657 class member access expression. */
7658 parser
->context
->object_type
= scope
;
7659 /* If something went wrong, we want to be able to discern that case,
7660 as opposed to the case where there was no SCOPE due to the type
7661 of expression being dependent. */
7663 scope
= error_mark_node
;
7664 /* If the SCOPE was erroneous, make the various semantic analysis
7665 functions exit quickly -- and without issuing additional error
7667 if (scope
== error_mark_node
)
7668 postfix_expression
= error_mark_node
;
7673 /* Tell cp_parser_lookup_name that there was an object, even though it's
7675 parser
->context
->object_type
= unknown_type_node
;
7677 /* Assume this expression is not a pseudo-destructor access. */
7678 pseudo_destructor_p
= false;
7680 /* If the SCOPE is a scalar type, then, if this is a valid program,
7681 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7682 is type dependent, it can be pseudo-destructor-name or something else.
7683 Try to parse it as pseudo-destructor-name first. */
7684 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7689 cp_parser_parse_tentatively (parser
);
7690 /* Parse the pseudo-destructor-name. */
7692 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7695 && (cp_parser_error_occurred (parser
)
7696 || !SCALAR_TYPE_P (type
)))
7697 cp_parser_abort_tentative_parse (parser
);
7698 else if (cp_parser_parse_definitely (parser
))
7700 pseudo_destructor_p
= true;
7702 = finish_pseudo_destructor_expr (postfix_expression
,
7707 if (!pseudo_destructor_p
)
7709 /* If the SCOPE is not a scalar type, we are looking at an
7710 ordinary class member access expression, rather than a
7711 pseudo-destructor-name. */
7713 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7714 /* Parse the id-expression. */
7715 name
= (cp_parser_id_expression
7717 cp_parser_optional_template_keyword (parser
),
7718 /*check_dependency_p=*/true,
7720 /*declarator_p=*/false,
7721 /*optional_p=*/false));
7722 /* In general, build a SCOPE_REF if the member name is qualified.
7723 However, if the name was not dependent and has already been
7724 resolved; there is no need to build the SCOPE_REF. For example;
7726 struct X { void f(); };
7727 template <typename T> void f(T* t) { t->X::f(); }
7729 Even though "t" is dependent, "X::f" is not and has been resolved
7730 to a BASELINK; there is no need to include scope information. */
7732 /* But we do need to remember that there was an explicit scope for
7733 virtual function calls. */
7735 *idk
= CP_ID_KIND_QUALIFIED
;
7737 /* If the name is a template-id that names a type, we will get a
7738 TYPE_DECL here. That is invalid code. */
7739 if (TREE_CODE (name
) == TYPE_DECL
)
7741 error_at (token
->location
, "invalid use of %qD", name
);
7742 postfix_expression
= error_mark_node
;
7746 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7748 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7750 error_at (token
->location
, "%<%D::%D%> is not a class member",
7751 parser
->scope
, name
);
7752 postfix_expression
= error_mark_node
;
7755 name
= build_qualified_name (/*type=*/NULL_TREE
,
7759 parser
->scope
= NULL_TREE
;
7760 parser
->qualifying_scope
= NULL_TREE
;
7761 parser
->object_scope
= NULL_TREE
;
7763 if (parser
->scope
&& name
&& BASELINK_P (name
))
7764 adjust_result_of_qualified_name_lookup
7765 (name
, parser
->scope
, scope
);
7767 = finish_class_member_access_expr (postfix_expression
, name
,
7769 tf_warning_or_error
);
7770 /* Build a location e.g.:
7773 where the caret is at the deref token, ranging from
7774 the start of postfix_expression to the end of the access expr. */
7776 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7777 location_t combined_loc
7778 = make_location (input_location
, start_loc
, end_loc
);
7779 protected_set_expr_location (postfix_expression
, combined_loc
);
7783 /* We no longer need to look up names in the scope of the object on
7784 the left-hand side of the `.' or `->' operator. */
7785 parser
->context
->object_type
= NULL_TREE
;
7787 /* Outside of offsetof, these operators may not appear in
7788 constant-expressions. */
7790 && (cp_parser_non_integral_constant_expression
7791 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7792 postfix_expression
= error_mark_node
;
7794 return postfix_expression
;
7797 /* Parse a parenthesized expression-list.
7800 assignment-expression
7801 expression-list, assignment-expression
7806 identifier, expression-list
7808 CAST_P is true if this expression is the target of a cast.
7810 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7813 WRAP_LOCATIONS_P is true if expressions within this list for which
7814 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7815 their source locations.
7817 Returns a vector of trees. Each element is a representation of an
7818 assignment-expression. NULL is returned if the ( and or ) are
7819 missing. An empty, but allocated, vector is returned on no
7820 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7821 if we are parsing an attribute list for an attribute that wants a
7822 plain identifier argument, normal_attr for an attribute that wants
7823 an expression, or non_attr if we aren't parsing an attribute list. If
7824 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7825 not all of the expressions in the list were constant.
7826 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7827 will be written to with the location of the closing parenthesis. If
7828 an error occurs, it may or may not be written to. */
7830 static vec
<tree
, va_gc
> *
7831 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7832 int is_attribute_list
,
7834 bool allow_expansion_p
,
7835 bool *non_constant_p
,
7836 location_t
*close_paren_loc
,
7837 bool wrap_locations_p
)
7839 vec
<tree
, va_gc
> *expression_list
;
7840 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7841 tree identifier
= NULL_TREE
;
7842 bool saved_greater_than_is_operator_p
;
7844 /* Assume all the expressions will be constant. */
7846 *non_constant_p
= false;
7848 matching_parens parens
;
7849 if (!parens
.require_open (parser
))
7852 expression_list
= make_tree_vector ();
7854 /* Within a parenthesized expression, a `>' token is always
7855 the greater-than operator. */
7856 saved_greater_than_is_operator_p
7857 = parser
->greater_than_is_operator_p
;
7858 parser
->greater_than_is_operator_p
= true;
7860 cp_expr
expr (NULL_TREE
);
7862 /* Consume expressions until there are no more. */
7863 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7866 /* At the beginning of attribute lists, check to see if the
7867 next token is an identifier. */
7868 if (is_attribute_list
== id_attr
7869 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7873 /* Consume the identifier. */
7874 token
= cp_lexer_consume_token (parser
->lexer
);
7875 /* Save the identifier. */
7876 identifier
= token
->u
.value
;
7880 bool expr_non_constant_p
;
7882 /* Parse the next assignment-expression. */
7883 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7885 /* A braced-init-list. */
7886 cp_lexer_set_source_position (parser
->lexer
);
7887 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7888 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7889 if (non_constant_p
&& expr_non_constant_p
)
7890 *non_constant_p
= true;
7892 else if (non_constant_p
)
7894 expr
= (cp_parser_constant_expression
7895 (parser
, /*allow_non_constant_p=*/true,
7896 &expr_non_constant_p
));
7897 if (expr_non_constant_p
)
7898 *non_constant_p
= true;
7901 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7905 expr
= instantiate_non_dependent_expr (expr
);
7907 /* If we have an ellipsis, then this is an expression
7909 if (allow_expansion_p
7910 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7912 /* Consume the `...'. */
7913 cp_lexer_consume_token (parser
->lexer
);
7915 /* Build the argument pack. */
7916 expr
= make_pack_expansion (expr
);
7919 if (wrap_locations_p
)
7920 expr
.maybe_add_location_wrapper ();
7922 /* Add it to the list. We add error_mark_node
7923 expressions to the list, so that we can still tell if
7924 the correct form for a parenthesized expression-list
7925 is found. That gives better errors. */
7926 vec_safe_push (expression_list
, expr
.get_value ());
7928 if (expr
== error_mark_node
)
7932 /* After the first item, attribute lists look the same as
7933 expression lists. */
7934 is_attribute_list
= non_attr
;
7937 /* If the next token isn't a `,', then we are done. */
7938 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7941 /* Otherwise, consume the `,' and keep going. */
7942 cp_lexer_consume_token (parser
->lexer
);
7945 if (close_paren_loc
)
7946 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7948 if (!parens
.require_close (parser
))
7953 /* We try and resync to an unnested comma, as that will give the
7954 user better diagnostics. */
7955 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7956 /*recovering=*/true,
7958 /*consume_paren=*/true);
7963 parser
->greater_than_is_operator_p
7964 = saved_greater_than_is_operator_p
;
7969 parser
->greater_than_is_operator_p
7970 = saved_greater_than_is_operator_p
;
7973 vec_safe_insert (expression_list
, 0, identifier
);
7975 return expression_list
;
7978 /* Parse a pseudo-destructor-name.
7980 pseudo-destructor-name:
7981 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7982 :: [opt] nested-name-specifier template template-id :: ~ type-name
7983 :: [opt] nested-name-specifier [opt] ~ type-name
7985 If either of the first two productions is used, sets *SCOPE to the
7986 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7987 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7988 or ERROR_MARK_NODE if the parse fails. */
7991 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7996 bool nested_name_specifier_p
;
7999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
8000 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
8001 && !type_dependent_expression_p (object
))
8003 if (cxx_dialect
< cxx14
)
8004 pedwarn (input_location
, 0,
8005 "%<~auto%> only available with "
8006 "%<-std=c++14%> or %<-std=gnu++14%>");
8007 cp_lexer_consume_token (parser
->lexer
);
8008 cp_lexer_consume_token (parser
->lexer
);
8010 *type
= TREE_TYPE (object
);
8014 /* Assume that things will not work out. */
8015 *type
= error_mark_node
;
8017 /* Look for the optional `::' operator. */
8018 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8019 /* Look for the optional nested-name-specifier. */
8020 nested_name_specifier_p
8021 = (cp_parser_nested_name_specifier_opt (parser
,
8022 /*typename_keyword_p=*/false,
8023 /*check_dependency_p=*/true,
8025 /*is_declaration=*/false)
8027 /* Now, if we saw a nested-name-specifier, we might be doing the
8028 second production. */
8029 if (nested_name_specifier_p
8030 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8032 /* Consume the `template' keyword. */
8033 cp_lexer_consume_token (parser
->lexer
);
8034 /* Parse the template-id. */
8035 cp_parser_template_id (parser
,
8036 /*template_keyword_p=*/true,
8037 /*check_dependency_p=*/false,
8039 /*is_declaration=*/true);
8040 /* Look for the `::' token. */
8041 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8043 /* If the next token is not a `~', then there might be some
8044 additional qualification. */
8045 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8047 /* At this point, we're looking for "type-name :: ~". The type-name
8048 must not be a class-name, since this is a pseudo-destructor. So,
8049 it must be either an enum-name, or a typedef-name -- both of which
8050 are just identifiers. So, we peek ahead to check that the "::"
8051 and "~" tokens are present; if they are not, then we can avoid
8052 calling type_name. */
8053 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8054 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8055 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8057 cp_parser_error (parser
, "non-scalar type");
8061 /* Look for the type-name. */
8062 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8063 if (*scope
== error_mark_node
)
8066 /* Look for the `::' token. */
8067 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8072 /* Look for the `~'. */
8073 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8075 /* Once we see the ~, this has to be a pseudo-destructor. */
8076 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8077 cp_parser_commit_to_topmost_tentative_parse (parser
);
8079 /* Look for the type-name again. We are not responsible for
8080 checking that it matches the first type-name. */
8081 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8084 /* Parse a unary-expression.
8090 unary-operator cast-expression
8091 sizeof unary-expression
8093 alignof ( type-id ) [C++0x]
8100 __extension__ cast-expression
8101 __alignof__ unary-expression
8102 __alignof__ ( type-id )
8103 alignof unary-expression [C++0x]
8104 __real__ cast-expression
8105 __imag__ cast-expression
8107 sizeof ( type-id ) { initializer-list , [opt] }
8108 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8109 __alignof__ ( type-id ) { initializer-list , [opt] }
8111 ADDRESS_P is true iff the unary-expression is appearing as the
8112 operand of the `&' operator. CAST_P is true if this expression is
8113 the target of a cast.
8115 Returns a representation of the expression. */
8118 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8119 bool address_p
, bool cast_p
, bool decltype_p
)
8122 enum tree_code unary_operator
;
8124 /* Peek at the next token. */
8125 token
= cp_lexer_peek_token (parser
->lexer
);
8126 /* Some keywords give away the kind of expression. */
8127 if (token
->type
== CPP_KEYWORD
)
8129 enum rid keyword
= token
->keyword
;
8138 location_t start_loc
= token
->location
;
8140 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8141 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8143 /* Consume the token. */
8144 cp_lexer_consume_token (parser
->lexer
);
8145 /* Parse the operand. */
8146 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8148 if (TYPE_P (operand
))
8149 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8153 /* ISO C++ defines alignof only with types, not with
8154 expressions. So pedwarn if alignof is used with a non-
8155 type expression. However, __alignof__ is ok. */
8157 pedwarn (token
->location
, OPT_Wpedantic
,
8158 "ISO C++ does not allow %<alignof%> "
8161 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8163 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8164 SIZEOF_EXPR with the original operand. */
8165 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8167 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8169 if (!processing_template_decl
&& TYPE_P (operand
))
8171 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8172 build1 (NOP_EXPR
, operand
,
8174 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8177 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8178 TREE_SIDE_EFFECTS (ret
) = 0;
8179 TREE_READONLY (ret
) = 1;
8183 /* Construct a location e.g. :
8186 with start == caret at the start of the "alignof"/"sizeof"
8187 token, with the endpoint at the final closing paren. */
8188 location_t finish_loc
8189 = cp_lexer_previous_token (parser
->lexer
)->location
;
8190 location_t compound_loc
8191 = make_location (start_loc
, start_loc
, finish_loc
);
8193 cp_expr
ret_expr (ret
);
8194 ret_expr
.set_location (compound_loc
);
8195 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8199 case RID_BUILTIN_HAS_ATTRIBUTE
:
8200 return cp_parser_has_attribute_expression (parser
);
8203 return cp_parser_new_expression (parser
);
8206 return cp_parser_delete_expression (parser
);
8210 /* The saved value of the PEDANTIC flag. */
8214 /* Save away the PEDANTIC flag. */
8215 cp_parser_extension_opt (parser
, &saved_pedantic
);
8216 /* Parse the cast-expression. */
8217 expr
= cp_parser_simple_cast_expression (parser
);
8218 /* Restore the PEDANTIC flag. */
8219 pedantic
= saved_pedantic
;
8229 /* Consume the `__real__' or `__imag__' token. */
8230 cp_lexer_consume_token (parser
->lexer
);
8231 /* Parse the cast-expression. */
8232 expression
= cp_parser_simple_cast_expression (parser
);
8233 /* Create the complete representation. */
8234 return build_x_unary_op (token
->location
,
8235 (keyword
== RID_REALPART
8236 ? REALPART_EXPR
: IMAGPART_EXPR
),
8238 tf_warning_or_error
);
8242 case RID_TRANSACTION_ATOMIC
:
8243 case RID_TRANSACTION_RELAXED
:
8244 return cp_parser_transaction_expression (parser
, keyword
);
8249 const char *saved_message
;
8250 bool saved_integral_constant_expression_p
;
8251 bool saved_non_integral_constant_expression_p
;
8252 bool saved_greater_than_is_operator_p
;
8254 location_t start_loc
= token
->location
;
8256 cp_lexer_consume_token (parser
->lexer
);
8257 matching_parens parens
;
8258 parens
.require_open (parser
);
8260 saved_message
= parser
->type_definition_forbidden_message
;
8261 parser
->type_definition_forbidden_message
8262 = G_("types may not be defined in %<noexcept%> expressions");
8264 saved_integral_constant_expression_p
8265 = parser
->integral_constant_expression_p
;
8266 saved_non_integral_constant_expression_p
8267 = parser
->non_integral_constant_expression_p
;
8268 parser
->integral_constant_expression_p
= false;
8270 saved_greater_than_is_operator_p
8271 = parser
->greater_than_is_operator_p
;
8272 parser
->greater_than_is_operator_p
= true;
8274 ++cp_unevaluated_operand
;
8275 ++c_inhibit_evaluation_warnings
;
8276 ++cp_noexcept_operand
;
8277 expr
= cp_parser_expression (parser
);
8278 --cp_noexcept_operand
;
8279 --c_inhibit_evaluation_warnings
;
8280 --cp_unevaluated_operand
;
8282 parser
->greater_than_is_operator_p
8283 = saved_greater_than_is_operator_p
;
8285 parser
->integral_constant_expression_p
8286 = saved_integral_constant_expression_p
;
8287 parser
->non_integral_constant_expression_p
8288 = saved_non_integral_constant_expression_p
;
8290 parser
->type_definition_forbidden_message
= saved_message
;
8292 location_t finish_loc
8293 = cp_lexer_peek_token (parser
->lexer
)->location
;
8294 parens
.require_close (parser
);
8296 /* Construct a location of the form:
8299 with start == caret, finishing at the close-paren. */
8300 location_t noexcept_loc
8301 = make_location (start_loc
, start_loc
, finish_loc
);
8303 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8312 /* Look for the `:: new' and `:: delete', which also signal the
8313 beginning of a new-expression, or delete-expression,
8314 respectively. If the next token is `::', then it might be one of
8316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8320 /* See if the token after the `::' is one of the keywords in
8321 which we're interested. */
8322 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8323 /* If it's `new', we have a new-expression. */
8324 if (keyword
== RID_NEW
)
8325 return cp_parser_new_expression (parser
);
8326 /* Similarly, for `delete'. */
8327 else if (keyword
== RID_DELETE
)
8328 return cp_parser_delete_expression (parser
);
8331 /* Look for a unary operator. */
8332 unary_operator
= cp_parser_unary_operator (token
);
8333 /* The `++' and `--' operators can be handled similarly, even though
8334 they are not technically unary-operators in the grammar. */
8335 if (unary_operator
== ERROR_MARK
)
8337 if (token
->type
== CPP_PLUS_PLUS
)
8338 unary_operator
= PREINCREMENT_EXPR
;
8339 else if (token
->type
== CPP_MINUS_MINUS
)
8340 unary_operator
= PREDECREMENT_EXPR
;
8341 /* Handle the GNU address-of-label extension. */
8342 else if (cp_parser_allow_gnu_extensions_p (parser
)
8343 && token
->type
== CPP_AND_AND
)
8347 location_t start_loc
= token
->location
;
8349 /* Consume the '&&' token. */
8350 cp_lexer_consume_token (parser
->lexer
);
8351 /* Look for the identifier. */
8352 location_t finish_loc
8353 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8354 identifier
= cp_parser_identifier (parser
);
8355 /* Construct a location of the form:
8358 with caret==start at the "&&", finish at the end of the label. */
8359 location_t combined_loc
8360 = make_location (start_loc
, start_loc
, finish_loc
);
8361 /* Create an expression representing the address. */
8362 expression
= finish_label_address_expr (identifier
, combined_loc
);
8363 if (cp_parser_non_integral_constant_expression (parser
,
8365 expression
= error_mark_node
;
8369 if (unary_operator
!= ERROR_MARK
)
8371 cp_expr cast_expression
;
8372 cp_expr expression
= error_mark_node
;
8373 non_integral_constant non_constant_p
= NIC_NONE
;
8374 location_t loc
= token
->location
;
8375 tsubst_flags_t complain
= complain_flags (decltype_p
);
8377 /* Consume the operator token. */
8378 token
= cp_lexer_consume_token (parser
->lexer
);
8379 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8381 /* Parse the cast-expression. */
8383 = cp_parser_cast_expression (parser
,
8384 unary_operator
== ADDR_EXPR
,
8390 OP_TOKEN CAST_EXPRESSION
8391 ^~~~~~~~~~~~~~~~~~~~~~~~~
8392 with start==caret at the operator token, and
8393 extending to the end of the cast_expression. */
8394 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8396 /* Now, build an appropriate representation. */
8397 switch (unary_operator
)
8400 non_constant_p
= NIC_STAR
;
8401 expression
= build_x_indirect_ref (loc
, cast_expression
,
8404 /* TODO: build_x_indirect_ref does not always honor the
8405 location, so ensure it is set. */
8406 expression
.set_location (loc
);
8410 non_constant_p
= NIC_ADDR
;
8413 expression
= build_x_unary_op (loc
, unary_operator
,
8416 /* TODO: build_x_unary_op does not always honor the location,
8417 so ensure it is set. */
8418 expression
.set_location (loc
);
8421 case PREINCREMENT_EXPR
:
8422 case PREDECREMENT_EXPR
:
8423 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8424 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8427 /* Immediately fold negation of a constant, unless the constant is 0
8428 (since -0 == 0) or it would overflow. */
8429 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8432 = tree_strip_any_location_wrapper (cast_expression
);
8433 if (CONSTANT_CLASS_P (stripped_expr
)
8434 && !integer_zerop (stripped_expr
)
8435 && !TREE_OVERFLOW (stripped_expr
))
8437 tree folded
= fold_build1 (unary_operator
,
8438 TREE_TYPE (stripped_expr
),
8440 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8442 expression
= maybe_wrap_with_location (folded
, loc
);
8448 case UNARY_PLUS_EXPR
:
8449 case TRUTH_NOT_EXPR
:
8450 expression
= finish_unary_op_expr (loc
, unary_operator
,
8451 cast_expression
, complain
);
8458 if (non_constant_p
!= NIC_NONE
8459 && cp_parser_non_integral_constant_expression (parser
,
8461 expression
= error_mark_node
;
8466 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8467 /*member_access_only_p=*/false,
8472 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8473 unary-operator, the corresponding tree code is returned. */
8475 static enum tree_code
8476 cp_parser_unary_operator (cp_token
* token
)
8478 switch (token
->type
)
8481 return INDIRECT_REF
;
8487 return UNARY_PLUS_EXPR
;
8493 return TRUTH_NOT_EXPR
;
8496 return BIT_NOT_EXPR
;
8503 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8504 Returns a representation of the expression. */
8507 cp_parser_has_attribute_expression (cp_parser
*parser
)
8509 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8511 /* Consume the __builtin_has_attribute token. */
8512 cp_lexer_consume_token (parser
->lexer
);
8514 matching_parens parens
;
8515 if (!parens
.require_open (parser
))
8516 return error_mark_node
;
8518 /* Types cannot be defined in a `sizeof' expression. Save away the
8520 const char *saved_message
= parser
->type_definition_forbidden_message
;
8521 /* And create the new one. */
8522 const int kwd
= RID_BUILTIN_HAS_ATTRIBUTE
;
8523 char *tmp
= concat ("types may not be defined in %<",
8524 IDENTIFIER_POINTER (ridpointers
[kwd
]),
8525 "%> expressions", NULL
);
8526 parser
->type_definition_forbidden_message
= tmp
;
8528 /* The restrictions on constant-expressions do not apply inside
8529 sizeof expressions. */
8530 bool saved_integral_constant_expression_p
8531 = parser
->integral_constant_expression_p
;
8532 bool saved_non_integral_constant_expression_p
8533 = parser
->non_integral_constant_expression_p
;
8534 parser
->integral_constant_expression_p
= false;
8536 /* Do not actually evaluate the expression. */
8537 ++cp_unevaluated_operand
;
8538 ++c_inhibit_evaluation_warnings
;
8540 tree oper
= NULL_TREE
;
8542 /* We can't be sure yet whether we're looking at a type-id or an
8544 cp_parser_parse_tentatively (parser
);
8546 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8547 parser
->in_type_id_in_expr_p
= true;
8548 /* Look for the type-id. */
8549 oper
= cp_parser_type_id (parser
);
8550 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8552 cp_parser_parse_definitely (parser
);
8554 /* If the type-id production did not work out, then we must be
8555 looking at the unary-expression production. */
8556 if (!oper
|| oper
== error_mark_node
)
8557 oper
= cp_parser_unary_expression (parser
);
8559 STRIP_ANY_LOCATION_WRAPPER (oper
);
8561 /* Go back to evaluating expressions. */
8562 --cp_unevaluated_operand
;
8563 --c_inhibit_evaluation_warnings
;
8565 /* Free the message we created. */
8567 /* And restore the old one. */
8568 parser
->type_definition_forbidden_message
= saved_message
;
8569 parser
->integral_constant_expression_p
8570 = saved_integral_constant_expression_p
;
8571 parser
->non_integral_constant_expression_p
8572 = saved_non_integral_constant_expression_p
;
8574 /* Consume the comma if it's there. */
8575 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8577 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8578 /*consume_paren=*/true);
8579 return error_mark_node
;
8582 /* Parse the attribute specification. */
8584 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8585 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8587 if (oper
!= error_mark_node
)
8589 /* Fold constant expressions used in attributes first. */
8590 cp_check_const_attributes (attr
);
8592 /* Finally, see if OPER has been declared with ATTR. */
8593 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8596 parens
.require_close (parser
);
8600 error_at (atloc
, "expected identifier");
8601 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8604 /* Construct a location e.g. :
8605 __builtin_has_attribute (oper, attr)
8606 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8607 with start == caret at the start of the built-in token,
8608 and with the endpoint at the final closing paren. */
8609 location_t finish_loc
8610 = cp_lexer_previous_token (parser
->lexer
)->location
;
8611 location_t compound_loc
8612 = make_location (start_loc
, start_loc
, finish_loc
);
8614 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8615 ret_expr
.set_location (compound_loc
);
8616 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8620 /* Parse a new-expression.
8623 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8624 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8626 Returns a representation of the expression. */
8629 cp_parser_new_expression (cp_parser
* parser
)
8631 bool global_scope_p
;
8632 vec
<tree
, va_gc
> *placement
;
8634 vec
<tree
, va_gc
> *initializer
;
8635 tree nelts
= NULL_TREE
;
8638 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8640 /* Look for the optional `::' operator. */
8642 = (cp_parser_global_scope_opt (parser
,
8643 /*current_scope_valid_p=*/false)
8645 /* Look for the `new' operator. */
8646 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8647 /* There's no easy way to tell a new-placement from the
8648 `( type-id )' construct. */
8649 cp_parser_parse_tentatively (parser
);
8650 /* Look for a new-placement. */
8651 placement
= cp_parser_new_placement (parser
);
8652 /* If that didn't work out, there's no new-placement. */
8653 if (!cp_parser_parse_definitely (parser
))
8655 if (placement
!= NULL
)
8656 release_tree_vector (placement
);
8660 /* If the next token is a `(', then we have a parenthesized
8662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8665 const char *saved_message
= parser
->type_definition_forbidden_message
;
8667 /* Consume the `('. */
8668 matching_parens parens
;
8669 parens
.consume_open (parser
);
8671 /* Parse the type-id. */
8672 parser
->type_definition_forbidden_message
8673 = G_("types may not be defined in a new-expression");
8675 type_id_in_expr_sentinel
s (parser
);
8676 type
= cp_parser_type_id (parser
);
8678 parser
->type_definition_forbidden_message
= saved_message
;
8680 /* Look for the closing `)'. */
8681 parens
.require_close (parser
);
8682 token
= cp_lexer_peek_token (parser
->lexer
);
8683 /* There should not be a direct-new-declarator in this production,
8684 but GCC used to allowed this, so we check and emit a sensible error
8685 message for this case. */
8686 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8688 error_at (token
->location
,
8689 "array bound forbidden after parenthesized type-id");
8690 inform (token
->location
,
8691 "try removing the parentheses around the type-id");
8692 cp_parser_direct_new_declarator (parser
);
8695 /* Otherwise, there must be a new-type-id. */
8697 type
= cp_parser_new_type_id (parser
, &nelts
);
8699 /* If the next token is a `(' or '{', then we have a new-initializer. */
8700 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8701 if (token
->type
== CPP_OPEN_PAREN
8702 || token
->type
== CPP_OPEN_BRACE
)
8703 initializer
= cp_parser_new_initializer (parser
);
8707 /* A new-expression may not appear in an integral constant
8709 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8710 ret
= error_mark_node
;
8711 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8712 of a new-type-id or type-id of a new-expression, the new-expression shall
8713 contain a new-initializer of the form ( assignment-expression )".
8714 Additionally, consistently with the spirit of DR 1467, we want to accept
8715 'new auto { 2 }' too. */
8716 else if ((ret
= type_uses_auto (type
))
8717 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8718 && (vec_safe_length (initializer
) != 1
8719 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8720 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8722 error_at (token
->location
,
8723 "initialization of new-expression for type %<auto%> "
8724 "requires exactly one element");
8725 ret
= error_mark_node
;
8729 /* Construct a location e.g.:
8732 with caret == start at the start of the "new" token, and the end
8733 at the end of the final token we consumed. */
8734 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8735 location_t end_loc
= get_finish (end_tok
->location
);
8736 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8738 /* Create a representation of the new-expression. */
8739 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8740 tf_warning_or_error
);
8741 protected_set_expr_location (ret
, combined_loc
);
8744 if (placement
!= NULL
)
8745 release_tree_vector (placement
);
8746 if (initializer
!= NULL
)
8747 release_tree_vector (initializer
);
8752 /* Parse a new-placement.
8757 Returns the same representation as for an expression-list. */
8759 static vec
<tree
, va_gc
> *
8760 cp_parser_new_placement (cp_parser
* parser
)
8762 vec
<tree
, va_gc
> *expression_list
;
8764 /* Parse the expression-list. */
8765 expression_list
= (cp_parser_parenthesized_expression_list
8766 (parser
, non_attr
, /*cast_p=*/false,
8767 /*allow_expansion_p=*/true,
8768 /*non_constant_p=*/NULL
));
8770 if (expression_list
&& expression_list
->is_empty ())
8771 error ("expected expression-list or type-id");
8773 return expression_list
;
8776 /* Parse a new-type-id.
8779 type-specifier-seq new-declarator [opt]
8781 Returns the TYPE allocated. If the new-type-id indicates an array
8782 type, *NELTS is set to the number of elements in the last array
8783 bound; the TYPE will not include the last array bound. */
8786 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8788 cp_decl_specifier_seq type_specifier_seq
;
8789 cp_declarator
*new_declarator
;
8790 cp_declarator
*declarator
;
8791 cp_declarator
*outer_declarator
;
8792 const char *saved_message
;
8794 /* The type-specifier sequence must not contain type definitions.
8795 (It cannot contain declarations of new types either, but if they
8796 are not definitions we will catch that because they are not
8798 saved_message
= parser
->type_definition_forbidden_message
;
8799 parser
->type_definition_forbidden_message
8800 = G_("types may not be defined in a new-type-id");
8801 /* Parse the type-specifier-seq. */
8802 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8803 /*is_declaration=*/false,
8804 /*is_trailing_return=*/false,
8805 &type_specifier_seq
);
8806 /* Restore the old message. */
8807 parser
->type_definition_forbidden_message
= saved_message
;
8809 if (type_specifier_seq
.type
== error_mark_node
)
8810 return error_mark_node
;
8812 /* Parse the new-declarator. */
8813 new_declarator
= cp_parser_new_declarator_opt (parser
);
8815 /* Determine the number of elements in the last array dimension, if
8818 /* Skip down to the last array dimension. */
8819 declarator
= new_declarator
;
8820 outer_declarator
= NULL
;
8821 while (declarator
&& (declarator
->kind
== cdk_pointer
8822 || declarator
->kind
== cdk_ptrmem
))
8824 outer_declarator
= declarator
;
8825 declarator
= declarator
->declarator
;
8828 && declarator
->kind
== cdk_array
8829 && declarator
->declarator
8830 && declarator
->declarator
->kind
== cdk_array
)
8832 outer_declarator
= declarator
;
8833 declarator
= declarator
->declarator
;
8836 if (declarator
&& declarator
->kind
== cdk_array
)
8838 *nelts
= declarator
->u
.array
.bounds
;
8839 if (*nelts
== error_mark_node
)
8840 *nelts
= integer_one_node
;
8842 if (outer_declarator
)
8843 outer_declarator
->declarator
= declarator
->declarator
;
8845 new_declarator
= NULL
;
8848 return groktypename (&type_specifier_seq
, new_declarator
, false);
8851 /* Parse an (optional) new-declarator.
8854 ptr-operator new-declarator [opt]
8855 direct-new-declarator
8857 Returns the declarator. */
8859 static cp_declarator
*
8860 cp_parser_new_declarator_opt (cp_parser
* parser
)
8862 enum tree_code code
;
8863 tree type
, std_attributes
= NULL_TREE
;
8864 cp_cv_quals cv_quals
;
8866 /* We don't know if there's a ptr-operator next, or not. */
8867 cp_parser_parse_tentatively (parser
);
8868 /* Look for a ptr-operator. */
8869 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8870 /* If that worked, look for more new-declarators. */
8871 if (cp_parser_parse_definitely (parser
))
8873 cp_declarator
*declarator
;
8875 /* Parse another optional declarator. */
8876 declarator
= cp_parser_new_declarator_opt (parser
);
8878 declarator
= cp_parser_make_indirect_declarator
8879 (code
, type
, cv_quals
, declarator
, std_attributes
);
8884 /* If the next token is a `[', there is a direct-new-declarator. */
8885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8886 return cp_parser_direct_new_declarator (parser
);
8891 /* Parse a direct-new-declarator.
8893 direct-new-declarator:
8895 direct-new-declarator [constant-expression]
8899 static cp_declarator
*
8900 cp_parser_direct_new_declarator (cp_parser
* parser
)
8902 cp_declarator
*declarator
= NULL
;
8909 /* Look for the opening `['. */
8910 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8912 token
= cp_lexer_peek_token (parser
->lexer
);
8913 expression
= cp_parser_expression (parser
);
8914 /* The standard requires that the expression have integral
8915 type. DR 74 adds enumeration types. We believe that the
8916 real intent is that these expressions be handled like the
8917 expression in a `switch' condition, which also allows
8918 classes with a single conversion to integral or
8919 enumeration type. */
8920 if (!processing_template_decl
)
8923 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8928 error_at (token
->location
,
8929 "expression in new-declarator must have integral "
8930 "or enumeration type");
8931 expression
= error_mark_node
;
8935 /* Look for the closing `]'. */
8936 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8938 /* Add this bound to the declarator. */
8939 declarator
= make_array_declarator (declarator
, expression
);
8941 /* If the next token is not a `[', then there are no more
8943 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8950 /* Parse a new-initializer.
8953 ( expression-list [opt] )
8956 Returns a representation of the expression-list. */
8958 static vec
<tree
, va_gc
> *
8959 cp_parser_new_initializer (cp_parser
* parser
)
8961 vec
<tree
, va_gc
> *expression_list
;
8963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8966 bool expr_non_constant_p
;
8967 cp_lexer_set_source_position (parser
->lexer
);
8968 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8969 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8970 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8971 expression_list
= make_tree_vector_single (t
);
8974 expression_list
= (cp_parser_parenthesized_expression_list
8975 (parser
, non_attr
, /*cast_p=*/false,
8976 /*allow_expansion_p=*/true,
8977 /*non_constant_p=*/NULL
));
8979 return expression_list
;
8982 /* Parse a delete-expression.
8985 :: [opt] delete cast-expression
8986 :: [opt] delete [ ] cast-expression
8988 Returns a representation of the expression. */
8991 cp_parser_delete_expression (cp_parser
* parser
)
8993 bool global_scope_p
;
8997 /* Look for the optional `::' operator. */
8999 = (cp_parser_global_scope_opt (parser
,
9000 /*current_scope_valid_p=*/false)
9002 /* Look for the `delete' keyword. */
9003 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
9004 /* See if the array syntax is in use. */
9005 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9007 /* Consume the `[' token. */
9008 cp_lexer_consume_token (parser
->lexer
);
9009 /* Look for the `]' token. */
9010 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9011 /* Remember that this is the `[]' construct. */
9017 /* Parse the cast-expression. */
9018 expression
= cp_parser_simple_cast_expression (parser
);
9020 /* A delete-expression may not appear in an integral constant
9022 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9023 return error_mark_node
;
9025 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
9026 tf_warning_or_error
);
9029 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9030 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9034 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9036 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9037 switch (token
->type
)
9043 case CPP_CLOSE_SQUARE
:
9044 case CPP_CLOSE_PAREN
:
9045 case CPP_CLOSE_BRACE
:
9046 case CPP_OPEN_BRACE
:
9050 case CPP_DEREF_STAR
:
9058 case CPP_GREATER_EQ
:
9079 case CPP_OPEN_PAREN
:
9080 /* In ((type ()) () the last () isn't a valid cast-expression,
9081 so the whole must be parsed as postfix-expression. */
9082 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9085 case CPP_OPEN_SQUARE
:
9086 /* '[' may start a primary-expression in obj-c++ and in C++11,
9087 as a lambda-expression, eg, '(void)[]{}'. */
9088 if (cxx_dialect
>= cxx11
)
9090 return c_dialect_objc ();
9093 case CPP_MINUS_MINUS
:
9094 /* '++' and '--' may or may not start a cast-expression:
9096 struct T { void operator++(int); };
9097 void f() { (T())++; }
9110 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9111 in the order: const_cast, static_cast, reinterpret_cast.
9113 Don't suggest dynamic_cast.
9115 Return the first legal cast kind found, or NULL otherwise. */
9118 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9122 /* Reuse the parser logic by attempting to build the various kinds of
9123 cast, with "complain" disabled.
9124 Identify the first such cast that is valid. */
9126 /* Don't attempt to run such logic within template processing. */
9127 if (processing_template_decl
)
9130 /* First try const_cast. */
9131 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9132 if (trial
!= error_mark_node
)
9133 return "const_cast";
9135 /* If that fails, try static_cast. */
9136 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9137 if (trial
!= error_mark_node
)
9138 return "static_cast";
9140 /* Finally, try reinterpret_cast. */
9141 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9142 if (trial
!= error_mark_node
)
9143 return "reinterpret_cast";
9145 /* No such cast possible. */
9149 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9150 suggesting how to convert a C-style cast of the form:
9154 to a C++-style cast.
9156 The primary range of RICHLOC is asssumed to be that of the original
9157 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9158 of the parens in the C-style cast. */
9161 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9162 location_t close_paren_loc
, tree orig_expr
,
9165 /* This function is non-trivial, so bail out now if the warning isn't
9166 going to be emitted. */
9167 if (!warn_old_style_cast
)
9170 /* Try to find a legal C++ cast, trying them in order:
9171 const_cast, static_cast, reinterpret_cast. */
9172 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9173 if (!cast_suggestion
)
9176 /* Replace the open paren with "CAST_SUGGESTION<". */
9178 pp_printf (&pp
, "%s<", cast_suggestion
);
9179 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9181 /* Replace the close paren with "> (". */
9182 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9184 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9185 rich_loc
->add_fixit_insert_after (")");
9189 /* Parse a cast-expression.
9193 ( type-id ) cast-expression
9195 ADDRESS_P is true iff the unary-expression is appearing as the
9196 operand of the `&' operator. CAST_P is true if this expression is
9197 the target of a cast.
9199 Returns a representation of the expression. */
9202 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9203 bool decltype_p
, cp_id_kind
* pidk
)
9205 /* If it's a `(', then we might be looking at a cast. */
9206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9208 tree type
= NULL_TREE
;
9209 cp_expr
expr (NULL_TREE
);
9210 int cast_expression
= 0;
9211 const char *saved_message
;
9213 /* There's no way to know yet whether or not this is a cast.
9214 For example, `(int (3))' is a unary-expression, while `(int)
9215 3' is a cast. So, we resort to parsing tentatively. */
9216 cp_parser_parse_tentatively (parser
);
9217 /* Types may not be defined in a cast. */
9218 saved_message
= parser
->type_definition_forbidden_message
;
9219 parser
->type_definition_forbidden_message
9220 = G_("types may not be defined in casts");
9221 /* Consume the `('. */
9222 matching_parens parens
;
9223 cp_token
*open_paren
= parens
.consume_open (parser
);
9224 location_t open_paren_loc
= open_paren
->location
;
9225 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9227 /* A very tricky bit is that `(struct S) { 3 }' is a
9228 compound-literal (which we permit in C++ as an extension).
9229 But, that construct is not a cast-expression -- it is a
9230 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9231 is legal; if the compound-literal were a cast-expression,
9232 you'd need an extra set of parentheses.) But, if we parse
9233 the type-id, and it happens to be a class-specifier, then we
9234 will commit to the parse at that point, because we cannot
9235 undo the action that is done when creating a new class. So,
9236 then we cannot back up and do a postfix-expression.
9238 Another tricky case is the following (c++/29234):
9240 struct S { void operator () (); };
9247 As a type-id we parse the parenthesized S()() as a function
9248 returning a function, groktypename complains and we cannot
9249 back up in this case either.
9251 Therefore, we scan ahead to the closing `)', and check to see
9252 if the tokens after the `)' can start a cast-expression. Otherwise
9253 we are dealing with an unary-expression, a postfix-expression
9256 Yet another tricky case, in C++11, is the following (c++/54891):
9260 The issue is that usually, besides the case of lambda-expressions,
9261 the parenthesized type-id cannot be followed by '[', and, eg, we
9262 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9263 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9264 we don't commit, we try a cast-expression, then an unary-expression.
9266 Save tokens so that we can put them back. */
9267 cp_lexer_save_tokens (parser
->lexer
);
9269 /* We may be looking at a cast-expression. */
9270 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9271 /*consume_paren=*/true))
9273 = cp_parser_tokens_start_cast_expression (parser
);
9275 /* Roll back the tokens we skipped. */
9276 cp_lexer_rollback_tokens (parser
->lexer
);
9277 /* If we aren't looking at a cast-expression, simulate an error so
9278 that the call to cp_parser_error_occurred below returns true. */
9279 if (!cast_expression
)
9280 cp_parser_simulate_error (parser
);
9283 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9284 parser
->in_type_id_in_expr_p
= true;
9285 /* Look for the type-id. */
9286 type
= cp_parser_type_id (parser
);
9287 /* Look for the closing `)'. */
9288 cp_token
*close_paren
= parens
.require_close (parser
);
9290 close_paren_loc
= close_paren
->location
;
9291 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9294 /* Restore the saved message. */
9295 parser
->type_definition_forbidden_message
= saved_message
;
9297 /* At this point this can only be either a cast or a
9298 parenthesized ctor such as `(T ())' that looks like a cast to
9299 function returning T. */
9300 if (!cp_parser_error_occurred (parser
))
9302 /* Only commit if the cast-expression doesn't start with
9303 '++', '--', or '[' in C++11. */
9304 if (cast_expression
> 0)
9305 cp_parser_commit_to_topmost_tentative_parse (parser
);
9307 expr
= cp_parser_cast_expression (parser
,
9308 /*address_p=*/false,
9310 /*decltype_p=*/false,
9313 if (cp_parser_parse_definitely (parser
))
9315 /* Warn about old-style casts, if so requested. */
9316 if (warn_old_style_cast
9317 && !in_system_header_at (input_location
)
9318 && !VOID_TYPE_P (type
)
9319 && current_lang_name
!= lang_name_c
)
9321 gcc_rich_location
rich_loc (input_location
);
9322 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9324 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9325 "use of old-style cast to %q#T", type
);
9328 /* Only type conversions to integral or enumeration types
9329 can be used in constant-expressions. */
9330 if (!cast_valid_in_integral_constant_expression_p (type
)
9331 && cp_parser_non_integral_constant_expression (parser
,
9333 return error_mark_node
;
9335 /* Perform the cast. */
9339 with start==caret at the open paren, extending to the
9341 location_t cast_loc
= make_location (open_paren_loc
,
9343 expr
.get_finish ());
9344 expr
= build_c_cast (cast_loc
, type
, expr
);
9349 cp_parser_abort_tentative_parse (parser
);
9352 /* If we get here, then it's not a cast, so it must be a
9353 unary-expression. */
9354 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9355 cast_p
, decltype_p
);
9358 /* Parse a binary expression of the general form:
9362 pm-expression .* cast-expression
9363 pm-expression ->* cast-expression
9365 multiplicative-expression:
9367 multiplicative-expression * pm-expression
9368 multiplicative-expression / pm-expression
9369 multiplicative-expression % pm-expression
9371 additive-expression:
9372 multiplicative-expression
9373 additive-expression + multiplicative-expression
9374 additive-expression - multiplicative-expression
9378 shift-expression << additive-expression
9379 shift-expression >> additive-expression
9381 relational-expression:
9383 relational-expression < shift-expression
9384 relational-expression > shift-expression
9385 relational-expression <= shift-expression
9386 relational-expression >= shift-expression
9390 relational-expression:
9391 relational-expression <? shift-expression
9392 relational-expression >? shift-expression
9394 equality-expression:
9395 relational-expression
9396 equality-expression == relational-expression
9397 equality-expression != relational-expression
9401 and-expression & equality-expression
9403 exclusive-or-expression:
9405 exclusive-or-expression ^ and-expression
9407 inclusive-or-expression:
9408 exclusive-or-expression
9409 inclusive-or-expression | exclusive-or-expression
9411 logical-and-expression:
9412 inclusive-or-expression
9413 logical-and-expression && inclusive-or-expression
9415 logical-or-expression:
9416 logical-and-expression
9417 logical-or-expression || logical-and-expression
9419 All these are implemented with a single function like:
9422 simple-cast-expression
9423 binary-expression <token> binary-expression
9425 CAST_P is true if this expression is the target of a cast.
9427 The binops_by_token map is used to get the tree codes for each <token> type.
9428 binary-expressions are associated according to a precedence table. */
9430 #define TOKEN_PRECEDENCE(token) \
9431 (((token->type == CPP_GREATER \
9432 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9433 && !parser->greater_than_is_operator_p) \
9434 ? PREC_NOT_OPERATOR \
9435 : binops_by_token[token->type].prec)
9438 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9439 bool no_toplevel_fold_p
,
9441 enum cp_parser_prec prec
,
9444 cp_parser_expression_stack stack
;
9445 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9446 cp_parser_expression_stack_entry current
;
9449 enum tree_code rhs_type
;
9450 enum cp_parser_prec new_prec
, lookahead_prec
;
9453 /* Parse the first expression. */
9454 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9455 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9456 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9457 cast_p
, decltype_p
, pidk
);
9458 current
.prec
= prec
;
9460 if (cp_parser_error_occurred (parser
))
9461 return error_mark_node
;
9465 /* Get an operator token. */
9466 token
= cp_lexer_peek_token (parser
->lexer
);
9468 if (warn_cxx11_compat
9469 && token
->type
== CPP_RSHIFT
9470 && !parser
->greater_than_is_operator_p
)
9472 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9473 "%<>>%> operator is treated"
9474 " as two right angle brackets in C++11"))
9475 inform (token
->location
,
9476 "suggest parentheses around %<>>%> expression");
9479 new_prec
= TOKEN_PRECEDENCE (token
);
9480 if (new_prec
!= PREC_NOT_OPERATOR
9481 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9482 /* This is a fold-expression; handle it later. */
9483 new_prec
= PREC_NOT_OPERATOR
;
9485 /* Popping an entry off the stack means we completed a subexpression:
9486 - either we found a token which is not an operator (`>' where it is not
9487 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9488 will happen repeatedly;
9489 - or, we found an operator which has lower priority. This is the case
9490 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9492 if (new_prec
<= current
.prec
)
9501 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9502 current
.loc
= token
->location
;
9504 /* We used the operator token. */
9505 cp_lexer_consume_token (parser
->lexer
);
9507 /* For "false && x" or "true || x", x will never be executed;
9508 disable warnings while evaluating it. */
9509 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9510 c_inhibit_evaluation_warnings
+=
9511 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9512 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9513 c_inhibit_evaluation_warnings
+=
9514 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9516 /* Extract another operand. It may be the RHS of this expression
9517 or the LHS of a new, higher priority expression. */
9518 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9519 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9520 rhs
= cp_parser_simple_cast_expression (parser
);
9522 /* Get another operator token. Look up its precedence to avoid
9523 building a useless (immediately popped) stack entry for common
9524 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9525 token
= cp_lexer_peek_token (parser
->lexer
);
9526 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9527 if (lookahead_prec
!= PREC_NOT_OPERATOR
9528 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9529 lookahead_prec
= PREC_NOT_OPERATOR
;
9530 if (lookahead_prec
> new_prec
)
9532 /* ... and prepare to parse the RHS of the new, higher priority
9533 expression. Since precedence levels on the stack are
9534 monotonically increasing, we do not have to care about
9539 current
.lhs_type
= rhs_type
;
9540 current
.prec
= new_prec
;
9541 new_prec
= lookahead_prec
;
9545 lookahead_prec
= new_prec
;
9546 /* If the stack is not empty, we have parsed into LHS the right side
9547 (`4' in the example above) of an expression we had suspended.
9548 We can use the information on the stack to recover the LHS (`3')
9549 from the stack together with the tree code (`MULT_EXPR'), and
9550 the precedence of the higher level subexpression
9551 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9552 which will be used to actually build the additive expression. */
9554 rhs_type
= current
.lhs_type
;
9559 /* Undo the disabling of warnings done above. */
9560 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9561 c_inhibit_evaluation_warnings
-=
9562 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9563 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9564 c_inhibit_evaluation_warnings
-=
9565 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9567 if (warn_logical_not_paren
9568 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9569 && current
.lhs_type
== TRUTH_NOT_EXPR
9570 /* Avoid warning for !!x == y. */
9571 && (TREE_CODE (current
.lhs
) != NE_EXPR
9572 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9573 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9574 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9575 /* Avoid warning for !b == y where b is boolean. */
9576 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9577 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9579 /* Avoid warning for !!b == y where b is boolean. */
9580 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9581 || TREE_TYPE (current
.lhs
) == NULL_TREE
9582 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9583 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9584 current
.lhs
, maybe_constant_value (rhs
));
9588 location_t combined_loc
= make_location (current
.loc
,
9589 current
.lhs
.get_start (),
9592 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9593 ERROR_MARK for everything that is not a binary expression.
9594 This makes warn_about_parentheses miss some warnings that
9595 involve unary operators. For unary expressions we should
9596 pass the correct tree_code unless the unary expression was
9597 surrounded by parentheses.
9599 if (no_toplevel_fold_p
9600 && lookahead_prec
<= current
.prec
9603 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9604 current
.lhs
= error_mark_node
;
9608 = build_min (current
.tree_type
,
9609 TREE_CODE_CLASS (current
.tree_type
)
9611 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9612 current
.lhs
.get_value (), rhs
.get_value ());
9613 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9618 op_location_t
op_loc (current
.loc
, combined_loc
);
9619 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9620 current
.lhs
, current
.lhs_type
,
9621 rhs
, rhs_type
, &overload
,
9622 complain_flags (decltype_p
));
9623 /* TODO: build_x_binary_op doesn't always honor the location. */
9624 current
.lhs
.set_location (combined_loc
);
9626 current
.lhs_type
= current
.tree_type
;
9628 /* If the binary operator required the use of an overloaded operator,
9629 then this expression cannot be an integral constant-expression.
9630 An overloaded operator can be used even if both operands are
9631 otherwise permissible in an integral constant-expression if at
9632 least one of the operands is of enumeration type. */
9635 && cp_parser_non_integral_constant_expression (parser
,
9637 return error_mark_node
;
9644 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9645 bool no_toplevel_fold_p
,
9646 enum cp_parser_prec prec
,
9649 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9650 /*decltype*/false, prec
, pidk
);
9653 /* Parse the `? expression : assignment-expression' part of a
9654 conditional-expression. The LOGICAL_OR_EXPR is the
9655 logical-or-expression that started the conditional-expression.
9656 Returns a representation of the entire conditional-expression.
9658 This routine is used by cp_parser_assignment_expression.
9660 ? expression : assignment-expression
9664 ? : assignment-expression */
9667 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9669 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9670 cp_expr assignment_expr
;
9671 struct cp_token
*token
;
9672 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9674 /* Consume the `?' token. */
9675 cp_lexer_consume_token (parser
->lexer
);
9676 token
= cp_lexer_peek_token (parser
->lexer
);
9677 if (cp_parser_allow_gnu_extensions_p (parser
)
9678 && token
->type
== CPP_COLON
)
9680 pedwarn (token
->location
, OPT_Wpedantic
,
9681 "ISO C++ does not allow ?: with omitted middle operand");
9682 /* Implicit true clause. */
9684 c_inhibit_evaluation_warnings
+=
9685 folded_logical_or_expr
== truthvalue_true_node
;
9686 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9690 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9691 parser
->colon_corrects_to_scope_p
= false;
9692 /* Parse the expression. */
9693 c_inhibit_evaluation_warnings
+=
9694 folded_logical_or_expr
== truthvalue_false_node
;
9695 expr
= cp_parser_expression (parser
);
9696 c_inhibit_evaluation_warnings
+=
9697 ((folded_logical_or_expr
== truthvalue_true_node
)
9698 - (folded_logical_or_expr
== truthvalue_false_node
));
9699 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9702 /* The next token should be a `:'. */
9703 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9704 /* Parse the assignment-expression. */
9705 assignment_expr
= cp_parser_assignment_expression (parser
);
9706 c_inhibit_evaluation_warnings
-=
9707 folded_logical_or_expr
== truthvalue_true_node
;
9710 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9711 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9712 with the caret at the "?", ranging from the start of
9713 the logical_or_expr to the end of the assignment_expr. */
9714 loc
= make_location (loc
,
9715 logical_or_expr
.get_start (),
9716 assignment_expr
.get_finish ());
9718 /* Build the conditional-expression. */
9719 return build_x_conditional_expr (loc
, logical_or_expr
,
9722 tf_warning_or_error
);
9725 /* Parse an assignment-expression.
9727 assignment-expression:
9728 conditional-expression
9729 logical-or-expression assignment-operator assignment_expression
9732 CAST_P is true if this expression is the target of a cast.
9733 DECLTYPE_P is true if this expression is the operand of decltype.
9735 Returns a representation for the expression. */
9738 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9739 bool cast_p
, bool decltype_p
)
9743 /* If the next token is the `throw' keyword, then we're looking at
9744 a throw-expression. */
9745 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9746 expr
= cp_parser_throw_expression (parser
);
9747 /* Otherwise, it must be that we are looking at a
9748 logical-or-expression. */
9751 /* Parse the binary expressions (logical-or-expression). */
9752 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9754 PREC_NOT_OPERATOR
, pidk
);
9755 /* If the next token is a `?' then we're actually looking at a
9756 conditional-expression. */
9757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9758 return cp_parser_question_colon_clause (parser
, expr
);
9761 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9763 /* If it's an assignment-operator, we're using the second
9765 enum tree_code assignment_operator
9766 = cp_parser_assignment_operator_opt (parser
);
9767 if (assignment_operator
!= ERROR_MARK
)
9769 bool non_constant_p
;
9771 /* Parse the right-hand side of the assignment. */
9772 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9775 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9776 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9778 /* An assignment may not appear in a
9779 constant-expression. */
9780 if (cp_parser_non_integral_constant_expression (parser
,
9782 return error_mark_node
;
9783 /* Build the assignment expression. Its default
9787 is the location of the '=' token as the
9788 caret, ranging from the start of the lhs to the
9790 loc
= make_location (loc
,
9793 expr
= build_x_modify_expr (loc
, expr
,
9794 assignment_operator
,
9796 complain_flags (decltype_p
));
9797 /* TODO: build_x_modify_expr doesn't honor the location,
9798 so we must set it here. */
9799 expr
.set_location (loc
);
9807 /* Parse an (optional) assignment-operator.
9809 assignment-operator: one of
9810 = *= /= %= += -= >>= <<= &= ^= |=
9814 assignment-operator: one of
9817 If the next token is an assignment operator, the corresponding tree
9818 code is returned, and the token is consumed. For example, for
9819 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9820 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9821 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9822 operator, ERROR_MARK is returned. */
9824 static enum tree_code
9825 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9830 /* Peek at the next token. */
9831 token
= cp_lexer_peek_token (parser
->lexer
);
9833 switch (token
->type
)
9844 op
= TRUNC_DIV_EXPR
;
9848 op
= TRUNC_MOD_EXPR
;
9880 /* Nothing else is an assignment operator. */
9884 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9885 if (op
!= ERROR_MARK
9886 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9889 /* If it was an assignment operator, consume it. */
9890 if (op
!= ERROR_MARK
)
9891 cp_lexer_consume_token (parser
->lexer
);
9896 /* Parse an expression.
9899 assignment-expression
9900 expression , assignment-expression
9902 CAST_P is true if this expression is the target of a cast.
9903 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9904 except possibly parenthesized or on the RHS of a comma (N3276).
9906 Returns a representation of the expression. */
9909 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9910 bool cast_p
, bool decltype_p
)
9912 cp_expr expression
= NULL_TREE
;
9913 location_t loc
= UNKNOWN_LOCATION
;
9917 cp_expr assignment_expression
;
9919 /* Parse the next assignment-expression. */
9920 assignment_expression
9921 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9923 /* We don't create a temporary for a call that is the immediate operand
9924 of decltype or on the RHS of a comma. But when we see a comma, we
9925 need to create a temporary for a call on the LHS. */
9926 if (decltype_p
&& !processing_template_decl
9927 && TREE_CODE (assignment_expression
) == CALL_EXPR
9928 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9929 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9930 assignment_expression
9931 = build_cplus_new (TREE_TYPE (assignment_expression
),
9932 assignment_expression
, tf_warning_or_error
);
9934 /* If this is the first assignment-expression, we can just
9937 expression
= assignment_expression
;
9940 /* Create a location with caret at the comma, ranging
9941 from the start of the LHS to the end of the RHS. */
9942 loc
= make_location (loc
,
9943 expression
.get_start (),
9944 assignment_expression
.get_finish ());
9945 expression
= build_x_compound_expr (loc
, expression
,
9946 assignment_expression
,
9947 complain_flags (decltype_p
));
9948 expression
.set_location (loc
);
9950 /* If the next token is not a comma, or we're in a fold-expression, then
9951 we are done with the expression. */
9952 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9953 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9955 /* Consume the `,'. */
9956 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9957 cp_lexer_consume_token (parser
->lexer
);
9958 /* A comma operator cannot appear in a constant-expression. */
9959 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9960 expression
= error_mark_node
;
9966 /* Parse a constant-expression.
9968 constant-expression:
9969 conditional-expression
9971 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9972 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9973 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9974 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9975 only parse a conditional-expression, otherwise parse an
9976 assignment-expression. See below for rationale. */
9979 cp_parser_constant_expression (cp_parser
* parser
,
9980 bool allow_non_constant_p
,
9981 bool *non_constant_p
,
9984 bool saved_integral_constant_expression_p
;
9985 bool saved_allow_non_integral_constant_expression_p
;
9986 bool saved_non_integral_constant_expression_p
;
9989 /* It might seem that we could simply parse the
9990 conditional-expression, and then check to see if it were
9991 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9992 one that the compiler can figure out is constant, possibly after
9993 doing some simplifications or optimizations. The standard has a
9994 precise definition of constant-expression, and we must honor
9995 that, even though it is somewhat more restrictive.
10001 is not a legal declaration, because `(2, 3)' is not a
10002 constant-expression. The `,' operator is forbidden in a
10003 constant-expression. However, GCC's constant-folding machinery
10004 will fold this operation to an INTEGER_CST for `3'. */
10006 /* Save the old settings. */
10007 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10008 saved_allow_non_integral_constant_expression_p
10009 = parser
->allow_non_integral_constant_expression_p
;
10010 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10011 /* We are now parsing a constant-expression. */
10012 parser
->integral_constant_expression_p
= true;
10013 parser
->allow_non_integral_constant_expression_p
10014 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10015 parser
->non_integral_constant_expression_p
= false;
10016 /* Although the grammar says "conditional-expression", when not STRICT_P,
10017 we parse an "assignment-expression", which also permits
10018 "throw-expression" and the use of assignment operators. In the case
10019 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10020 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10021 actually essential that we look for an assignment-expression.
10022 For example, cp_parser_initializer_clauses uses this function to
10023 determine whether a particular assignment-expression is in fact
10027 /* Parse the binary expressions (logical-or-expression). */
10028 expression
= cp_parser_binary_expression (parser
, false, false, false,
10029 PREC_NOT_OPERATOR
, NULL
);
10030 /* If the next token is a `?' then we're actually looking at
10031 a conditional-expression; otherwise we're done. */
10032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10033 expression
= cp_parser_question_colon_clause (parser
, expression
);
10036 expression
= cp_parser_assignment_expression (parser
);
10037 /* Restore the old settings. */
10038 parser
->integral_constant_expression_p
10039 = saved_integral_constant_expression_p
;
10040 parser
->allow_non_integral_constant_expression_p
10041 = saved_allow_non_integral_constant_expression_p
;
10042 if (cxx_dialect
>= cxx11
)
10044 /* Require an rvalue constant expression here; that's what our
10045 callers expect. Reference constant expressions are handled
10046 separately in e.g. cp_parser_template_argument. */
10047 tree decay
= expression
;
10048 if (TREE_TYPE (expression
)
10049 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10050 decay
= build_address (expression
);
10051 bool is_const
= potential_rvalue_constant_expression (decay
);
10052 parser
->non_integral_constant_expression_p
= !is_const
;
10053 if (!is_const
&& !allow_non_constant_p
)
10054 require_potential_rvalue_constant_expression (decay
);
10056 if (allow_non_constant_p
)
10057 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10058 parser
->non_integral_constant_expression_p
10059 = saved_non_integral_constant_expression_p
;
10064 /* Parse __builtin_offsetof.
10066 offsetof-expression:
10067 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10069 offsetof-member-designator:
10071 | offsetof-member-designator "." id-expression
10072 | offsetof-member-designator "[" expression "]"
10073 | offsetof-member-designator "->" id-expression */
10076 cp_parser_builtin_offsetof (cp_parser
*parser
)
10078 int save_ice_p
, save_non_ice_p
;
10083 location_t finish_loc
;
10085 /* We're about to accept non-integral-constant things, but will
10086 definitely yield an integral constant expression. Save and
10087 restore these values around our local parsing. */
10088 save_ice_p
= parser
->integral_constant_expression_p
;
10089 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10091 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10093 /* Consume the "__builtin_offsetof" token. */
10094 cp_lexer_consume_token (parser
->lexer
);
10095 /* Consume the opening `('. */
10096 matching_parens parens
;
10097 parens
.require_open (parser
);
10098 /* Parse the type-id. */
10099 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10101 const char *saved_message
= parser
->type_definition_forbidden_message
;
10102 parser
->type_definition_forbidden_message
10103 = G_("types may not be defined within %<__builtin_offsetof%>");
10104 type
= cp_parser_type_id (parser
);
10105 parser
->type_definition_forbidden_message
= saved_message
;
10107 /* Look for the `,'. */
10108 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10109 token
= cp_lexer_peek_token (parser
->lexer
);
10111 /* Build the (type *)null that begins the traditional offsetof macro. */
10113 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10114 tf_warning_or_error
);
10116 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10117 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10118 true, &dummy
, token
->location
);
10121 token
= cp_lexer_peek_token (parser
->lexer
);
10122 switch (token
->type
)
10124 case CPP_OPEN_SQUARE
:
10125 /* offsetof-member-designator "[" expression "]" */
10126 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10131 /* offsetof-member-designator "->" identifier */
10132 expr
= grok_array_decl (token
->location
, expr
,
10133 integer_zero_node
, false);
10137 /* offsetof-member-designator "." identifier */
10138 cp_lexer_consume_token (parser
->lexer
);
10139 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10140 expr
, true, &dummy
,
10144 case CPP_CLOSE_PAREN
:
10145 /* Consume the ")" token. */
10146 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10147 cp_lexer_consume_token (parser
->lexer
);
10151 /* Error. We know the following require will fail, but
10152 that gives the proper error message. */
10153 parens
.require_close (parser
);
10154 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10155 expr
= error_mark_node
;
10161 /* Make a location of the form:
10162 __builtin_offsetof (struct s, f)
10163 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10164 with caret at the type-id, ranging from the start of the
10165 "_builtin_offsetof" token to the close paren. */
10166 loc
= make_location (loc
, start_loc
, finish_loc
);
10167 /* The result will be an INTEGER_CST, so we need to explicitly
10168 preserve the location. */
10169 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10172 parser
->integral_constant_expression_p
= save_ice_p
;
10173 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10175 expr
= expr
.maybe_add_location_wrapper ();
10179 /* Parse a trait expression.
10181 Returns a representation of the expression, the underlying type
10182 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10185 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10187 cp_trait_kind kind
;
10188 tree type1
, type2
= NULL_TREE
;
10189 bool binary
= false;
10190 bool variadic
= false;
10194 case RID_HAS_NOTHROW_ASSIGN
:
10195 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10197 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10198 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10200 case RID_HAS_NOTHROW_COPY
:
10201 kind
= CPTK_HAS_NOTHROW_COPY
;
10203 case RID_HAS_TRIVIAL_ASSIGN
:
10204 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10206 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10207 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10209 case RID_HAS_TRIVIAL_COPY
:
10210 kind
= CPTK_HAS_TRIVIAL_COPY
;
10212 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10213 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10215 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10216 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10218 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10219 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10221 case RID_IS_ABSTRACT
:
10222 kind
= CPTK_IS_ABSTRACT
;
10224 case RID_IS_AGGREGATE
:
10225 kind
= CPTK_IS_AGGREGATE
;
10227 case RID_IS_BASE_OF
:
10228 kind
= CPTK_IS_BASE_OF
;
10232 kind
= CPTK_IS_CLASS
;
10235 kind
= CPTK_IS_EMPTY
;
10238 kind
= CPTK_IS_ENUM
;
10241 kind
= CPTK_IS_FINAL
;
10243 case RID_IS_LITERAL_TYPE
:
10244 kind
= CPTK_IS_LITERAL_TYPE
;
10247 kind
= CPTK_IS_POD
;
10249 case RID_IS_POLYMORPHIC
:
10250 kind
= CPTK_IS_POLYMORPHIC
;
10252 case RID_IS_SAME_AS
:
10253 kind
= CPTK_IS_SAME_AS
;
10256 case RID_IS_STD_LAYOUT
:
10257 kind
= CPTK_IS_STD_LAYOUT
;
10259 case RID_IS_TRIVIAL
:
10260 kind
= CPTK_IS_TRIVIAL
;
10262 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10263 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10266 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10267 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10270 case RID_IS_TRIVIALLY_COPYABLE
:
10271 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10274 kind
= CPTK_IS_UNION
;
10276 case RID_UNDERLYING_TYPE
:
10277 kind
= CPTK_UNDERLYING_TYPE
;
10282 case RID_DIRECT_BASES
:
10283 kind
= CPTK_DIRECT_BASES
;
10285 case RID_IS_ASSIGNABLE
:
10286 kind
= CPTK_IS_ASSIGNABLE
;
10289 case RID_IS_CONSTRUCTIBLE
:
10290 kind
= CPTK_IS_CONSTRUCTIBLE
;
10294 gcc_unreachable ();
10297 /* Get location of initial token. */
10298 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10300 /* Consume the token. */
10301 cp_lexer_consume_token (parser
->lexer
);
10303 matching_parens parens
;
10304 parens
.require_open (parser
);
10307 type_id_in_expr_sentinel
s (parser
);
10308 type1
= cp_parser_type_id (parser
);
10311 if (type1
== error_mark_node
)
10312 return error_mark_node
;
10316 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10319 type_id_in_expr_sentinel
s (parser
);
10320 type2
= cp_parser_type_id (parser
);
10323 if (type2
== error_mark_node
)
10324 return error_mark_node
;
10328 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10330 cp_lexer_consume_token (parser
->lexer
);
10331 tree elt
= cp_parser_type_id (parser
);
10332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10334 cp_lexer_consume_token (parser
->lexer
);
10335 elt
= make_pack_expansion (elt
);
10337 if (elt
== error_mark_node
)
10338 return error_mark_node
;
10339 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10343 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10344 parens
.require_close (parser
);
10346 /* Construct a location of the form:
10347 __is_trivially_copyable(_Tp)
10348 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10349 with start == caret, finishing at the close-paren. */
10350 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10352 /* Complete the trait expression, which may mean either processing
10353 the trait expr now or saving it for template instantiation. */
10356 case CPTK_UNDERLYING_TYPE
:
10357 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10359 return cp_expr (finish_bases (type1
, false), trait_loc
);
10360 case CPTK_DIRECT_BASES
:
10361 return cp_expr (finish_bases (type1
, true), trait_loc
);
10363 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10367 /* Parse a lambda expression.
10370 lambda-introducer lambda-declarator [opt] compound-statement
10372 Returns a representation of the expression. */
10375 cp_parser_lambda_expression (cp_parser
* parser
)
10377 tree lambda_expr
= build_lambda_expr ();
10380 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10381 cp_token_position start
= 0;
10383 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10385 if (cxx_dialect
>= cxx2a
)
10386 /* C++20 allows lambdas in unevaluated context. */;
10387 else if (cp_unevaluated_operand
)
10389 if (!token
->error_reported
)
10391 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10392 "lambda-expression in unevaluated context"
10393 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10394 token
->error_reported
= true;
10398 else if (parser
->in_template_argument_list_p
)
10400 if (!token
->error_reported
)
10402 error_at (token
->location
, "lambda-expression in template-argument"
10403 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10404 token
->error_reported
= true;
10409 /* We may be in the middle of deferred access check. Disable
10411 push_deferring_access_checks (dk_no_deferred
);
10413 cp_parser_lambda_introducer (parser
, lambda_expr
);
10414 if (cp_parser_error_occurred (parser
))
10415 return error_mark_node
;
10417 type
= begin_lambda_type (lambda_expr
);
10418 if (type
== error_mark_node
)
10419 return error_mark_node
;
10421 record_lambda_scope (lambda_expr
);
10423 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10424 determine_visibility (TYPE_NAME (type
));
10426 /* Now that we've started the type, add the capture fields for any
10427 explicit captures. */
10428 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10431 /* Inside the class, surrounding template-parameter-lists do not apply. */
10432 unsigned int saved_num_template_parameter_lists
10433 = parser
->num_template_parameter_lists
;
10434 unsigned char in_statement
= parser
->in_statement
;
10435 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10436 bool fully_implicit_function_template_p
10437 = parser
->fully_implicit_function_template_p
;
10438 tree implicit_template_parms
= parser
->implicit_template_parms
;
10439 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10440 bool auto_is_implicit_function_template_parm_p
10441 = parser
->auto_is_implicit_function_template_parm_p
;
10443 parser
->num_template_parameter_lists
= 0;
10444 parser
->in_statement
= 0;
10445 parser
->in_switch_statement_p
= false;
10446 parser
->fully_implicit_function_template_p
= false;
10447 parser
->implicit_template_parms
= 0;
10448 parser
->implicit_template_scope
= 0;
10449 parser
->auto_is_implicit_function_template_parm_p
= false;
10451 /* By virtue of defining a local class, a lambda expression has access to
10452 the private variables of enclosing classes. */
10454 if (cp_parser_start_tentative_firewall (parser
))
10457 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10459 if (ok
&& cp_parser_error_occurred (parser
))
10464 cp_parser_lambda_body (parser
, lambda_expr
);
10466 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10468 if (cp_parser_skip_to_closing_brace (parser
))
10469 cp_lexer_consume_token (parser
->lexer
);
10472 /* The capture list was built up in reverse order; fix that now. */
10473 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10474 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10477 maybe_add_lambda_conv_op (type
);
10479 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10481 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10482 parser
->in_statement
= in_statement
;
10483 parser
->in_switch_statement_p
= in_switch_statement_p
;
10484 parser
->fully_implicit_function_template_p
10485 = fully_implicit_function_template_p
;
10486 parser
->implicit_template_parms
= implicit_template_parms
;
10487 parser
->implicit_template_scope
= implicit_template_scope
;
10488 parser
->auto_is_implicit_function_template_parm_p
10489 = auto_is_implicit_function_template_parm_p
;
10492 /* This field is only used during parsing of the lambda. */
10493 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10495 /* This lambda shouldn't have any proxies left at this point. */
10496 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10497 /* And now that we're done, push proxies for an enclosing lambda. */
10498 insert_pending_capture_proxies ();
10500 /* Update the lambda expression to a range. */
10501 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10502 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10504 end_tok
->location
);
10507 lambda_expr
= build_lambda_object (lambda_expr
);
10509 lambda_expr
= error_mark_node
;
10511 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10513 pop_deferring_access_checks ();
10515 return lambda_expr
;
10518 /* Parse the beginning of a lambda expression.
10521 [ lambda-capture [opt] ]
10523 LAMBDA_EXPR is the current representation of the lambda expression. */
10526 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10528 /* Need commas after the first capture. */
10531 /* Eat the leading `['. */
10532 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10534 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10536 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10537 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10538 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10539 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10541 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10543 cp_lexer_consume_token (parser
->lexer
);
10546 if (!(at_function_scope_p () || parsing_nsdmi ()))
10547 error ("non-local lambda expression cannot have a capture-default");
10550 hash_set
<tree
, true> ids
;
10551 tree first_capture_id
= NULL_TREE
;
10552 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10554 cp_token
* capture_token
;
10556 tree capture_init_expr
;
10557 cp_id_kind idk
= CP_ID_KIND_NONE
;
10558 bool explicit_init_p
= false;
10560 enum capture_kind_type
10565 enum capture_kind_type capture_kind
= BY_COPY
;
10567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10569 error ("expected end of capture-list");
10576 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10578 /* Possibly capture `this'. */
10579 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10581 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10582 if (cxx_dialect
< cxx2a
10583 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10584 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10585 "with by-copy capture default");
10586 cp_lexer_consume_token (parser
->lexer
);
10587 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10588 pedwarn (input_location
, 0,
10589 "already captured %qD in lambda expression",
10592 add_capture (lambda_expr
, /*id=*/this_identifier
,
10593 /*initializer=*/finish_this_expr (),
10594 /*by_reference_p=*/true, explicit_init_p
);
10598 /* Possibly capture `*this'. */
10599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10600 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10602 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10603 if (cxx_dialect
< cxx17
)
10604 pedwarn (loc
, 0, "%<*this%> capture only available with "
10605 "%<-std=c++17%> or %<-std=gnu++17%>");
10606 cp_lexer_consume_token (parser
->lexer
);
10607 cp_lexer_consume_token (parser
->lexer
);
10608 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10609 pedwarn (input_location
, 0,
10610 "already captured %qD in lambda expression",
10613 add_capture (lambda_expr
, /*id=*/this_identifier
,
10614 /*initializer=*/finish_this_expr (),
10615 /*by_reference_p=*/false, explicit_init_p
);
10619 bool init_pack_expansion
= false;
10620 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
10621 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10623 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10624 if (cxx_dialect
< cxx2a
)
10625 pedwarn (ellipsis_loc
, 0, "pack init-capture only available with "
10626 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10627 cp_lexer_consume_token (parser
->lexer
);
10628 init_pack_expansion
= true;
10631 /* Remember whether we want to capture as a reference or not. */
10632 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10634 capture_kind
= BY_REFERENCE
;
10635 cp_lexer_consume_token (parser
->lexer
);
10638 /* Get the identifier. */
10639 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10640 capture_id
= cp_parser_identifier (parser
);
10642 if (capture_id
== error_mark_node
)
10643 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10644 delimiters, but I modified this to stop on unnested ']' as well. It
10645 was already changed to stop on unnested '}', so the
10646 "closing_parenthesis" name is no more misleading with my change. */
10648 cp_parser_skip_to_closing_parenthesis (parser
,
10649 /*recovering=*/true,
10651 /*consume_paren=*/true);
10655 /* Find the initializer for this capture. */
10656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10657 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10658 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10660 bool direct
, non_constant
;
10661 /* An explicit initializer exists. */
10662 if (cxx_dialect
< cxx14
)
10663 pedwarn (input_location
, 0,
10664 "lambda capture initializers "
10665 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10666 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10667 &non_constant
, true);
10668 explicit_init_p
= true;
10669 if (capture_init_expr
== NULL_TREE
)
10671 error ("empty initializer for lambda init-capture");
10672 capture_init_expr
= error_mark_node
;
10674 if (init_pack_expansion
)
10675 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10679 const char* error_msg
;
10681 /* Turn the identifier into an id-expression. */
10683 = cp_parser_lookup_name_simple (parser
, capture_id
,
10684 capture_token
->location
);
10686 if (capture_init_expr
== error_mark_node
)
10688 unqualified_name_lookup_error (capture_id
);
10691 else if (!VAR_P (capture_init_expr
)
10692 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10694 error_at (capture_token
->location
,
10695 "capture of non-variable %qE",
10696 capture_init_expr
);
10697 if (DECL_P (capture_init_expr
))
10698 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10699 "%q#D declared here", capture_init_expr
);
10702 if (VAR_P (capture_init_expr
)
10703 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10705 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10706 "%qD with non-automatic storage duration",
10707 capture_init_expr
))
10708 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10709 "%q#D declared here", capture_init_expr
);
10714 = finish_id_expression
10719 /*integral_constant_expression_p=*/false,
10720 /*allow_non_integral_constant_expression_p=*/false,
10721 /*non_integral_constant_expression_p=*/NULL
,
10722 /*template_p=*/false,
10724 /*address_p=*/false,
10725 /*template_arg_p=*/false,
10727 capture_token
->location
);
10729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10731 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10732 cp_lexer_consume_token (parser
->lexer
);
10733 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10734 if (init_pack_expansion
)
10736 /* If what follows is an initializer, the second '...' is
10737 invalid. But for cases like [...xs...], the first one
10739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10740 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10741 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10742 ellipsis_loc
= loc
;
10743 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
10749 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10750 && !explicit_init_p
)
10752 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10753 && capture_kind
== BY_COPY
)
10754 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10755 "of %qD redundant with by-copy capture default",
10757 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10758 && capture_kind
== BY_REFERENCE
)
10759 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10760 "capture of %qD redundant with by-reference capture "
10761 "default", capture_id
);
10764 /* Check for duplicates.
10765 Optimize for the zero or one explicit captures cases and only create
10766 the hash_set after adding second capture. */
10767 bool found
= false;
10768 if (ids
.elements ())
10769 found
= ids
.add (capture_id
);
10770 else if (first_capture_id
== NULL_TREE
)
10771 first_capture_id
= capture_id
;
10772 else if (capture_id
== first_capture_id
)
10776 ids
.add (first_capture_id
);
10777 ids
.add (capture_id
);
10780 pedwarn (input_location
, 0,
10781 "already captured %qD in lambda expression", capture_id
);
10783 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
10784 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10787 /* If there is any qualification still in effect, clear it
10788 now; we will be starting fresh with the next capture. */
10789 parser
->scope
= NULL_TREE
;
10790 parser
->qualifying_scope
= NULL_TREE
;
10791 parser
->object_scope
= NULL_TREE
;
10794 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10797 /* Parse the (optional) middle of a lambda expression.
10800 < template-parameter-list [opt] >
10801 ( parameter-declaration-clause [opt] )
10802 attribute-specifier [opt]
10803 decl-specifier-seq [opt]
10804 exception-specification [opt]
10805 lambda-return-type-clause [opt]
10807 LAMBDA_EXPR is the current representation of the lambda expression. */
10810 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10812 /* 5.1.1.4 of the standard says:
10813 If a lambda-expression does not include a lambda-declarator, it is as if
10814 the lambda-declarator were ().
10815 This means an empty parameter list, no attributes, and no exception
10817 tree param_list
= void_list_node
;
10818 tree std_attrs
= NULL_TREE
;
10819 tree gnu_attrs
= NULL_TREE
;
10820 tree exception_spec
= NULL_TREE
;
10821 tree template_param_list
= NULL_TREE
;
10822 tree tx_qual
= NULL_TREE
;
10823 tree return_type
= NULL_TREE
;
10824 cp_decl_specifier_seq lambda_specs
;
10825 clear_decl_specs (&lambda_specs
);
10827 /* The template-parameter-list is optional, but must begin with
10828 an opening angle if present. */
10829 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10831 if (cxx_dialect
< cxx14
)
10832 pedwarn (parser
->lexer
->next_token
->location
, 0,
10833 "lambda templates are only available with "
10834 "%<-std=c++14%> or %<-std=gnu++14%>");
10835 else if (cxx_dialect
< cxx2a
)
10836 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10837 "lambda templates are only available with "
10838 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10840 cp_lexer_consume_token (parser
->lexer
);
10842 template_param_list
= cp_parser_template_parameter_list (parser
);
10844 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10846 /* We just processed one more parameter list. */
10847 ++parser
->num_template_parameter_lists
;
10850 /* The parameter-declaration-clause is optional (unless
10851 template-parameter-list was given), but must begin with an
10852 opening parenthesis if present. */
10853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10855 matching_parens parens
;
10856 parens
.consume_open (parser
);
10858 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10860 /* Parse parameters. */
10862 = cp_parser_parameter_declaration_clause
10863 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
10865 /* Default arguments shall not be specified in the
10866 parameter-declaration-clause of a lambda-declarator. */
10867 if (cxx_dialect
< cxx14
)
10868 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10869 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10870 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10871 "default argument specified for lambda parameter");
10873 parens
.require_close (parser
);
10875 /* In the decl-specifier-seq of the lambda-declarator, each
10876 decl-specifier shall either be mutable or constexpr. */
10877 int declares_class_or_enum
;
10878 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
10879 && !cp_next_tokens_can_be_gnu_attribute_p (parser
))
10880 cp_parser_decl_specifier_seq (parser
,
10881 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10882 &lambda_specs
, &declares_class_or_enum
);
10883 if (lambda_specs
.storage_class
== sc_mutable
)
10885 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10886 if (lambda_specs
.conflicting_specifiers_p
)
10887 error_at (lambda_specs
.locations
[ds_storage_class
],
10888 "duplicate %<mutable%>");
10891 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10893 /* Parse optional exception specification. */
10894 exception_spec
= cp_parser_exception_specification_opt (parser
);
10896 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10898 /* Parse optional trailing return type. */
10899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10901 cp_lexer_consume_token (parser
->lexer
);
10902 return_type
= cp_parser_trailing_type_id (parser
);
10905 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
10906 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
10908 /* The function parameters must be in scope all the way until after the
10909 trailing-return-type in case of decltype. */
10910 pop_bindings_and_leave_scope ();
10912 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10913 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10915 /* Create the function call operator.
10917 Messing with declarators like this is no uglier than building up the
10918 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10921 cp_decl_specifier_seq return_type_specs
;
10922 cp_declarator
* declarator
;
10927 clear_decl_specs (&return_type_specs
);
10928 return_type_specs
.type
= make_auto ();
10930 if (lambda_specs
.locations
[ds_constexpr
])
10932 if (cxx_dialect
>= cxx17
)
10933 return_type_specs
.locations
[ds_constexpr
]
10934 = lambda_specs
.locations
[ds_constexpr
];
10936 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10937 "lambda only available with %<-std=c++17%> or "
10938 "%<-std=gnu++17%>");
10941 p
= obstack_alloc (&declarator_obstack
, 0);
10943 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
10944 LAMBDA_EXPR_LOCATION (lambda_expr
));
10946 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10947 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10948 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10949 VIRT_SPEC_UNSPECIFIED
,
10954 /*requires_clause*/NULL_TREE
);
10955 declarator
->std_attributes
= std_attrs
;
10957 fco
= grokmethod (&return_type_specs
,
10960 if (fco
!= error_mark_node
)
10962 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10963 DECL_ARTIFICIAL (fco
) = 1;
10964 /* Give the object parameter a different name. */
10965 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10966 DECL_LAMBDA_FUNCTION (fco
) = 1;
10968 if (template_param_list
)
10970 fco
= finish_member_template_decl (fco
);
10971 finish_template_decl (template_param_list
);
10972 --parser
->num_template_parameter_lists
;
10974 else if (parser
->fully_implicit_function_template_p
)
10975 fco
= finish_fully_implicit_template (parser
, fco
);
10977 finish_member_declaration (fco
);
10979 obstack_free (&declarator_obstack
, p
);
10981 return (fco
!= error_mark_node
);
10985 /* Parse the body of a lambda expression, which is simply
10989 but which requires special handling.
10990 LAMBDA_EXPR is the current representation of the lambda expression. */
10993 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10995 bool nested
= (current_function_decl
!= NULL_TREE
);
10996 unsigned char local_variables_forbidden_p
10997 = parser
->local_variables_forbidden_p
;
10998 bool in_function_body
= parser
->in_function_body
;
11000 /* The body of a lambda-expression is not a subexpression of the enclosing
11005 push_function_context ();
11007 /* Still increment function_depth so that we don't GC in the
11008 middle of an expression. */
11011 vec
<tree
> omp_privatization_save
;
11012 save_omp_privatization_clauses (omp_privatization_save
);
11013 /* Clear this in case we're in the middle of a default argument. */
11014 parser
->local_variables_forbidden_p
= 0;
11015 parser
->in_function_body
= true;
11018 local_specialization_stack
s (lss_copy
);
11019 tree fco
= lambda_function (lambda_expr
);
11020 tree body
= start_lambda_function (fco
, lambda_expr
);
11021 matching_braces braces
;
11023 if (braces
.require_open (parser
))
11025 tree compound_stmt
= begin_compound_stmt (0);
11027 /* Originally C++11 required us to peek for 'return expr'; and
11028 process it specially here to deduce the return type. N3638
11029 removed the need for that. */
11031 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11032 cp_parser_label_declaration (parser
);
11033 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11034 braces
.require_close (parser
);
11036 finish_compound_stmt (compound_stmt
);
11039 finish_lambda_function (body
);
11042 restore_omp_privatization_clauses (omp_privatization_save
);
11043 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11044 parser
->in_function_body
= in_function_body
;
11046 pop_function_context();
11051 /* Statements [gram.stmt.stmt] */
11053 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11056 add_debug_begin_stmt (location_t loc
)
11058 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11060 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11061 /* A concept is never expanded normally. */
11064 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11065 SET_EXPR_LOCATION (stmt
, loc
);
11069 /* Parse a statement.
11073 expression-statement
11075 selection-statement
11076 iteration-statement
11078 declaration-statement
11085 attribute-specifier-seq (opt) expression-statement
11086 attribute-specifier-seq (opt) compound-statement
11087 attribute-specifier-seq (opt) selection-statement
11088 attribute-specifier-seq (opt) iteration-statement
11089 attribute-specifier-seq (opt) jump-statement
11090 declaration-statement
11091 attribute-specifier-seq (opt) try-block
11094 expression-statement
11102 IN_COMPOUND is true when the statement is nested inside a
11103 cp_parser_compound_statement; this matters for certain pragmas.
11105 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11106 is a (possibly labeled) if statement which is not enclosed in braces
11107 and has an else clause. This is used to implement -Wparentheses.
11109 CHAIN is a vector of if-else-if conditions. */
11112 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11113 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11114 location_t
*loc_after_labels
)
11116 tree statement
, std_attrs
= NULL_TREE
;
11118 location_t statement_location
, attrs_loc
;
11123 /* There is no statement yet. */
11124 statement
= NULL_TREE
;
11126 saved_token_sentinel
saved_tokens (parser
->lexer
);
11127 attrs_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11128 if (c_dialect_objc ())
11129 /* In obj-c++, seeing '[[' might be the either the beginning of
11130 c++11 attributes, or a nested objc-message-expression. So
11131 let's parse the c++11 attributes tentatively. */
11132 cp_parser_parse_tentatively (parser
);
11133 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11137 = cp_lexer_previous_token (parser
->lexer
)->location
;
11138 attrs_loc
= make_location (attrs_loc
, attrs_loc
, end_loc
);
11140 if (c_dialect_objc ())
11142 if (!cp_parser_parse_definitely (parser
))
11143 std_attrs
= NULL_TREE
;
11146 /* Peek at the next token. */
11147 token
= cp_lexer_peek_token (parser
->lexer
);
11148 /* Remember the location of the first token in the statement. */
11149 cp_token
*statement_token
= token
;
11150 statement_location
= token
->location
;
11151 add_debug_begin_stmt (statement_location
);
11152 /* If this is a keyword, then that will often determine what kind of
11153 statement we have. */
11154 if (token
->type
== CPP_KEYWORD
)
11156 enum rid keyword
= token
->keyword
;
11162 /* Looks like a labeled-statement with a case label.
11163 Parse the label, and then use tail recursion to parse
11165 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11166 in_compound
= false;
11171 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11172 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11178 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11179 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11186 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11187 statement
= cp_parser_jump_statement (parser
);
11190 /* Objective-C++ exception-handling constructs. */
11193 case RID_AT_FINALLY
:
11194 case RID_AT_SYNCHRONIZED
:
11196 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11197 statement
= cp_parser_objc_statement (parser
);
11201 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11202 statement
= cp_parser_try_block (parser
);
11205 case RID_NAMESPACE
:
11206 /* This must be a namespace alias definition. */
11207 if (std_attrs
!= NULL_TREE
)
11209 /* Attributes should be parsed as part of the the
11210 declaration, so let's un-parse them. */
11211 saved_tokens
.rollback();
11212 std_attrs
= NULL_TREE
;
11214 cp_parser_declaration_statement (parser
);
11217 case RID_TRANSACTION_ATOMIC
:
11218 case RID_TRANSACTION_RELAXED
:
11219 case RID_SYNCHRONIZED
:
11220 case RID_ATOMIC_NOEXCEPT
:
11221 case RID_ATOMIC_CANCEL
:
11222 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11223 statement
= cp_parser_transaction (parser
, token
);
11225 case RID_TRANSACTION_CANCEL
:
11226 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11227 statement
= cp_parser_transaction_cancel (parser
);
11231 /* It might be a keyword like `int' that can start a
11232 declaration-statement. */
11236 else if (token
->type
== CPP_NAME
)
11238 /* If the next token is a `:', then we are looking at a
11239 labeled-statement. */
11240 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11241 if (token
->type
== CPP_COLON
)
11243 /* Looks like a labeled-statement with an ordinary label.
11244 Parse the label, and then use tail recursion to parse
11247 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11248 in_compound
= false;
11252 /* Anything that starts with a `{' must be a compound-statement. */
11253 else if (token
->type
== CPP_OPEN_BRACE
)
11254 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11255 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11256 a statement all its own. */
11257 else if (token
->type
== CPP_PRAGMA
)
11259 /* Only certain OpenMP pragmas are attached to statements, and thus
11260 are considered statements themselves. All others are not. In
11261 the context of a compound, accept the pragma as a "statement" and
11262 return so that we can check for a close brace. Otherwise we
11263 require a real statement and must go back and read one. */
11265 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11266 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11270 else if (token
->type
== CPP_EOF
)
11272 cp_parser_error (parser
, "expected statement");
11276 /* Everything else must be a declaration-statement or an
11277 expression-statement. Try for the declaration-statement
11278 first, unless we are looking at a `;', in which case we know that
11279 we have an expression-statement. */
11282 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11284 if (std_attrs
!= NULL_TREE
)
11285 /* Attributes should be parsed as part of the declaration,
11286 so let's un-parse them. */
11287 saved_tokens
.rollback();
11289 cp_parser_parse_tentatively (parser
);
11290 /* Try to parse the declaration-statement. */
11291 cp_parser_declaration_statement (parser
);
11292 /* If that worked, we're done. */
11293 if (cp_parser_parse_definitely (parser
))
11295 /* It didn't work, restore the post-attribute position. */
11297 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11299 /* All preceding labels have been parsed at this point. */
11300 if (loc_after_labels
!= NULL
)
11301 *loc_after_labels
= statement_location
;
11303 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11305 /* Look for an expression-statement instead. */
11306 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11308 /* Handle [[fallthrough]];. */
11309 if (attribute_fallthrough_p (std_attrs
))
11311 /* The next token after the fallthrough attribute is ';'. */
11312 if (statement
== NULL_TREE
)
11314 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11315 statement
= build_call_expr_internal_loc (statement_location
,
11317 void_type_node
, 0);
11318 finish_expr_stmt (statement
);
11321 warning_at (statement_location
, OPT_Wattributes
,
11322 "%<fallthrough%> attribute not followed by %<;%>");
11323 std_attrs
= NULL_TREE
;
11327 /* Set the line number for the statement. */
11328 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11329 SET_EXPR_LOCATION (statement
, statement_location
);
11331 /* Allow "[[fallthrough]];", but warn otherwise. */
11332 if (std_attrs
!= NULL_TREE
)
11333 warning_at (attrs_loc
,
11335 "attributes at the beginning of statement are ignored");
11338 /* Append ATTR to attribute list ATTRS. */
11341 attr_chainon (tree attrs
, tree attr
)
11343 if (attrs
== error_mark_node
)
11344 return error_mark_node
;
11345 if (attr
== error_mark_node
)
11346 return error_mark_node
;
11347 return chainon (attrs
, attr
);
11350 /* Parse the label for a labeled-statement, i.e.
11353 case constant-expression :
11357 case constant-expression ... constant-expression : statement
11359 When a label is parsed without errors, the label is added to the
11360 parse tree by the finish_* functions, so this function doesn't
11361 have to return the label. */
11364 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11367 tree label
= NULL_TREE
;
11368 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11370 /* The next token should be an identifier. */
11371 token
= cp_lexer_peek_token (parser
->lexer
);
11372 if (token
->type
!= CPP_NAME
11373 && token
->type
!= CPP_KEYWORD
)
11375 cp_parser_error (parser
, "expected labeled-statement");
11379 /* Remember whether this case or a user-defined label is allowed to fall
11381 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11383 parser
->colon_corrects_to_scope_p
= false;
11384 switch (token
->keyword
)
11388 tree expr
, expr_hi
;
11389 cp_token
*ellipsis
;
11391 /* Consume the `case' token. */
11392 cp_lexer_consume_token (parser
->lexer
);
11393 /* Parse the constant-expression. */
11394 expr
= cp_parser_constant_expression (parser
);
11395 if (check_for_bare_parameter_packs (expr
))
11396 expr
= error_mark_node
;
11398 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11399 if (ellipsis
->type
== CPP_ELLIPSIS
)
11401 /* Consume the `...' token. */
11402 cp_lexer_consume_token (parser
->lexer
);
11403 expr_hi
= cp_parser_constant_expression (parser
);
11404 if (check_for_bare_parameter_packs (expr_hi
))
11405 expr_hi
= error_mark_node
;
11407 /* We don't need to emit warnings here, as the common code
11408 will do this for us. */
11411 expr_hi
= NULL_TREE
;
11413 if (parser
->in_switch_statement_p
)
11415 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11416 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11418 label
= CASE_LABEL (l
);
11419 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11423 error_at (token
->location
,
11424 "case label %qE not within a switch statement",
11430 /* Consume the `default' token. */
11431 cp_lexer_consume_token (parser
->lexer
);
11433 if (parser
->in_switch_statement_p
)
11435 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11436 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11438 label
= CASE_LABEL (l
);
11439 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11443 error_at (token
->location
, "case label not within a switch statement");
11447 /* Anything else must be an ordinary label. */
11448 label
= finish_label_stmt (cp_parser_identifier (parser
));
11449 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11450 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11454 /* Require the `:' token. */
11455 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11457 /* An ordinary label may optionally be followed by attributes.
11458 However, this is only permitted if the attributes are then
11459 followed by a semicolon. This is because, for backward
11460 compatibility, when parsing
11461 lab: __attribute__ ((unused)) int i;
11462 we want the attribute to attach to "i", not "lab". */
11463 if (label
!= NULL_TREE
11464 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11467 cp_parser_parse_tentatively (parser
);
11468 attrs
= cp_parser_gnu_attributes_opt (parser
);
11469 if (attrs
== NULL_TREE
11470 /* And fallthrough always binds to the expression-statement. */
11471 || attribute_fallthrough_p (attrs
)
11472 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11473 cp_parser_abort_tentative_parse (parser
);
11474 else if (!cp_parser_parse_definitely (parser
))
11477 attributes
= attr_chainon (attributes
, attrs
);
11480 if (attributes
!= NULL_TREE
)
11481 cplus_decl_attributes (&label
, attributes
, 0);
11483 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11486 /* Parse an expression-statement.
11488 expression-statement:
11491 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11492 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11493 indicates whether this expression-statement is part of an
11494 expression statement. */
11497 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11499 tree statement
= NULL_TREE
;
11500 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11501 location_t loc
= token
->location
;
11503 /* There might be attribute fallthrough. */
11504 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11506 /* If the next token is a ';', then there is no expression
11508 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11510 statement
= cp_parser_expression (parser
);
11511 if (statement
== error_mark_node
11512 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11514 cp_parser_skip_to_end_of_block_or_statement (parser
);
11515 return error_mark_node
;
11519 /* Handle [[fallthrough]];. */
11520 if (attribute_fallthrough_p (attr
))
11522 /* The next token after the fallthrough attribute is ';'. */
11523 if (statement
== NULL_TREE
)
11524 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11525 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11526 void_type_node
, 0);
11528 warning_at (loc
, OPT_Wattributes
,
11529 "%<fallthrough%> attribute not followed by %<;%>");
11533 /* Allow "[[fallthrough]];", but warn otherwise. */
11534 if (attr
!= NULL_TREE
)
11535 warning_at (loc
, OPT_Wattributes
,
11536 "attributes at the beginning of statement are ignored");
11538 /* Give a helpful message for "A<T>::type t;" and the like. */
11539 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11540 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11542 if (TREE_CODE (statement
) == SCOPE_REF
)
11543 error_at (token
->location
, "need %<typename%> before %qE because "
11544 "%qT is a dependent scope",
11545 statement
, TREE_OPERAND (statement
, 0));
11546 else if (is_overloaded_fn (statement
)
11547 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11550 tree fn
= get_first_fn (statement
);
11551 error_at (token
->location
,
11552 "%<%T::%D%> names the constructor, not the type",
11553 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11557 /* Consume the final `;'. */
11558 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11560 if (in_statement_expr
11561 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11562 /* This is the final expression statement of a statement
11564 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11565 else if (statement
)
11566 statement
= finish_expr_stmt (statement
);
11571 /* Parse a compound-statement.
11573 compound-statement:
11574 { statement-seq [opt] }
11578 compound-statement:
11579 { label-declaration-seq [opt] statement-seq [opt] }
11581 label-declaration-seq:
11583 label-declaration-seq label-declaration
11585 Returns a tree representing the statement. */
11588 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11589 int bcs_flags
, bool function_body
)
11591 tree compound_stmt
;
11592 matching_braces braces
;
11594 /* Consume the `{'. */
11595 if (!braces
.require_open (parser
))
11596 return error_mark_node
;
11597 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11598 && !function_body
&& cxx_dialect
< cxx14
)
11599 pedwarn (input_location
, OPT_Wpedantic
,
11600 "compound-statement in %<constexpr%> function");
11601 /* Begin the compound-statement. */
11602 compound_stmt
= begin_compound_stmt (bcs_flags
);
11603 /* If the next keyword is `__label__' we have a label declaration. */
11604 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11605 cp_parser_label_declaration (parser
);
11606 /* Parse an (optional) statement-seq. */
11607 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11608 /* Finish the compound-statement. */
11609 finish_compound_stmt (compound_stmt
);
11610 /* Consume the `}'. */
11611 braces
.require_close (parser
);
11613 return compound_stmt
;
11616 /* Parse an (optional) statement-seq.
11620 statement-seq [opt] statement */
11623 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11625 /* Scan statements until there aren't any more. */
11628 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11630 /* If we are looking at a `}', then we have run out of
11631 statements; the same is true if we have reached the end
11632 of file, or have stumbled upon a stray '@end'. */
11633 if (token
->type
== CPP_CLOSE_BRACE
11634 || token
->type
== CPP_EOF
11635 || token
->type
== CPP_PRAGMA_EOL
11636 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11639 /* If we are in a compound statement and find 'else' then
11640 something went wrong. */
11641 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11643 if (parser
->in_statement
& IN_IF_STMT
)
11647 token
= cp_lexer_consume_token (parser
->lexer
);
11648 error_at (token
->location
, "%<else%> without a previous %<if%>");
11652 /* Parse the statement. */
11653 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11657 /* Return true if this is the C++20 version of range-based-for with
11661 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11665 /* Save tokens so that we can put them back. */
11666 cp_lexer_save_tokens (parser
->lexer
);
11668 /* There has to be an unnested ; followed by an unnested :. */
11669 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11670 /*recovering=*/false,
11672 /*consume_paren=*/false) != -1)
11675 /* We found the semicolon, eat it now. */
11676 cp_lexer_consume_token (parser
->lexer
);
11678 /* Now look for ':' that is not nested in () or {}. */
11679 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11680 /*recovering=*/false,
11682 /*consume_paren=*/false) == -1);
11685 /* Roll back the tokens we skipped. */
11686 cp_lexer_rollback_tokens (parser
->lexer
);
11691 /* Return true if we're looking at (init; cond), false otherwise. */
11694 cp_parser_init_statement_p (cp_parser
*parser
)
11696 /* Save tokens so that we can put them back. */
11697 cp_lexer_save_tokens (parser
->lexer
);
11699 /* Look for ';' that is not nested in () or {}. */
11700 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11701 /*recovering=*/false,
11703 /*consume_paren=*/false);
11705 /* Roll back the tokens we skipped. */
11706 cp_lexer_rollback_tokens (parser
->lexer
);
11711 /* Parse a selection-statement.
11713 selection-statement:
11714 if ( init-statement [opt] condition ) statement
11715 if ( init-statement [opt] condition ) statement else statement
11716 switch ( init-statement [opt] condition ) statement
11718 Returns the new IF_STMT or SWITCH_STMT.
11720 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11721 is a (possibly labeled) if statement which is not enclosed in
11722 braces and has an else clause. This is used to implement
11725 CHAIN is a vector of if-else-if conditions. This is used to implement
11726 -Wduplicated-cond. */
11729 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11734 token_indent_info guard_tinfo
;
11739 /* Peek at the next token. */
11740 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11741 guard_tinfo
= get_token_indent_info (token
);
11743 /* See what kind of keyword it is. */
11744 keyword
= token
->keyword
;
11754 if (keyword
== RID_IF
11755 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11759 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11760 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11761 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11762 "with %<-std=c++17%> or %<-std=gnu++17%>");
11765 /* Look for the `('. */
11766 matching_parens parens
;
11767 if (!parens
.require_open (parser
))
11769 cp_parser_skip_to_end_of_statement (parser
);
11770 return error_mark_node
;
11773 /* Begin the selection-statement. */
11774 if (keyword
== RID_IF
)
11776 statement
= begin_if_stmt ();
11777 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11780 statement
= begin_switch_stmt ();
11782 /* Parse the optional init-statement. */
11783 if (cp_parser_init_statement_p (parser
))
11786 if (cxx_dialect
< cxx17
)
11787 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11788 "init-statement in selection statements only available "
11789 "with %<-std=c++17%> or %<-std=gnu++17%>");
11790 cp_parser_init_statement (parser
, &decl
);
11793 /* Parse the condition. */
11794 condition
= cp_parser_condition (parser
);
11795 /* Look for the `)'. */
11796 if (!parens
.require_close (parser
))
11797 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11798 /*consume_paren=*/true);
11800 if (keyword
== RID_IF
)
11803 unsigned char in_statement
;
11805 /* Add the condition. */
11806 condition
= finish_if_stmt_cond (condition
, statement
);
11808 if (warn_duplicated_cond
)
11809 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11812 /* Parse the then-clause. */
11813 in_statement
= parser
->in_statement
;
11814 parser
->in_statement
|= IN_IF_STMT
;
11816 /* Outside a template, the non-selected branch of a constexpr
11817 if is a 'discarded statement', i.e. unevaluated. */
11818 bool was_discarded
= in_discarded_stmt
;
11819 bool discard_then
= (cx
&& !processing_template_decl
11820 && integer_zerop (condition
));
11823 in_discarded_stmt
= true;
11824 ++c_inhibit_evaluation_warnings
;
11827 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11830 parser
->in_statement
= in_statement
;
11832 finish_then_clause (statement
);
11836 THEN_CLAUSE (statement
) = NULL_TREE
;
11837 in_discarded_stmt
= was_discarded
;
11838 --c_inhibit_evaluation_warnings
;
11841 /* If the next token is `else', parse the else-clause. */
11842 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11845 bool discard_else
= (cx
&& !processing_template_decl
11846 && integer_nonzerop (condition
));
11849 in_discarded_stmt
= true;
11850 ++c_inhibit_evaluation_warnings
;
11854 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11855 /* Consume the `else' keyword. */
11856 cp_lexer_consume_token (parser
->lexer
);
11857 if (warn_duplicated_cond
)
11859 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11863 /* We've got "if (COND) else if (COND2)". Start
11864 the condition chain and add COND as the first
11866 chain
= new vec
<tree
> ();
11867 if (!CONSTANT_CLASS_P (condition
)
11868 && !TREE_SIDE_EFFECTS (condition
))
11870 /* Wrap it in a NOP_EXPR so that we can set the
11871 location of the condition. */
11872 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11874 SET_EXPR_LOCATION (e
, token
->location
);
11875 chain
->safe_push (e
);
11878 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11881 /* This is if-else without subsequent if. Zap the
11882 condition chain; we would have already warned at
11888 begin_else_clause (statement
);
11889 /* Parse the else-clause. */
11890 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11891 guard_tinfo
, chain
);
11893 finish_else_clause (statement
);
11895 /* If we are currently parsing a then-clause, then
11896 IF_P will not be NULL. We set it to true to
11897 indicate that this if statement has an else clause.
11898 This may trigger the Wparentheses warning below
11899 when we get back up to the parent if statement. */
11905 ELSE_CLAUSE (statement
) = NULL_TREE
;
11906 in_discarded_stmt
= was_discarded
;
11907 --c_inhibit_evaluation_warnings
;
11912 /* This if statement does not have an else clause. If
11913 NESTED_IF is true, then the then-clause has an if
11914 statement which does have an else clause. We warn
11915 about the potential ambiguity. */
11917 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11918 "suggest explicit braces to avoid ambiguous"
11920 if (warn_duplicated_cond
)
11922 /* We don't need the condition chain anymore. */
11928 /* Now we're all done with the if-statement. */
11929 finish_if_stmt (statement
);
11933 bool in_switch_statement_p
;
11934 unsigned char in_statement
;
11936 /* Add the condition. */
11937 finish_switch_cond (condition
, statement
);
11939 /* Parse the body of the switch-statement. */
11940 in_switch_statement_p
= parser
->in_switch_statement_p
;
11941 in_statement
= parser
->in_statement
;
11942 parser
->in_switch_statement_p
= true;
11943 parser
->in_statement
|= IN_SWITCH_STMT
;
11944 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11946 parser
->in_switch_statement_p
= in_switch_statement_p
;
11947 parser
->in_statement
= in_statement
;
11949 /* Now we're all done with the switch-statement. */
11950 finish_switch_stmt (statement
);
11958 cp_parser_error (parser
, "expected selection-statement");
11959 return error_mark_node
;
11963 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11964 If we have seen at least one decl-specifier, and the next token
11965 is not a parenthesis, then we must be looking at a declaration.
11966 (After "int (" we might be looking at a functional cast.) */
11969 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11970 bool any_specifiers_p
)
11972 if (any_specifiers_p
11973 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11974 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11975 && !cp_parser_error_occurred (parser
))
11976 cp_parser_commit_to_tentative_parse (parser
);
11979 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11980 The declarator shall not specify a function or an array. Returns
11981 TRUE if the declarator is valid, FALSE otherwise. */
11984 cp_parser_check_condition_declarator (cp_parser
* parser
,
11985 cp_declarator
*declarator
,
11988 if (declarator
== cp_error_declarator
11989 || function_declarator_p (declarator
)
11990 || declarator
->kind
== cdk_array
)
11992 if (declarator
== cp_error_declarator
)
11993 /* Already complained. */;
11994 else if (declarator
->kind
== cdk_array
)
11995 error_at (loc
, "condition declares an array");
11997 error_at (loc
, "condition declares a function");
11998 if (parser
->fully_implicit_function_template_p
)
11999 abort_fully_implicit_template (parser
);
12000 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
12001 /*or_comma=*/false,
12002 /*consume_paren=*/false);
12009 /* Parse a condition.
12013 type-specifier-seq declarator = initializer-clause
12014 type-specifier-seq declarator braced-init-list
12019 type-specifier-seq declarator asm-specification [opt]
12020 attributes [opt] = assignment-expression
12022 Returns the expression that should be tested. */
12025 cp_parser_condition (cp_parser
* parser
)
12027 cp_decl_specifier_seq type_specifiers
;
12028 const char *saved_message
;
12029 int declares_class_or_enum
;
12031 /* Try the declaration first. */
12032 cp_parser_parse_tentatively (parser
);
12033 /* New types are not allowed in the type-specifier-seq for a
12035 saved_message
= parser
->type_definition_forbidden_message
;
12036 parser
->type_definition_forbidden_message
12037 = G_("types may not be defined in conditions");
12038 /* Parse the type-specifier-seq. */
12039 cp_parser_decl_specifier_seq (parser
,
12040 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
12042 &declares_class_or_enum
);
12043 /* Restore the saved message. */
12044 parser
->type_definition_forbidden_message
= saved_message
;
12046 cp_parser_maybe_commit_to_declaration (parser
,
12047 type_specifiers
.any_specifiers_p
);
12049 /* If all is well, we might be looking at a declaration. */
12050 if (!cp_parser_error_occurred (parser
))
12053 tree asm_specification
;
12055 cp_declarator
*declarator
;
12056 tree initializer
= NULL_TREE
;
12057 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12059 /* Parse the declarator. */
12060 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12061 CP_PARSER_FLAGS_NONE
,
12062 /*ctor_dtor_or_conv_p=*/NULL
,
12063 /*parenthesized_p=*/NULL
,
12064 /*member_p=*/false,
12065 /*friend_p=*/false,
12066 /*static_p=*/false);
12067 /* Parse the attributes. */
12068 attributes
= cp_parser_attributes_opt (parser
);
12069 /* Parse the asm-specification. */
12070 asm_specification
= cp_parser_asm_specification_opt (parser
);
12071 /* If the next token is not an `=' or '{', then we might still be
12072 looking at an expression. For example:
12076 looks like a decl-specifier-seq and a declarator -- but then
12077 there is no `=', so this is an expression. */
12078 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12079 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12080 cp_parser_simulate_error (parser
);
12082 /* If we did see an `=' or '{', then we are looking at a declaration
12084 if (cp_parser_parse_definitely (parser
))
12087 bool non_constant_p
= false;
12088 int flags
= LOOKUP_ONLYCONVERTING
;
12090 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12091 return error_mark_node
;
12093 /* Create the declaration. */
12094 decl
= start_decl (declarator
, &type_specifiers
,
12095 /*initialized_p=*/true,
12096 attributes
, /*prefix_attributes=*/NULL_TREE
,
12099 /* Parse the initializer. */
12100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12102 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12103 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12106 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12108 /* Consume the `='. */
12109 cp_lexer_consume_token (parser
->lexer
);
12110 initializer
= cp_parser_initializer_clause (parser
,
12115 cp_parser_error (parser
, "expected initializer");
12116 initializer
= error_mark_node
;
12118 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12119 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12121 /* Process the initializer. */
12122 cp_finish_decl (decl
,
12123 initializer
, !non_constant_p
,
12128 pop_scope (pushed_scope
);
12130 return convert_from_reference (decl
);
12133 /* If we didn't even get past the declarator successfully, we are
12134 definitely not looking at a declaration. */
12136 cp_parser_abort_tentative_parse (parser
);
12138 /* Otherwise, we are looking at an expression. */
12139 return cp_parser_expression (parser
);
12142 /* Parses a for-statement or range-for-statement until the closing ')',
12146 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12148 tree init
, scope
, decl
;
12151 /* Begin the for-statement. */
12152 scope
= begin_for_scope (&init
);
12154 /* Parse the initialization. */
12155 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12158 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12161 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12165 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12166 unsigned short unroll
)
12168 /* Normal for loop */
12169 tree condition
= NULL_TREE
;
12170 tree expression
= NULL_TREE
;
12173 stmt
= begin_for_stmt (scope
, init
);
12174 /* The init-statement has already been parsed in
12175 cp_parser_init_statement, so no work is needed here. */
12176 finish_init_stmt (stmt
);
12178 /* If there's a condition, process it. */
12179 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12180 condition
= cp_parser_condition (parser
);
12183 cp_parser_error (parser
, "missing loop condition in loop with "
12184 "%<GCC ivdep%> pragma");
12185 condition
= error_mark_node
;
12189 cp_parser_error (parser
, "missing loop condition in loop with "
12190 "%<GCC unroll%> pragma");
12191 condition
= error_mark_node
;
12193 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12194 /* Look for the `;'. */
12195 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12197 /* If there's an expression, process it. */
12198 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12199 expression
= cp_parser_expression (parser
);
12200 finish_for_expr (expression
, stmt
);
12205 /* Tries to parse a range-based for-statement:
12208 decl-specifier-seq declarator : expression
12210 The decl-specifier-seq declarator and the `:' are already parsed by
12211 cp_parser_init_statement. If processing_template_decl it returns a
12212 newly created RANGE_FOR_STMT; if not, it is converted to a
12213 regular FOR_STMT. */
12216 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12217 bool ivdep
, unsigned short unroll
, bool is_omp
)
12219 tree stmt
, range_expr
;
12220 auto_vec
<cxx_binding
*, 16> bindings
;
12221 auto_vec
<tree
, 16> names
;
12222 tree decomp_first_name
= NULL_TREE
;
12223 unsigned int decomp_cnt
= 0;
12225 /* Get the range declaration momentarily out of the way so that
12226 the range expression doesn't clash with it. */
12227 if (range_decl
!= error_mark_node
)
12229 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12231 tree v
= DECL_VALUE_EXPR (range_decl
);
12232 /* For decomposition declaration get all of the corresponding
12233 declarations out of the way. */
12234 if (TREE_CODE (v
) == ARRAY_REF
12235 && VAR_P (TREE_OPERAND (v
, 0))
12236 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12238 tree d
= range_decl
;
12239 range_decl
= TREE_OPERAND (v
, 0);
12240 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12241 decomp_first_name
= d
;
12242 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12244 tree name
= DECL_NAME (d
);
12245 names
.safe_push (name
);
12246 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12247 IDENTIFIER_BINDING (name
)
12248 = IDENTIFIER_BINDING (name
)->previous
;
12252 if (names
.is_empty ())
12254 tree name
= DECL_NAME (range_decl
);
12255 names
.safe_push (name
);
12256 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12257 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12263 bool expr_non_constant_p
;
12264 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12267 range_expr
= cp_parser_expression (parser
);
12269 /* Put the range declaration(s) back into scope. */
12270 for (unsigned int i
= 0; i
< names
.length (); i
++)
12272 cxx_binding
*binding
= bindings
[i
];
12273 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12274 IDENTIFIER_BINDING (names
[i
]) = binding
;
12277 /* finish_omp_for has its own code for the following, so just
12278 return the range_expr instead. */
12282 /* If in template, STMT is converted to a normal for-statement
12283 at instantiation. If not, it is done just ahead. */
12284 if (processing_template_decl
)
12286 if (check_for_bare_parameter_packs (range_expr
))
12287 range_expr
= error_mark_node
;
12288 stmt
= begin_range_for_stmt (scope
, init
);
12290 RANGE_FOR_IVDEP (stmt
) = 1;
12292 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12293 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12294 if (!type_dependent_expression_p (range_expr
)
12295 /* do_auto_deduction doesn't mess with template init-lists. */
12296 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12297 do_range_for_auto_deduction (range_decl
, range_expr
);
12301 stmt
= begin_for_stmt (scope
, init
);
12302 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12303 decomp_first_name
, decomp_cnt
, ivdep
,
12309 /* Subroutine of cp_convert_range_for: given the initializer expression,
12310 builds up the range temporary. */
12313 build_range_temp (tree range_expr
)
12315 tree range_type
, range_temp
;
12317 /* Find out the type deduced by the declaration
12318 `auto &&__range = range_expr'. */
12319 range_type
= cp_build_reference_type (make_auto (), true);
12320 range_type
= do_auto_deduction (range_type
, range_expr
,
12321 type_uses_auto (range_type
));
12323 /* Create the __range variable. */
12324 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12326 TREE_USED (range_temp
) = 1;
12327 DECL_ARTIFICIAL (range_temp
) = 1;
12332 /* Used by cp_parser_range_for in template context: we aren't going to
12333 do a full conversion yet, but we still need to resolve auto in the
12334 type of the for-range-declaration if present. This is basically
12335 a shortcut version of cp_convert_range_for. */
12338 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12340 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12343 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12344 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12345 iter_type
= (cp_parser_perform_range_for_lookup
12346 (range_temp
, &begin_dummy
, &end_dummy
));
12349 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12351 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12353 tf_warning_or_error
);
12354 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12355 iter_decl
, auto_node
);
12360 /* Converts a range-based for-statement into a normal
12361 for-statement, as per the definition.
12363 for (RANGE_DECL : RANGE_EXPR)
12366 should be equivalent to:
12369 auto &&__range = RANGE_EXPR;
12370 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12374 RANGE_DECL = *__begin;
12379 If RANGE_EXPR is an array:
12380 BEGIN_EXPR = __range
12381 END_EXPR = __range + ARRAY_SIZE(__range)
12382 Else if RANGE_EXPR has a member 'begin' or 'end':
12383 BEGIN_EXPR = __range.begin()
12384 END_EXPR = __range.end()
12386 BEGIN_EXPR = begin(__range)
12387 END_EXPR = end(__range);
12389 If __range has a member 'begin' but not 'end', or vice versa, we must
12390 still use the second alternative (it will surely fail, however).
12391 When calling begin()/end() in the third alternative we must use
12392 argument dependent lookup, but always considering 'std' as an associated
12396 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12397 tree decomp_first_name
, unsigned int decomp_cnt
,
12398 bool ivdep
, unsigned short unroll
)
12401 tree iter_type
, begin_expr
, end_expr
;
12402 tree condition
, expression
;
12404 range_expr
= mark_lvalue_use (range_expr
);
12406 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12407 /* If an error happened previously do nothing or else a lot of
12408 unhelpful errors would be issued. */
12409 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12414 if (VAR_P (range_expr
)
12415 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12416 /* Can't bind a reference to an array of runtime bound. */
12417 range_temp
= range_expr
;
12420 range_temp
= build_range_temp (range_expr
);
12421 pushdecl (range_temp
);
12422 cp_finish_decl (range_temp
, range_expr
,
12423 /*is_constant_init*/false, NULL_TREE
,
12424 LOOKUP_ONLYCONVERTING
);
12425 range_temp
= convert_from_reference (range_temp
);
12427 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12428 &begin_expr
, &end_expr
);
12431 /* The new for initialization statement. */
12432 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12434 TREE_USED (begin
) = 1;
12435 DECL_ARTIFICIAL (begin
) = 1;
12437 cp_finish_decl (begin
, begin_expr
,
12438 /*is_constant_init*/false, NULL_TREE
,
12439 LOOKUP_ONLYCONVERTING
);
12441 if (cxx_dialect
>= cxx17
)
12442 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12443 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12444 TREE_USED (end
) = 1;
12445 DECL_ARTIFICIAL (end
) = 1;
12447 cp_finish_decl (end
, end_expr
,
12448 /*is_constant_init*/false, NULL_TREE
,
12449 LOOKUP_ONLYCONVERTING
);
12451 finish_init_stmt (statement
);
12453 /* The new for condition. */
12454 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12457 NULL
, tf_warning_or_error
);
12458 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12460 /* The new increment expression. */
12461 expression
= finish_unary_op_expr (input_location
,
12462 PREINCREMENT_EXPR
, begin
,
12463 tf_warning_or_error
);
12464 finish_for_expr (expression
, statement
);
12466 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12467 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12469 /* The declaration is initialized with *__begin inside the loop body. */
12470 cp_finish_decl (range_decl
,
12471 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12472 tf_warning_or_error
),
12473 /*is_constant_init*/false, NULL_TREE
,
12474 LOOKUP_ONLYCONVERTING
);
12475 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12476 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12481 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12482 We need to solve both at the same time because the method used
12483 depends on the existence of members begin or end.
12484 Returns the type deduced for the iterator expression. */
12487 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12489 if (error_operand_p (range
))
12491 *begin
= *end
= error_mark_node
;
12492 return error_mark_node
;
12495 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12497 error ("range-based %<for%> expression of type %qT "
12498 "has incomplete type", TREE_TYPE (range
));
12499 *begin
= *end
= error_mark_node
;
12500 return error_mark_node
;
12502 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12504 /* If RANGE is an array, we will use pointer arithmetic. */
12505 *begin
= decay_conversion (range
, tf_warning_or_error
);
12506 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12508 array_type_nelts_top (TREE_TYPE (range
)),
12510 return TREE_TYPE (*begin
);
12514 /* If it is not an array, we must do a bit of magic. */
12515 tree id_begin
, id_end
;
12516 tree member_begin
, member_end
;
12518 *begin
= *end
= error_mark_node
;
12520 id_begin
= get_identifier ("begin");
12521 id_end
= get_identifier ("end");
12522 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12523 /*protect=*/2, /*want_type=*/false,
12524 tf_warning_or_error
);
12525 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12526 /*protect=*/2, /*want_type=*/false,
12527 tf_warning_or_error
);
12529 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12531 /* Use the member functions. */
12532 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12533 *end
= cp_parser_range_for_member_function (range
, id_end
);
12537 /* Use global functions with ADL. */
12538 vec
<tree
, va_gc
> *vec
;
12539 vec
= make_tree_vector ();
12541 vec_safe_push (vec
, range
);
12543 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12544 tf_warning_or_error
);
12545 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12546 tf_warning_or_error
);
12547 member_end
= perform_koenig_lookup (id_end
, vec
,
12548 tf_warning_or_error
);
12549 *end
= finish_call_expr (member_end
, &vec
, false, true,
12550 tf_warning_or_error
);
12552 release_tree_vector (vec
);
12555 /* Last common checks. */
12556 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12558 /* If one of the expressions is an error do no more checks. */
12559 *begin
= *end
= error_mark_node
;
12560 return error_mark_node
;
12562 else if (type_dependent_expression_p (*begin
)
12563 || type_dependent_expression_p (*end
))
12564 /* Can happen, when, eg, in a template context, Koenig lookup
12565 can't resolve begin/end (c++/58503). */
12569 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12570 /* The unqualified type of the __begin and __end temporaries should
12571 be the same, as required by the multiple auto declaration. */
12572 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12574 if (cxx_dialect
>= cxx17
12575 && (build_x_binary_op (input_location
, NE_EXPR
,
12576 *begin
, ERROR_MARK
,
12579 != error_mark_node
))
12580 /* P0184R0 allows __begin and __end to have different types,
12581 but make sure they are comparable so we can give a better
12584 error ("inconsistent begin/end types in range-based %<for%> "
12585 "statement: %qT and %qT",
12586 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12593 /* Helper function for cp_parser_perform_range_for_lookup.
12594 Builds a tree for RANGE.IDENTIFIER(). */
12597 cp_parser_range_for_member_function (tree range
, tree identifier
)
12600 vec
<tree
, va_gc
> *vec
;
12602 member
= finish_class_member_access_expr (range
, identifier
,
12603 false, tf_warning_or_error
);
12604 if (member
== error_mark_node
)
12605 return error_mark_node
;
12607 vec
= make_tree_vector ();
12608 res
= finish_call_expr (member
, &vec
,
12609 /*disallow_virtual=*/false,
12610 /*koenig_p=*/false,
12611 tf_warning_or_error
);
12612 release_tree_vector (vec
);
12616 /* Parse an iteration-statement.
12618 iteration-statement:
12619 while ( condition ) statement
12620 do statement while ( expression ) ;
12621 for ( init-statement condition [opt] ; expression [opt] )
12624 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12627 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12628 unsigned short unroll
)
12633 unsigned char in_statement
;
12634 token_indent_info guard_tinfo
;
12636 /* Peek at the next token. */
12637 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12639 return error_mark_node
;
12641 guard_tinfo
= get_token_indent_info (token
);
12643 /* Remember whether or not we are already within an iteration
12645 in_statement
= parser
->in_statement
;
12647 /* See what kind of keyword it is. */
12648 keyword
= token
->keyword
;
12655 /* Begin the while-statement. */
12656 statement
= begin_while_stmt ();
12657 /* Look for the `('. */
12658 matching_parens parens
;
12659 parens
.require_open (parser
);
12660 /* Parse the condition. */
12661 condition
= cp_parser_condition (parser
);
12662 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12663 /* Look for the `)'. */
12664 parens
.require_close (parser
);
12665 /* Parse the dependent statement. */
12666 parser
->in_statement
= IN_ITERATION_STMT
;
12667 bool prev
= note_iteration_stmt_body_start ();
12668 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12669 note_iteration_stmt_body_end (prev
);
12670 parser
->in_statement
= in_statement
;
12671 /* We're done with the while-statement. */
12672 finish_while_stmt (statement
);
12680 /* Begin the do-statement. */
12681 statement
= begin_do_stmt ();
12682 /* Parse the body of the do-statement. */
12683 parser
->in_statement
= IN_ITERATION_STMT
;
12684 bool prev
= note_iteration_stmt_body_start ();
12685 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12686 note_iteration_stmt_body_end (prev
);
12687 parser
->in_statement
= in_statement
;
12688 finish_do_body (statement
);
12689 /* Look for the `while' keyword. */
12690 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12691 /* Look for the `('. */
12692 matching_parens parens
;
12693 parens
.require_open (parser
);
12694 /* Parse the expression. */
12695 expression
= cp_parser_expression (parser
);
12696 /* We're done with the do-statement. */
12697 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12698 /* Look for the `)'. */
12699 parens
.require_close (parser
);
12700 /* Look for the `;'. */
12701 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12707 /* Look for the `('. */
12708 matching_parens parens
;
12709 parens
.require_open (parser
);
12711 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12713 /* Look for the `)'. */
12714 parens
.require_close (parser
);
12716 /* Parse the body of the for-statement. */
12717 parser
->in_statement
= IN_ITERATION_STMT
;
12718 bool prev
= note_iteration_stmt_body_start ();
12719 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12720 note_iteration_stmt_body_end (prev
);
12721 parser
->in_statement
= in_statement
;
12723 /* We're done with the for-statement. */
12724 finish_for_stmt (statement
);
12729 cp_parser_error (parser
, "expected iteration-statement");
12730 statement
= error_mark_node
;
12737 /* Parse a init-statement or the declarator of a range-based-for.
12738 Returns true if a range-based-for declaration is seen.
12741 expression-statement
12742 simple-declaration */
12745 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12747 /* If the next token is a `;', then we have an empty
12748 expression-statement. Grammatically, this is also a
12749 simple-declaration, but an invalid one, because it does not
12750 declare anything. Therefore, if we did not handle this case
12751 specially, we would issue an error message about an invalid
12753 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12755 bool is_range_for
= false;
12756 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12758 /* Try to parse the init-statement. */
12759 if (cp_parser_range_based_for_with_init_p (parser
))
12762 cp_parser_parse_tentatively (parser
);
12763 /* Parse the declaration. */
12764 cp_parser_simple_declaration (parser
,
12765 /*function_definition_allowed_p=*/false,
12767 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12768 if (!cp_parser_parse_definitely (parser
))
12769 /* That didn't work, try to parse it as an expression-statement. */
12770 cp_parser_expression_statement (parser
, NULL_TREE
);
12772 if (cxx_dialect
< cxx2a
)
12774 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12775 "range-based %<for%> loops with initializer only "
12776 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12777 *decl
= error_mark_node
;
12781 /* A colon is used in range-based for. */
12782 parser
->colon_corrects_to_scope_p
= false;
12784 /* We're going to speculatively look for a declaration, falling back
12785 to an expression, if necessary. */
12786 cp_parser_parse_tentatively (parser
);
12787 /* Parse the declaration. */
12788 cp_parser_simple_declaration (parser
,
12789 /*function_definition_allowed_p=*/false,
12791 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12794 /* It is a range-for, consume the ':'. */
12795 cp_lexer_consume_token (parser
->lexer
);
12796 is_range_for
= true;
12797 if (cxx_dialect
< cxx11
)
12798 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12799 "range-based %<for%> loops only available with "
12800 "%<-std=c++11%> or %<-std=gnu++11%>");
12803 /* The ';' is not consumed yet because we told
12804 cp_parser_simple_declaration not to. */
12805 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12807 if (cp_parser_parse_definitely (parser
))
12808 return is_range_for
;
12809 /* If the tentative parse failed, then we shall need to look for an
12810 expression-statement. */
12812 /* If we are here, it is an expression-statement. */
12813 cp_parser_expression_statement (parser
, NULL_TREE
);
12817 /* Parse a jump-statement.
12822 return expression [opt] ;
12823 return braced-init-list ;
12829 goto * expression ;
12831 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12834 cp_parser_jump_statement (cp_parser
* parser
)
12836 tree statement
= error_mark_node
;
12839 unsigned char in_statement
;
12841 /* Peek at the next token. */
12842 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12844 return error_mark_node
;
12846 /* See what kind of keyword it is. */
12847 keyword
= token
->keyword
;
12851 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12852 switch (in_statement
)
12855 error_at (token
->location
, "break statement not within loop or switch");
12858 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12859 || in_statement
== IN_ITERATION_STMT
);
12860 statement
= finish_break_stmt ();
12861 if (in_statement
== IN_ITERATION_STMT
)
12862 break_maybe_infinite_loop ();
12865 error_at (token
->location
, "invalid exit from OpenMP structured block");
12868 error_at (token
->location
, "break statement used with OpenMP for loop");
12871 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12875 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12878 error_at (token
->location
, "continue statement not within a loop");
12880 /* Fall through. */
12881 case IN_ITERATION_STMT
:
12883 statement
= finish_continue_stmt ();
12886 error_at (token
->location
, "invalid exit from OpenMP structured block");
12889 gcc_unreachable ();
12891 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12897 bool expr_non_constant_p
;
12899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12901 cp_lexer_set_source_position (parser
->lexer
);
12902 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12903 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12905 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12906 expr
= cp_parser_expression (parser
);
12908 /* If the next token is a `;', then there is no
12911 /* Build the return-statement. */
12912 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12913 /* Don't deduce from a discarded return statement. */;
12915 statement
= finish_return_stmt (expr
);
12916 /* Look for the final `;'. */
12917 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12922 if (parser
->in_function_body
12923 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12925 error ("%<goto%> in %<constexpr%> function");
12926 cp_function_chain
->invalid_constexpr
= true;
12929 /* Create the goto-statement. */
12930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12932 /* Issue a warning about this use of a GNU extension. */
12933 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12934 /* Consume the '*' token. */
12935 cp_lexer_consume_token (parser
->lexer
);
12936 /* Parse the dependent expression. */
12937 finish_goto_stmt (cp_parser_expression (parser
));
12940 finish_goto_stmt (cp_parser_identifier (parser
));
12941 /* Look for the final `;'. */
12942 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12946 cp_parser_error (parser
, "expected jump-statement");
12953 /* Parse a declaration-statement.
12955 declaration-statement:
12956 block-declaration */
12959 cp_parser_declaration_statement (cp_parser
* parser
)
12963 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12964 p
= obstack_alloc (&declarator_obstack
, 0);
12966 /* Parse the block-declaration. */
12967 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12969 /* Free any declarators allocated. */
12970 obstack_free (&declarator_obstack
, p
);
12973 /* Some dependent statements (like `if (cond) statement'), are
12974 implicitly in their own scope. In other words, if the statement is
12975 a single statement (as opposed to a compound-statement), it is
12976 none-the-less treated as if it were enclosed in braces. Any
12977 declarations appearing in the dependent statement are out of scope
12978 after control passes that point. This function parses a statement,
12979 but ensures that is in its own scope, even if it is not a
12980 compound-statement.
12982 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12983 is a (possibly labeled) if statement which is not enclosed in
12984 braces and has an else clause. This is used to implement
12987 CHAIN is a vector of if-else-if conditions. This is used to implement
12990 Returns the new statement. */
12993 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12994 const token_indent_info
&guard_tinfo
,
12998 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12999 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
13000 token_indent_info body_tinfo
13001 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13006 /* Mark if () ; with a special NOP_EXPR. */
13007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13009 cp_lexer_consume_token (parser
->lexer
);
13010 statement
= add_stmt (build_empty_stmt (body_loc
));
13012 if (guard_tinfo
.keyword
== RID_IF
13013 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
13014 warning_at (body_loc
, OPT_Wempty_body
,
13015 "suggest braces around empty body in an %<if%> statement");
13016 else if (guard_tinfo
.keyword
== RID_ELSE
)
13017 warning_at (body_loc
, OPT_Wempty_body
,
13018 "suggest braces around empty body in an %<else%> statement");
13020 /* if a compound is opened, we simply parse the statement directly. */
13021 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13022 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
13023 /* If the token is not a `{', then we must take special action. */
13026 /* Create a compound-statement. */
13027 statement
= begin_compound_stmt (0);
13028 /* Parse the dependent-statement. */
13029 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
13030 &body_loc_after_labels
);
13031 /* Finish the dummy compound-statement. */
13032 finish_compound_stmt (statement
);
13035 token_indent_info next_tinfo
13036 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13037 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13039 if (body_loc_after_labels
!= UNKNOWN_LOCATION
13040 && next_tinfo
.type
!= CPP_SEMICOLON
)
13041 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
13042 guard_tinfo
.location
, guard_tinfo
.keyword
);
13044 /* Return the statement. */
13048 /* For some dependent statements (like `while (cond) statement'), we
13049 have already created a scope. Therefore, even if the dependent
13050 statement is a compound-statement, we do not want to create another
13054 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
13055 const token_indent_info
&guard_tinfo
)
13057 /* If the token is a `{', then we must take special action. */
13058 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13060 token_indent_info body_tinfo
13061 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13062 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13064 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13065 &loc_after_labels
);
13066 token_indent_info next_tinfo
13067 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13068 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13070 if (loc_after_labels
!= UNKNOWN_LOCATION
13071 && next_tinfo
.type
!= CPP_SEMICOLON
)
13072 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13073 guard_tinfo
.location
,
13074 guard_tinfo
.keyword
);
13078 /* Avoid calling cp_parser_compound_statement, so that we
13079 don't create a new scope. Do everything else by hand. */
13080 matching_braces braces
;
13081 braces
.require_open (parser
);
13082 /* If the next keyword is `__label__' we have a label declaration. */
13083 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13084 cp_parser_label_declaration (parser
);
13085 /* Parse an (optional) statement-seq. */
13086 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13087 braces
.require_close (parser
);
13091 /* Declarations [gram.dcl.dcl] */
13093 /* Parse an optional declaration-sequence.
13097 declaration-seq declaration */
13100 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13104 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13106 if (token
->type
== CPP_CLOSE_BRACE
13107 || token
->type
== CPP_EOF
)
13110 cp_parser_toplevel_declaration (parser
);
13114 /* Parse a declaration.
13118 function-definition
13119 template-declaration
13120 explicit-instantiation
13121 explicit-specialization
13122 linkage-specification
13123 namespace-definition
13131 __extension__ declaration */
13134 cp_parser_declaration (cp_parser
* parser
)
13138 int saved_pedantic
;
13140 tree attributes
= NULL_TREE
;
13142 /* Check for the `__extension__' keyword. */
13143 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13145 /* Parse the qualified declaration. */
13146 cp_parser_declaration (parser
);
13147 /* Restore the PEDANTIC flag. */
13148 pedantic
= saved_pedantic
;
13153 /* Try to figure out what kind of declaration is present. */
13154 token1
= *cp_lexer_peek_token (parser
->lexer
);
13156 if (token1
.type
!= CPP_EOF
)
13157 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13160 token2
.type
= CPP_EOF
;
13161 token2
.keyword
= RID_MAX
;
13164 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13165 p
= obstack_alloc (&declarator_obstack
, 0);
13167 /* If the next token is `extern' and the following token is a string
13168 literal, then we have a linkage specification. */
13169 if (token1
.keyword
== RID_EXTERN
13170 && cp_parser_is_pure_string_literal (&token2
))
13171 cp_parser_linkage_specification (parser
);
13172 /* If the next token is `template', then we have either a template
13173 declaration, an explicit instantiation, or an explicit
13175 else if (token1
.keyword
== RID_TEMPLATE
)
13177 /* `template <>' indicates a template specialization. */
13178 if (token2
.type
== CPP_LESS
13179 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13180 cp_parser_explicit_specialization (parser
);
13181 /* `template <' indicates a template declaration. */
13182 else if (token2
.type
== CPP_LESS
)
13183 cp_parser_template_declaration (parser
, /*member_p=*/false);
13184 /* Anything else must be an explicit instantiation. */
13186 cp_parser_explicit_instantiation (parser
);
13188 /* If the next token is `export', then we have a template
13190 else if (token1
.keyword
== RID_EXPORT
)
13191 cp_parser_template_declaration (parser
, /*member_p=*/false);
13192 /* If the next token is `extern', 'static' or 'inline' and the one
13193 after that is `template', we have a GNU extended explicit
13194 instantiation directive. */
13195 else if (cp_parser_allow_gnu_extensions_p (parser
)
13196 && (token1
.keyword
== RID_EXTERN
13197 || token1
.keyword
== RID_STATIC
13198 || token1
.keyword
== RID_INLINE
)
13199 && token2
.keyword
== RID_TEMPLATE
)
13200 cp_parser_explicit_instantiation (parser
);
13201 /* If the next token is `namespace', check for a named or unnamed
13202 namespace definition. */
13203 else if (token1
.keyword
== RID_NAMESPACE
13204 && (/* A named namespace definition. */
13205 (token2
.type
== CPP_NAME
13206 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13208 || (token2
.type
== CPP_OPEN_SQUARE
13209 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13210 == CPP_OPEN_SQUARE
)
13211 /* An unnamed namespace definition. */
13212 || token2
.type
== CPP_OPEN_BRACE
13213 || token2
.keyword
== RID_ATTRIBUTE
))
13214 cp_parser_namespace_definition (parser
);
13215 /* An inline (associated) namespace definition. */
13216 else if (token1
.keyword
== RID_INLINE
13217 && token2
.keyword
== RID_NAMESPACE
)
13218 cp_parser_namespace_definition (parser
);
13219 /* Objective-C++ declaration/definition. */
13220 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13221 cp_parser_objc_declaration (parser
, NULL_TREE
);
13222 else if (c_dialect_objc ()
13223 && token1
.keyword
== RID_ATTRIBUTE
13224 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13225 cp_parser_objc_declaration (parser
, attributes
);
13226 /* At this point we may have a template declared by a concept
13228 else if (flag_concepts
13229 && cp_parser_template_declaration_after_export (parser
,
13230 /*member_p=*/false))
13233 /* Try to parse a block-declaration, or a function-definition. */
13234 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13236 /* Free any declarators allocated. */
13237 obstack_free (&declarator_obstack
, p
);
13240 /* Parse a namespace-scope declaration. */
13243 cp_parser_toplevel_declaration (cp_parser
* parser
)
13245 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13247 if (token
->type
== CPP_PRAGMA
)
13248 /* A top-level declaration can consist solely of a #pragma. A
13249 nested declaration cannot, so this is done here and not in
13250 cp_parser_declaration. (A #pragma at block scope is
13251 handled in cp_parser_statement.) */
13252 cp_parser_pragma (parser
, pragma_external
, NULL
);
13253 else if (token
->type
== CPP_SEMICOLON
)
13255 /* A declaration consisting of a single semicolon is
13256 invalid. Allow it unless we're being pedantic. */
13257 cp_lexer_consume_token (parser
->lexer
);
13258 if (!in_system_header_at (input_location
))
13259 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13262 /* Parse the declaration itself. */
13263 cp_parser_declaration (parser
);
13266 /* Parse a block-declaration.
13271 namespace-alias-definition
13278 __extension__ block-declaration
13283 static_assert-declaration
13285 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13286 part of a declaration-statement. */
13289 cp_parser_block_declaration (cp_parser
*parser
,
13293 int saved_pedantic
;
13295 /* Check for the `__extension__' keyword. */
13296 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13298 /* Parse the qualified declaration. */
13299 cp_parser_block_declaration (parser
, statement_p
);
13300 /* Restore the PEDANTIC flag. */
13301 pedantic
= saved_pedantic
;
13306 /* Peek at the next token to figure out which kind of declaration is
13308 token1
= cp_lexer_peek_token (parser
->lexer
);
13310 /* If the next keyword is `asm', we have an asm-definition. */
13311 if (token1
->keyword
== RID_ASM
)
13314 cp_parser_commit_to_tentative_parse (parser
);
13315 cp_parser_asm_definition (parser
);
13317 /* If the next keyword is `namespace', we have a
13318 namespace-alias-definition. */
13319 else if (token1
->keyword
== RID_NAMESPACE
)
13320 cp_parser_namespace_alias_definition (parser
);
13321 /* If the next keyword is `using', we have a
13322 using-declaration, a using-directive, or an alias-declaration. */
13323 else if (token1
->keyword
== RID_USING
)
13328 cp_parser_commit_to_tentative_parse (parser
);
13329 /* If the token after `using' is `namespace', then we have a
13330 using-directive. */
13331 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13332 if (token2
->keyword
== RID_NAMESPACE
)
13333 cp_parser_using_directive (parser
);
13334 /* If the second token after 'using' is '=', then we have an
13335 alias-declaration. */
13336 else if (cxx_dialect
>= cxx11
13337 && token2
->type
== CPP_NAME
13338 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13339 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13340 cp_parser_alias_declaration (parser
);
13341 /* Otherwise, it's a using-declaration. */
13343 cp_parser_using_declaration (parser
,
13344 /*access_declaration_p=*/false);
13346 /* If the next keyword is `__label__' we have a misplaced label
13348 else if (token1
->keyword
== RID_LABEL
)
13350 cp_lexer_consume_token (parser
->lexer
);
13351 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13352 cp_parser_skip_to_end_of_statement (parser
);
13353 /* If the next token is now a `;', consume it. */
13354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13355 cp_lexer_consume_token (parser
->lexer
);
13357 /* If the next token is `static_assert' we have a static assertion. */
13358 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13359 cp_parser_static_assert (parser
, /*member_p=*/false);
13360 /* Anything else must be a simple-declaration. */
13362 cp_parser_simple_declaration (parser
, !statement_p
,
13363 /*maybe_range_for_decl*/NULL
);
13366 /* Parse a simple-declaration.
13368 simple-declaration:
13369 decl-specifier-seq [opt] init-declarator-list [opt] ;
13370 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13371 brace-or-equal-initializer ;
13373 init-declarator-list:
13375 init-declarator-list , init-declarator
13377 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13378 function-definition as a simple-declaration.
13380 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13381 parsed declaration if it is an uninitialized single declarator not followed
13382 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13383 if present, will not be consumed. */
13386 cp_parser_simple_declaration (cp_parser
* parser
,
13387 bool function_definition_allowed_p
,
13388 tree
*maybe_range_for_decl
)
13390 cp_decl_specifier_seq decl_specifiers
;
13391 int declares_class_or_enum
;
13392 bool saw_declarator
;
13393 location_t comma_loc
= UNKNOWN_LOCATION
;
13394 location_t init_loc
= UNKNOWN_LOCATION
;
13396 if (maybe_range_for_decl
)
13397 *maybe_range_for_decl
= NULL_TREE
;
13399 /* Defer access checks until we know what is being declared; the
13400 checks for names appearing in the decl-specifier-seq should be
13401 done as if we were in the scope of the thing being declared. */
13402 push_deferring_access_checks (dk_deferred
);
13404 /* Parse the decl-specifier-seq. We have to keep track of whether
13405 or not the decl-specifier-seq declares a named class or
13406 enumeration type, since that is the only case in which the
13407 init-declarator-list is allowed to be empty.
13411 In a simple-declaration, the optional init-declarator-list can be
13412 omitted only when declaring a class or enumeration, that is when
13413 the decl-specifier-seq contains either a class-specifier, an
13414 elaborated-type-specifier, or an enum-specifier. */
13415 cp_parser_decl_specifier_seq (parser
,
13416 CP_PARSER_FLAGS_OPTIONAL
,
13418 &declares_class_or_enum
);
13419 /* We no longer need to defer access checks. */
13420 stop_deferring_access_checks ();
13422 /* In a block scope, a valid declaration must always have a
13423 decl-specifier-seq. By not trying to parse declarators, we can
13424 resolve the declaration/expression ambiguity more quickly. */
13425 if (!function_definition_allowed_p
13426 && !decl_specifiers
.any_specifiers_p
)
13428 cp_parser_error (parser
, "expected declaration");
13432 /* If the next two tokens are both identifiers, the code is
13433 erroneous. The usual cause of this situation is code like:
13437 where "T" should name a type -- but does not. */
13438 if (!decl_specifiers
.any_type_specifiers_p
13439 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13441 /* If parsing tentatively, we should commit; we really are
13442 looking at a declaration. */
13443 cp_parser_commit_to_tentative_parse (parser
);
13448 cp_parser_maybe_commit_to_declaration (parser
,
13449 decl_specifiers
.any_specifiers_p
);
13451 /* Look for C++17 decomposition declaration. */
13452 for (size_t n
= 1; ; n
++)
13453 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13454 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13456 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13457 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13458 && decl_specifiers
.any_specifiers_p
)
13461 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13462 maybe_range_for_decl
,
13465 /* The next token should be either a `,' or a `;'. */
13466 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13467 /* If it's a `;', we are done. */
13468 if (token
->type
== CPP_SEMICOLON
)
13470 else if (maybe_range_for_decl
)
13472 if (*maybe_range_for_decl
== NULL_TREE
)
13473 *maybe_range_for_decl
= error_mark_node
;
13476 /* Anything else is an error. */
13479 /* If we have already issued an error message we don't need
13480 to issue another one. */
13481 if ((decl
!= error_mark_node
13482 && DECL_INITIAL (decl
) != error_mark_node
)
13483 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13484 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13485 /* Skip tokens until we reach the end of the statement. */
13486 cp_parser_skip_to_end_of_statement (parser
);
13487 /* If the next token is now a `;', consume it. */
13488 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13489 cp_lexer_consume_token (parser
->lexer
);
13497 bool auto_specifier_p
;
13498 /* NULL_TREE if both variable and function declaration are allowed,
13499 error_mark_node if function declaration are not allowed and
13500 a FUNCTION_DECL that should be diagnosed if it is followed by
13501 variable declarations. */
13502 tree auto_function_declaration
;
13504 last_type
= NULL_TREE
;
13506 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13507 auto_function_declaration
= NULL_TREE
;
13509 /* Keep going until we hit the `;' at the end of the simple
13511 saw_declarator
= false;
13512 while (cp_lexer_next_token_is_not (parser
->lexer
,
13516 bool function_definition_p
;
13518 tree auto_result
= NULL_TREE
;
13520 if (saw_declarator
)
13522 /* If we are processing next declarator, comma is expected */
13523 token
= cp_lexer_peek_token (parser
->lexer
);
13524 gcc_assert (token
->type
== CPP_COMMA
);
13525 cp_lexer_consume_token (parser
->lexer
);
13526 if (maybe_range_for_decl
)
13528 *maybe_range_for_decl
= error_mark_node
;
13529 if (comma_loc
== UNKNOWN_LOCATION
)
13530 comma_loc
= token
->location
;
13534 saw_declarator
= true;
13536 /* Parse the init-declarator. */
13537 decl
= cp_parser_init_declarator (parser
,
13538 CP_PARSER_FLAGS_NONE
,
13541 function_definition_allowed_p
,
13542 /*member_p=*/false,
13543 declares_class_or_enum
,
13544 &function_definition_p
,
13545 maybe_range_for_decl
,
13548 /* If an error occurred while parsing tentatively, exit quickly.
13549 (That usually happens when in the body of a function; each
13550 statement is treated as a declaration-statement until proven
13552 if (cp_parser_error_occurred (parser
))
13555 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13557 /* If the init-declarator-list contains more than one
13558 init-declarator, they shall all form declarations of
13560 if (auto_function_declaration
== NULL_TREE
)
13561 auto_function_declaration
13562 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13563 else if (TREE_CODE (decl
) == FUNCTION_DECL
13564 || auto_function_declaration
!= error_mark_node
)
13566 error_at (decl_specifiers
.locations
[ds_type_spec
],
13567 "non-variable %qD in declaration with more than one "
13568 "declarator with placeholder type",
13569 TREE_CODE (decl
) == FUNCTION_DECL
13570 ? decl
: auto_function_declaration
);
13571 auto_function_declaration
= error_mark_node
;
13576 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13579 && last_type
!= error_mark_node
13580 && !same_type_p (auto_result
, last_type
))
13582 /* If the list of declarators contains more than one declarator,
13583 the type of each declared variable is determined as described
13584 above. If the type deduced for the template parameter U is not
13585 the same in each deduction, the program is ill-formed. */
13586 error_at (decl_specifiers
.locations
[ds_type_spec
],
13587 "inconsistent deduction for %qT: %qT and then %qT",
13588 decl_specifiers
.type
, last_type
, auto_result
);
13589 last_type
= error_mark_node
;
13592 last_type
= auto_result
;
13595 /* Handle function definitions specially. */
13596 if (function_definition_p
)
13598 /* If the next token is a `,', then we are probably
13599 processing something like:
13603 which is erroneous. */
13604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13606 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13607 error_at (token
->location
,
13609 " declarations and function-definitions is forbidden");
13611 /* Otherwise, we're done with the list of declarators. */
13614 pop_deferring_access_checks ();
13618 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13619 *maybe_range_for_decl
= decl
;
13620 /* The next token should be either a `,' or a `;'. */
13621 token
= cp_lexer_peek_token (parser
->lexer
);
13622 /* If it's a `,', there are more declarators to come. */
13623 if (token
->type
== CPP_COMMA
)
13624 /* will be consumed next time around */;
13625 /* If it's a `;', we are done. */
13626 else if (token
->type
== CPP_SEMICOLON
)
13628 else if (maybe_range_for_decl
)
13630 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13631 permerror (decl_specifiers
.locations
[ds_type_spec
],
13632 "types may not be defined in a for-range-declaration");
13635 /* Anything else is an error. */
13638 /* If we have already issued an error message we don't need
13639 to issue another one. */
13640 if ((decl
!= error_mark_node
13641 && DECL_INITIAL (decl
) != error_mark_node
)
13642 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13643 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13644 /* Skip tokens until we reach the end of the statement. */
13645 cp_parser_skip_to_end_of_statement (parser
);
13646 /* If the next token is now a `;', consume it. */
13647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13648 cp_lexer_consume_token (parser
->lexer
);
13651 /* After the first time around, a function-definition is not
13652 allowed -- even if it was OK at first. For example:
13657 function_definition_allowed_p
= false;
13660 /* Issue an error message if no declarators are present, and the
13661 decl-specifier-seq does not itself declare a class or
13662 enumeration: [dcl.dcl]/3. */
13663 if (!saw_declarator
)
13665 if (cp_parser_declares_only_class_p (parser
))
13667 if (!declares_class_or_enum
13668 && decl_specifiers
.type
13669 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13670 /* Ensure an error is issued anyway when finish_decltype_type,
13671 called via cp_parser_decl_specifier_seq, returns a class or
13672 an enumeration (c++/51786). */
13673 decl_specifiers
.type
= NULL_TREE
;
13674 shadow_tag (&decl_specifiers
);
13676 /* Perform any deferred access checks. */
13677 perform_deferred_access_checks (tf_warning_or_error
);
13680 /* Consume the `;'. */
13682 if (!maybe_range_for_decl
)
13683 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13684 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13686 if (init_loc
!= UNKNOWN_LOCATION
)
13687 error_at (init_loc
, "initializer in range-based %<for%> loop");
13688 if (comma_loc
!= UNKNOWN_LOCATION
)
13689 error_at (comma_loc
,
13690 "multiple declarations in range-based %<for%> loop");
13694 pop_deferring_access_checks ();
13697 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13698 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13702 cp_parser_decomposition_declaration (cp_parser
*parser
,
13703 cp_decl_specifier_seq
*decl_specifiers
,
13704 tree
*maybe_range_for_decl
,
13705 location_t
*init_loc
)
13707 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13708 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13709 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13711 /* Parse the identifier-list. */
13712 auto_vec
<cp_expr
, 10> v
;
13713 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13716 cp_expr e
= cp_parser_identifier (parser
);
13717 if (e
.get_value () == error_mark_node
)
13720 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13722 cp_lexer_consume_token (parser
->lexer
);
13725 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13726 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13728 end_loc
= UNKNOWN_LOCATION
;
13729 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13731 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13732 cp_lexer_consume_token (parser
->lexer
);
13735 cp_parser_skip_to_end_of_statement (parser
);
13736 return error_mark_node
;
13740 if (cxx_dialect
< cxx17
)
13741 pedwarn (loc
, 0, "structured bindings only available with "
13742 "%<-std=c++17%> or %<-std=gnu++17%>");
13745 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13746 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13747 declarator
->id_loc
= loc
;
13748 if (ref_qual
!= REF_QUAL_NONE
)
13749 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13750 ref_qual
== REF_QUAL_RVALUE
,
13752 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13753 NULL_TREE
, decl_specifiers
->attributes
,
13755 tree orig_decl
= decl
;
13759 cp_decl_specifier_seq decl_specs
;
13760 clear_decl_specs (&decl_specs
);
13761 decl_specs
.type
= make_auto ();
13763 FOR_EACH_VEC_ELT (v
, i
, e
)
13766 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13767 sfk_none
, e
.get_location ());
13770 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13771 declarator
->id_loc
= e
.get_location ();
13773 tree elt_pushed_scope
;
13774 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13775 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13776 if (decl2
== error_mark_node
)
13777 decl
= error_mark_node
;
13778 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13780 /* Ensure we've diagnosed redeclaration if we aren't creating
13782 gcc_assert (errorcount
);
13783 decl
= error_mark_node
;
13787 if (elt_pushed_scope
)
13788 pop_scope (elt_pushed_scope
);
13793 error_at (loc
, "empty structured binding declaration");
13794 decl
= error_mark_node
;
13797 if (maybe_range_for_decl
== NULL
13798 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13800 bool non_constant_p
= false, is_direct_init
= false;
13801 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13802 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13804 if (initializer
== NULL_TREE
13805 || (TREE_CODE (initializer
) == TREE_LIST
13806 && TREE_CHAIN (initializer
))
13808 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13809 && CONSTRUCTOR_NELTS (initializer
) != 1))
13811 error_at (loc
, "invalid initializer for structured binding "
13813 initializer
= error_mark_node
;
13816 if (decl
!= error_mark_node
)
13818 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13819 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13820 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13821 cp_finish_decomp (decl
, prev
, v
.length ());
13824 else if (decl
!= error_mark_node
)
13826 *maybe_range_for_decl
= prev
;
13827 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13828 the underlying DECL. */
13829 cp_finish_decomp (decl
, prev
, v
.length ());
13833 pop_scope (pushed_scope
);
13835 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13837 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13838 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13844 /* Parse a decl-specifier-seq.
13846 decl-specifier-seq:
13847 decl-specifier-seq [opt] decl-specifier
13848 decl-specifier attribute-specifier-seq [opt] (C++11)
13851 storage-class-specifier
13862 Concepts Extension:
13867 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13869 The parser flags FLAGS is used to control type-specifier parsing.
13871 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13874 1: one of the decl-specifiers is an elaborated-type-specifier
13875 (i.e., a type declaration)
13876 2: one of the decl-specifiers is an enum-specifier or a
13877 class-specifier (i.e., a type definition)
13882 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13883 cp_parser_flags flags
,
13884 cp_decl_specifier_seq
*decl_specs
,
13885 int* declares_class_or_enum
)
13887 bool constructor_possible_p
= !parser
->in_declarator_p
;
13888 bool found_decl_spec
= false;
13889 cp_token
*start_token
= NULL
;
13892 /* Clear DECL_SPECS. */
13893 clear_decl_specs (decl_specs
);
13895 /* Assume no class or enumeration type is declared. */
13896 *declares_class_or_enum
= 0;
13898 /* Keep reading specifiers until there are no more to read. */
13901 bool constructor_p
;
13905 /* Peek at the next token. */
13906 token
= cp_lexer_peek_token (parser
->lexer
);
13908 /* Save the first token of the decl spec list for error
13911 start_token
= token
;
13912 /* Handle attributes. */
13913 if (cp_next_tokens_can_be_attribute_p (parser
))
13915 /* Parse the attributes. */
13916 tree attrs
= cp_parser_attributes_opt (parser
);
13918 /* In a sequence of declaration specifiers, c++11 attributes
13919 appertain to the type that precede them. In that case
13922 The attribute-specifier-seq affects the type only for
13923 the declaration it appears in, not other declarations
13924 involving the same type.
13926 But for now let's force the user to position the
13927 attribute either at the beginning of the declaration or
13928 after the declarator-id, which would clearly mean that it
13929 applies to the declarator. */
13930 if (cxx11_attribute_p (attrs
))
13932 if (!found_decl_spec
)
13933 /* The c++11 attribute is at the beginning of the
13934 declaration. It appertains to the entity being
13938 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13940 /* This is an attribute following a
13941 class-specifier. */
13942 if (decl_specs
->type_definition_p
)
13943 warn_misplaced_attr_for_class_type (token
->location
,
13949 decl_specs
->std_attributes
13950 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13951 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13952 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13958 decl_specs
->attributes
13959 = attr_chainon (decl_specs
->attributes
, attrs
);
13960 if (decl_specs
->locations
[ds_attribute
] == 0)
13961 decl_specs
->locations
[ds_attribute
] = token
->location
;
13964 /* Assume we will find a decl-specifier keyword. */
13965 found_decl_spec
= true;
13966 /* If the next token is an appropriate keyword, we can simply
13967 add it to the list. */
13968 switch (token
->keyword
)
13974 if (!at_class_scope_p ())
13976 gcc_rich_location
richloc (token
->location
);
13977 richloc
.add_fixit_remove ();
13978 error_at (&richloc
, "%<friend%> used outside of class");
13979 cp_lexer_purge_token (parser
->lexer
);
13984 /* Consume the token. */
13985 cp_lexer_consume_token (parser
->lexer
);
13989 case RID_CONSTEXPR
:
13991 cp_lexer_consume_token (parser
->lexer
);
13996 cp_lexer_consume_token (parser
->lexer
);
13997 /* In C++20 a concept definition is just 'concept name = expr;'
13998 Support that syntax by pretending we've seen 'bool'. */
13999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
14000 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
14002 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
14003 token
, /*type_definition*/false);
14004 decl_specs
->any_type_specifiers_p
= true;
14008 /* function-specifier:
14015 cp_parser_function_specifier_opt (parser
, decl_specs
);
14022 /* Consume the token. */
14023 cp_lexer_consume_token (parser
->lexer
);
14024 /* A constructor declarator cannot appear in a typedef. */
14025 constructor_possible_p
= false;
14026 /* The "typedef" keyword can only occur in a declaration; we
14027 may as well commit at this point. */
14028 cp_parser_commit_to_tentative_parse (parser
);
14030 if (decl_specs
->storage_class
!= sc_none
)
14031 decl_specs
->conflicting_specifiers_p
= true;
14034 /* storage-class-specifier:
14044 if (cxx_dialect
== cxx98
)
14046 /* Consume the token. */
14047 cp_lexer_consume_token (parser
->lexer
);
14049 /* Complain about `auto' as a storage specifier, if
14050 we're complaining about C++0x compatibility. */
14051 gcc_rich_location
richloc (token
->location
);
14052 richloc
.add_fixit_remove ();
14053 warning_at (&richloc
, OPT_Wc__11_compat
,
14054 "%<auto%> changes meaning in C++11; "
14055 "please remove it");
14057 /* Set the storage class anyway. */
14058 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
14062 /* C++0x auto type-specifier. */
14063 found_decl_spec
= false;
14070 /* Consume the token. */
14071 cp_lexer_consume_token (parser
->lexer
);
14072 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14076 /* Consume the token. */
14078 cp_lexer_consume_token (parser
->lexer
);
14082 /* We did not yet find a decl-specifier yet. */
14083 found_decl_spec
= false;
14087 if (found_decl_spec
14088 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14089 && token
->keyword
!= RID_CONSTEXPR
)
14090 error ("decl-specifier invalid in condition");
14092 if (found_decl_spec
14093 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14094 && token
->keyword
!= RID_MUTABLE
14095 && token
->keyword
!= RID_CONSTEXPR
)
14096 error_at (token
->location
, "%qD invalid in lambda",
14097 ridpointers
[token
->keyword
]);
14100 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14102 /* Constructors are a special case. The `S' in `S()' is not a
14103 decl-specifier; it is the beginning of the declarator. */
14105 = (!found_decl_spec
14106 && constructor_possible_p
14107 && (cp_parser_constructor_declarator_p
14108 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
14111 /* If we don't have a DECL_SPEC yet, then we must be looking at
14112 a type-specifier. */
14113 if (!found_decl_spec
&& !constructor_p
)
14115 int decl_spec_declares_class_or_enum
;
14116 bool is_cv_qualifier
;
14120 = cp_parser_type_specifier (parser
, flags
,
14122 /*is_declaration=*/true,
14123 &decl_spec_declares_class_or_enum
,
14125 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14127 /* If this type-specifier referenced a user-defined type
14128 (a typedef, class-name, etc.), then we can't allow any
14129 more such type-specifiers henceforth.
14133 The longest sequence of decl-specifiers that could
14134 possibly be a type name is taken as the
14135 decl-specifier-seq of a declaration. The sequence shall
14136 be self-consistent as described below.
14140 As a general rule, at most one type-specifier is allowed
14141 in the complete decl-specifier-seq of a declaration. The
14142 only exceptions are the following:
14144 -- const or volatile can be combined with any other
14147 -- signed or unsigned can be combined with char, long,
14155 void g (const int Pc);
14157 Here, Pc is *not* part of the decl-specifier seq; it's
14158 the declarator. Therefore, once we see a type-specifier
14159 (other than a cv-qualifier), we forbid any additional
14160 user-defined types. We *do* still allow things like `int
14161 int' to be considered a decl-specifier-seq, and issue the
14162 error message later. */
14163 if (type_spec
&& !is_cv_qualifier
)
14164 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14165 /* A constructor declarator cannot follow a type-specifier. */
14168 constructor_possible_p
= false;
14169 found_decl_spec
= true;
14170 if (!is_cv_qualifier
)
14171 decl_specs
->any_type_specifiers_p
= true;
14173 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14174 error_at (token
->location
, "type-specifier invalid in lambda");
14178 /* If we still do not have a DECL_SPEC, then there are no more
14179 decl-specifiers. */
14180 if (!found_decl_spec
)
14183 decl_specs
->any_specifiers_p
= true;
14184 /* After we see one decl-specifier, further decl-specifiers are
14185 always optional. */
14186 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14189 /* Don't allow a friend specifier with a class definition. */
14190 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14191 && (*declares_class_or_enum
& 2))
14192 error_at (decl_specs
->locations
[ds_friend
],
14193 "class definition may not be declared a friend");
14196 /* Parse an (optional) storage-class-specifier.
14198 storage-class-specifier:
14207 storage-class-specifier:
14210 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14213 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14215 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14218 if (cxx_dialect
!= cxx98
)
14220 /* Fall through for C++98. */
14221 gcc_fallthrough ();
14228 /* Consume the token. */
14229 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14236 /* Parse an (optional) function-specifier.
14238 function-specifier:
14244 explicit(constant-expression)
14246 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14247 Updates DECL_SPECS, if it is non-NULL. */
14250 cp_parser_function_specifier_opt (cp_parser
* parser
,
14251 cp_decl_specifier_seq
*decl_specs
)
14253 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14254 switch (token
->keyword
)
14257 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14261 /* 14.5.2.3 [temp.mem]
14263 A member function template shall not be virtual. */
14264 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14265 && current_class_type
)
14266 error_at (token
->location
, "templates may not be %<virtual%>");
14268 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14273 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14274 /* If we see '(', it's C++20 explicit(bool). */
14276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14278 matching_parens parens
;
14279 parens
.consume_open (parser
);
14281 /* New types are not allowed in an explicit-specifier. */
14282 const char *saved_message
14283 = parser
->type_definition_forbidden_message
;
14284 parser
->type_definition_forbidden_message
14285 = G_("types may not be defined in explicit-specifier");
14287 if (cxx_dialect
< cxx2a
)
14288 pedwarn (token
->location
, 0,
14289 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14290 "or %<-std=gnu++2a%>");
14292 /* Parse the constant-expression. */
14293 expr
= cp_parser_constant_expression (parser
);
14295 /* Restore the saved message. */
14296 parser
->type_definition_forbidden_message
= saved_message
;
14297 parens
.require_close (parser
);
14300 /* The explicit-specifier explicit without a constant-expression is
14301 equivalent to the explicit-specifier explicit(true). */
14302 expr
= boolean_true_node
;
14305 "the constant-expression, if supplied, shall be a contextually
14306 converted constant expression of type bool." */
14307 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14308 /* We could evaluate it -- mark the decl as appropriate. */
14309 if (expr
== boolean_true_node
)
14310 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14311 else if (expr
== boolean_false_node
)
14312 /* Don't mark the decl as explicit. */;
14313 else if (decl_specs
)
14314 /* The expression was value-dependent. Remember it so that we can
14315 substitute it later. */
14316 decl_specs
->explicit_specifier
= expr
;
14324 /* Consume the token. */
14325 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14328 /* Parse a linkage-specification.
14330 linkage-specification:
14331 extern string-literal { declaration-seq [opt] }
14332 extern string-literal declaration */
14335 cp_parser_linkage_specification (cp_parser
* parser
)
14339 /* Look for the `extern' keyword. */
14340 cp_token
*extern_token
14341 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14343 /* Look for the string-literal. */
14344 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14345 linkage
= cp_parser_string_literal (parser
, false, false);
14347 /* Transform the literal into an identifier. If the literal is a
14348 wide-character string, or contains embedded NULs, then we can't
14349 handle it as the user wants. */
14350 if (strlen (TREE_STRING_POINTER (linkage
))
14351 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14353 cp_parser_error (parser
, "invalid linkage-specification");
14354 /* Assume C++ linkage. */
14355 linkage
= lang_name_cplusplus
;
14358 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14360 /* We're now using the new linkage. */
14361 push_lang_context (linkage
);
14363 /* Preserve the location of the the innermost linkage specification,
14364 tracking the locations of nested specifications via a local. */
14365 location_t saved_location
14366 = parser
->innermost_linkage_specification_location
;
14367 /* Construct a location ranging from the start of the "extern" to
14368 the end of the string-literal, with the caret at the start, e.g.:
14372 parser
->innermost_linkage_specification_location
14373 = make_location (extern_token
->location
,
14374 extern_token
->location
,
14375 get_finish (string_token
->location
));
14377 /* If the next token is a `{', then we're using the first
14379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14381 cp_ensure_no_omp_declare_simd (parser
);
14382 cp_ensure_no_oacc_routine (parser
);
14384 /* Consume the `{' token. */
14385 matching_braces braces
;
14386 braces
.consume_open (parser
);
14387 /* Parse the declarations. */
14388 cp_parser_declaration_seq_opt (parser
);
14389 /* Look for the closing `}'. */
14390 braces
.require_close (parser
);
14392 /* Otherwise, there's just one declaration. */
14395 bool saved_in_unbraced_linkage_specification_p
;
14397 saved_in_unbraced_linkage_specification_p
14398 = parser
->in_unbraced_linkage_specification_p
;
14399 parser
->in_unbraced_linkage_specification_p
= true;
14400 cp_parser_declaration (parser
);
14401 parser
->in_unbraced_linkage_specification_p
14402 = saved_in_unbraced_linkage_specification_p
;
14405 /* We're done with the linkage-specification. */
14406 pop_lang_context ();
14408 /* Restore location of parent linkage specification, if any. */
14409 parser
->innermost_linkage_specification_location
= saved_location
;
14412 /* Parse a static_assert-declaration.
14414 static_assert-declaration:
14415 static_assert ( constant-expression , string-literal ) ;
14416 static_assert ( constant-expression ) ; (C++17)
14418 If MEMBER_P, this static_assert is a class member. */
14421 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14424 location_t token_loc
;
14428 /* Peek at the `static_assert' token so we can keep track of exactly
14429 where the static assertion started. */
14430 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14432 /* Look for the `static_assert' keyword. */
14433 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14437 /* We know we are in a static assertion; commit to any tentative
14439 if (cp_parser_parsing_tentatively (parser
))
14440 cp_parser_commit_to_tentative_parse (parser
);
14442 /* Parse the `(' starting the static assertion condition. */
14443 matching_parens parens
;
14444 parens
.require_open (parser
);
14446 /* Parse the constant-expression. Allow a non-constant expression
14447 here in order to give better diagnostics in finish_static_assert. */
14449 cp_parser_constant_expression (parser
,
14450 /*allow_non_constant_p=*/true,
14451 /*non_constant_p=*/&dummy
);
14453 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14455 if (cxx_dialect
< cxx17
)
14456 pedwarn (input_location
, OPT_Wpedantic
,
14457 "static_assert without a message "
14458 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14460 cp_lexer_consume_token (parser
->lexer
);
14461 message
= build_string (1, "");
14462 TREE_TYPE (message
) = char_array_type_node
;
14463 fix_string_type (message
);
14467 /* Parse the separating `,'. */
14468 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14470 /* Parse the string-literal message. */
14471 message
= cp_parser_string_literal (parser
,
14472 /*translate=*/false,
14475 /* A `)' completes the static assertion. */
14476 if (!parens
.require_close (parser
))
14477 cp_parser_skip_to_closing_parenthesis (parser
,
14478 /*recovering=*/true,
14479 /*or_comma=*/false,
14480 /*consume_paren=*/true);
14483 /* A semicolon terminates the declaration. */
14484 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14486 /* Get the location for the static assertion. Use that of the
14487 condition if available, otherwise, use that of the "static_assert"
14489 location_t assert_loc
= condition
.get_location ();
14490 if (assert_loc
== UNKNOWN_LOCATION
)
14491 assert_loc
= token_loc
;
14493 /* Complete the static assertion, which may mean either processing
14494 the static assert now or saving it for template instantiation. */
14495 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14498 /* Parse the expression in decltype ( expression ). */
14501 cp_parser_decltype_expr (cp_parser
*parser
,
14502 bool &id_expression_or_member_access_p
)
14504 cp_token
*id_expr_start_token
;
14507 /* Since we're going to preserve any side-effects from this parse, set up a
14508 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14509 in the expression. */
14510 tentative_firewall
firewall (parser
);
14512 /* First, try parsing an id-expression. */
14513 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14514 cp_parser_parse_tentatively (parser
);
14515 expr
= cp_parser_id_expression (parser
,
14516 /*template_keyword_p=*/false,
14517 /*check_dependency_p=*/true,
14518 /*template_p=*/NULL
,
14519 /*declarator_p=*/false,
14520 /*optional_p=*/false);
14522 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14524 bool non_integral_constant_expression_p
= false;
14525 tree id_expression
= expr
;
14527 const char *error_msg
;
14529 if (identifier_p (expr
))
14530 /* Lookup the name we got back from the id-expression. */
14531 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14532 id_expr_start_token
->location
);
14534 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14535 /* A template without args is not a complete id-expression. */
14536 expr
= error_mark_node
;
14539 && expr
!= error_mark_node
14540 && TREE_CODE (expr
) != TYPE_DECL
14541 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14542 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14543 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14545 /* Complete lookup of the id-expression. */
14546 expr
= (finish_id_expression
14547 (id_expression
, expr
, parser
->scope
, &idk
,
14548 /*integral_constant_expression_p=*/false,
14549 /*allow_non_integral_constant_expression_p=*/true,
14550 &non_integral_constant_expression_p
,
14551 /*template_p=*/false,
14553 /*address_p=*/false,
14554 /*template_arg_p=*/false,
14556 id_expr_start_token
->location
));
14558 if (expr
== error_mark_node
)
14559 /* We found an id-expression, but it was something that we
14560 should not have found. This is an error, not something
14561 we can recover from, so note that we found an
14562 id-expression and we'll recover as gracefully as
14564 id_expression_or_member_access_p
= true;
14568 && expr
!= error_mark_node
14569 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14570 /* We have an id-expression. */
14571 id_expression_or_member_access_p
= true;
14574 if (!id_expression_or_member_access_p
)
14576 /* Abort the id-expression parse. */
14577 cp_parser_abort_tentative_parse (parser
);
14579 /* Parsing tentatively, again. */
14580 cp_parser_parse_tentatively (parser
);
14582 /* Parse a class member access. */
14583 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14584 /*cast_p=*/false, /*decltype*/true,
14585 /*member_access_only_p=*/true, NULL
);
14588 && expr
!= error_mark_node
14589 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14590 /* We have an id-expression. */
14591 id_expression_or_member_access_p
= true;
14594 if (id_expression_or_member_access_p
)
14595 /* We have parsed the complete id-expression or member access. */
14596 cp_parser_parse_definitely (parser
);
14599 /* Abort our attempt to parse an id-expression or member access
14601 cp_parser_abort_tentative_parse (parser
);
14603 /* Commit to the tentative_firewall so we get syntax errors. */
14604 cp_parser_commit_to_tentative_parse (parser
);
14606 /* Parse a full expression. */
14607 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14608 /*decltype_p=*/true);
14614 /* Parse a `decltype' type. Returns the type.
14616 simple-type-specifier:
14617 decltype ( expression )
14619 decltype ( auto ) */
14622 cp_parser_decltype (cp_parser
*parser
)
14624 bool id_expression_or_member_access_p
= false;
14625 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14627 if (start_token
->type
== CPP_DECLTYPE
)
14629 /* Already parsed. */
14630 cp_lexer_consume_token (parser
->lexer
);
14631 return saved_checks_value (start_token
->u
.tree_check_value
);
14634 /* Look for the `decltype' token. */
14635 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14636 return error_mark_node
;
14638 /* Parse the opening `('. */
14639 matching_parens parens
;
14640 if (!parens
.require_open (parser
))
14641 return error_mark_node
;
14643 push_deferring_access_checks (dk_deferred
);
14645 tree expr
= NULL_TREE
;
14647 if (cxx_dialect
>= cxx14
14648 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14649 /* decltype (auto) */
14650 cp_lexer_consume_token (parser
->lexer
);
14653 /* decltype (expression) */
14655 /* Types cannot be defined in a `decltype' expression. Save away the
14656 old message and set the new one. */
14657 const char *saved_message
= parser
->type_definition_forbidden_message
;
14658 parser
->type_definition_forbidden_message
14659 = G_("types may not be defined in %<decltype%> expressions");
14661 /* The restrictions on constant-expressions do not apply inside
14662 decltype expressions. */
14663 bool saved_integral_constant_expression_p
14664 = parser
->integral_constant_expression_p
;
14665 bool saved_non_integral_constant_expression_p
14666 = parser
->non_integral_constant_expression_p
;
14667 parser
->integral_constant_expression_p
= false;
14669 /* Within a parenthesized expression, a `>' token is always
14670 the greater-than operator. */
14671 bool saved_greater_than_is_operator_p
14672 = parser
->greater_than_is_operator_p
;
14673 parser
->greater_than_is_operator_p
= true;
14675 /* Do not actually evaluate the expression. */
14676 ++cp_unevaluated_operand
;
14678 /* Do not warn about problems with the expression. */
14679 ++c_inhibit_evaluation_warnings
;
14681 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14682 STRIP_ANY_LOCATION_WRAPPER (expr
);
14684 /* Go back to evaluating expressions. */
14685 --cp_unevaluated_operand
;
14686 --c_inhibit_evaluation_warnings
;
14688 /* The `>' token might be the end of a template-id or
14689 template-parameter-list now. */
14690 parser
->greater_than_is_operator_p
14691 = saved_greater_than_is_operator_p
;
14693 /* Restore the old message and the integral constant expression
14695 parser
->type_definition_forbidden_message
= saved_message
;
14696 parser
->integral_constant_expression_p
14697 = saved_integral_constant_expression_p
;
14698 parser
->non_integral_constant_expression_p
14699 = saved_non_integral_constant_expression_p
;
14702 /* Parse to the closing `)'. */
14703 if (!parens
.require_close (parser
))
14705 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14706 /*consume_paren=*/true);
14707 pop_deferring_access_checks ();
14708 return error_mark_node
;
14714 expr
= make_decltype_auto ();
14715 AUTO_IS_DECLTYPE (expr
) = true;
14718 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14719 tf_warning_or_error
);
14721 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14723 start_token
->type
= CPP_DECLTYPE
;
14724 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14725 start_token
->u
.tree_check_value
->value
= expr
;
14726 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14727 start_token
->keyword
= RID_MAX
;
14728 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14730 pop_to_parent_deferring_access_checks ();
14735 /* Special member functions [gram.special] */
14737 /* Parse a conversion-function-id.
14739 conversion-function-id:
14740 operator conversion-type-id
14742 Returns an IDENTIFIER_NODE representing the operator. */
14745 cp_parser_conversion_function_id (cp_parser
* parser
)
14749 tree saved_qualifying_scope
;
14750 tree saved_object_scope
;
14751 tree pushed_scope
= NULL_TREE
;
14753 /* Look for the `operator' token. */
14754 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14755 return error_mark_node
;
14756 /* When we parse the conversion-type-id, the current scope will be
14757 reset. However, we need that information in able to look up the
14758 conversion function later, so we save it here. */
14759 saved_scope
= parser
->scope
;
14760 saved_qualifying_scope
= parser
->qualifying_scope
;
14761 saved_object_scope
= parser
->object_scope
;
14762 /* We must enter the scope of the class so that the names of
14763 entities declared within the class are available in the
14764 conversion-type-id. For example, consider:
14771 S::operator I() { ... }
14773 In order to see that `I' is a type-name in the definition, we
14774 must be in the scope of `S'. */
14776 pushed_scope
= push_scope (saved_scope
);
14777 /* Parse the conversion-type-id. */
14778 type
= cp_parser_conversion_type_id (parser
);
14779 /* Leave the scope of the class, if any. */
14781 pop_scope (pushed_scope
);
14782 /* Restore the saved scope. */
14783 parser
->scope
= saved_scope
;
14784 parser
->qualifying_scope
= saved_qualifying_scope
;
14785 parser
->object_scope
= saved_object_scope
;
14786 /* If the TYPE is invalid, indicate failure. */
14787 if (type
== error_mark_node
)
14788 return error_mark_node
;
14789 return make_conv_op_name (type
);
14792 /* Parse a conversion-type-id:
14794 conversion-type-id:
14795 type-specifier-seq conversion-declarator [opt]
14797 Returns the TYPE specified. */
14800 cp_parser_conversion_type_id (cp_parser
* parser
)
14803 cp_decl_specifier_seq type_specifiers
;
14804 cp_declarator
*declarator
;
14805 tree type_specified
;
14806 const char *saved_message
;
14808 /* Parse the attributes. */
14809 attributes
= cp_parser_attributes_opt (parser
);
14811 saved_message
= parser
->type_definition_forbidden_message
;
14812 parser
->type_definition_forbidden_message
14813 = G_("types may not be defined in a conversion-type-id");
14815 /* Parse the type-specifiers. */
14816 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
14817 /*is_declaration=*/false,
14818 /*is_trailing_return=*/false,
14821 parser
->type_definition_forbidden_message
= saved_message
;
14823 /* If that didn't work, stop. */
14824 if (type_specifiers
.type
== error_mark_node
)
14825 return error_mark_node
;
14826 /* Parse the conversion-declarator. */
14827 declarator
= cp_parser_conversion_declarator_opt (parser
);
14829 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14830 /*initialized=*/0, &attributes
);
14832 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14834 /* Don't give this error when parsing tentatively. This happens to
14835 work because we always parse this definitively once. */
14836 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14837 && type_uses_auto (type_specified
))
14839 if (cxx_dialect
< cxx14
)
14841 error ("invalid use of %<auto%> in conversion operator");
14842 return error_mark_node
;
14844 else if (template_parm_scope_p ())
14845 warning (0, "use of %<auto%> in member template "
14846 "conversion operator can never be deduced");
14849 return type_specified
;
14852 /* Parse an (optional) conversion-declarator.
14854 conversion-declarator:
14855 ptr-operator conversion-declarator [opt]
14859 static cp_declarator
*
14860 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14862 enum tree_code code
;
14863 tree class_type
, std_attributes
= NULL_TREE
;
14864 cp_cv_quals cv_quals
;
14866 /* We don't know if there's a ptr-operator next, or not. */
14867 cp_parser_parse_tentatively (parser
);
14868 /* Try the ptr-operator. */
14869 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14871 /* If it worked, look for more conversion-declarators. */
14872 if (cp_parser_parse_definitely (parser
))
14874 cp_declarator
*declarator
;
14876 /* Parse another optional declarator. */
14877 declarator
= cp_parser_conversion_declarator_opt (parser
);
14879 declarator
= cp_parser_make_indirect_declarator
14880 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14888 /* Parse an (optional) ctor-initializer.
14891 : mem-initializer-list */
14894 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14896 /* If the next token is not a `:', then there is no
14897 ctor-initializer. */
14898 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14900 /* Do default initialization of any bases and members. */
14901 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14902 finish_mem_initializers (NULL_TREE
);
14906 /* Consume the `:' token. */
14907 cp_lexer_consume_token (parser
->lexer
);
14908 /* And the mem-initializer-list. */
14909 cp_parser_mem_initializer_list (parser
);
14912 /* Parse a mem-initializer-list.
14914 mem-initializer-list:
14915 mem-initializer ... [opt]
14916 mem-initializer ... [opt] , mem-initializer-list */
14919 cp_parser_mem_initializer_list (cp_parser
* parser
)
14921 tree mem_initializer_list
= NULL_TREE
;
14922 tree target_ctor
= error_mark_node
;
14923 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14925 /* Let the semantic analysis code know that we are starting the
14926 mem-initializer-list. */
14927 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14928 error_at (token
->location
,
14929 "only constructors take member initializers");
14931 /* Loop through the list. */
14934 tree mem_initializer
;
14936 token
= cp_lexer_peek_token (parser
->lexer
);
14937 /* Parse the mem-initializer. */
14938 mem_initializer
= cp_parser_mem_initializer (parser
);
14939 /* If the next token is a `...', we're expanding member initializers. */
14940 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14942 || (mem_initializer
!= error_mark_node
14943 && check_for_bare_parameter_packs (TREE_PURPOSE
14944 (mem_initializer
))))
14946 /* Consume the `...'. */
14948 cp_lexer_consume_token (parser
->lexer
);
14950 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14951 can be expanded but members cannot. */
14952 if (mem_initializer
!= error_mark_node
14953 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14955 error_at (token
->location
,
14956 "cannot expand initializer for member %qD",
14957 TREE_PURPOSE (mem_initializer
));
14958 mem_initializer
= error_mark_node
;
14961 /* Construct the pack expansion type. */
14962 if (mem_initializer
!= error_mark_node
)
14963 mem_initializer
= make_pack_expansion (mem_initializer
);
14965 if (target_ctor
!= error_mark_node
14966 && mem_initializer
!= error_mark_node
)
14968 error ("mem-initializer for %qD follows constructor delegation",
14969 TREE_PURPOSE (mem_initializer
));
14970 mem_initializer
= error_mark_node
;
14972 /* Look for a target constructor. */
14973 if (mem_initializer
!= error_mark_node
14974 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14975 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14977 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14978 if (mem_initializer_list
)
14980 error ("constructor delegation follows mem-initializer for %qD",
14981 TREE_PURPOSE (mem_initializer_list
));
14982 mem_initializer
= error_mark_node
;
14984 target_ctor
= mem_initializer
;
14986 /* Add it to the list, unless it was erroneous. */
14987 if (mem_initializer
!= error_mark_node
)
14989 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14990 mem_initializer_list
= mem_initializer
;
14992 /* If the next token is not a `,', we're done. */
14993 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14995 /* Consume the `,' token. */
14996 cp_lexer_consume_token (parser
->lexer
);
14999 /* Perform semantic analysis. */
15000 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15001 finish_mem_initializers (mem_initializer_list
);
15004 /* Parse a mem-initializer.
15007 mem-initializer-id ( expression-list [opt] )
15008 mem-initializer-id braced-init-list
15013 ( expression-list [opt] )
15015 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15016 class) or FIELD_DECL (for a non-static data member) to initialize;
15017 the TREE_VALUE is the expression-list. An empty initialization
15018 list is represented by void_list_node. */
15021 cp_parser_mem_initializer (cp_parser
* parser
)
15023 tree mem_initializer_id
;
15024 tree expression_list
;
15026 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15028 /* Find out what is being initialized. */
15029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15031 permerror (token
->location
,
15032 "anachronistic old-style base class initializer");
15033 mem_initializer_id
= NULL_TREE
;
15037 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
15038 if (mem_initializer_id
== error_mark_node
)
15039 return mem_initializer_id
;
15041 member
= expand_member_init (mem_initializer_id
);
15042 if (member
&& !DECL_P (member
))
15043 in_base_initializer
= 1;
15045 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15047 bool expr_non_constant_p
;
15048 cp_lexer_set_source_position (parser
->lexer
);
15049 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
15050 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
15051 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
15052 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
15056 vec
<tree
, va_gc
> *vec
;
15057 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
15059 /*allow_expansion_p=*/true,
15060 /*non_constant_p=*/NULL
,
15061 /*close_paren_loc=*/NULL
,
15062 /*wrap_locations_p=*/true);
15064 return error_mark_node
;
15065 expression_list
= build_tree_list_vec (vec
);
15066 release_tree_vector (vec
);
15069 if (expression_list
== error_mark_node
)
15070 return error_mark_node
;
15071 if (!expression_list
)
15072 expression_list
= void_type_node
;
15074 in_base_initializer
= 0;
15076 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
15079 /* Parse a mem-initializer-id.
15081 mem-initializer-id:
15082 :: [opt] nested-name-specifier [opt] class-name
15083 decltype-specifier (C++11)
15086 Returns a TYPE indicating the class to be initialized for the first
15087 production (and the second in C++11). Returns an IDENTIFIER_NODE
15088 indicating the data member to be initialized for the last production. */
15091 cp_parser_mem_initializer_id (cp_parser
* parser
)
15093 bool global_scope_p
;
15094 bool nested_name_specifier_p
;
15095 bool template_p
= false;
15098 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15100 /* `typename' is not allowed in this context ([temp.res]). */
15101 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15103 error_at (token
->location
,
15104 "keyword %<typename%> not allowed in this context (a qualified "
15105 "member initializer is implicitly a type)");
15106 cp_lexer_consume_token (parser
->lexer
);
15108 /* Look for the optional `::' operator. */
15110 = (cp_parser_global_scope_opt (parser
,
15111 /*current_scope_valid_p=*/false)
15113 /* Look for the optional nested-name-specifier. The simplest way to
15118 The keyword `typename' is not permitted in a base-specifier or
15119 mem-initializer; in these contexts a qualified name that
15120 depends on a template-parameter is implicitly assumed to be a
15123 is to assume that we have seen the `typename' keyword at this
15125 nested_name_specifier_p
15126 = (cp_parser_nested_name_specifier_opt (parser
,
15127 /*typename_keyword_p=*/true,
15128 /*check_dependency_p=*/true,
15130 /*is_declaration=*/true)
15132 if (nested_name_specifier_p
)
15133 template_p
= cp_parser_optional_template_keyword (parser
);
15134 /* If there is a `::' operator or a nested-name-specifier, then we
15135 are definitely looking for a class-name. */
15136 if (global_scope_p
|| nested_name_specifier_p
)
15137 return cp_parser_class_name (parser
,
15138 /*typename_keyword_p=*/true,
15139 /*template_keyword_p=*/template_p
,
15141 /*check_dependency_p=*/true,
15142 /*class_head_p=*/false,
15143 /*is_declaration=*/true);
15144 /* Otherwise, we could also be looking for an ordinary identifier. */
15145 cp_parser_parse_tentatively (parser
);
15146 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15147 /* Try a decltype-specifier. */
15148 id
= cp_parser_decltype (parser
);
15150 /* Otherwise, try a class-name. */
15151 id
= cp_parser_class_name (parser
,
15152 /*typename_keyword_p=*/true,
15153 /*template_keyword_p=*/false,
15155 /*check_dependency_p=*/true,
15156 /*class_head_p=*/false,
15157 /*is_declaration=*/true);
15158 /* If we found one, we're done. */
15159 if (cp_parser_parse_definitely (parser
))
15161 /* Otherwise, look for an ordinary identifier. */
15162 return cp_parser_identifier (parser
);
15165 /* Overloading [gram.over] */
15167 /* Parse an operator-function-id.
15169 operator-function-id:
15172 Returns an IDENTIFIER_NODE for the operator which is a
15173 human-readable spelling of the identifier, e.g., `operator +'. */
15176 cp_parser_operator_function_id (cp_parser
* parser
)
15178 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15179 /* Look for the `operator' keyword. */
15180 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15181 return error_mark_node
;
15182 /* And then the name of the operator itself. */
15183 return cp_parser_operator (parser
, start_loc
);
15186 /* Return an identifier node for a user-defined literal operator.
15187 The suffix identifier is chained to the operator name identifier. */
15190 cp_literal_operator_id (const char* name
)
15193 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15194 + strlen (name
) + 10);
15195 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15196 identifier
= get_identifier (buffer
);
15201 /* Parse an operator.
15204 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15205 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15206 || ++ -- , ->* -> () []
15213 Returns an IDENTIFIER_NODE for the operator which is a
15214 human-readable spelling of the identifier, e.g., `operator +'. */
15217 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15219 tree id
= NULL_TREE
;
15223 /* Peek at the next token. */
15224 token
= cp_lexer_peek_token (parser
->lexer
);
15226 location_t end_loc
= token
->location
;
15228 /* Figure out which operator we have. */
15229 enum tree_code op
= ERROR_MARK
;
15230 bool assop
= false;
15231 bool consumed
= false;
15232 switch (token
->type
)
15236 /* The keyword should be either `new' or `delete'. */
15237 if (token
->keyword
== RID_NEW
)
15239 else if (token
->keyword
== RID_DELETE
)
15244 /* Consume the `new' or `delete' token. */
15245 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15247 /* Peek at the next token. */
15248 token
= cp_lexer_peek_token (parser
->lexer
);
15249 /* If it's a `[' token then this is the array variant of the
15251 if (token
->type
== CPP_OPEN_SQUARE
)
15253 /* Consume the `[' token. */
15254 cp_lexer_consume_token (parser
->lexer
);
15255 /* Look for the `]' token. */
15256 if (cp_token
*close_token
15257 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15258 end_loc
= close_token
->location
;
15259 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15278 op
= TRUNC_DIV_EXPR
;
15282 op
= TRUNC_MOD_EXPR
;
15302 op
= TRUTH_NOT_EXPR
;
15335 op
= TRUNC_DIV_EXPR
;
15340 op
= TRUNC_MOD_EXPR
;
15366 case CPP_LSHIFT_EQ
:
15371 case CPP_RSHIFT_EQ
:
15388 case CPP_GREATER_EQ
:
15393 op
= TRUTH_ANDIF_EXPR
;
15397 op
= TRUTH_ORIF_EXPR
;
15400 case CPP_PLUS_PLUS
:
15401 op
= POSTINCREMENT_EXPR
;
15404 case CPP_MINUS_MINUS
:
15405 op
= PREDECREMENT_EXPR
;
15409 op
= COMPOUND_EXPR
;
15412 case CPP_DEREF_STAR
:
15417 op
= COMPONENT_REF
;
15420 case CPP_OPEN_PAREN
:
15422 /* Consume the `('. */
15423 matching_parens parens
;
15424 parens
.consume_open (parser
);
15425 /* Look for the matching `)'. */
15426 token
= parens
.require_close (parser
);
15428 end_loc
= token
->location
;
15434 case CPP_OPEN_SQUARE
:
15435 /* Consume the `['. */
15436 cp_lexer_consume_token (parser
->lexer
);
15437 /* Look for the matching `]'. */
15438 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15440 end_loc
= token
->location
;
15445 case CPP_UTF8STRING
:
15446 case CPP_UTF8STRING_USERDEF
:
15453 case CPP_STRING_USERDEF
:
15454 case CPP_WSTRING_USERDEF
:
15455 case CPP_STRING16_USERDEF
:
15456 case CPP_STRING32_USERDEF
:
15462 if (cxx_dialect
== cxx98
)
15463 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15465 /* Consume the string. */
15466 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15467 /*wide_ok=*/true, /*lookup_udlit=*/false);
15468 if (str
== error_mark_node
)
15469 return error_mark_node
;
15470 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15472 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15473 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15474 end_loc
= str
.get_location ();
15479 /* Look for the suffix identifier. */
15480 token
= cp_lexer_peek_token (parser
->lexer
);
15481 if (token
->type
== CPP_NAME
)
15483 id
= cp_parser_identifier (parser
);
15484 end_loc
= token
->location
;
15486 else if (token
->type
== CPP_KEYWORD
)
15488 error ("unexpected keyword;"
15489 " remove space between quotes and suffix identifier");
15490 return error_mark_node
;
15494 error ("expected suffix identifier");
15495 return error_mark_node
;
15498 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15499 (TREE_TYPE (TREE_TYPE (string_tree
))));
15500 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15503 error ("expected empty string after %<operator%> keyword");
15504 return error_mark_node
;
15506 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15509 error ("invalid encoding prefix in literal operator");
15510 return error_mark_node
;
15512 if (id
!= error_mark_node
)
15514 const char *name
= IDENTIFIER_POINTER (id
);
15515 id
= cp_literal_operator_id (name
);
15517 /* Generate a location of the form:
15518 "" _suffix_identifier
15519 ^~~~~~~~~~~~~~~~~~~~~
15520 with caret == start at the start token, finish at the end of the
15521 suffix identifier. */
15522 location_t finish_loc
15523 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15524 location_t combined_loc
15525 = make_location (start_loc
, start_loc
, finish_loc
);
15526 return cp_expr (id
, combined_loc
);
15530 /* Anything else is an error. */
15534 /* If we have selected an identifier, we need to consume the
15536 if (op
!= ERROR_MARK
)
15538 id
= ovl_op_identifier (assop
, op
);
15540 cp_lexer_consume_token (parser
->lexer
);
15542 /* Otherwise, no valid operator name was present. */
15545 cp_parser_error (parser
, "expected operator");
15546 id
= error_mark_node
;
15549 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15550 return cp_expr (id
, start_loc
);
15553 /* Parse a template-declaration.
15555 template-declaration:
15556 export [opt] template < template-parameter-list > declaration
15558 If MEMBER_P is TRUE, this template-declaration occurs within a
15561 The grammar rule given by the standard isn't correct. What
15562 is really meant is:
15564 template-declaration:
15565 export [opt] template-parameter-list-seq
15566 decl-specifier-seq [opt] init-declarator [opt] ;
15567 export [opt] template-parameter-list-seq
15568 function-definition
15570 template-parameter-list-seq:
15571 template-parameter-list-seq [opt]
15572 template < template-parameter-list >
15574 Concept Extensions:
15576 template-parameter-list-seq:
15577 template < template-parameter-list > requires-clause [opt]
15580 requires logical-or-expression */
15583 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15585 /* Check for `export'. */
15586 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15588 /* Consume the `export' token. */
15589 cp_lexer_consume_token (parser
->lexer
);
15590 /* Warn that we do not support `export'. */
15591 warning (0, "keyword %<export%> not implemented, and will be ignored");
15594 cp_parser_template_declaration_after_export (parser
, member_p
);
15597 /* Parse a template-parameter-list.
15599 template-parameter-list:
15601 template-parameter-list , template-parameter
15603 Returns a TREE_LIST. Each node represents a template parameter.
15604 The nodes are connected via their TREE_CHAINs. */
15607 cp_parser_template_parameter_list (cp_parser
* parser
)
15609 tree parameter_list
= NULL_TREE
;
15611 /* Don't create wrapper nodes within a template-parameter-list,
15612 since we don't want to have different types based on the
15613 spelling location of constants and decls within them. */
15614 auto_suppress_location_wrappers sentinel
;
15616 begin_template_parm_list ();
15618 /* The loop below parses the template parms. We first need to know
15619 the total number of template parms to be able to compute proper
15620 canonical types of each dependent type. So after the loop, when
15621 we know the total number of template parms,
15622 end_template_parm_list computes the proper canonical types and
15623 fixes up the dependent types accordingly. */
15628 bool is_parameter_pack
;
15629 location_t parm_loc
;
15631 /* Parse the template-parameter. */
15632 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15633 parameter
= cp_parser_template_parameter (parser
,
15635 &is_parameter_pack
);
15636 /* Add it to the list. */
15637 if (parameter
!= error_mark_node
)
15638 parameter_list
= process_template_parm (parameter_list
,
15642 is_parameter_pack
);
15645 tree err_parm
= build_tree_list (parameter
, parameter
);
15646 parameter_list
= chainon (parameter_list
, err_parm
);
15649 /* If the next token is not a `,', we're done. */
15650 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15652 /* Otherwise, consume the `,' token. */
15653 cp_lexer_consume_token (parser
->lexer
);
15656 return end_template_parm_list (parameter_list
);
15659 /* Parse a introduction-list.
15662 introduced-parameter
15663 introduction-list , introduced-parameter
15665 introduced-parameter:
15666 ...[opt] identifier
15668 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15669 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15670 WILDCARD_DECL will also have DECL_NAME set and token location in
15671 DECL_SOURCE_LOCATION. */
15674 cp_parser_introduction_list (cp_parser
*parser
)
15676 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15680 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15682 cp_lexer_consume_token (parser
->lexer
);
15684 tree identifier
= cp_parser_identifier (parser
);
15685 if (identifier
== error_mark_node
)
15688 /* Build placeholder. */
15689 tree parm
= build_nt (WILDCARD_DECL
);
15690 DECL_SOURCE_LOCATION (parm
)
15691 = cp_lexer_peek_token (parser
->lexer
)->location
;
15692 DECL_NAME (parm
) = identifier
;
15693 WILDCARD_PACK_P (parm
) = is_pack
;
15694 vec_safe_push (introduction_vec
, parm
);
15696 /* If the next token is not a `,', we're done. */
15697 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15699 /* Otherwise, consume the `,' token. */
15700 cp_lexer_consume_token (parser
->lexer
);
15703 /* Convert the vec into a TREE_VEC. */
15704 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15707 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15708 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15710 release_tree_vector (introduction_vec
);
15711 return introduction_list
;
15714 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15715 is an abstract declarator. */
15717 static inline cp_declarator
*
15718 get_id_declarator (cp_declarator
*declarator
)
15720 cp_declarator
*d
= declarator
;
15721 while (d
&& d
->kind
!= cdk_id
)
15726 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15727 is an abstract declarator. */
15730 get_unqualified_id (cp_declarator
*declarator
)
15732 declarator
= get_id_declarator (declarator
);
15734 return declarator
->u
.id
.unqualified_name
;
15739 /* Returns true if DECL represents a constrained-parameter. */
15742 is_constrained_parameter (tree decl
)
15745 && TREE_CODE (decl
) == TYPE_DECL
15746 && CONSTRAINED_PARM_CONCEPT (decl
)
15747 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15750 /* Returns true if PARM declares a constrained-parameter. */
15753 is_constrained_parameter (cp_parameter_declarator
*parm
)
15755 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15758 /* Check that the type parameter is only a declarator-id, and that its
15759 type is not cv-qualified. */
15762 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15763 cp_parameter_declarator
*parm
)
15765 if (!parm
->declarator
)
15768 if (parm
->declarator
->kind
!= cdk_id
)
15770 cp_parser_error (parser
, "invalid constrained type parameter");
15774 /* Don't allow cv-qualified type parameters. */
15775 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15776 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15778 cp_parser_error (parser
, "cv-qualified type parameter");
15785 /* Finish parsing/processing a template type parameter and checking
15786 various restrictions. */
15789 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15791 cp_parameter_declarator
* parmdecl
)
15793 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15794 return finish_template_type_parm (class_type_node
, id
);
15796 return error_mark_node
;
15800 finish_constrained_template_template_parm (tree proto
, tree id
)
15802 /* FIXME: This should probably be copied, and we may need to adjust
15803 the template parameter depths. */
15804 tree saved_parms
= current_template_parms
;
15805 begin_template_parm_list ();
15806 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15807 end_template_parm_list ();
15809 tree parm
= finish_template_template_parm (class_type_node
, id
);
15810 current_template_parms
= saved_parms
;
15815 /* Finish parsing/processing a template template parameter by borrowing
15816 the template parameter list from the prototype parameter. */
15819 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15822 cp_parameter_declarator
*parmdecl
)
15824 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15825 return error_mark_node
;
15826 return finish_constrained_template_template_parm (proto
, id
);
15829 /* Create a new non-type template parameter from the given PARM
15833 constrained_non_type_template_parm (bool *is_non_type
,
15834 cp_parameter_declarator
*parm
)
15836 *is_non_type
= true;
15837 cp_declarator
*decl
= parm
->declarator
;
15838 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15839 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15840 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15843 /* Build a constrained template parameter based on the PARMDECL
15844 declarator. The type of PARMDECL is the constrained type, which
15845 refers to the prototype template parameter that ultimately
15846 specifies the type of the declared parameter. */
15849 finish_constrained_parameter (cp_parser
*parser
,
15850 cp_parameter_declarator
*parmdecl
,
15852 bool *is_parameter_pack
)
15854 tree decl
= parmdecl
->decl_specifiers
.type
;
15855 tree id
= get_unqualified_id (parmdecl
->declarator
);
15856 tree def
= parmdecl
->default_argument
;
15857 tree proto
= DECL_INITIAL (decl
);
15859 /* A template parameter constrained by a variadic concept shall also
15860 be declared as a template parameter pack. */
15861 bool is_variadic
= template_parameter_pack_p (proto
);
15862 if (is_variadic
&& !*is_parameter_pack
)
15863 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15865 /* Build the parameter. Return an error if the declarator was invalid. */
15867 if (TREE_CODE (proto
) == TYPE_DECL
)
15868 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15869 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15870 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15873 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15874 if (parm
== error_mark_node
)
15875 return error_mark_node
;
15877 /* Finish the parameter decl and create a node attaching the
15878 default argument and constraint. */
15879 parm
= build_tree_list (def
, parm
);
15880 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15885 /* Returns true if the parsed type actually represents the declaration
15886 of a type template-parameter. */
15889 declares_constrained_type_template_parameter (tree type
)
15891 return (is_constrained_parameter (type
)
15892 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15896 /* Returns true if the parsed type actually represents the declaration of
15897 a template template-parameter. */
15900 declares_constrained_template_template_parameter (tree type
)
15902 return (is_constrained_parameter (type
)
15903 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15906 /* Parse a default argument for a type template-parameter.
15907 Note that diagnostics are handled in cp_parser_template_parameter. */
15910 cp_parser_default_type_template_argument (cp_parser
*parser
)
15912 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15914 /* Consume the `=' token. */
15915 cp_lexer_consume_token (parser
->lexer
);
15917 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15919 /* Parse the default-argument. */
15920 push_deferring_access_checks (dk_no_deferred
);
15921 tree default_argument
= cp_parser_type_id (parser
,
15922 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15924 pop_deferring_access_checks ();
15926 if (flag_concepts
&& type_uses_auto (default_argument
))
15928 error_at (token
->location
,
15929 "invalid use of %<auto%> in default template argument");
15930 return error_mark_node
;
15933 return default_argument
;
15936 /* Parse a default argument for a template template-parameter. */
15939 cp_parser_default_template_template_argument (cp_parser
*parser
)
15941 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15945 /* Consume the `='. */
15946 cp_lexer_consume_token (parser
->lexer
);
15947 /* Parse the id-expression. */
15948 push_deferring_access_checks (dk_no_deferred
);
15949 /* save token before parsing the id-expression, for error
15951 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15952 tree default_argument
15953 = cp_parser_id_expression (parser
,
15954 /*template_keyword_p=*/false,
15955 /*check_dependency_p=*/true,
15956 /*template_p=*/&is_template
,
15957 /*declarator_p=*/false,
15958 /*optional_p=*/false);
15959 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15960 /* If the id-expression was a template-id that refers to
15961 a template-class, we already have the declaration here,
15962 so no further lookup is needed. */
15965 /* Look up the name. */
15967 = cp_parser_lookup_name (parser
, default_argument
,
15969 /*is_template=*/is_template
,
15970 /*is_namespace=*/false,
15971 /*check_dependency=*/true,
15972 /*ambiguous_decls=*/NULL
,
15974 /* See if the default argument is valid. */
15975 default_argument
= check_template_template_default_arg (default_argument
);
15976 pop_deferring_access_checks ();
15977 return default_argument
;
15980 /* Parse a template-parameter.
15982 template-parameter:
15984 parameter-declaration
15986 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15987 the parameter. The TREE_PURPOSE is the default value, if any.
15988 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15989 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15990 set to true iff this parameter is a parameter pack. */
15993 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15994 bool *is_parameter_pack
)
15997 cp_parameter_declarator
*parameter_declarator
;
16000 /* Assume it is a type parameter or a template parameter. */
16001 *is_non_type
= false;
16002 /* Assume it not a parameter pack. */
16003 *is_parameter_pack
= false;
16004 /* Peek at the next token. */
16005 token
= cp_lexer_peek_token (parser
->lexer
);
16006 /* If it is `template', we have a type-parameter. */
16007 if (token
->keyword
== RID_TEMPLATE
)
16008 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16009 /* If it is `class' or `typename' we do not know yet whether it is a
16010 type parameter or a non-type parameter. Consider:
16012 template <typename T, typename T::X X> ...
16016 template <class C, class D*> ...
16018 Here, the first parameter is a type parameter, and the second is
16019 a non-type parameter. We can tell by looking at the token after
16020 the identifier -- if it is a `,', `=', or `>' then we have a type
16022 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
16024 /* Peek at the token after `class' or `typename'. */
16025 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16026 /* If it's an ellipsis, we have a template type parameter
16028 if (token
->type
== CPP_ELLIPSIS
)
16029 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16030 /* If it's an identifier, skip it. */
16031 if (token
->type
== CPP_NAME
)
16032 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
16033 /* Now, see if the token looks like the end of a template
16035 if (token
->type
== CPP_COMMA
16036 || token
->type
== CPP_EQ
16037 || token
->type
== CPP_GREATER
)
16038 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16041 /* Otherwise, it is a non-type parameter or a constrained parameter.
16045 When parsing a default template-argument for a non-type
16046 template-parameter, the first non-nested `>' is taken as the end
16047 of the template parameter-list rather than a greater-than
16049 parameter_declarator
16050 = cp_parser_parameter_declaration (parser
,
16051 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16052 /*template_parm_p=*/true,
16053 /*parenthesized_p=*/NULL
);
16055 if (!parameter_declarator
)
16056 return error_mark_node
;
16058 /* If the parameter declaration is marked as a parameter pack, set
16059 *IS_PARAMETER_PACK to notify the caller. */
16060 if (parameter_declarator
->template_parameter_pack_p
)
16061 *is_parameter_pack
= true;
16063 if (parameter_declarator
->default_argument
)
16065 /* Can happen in some cases of erroneous input (c++/34892). */
16066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16067 /* Consume the `...' for better error recovery. */
16068 cp_lexer_consume_token (parser
->lexer
);
16071 // The parameter may have been constrained.
16072 if (is_constrained_parameter (parameter_declarator
))
16073 return finish_constrained_parameter (parser
,
16074 parameter_declarator
,
16076 is_parameter_pack
);
16078 // Now we're sure that the parameter is a non-type parameter.
16079 *is_non_type
= true;
16081 parm
= grokdeclarator (parameter_declarator
->declarator
,
16082 ¶meter_declarator
->decl_specifiers
,
16083 TPARM
, /*initialized=*/0,
16084 /*attrlist=*/NULL
);
16085 if (parm
== error_mark_node
)
16086 return error_mark_node
;
16088 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16091 /* Parse a type-parameter.
16094 class identifier [opt]
16095 class identifier [opt] = type-id
16096 typename identifier [opt]
16097 typename identifier [opt] = type-id
16098 template < template-parameter-list > class identifier [opt]
16099 template < template-parameter-list > class identifier [opt]
16102 GNU Extension (variadic templates):
16105 class ... identifier [opt]
16106 typename ... identifier [opt]
16108 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16109 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16110 the declaration of the parameter.
16112 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16115 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16120 /* Look for a keyword to tell us what kind of parameter this is. */
16121 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16123 return error_mark_node
;
16125 switch (token
->keyword
)
16131 tree default_argument
;
16133 /* If the next token is an ellipsis, we have a template
16135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16137 /* Consume the `...' token. */
16138 cp_lexer_consume_token (parser
->lexer
);
16139 maybe_warn_variadic_templates ();
16141 *is_parameter_pack
= true;
16144 /* If the next token is an identifier, then it names the
16146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16147 identifier
= cp_parser_identifier (parser
);
16149 identifier
= NULL_TREE
;
16151 /* Create the parameter. */
16152 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16154 /* If the next token is an `=', we have a default argument. */
16155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16158 = cp_parser_default_type_template_argument (parser
);
16160 /* Template parameter packs cannot have default
16162 if (*is_parameter_pack
)
16165 error_at (token
->location
,
16166 "template parameter pack %qD cannot have a "
16167 "default argument", identifier
);
16169 error_at (token
->location
,
16170 "template parameter packs cannot have "
16171 "default arguments");
16172 default_argument
= NULL_TREE
;
16174 else if (check_for_bare_parameter_packs (default_argument
))
16175 default_argument
= error_mark_node
;
16178 default_argument
= NULL_TREE
;
16180 /* Create the combined representation of the parameter and the
16181 default argument. */
16182 parameter
= build_tree_list (default_argument
, parameter
);
16189 tree default_argument
;
16191 /* Look for the `<'. */
16192 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16193 /* Parse the template-parameter-list. */
16194 cp_parser_template_parameter_list (parser
);
16195 /* Look for the `>'. */
16196 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16198 // If template requirements are present, parse them.
16201 tree reqs
= get_shorthand_constraints (current_template_parms
);
16202 if (tree r
= cp_parser_requires_clause_opt (parser
))
16203 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
16204 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16207 /* Look for the `class' or 'typename' keywords. */
16208 cp_parser_type_parameter_key (parser
);
16209 /* If the next token is an ellipsis, we have a template
16211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16213 /* Consume the `...' token. */
16214 cp_lexer_consume_token (parser
->lexer
);
16215 maybe_warn_variadic_templates ();
16217 *is_parameter_pack
= true;
16219 /* If the next token is an `=', then there is a
16220 default-argument. If the next token is a `>', we are at
16221 the end of the parameter-list. If the next token is a `,',
16222 then we are at the end of this parameter. */
16223 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16224 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16225 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16227 identifier
= cp_parser_identifier (parser
);
16228 /* Treat invalid names as if the parameter were nameless. */
16229 if (identifier
== error_mark_node
)
16230 identifier
= NULL_TREE
;
16233 identifier
= NULL_TREE
;
16235 /* Create the template parameter. */
16236 parameter
= finish_template_template_parm (class_type_node
,
16239 /* If the next token is an `=', then there is a
16240 default-argument. */
16241 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16244 = cp_parser_default_template_template_argument (parser
);
16246 /* Template parameter packs cannot have default
16248 if (*is_parameter_pack
)
16251 error_at (token
->location
,
16252 "template parameter pack %qD cannot "
16253 "have a default argument",
16256 error_at (token
->location
, "template parameter packs cannot "
16257 "have default arguments");
16258 default_argument
= NULL_TREE
;
16262 default_argument
= NULL_TREE
;
16264 /* Create the combined representation of the parameter and the
16265 default argument. */
16266 parameter
= build_tree_list (default_argument
, parameter
);
16271 gcc_unreachable ();
16278 /* Parse a template-id.
16281 template-name < template-argument-list [opt] >
16283 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16284 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16285 returned. Otherwise, if the template-name names a function, or set
16286 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16287 names a class, returns a TYPE_DECL for the specialization.
16289 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16290 uninstantiated templates. */
16293 cp_parser_template_id (cp_parser
*parser
,
16294 bool template_keyword_p
,
16295 bool check_dependency_p
,
16296 enum tag_types tag_type
,
16297 bool is_declaration
)
16302 cp_token_position start_of_id
= 0;
16303 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16304 bool is_identifier
;
16306 /* If the next token corresponds to a template-id, there is no need
16308 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16309 if (token
->type
== CPP_TEMPLATE_ID
)
16311 cp_lexer_consume_token (parser
->lexer
);
16312 return saved_checks_value (token
->u
.tree_check_value
);
16315 /* Avoid performing name lookup if there is no possibility of
16316 finding a template-id. */
16317 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16318 || (token
->type
== CPP_NAME
16319 && !cp_parser_nth_token_starts_template_argument_list_p
16322 cp_parser_error (parser
, "expected template-id");
16323 return error_mark_node
;
16326 /* Remember where the template-id starts. */
16327 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16328 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16330 push_deferring_access_checks (dk_deferred
);
16332 /* Parse the template-name. */
16333 is_identifier
= false;
16334 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16335 check_dependency_p
,
16340 /* Push any access checks inside the firewall we're about to create. */
16341 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16342 pop_deferring_access_checks ();
16343 if (templ
== error_mark_node
|| is_identifier
)
16346 /* Since we're going to preserve any side-effects from this parse, set up a
16347 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16348 in the template arguments. */
16349 tentative_firewall
firewall (parser
);
16350 reopen_deferring_access_checks (checks
);
16352 /* If we find the sequence `[:' after a template-name, it's probably
16353 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16354 parse correctly the argument list. */
16355 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16356 == CPP_OPEN_SQUARE
)
16357 && next_token
->flags
& DIGRAPH
16358 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16360 && !(next_token_2
->flags
& PREV_WHITE
))
16362 cp_parser_parse_tentatively (parser
);
16363 /* Change `:' into `::'. */
16364 next_token_2
->type
= CPP_SCOPE
;
16365 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16367 cp_lexer_consume_token (parser
->lexer
);
16369 /* Parse the arguments. */
16370 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16371 if (!cp_parser_parse_definitely (parser
))
16373 /* If we couldn't parse an argument list, then we revert our changes
16374 and return simply an error. Maybe this is not a template-id
16376 next_token_2
->type
= CPP_COLON
;
16377 cp_parser_error (parser
, "expected %<<%>");
16378 pop_deferring_access_checks ();
16379 return error_mark_node
;
16381 /* Otherwise, emit an error about the invalid digraph, but continue
16382 parsing because we got our argument list. */
16383 if (permerror (next_token
->location
,
16384 "%<<::%> cannot begin a template-argument list"))
16386 static bool hint
= false;
16387 inform (next_token
->location
,
16388 "%<<:%> is an alternate spelling for %<[%>."
16389 " Insert whitespace between %<<%> and %<::%>");
16390 if (!hint
&& !flag_permissive
)
16392 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16393 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16394 "accept your code)");
16401 /* Look for the `<' that starts the template-argument-list. */
16402 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16404 pop_deferring_access_checks ();
16405 return error_mark_node
;
16407 /* Parse the arguments. */
16408 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16410 if ((cxx_dialect
> cxx17
)
16411 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16412 && !template_keyword_p
16413 && (cp_parser_error_occurred (parser
)
16414 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16416 /* This didn't go well. */
16417 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16419 /* C++2A says that "function-name < a;" is now ill-formed. */
16420 if (cp_parser_error_occurred (parser
))
16422 error_at (token
->location
, "invalid template-argument-list");
16423 inform (token
->location
, "function name as the left hand "
16424 "operand of %<<%> is ill-formed in C++2a; wrap the "
16425 "function name in %<()%>");
16428 /* We expect "f<targs>" to be followed by "(args)". */
16429 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16430 "expected %<(%> after template-argument-list");
16432 /* Purge all subsequent tokens. */
16433 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16436 cp_parser_simulate_error (parser
);
16437 pop_deferring_access_checks ();
16438 return error_mark_node
;
16442 /* Set the location to be of the form:
16443 template-name < template-argument-list [opt] >
16444 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16445 with caret == start at the start of the template-name,
16446 ranging until the closing '>'. */
16447 location_t finish_loc
16448 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
16449 location_t combined_loc
16450 = make_location (token
->location
, token
->location
, finish_loc
);
16452 /* Check for concepts autos where they don't belong. We could
16453 identify types in some cases of idnetifier TEMPL, looking ahead
16454 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16455 types. We reject them in functions, but if what we have is an
16456 identifier, even with none_type we can't conclude it's NOT a
16457 type, we have to wait for template substitution. */
16458 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16459 template_id
= error_mark_node
;
16460 /* Build a representation of the specialization. */
16461 else if (identifier_p (templ
))
16462 template_id
= build_min_nt_loc (combined_loc
,
16465 else if (DECL_TYPE_TEMPLATE_P (templ
)
16466 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16468 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16469 template (rather than some instantiation thereof) only if
16470 is not nested within some other construct. For example, in
16471 "template <typename T> void f(T) { A<T>::", A<T> is just an
16472 instantiation of A. */
16473 bool entering_scope
16474 = (template_parm_scope_p ()
16475 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16477 = finish_template_type (templ
, arguments
, entering_scope
);
16479 /* A template-like identifier may be a partial concept id. */
16480 else if (flag_concepts
16481 && (template_id
= (cp_parser_maybe_partial_concept_id
16482 (parser
, templ
, arguments
))))
16483 return template_id
;
16484 else if (variable_template_p (templ
))
16486 template_id
= lookup_template_variable (templ
, arguments
);
16487 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16488 SET_EXPR_LOCATION (template_id
, combined_loc
);
16492 /* If it's not a class-template or a template-template, it should be
16493 a function-template. */
16494 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16495 || TREE_CODE (templ
) == OVERLOAD
16496 || TREE_CODE (templ
) == FUNCTION_DECL
16497 || BASELINK_P (templ
)));
16499 template_id
= lookup_template_function (templ
, arguments
);
16500 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16501 SET_EXPR_LOCATION (template_id
, combined_loc
);
16504 /* If parsing tentatively, replace the sequence of tokens that makes
16505 up the template-id with a CPP_TEMPLATE_ID token. That way,
16506 should we re-parse the token stream, we will not have to repeat
16507 the effort required to do the parse, nor will we issue duplicate
16508 error messages about problems during instantiation of the
16511 /* Don't do this if we had a parse error in a declarator; re-parsing
16512 might succeed if a name changes meaning (60361). */
16513 && !(cp_parser_error_occurred (parser
)
16514 && cp_parser_parsing_tentatively (parser
)
16515 && parser
->in_declarator_p
))
16517 /* Reset the contents of the START_OF_ID token. */
16518 token
->type
= CPP_TEMPLATE_ID
;
16519 token
->location
= combined_loc
;
16521 /* Retrieve any deferred checks. Do not pop this access checks yet
16522 so the memory will not be reclaimed during token replacing below. */
16523 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16524 token
->u
.tree_check_value
->value
= template_id
;
16525 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16526 token
->keyword
= RID_MAX
;
16528 /* Purge all subsequent tokens. */
16529 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16531 /* ??? Can we actually assume that, if template_id ==
16532 error_mark_node, we will have issued a diagnostic to the
16533 user, as opposed to simply marking the tentative parse as
16535 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16536 error_at (token
->location
, "parse error in template argument list");
16539 pop_to_parent_deferring_access_checks ();
16540 return template_id
;
16543 /* Parse a template-name.
16548 The standard should actually say:
16552 operator-function-id
16554 A defect report has been filed about this issue.
16556 A conversion-function-id cannot be a template name because they cannot
16557 be part of a template-id. In fact, looking at this code:
16559 a.operator K<int>()
16561 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16562 It is impossible to call a templated conversion-function-id with an
16563 explicit argument list, since the only allowed template parameter is
16564 the type to which it is converting.
16566 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16567 `template' keyword, in a construction like:
16571 In that case `f' is taken to be a template-name, even though there
16572 is no way of knowing for sure.
16574 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16575 name refers to a set of overloaded functions, at least one of which
16576 is a template, or an IDENTIFIER_NODE with the name of the template,
16577 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16578 names are looked up inside uninstantiated templates. */
16581 cp_parser_template_name (cp_parser
* parser
,
16582 bool template_keyword_p
,
16583 bool check_dependency_p
,
16584 bool is_declaration
,
16585 enum tag_types tag_type
,
16586 bool *is_identifier
)
16590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16592 /* If the next token is `operator', then we have either an
16593 operator-function-id or a conversion-function-id. */
16594 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16596 /* We don't know whether we're looking at an
16597 operator-function-id or a conversion-function-id. */
16598 cp_parser_parse_tentatively (parser
);
16599 /* Try an operator-function-id. */
16600 identifier
= cp_parser_operator_function_id (parser
);
16601 /* If that didn't work, try a conversion-function-id. */
16602 if (!cp_parser_parse_definitely (parser
))
16604 cp_parser_error (parser
, "expected template-name");
16605 return error_mark_node
;
16608 /* Look for the identifier. */
16610 identifier
= cp_parser_identifier (parser
);
16612 /* If we didn't find an identifier, we don't have a template-id. */
16613 if (identifier
== error_mark_node
)
16614 return error_mark_node
;
16616 /* If the name immediately followed the `template' keyword, then it
16617 is a template-name. However, if the next token is not `<', then
16618 we do not treat it as a template-name, since it is not being used
16619 as part of a template-id. This enables us to handle constructs
16622 template <typename T> struct S { S(); };
16623 template <typename T> S<T>::S();
16625 correctly. We would treat `S' as a template -- if it were `S<T>'
16626 -- but we do not if there is no `<'. */
16628 if (processing_template_decl
16629 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16631 /* In a declaration, in a dependent context, we pretend that the
16632 "template" keyword was present in order to improve error
16633 recovery. For example, given:
16635 template <typename T> void f(T::X<int>);
16637 we want to treat "X<int>" as a template-id. */
16639 && !template_keyword_p
16640 && parser
->scope
&& TYPE_P (parser
->scope
)
16641 && check_dependency_p
16642 && dependent_scope_p (parser
->scope
)
16643 /* Do not do this for dtors (or ctors), since they never
16644 need the template keyword before their name. */
16645 && !constructor_name_p (identifier
, parser
->scope
))
16647 cp_token_position start
= 0;
16649 /* Explain what went wrong. */
16650 error_at (token
->location
, "non-template %qD used as template",
16652 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16653 parser
->scope
, identifier
);
16654 /* If parsing tentatively, find the location of the "<" token. */
16655 if (cp_parser_simulate_error (parser
))
16656 start
= cp_lexer_token_position (parser
->lexer
, true);
16657 /* Parse the template arguments so that we can issue error
16658 messages about them. */
16659 cp_lexer_consume_token (parser
->lexer
);
16660 cp_parser_enclosed_template_argument_list (parser
);
16661 /* Skip tokens until we find a good place from which to
16662 continue parsing. */
16663 cp_parser_skip_to_closing_parenthesis (parser
,
16664 /*recovering=*/true,
16666 /*consume_paren=*/false);
16667 /* If parsing tentatively, permanently remove the
16668 template argument list. That will prevent duplicate
16669 error messages from being issued about the missing
16670 "template" keyword. */
16672 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16674 *is_identifier
= true;
16675 parser
->context
->object_type
= NULL_TREE
;
16679 /* If the "template" keyword is present, then there is generally
16680 no point in doing name-lookup, so we just return IDENTIFIER.
16681 But, if the qualifying scope is non-dependent then we can
16682 (and must) do name-lookup normally. */
16683 if (template_keyword_p
)
16685 tree scope
= (parser
->scope
? parser
->scope
16686 : parser
->context
->object_type
);
16687 if (scope
&& TYPE_P (scope
)
16688 && (!CLASS_TYPE_P (scope
)
16689 || (check_dependency_p
&& dependent_type_p (scope
))))
16691 /* We're optimizing away the call to cp_parser_lookup_name, but
16692 we still need to do this. */
16693 parser
->context
->object_type
= NULL_TREE
;
16699 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16700 const bool scoped_p
= ((parser
->scope
? parser
->scope
16701 : parser
->context
->object_type
) != NULL_TREE
);
16703 /* Look up the name. */
16704 decl
= cp_parser_lookup_name (parser
, identifier
,
16706 /*is_template=*/true,
16707 /*is_namespace=*/false,
16708 check_dependency_p
,
16709 /*ambiguous_decls=*/NULL
,
16712 decl
= strip_using_decl (decl
);
16714 /* If DECL is a template, then the name was a template-name. */
16715 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16717 if (TREE_DEPRECATED (decl
)
16718 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16719 warn_deprecated_use (decl
, NULL_TREE
);
16723 /* The standard does not explicitly indicate whether a name that
16724 names a set of overloaded declarations, some of which are
16725 templates, is a template-name. However, such a name should
16726 be a template-name; otherwise, there is no way to form a
16727 template-id for the overloaded templates. */
16728 bool found
= false;
16730 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16731 !found
&& iter
; ++iter
)
16732 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16736 && (cxx_dialect
> cxx17
)
16738 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
16739 && tag_type
== none_type
)
16741 /* [temp.names] says "A name is also considered to refer to a template
16742 if it is an unqualified-id followed by a < and name lookup finds
16743 either one or more functions or finds nothing." */
16745 /* The "more functions" case. Just use the OVERLOAD as normally.
16746 We don't use is_overloaded_fn here to avoid considering
16748 if (TREE_CODE (decl
) == OVERLOAD
16749 /* Name lookup found one function. */
16750 || TREE_CODE (decl
) == FUNCTION_DECL
)
16752 /* Name lookup found nothing. */
16753 else if (decl
== error_mark_node
)
16759 /* The name does not name a template. */
16760 cp_parser_error (parser
, "expected template-name");
16761 return error_mark_node
;
16768 /* Parse a template-argument-list.
16770 template-argument-list:
16771 template-argument ... [opt]
16772 template-argument-list , template-argument ... [opt]
16774 Returns a TREE_VEC containing the arguments. */
16777 cp_parser_template_argument_list (cp_parser
* parser
)
16779 tree fixed_args
[10];
16780 unsigned n_args
= 0;
16781 unsigned alloced
= 10;
16782 tree
*arg_ary
= fixed_args
;
16784 bool saved_in_template_argument_list_p
;
16786 bool saved_non_ice_p
;
16788 /* Don't create location wrapper nodes within a template-argument-list. */
16789 auto_suppress_location_wrappers sentinel
;
16791 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16792 parser
->in_template_argument_list_p
= true;
16793 /* Even if the template-id appears in an integral
16794 constant-expression, the contents of the argument list do
16796 saved_ice_p
= parser
->integral_constant_expression_p
;
16797 parser
->integral_constant_expression_p
= false;
16798 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16799 parser
->non_integral_constant_expression_p
= false;
16801 /* Parse the arguments. */
16807 /* Consume the comma. */
16808 cp_lexer_consume_token (parser
->lexer
);
16810 /* Parse the template-argument. */
16811 argument
= cp_parser_template_argument (parser
);
16813 /* If the next token is an ellipsis, we're expanding a template
16815 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16817 if (argument
== error_mark_node
)
16819 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16820 error_at (token
->location
,
16821 "expected parameter pack before %<...%>");
16823 /* Consume the `...' token. */
16824 cp_lexer_consume_token (parser
->lexer
);
16826 /* Make the argument into a TYPE_PACK_EXPANSION or
16827 EXPR_PACK_EXPANSION. */
16828 argument
= make_pack_expansion (argument
);
16831 if (n_args
== alloced
)
16835 if (arg_ary
== fixed_args
)
16837 arg_ary
= XNEWVEC (tree
, alloced
);
16838 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16841 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16843 arg_ary
[n_args
++] = argument
;
16845 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16847 vec
= make_tree_vec (n_args
);
16850 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16852 if (arg_ary
!= fixed_args
)
16854 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16855 parser
->integral_constant_expression_p
= saved_ice_p
;
16856 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16858 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16862 /* Parse a template-argument.
16865 assignment-expression
16869 The representation is that of an assignment-expression, type-id, or
16870 id-expression -- except that the qualified id-expression is
16871 evaluated, so that the value returned is either a DECL or an
16874 Although the standard says "assignment-expression", it forbids
16875 throw-expressions or assignments in the template argument.
16876 Therefore, we use "conditional-expression" instead. */
16879 cp_parser_template_argument (cp_parser
* parser
)
16884 bool maybe_type_id
= false;
16885 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16886 location_t loc
= 0;
16889 /* There's really no way to know what we're looking at, so we just
16890 try each alternative in order.
16894 In a template-argument, an ambiguity between a type-id and an
16895 expression is resolved to a type-id, regardless of the form of
16896 the corresponding template-parameter.
16898 Therefore, we try a type-id first. */
16899 cp_parser_parse_tentatively (parser
);
16900 argument
= cp_parser_template_type_arg (parser
);
16901 /* If there was no error parsing the type-id but the next token is a
16902 '>>', our behavior depends on which dialect of C++ we're
16903 parsing. In C++98, we probably found a typo for '> >'. But there
16904 are type-id which are also valid expressions. For instance:
16906 struct X { int operator >> (int); };
16907 template <int V> struct Foo {};
16910 Here 'X()' is a valid type-id of a function type, but the user just
16911 wanted to write the expression "X() >> 5". Thus, we remember that we
16912 found a valid type-id, but we still try to parse the argument as an
16913 expression to see what happens.
16915 In C++0x, the '>>' will be considered two separate '>'
16917 if (!cp_parser_error_occurred (parser
)
16918 && cxx_dialect
== cxx98
16919 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16921 maybe_type_id
= true;
16922 cp_parser_abort_tentative_parse (parser
);
16926 /* If the next token isn't a `,' or a `>', then this argument wasn't
16927 really finished. This means that the argument is not a valid
16929 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16930 cp_parser_error (parser
, "expected template-argument");
16931 /* If that worked, we're done. */
16932 if (cp_parser_parse_definitely (parser
))
16935 /* We're still not sure what the argument will be. */
16936 cp_parser_parse_tentatively (parser
);
16937 /* Try a template. */
16938 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16939 argument
= cp_parser_id_expression (parser
,
16940 /*template_keyword_p=*/false,
16941 /*check_dependency_p=*/true,
16943 /*declarator_p=*/false,
16944 /*optional_p=*/false);
16945 /* If the next token isn't a `,' or a `>', then this argument wasn't
16946 really finished. */
16947 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16948 cp_parser_error (parser
, "expected template-argument");
16949 if (!cp_parser_error_occurred (parser
))
16951 /* Figure out what is being referred to. If the id-expression
16952 was for a class template specialization, then we will have a
16953 TYPE_DECL at this point. There is no need to do name lookup
16954 at this point in that case. */
16955 if (TREE_CODE (argument
) != TYPE_DECL
)
16956 argument
= cp_parser_lookup_name (parser
, argument
,
16958 /*is_template=*/template_p
,
16959 /*is_namespace=*/false,
16960 /*check_dependency=*/true,
16961 /*ambiguous_decls=*/NULL
,
16962 argument_start_token
->location
);
16963 /* Handle a constrained-type-specifier for a non-type template
16965 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16967 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16968 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16969 cp_parser_error (parser
, "expected template-name");
16971 if (cp_parser_parse_definitely (parser
))
16973 if (TREE_DEPRECATED (argument
))
16974 warn_deprecated_use (argument
, NULL_TREE
);
16977 /* It must be a non-type argument. In C++17 any constant-expression is
16979 if (cxx_dialect
> cxx14
)
16982 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16984 -- an integral constant-expression of integral or enumeration
16987 -- the name of a non-type template-parameter; or
16989 -- the name of an object or function with external linkage...
16991 -- the address of an object or function with external linkage...
16993 -- a pointer to member... */
16994 /* Look for a non-type template parameter. */
16995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16997 cp_parser_parse_tentatively (parser
);
16998 argument
= cp_parser_primary_expression (parser
,
16999 /*address_p=*/false,
17001 /*template_arg_p=*/true,
17003 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
17004 || !cp_parser_next_token_ends_template_argument_p (parser
))
17005 cp_parser_simulate_error (parser
);
17006 if (cp_parser_parse_definitely (parser
))
17010 /* If the next token is "&", the argument must be the address of an
17011 object or function with external linkage. */
17012 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
17015 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17016 cp_lexer_consume_token (parser
->lexer
);
17018 /* See if we might have an id-expression. */
17019 token
= cp_lexer_peek_token (parser
->lexer
);
17020 if (token
->type
== CPP_NAME
17021 || token
->keyword
== RID_OPERATOR
17022 || token
->type
== CPP_SCOPE
17023 || token
->type
== CPP_TEMPLATE_ID
17024 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
17026 cp_parser_parse_tentatively (parser
);
17027 argument
= cp_parser_primary_expression (parser
,
17030 /*template_arg_p=*/true,
17032 if (cp_parser_error_occurred (parser
)
17033 || !cp_parser_next_token_ends_template_argument_p (parser
))
17034 cp_parser_abort_tentative_parse (parser
);
17039 if (INDIRECT_REF_P (argument
))
17041 /* Strip the dereference temporarily. */
17042 gcc_assert (REFERENCE_REF_P (argument
));
17043 argument
= TREE_OPERAND (argument
, 0);
17046 /* If we're in a template, we represent a qualified-id referring
17047 to a static data member as a SCOPE_REF even if the scope isn't
17048 dependent so that we can check access control later. */
17050 if (TREE_CODE (probe
) == SCOPE_REF
)
17051 probe
= TREE_OPERAND (probe
, 1);
17054 /* A variable without external linkage might still be a
17055 valid constant-expression, so no error is issued here
17056 if the external-linkage check fails. */
17057 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
17058 cp_parser_simulate_error (parser
);
17060 else if (is_overloaded_fn (argument
))
17061 /* All overloaded functions are allowed; if the external
17062 linkage test does not pass, an error will be issued
17066 && (TREE_CODE (argument
) == OFFSET_REF
17067 || TREE_CODE (argument
) == SCOPE_REF
))
17068 /* A pointer-to-member. */
17070 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
17073 cp_parser_simulate_error (parser
);
17075 if (cp_parser_parse_definitely (parser
))
17078 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17079 tf_warning_or_error
);
17081 argument
= convert_from_reference (argument
);
17086 /* If the argument started with "&", there are no other valid
17087 alternatives at this point. */
17090 cp_parser_error (parser
, "invalid non-type template argument");
17091 return error_mark_node
;
17095 /* If the argument wasn't successfully parsed as a type-id followed
17096 by '>>', the argument can only be a constant expression now.
17097 Otherwise, we try parsing the constant-expression tentatively,
17098 because the argument could really be a type-id. */
17100 cp_parser_parse_tentatively (parser
);
17102 if (cxx_dialect
<= cxx14
)
17103 argument
= cp_parser_constant_expression (parser
);
17106 /* In C++20, we can encounter a braced-init-list. */
17107 if (cxx_dialect
>= cxx2a
17108 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17110 bool expr_non_constant_p
;
17111 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17114 /* With C++17 generalized non-type template arguments we need to handle
17115 lvalue constant expressions, too. */
17116 argument
= cp_parser_assignment_expression (parser
);
17117 require_potential_constant_expression (argument
);
17120 if (!maybe_type_id
)
17122 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17123 cp_parser_error (parser
, "expected template-argument");
17124 if (cp_parser_parse_definitely (parser
))
17126 /* We did our best to parse the argument as a non type-id, but that
17127 was the only alternative that matched (albeit with a '>' after
17128 it). We can assume it's just a typo from the user, and a
17129 diagnostic will then be issued. */
17130 return cp_parser_template_type_arg (parser
);
17133 /* Parse an explicit-instantiation.
17135 explicit-instantiation:
17136 template declaration
17138 Although the standard says `declaration', what it really means is:
17140 explicit-instantiation:
17141 template decl-specifier-seq [opt] declarator [opt] ;
17143 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17144 supposed to be allowed. A defect report has been filed about this
17149 explicit-instantiation:
17150 storage-class-specifier template
17151 decl-specifier-seq [opt] declarator [opt] ;
17152 function-specifier template
17153 decl-specifier-seq [opt] declarator [opt] ; */
17156 cp_parser_explicit_instantiation (cp_parser
* parser
)
17158 int declares_class_or_enum
;
17159 cp_decl_specifier_seq decl_specifiers
;
17160 tree extension_specifier
= NULL_TREE
;
17162 timevar_push (TV_TEMPLATE_INST
);
17164 /* Look for an (optional) storage-class-specifier or
17165 function-specifier. */
17166 if (cp_parser_allow_gnu_extensions_p (parser
))
17168 extension_specifier
17169 = cp_parser_storage_class_specifier_opt (parser
);
17170 if (!extension_specifier
)
17171 extension_specifier
17172 = cp_parser_function_specifier_opt (parser
,
17173 /*decl_specs=*/NULL
);
17176 /* Look for the `template' keyword. */
17177 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17178 /* Let the front end know that we are processing an explicit
17180 begin_explicit_instantiation ();
17181 /* [temp.explicit] says that we are supposed to ignore access
17182 control while processing explicit instantiation directives. */
17183 push_deferring_access_checks (dk_no_check
);
17184 /* Parse a decl-specifier-seq. */
17185 cp_parser_decl_specifier_seq (parser
,
17186 CP_PARSER_FLAGS_OPTIONAL
,
17188 &declares_class_or_enum
);
17189 /* If there was exactly one decl-specifier, and it declared a class,
17190 and there's no declarator, then we have an explicit type
17192 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17196 type
= check_tag_decl (&decl_specifiers
,
17197 /*explicit_type_instantiation_p=*/true);
17198 /* Turn access control back on for names used during
17199 template instantiation. */
17200 pop_deferring_access_checks ();
17202 do_type_instantiation (type
, extension_specifier
,
17203 /*complain=*/tf_error
);
17207 cp_declarator
*declarator
;
17210 /* Parse the declarator. */
17212 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17213 CP_PARSER_FLAGS_NONE
,
17214 /*ctor_dtor_or_conv_p=*/NULL
,
17215 /*parenthesized_p=*/NULL
,
17216 /*member_p=*/false,
17217 /*friend_p=*/false,
17218 /*static_p=*/false);
17219 if (declares_class_or_enum
& 2)
17220 cp_parser_check_for_definition_in_return_type (declarator
,
17221 decl_specifiers
.type
,
17222 decl_specifiers
.locations
[ds_type_spec
]);
17223 if (declarator
!= cp_error_declarator
)
17225 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17226 permerror (decl_specifiers
.locations
[ds_inline
],
17227 "explicit instantiation shall not use"
17228 " %<inline%> specifier");
17229 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17230 permerror (decl_specifiers
.locations
[ds_constexpr
],
17231 "explicit instantiation shall not use"
17232 " %<constexpr%> specifier");
17234 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17235 NORMAL
, 0, &decl_specifiers
.attributes
);
17236 /* Turn access control back on for names used during
17237 template instantiation. */
17238 pop_deferring_access_checks ();
17239 /* Do the explicit instantiation. */
17240 do_decl_instantiation (decl
, extension_specifier
);
17244 pop_deferring_access_checks ();
17245 /* Skip the body of the explicit instantiation. */
17246 cp_parser_skip_to_end_of_statement (parser
);
17249 /* We're done with the instantiation. */
17250 end_explicit_instantiation ();
17252 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17254 timevar_pop (TV_TEMPLATE_INST
);
17257 /* Parse an explicit-specialization.
17259 explicit-specialization:
17260 template < > declaration
17262 Although the standard says `declaration', what it really means is:
17264 explicit-specialization:
17265 template <> decl-specifier [opt] init-declarator [opt] ;
17266 template <> function-definition
17267 template <> explicit-specialization
17268 template <> template-declaration */
17271 cp_parser_explicit_specialization (cp_parser
* parser
)
17273 bool need_lang_pop
;
17274 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17276 /* Look for the `template' keyword. */
17277 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17278 /* Look for the `<'. */
17279 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17280 /* Look for the `>'. */
17281 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17282 /* We have processed another parameter list. */
17283 ++parser
->num_template_parameter_lists
;
17286 A template ... explicit specialization ... shall not have C
17288 if (current_lang_name
== lang_name_c
)
17290 error_at (token
->location
, "template specialization with C linkage");
17291 maybe_show_extern_c_location ();
17292 /* Give it C++ linkage to avoid confusing other parts of the
17294 push_lang_context (lang_name_cplusplus
);
17295 need_lang_pop
= true;
17298 need_lang_pop
= false;
17299 /* Let the front end know that we are beginning a specialization. */
17300 if (!begin_specialization ())
17302 end_specialization ();
17306 /* If the next keyword is `template', we need to figure out whether
17307 or not we're looking a template-declaration. */
17308 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17310 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17311 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17312 cp_parser_template_declaration_after_export (parser
,
17313 /*member_p=*/false);
17315 cp_parser_explicit_specialization (parser
);
17318 /* Parse the dependent declaration. */
17319 cp_parser_single_declaration (parser
,
17321 /*member_p=*/false,
17322 /*explicit_specialization_p=*/true,
17323 /*friend_p=*/NULL
);
17324 /* We're done with the specialization. */
17325 end_specialization ();
17326 /* For the erroneous case of a template with C linkage, we pushed an
17327 implicit C++ linkage scope; exit that scope now. */
17329 pop_lang_context ();
17330 /* We're done with this parameter list. */
17331 --parser
->num_template_parameter_lists
;
17334 /* Parse a type-specifier.
17337 simple-type-specifier
17340 elaborated-type-specifier
17348 Returns a representation of the type-specifier. For a
17349 class-specifier, enum-specifier, or elaborated-type-specifier, a
17350 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17352 The parser flags FLAGS is used to control type-specifier parsing.
17354 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17355 in a decl-specifier-seq.
17357 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17358 class-specifier, enum-specifier, or elaborated-type-specifier, then
17359 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17360 if a type is declared; 2 if it is defined. Otherwise, it is set to
17363 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17364 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17365 is set to FALSE. */
17368 cp_parser_type_specifier (cp_parser
* parser
,
17369 cp_parser_flags flags
,
17370 cp_decl_specifier_seq
*decl_specs
,
17371 bool is_declaration
,
17372 int* declares_class_or_enum
,
17373 bool* is_cv_qualifier
)
17375 tree type_spec
= NULL_TREE
;
17378 cp_decl_spec ds
= ds_last
;
17380 /* Assume this type-specifier does not declare a new type. */
17381 if (declares_class_or_enum
)
17382 *declares_class_or_enum
= 0;
17383 /* And that it does not specify a cv-qualifier. */
17384 if (is_cv_qualifier
)
17385 *is_cv_qualifier
= false;
17386 /* Peek at the next token. */
17387 token
= cp_lexer_peek_token (parser
->lexer
);
17389 /* If we're looking at a keyword, we can use that to guide the
17390 production we choose. */
17391 keyword
= token
->keyword
;
17395 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17396 goto elaborated_type_specifier
;
17398 /* Look for the enum-specifier. */
17399 type_spec
= cp_parser_enum_specifier (parser
);
17400 /* If that worked, we're done. */
17403 if (declares_class_or_enum
)
17404 *declares_class_or_enum
= 2;
17406 cp_parser_set_decl_spec_type (decl_specs
,
17409 /*type_definition_p=*/true);
17413 goto elaborated_type_specifier
;
17415 /* Any of these indicate either a class-specifier, or an
17416 elaborated-type-specifier. */
17420 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17421 goto elaborated_type_specifier
;
17423 /* Parse tentatively so that we can back up if we don't find a
17424 class-specifier. */
17425 cp_parser_parse_tentatively (parser
);
17426 /* Look for the class-specifier. */
17427 type_spec
= cp_parser_class_specifier (parser
);
17428 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17429 /* If that worked, we're done. */
17430 if (cp_parser_parse_definitely (parser
))
17432 if (declares_class_or_enum
)
17433 *declares_class_or_enum
= 2;
17435 cp_parser_set_decl_spec_type (decl_specs
,
17438 /*type_definition_p=*/true);
17442 /* Fall through. */
17443 elaborated_type_specifier
:
17444 /* We're declaring (not defining) a class or enum. */
17445 if (declares_class_or_enum
)
17446 *declares_class_or_enum
= 1;
17448 /* Fall through. */
17450 /* Look for an elaborated-type-specifier. */
17452 = (cp_parser_elaborated_type_specifier
17454 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17457 cp_parser_set_decl_spec_type (decl_specs
,
17460 /*type_definition_p=*/false);
17465 if (is_cv_qualifier
)
17466 *is_cv_qualifier
= true;
17471 if (is_cv_qualifier
)
17472 *is_cv_qualifier
= true;
17477 if (is_cv_qualifier
)
17478 *is_cv_qualifier
= true;
17482 /* The `__complex__' keyword is a GNU extension. */
17490 /* Handle simple keywords. */
17495 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17496 decl_specs
->any_specifiers_p
= true;
17498 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17501 /* If we do not already have a type-specifier, assume we are looking
17502 at a simple-type-specifier. */
17503 type_spec
= cp_parser_simple_type_specifier (parser
,
17507 /* If we didn't find a type-specifier, and a type-specifier was not
17508 optional in this context, issue an error message. */
17509 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17511 cp_parser_error (parser
, "expected type specifier");
17512 return error_mark_node
;
17518 /* Parse a simple-type-specifier.
17520 simple-type-specifier:
17521 :: [opt] nested-name-specifier [opt] type-name
17522 :: [opt] nested-name-specifier template template-id
17537 simple-type-specifier:
17539 decltype ( expression )
17542 __underlying_type ( type-id )
17546 nested-name-specifier(opt) template-name
17550 simple-type-specifier:
17552 __typeof__ unary-expression
17553 __typeof__ ( type-id )
17554 __typeof__ ( type-id ) { initializer-list , [opt] }
17556 Concepts Extension:
17558 simple-type-specifier:
17559 constrained-type-specifier
17561 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17562 appropriately updated. */
17565 cp_parser_simple_type_specifier (cp_parser
* parser
,
17566 cp_decl_specifier_seq
*decl_specs
,
17567 cp_parser_flags flags
)
17569 tree type
= NULL_TREE
;
17573 /* Peek at the next token. */
17574 token
= cp_lexer_peek_token (parser
->lexer
);
17576 /* If we're looking at a keyword, things are easy. */
17577 switch (token
->keyword
)
17581 decl_specs
->explicit_char_p
= true;
17582 type
= char_type_node
;
17585 type
= char8_type_node
;
17588 type
= char16_type_node
;
17591 type
= char32_type_node
;
17594 type
= wchar_type_node
;
17597 type
= boolean_type_node
;
17600 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17601 type
= short_integer_type_node
;
17605 decl_specs
->explicit_int_p
= true;
17606 type
= integer_type_node
;
17612 idx
= token
->keyword
- RID_INT_N_0
;
17613 if (! int_n_enabled_p
[idx
])
17617 decl_specs
->explicit_intN_p
= true;
17618 decl_specs
->int_n_idx
= idx
;
17620 type
= int_n_trees
[idx
].signed_type
;
17624 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17625 type
= long_integer_type_node
;
17628 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17629 type
= integer_type_node
;
17632 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17633 type
= unsigned_type_node
;
17636 type
= float_type_node
;
17639 type
= double_type_node
;
17642 type
= void_type_node
;
17646 maybe_warn_cpp0x (CPP0X_AUTO
);
17647 if (parser
->auto_is_implicit_function_template_parm_p
)
17649 /* The 'auto' might be the placeholder return type for a function decl
17650 with trailing return type. */
17651 bool have_trailing_return_fn_decl
= false;
17653 cp_parser_parse_tentatively (parser
);
17654 cp_lexer_consume_token (parser
->lexer
);
17655 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17656 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17657 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17658 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17662 cp_lexer_consume_token (parser
->lexer
);
17663 cp_parser_skip_to_closing_parenthesis (parser
,
17664 /*recovering*/false,
17666 /*consume_paren*/true);
17670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17672 have_trailing_return_fn_decl
= true;
17676 cp_lexer_consume_token (parser
->lexer
);
17678 cp_parser_abort_tentative_parse (parser
);
17680 if (have_trailing_return_fn_decl
)
17682 type
= make_auto ();
17686 if (cxx_dialect
>= cxx14
)
17688 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17689 type
= TREE_TYPE (type
);
17692 type
= error_mark_node
;
17694 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17696 if (cxx_dialect
< cxx14
)
17697 error_at (token
->location
,
17698 "use of %<auto%> in lambda parameter declaration "
17699 "only available with "
17700 "%<-std=c++14%> or %<-std=gnu++14%>");
17702 else if (cxx_dialect
< cxx14
)
17703 error_at (token
->location
,
17704 "use of %<auto%> in parameter declaration "
17705 "only available with "
17706 "%<-std=c++14%> or %<-std=gnu++14%>");
17707 else if (!flag_concepts
)
17708 pedwarn (token
->location
, 0,
17709 "use of %<auto%> in parameter declaration "
17710 "only available with %<-fconcepts%>");
17713 type
= make_auto ();
17717 /* Since DR 743, decltype can either be a simple-type-specifier by
17718 itself or begin a nested-name-specifier. Parsing it will replace
17719 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17720 handling below decide what to do. */
17721 cp_parser_decltype (parser
);
17722 cp_lexer_set_token_position (parser
->lexer
, token
);
17726 /* Consume the `typeof' token. */
17727 cp_lexer_consume_token (parser
->lexer
);
17728 /* Parse the operand to `typeof'. */
17729 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17730 /* If it is not already a TYPE, take its type. */
17731 if (!TYPE_P (type
))
17732 type
= finish_typeof (type
);
17735 cp_parser_set_decl_spec_type (decl_specs
, type
,
17737 /*type_definition_p=*/false);
17741 case RID_UNDERLYING_TYPE
:
17742 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17744 cp_parser_set_decl_spec_type (decl_specs
, type
,
17746 /*type_definition_p=*/false);
17751 case RID_DIRECT_BASES
:
17752 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17754 cp_parser_set_decl_spec_type (decl_specs
, type
,
17756 /*type_definition_p=*/false);
17762 /* If token is an already-parsed decltype not followed by ::,
17763 it's a simple-type-specifier. */
17764 if (token
->type
== CPP_DECLTYPE
17765 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17767 type
= saved_checks_value (token
->u
.tree_check_value
);
17770 cp_parser_set_decl_spec_type (decl_specs
, type
,
17772 /*type_definition_p=*/false);
17773 /* Remember that we are handling a decltype in order to
17774 implement the resolution of DR 1510 when the argument
17775 isn't instantiation dependent. */
17776 decl_specs
->decltype_p
= true;
17778 cp_lexer_consume_token (parser
->lexer
);
17782 /* If the type-specifier was for a built-in type, we're done. */
17785 /* Record the type. */
17787 && (token
->keyword
!= RID_SIGNED
17788 && token
->keyword
!= RID_UNSIGNED
17789 && token
->keyword
!= RID_SHORT
17790 && token
->keyword
!= RID_LONG
))
17791 cp_parser_set_decl_spec_type (decl_specs
,
17794 /*type_definition_p=*/false);
17796 decl_specs
->any_specifiers_p
= true;
17798 /* Consume the token. */
17799 cp_lexer_consume_token (parser
->lexer
);
17801 if (type
== error_mark_node
)
17802 return error_mark_node
;
17804 /* There is no valid C++ program where a non-template type is
17805 followed by a "<". That usually indicates that the user thought
17806 that the type was a template. */
17807 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17810 return TYPE_NAME (type
);
17813 /* The type-specifier must be a user-defined type. */
17814 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17818 const bool typename_p
= (cxx_dialect
>= cxx2a
17819 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
17821 /* Don't gobble tokens or issue error messages if this is an
17822 optional type-specifier. */
17823 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17824 cp_parser_parse_tentatively (parser
);
17826 token
= cp_lexer_peek_token (parser
->lexer
);
17828 /* Look for the optional `::' operator. */
17830 = (cp_parser_global_scope_opt (parser
,
17831 /*current_scope_valid_p=*/false)
17833 /* Look for the nested-name specifier. */
17835 = (cp_parser_nested_name_specifier_opt (parser
,
17836 /*typename_keyword_p=*/false,
17837 /*check_dependency_p=*/true,
17839 /*is_declaration=*/false)
17841 /* If we have seen a nested-name-specifier, and the next token
17842 is `template', then we are using the template-id production. */
17844 && cp_parser_optional_template_keyword (parser
))
17846 /* Look for the template-id. */
17847 type
= cp_parser_template_id (parser
,
17848 /*template_keyword_p=*/true,
17849 /*check_dependency_p=*/true,
17851 /*is_declaration=*/false);
17852 /* If the template-id did not name a type, we are out of
17854 if (TREE_CODE (type
) != TYPE_DECL
)
17856 /* ...unless we pretend we have seen 'typename'. */
17858 type
= cp_parser_make_typename_type (parser
, type
,
17862 cp_parser_error (parser
, "expected template-id for type");
17867 /* Otherwise, look for a type-name. */
17869 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
17871 /* Keep track of all name-lookups performed in class scopes. */
17875 && TREE_CODE (type
) == TYPE_DECL
17876 && identifier_p (DECL_NAME (type
)))
17877 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17878 /* If it didn't work out, we don't have a TYPE. */
17879 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17880 && !cp_parser_parse_definitely (parser
))
17882 if (!type
&& cxx_dialect
>= cxx17
)
17884 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17885 cp_parser_parse_tentatively (parser
);
17887 cp_parser_global_scope_opt (parser
,
17888 /*current_scope_valid_p=*/false);
17889 cp_parser_nested_name_specifier_opt (parser
,
17890 /*typename_keyword_p=*/false,
17891 /*check_dependency_p=*/true,
17893 /*is_declaration=*/false);
17894 tree name
= cp_parser_identifier (parser
);
17895 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17896 && parser
->scope
!= error_mark_node
)
17898 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17900 /*is_template=*/false,
17901 /*is_namespace=*/false,
17902 /*check_dependency=*/true,
17903 /*ambiguous_decls=*/NULL
,
17905 if (tmpl
&& tmpl
!= error_mark_node
17906 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17907 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17908 type
= make_template_placeholder (tmpl
);
17911 type
= error_mark_node
;
17912 if (!cp_parser_simulate_error (parser
))
17913 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17914 NLE_TYPE
, token
->location
);
17918 type
= error_mark_node
;
17920 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17921 && !cp_parser_parse_definitely (parser
))
17924 if (type
&& decl_specs
)
17925 cp_parser_set_decl_spec_type (decl_specs
, type
,
17927 /*type_definition_p=*/false);
17930 /* If we didn't get a type-name, issue an error message. */
17931 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17933 cp_parser_error (parser
, "expected type-name");
17934 return error_mark_node
;
17937 if (type
&& type
!= error_mark_node
)
17939 /* See if TYPE is an Objective-C type, and if so, parse and
17940 accept any protocol references following it. Do this before
17941 the cp_parser_check_for_invalid_template_id() call, because
17942 Objective-C types can be followed by '<...>' which would
17943 enclose protocol names rather than template arguments, and so
17944 everything is fine. */
17945 if (c_dialect_objc () && !parser
->scope
17946 && (objc_is_id (type
) || objc_is_class_name (type
)))
17948 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17949 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17951 /* Clobber the "unqualified" type previously entered into
17952 DECL_SPECS with the new, improved protocol-qualified version. */
17954 decl_specs
->type
= qual_type
;
17959 /* There is no valid C++ program where a non-template type is
17960 followed by a "<". That usually indicates that the user
17961 thought that the type was a template. */
17962 cp_parser_check_for_invalid_template_id (parser
, type
,
17970 /* Parse a type-name.
17976 simple-template-id [in c++0x]
17993 Returns a TYPE_DECL for the type. */
17996 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
18000 /* We can't know yet whether it is a class-name or not. */
18001 cp_parser_parse_tentatively (parser
);
18002 /* Try a class-name. */
18003 type_decl
= cp_parser_class_name (parser
,
18004 typename_keyword_p
,
18005 /*template_keyword_p=*/false,
18007 /*check_dependency_p=*/true,
18008 /*class_head_p=*/false,
18009 /*is_declaration=*/false);
18010 /* If it's not a class-name, keep looking. */
18011 if (!cp_parser_parse_definitely (parser
))
18013 if (cxx_dialect
< cxx11
)
18014 /* It must be a typedef-name or an enum-name. */
18015 return cp_parser_nonclass_name (parser
);
18017 cp_parser_parse_tentatively (parser
);
18018 /* It is either a simple-template-id representing an
18019 instantiation of an alias template... */
18020 type_decl
= cp_parser_template_id (parser
,
18021 /*template_keyword_p=*/false,
18022 /*check_dependency_p=*/true,
18024 /*is_declaration=*/false);
18025 /* Note that this must be an instantiation of an alias template
18026 because [temp.names]/6 says:
18028 A template-id that names an alias template specialization
18031 Whereas [temp.names]/7 says:
18033 A simple-template-id that names a class template
18034 specialization is a class-name.
18036 With concepts, this could also be a partial-concept-id that
18037 declares a non-type template parameter. */
18038 if (type_decl
!= NULL_TREE
18039 && TREE_CODE (type_decl
) == TYPE_DECL
18040 && TYPE_DECL_ALIAS_P (type_decl
))
18041 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
18042 else if (is_constrained_parameter (type_decl
))
18043 /* Don't do anything. */ ;
18045 cp_parser_simulate_error (parser
);
18047 if (!cp_parser_parse_definitely (parser
))
18048 /* ... Or a typedef-name or an enum-name. */
18049 return cp_parser_nonclass_name (parser
);
18055 /* Check if DECL and ARGS can form a constrained-type-specifier.
18056 If ARGS is non-null, we try to form a concept check of the
18057 form DECL<?, ARGS> where ? is a wildcard that matches any
18058 kind of template argument. If ARGS is NULL, then we try to
18059 form a concept check of the form DECL<?>. */
18062 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
18063 tree decl
, tree args
)
18065 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
18067 /* If we a constrained-type-specifier cannot be deduced. */
18068 if (parser
->prevent_constrained_type_specifiers
)
18071 /* A constrained type specifier can only be found in an
18072 overload set or as a reference to a template declaration.
18074 FIXME: This might be masking a bug. It's possible that
18075 that the deduction below is causing template specializations
18076 to be formed with the wildcard as an argument. */
18077 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
18080 /* Try to build a call expression that evaluates the
18081 concept. This can fail if the overload set refers
18082 only to non-templates. */
18083 tree placeholder
= build_nt (WILDCARD_DECL
);
18084 tree check
= build_concept_check (decl
, placeholder
, args
);
18085 if (check
== error_mark_node
)
18088 /* Deduce the checked constraint and the prototype parameter.
18090 FIXME: In certain cases, failure to deduce should be a
18091 diagnosable error. */
18094 if (!deduce_constrained_parameter (check
, conc
, proto
))
18097 /* In template parameter scope, this results in a constrained
18098 parameter. Return a descriptor of that parm. */
18099 if (processing_template_parmlist
)
18100 return build_constrained_parameter (conc
, proto
, args
);
18102 /* In a parameter-declaration-clause, constrained-type
18103 specifiers result in invented template parameters. */
18104 if (parser
->auto_is_implicit_function_template_parm_p
)
18106 tree x
= build_constrained_parameter (conc
, proto
, args
);
18107 return synthesize_implicit_template_parm (parser
, x
);
18111 /* Otherwise, we're in a context where the constrained
18112 type name is deduced and the constraint applies
18113 after deduction. */
18114 return make_constrained_auto (conc
, args
);
18120 /* If DECL refers to a concept, return a TYPE_DECL representing
18121 the result of using the constrained type specifier in the
18122 current context. DECL refers to a concept if
18124 - it is an overload set containing a function concept taking a single
18127 - it is a variable concept taking a single type argument. */
18130 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
18133 && (TREE_CODE (decl
) == OVERLOAD
18134 || BASELINK_P (decl
)
18135 || variable_concept_p (decl
)))
18136 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
18141 /* Check if DECL and ARGS form a partial-concept-id. If so,
18142 assign ID to the resulting constrained placeholder.
18144 Returns true if the partial-concept-id designates a placeholder
18145 and false otherwise. Note that *id is set to NULL_TREE in
18149 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
18151 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
18154 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18166 Returns a TYPE_DECL for the type. */
18169 cp_parser_nonclass_name (cp_parser
* parser
)
18174 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18175 identifier
= cp_parser_identifier (parser
);
18176 if (identifier
== error_mark_node
)
18177 return error_mark_node
;
18179 /* Look up the type-name. */
18180 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18182 type_decl
= strip_using_decl (type_decl
);
18184 /* If we found an overload set, then it may refer to a concept-name. */
18185 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
18188 if (TREE_CODE (type_decl
) != TYPE_DECL
18189 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18191 /* See if this is an Objective-C type. */
18192 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18193 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18195 type_decl
= TYPE_NAME (type
);
18198 /* Issue an error if we did not find a type-name. */
18199 if (TREE_CODE (type_decl
) != TYPE_DECL
18200 /* In Objective-C, we have the complication that class names are
18201 normally type names and start declarations (eg, the
18202 "NSObject" in "NSObject *object;"), but can be used in an
18203 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18204 is an expression. So, a classname followed by a dot is not a
18205 valid type-name. */
18206 || (objc_is_class_name (TREE_TYPE (type_decl
))
18207 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18209 if (!cp_parser_simulate_error (parser
))
18210 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18211 NLE_TYPE
, token
->location
);
18212 return error_mark_node
;
18214 /* Remember that the name was used in the definition of the
18215 current class so that we can check later to see if the
18216 meaning would have been different after the class was
18217 entirely defined. */
18218 else if (type_decl
!= error_mark_node
18220 maybe_note_name_used_in_class (identifier
, type_decl
);
18225 /* Parse an elaborated-type-specifier. Note that the grammar given
18226 here incorporates the resolution to DR68.
18228 elaborated-type-specifier:
18229 class-key :: [opt] nested-name-specifier [opt] identifier
18230 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18231 enum-key :: [opt] nested-name-specifier [opt] identifier
18232 typename :: [opt] nested-name-specifier identifier
18233 typename :: [opt] nested-name-specifier template [opt]
18238 elaborated-type-specifier:
18239 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18240 class-key attributes :: [opt] nested-name-specifier [opt]
18241 template [opt] template-id
18242 enum attributes :: [opt] nested-name-specifier [opt] identifier
18244 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18245 declared `friend'. If IS_DECLARATION is TRUE, then this
18246 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18247 something is being declared.
18249 Returns the TYPE specified. */
18252 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18254 bool is_declaration
)
18256 enum tag_types tag_type
;
18258 tree type
= NULL_TREE
;
18259 tree attributes
= NULL_TREE
;
18261 cp_token
*token
= NULL
;
18263 /* See if we're looking at the `enum' keyword. */
18264 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18266 /* Consume the `enum' token. */
18267 cp_lexer_consume_token (parser
->lexer
);
18268 /* Remember that it's an enumeration type. */
18269 tag_type
= enum_type
;
18270 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18271 enums) is used here. */
18272 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18273 if (cp_parser_is_keyword (token
, RID_CLASS
)
18274 || cp_parser_is_keyword (token
, RID_STRUCT
))
18276 gcc_rich_location
richloc (token
->location
);
18277 richloc
.add_range (input_location
);
18278 richloc
.add_fixit_remove ();
18279 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18280 "a scoped enum must not use the %qD keyword",
18282 /* Consume the `struct' or `class' and parse it anyway. */
18283 cp_lexer_consume_token (parser
->lexer
);
18285 /* Parse the attributes. */
18286 attributes
= cp_parser_attributes_opt (parser
);
18288 /* Or, it might be `typename'. */
18289 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18292 /* Consume the `typename' token. */
18293 cp_lexer_consume_token (parser
->lexer
);
18294 /* Remember that it's a `typename' type. */
18295 tag_type
= typename_type
;
18297 /* Otherwise it must be a class-key. */
18300 tag_type
= cp_parser_class_key (parser
);
18301 if (tag_type
== none_type
)
18302 return error_mark_node
;
18303 /* Parse the attributes. */
18304 attributes
= cp_parser_attributes_opt (parser
);
18307 /* Look for the `::' operator. */
18308 globalscope
= cp_parser_global_scope_opt (parser
,
18309 /*current_scope_valid_p=*/false);
18310 /* Look for the nested-name-specifier. */
18311 tree nested_name_specifier
;
18312 if (tag_type
== typename_type
&& !globalscope
)
18314 nested_name_specifier
18315 = cp_parser_nested_name_specifier (parser
,
18316 /*typename_keyword_p=*/true,
18317 /*check_dependency_p=*/true,
18320 if (!nested_name_specifier
)
18321 return error_mark_node
;
18324 /* Even though `typename' is not present, the proposed resolution
18325 to Core Issue 180 says that in `class A<T>::B', `B' should be
18326 considered a type-name, even if `A<T>' is dependent. */
18327 nested_name_specifier
18328 = cp_parser_nested_name_specifier_opt (parser
,
18329 /*typename_keyword_p=*/true,
18330 /*check_dependency_p=*/true,
18333 /* For everything but enumeration types, consider a template-id.
18334 For an enumeration type, consider only a plain identifier. */
18335 if (tag_type
!= enum_type
)
18337 bool template_p
= false;
18340 /* Allow the `template' keyword. */
18341 template_p
= cp_parser_optional_template_keyword (parser
);
18342 /* If we didn't see `template', we don't know if there's a
18343 template-id or not. */
18345 cp_parser_parse_tentatively (parser
);
18346 /* The `template' keyword must follow a nested-name-specifier. */
18347 else if (!nested_name_specifier
)
18349 cp_parser_error (parser
, "%<template%> must follow a nested-"
18351 return error_mark_node
;
18354 /* Parse the template-id. */
18355 token
= cp_lexer_peek_token (parser
->lexer
);
18356 decl
= cp_parser_template_id (parser
, template_p
,
18357 /*check_dependency_p=*/true,
18360 /* If we didn't find a template-id, look for an ordinary
18362 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18364 /* We can get here when cp_parser_template_id, called by
18365 cp_parser_class_name with tag_type == none_type, succeeds
18366 and caches a BASELINK. Then, when called again here,
18367 instead of failing and returning an error_mark_node
18368 returns it (see template/typename17.C in C++11).
18369 ??? Could we diagnose this earlier? */
18370 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18372 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18373 type
= error_mark_node
;
18375 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18376 in effect, then we must assume that, upon instantiation, the
18377 template will correspond to a class. */
18378 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18379 && tag_type
== typename_type
)
18380 type
= make_typename_type (parser
->scope
, decl
,
18382 /*complain=*/tf_error
);
18383 /* If the `typename' keyword is in effect and DECL is not a type
18384 decl, then type is non existent. */
18385 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18387 else if (TREE_CODE (decl
) == TYPE_DECL
)
18389 type
= check_elaborated_type_specifier (tag_type
, decl
,
18390 /*allow_template_p=*/true);
18392 /* If the next token is a semicolon, this must be a specialization,
18393 instantiation, or friend declaration. Check the scope while we
18394 still know whether or not we had a nested-name-specifier. */
18395 if (type
!= error_mark_node
18396 && !nested_name_specifier
&& !is_friend
18397 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18398 check_unqualified_spec_or_inst (type
, token
->location
);
18400 else if (decl
== error_mark_node
)
18401 type
= error_mark_node
;
18406 token
= cp_lexer_peek_token (parser
->lexer
);
18407 identifier
= cp_parser_identifier (parser
);
18409 if (identifier
== error_mark_node
)
18411 parser
->scope
= NULL_TREE
;
18412 return error_mark_node
;
18415 /* For a `typename', we needn't call xref_tag. */
18416 if (tag_type
== typename_type
18417 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18418 return cp_parser_make_typename_type (parser
, identifier
,
18421 /* Template parameter lists apply only if we are not within a
18422 function parameter list. */
18423 bool template_parm_lists_apply
18424 = parser
->num_template_parameter_lists
;
18425 if (template_parm_lists_apply
)
18426 for (cp_binding_level
*s
= current_binding_level
;
18427 s
&& s
->kind
!= sk_template_parms
;
18428 s
= s
->level_chain
)
18429 if (s
->kind
== sk_function_parms
)
18430 template_parm_lists_apply
= false;
18432 /* Look up a qualified name in the usual way. */
18436 tree ambiguous_decls
;
18438 decl
= cp_parser_lookup_name (parser
, identifier
,
18440 /*is_template=*/false,
18441 /*is_namespace=*/false,
18442 /*check_dependency=*/true,
18446 /* If the lookup was ambiguous, an error will already have been
18448 if (ambiguous_decls
)
18449 return error_mark_node
;
18451 /* If we are parsing friend declaration, DECL may be a
18452 TEMPLATE_DECL tree node here. However, we need to check
18453 whether this TEMPLATE_DECL results in valid code. Consider
18454 the following example:
18457 template <class T> class C {};
18460 template <class T> friend class N::C; // #1, valid code
18462 template <class T> class Y {
18463 friend class N::C; // #2, invalid code
18466 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18467 name lookup of `N::C'. We see that friend declaration must
18468 be template for the code to be valid. Note that
18469 processing_template_decl does not work here since it is
18470 always 1 for the above two cases. */
18472 decl
= (cp_parser_maybe_treat_template_as_class
18473 (decl
, /*tag_name_p=*/is_friend
18474 && template_parm_lists_apply
));
18476 if (TREE_CODE (decl
) != TYPE_DECL
)
18478 cp_parser_diagnose_invalid_type_name (parser
,
18481 return error_mark_node
;
18484 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18486 bool allow_template
= (template_parm_lists_apply
18487 || DECL_SELF_REFERENCE_P (decl
));
18488 type
= check_elaborated_type_specifier (tag_type
, decl
,
18491 if (type
== error_mark_node
)
18492 return error_mark_node
;
18495 /* Forward declarations of nested types, such as
18500 are invalid unless all components preceding the final '::'
18501 are complete. If all enclosing types are complete, these
18502 declarations become merely pointless.
18504 Invalid forward declarations of nested types are errors
18505 caught elsewhere in parsing. Those that are pointless arrive
18508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18509 && !is_friend
&& !processing_explicit_instantiation
)
18510 warning (0, "declaration %qD does not declare anything", decl
);
18512 type
= TREE_TYPE (decl
);
18516 /* An elaborated-type-specifier sometimes introduces a new type and
18517 sometimes names an existing type. Normally, the rule is that it
18518 introduces a new type only if there is not an existing type of
18519 the same name already in scope. For example, given:
18522 void f() { struct S s; }
18524 the `struct S' in the body of `f' is the same `struct S' as in
18525 the global scope; the existing definition is used. However, if
18526 there were no global declaration, this would introduce a new
18527 local class named `S'.
18529 An exception to this rule applies to the following code:
18531 namespace N { struct S; }
18533 Here, the elaborated-type-specifier names a new type
18534 unconditionally; even if there is already an `S' in the
18535 containing scope this declaration names a new type.
18536 This exception only applies if the elaborated-type-specifier
18537 forms the complete declaration:
18541 A declaration consisting solely of `class-key identifier ;' is
18542 either a redeclaration of the name in the current scope or a
18543 forward declaration of the identifier as a class name. It
18544 introduces the name into the current scope.
18546 We are in this situation precisely when the next token is a `;'.
18548 An exception to the exception is that a `friend' declaration does
18549 *not* name a new type; i.e., given:
18551 struct S { friend struct T; };
18553 `T' is not a new type in the scope of `S'.
18555 Also, `new struct S' or `sizeof (struct S)' never results in the
18556 definition of a new type; a new type can only be declared in a
18557 declaration context. */
18563 /* Friends have special name lookup rules. */
18564 ts
= ts_within_enclosing_non_class
;
18565 else if (is_declaration
18566 && cp_lexer_next_token_is (parser
->lexer
,
18568 /* This is a `class-key identifier ;' */
18574 (template_parm_lists_apply
18575 && (cp_parser_next_token_starts_class_definition_p (parser
)
18576 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18577 /* An unqualified name was used to reference this type, so
18578 there were no qualifying templates. */
18579 if (template_parm_lists_apply
18580 && !cp_parser_check_template_parameters (parser
,
18581 /*num_templates=*/0,
18582 /*template_id*/false,
18584 /*declarator=*/NULL
))
18585 return error_mark_node
;
18586 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18590 if (type
== error_mark_node
)
18591 return error_mark_node
;
18593 /* Allow attributes on forward declarations of classes. */
18596 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18597 warning (OPT_Wattributes
,
18598 "attributes ignored on uninstantiated type");
18599 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18600 && ! processing_explicit_instantiation
)
18601 warning (OPT_Wattributes
,
18602 "attributes ignored on template instantiation");
18603 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18604 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18606 warning (OPT_Wattributes
,
18607 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18610 if (tag_type
!= enum_type
)
18612 /* Indicate whether this class was declared as a `class' or as a
18614 if (CLASS_TYPE_P (type
))
18615 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18616 cp_parser_check_class_key (tag_type
, type
);
18619 /* A "<" cannot follow an elaborated type specifier. If that
18620 happens, the user was probably trying to form a template-id. */
18621 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18627 /* Parse an enum-specifier.
18630 enum-head { enumerator-list [opt] }
18631 enum-head { enumerator-list , } [C++0x]
18634 enum-key identifier [opt] enum-base [opt]
18635 enum-key nested-name-specifier identifier enum-base [opt]
18640 enum struct [C++0x]
18643 : type-specifier-seq
18645 opaque-enum-specifier:
18646 enum-key identifier enum-base [opt] ;
18649 enum-key attributes[opt] identifier [opt] enum-base [opt]
18650 { enumerator-list [opt] }attributes[opt]
18651 enum-key attributes[opt] identifier [opt] enum-base [opt]
18652 { enumerator-list, }attributes[opt] [C++0x]
18654 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18655 if the token stream isn't an enum-specifier after all. */
18658 cp_parser_enum_specifier (cp_parser
* parser
)
18661 tree type
= NULL_TREE
;
18663 tree nested_name_specifier
= NULL_TREE
;
18665 bool scoped_enum_p
= false;
18666 bool has_underlying_type
= false;
18667 bool nested_being_defined
= false;
18668 bool new_value_list
= false;
18669 bool is_new_type
= false;
18670 bool is_unnamed
= false;
18671 tree underlying_type
= NULL_TREE
;
18672 cp_token
*type_start_token
= NULL
;
18673 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18675 parser
->colon_corrects_to_scope_p
= false;
18677 /* Parse tentatively so that we can back up if we don't find a
18679 cp_parser_parse_tentatively (parser
);
18681 /* Caller guarantees that the current token is 'enum', an identifier
18682 possibly follows, and the token after that is an opening brace.
18683 If we don't have an identifier, fabricate an anonymous name for
18684 the enumeration being defined. */
18685 cp_lexer_consume_token (parser
->lexer
);
18687 /* Parse the "class" or "struct", which indicates a scoped
18688 enumeration type in C++0x. */
18689 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18690 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18692 if (cxx_dialect
< cxx11
)
18693 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18695 /* Consume the `struct' or `class' token. */
18696 cp_lexer_consume_token (parser
->lexer
);
18698 scoped_enum_p
= true;
18701 attributes
= cp_parser_attributes_opt (parser
);
18703 /* Clear the qualification. */
18704 parser
->scope
= NULL_TREE
;
18705 parser
->qualifying_scope
= NULL_TREE
;
18706 parser
->object_scope
= NULL_TREE
;
18708 /* Figure out in what scope the declaration is being placed. */
18709 prev_scope
= current_scope ();
18711 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18713 push_deferring_access_checks (dk_no_check
);
18714 nested_name_specifier
18715 = cp_parser_nested_name_specifier_opt (parser
,
18716 /*typename_keyword_p=*/true,
18717 /*check_dependency_p=*/false,
18719 /*is_declaration=*/false);
18721 if (nested_name_specifier
)
18725 identifier
= cp_parser_identifier (parser
);
18726 name
= cp_parser_lookup_name (parser
, identifier
,
18728 /*is_template=*/false,
18729 /*is_namespace=*/false,
18730 /*check_dependency=*/true,
18731 /*ambiguous_decls=*/NULL
,
18733 if (name
&& name
!= error_mark_node
)
18735 type
= TREE_TYPE (name
);
18736 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18738 /* Are template enums allowed in ISO? */
18739 if (template_parm_scope_p ())
18740 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18741 "%qD is an enumeration template", name
);
18742 /* ignore a typename reference, for it will be solved by name
18747 else if (nested_name_specifier
== error_mark_node
)
18748 /* We already issued an error. */;
18751 error_at (type_start_token
->location
,
18752 "%qD does not name an enumeration in %qT",
18753 identifier
, nested_name_specifier
);
18754 nested_name_specifier
= error_mark_node
;
18759 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18760 identifier
= cp_parser_identifier (parser
);
18763 identifier
= make_anon_name ();
18766 error_at (type_start_token
->location
,
18767 "unnamed scoped enum is not allowed");
18770 pop_deferring_access_checks ();
18772 /* Check for the `:' that denotes a specified underlying type in C++0x.
18773 Note that a ':' could also indicate a bitfield width, however. */
18774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18776 cp_decl_specifier_seq type_specifiers
;
18778 /* Consume the `:'. */
18779 cp_lexer_consume_token (parser
->lexer
);
18781 /* Parse the type-specifier-seq. */
18782 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
18783 /*is_declaration=*/false,
18784 /*is_trailing_return=*/false,
18787 /* At this point this is surely not elaborated type specifier. */
18788 if (!cp_parser_parse_definitely (parser
))
18791 if (cxx_dialect
< cxx11
)
18792 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18794 has_underlying_type
= true;
18796 /* If that didn't work, stop. */
18797 if (type_specifiers
.type
!= error_mark_node
)
18799 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18800 /*initialized=*/0, NULL
);
18801 if (underlying_type
== error_mark_node
18802 || check_for_bare_parameter_packs (underlying_type
))
18803 underlying_type
= NULL_TREE
;
18807 /* Look for the `{' but don't consume it yet. */
18808 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18810 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18812 cp_parser_error (parser
, "expected %<{%>");
18813 if (has_underlying_type
)
18819 /* An opaque-enum-specifier must have a ';' here. */
18820 if ((scoped_enum_p
|| underlying_type
)
18821 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18823 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18824 if (has_underlying_type
)
18832 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18835 if (nested_name_specifier
)
18837 if (CLASS_TYPE_P (nested_name_specifier
))
18839 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18840 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18841 push_scope (nested_name_specifier
);
18843 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18845 push_nested_namespace (nested_name_specifier
);
18849 /* Issue an error message if type-definitions are forbidden here. */
18850 if (!cp_parser_check_type_definition (parser
))
18851 type
= error_mark_node
;
18853 /* Create the new type. We do this before consuming the opening
18854 brace so the enum will be recorded as being on the line of its
18855 tag (or the 'enum' keyword, if there is no tag). */
18856 type
= start_enum (identifier
, type
, underlying_type
,
18857 attributes
, scoped_enum_p
, &is_new_type
);
18859 /* If the next token is not '{' it is an opaque-enum-specifier or an
18860 elaborated-type-specifier. */
18861 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18863 timevar_push (TV_PARSE_ENUM
);
18864 if (nested_name_specifier
18865 && nested_name_specifier
!= error_mark_node
)
18867 /* The following catches invalid code such as:
18868 enum class S<int>::E { A, B, C }; */
18869 if (!processing_specialization
18870 && CLASS_TYPE_P (nested_name_specifier
)
18871 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18872 error_at (type_start_token
->location
, "cannot add an enumerator "
18873 "list to a template instantiation");
18875 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18877 error_at (type_start_token
->location
,
18878 "%<%T::%E%> has not been declared",
18879 TYPE_CONTEXT (nested_name_specifier
),
18880 nested_name_specifier
);
18881 type
= error_mark_node
;
18883 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18884 && !CLASS_TYPE_P (nested_name_specifier
))
18886 error_at (type_start_token
->location
, "nested name specifier "
18887 "%qT for enum declaration does not name a class "
18888 "or namespace", nested_name_specifier
);
18889 type
= error_mark_node
;
18891 /* If that scope does not contain the scope in which the
18892 class was originally declared, the program is invalid. */
18893 else if (prev_scope
&& !is_ancestor (prev_scope
,
18894 nested_name_specifier
))
18896 if (at_namespace_scope_p ())
18897 error_at (type_start_token
->location
,
18898 "declaration of %qD in namespace %qD which does not "
18900 type
, prev_scope
, nested_name_specifier
);
18902 error_at (type_start_token
->location
,
18903 "declaration of %qD in %qD which does not "
18905 type
, prev_scope
, nested_name_specifier
);
18906 type
= error_mark_node
;
18908 /* If that scope is the scope where the declaration is being placed
18909 the program is invalid. */
18910 else if (CLASS_TYPE_P (nested_name_specifier
)
18911 && CLASS_TYPE_P (prev_scope
)
18912 && same_type_p (nested_name_specifier
, prev_scope
))
18914 permerror (type_start_token
->location
,
18915 "extra qualification not allowed");
18916 nested_name_specifier
= NULL_TREE
;
18921 begin_scope (sk_scoped_enum
, type
);
18923 /* Consume the opening brace. */
18924 matching_braces braces
;
18925 braces
.consume_open (parser
);
18927 if (type
== error_mark_node
)
18928 ; /* Nothing to add */
18929 else if (OPAQUE_ENUM_P (type
)
18930 || (cxx_dialect
> cxx98
&& processing_specialization
))
18932 new_value_list
= true;
18933 SET_OPAQUE_ENUM_P (type
, false);
18934 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18938 error_at (type_start_token
->location
,
18939 "multiple definition of %q#T", type
);
18940 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18941 "previous definition here");
18942 type
= error_mark_node
;
18945 if (type
== error_mark_node
)
18946 cp_parser_skip_to_end_of_block_or_statement (parser
);
18947 /* If the next token is not '}', then there are some enumerators. */
18948 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18950 if (is_unnamed
&& !scoped_enum_p
)
18951 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18952 "ISO C++ forbids empty unnamed enum");
18955 cp_parser_enumerator_list (parser
, type
);
18957 /* Consume the final '}'. */
18958 braces
.require_close (parser
);
18962 timevar_pop (TV_PARSE_ENUM
);
18966 /* If a ';' follows, then it is an opaque-enum-specifier
18967 and additional restrictions apply. */
18968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18971 error_at (type_start_token
->location
,
18972 "opaque-enum-specifier without name");
18973 else if (nested_name_specifier
)
18974 error_at (type_start_token
->location
,
18975 "opaque-enum-specifier must use a simple identifier");
18979 /* Look for trailing attributes to apply to this enumeration, and
18980 apply them if appropriate. */
18981 if (cp_parser_allow_gnu_extensions_p (parser
))
18983 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18984 cplus_decl_attributes (&type
,
18986 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18989 /* Finish up the enumeration. */
18990 if (type
!= error_mark_node
)
18992 if (new_value_list
)
18993 finish_enum_value_list (type
);
18995 finish_enum (type
);
18998 if (nested_name_specifier
)
19000 if (CLASS_TYPE_P (nested_name_specifier
))
19002 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
19003 pop_scope (nested_name_specifier
);
19005 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19007 pop_nested_namespace (nested_name_specifier
);
19011 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19015 /* Parse an enumerator-list. The enumerators all have the indicated
19019 enumerator-definition
19020 enumerator-list , enumerator-definition */
19023 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
19027 /* Parse an enumerator-definition. */
19028 cp_parser_enumerator_definition (parser
, type
);
19030 /* If the next token is not a ',', we've reached the end of
19032 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19034 /* Otherwise, consume the `,' and keep going. */
19035 cp_lexer_consume_token (parser
->lexer
);
19036 /* If the next token is a `}', there is a trailing comma. */
19037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19039 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
19040 pedwarn (input_location
, OPT_Wpedantic
,
19041 "comma at end of enumerator list");
19047 /* Parse an enumerator-definition. The enumerator has the indicated
19050 enumerator-definition:
19052 enumerator = constant-expression
19059 enumerator-definition:
19060 enumerator attributes [opt]
19061 enumerator attributes [opt] = constant-expression */
19064 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
19070 /* Save the input location because we are interested in the location
19071 of the identifier and not the location of the explicit value. */
19072 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19074 /* Look for the identifier. */
19075 identifier
= cp_parser_identifier (parser
);
19076 if (identifier
== error_mark_node
)
19079 /* Parse any specified attributes. */
19080 tree attrs
= cp_parser_attributes_opt (parser
);
19082 /* If the next token is an '=', then there is an explicit value. */
19083 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19085 /* Consume the `=' token. */
19086 cp_lexer_consume_token (parser
->lexer
);
19087 /* Parse the value. */
19088 value
= cp_parser_constant_expression (parser
);
19093 /* If we are processing a template, make sure the initializer of the
19094 enumerator doesn't contain any bare template parameter pack. */
19095 if (check_for_bare_parameter_packs (value
))
19096 value
= error_mark_node
;
19098 /* Create the enumerator. */
19099 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19102 /* Parse a namespace-name.
19105 original-namespace-name
19108 Returns the NAMESPACE_DECL for the namespace. */
19111 cp_parser_namespace_name (cp_parser
* parser
)
19114 tree namespace_decl
;
19116 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19118 /* Get the name of the namespace. */
19119 identifier
= cp_parser_identifier (parser
);
19120 if (identifier
== error_mark_node
)
19121 return error_mark_node
;
19123 /* Look up the identifier in the currently active scope. Look only
19124 for namespaces, due to:
19126 [basic.lookup.udir]
19128 When looking up a namespace-name in a using-directive or alias
19129 definition, only namespace names are considered.
19133 [basic.lookup.qual]
19135 During the lookup of a name preceding the :: scope resolution
19136 operator, object, function, and enumerator names are ignored.
19138 (Note that cp_parser_qualifying_entity only calls this
19139 function if the token after the name is the scope resolution
19141 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19143 /*is_template=*/false,
19144 /*is_namespace=*/true,
19145 /*check_dependency=*/true,
19146 /*ambiguous_decls=*/NULL
,
19148 /* If it's not a namespace, issue an error. */
19149 if (namespace_decl
== error_mark_node
19150 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19152 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19154 auto_diagnostic_group d
;
19156 if (namespace_decl
== error_mark_node
19157 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19158 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19161 if (const char *suggestion
= hint
.suggestion ())
19163 gcc_rich_location
richloc (token
->location
);
19164 richloc
.add_fixit_replace (suggestion
);
19165 error_at (&richloc
,
19166 "%qD is not a namespace-name; did you mean %qs?",
19167 identifier
, suggestion
);
19170 error_at (token
->location
, "%qD is not a namespace-name",
19174 cp_parser_error (parser
, "expected namespace-name");
19175 namespace_decl
= error_mark_node
;
19178 return namespace_decl
;
19181 /* Parse a namespace-definition.
19183 namespace-definition:
19184 named-namespace-definition
19185 unnamed-namespace-definition
19187 named-namespace-definition:
19188 original-namespace-definition
19189 extension-namespace-definition
19191 original-namespace-definition:
19192 namespace identifier { namespace-body }
19194 extension-namespace-definition:
19195 namespace original-namespace-name { namespace-body }
19197 unnamed-namespace-definition:
19198 namespace { namespace-body } */
19201 cp_parser_namespace_definition (cp_parser
* parser
)
19204 int nested_definition_count
= 0;
19206 cp_ensure_no_omp_declare_simd (parser
);
19207 cp_ensure_no_oacc_routine (parser
);
19209 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19210 const bool topmost_inline_p
= is_inline
;
19214 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19215 cp_lexer_consume_token (parser
->lexer
);
19218 /* Look for the `namespace' keyword. */
19220 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19222 /* Parse any specified attributes before the identifier. */
19223 tree attribs
= cp_parser_attributes_opt (parser
);
19227 identifier
= NULL_TREE
;
19229 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19231 if (nested_inline_p
&& nested_definition_count
!= 0)
19233 if (cxx_dialect
< cxx2a
)
19234 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19235 OPT_Wpedantic
, "nested inline namespace definitions only "
19236 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19237 cp_lexer_consume_token (parser
->lexer
);
19240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19242 identifier
= cp_parser_identifier (parser
);
19244 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19245 pedwarn (input_location
, OPT_Wpedantic
,
19246 "standard attributes on namespaces must precede "
19247 "the namespace name");
19249 /* Parse any attributes specified after the identifier. */
19250 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19253 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19255 /* Don't forget that the innermost namespace might have been
19256 marked as inline. Use |= because we cannot overwrite
19257 IS_INLINE in case the outermost namespace is inline, but
19258 there are no nested inlines. */
19259 is_inline
|= nested_inline_p
;
19263 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19264 pedwarn (input_location
, OPT_Wpedantic
,
19265 "nested namespace definitions only available with "
19266 "%<-std=c++17%> or %<-std=gnu++17%>");
19268 /* Nested namespace names can create new namespaces (unlike
19269 other qualified-ids). */
19270 if (int count
= (identifier
19271 ? push_namespace (identifier
, nested_inline_p
)
19273 nested_definition_count
+= count
;
19275 cp_parser_error (parser
, "nested namespace name required");
19276 cp_lexer_consume_token (parser
->lexer
);
19279 if (nested_definition_count
&& !identifier
)
19280 cp_parser_error (parser
, "namespace name required");
19282 if (nested_definition_count
&& attribs
)
19283 error_at (token
->location
,
19284 "a nested namespace definition cannot have attributes");
19285 if (nested_definition_count
&& topmost_inline_p
)
19286 error_at (token
->location
,
19287 "a nested namespace definition cannot be inline");
19289 /* Start the namespace. */
19290 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19292 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19294 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19296 /* Look for the `{' to validate starting the namespace. */
19297 matching_braces braces
;
19298 if (braces
.require_open (parser
))
19300 /* Parse the body of the namespace. */
19301 cp_parser_namespace_body (parser
);
19303 /* Look for the final `}'. */
19304 braces
.require_close (parser
);
19307 if (has_visibility
)
19308 pop_visibility (1);
19310 /* Pop the nested namespace definitions. */
19311 while (nested_definition_count
--)
19315 /* Parse a namespace-body.
19318 declaration-seq [opt] */
19321 cp_parser_namespace_body (cp_parser
* parser
)
19323 cp_parser_declaration_seq_opt (parser
);
19326 /* Parse a namespace-alias-definition.
19328 namespace-alias-definition:
19329 namespace identifier = qualified-namespace-specifier ; */
19332 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19335 tree namespace_specifier
;
19337 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19339 /* Look for the `namespace' keyword. */
19340 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19341 /* Look for the identifier. */
19342 identifier
= cp_parser_identifier (parser
);
19343 if (identifier
== error_mark_node
)
19345 /* Look for the `=' token. */
19346 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19347 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19349 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19350 /* Skip the definition. */
19351 cp_lexer_consume_token (parser
->lexer
);
19352 if (cp_parser_skip_to_closing_brace (parser
))
19353 cp_lexer_consume_token (parser
->lexer
);
19356 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19357 /* Look for the qualified-namespace-specifier. */
19358 namespace_specifier
19359 = cp_parser_qualified_namespace_specifier (parser
);
19360 /* Look for the `;' token. */
19361 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19363 /* Register the alias in the symbol table. */
19364 do_namespace_alias (identifier
, namespace_specifier
);
19367 /* Parse a qualified-namespace-specifier.
19369 qualified-namespace-specifier:
19370 :: [opt] nested-name-specifier [opt] namespace-name
19372 Returns a NAMESPACE_DECL corresponding to the specified
19376 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19378 /* Look for the optional `::'. */
19379 cp_parser_global_scope_opt (parser
,
19380 /*current_scope_valid_p=*/false);
19382 /* Look for the optional nested-name-specifier. */
19383 cp_parser_nested_name_specifier_opt (parser
,
19384 /*typename_keyword_p=*/false,
19385 /*check_dependency_p=*/true,
19387 /*is_declaration=*/true);
19389 return cp_parser_namespace_name (parser
);
19392 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19393 access declaration.
19396 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19397 using :: unqualified-id ;
19399 access-declaration:
19405 cp_parser_using_declaration (cp_parser
* parser
,
19406 bool access_declaration_p
)
19409 bool typename_p
= false;
19410 bool global_scope_p
;
19414 int oldcount
= errorcount
;
19415 cp_token
*diag_token
= NULL
;
19417 if (access_declaration_p
)
19419 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19420 cp_parser_parse_tentatively (parser
);
19424 /* Look for the `using' keyword. */
19425 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19428 /* Peek at the next token. */
19429 token
= cp_lexer_peek_token (parser
->lexer
);
19430 /* See if it's `typename'. */
19431 if (token
->keyword
== RID_TYPENAME
)
19433 /* Remember that we've seen it. */
19435 /* Consume the `typename' token. */
19436 cp_lexer_consume_token (parser
->lexer
);
19440 /* Look for the optional global scope qualification. */
19442 = (cp_parser_global_scope_opt (parser
,
19443 /*current_scope_valid_p=*/false)
19446 /* If we saw `typename', or didn't see `::', then there must be a
19447 nested-name-specifier present. */
19448 if (typename_p
|| !global_scope_p
)
19450 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19451 /*check_dependency_p=*/true,
19453 /*is_declaration=*/true);
19454 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19456 cp_parser_skip_to_end_of_block_or_statement (parser
);
19460 /* Otherwise, we could be in either of the two productions. In that
19461 case, treat the nested-name-specifier as optional. */
19463 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19464 /*typename_keyword_p=*/false,
19465 /*check_dependency_p=*/true,
19467 /*is_declaration=*/true);
19469 qscope
= global_namespace
;
19470 else if (UNSCOPED_ENUM_P (qscope
)
19471 && !TYPE_FUNCTION_SCOPE_P (qscope
))
19472 qscope
= CP_TYPE_CONTEXT (qscope
);
19474 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19475 /* Something has already gone wrong; there's no need to parse
19476 further. Since an error has occurred, the return value of
19477 cp_parser_parse_definitely will be false, as required. */
19478 return cp_parser_parse_definitely (parser
);
19480 token
= cp_lexer_peek_token (parser
->lexer
);
19481 /* Parse the unqualified-id. */
19482 identifier
= cp_parser_unqualified_id (parser
,
19483 /*template_keyword_p=*/false,
19484 /*check_dependency_p=*/true,
19485 /*declarator_p=*/true,
19486 /*optional_p=*/false);
19488 if (access_declaration_p
)
19490 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19491 cp_parser_simulate_error (parser
);
19492 if (!cp_parser_parse_definitely (parser
))
19495 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19497 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19498 if (cxx_dialect
< cxx17
19499 && !in_system_header_at (ell
->location
))
19500 pedwarn (ell
->location
, 0,
19501 "pack expansion in using-declaration only available "
19502 "with %<-std=c++17%> or %<-std=gnu++17%>");
19503 qscope
= make_pack_expansion (qscope
);
19506 /* The function we call to handle a using-declaration is different
19507 depending on what scope we are in. */
19508 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19510 else if (!identifier_p (identifier
)
19511 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19512 /* [namespace.udecl]
19514 A using declaration shall not name a template-id. */
19515 error_at (token
->location
,
19516 "a template-id may not appear in a using-declaration");
19519 if (at_class_scope_p ())
19521 /* Create the USING_DECL. */
19522 decl
= do_class_using_decl (qscope
, identifier
);
19524 if (decl
&& typename_p
)
19525 USING_DECL_TYPENAME_P (decl
) = 1;
19527 if (check_for_bare_parameter_packs (decl
))
19529 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19533 /* Add it to the list of members in this class. */
19534 finish_member_declaration (decl
);
19538 decl
= cp_parser_lookup_name_simple (parser
,
19541 if (decl
== error_mark_node
)
19542 cp_parser_name_lookup_error (parser
, identifier
,
19545 else if (check_for_bare_parameter_packs (decl
))
19547 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19550 else if (!at_namespace_scope_p ())
19551 finish_local_using_decl (decl
, qscope
, identifier
);
19553 finish_namespace_using_decl (decl
, qscope
, identifier
);
19557 if (!access_declaration_p
19558 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19560 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19561 if (cxx_dialect
< cxx17
)
19562 pedwarn (comma
->location
, 0,
19563 "comma-separated list in using-declaration only available "
19564 "with %<-std=c++17%> or %<-std=gnu++17%>");
19568 /* Look for the final `;'. */
19569 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19571 if (access_declaration_p
&& errorcount
== oldcount
)
19572 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19573 "access declarations are deprecated "
19574 "in favour of using-declarations; "
19575 "suggestion: add the %<using%> keyword");
19580 /* Parse an alias-declaration.
19583 using identifier attribute-specifier-seq [opt] = type-id */
19586 cp_parser_alias_declaration (cp_parser
* parser
)
19588 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19589 location_t id_location
, type_location
;
19590 cp_declarator
*declarator
;
19591 cp_decl_specifier_seq decl_specs
;
19593 const char *saved_message
= NULL
;
19595 /* Look for the `using' keyword. */
19596 cp_token
*using_token
19597 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19598 if (using_token
== NULL
)
19599 return error_mark_node
;
19601 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19602 id
= cp_parser_identifier (parser
);
19603 if (id
== error_mark_node
)
19604 return error_mark_node
;
19606 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19607 attributes
= cp_parser_attributes_opt (parser
);
19608 if (attributes
== error_mark_node
)
19609 return error_mark_node
;
19611 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19613 if (cp_parser_error_occurred (parser
))
19614 return error_mark_node
;
19616 cp_parser_commit_to_tentative_parse (parser
);
19618 /* Now we are going to parse the type-id of the declaration. */
19623 "A type-specifier-seq shall not define a class or enumeration
19624 unless it appears in the type-id of an alias-declaration (7.1.3) that
19625 is not the declaration of a template-declaration."
19627 In other words, if we currently are in an alias template, the
19628 type-id should not define a type.
19630 So let's set parser->type_definition_forbidden_message in that
19631 case; cp_parser_check_type_definition (called by
19632 cp_parser_class_specifier) will then emit an error if a type is
19633 defined in the type-id. */
19634 if (parser
->num_template_parameter_lists
)
19636 saved_message
= parser
->type_definition_forbidden_message
;
19637 parser
->type_definition_forbidden_message
=
19638 G_("types may not be defined in alias template declarations");
19641 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
19644 /* Restore the error message if need be. */
19645 if (parser
->num_template_parameter_lists
)
19646 parser
->type_definition_forbidden_message
= saved_message
;
19648 if (type
== error_mark_node
19649 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19651 cp_parser_skip_to_end_of_block_or_statement (parser
);
19652 return error_mark_node
;
19655 /* A typedef-name can also be introduced by an alias-declaration. The
19656 identifier following the using keyword becomes a typedef-name. It has
19657 the same semantics as if it were introduced by the typedef
19658 specifier. In particular, it does not define a new type and it shall
19659 not appear in the type-id. */
19661 clear_decl_specs (&decl_specs
);
19662 decl_specs
.type
= type
;
19663 if (attributes
!= NULL_TREE
)
19665 decl_specs
.attributes
= attributes
;
19666 set_and_check_decl_spec_loc (&decl_specs
,
19670 set_and_check_decl_spec_loc (&decl_specs
,
19673 set_and_check_decl_spec_loc (&decl_specs
,
19676 decl_specs
.locations
[ds_type_spec
] = type_location
;
19678 if (parser
->num_template_parameter_lists
19679 && !cp_parser_check_template_parameters (parser
,
19680 /*num_templates=*/0,
19681 /*template_id*/false,
19683 /*declarator=*/NULL
))
19684 return error_mark_node
;
19686 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19688 member_p
= at_class_scope_p ();
19690 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19691 NULL_TREE
, attributes
);
19693 decl
= start_decl (declarator
, &decl_specs
, 0,
19694 attributes
, NULL_TREE
, &pushed_scope
);
19695 if (decl
== error_mark_node
)
19698 // Attach constraints to the alias declaration.
19699 if (flag_concepts
&& current_template_parms
)
19701 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19702 tree constr
= build_constraints (reqs
, NULL_TREE
);
19703 set_constraints (decl
, constr
);
19706 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19709 pop_scope (pushed_scope
);
19711 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19712 added into the symbol table; otherwise, return the TYPE_DECL. */
19713 if (DECL_LANG_SPECIFIC (decl
)
19714 && DECL_TEMPLATE_INFO (decl
)
19715 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19717 decl
= DECL_TI_TEMPLATE (decl
);
19719 check_member_template (decl
);
19725 /* Parse a using-directive.
19728 using namespace :: [opt] nested-name-specifier [opt]
19729 namespace-name ; */
19732 cp_parser_using_directive (cp_parser
* parser
)
19734 tree namespace_decl
;
19737 /* Look for the `using' keyword. */
19738 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19739 /* And the `namespace' keyword. */
19740 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19741 /* Look for the optional `::' operator. */
19742 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19743 /* And the optional nested-name-specifier. */
19744 cp_parser_nested_name_specifier_opt (parser
,
19745 /*typename_keyword_p=*/false,
19746 /*check_dependency_p=*/true,
19748 /*is_declaration=*/true);
19749 /* Get the namespace being used. */
19750 namespace_decl
= cp_parser_namespace_name (parser
);
19751 /* And any specified attributes. */
19752 attribs
= cp_parser_attributes_opt (parser
);
19754 /* Update the symbol table. */
19755 if (namespace_bindings_p ())
19756 finish_namespace_using_directive (namespace_decl
, attribs
);
19758 finish_local_using_directive (namespace_decl
, attribs
);
19760 /* Look for the final `;'. */
19761 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19764 /* Parse an asm-definition.
19771 asm-qualifier-list:
19773 asm-qualifier-list asm-qualifier
19776 asm ( string-literal ) ;
19781 asm asm-qualifier-list [opt] ( string-literal ) ;
19782 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19783 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19784 : asm-operand-list [opt] ) ;
19785 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19786 : asm-operand-list [opt]
19787 : asm-clobber-list [opt] ) ;
19788 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19789 : asm-clobber-list [opt]
19790 : asm-goto-list ) ;
19792 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19793 contains goto, and is the only allowed form in that case. No duplicates are
19794 allowed in an asm-qualifier-list. */
19797 cp_parser_asm_definition (cp_parser
* parser
)
19800 tree outputs
= NULL_TREE
;
19801 tree inputs
= NULL_TREE
;
19802 tree clobbers
= NULL_TREE
;
19803 tree labels
= NULL_TREE
;
19805 bool extended_p
= false;
19806 bool invalid_inputs_p
= false;
19807 bool invalid_outputs_p
= false;
19808 required_token missing
= RT_NONE
;
19810 /* Look for the `asm' keyword. */
19811 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19813 if (parser
->in_function_body
19814 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19816 error ("%<asm%> in %<constexpr%> function");
19817 cp_function_chain
->invalid_constexpr
= true;
19820 /* Handle the asm-qualifier-list. */
19821 location_t volatile_loc
= UNKNOWN_LOCATION
;
19822 location_t inline_loc
= UNKNOWN_LOCATION
;
19823 location_t goto_loc
= UNKNOWN_LOCATION
;
19824 location_t first_loc
= UNKNOWN_LOCATION
;
19826 if (cp_parser_allow_gnu_extensions_p (parser
))
19829 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19830 location_t loc
= token
->location
;
19831 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
19836 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19837 inform (volatile_loc
, "first seen here");
19841 if (!parser
->in_function_body
)
19842 warning_at (loc
, 0, "asm qualifier %qT ignored outside of "
19843 "function body", token
->u
.value
);
19844 volatile_loc
= loc
;
19846 cp_lexer_consume_token (parser
->lexer
);
19852 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19853 inform (inline_loc
, "first seen here");
19859 cp_lexer_consume_token (parser
->lexer
);
19865 error_at (loc
, "duplicate asm qualifier %qT", token
->u
.value
);
19866 inform (goto_loc
, "first seen here");
19872 cp_lexer_consume_token (parser
->lexer
);
19877 error_at (loc
, "%qT is not an asm qualifier", token
->u
.value
);
19878 cp_lexer_consume_token (parser
->lexer
);
19887 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
19888 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
19889 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
19891 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
19893 error_at (first_loc
, "asm qualifier outside of function body");
19894 inline_p
= goto_p
= false;
19897 /* Look for the opening `('. */
19898 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19900 /* Look for the string. */
19901 string
= cp_parser_string_literal (parser
, false, false);
19902 if (string
== error_mark_node
)
19904 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19905 /*consume_paren=*/true);
19909 /* If we're allowing GNU extensions, check for the extended assembly
19910 syntax. Unfortunately, the `:' tokens need not be separated by
19911 a space in C, and so, for compatibility, we tolerate that here
19912 too. Doing that means that we have to treat the `::' operator as
19914 if (cp_parser_allow_gnu_extensions_p (parser
)
19915 && parser
->in_function_body
19916 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19917 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19919 bool inputs_p
= false;
19920 bool clobbers_p
= false;
19921 bool labels_p
= false;
19923 /* The extended syntax was used. */
19926 /* Look for outputs. */
19927 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19929 /* Consume the `:'. */
19930 cp_lexer_consume_token (parser
->lexer
);
19931 /* Parse the output-operands. */
19932 if (cp_lexer_next_token_is_not (parser
->lexer
,
19934 && cp_lexer_next_token_is_not (parser
->lexer
,
19936 && cp_lexer_next_token_is_not (parser
->lexer
,
19940 outputs
= cp_parser_asm_operand_list (parser
);
19941 if (outputs
== error_mark_node
)
19942 invalid_outputs_p
= true;
19945 /* If the next token is `::', there are no outputs, and the
19946 next token is the beginning of the inputs. */
19947 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19948 /* The inputs are coming next. */
19951 /* Look for inputs. */
19953 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19955 /* Consume the `:' or `::'. */
19956 cp_lexer_consume_token (parser
->lexer
);
19957 /* Parse the output-operands. */
19958 if (cp_lexer_next_token_is_not (parser
->lexer
,
19960 && cp_lexer_next_token_is_not (parser
->lexer
,
19962 && cp_lexer_next_token_is_not (parser
->lexer
,
19965 inputs
= cp_parser_asm_operand_list (parser
);
19966 if (inputs
== error_mark_node
)
19967 invalid_inputs_p
= true;
19970 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19971 /* The clobbers are coming next. */
19974 /* Look for clobbers. */
19976 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19979 /* Consume the `:' or `::'. */
19980 cp_lexer_consume_token (parser
->lexer
);
19981 /* Parse the clobbers. */
19982 if (cp_lexer_next_token_is_not (parser
->lexer
,
19984 && cp_lexer_next_token_is_not (parser
->lexer
,
19986 clobbers
= cp_parser_asm_clobber_list (parser
);
19988 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19989 /* The labels are coming next. */
19992 /* Look for labels. */
19994 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19997 /* Consume the `:' or `::'. */
19998 cp_lexer_consume_token (parser
->lexer
);
19999 /* Parse the labels. */
20000 labels
= cp_parser_asm_label_list (parser
);
20003 if (goto_p
&& !labels_p
)
20004 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
20007 missing
= RT_COLON_SCOPE
;
20009 /* Look for the closing `)'. */
20010 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
20011 missing
? missing
: RT_CLOSE_PAREN
))
20012 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
20013 /*consume_paren=*/true);
20014 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20016 if (!invalid_inputs_p
&& !invalid_outputs_p
)
20018 /* Create the ASM_EXPR. */
20019 if (parser
->in_function_body
)
20021 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
20022 inputs
, clobbers
, labels
, inline_p
);
20023 /* If the extended syntax was not used, mark the ASM_EXPR. */
20026 tree temp
= asm_stmt
;
20027 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
20028 temp
= TREE_OPERAND (temp
, 0);
20030 ASM_INPUT_P (temp
) = 1;
20034 symtab
->finalize_toplevel_asm (string
);
20038 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20039 type that comes from the decl-specifier-seq. */
20042 strip_declarator_types (tree type
, cp_declarator
*declarator
)
20044 for (cp_declarator
*d
= declarator
; d
;)
20054 if (TYPE_PTRMEMFUNC_P (type
))
20055 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
20056 type
= TREE_TYPE (type
);
20064 /* Declarators [gram.dcl.decl] */
20066 /* Parse an init-declarator.
20069 declarator initializer [opt]
20074 declarator asm-specification [opt] attributes [opt] initializer [opt]
20076 function-definition:
20077 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20079 decl-specifier-seq [opt] declarator function-try-block
20083 function-definition:
20084 __extension__ function-definition
20088 function-definition:
20089 decl-specifier-seq [opt] declarator function-transaction-block
20091 The parser flags FLAGS is used to control type-specifier parsing.
20093 The DECL_SPECIFIERS apply to this declarator. Returns a
20094 representation of the entity declared. If MEMBER_P is TRUE, then
20095 this declarator appears in a class scope. The new DECL created by
20096 this declarator is returned.
20098 The CHECKS are access checks that should be performed once we know
20099 what entity is being declared (and, therefore, what classes have
20102 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20103 for a function-definition here as well. If the declarator is a
20104 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20105 be TRUE upon return. By that point, the function-definition will
20106 have been completely parsed.
20108 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20111 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20112 parsed declaration if it is an uninitialized single declarator not followed
20113 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20114 if present, will not be consumed. If returned, this declarator will be
20115 created with SD_INITIALIZED but will not call cp_finish_decl.
20117 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20118 and there is an initializer, the pointed location_t is set to the
20119 location of the '=' or `(', or '{' in C++11 token introducing the
20123 cp_parser_init_declarator (cp_parser
* parser
,
20124 cp_parser_flags flags
,
20125 cp_decl_specifier_seq
*decl_specifiers
,
20126 vec
<deferred_access_check
, va_gc
> *checks
,
20127 bool function_definition_allowed_p
,
20129 int declares_class_or_enum
,
20130 bool* function_definition_p
,
20131 tree
* maybe_range_for_decl
,
20132 location_t
* init_loc
,
20135 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20136 *attributes_start_token
= NULL
;
20137 cp_declarator
*declarator
;
20138 tree prefix_attributes
;
20139 tree attributes
= NULL
;
20140 tree asm_specification
;
20142 tree decl
= NULL_TREE
;
20144 int is_initialized
;
20145 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20146 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20148 enum cpp_ttype initialization_kind
;
20149 bool is_direct_init
= false;
20150 bool is_non_constant_init
;
20151 int ctor_dtor_or_conv_p
;
20152 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20153 tree pushed_scope
= NULL_TREE
;
20154 bool range_for_decl_p
= false;
20155 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20156 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20158 /* Gather the attributes that were provided with the
20159 decl-specifiers. */
20160 prefix_attributes
= decl_specifiers
->attributes
;
20162 /* Assume that this is not the declarator for a function
20164 if (function_definition_p
)
20165 *function_definition_p
= false;
20167 /* Default arguments are only permitted for function parameters. */
20168 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20169 parser
->default_arg_ok_p
= false;
20171 /* Defer access checks while parsing the declarator; we cannot know
20172 what names are accessible until we know what is being
20174 resume_deferring_access_checks ();
20176 token
= cp_lexer_peek_token (parser
->lexer
);
20178 /* Parse the declarator. */
20180 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20181 flags
, &ctor_dtor_or_conv_p
,
20182 /*parenthesized_p=*/NULL
,
20183 member_p
, friend_p
, /*static_p=*/false);
20184 /* Gather up the deferred checks. */
20185 stop_deferring_access_checks ();
20187 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20189 /* If the DECLARATOR was erroneous, there's no need to go
20191 if (declarator
== cp_error_declarator
)
20192 return error_mark_node
;
20194 /* Check that the number of template-parameter-lists is OK. */
20195 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20197 return error_mark_node
;
20199 if (declares_class_or_enum
& 2)
20200 cp_parser_check_for_definition_in_return_type (declarator
,
20201 decl_specifiers
->type
,
20202 decl_specifiers
->locations
[ds_type_spec
]);
20204 /* Figure out what scope the entity declared by the DECLARATOR is
20205 located in. `grokdeclarator' sometimes changes the scope, so
20206 we compute it now. */
20207 scope
= get_scope_of_declarator (declarator
);
20209 /* Perform any lookups in the declared type which were thought to be
20210 dependent, but are not in the scope of the declarator. */
20211 decl_specifiers
->type
20212 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20214 /* If we're allowing GNU extensions, look for an
20215 asm-specification. */
20216 if (cp_parser_allow_gnu_extensions_p (parser
))
20218 /* Look for an asm-specification. */
20219 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20220 asm_specification
= cp_parser_asm_specification_opt (parser
);
20223 asm_specification
= NULL_TREE
;
20225 /* Look for attributes. */
20226 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20227 attributes
= cp_parser_attributes_opt (parser
);
20229 /* Peek at the next token. */
20230 token
= cp_lexer_peek_token (parser
->lexer
);
20232 bool bogus_implicit_tmpl
= false;
20234 if (function_declarator_p (declarator
))
20236 /* Handle C++17 deduction guides. */
20237 if (!decl_specifiers
->type
20238 && ctor_dtor_or_conv_p
<= 0
20239 && cxx_dialect
>= cxx17
)
20241 cp_declarator
*id
= get_id_declarator (declarator
);
20242 tree name
= id
->u
.id
.unqualified_name
;
20243 parser
->scope
= id
->u
.id
.qualifying_scope
;
20244 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20246 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20247 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20249 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20250 id
->u
.id
.sfk
= sfk_deduction_guide
;
20251 ctor_dtor_or_conv_p
= 1;
20255 /* Check to see if the token indicates the start of a
20256 function-definition. */
20257 if (cp_parser_token_starts_function_definition_p (token
))
20259 if (!function_definition_allowed_p
)
20261 /* If a function-definition should not appear here, issue an
20263 cp_parser_error (parser
,
20264 "a function-definition is not allowed here");
20265 return error_mark_node
;
20268 location_t func_brace_location
20269 = cp_lexer_peek_token (parser
->lexer
)->location
;
20271 /* Neither attributes nor an asm-specification are allowed
20272 on a function-definition. */
20273 if (asm_specification
)
20274 error_at (asm_spec_start_token
->location
,
20275 "an asm-specification is not allowed "
20276 "on a function-definition");
20278 error_at (attributes_start_token
->location
,
20279 "attributes are not allowed "
20280 "on a function-definition");
20281 /* This is a function-definition. */
20282 *function_definition_p
= true;
20284 /* Parse the function definition. */
20286 decl
= cp_parser_save_member_function_body (parser
,
20289 prefix_attributes
);
20292 (cp_parser_function_definition_from_specifiers_and_declarator
20293 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20295 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20297 /* This is where the prologue starts... */
20298 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20299 = func_brace_location
;
20305 else if (parser
->fully_implicit_function_template_p
)
20307 /* A non-template declaration involving a function parameter list
20308 containing an implicit template parameter will be made into a
20309 template. If the resulting declaration is not going to be an
20310 actual function then finish the template scope here to prevent it.
20311 An error message will be issued once we have a decl to talk about.
20313 FIXME probably we should do type deduction rather than create an
20314 implicit template, but the standard currently doesn't allow it. */
20315 bogus_implicit_tmpl
= true;
20316 finish_fully_implicit_template (parser
, NULL_TREE
);
20321 Only in function declarations for constructors, destructors, type
20322 conversions, and deduction guides can the decl-specifier-seq be omitted.
20324 We explicitly postpone this check past the point where we handle
20325 function-definitions because we tolerate function-definitions
20326 that are missing their return types in some modes. */
20327 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20329 cp_parser_error (parser
,
20330 "expected constructor, destructor, or type conversion");
20331 return error_mark_node
;
20334 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20335 if (token
->type
== CPP_EQ
20336 || token
->type
== CPP_OPEN_PAREN
20337 || token
->type
== CPP_OPEN_BRACE
)
20339 is_initialized
= SD_INITIALIZED
;
20340 initialization_kind
= token
->type
;
20341 if (maybe_range_for_decl
)
20342 *maybe_range_for_decl
= error_mark_node
;
20343 tmp_init_loc
= token
->location
;
20344 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20345 *init_loc
= tmp_init_loc
;
20347 if (token
->type
== CPP_EQ
20348 && function_declarator_p (declarator
))
20350 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20351 if (t2
->keyword
== RID_DEFAULT
)
20352 is_initialized
= SD_DEFAULTED
;
20353 else if (t2
->keyword
== RID_DELETE
)
20354 is_initialized
= SD_DELETED
;
20359 /* If the init-declarator isn't initialized and isn't followed by a
20360 `,' or `;', it's not a valid init-declarator. */
20361 if (token
->type
!= CPP_COMMA
20362 && token
->type
!= CPP_SEMICOLON
)
20364 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20365 range_for_decl_p
= true;
20368 if (!maybe_range_for_decl
)
20369 cp_parser_error (parser
, "expected initializer");
20370 return error_mark_node
;
20373 is_initialized
= SD_UNINITIALIZED
;
20374 initialization_kind
= CPP_EOF
;
20377 /* Because start_decl has side-effects, we should only call it if we
20378 know we're going ahead. By this point, we know that we cannot
20379 possibly be looking at any other construct. */
20380 cp_parser_commit_to_tentative_parse (parser
);
20382 /* Enter the newly declared entry in the symbol table. If we're
20383 processing a declaration in a class-specifier, we wait until
20384 after processing the initializer. */
20387 if (parser
->in_unbraced_linkage_specification_p
)
20388 decl_specifiers
->storage_class
= sc_extern
;
20389 decl
= start_decl (declarator
, decl_specifiers
,
20390 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20391 attributes
, prefix_attributes
, &pushed_scope
);
20392 cp_finalize_omp_declare_simd (parser
, decl
);
20393 cp_finalize_oacc_routine (parser
, decl
, false);
20394 /* Adjust location of decl if declarator->id_loc is more appropriate:
20395 set, and decl wasn't merged with another decl, in which case its
20396 location would be different from input_location, and more accurate. */
20398 && declarator
->id_loc
!= UNKNOWN_LOCATION
20399 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20400 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20403 /* Enter the SCOPE. That way unqualified names appearing in the
20404 initializer will be looked up in SCOPE. */
20405 pushed_scope
= push_scope (scope
);
20407 /* Perform deferred access control checks, now that we know in which
20408 SCOPE the declared entity resides. */
20409 if (!member_p
&& decl
)
20411 tree saved_current_function_decl
= NULL_TREE
;
20413 /* If the entity being declared is a function, pretend that we
20414 are in its scope. If it is a `friend', it may have access to
20415 things that would not otherwise be accessible. */
20416 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20418 saved_current_function_decl
= current_function_decl
;
20419 current_function_decl
= decl
;
20422 /* Perform access checks for template parameters. */
20423 cp_parser_perform_template_parameter_access_checks (checks
);
20425 /* Perform the access control checks for the declarator and the
20426 decl-specifiers. */
20427 perform_deferred_access_checks (tf_warning_or_error
);
20429 /* Restore the saved value. */
20430 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20431 current_function_decl
= saved_current_function_decl
;
20434 /* Parse the initializer. */
20435 initializer
= NULL_TREE
;
20436 is_direct_init
= false;
20437 is_non_constant_init
= true;
20438 if (is_initialized
)
20440 if (function_declarator_p (declarator
))
20442 if (initialization_kind
== CPP_EQ
)
20443 initializer
= cp_parser_pure_specifier (parser
);
20446 /* If the declaration was erroneous, we don't really
20447 know what the user intended, so just silently
20448 consume the initializer. */
20449 if (decl
!= error_mark_node
)
20450 error_at (tmp_init_loc
, "initializer provided for function");
20451 cp_parser_skip_to_closing_parenthesis (parser
,
20452 /*recovering=*/true,
20453 /*or_comma=*/false,
20454 /*consume_paren=*/true);
20459 /* We want to record the extra mangling scope for in-class
20460 initializers of class members and initializers of static data
20461 member templates. The former involves deferring
20462 parsing of the initializer until end of class as with default
20463 arguments. So right here we only handle the latter. */
20464 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20465 start_lambda_scope (decl
);
20466 initializer
= cp_parser_initializer (parser
,
20468 &is_non_constant_init
);
20469 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20470 finish_lambda_scope ();
20471 if (initializer
== error_mark_node
)
20472 cp_parser_skip_to_end_of_statement (parser
);
20476 /* The old parser allows attributes to appear after a parenthesized
20477 initializer. Mark Mitchell proposed removing this functionality
20478 on the GCC mailing lists on 2002-08-13. This parser accepts the
20479 attributes -- but ignores them. Made a permerror in GCC 8. */
20480 if (cp_parser_allow_gnu_extensions_p (parser
)
20481 && initialization_kind
== CPP_OPEN_PAREN
20482 && cp_parser_attributes_opt (parser
)
20483 && permerror (input_location
,
20484 "attributes after parenthesized initializer ignored"))
20487 if (flag_permissive
&& !hint
)
20490 inform (input_location
,
20491 "this flexibility is deprecated and will be removed");
20495 /* And now complain about a non-function implicit template. */
20496 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20497 error_at (DECL_SOURCE_LOCATION (decl
),
20498 "non-function %qD declared as implicit template", decl
);
20500 /* For an in-class declaration, use `grokfield' to create the
20506 pop_scope (pushed_scope
);
20507 pushed_scope
= NULL_TREE
;
20509 decl
= grokfield (declarator
, decl_specifiers
,
20510 initializer
, !is_non_constant_init
,
20511 /*asmspec=*/NULL_TREE
,
20512 attr_chainon (attributes
, prefix_attributes
));
20513 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20514 cp_parser_save_default_args (parser
, decl
);
20515 cp_finalize_omp_declare_simd (parser
, decl
);
20516 cp_finalize_oacc_routine (parser
, decl
, false);
20519 /* Finish processing the declaration. But, skip member
20521 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20523 cp_finish_decl (decl
,
20524 initializer
, !is_non_constant_init
,
20526 /* If the initializer is in parentheses, then this is
20527 a direct-initialization, which means that an
20528 `explicit' constructor is OK. Otherwise, an
20529 `explicit' constructor cannot be used. */
20530 ((is_direct_init
|| !is_initialized
)
20531 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
20533 else if ((cxx_dialect
!= cxx98
) && friend_p
20534 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20535 /* Core issue #226 (C++0x only): A default template-argument
20536 shall not be specified in a friend class template
20538 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20539 /*is_partial=*/false, /*is_friend_decl=*/1);
20541 if (!friend_p
&& pushed_scope
)
20542 pop_scope (pushed_scope
);
20544 if (function_declarator_p (declarator
)
20545 && parser
->fully_implicit_function_template_p
)
20548 decl
= finish_fully_implicit_template (parser
, decl
);
20550 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20553 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20554 && type_uses_auto (decl_specifiers
->type
))
20555 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20560 /* Parse a declarator.
20564 ptr-operator declarator
20566 abstract-declarator:
20567 ptr-operator abstract-declarator [opt]
20568 direct-abstract-declarator
20573 attributes [opt] direct-declarator
20574 attributes [opt] ptr-operator declarator
20576 abstract-declarator:
20577 attributes [opt] ptr-operator abstract-declarator [opt]
20578 attributes [opt] direct-abstract-declarator
20580 The parser flags FLAGS is used to control type-specifier parsing.
20582 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20583 detect constructors, destructors, deduction guides, or conversion operators.
20584 It is set to -1 if the declarator is a name, and +1 if it is a
20585 function. Otherwise it is set to zero. Usually you just want to
20586 test for >0, but internally the negative value is used.
20588 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20589 a decl-specifier-seq unless it declares a constructor, destructor,
20590 or conversion. It might seem that we could check this condition in
20591 semantic analysis, rather than parsing, but that makes it difficult
20592 to handle something like `f()'. We want to notice that there are
20593 no decl-specifiers, and therefore realize that this is an
20594 expression, not a declaration.)
20596 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20597 the declarator is a direct-declarator of the form "(...)".
20599 MEMBER_P is true iff this declarator is a member-declarator.
20601 FRIEND_P is true iff this declarator is a friend.
20603 STATIC_P is true iff the keyword static was seen. */
20605 static cp_declarator
*
20606 cp_parser_declarator (cp_parser
* parser
,
20607 cp_parser_declarator_kind dcl_kind
,
20608 cp_parser_flags flags
,
20609 int* ctor_dtor_or_conv_p
,
20610 bool* parenthesized_p
,
20611 bool member_p
, bool friend_p
, bool static_p
)
20613 cp_declarator
*declarator
;
20614 enum tree_code code
;
20615 cp_cv_quals cv_quals
;
20617 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20619 /* Assume this is not a constructor, destructor, or type-conversion
20621 if (ctor_dtor_or_conv_p
)
20622 *ctor_dtor_or_conv_p
= 0;
20624 if (cp_parser_allow_gnu_extensions_p (parser
))
20625 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20627 /* Check for the ptr-operator production. */
20628 cp_parser_parse_tentatively (parser
);
20629 /* Parse the ptr-operator. */
20630 code
= cp_parser_ptr_operator (parser
,
20635 /* If that worked, then we have a ptr-operator. */
20636 if (cp_parser_parse_definitely (parser
))
20638 /* If a ptr-operator was found, then this declarator was not
20640 if (parenthesized_p
)
20641 *parenthesized_p
= true;
20642 /* The dependent declarator is optional if we are parsing an
20643 abstract-declarator. */
20644 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20645 cp_parser_parse_tentatively (parser
);
20647 /* Parse the dependent declarator. */
20648 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20649 CP_PARSER_FLAGS_NONE
,
20650 /*ctor_dtor_or_conv_p=*/NULL
,
20651 /*parenthesized_p=*/NULL
,
20652 /*member_p=*/false,
20653 friend_p
, /*static_p=*/false);
20655 /* If we are parsing an abstract-declarator, we must handle the
20656 case where the dependent declarator is absent. */
20657 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20658 && !cp_parser_parse_definitely (parser
))
20661 declarator
= cp_parser_make_indirect_declarator
20662 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20664 /* Everything else is a direct-declarator. */
20667 if (parenthesized_p
)
20668 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20670 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20671 flags
, ctor_dtor_or_conv_p
,
20672 member_p
, friend_p
, static_p
);
20675 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20676 declarator
->attributes
= gnu_attributes
;
20680 /* Parse a direct-declarator or direct-abstract-declarator.
20684 direct-declarator ( parameter-declaration-clause )
20685 cv-qualifier-seq [opt]
20686 ref-qualifier [opt]
20687 exception-specification [opt]
20688 direct-declarator [ constant-expression [opt] ]
20691 direct-abstract-declarator:
20692 direct-abstract-declarator [opt]
20693 ( parameter-declaration-clause )
20694 cv-qualifier-seq [opt]
20695 ref-qualifier [opt]
20696 exception-specification [opt]
20697 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20698 ( abstract-declarator )
20700 Returns a representation of the declarator. DCL_KIND is
20701 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20702 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20703 we are parsing a direct-declarator. It is
20704 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20705 of ambiguity we prefer an abstract declarator, as per
20707 The parser flags FLAGS is used to control type-specifier parsing.
20708 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20709 as for cp_parser_declarator. */
20711 static cp_declarator
*
20712 cp_parser_direct_declarator (cp_parser
* parser
,
20713 cp_parser_declarator_kind dcl_kind
,
20714 cp_parser_flags flags
,
20715 int* ctor_dtor_or_conv_p
,
20716 bool member_p
, bool friend_p
, bool static_p
)
20719 cp_declarator
*declarator
= NULL
;
20720 tree scope
= NULL_TREE
;
20721 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20722 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20724 tree pushed_scope
= NULL_TREE
;
20725 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20729 /* Peek at the next token. */
20730 token
= cp_lexer_peek_token (parser
->lexer
);
20731 if (token
->type
== CPP_OPEN_PAREN
)
20733 /* This is either a parameter-declaration-clause, or a
20734 parenthesized declarator. When we know we are parsing a
20735 named declarator, it must be a parenthesized declarator
20736 if FIRST is true. For instance, `(int)' is a
20737 parameter-declaration-clause, with an omitted
20738 direct-abstract-declarator. But `((*))', is a
20739 parenthesized abstract declarator. Finally, when T is a
20740 template parameter `(T)' is a
20741 parameter-declaration-clause, and not a parenthesized
20744 We first try and parse a parameter-declaration-clause,
20745 and then try a nested declarator (if FIRST is true).
20747 It is not an error for it not to be a
20748 parameter-declaration-clause, even when FIRST is
20754 The first is the declaration of a function while the
20755 second is the definition of a variable, including its
20758 Having seen only the parenthesis, we cannot know which of
20759 these two alternatives should be selected. Even more
20760 complex are examples like:
20765 The former is a function-declaration; the latter is a
20766 variable initialization.
20768 Thus again, we try a parameter-declaration-clause, and if
20769 that fails, we back out and return. */
20771 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20774 bool is_declarator
= false;
20778 /* In a member-declarator, the only valid interpretation
20779 of a parenthesis is the start of a
20780 parameter-declaration-clause. (It is invalid to
20781 initialize a static data member with a parenthesized
20782 initializer; only the "=" form of initialization is
20785 cp_parser_parse_tentatively (parser
);
20787 /* Consume the `('. */
20788 matching_parens parens
;
20789 parens
.consume_open (parser
);
20792 /* If this is going to be an abstract declarator, we're
20793 in a declarator and we can't have default args. */
20794 parser
->default_arg_ok_p
= false;
20795 parser
->in_declarator_p
= true;
20798 begin_scope (sk_function_parms
, NULL_TREE
);
20800 /* Parse the parameter-declaration-clause. */
20802 = cp_parser_parameter_declaration_clause (parser
, flags
);
20804 /* Consume the `)'. */
20805 parens
.require_close (parser
);
20807 /* If all went well, parse the cv-qualifier-seq,
20808 ref-qualifier and the exception-specification. */
20809 if (member_p
|| cp_parser_parse_definitely (parser
))
20811 cp_cv_quals cv_quals
;
20812 cp_virt_specifiers virt_specifiers
;
20813 cp_ref_qualifier ref_qual
;
20814 tree exception_specification
;
20817 bool memfn
= (member_p
|| (pushed_scope
20818 && CLASS_TYPE_P (pushed_scope
)));
20819 unsigned char local_variables_forbidden_p
20820 = parser
->local_variables_forbidden_p
;
20821 /* 'this' is not allowed in static member functions. */
20822 if (static_p
|| friend_p
)
20823 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
20825 is_declarator
= true;
20827 if (ctor_dtor_or_conv_p
)
20828 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20831 /* Parse the cv-qualifier-seq. */
20832 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20833 /* Parse the ref-qualifier. */
20834 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20835 /* Parse the tx-qualifier. */
20836 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20837 /* And the exception-specification. */
20838 exception_specification
20839 = cp_parser_exception_specification_opt (parser
);
20841 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20843 /* In here, we handle cases where attribute is used after
20844 the function declaration. For example:
20845 void func (int x) __attribute__((vector(..))); */
20846 tree gnu_attrs
= NULL_TREE
;
20847 tree requires_clause
= NULL_TREE
;
20848 late_return
= (cp_parser_late_return_type_opt
20849 (parser
, declarator
, requires_clause
,
20850 memfn
? cv_quals
: -1));
20852 /* Parse the virt-specifier-seq. */
20853 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20855 /* Create the function-declarator. */
20856 declarator
= make_call_declarator (declarator
,
20862 exception_specification
,
20865 declarator
->std_attributes
= attrs
;
20866 declarator
->attributes
= gnu_attrs
;
20867 /* Any subsequent parameter lists are to do with
20868 return type, so are not those of the declared
20870 parser
->default_arg_ok_p
= false;
20872 /* Restore the state of local_variables_forbidden_p. */
20873 parser
->local_variables_forbidden_p
20874 = local_variables_forbidden_p
;
20877 /* Remove the function parms from scope. */
20878 pop_bindings_and_leave_scope ();
20881 /* Repeat the main loop. */
20885 /* If this is the first, we can try a parenthesized
20889 bool saved_in_type_id_in_expr_p
;
20891 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20892 parser
->in_declarator_p
= saved_in_declarator_p
;
20894 open_paren
= token
;
20895 /* Consume the `('. */
20896 matching_parens parens
;
20897 parens
.consume_open (parser
);
20898 /* Parse the nested declarator. */
20899 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20900 parser
->in_type_id_in_expr_p
= true;
20902 = cp_parser_declarator (parser
, dcl_kind
, flags
,
20903 ctor_dtor_or_conv_p
,
20904 /*parenthesized_p=*/NULL
,
20905 member_p
, friend_p
,
20906 /*static_p=*/false);
20907 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20909 /* Expect a `)'. */
20910 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20911 if (!parens
.require_close (parser
))
20912 declarator
= cp_error_declarator
;
20913 if (declarator
== cp_error_declarator
)
20916 goto handle_declarator
;
20918 /* Otherwise, we must be done. */
20922 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20923 && token
->type
== CPP_OPEN_SQUARE
20924 && !cp_next_tokens_can_be_attribute_p (parser
))
20926 /* Parse an array-declarator. */
20927 tree bounds
, attrs
;
20929 if (ctor_dtor_or_conv_p
)
20930 *ctor_dtor_or_conv_p
= 0;
20934 parser
->default_arg_ok_p
= false;
20935 parser
->in_declarator_p
= true;
20936 /* Consume the `['. */
20937 cp_lexer_consume_token (parser
->lexer
);
20938 /* Peek at the next token. */
20939 token
= cp_lexer_peek_token (parser
->lexer
);
20940 /* If the next token is `]', then there is no
20941 constant-expression. */
20942 if (token
->type
!= CPP_CLOSE_SQUARE
)
20944 bool non_constant_p
;
20946 = cp_parser_constant_expression (parser
,
20947 /*allow_non_constant=*/true,
20949 if (!non_constant_p
)
20951 else if (error_operand_p (bounds
))
20952 /* Already gave an error. */;
20953 else if (!parser
->in_function_body
20954 || current_binding_level
->kind
== sk_function_parms
)
20956 /* Normally, the array bound must be an integral constant
20957 expression. However, as an extension, we allow VLAs
20958 in function scopes as long as they aren't part of a
20959 parameter declaration. */
20960 cp_parser_error (parser
,
20961 "array bound is not an integer constant");
20962 bounds
= error_mark_node
;
20964 else if (processing_template_decl
20965 && !type_dependent_expression_p (bounds
))
20967 /* Remember this wasn't a constant-expression. */
20968 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20969 TREE_SIDE_EFFECTS (bounds
) = 1;
20973 bounds
= NULL_TREE
;
20974 /* Look for the closing `]'. */
20975 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20977 declarator
= cp_error_declarator
;
20981 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20982 declarator
= make_array_declarator (declarator
, bounds
);
20983 declarator
->std_attributes
= attrs
;
20985 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20988 tree qualifying_scope
;
20989 tree unqualified_name
;
20991 special_function_kind sfk
;
20993 bool pack_expansion_p
= false;
20994 cp_token
*declarator_id_start_token
;
20996 /* Parse a declarator-id */
20997 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
21000 cp_parser_parse_tentatively (parser
);
21002 /* If we see an ellipsis, we should be looking at a
21004 if (token
->type
== CPP_ELLIPSIS
)
21006 /* Consume the `...' */
21007 cp_lexer_consume_token (parser
->lexer
);
21009 pack_expansion_p
= true;
21013 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
21015 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
21016 qualifying_scope
= parser
->scope
;
21021 if (!unqualified_name
&& pack_expansion_p
)
21023 /* Check whether an error occurred. */
21024 okay
= !cp_parser_error_occurred (parser
);
21026 /* We already consumed the ellipsis to mark a
21027 parameter pack, but we have no way to report it,
21028 so abort the tentative parse. We will be exiting
21029 immediately anyway. */
21030 cp_parser_abort_tentative_parse (parser
);
21033 okay
= cp_parser_parse_definitely (parser
);
21036 unqualified_name
= error_mark_node
;
21037 else if (unqualified_name
21038 && (qualifying_scope
21039 || (!identifier_p (unqualified_name
))))
21041 cp_parser_error (parser
, "expected unqualified-id");
21042 unqualified_name
= error_mark_node
;
21046 if (!unqualified_name
)
21048 if (unqualified_name
== error_mark_node
)
21050 declarator
= cp_error_declarator
;
21051 pack_expansion_p
= false;
21052 declarator
->parameter_pack_p
= false;
21056 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21058 if (qualifying_scope
&& at_namespace_scope_p ()
21059 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
21061 /* In the declaration of a member of a template class
21062 outside of the class itself, the SCOPE will sometimes
21063 be a TYPENAME_TYPE. For example, given:
21065 template <typename T>
21066 int S<T>::R::i = 3;
21068 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21069 this context, we must resolve S<T>::R to an ordinary
21070 type, rather than a typename type.
21072 The reason we normally avoid resolving TYPENAME_TYPEs
21073 is that a specialization of `S' might render
21074 `S<T>::R' not a type. However, if `S' is
21075 specialized, then this `i' will not be used, so there
21076 is no harm in resolving the types here. */
21079 /* Resolve the TYPENAME_TYPE. */
21080 type
= resolve_typename_type (qualifying_scope
,
21081 /*only_current_p=*/false);
21082 /* If that failed, the declarator is invalid. */
21083 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21085 if (typedef_variant_p (type
))
21086 error_at (declarator_id_start_token
->location
,
21087 "cannot define member of dependent typedef "
21090 error_at (declarator_id_start_token
->location
,
21091 "%<%T::%E%> is not a type",
21092 TYPE_CONTEXT (qualifying_scope
),
21093 TYPE_IDENTIFIER (qualifying_scope
));
21095 qualifying_scope
= type
;
21100 if (unqualified_name
)
21104 if (qualifying_scope
21105 && CLASS_TYPE_P (qualifying_scope
))
21106 class_type
= qualifying_scope
;
21108 class_type
= current_class_type
;
21110 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21112 tree name_type
= TREE_TYPE (unqualified_name
);
21114 if (!class_type
|| !same_type_p (name_type
, class_type
))
21116 /* We do not attempt to print the declarator
21117 here because we do not have enough
21118 information about its original syntactic
21120 cp_parser_error (parser
, "invalid declarator");
21121 declarator
= cp_error_declarator
;
21124 else if (qualifying_scope
21125 && CLASSTYPE_USE_TEMPLATE (name_type
))
21127 error_at (declarator_id_start_token
->location
,
21128 "invalid use of constructor as a template");
21129 inform (declarator_id_start_token
->location
,
21130 "use %<%T::%D%> instead of %<%T::%D%> to "
21131 "name the constructor in a qualified name",
21133 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21134 class_type
, name_type
);
21135 declarator
= cp_error_declarator
;
21138 unqualified_name
= constructor_name (class_type
);
21143 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21144 sfk
= sfk_destructor
;
21145 else if (identifier_p (unqualified_name
)
21146 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21147 sfk
= sfk_conversion
;
21148 else if (/* There's no way to declare a constructor
21149 for an unnamed type, even if the type
21150 got a name for linkage purposes. */
21151 !TYPE_WAS_UNNAMED (class_type
)
21152 /* Handle correctly (c++/19200):
21166 friend void N::S();
21168 && (!friend_p
|| class_type
== qualifying_scope
)
21169 && constructor_name_p (unqualified_name
,
21171 sfk
= sfk_constructor
;
21172 else if (is_overloaded_fn (unqualified_name
)
21173 && DECL_CONSTRUCTOR_P (get_first_fn
21174 (unqualified_name
)))
21175 sfk
= sfk_constructor
;
21177 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21178 *ctor_dtor_or_conv_p
= -1;
21181 declarator
= make_id_declarator (qualifying_scope
,
21183 sfk
, token
->location
);
21184 declarator
->std_attributes
= attrs
;
21185 declarator
->parameter_pack_p
= pack_expansion_p
;
21187 if (pack_expansion_p
)
21188 maybe_warn_variadic_templates ();
21190 /* We're looking for this case in [temp.res]:
21191 A qualified-id is assumed to name a type if [...]
21192 - it is a decl-specifier of the decl-specifier-seq of a
21193 parameter-declaration in a declarator of a function or
21194 function template declaration, ... */
21195 if (cxx_dialect
>= cxx2a
21196 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
21197 && declarator
->kind
== cdk_id
21198 && !at_class_scope_p ()
21199 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21201 /* ...whose declarator-id is qualified. If it isn't, never
21202 assume the parameters to refer to types. */
21203 if (qualifying_scope
== NULL_TREE
)
21204 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21207 /* Now we have something like
21208 template <typename T> int C::x(S::p);
21209 which can be a function template declaration or a
21210 variable template definition. If name lookup for
21211 the declarator-id C::x finds one or more function
21212 templates, assume S::p to name a type. Otherwise,
21215 = cp_parser_lookup_name_simple (parser
, unqualified_name
,
21217 if (!is_overloaded_fn (decl
)
21219 template<typename T>
21220 A<T>::A(T::type) { } */
21221 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
21222 && constructor_name_p (unqualified_name
,
21223 qualifying_scope
)))
21224 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21229 handle_declarator
:;
21230 scope
= get_scope_of_declarator (declarator
);
21233 /* Any names that appear after the declarator-id for a
21234 member are looked up in the containing scope. */
21235 if (at_function_scope_p ())
21237 /* But declarations with qualified-ids can't appear in a
21239 cp_parser_error (parser
, "qualified-id in declaration");
21240 declarator
= cp_error_declarator
;
21243 pushed_scope
= push_scope (scope
);
21245 parser
->in_declarator_p
= true;
21246 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21247 || (declarator
&& declarator
->kind
== cdk_id
))
21248 /* Default args are only allowed on function
21250 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21252 parser
->default_arg_ok_p
= false;
21261 /* For an abstract declarator, we might wind up with nothing at this
21262 point. That's an error; the declarator is not optional. */
21264 cp_parser_error (parser
, "expected declarator");
21265 else if (open_paren
)
21267 /* Record overly parenthesized declarator so we can give a
21268 diagnostic about confusing decl/expr disambiguation. */
21269 if (declarator
->kind
== cdk_array
)
21271 /* If the open and close parens are on different lines, this
21272 is probably a formatting thing, so ignore. */
21273 expanded_location open
= expand_location (open_paren
->location
);
21274 expanded_location close
= expand_location (close_paren
->location
);
21275 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21279 declarator
->parenthesized
= open_paren
->location
;
21282 /* If we entered a scope, we must exit it now. */
21284 pop_scope (pushed_scope
);
21286 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21287 parser
->in_declarator_p
= saved_in_declarator_p
;
21292 /* Parse a ptr-operator.
21295 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21296 * cv-qualifier-seq [opt]
21298 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21299 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21304 & cv-qualifier-seq [opt]
21306 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21307 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21308 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21309 filled in with the TYPE containing the member. *CV_QUALS is
21310 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21311 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21312 Note that the tree codes returned by this function have nothing
21313 to do with the types of trees that will be eventually be created
21314 to represent the pointer or reference type being parsed. They are
21315 just constants with suggestive names. */
21316 static enum tree_code
21317 cp_parser_ptr_operator (cp_parser
* parser
,
21319 cp_cv_quals
*cv_quals
,
21322 enum tree_code code
= ERROR_MARK
;
21324 tree attrs
= NULL_TREE
;
21326 /* Assume that it's not a pointer-to-member. */
21328 /* And that there are no cv-qualifiers. */
21329 *cv_quals
= TYPE_UNQUALIFIED
;
21331 /* Peek at the next token. */
21332 token
= cp_lexer_peek_token (parser
->lexer
);
21334 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21335 if (token
->type
== CPP_MULT
)
21336 code
= INDIRECT_REF
;
21337 else if (token
->type
== CPP_AND
)
21339 else if ((cxx_dialect
!= cxx98
) &&
21340 token
->type
== CPP_AND_AND
) /* C++0x only */
21341 code
= NON_LVALUE_EXPR
;
21343 if (code
!= ERROR_MARK
)
21345 /* Consume the `*', `&' or `&&'. */
21346 cp_lexer_consume_token (parser
->lexer
);
21348 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21349 `&', if we are allowing GNU extensions. (The only qualifier
21350 that can legally appear after `&' is `restrict', but that is
21351 enforced during semantic analysis. */
21352 if (code
== INDIRECT_REF
21353 || cp_parser_allow_gnu_extensions_p (parser
))
21354 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21356 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21357 if (attributes
!= NULL
)
21358 *attributes
= attrs
;
21362 /* Try the pointer-to-member case. */
21363 cp_parser_parse_tentatively (parser
);
21364 /* Look for the optional `::' operator. */
21365 cp_parser_global_scope_opt (parser
,
21366 /*current_scope_valid_p=*/false);
21367 /* Look for the nested-name specifier. */
21368 token
= cp_lexer_peek_token (parser
->lexer
);
21369 cp_parser_nested_name_specifier (parser
,
21370 /*typename_keyword_p=*/false,
21371 /*check_dependency_p=*/true,
21373 /*is_declaration=*/false);
21374 /* If we found it, and the next token is a `*', then we are
21375 indeed looking at a pointer-to-member operator. */
21376 if (!cp_parser_error_occurred (parser
)
21377 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21379 /* Indicate that the `*' operator was used. */
21380 code
= INDIRECT_REF
;
21382 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21383 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21384 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21385 error_at (token
->location
, "cannot form pointer to member of "
21386 "non-class %q#T", parser
->scope
);
21389 /* The type of which the member is a member is given by the
21391 *type
= parser
->scope
;
21392 /* The next name will not be qualified. */
21393 parser
->scope
= NULL_TREE
;
21394 parser
->qualifying_scope
= NULL_TREE
;
21395 parser
->object_scope
= NULL_TREE
;
21396 /* Look for optional c++11 attributes. */
21397 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21398 if (attributes
!= NULL
)
21399 *attributes
= attrs
;
21400 /* Look for the optional cv-qualifier-seq. */
21401 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21404 /* If that didn't work we don't have a ptr-operator. */
21405 if (!cp_parser_parse_definitely (parser
))
21406 cp_parser_error (parser
, "expected ptr-operator");
21412 /* Parse an (optional) cv-qualifier-seq.
21415 cv-qualifier cv-qualifier-seq [opt]
21426 Returns a bitmask representing the cv-qualifiers. */
21429 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21431 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21436 cp_cv_quals cv_qualifier
;
21438 /* Peek at the next token. */
21439 token
= cp_lexer_peek_token (parser
->lexer
);
21440 /* See if it's a cv-qualifier. */
21441 switch (token
->keyword
)
21444 cv_qualifier
= TYPE_QUAL_CONST
;
21448 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21452 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21456 cv_qualifier
= TYPE_UNQUALIFIED
;
21463 if (cv_quals
& cv_qualifier
)
21465 gcc_rich_location
richloc (token
->location
);
21466 richloc
.add_fixit_remove ();
21467 error_at (&richloc
, "duplicate cv-qualifier");
21468 cp_lexer_purge_token (parser
->lexer
);
21472 cp_lexer_consume_token (parser
->lexer
);
21473 cv_quals
|= cv_qualifier
;
21480 /* Parse an (optional) ref-qualifier
21486 Returns cp_ref_qualifier representing ref-qualifier. */
21488 static cp_ref_qualifier
21489 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21491 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21493 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21494 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21499 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21500 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21502 switch (token
->type
)
21505 curr_ref_qual
= REF_QUAL_LVALUE
;
21509 curr_ref_qual
= REF_QUAL_RVALUE
;
21513 curr_ref_qual
= REF_QUAL_NONE
;
21517 if (!curr_ref_qual
)
21521 error_at (token
->location
, "multiple ref-qualifiers");
21522 cp_lexer_purge_token (parser
->lexer
);
21526 ref_qual
= curr_ref_qual
;
21527 cp_lexer_consume_token (parser
->lexer
);
21534 /* Parse an optional tx-qualifier.
21538 transaction_safe_dynamic */
21541 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21543 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21544 if (token
->type
== CPP_NAME
)
21546 tree name
= token
->u
.value
;
21547 const char *p
= IDENTIFIER_POINTER (name
);
21548 const int len
= strlen ("transaction_safe");
21549 if (!strncmp (p
, "transaction_safe", len
))
21553 || !strcmp (p
, "_dynamic"))
21555 cp_lexer_consume_token (parser
->lexer
);
21558 error ("%qE requires %<-fgnu-tm%>", name
);
21569 /* Parse an (optional) virt-specifier-seq.
21571 virt-specifier-seq:
21572 virt-specifier virt-specifier-seq [opt]
21578 Returns a bitmask representing the virt-specifiers. */
21580 static cp_virt_specifiers
21581 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21583 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21588 cp_virt_specifiers virt_specifier
;
21590 /* Peek at the next token. */
21591 token
= cp_lexer_peek_token (parser
->lexer
);
21592 /* See if it's a virt-specifier-qualifier. */
21593 if (token
->type
!= CPP_NAME
)
21595 if (id_equal (token
->u
.value
, "override"))
21597 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21598 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21600 else if (id_equal (token
->u
.value
, "final"))
21602 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21603 virt_specifier
= VIRT_SPEC_FINAL
;
21605 else if (id_equal (token
->u
.value
, "__final"))
21607 virt_specifier
= VIRT_SPEC_FINAL
;
21612 if (virt_specifiers
& virt_specifier
)
21614 gcc_rich_location
richloc (token
->location
);
21615 richloc
.add_fixit_remove ();
21616 error_at (&richloc
, "duplicate virt-specifier");
21617 cp_lexer_purge_token (parser
->lexer
);
21621 cp_lexer_consume_token (parser
->lexer
);
21622 virt_specifiers
|= virt_specifier
;
21625 return virt_specifiers
;
21628 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21629 is in scope even though it isn't real. */
21632 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21636 if (current_class_ptr
)
21638 /* We don't clear this between NSDMIs. Is it already what we want? */
21639 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21640 if (DECL_P (current_class_ptr
)
21641 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21642 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21643 && cp_type_quals (type
) == quals
)
21647 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21648 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21649 current_class_ptr
= NULL_TREE
;
21651 = cp_build_fold_indirect_ref (this_parm
);
21652 current_class_ptr
= this_parm
;
21655 /* Return true iff our current scope is a non-static data member
21659 parsing_nsdmi (void)
21661 /* We recognize NSDMI context by the context-less 'this' pointer set up
21662 by the function above. */
21663 if (current_class_ptr
21664 && TREE_CODE (current_class_ptr
) == PARM_DECL
21665 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21670 /* Parse a late-specified return type, if any. This is not a separate
21671 non-terminal, but part of a function declarator, which looks like
21673 -> trailing-type-specifier-seq abstract-declarator(opt)
21675 Returns the type indicated by the type-id.
21677 In addition to this, parse any queued up #pragma omp declare simd
21678 clauses, and #pragma acc routine clauses.
21680 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21684 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21685 tree
& requires_clause
, cp_cv_quals quals
)
21688 tree type
= NULL_TREE
;
21689 bool declare_simd_p
= (parser
->omp_declare_simd
21691 && declarator
->kind
== cdk_id
);
21693 bool oacc_routine_p
= (parser
->oacc_routine
21695 && declarator
->kind
== cdk_id
);
21697 /* Peek at the next token. */
21698 token
= cp_lexer_peek_token (parser
->lexer
);
21699 /* A late-specified return type is indicated by an initial '->'. */
21700 if (token
->type
!= CPP_DEREF
21701 && token
->keyword
!= RID_REQUIRES
21702 && !(token
->type
== CPP_NAME
21703 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21704 && !(declare_simd_p
|| oacc_routine_p
))
21707 tree save_ccp
= current_class_ptr
;
21708 tree save_ccr
= current_class_ref
;
21711 /* DR 1207: 'this' is in scope in the trailing return type. */
21712 inject_this_parameter (current_class_type
, quals
);
21715 if (token
->type
== CPP_DEREF
)
21717 /* Consume the ->. */
21718 cp_lexer_consume_token (parser
->lexer
);
21720 type
= cp_parser_trailing_type_id (parser
);
21723 /* Function declarations may be followed by a trailing
21724 requires-clause. */
21725 requires_clause
= cp_parser_requires_clause_opt (parser
);
21727 if (declare_simd_p
)
21728 declarator
->attributes
21729 = cp_parser_late_parsing_omp_declare_simd (parser
,
21730 declarator
->attributes
);
21731 if (oacc_routine_p
)
21732 declarator
->attributes
21733 = cp_parser_late_parsing_oacc_routine (parser
,
21734 declarator
->attributes
);
21738 current_class_ptr
= save_ccp
;
21739 current_class_ref
= save_ccr
;
21745 /* Parse a declarator-id.
21749 :: [opt] nested-name-specifier [opt] type-name
21751 In the `id-expression' case, the value returned is as for
21752 cp_parser_id_expression if the id-expression was an unqualified-id.
21753 If the id-expression was a qualified-id, then a SCOPE_REF is
21754 returned. The first operand is the scope (either a NAMESPACE_DECL
21755 or TREE_TYPE), but the second is still just a representation of an
21759 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21762 /* The expression must be an id-expression. Assume that qualified
21763 names are the names of types so that:
21766 int S<T>::R::i = 3;
21768 will work; we must treat `S<T>::R' as the name of a type.
21769 Similarly, assume that qualified names are templates, where
21773 int S<T>::R<T>::i = 3;
21776 id
= cp_parser_id_expression (parser
,
21777 /*template_keyword_p=*/false,
21778 /*check_dependency_p=*/false,
21779 /*template_p=*/NULL
,
21780 /*declarator_p=*/true,
21782 if (id
&& BASELINK_P (id
))
21783 id
= BASELINK_FUNCTIONS (id
);
21787 /* Parse a type-id.
21790 type-specifier-seq abstract-declarator [opt]
21792 The parser flags FLAGS is used to control type-specifier parsing.
21794 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21796 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21797 i.e. we've just seen "->".
21799 Returns the TYPE specified. */
21802 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
21803 bool is_template_arg
, bool is_trailing_return
,
21804 location_t
*type_location
)
21806 cp_decl_specifier_seq type_specifier_seq
;
21807 cp_declarator
*abstract_declarator
;
21809 /* Parse the type-specifier-seq. */
21810 cp_parser_type_specifier_seq (parser
, flags
,
21811 /*is_declaration=*/false,
21812 is_trailing_return
,
21813 &type_specifier_seq
);
21815 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
21817 if (is_template_arg
&& type_specifier_seq
.type
21818 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21819 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21820 /* A bare template name as a template argument is a template template
21821 argument, not a placeholder, so fail parsing it as a type argument. */
21823 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21824 cp_parser_simulate_error (parser
);
21825 return error_mark_node
;
21827 if (type_specifier_seq
.type
== error_mark_node
)
21828 return error_mark_node
;
21830 /* There might or might not be an abstract declarator. */
21831 cp_parser_parse_tentatively (parser
);
21832 /* Look for the declarator. */
21833 abstract_declarator
21834 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
21835 CP_PARSER_FLAGS_NONE
, NULL
,
21836 /*parenthesized_p=*/NULL
,
21837 /*member_p=*/false,
21838 /*friend_p=*/false,
21839 /*static_p=*/false);
21840 /* Check to see if there really was a declarator. */
21841 if (!cp_parser_parse_definitely (parser
))
21842 abstract_declarator
= NULL
;
21844 if (type_specifier_seq
.type
21845 /* The concepts TS allows 'auto' as a type-id. */
21846 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21847 /* None of the valid uses of 'auto' in C++14 involve the type-id
21848 nonterminal, but it is valid in a trailing-return-type. */
21849 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21850 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21852 /* A type-id with type 'auto' is only ok if the abstract declarator
21853 is a function declarator with a late-specified return type.
21855 A type-id with 'auto' is also valid in a trailing-return-type
21856 in a compound-requirement. */
21857 if (abstract_declarator
21858 && abstract_declarator
->kind
== cdk_function
21859 && abstract_declarator
->u
.function
.late_return_type
)
21861 else if (parser
->in_result_type_constraint_p
)
21865 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21866 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21868 error_at (loc
, "missing template arguments after %qT",
21870 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21874 error_at (loc
, "invalid use of %qT", auto_node
);
21875 return error_mark_node
;
21879 return groktypename (&type_specifier_seq
, abstract_declarator
,
21883 /* Wrapper for cp_parser_type_id_1. */
21886 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
21887 location_t
*type_location
)
21889 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
21892 /* Wrapper for cp_parser_type_id_1. */
21895 cp_parser_template_type_arg (cp_parser
*parser
)
21898 const char *saved_message
= parser
->type_definition_forbidden_message
;
21899 parser
->type_definition_forbidden_message
21900 = G_("types may not be defined in template arguments");
21901 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
21902 parser
->type_definition_forbidden_message
= saved_message
;
21903 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21905 error ("invalid use of %<auto%> in template argument");
21906 r
= error_mark_node
;
21911 /* Wrapper for cp_parser_type_id_1. */
21914 cp_parser_trailing_type_id (cp_parser
*parser
)
21916 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
21917 false, true, NULL
);
21920 /* Parse a type-specifier-seq.
21922 type-specifier-seq:
21923 type-specifier type-specifier-seq [opt]
21927 type-specifier-seq:
21928 attributes type-specifier-seq [opt]
21930 The parser flags FLAGS is used to control type-specifier parsing.
21932 If IS_DECLARATION is true, we are at the start of a "condition" or
21933 exception-declaration, so we might be followed by a declarator-id.
21935 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21936 i.e. we've just seen "->".
21938 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21941 cp_parser_type_specifier_seq (cp_parser
* parser
,
21942 cp_parser_flags flags
,
21943 bool is_declaration
,
21944 bool is_trailing_return
,
21945 cp_decl_specifier_seq
*type_specifier_seq
)
21947 bool seen_type_specifier
= false;
21948 cp_token
*start_token
= NULL
;
21950 /* Clear the TYPE_SPECIFIER_SEQ. */
21951 clear_decl_specs (type_specifier_seq
);
21953 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
21954 /* In the context of a trailing return type, enum E { } is an
21955 elaborated-type-specifier followed by a function-body, not an
21957 if (is_trailing_return
)
21958 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21960 /* Parse the type-specifiers and attributes. */
21963 tree type_specifier
;
21964 bool is_cv_qualifier
;
21966 /* Check for attributes first. */
21967 if (cp_next_tokens_can_be_attribute_p (parser
))
21969 type_specifier_seq
->attributes
21970 = attr_chainon (type_specifier_seq
->attributes
,
21971 cp_parser_attributes_opt (parser
));
21975 /* record the token of the beginning of the type specifier seq,
21976 for error reporting purposes*/
21978 start_token
= cp_lexer_peek_token (parser
->lexer
);
21980 /* Look for the type-specifier. */
21981 type_specifier
= cp_parser_type_specifier (parser
,
21983 type_specifier_seq
,
21984 /*is_declaration=*/false,
21987 if (!type_specifier
)
21989 /* If the first type-specifier could not be found, this is not a
21990 type-specifier-seq at all. */
21991 if (!seen_type_specifier
)
21993 /* Set in_declarator_p to avoid skipping to the semicolon. */
21994 int in_decl
= parser
->in_declarator_p
;
21995 parser
->in_declarator_p
= true;
21997 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21998 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21999 cp_parser_error (parser
, "expected type-specifier");
22001 parser
->in_declarator_p
= in_decl
;
22003 type_specifier_seq
->type
= error_mark_node
;
22006 /* If subsequent type-specifiers could not be found, the
22007 type-specifier-seq is complete. */
22011 seen_type_specifier
= true;
22012 /* The standard says that a condition can be:
22014 type-specifier-seq declarator = assignment-expression
22021 we should treat the "S" as a declarator, not as a
22022 type-specifier. The standard doesn't say that explicitly for
22023 type-specifier-seq, but it does say that for
22024 decl-specifier-seq in an ordinary declaration. Perhaps it
22025 would be clearer just to allow a decl-specifier-seq here, and
22026 then add a semantic restriction that if any decl-specifiers
22027 that are not type-specifiers appear, the program is invalid. */
22028 if (is_declaration
&& !is_cv_qualifier
)
22029 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
22033 /* Return whether the function currently being declared has an associated
22034 template parameter list. */
22037 function_being_declared_is_template_p (cp_parser
* parser
)
22039 if (!current_template_parms
|| processing_template_parmlist
)
22042 if (parser
->implicit_template_scope
)
22045 if (at_class_scope_p ()
22046 && TYPE_BEING_DEFINED (current_class_type
))
22047 return parser
->num_template_parameter_lists
!= 0;
22049 return ((int) parser
->num_template_parameter_lists
> template_class_depth
22050 (current_class_type
));
22053 /* Parse a parameter-declaration-clause.
22055 parameter-declaration-clause:
22056 parameter-declaration-list [opt] ... [opt]
22057 parameter-declaration-list , ...
22059 The parser flags FLAGS is used to control type-specifier parsing.
22061 Returns a representation for the parameter declarations. A return
22062 value of NULL indicates a parameter-declaration-clause consisting
22063 only of an ellipsis. */
22066 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
22067 cp_parser_flags flags
)
22073 temp_override
<bool> cleanup
22074 (parser
->auto_is_implicit_function_template_parm_p
);
22076 if (!processing_specialization
22077 && !processing_template_parmlist
22078 && !processing_explicit_instantiation
22079 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22080 actual function or a random abstract declarator. */
22081 && parser
->default_arg_ok_p
)
22082 if (!current_function_decl
22083 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
22084 parser
->auto_is_implicit_function_template_parm_p
= true;
22086 /* Peek at the next token. */
22087 token
= cp_lexer_peek_token (parser
->lexer
);
22088 /* Check for trivial parameter-declaration-clauses. */
22089 if (token
->type
== CPP_ELLIPSIS
)
22091 /* Consume the `...' token. */
22092 cp_lexer_consume_token (parser
->lexer
);
22095 else if (token
->type
== CPP_CLOSE_PAREN
)
22096 /* There are no parameters. */
22097 return void_list_node
;
22098 /* Check for `(void)', too, which is a special case. */
22099 else if (token
->keyword
== RID_VOID
22100 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22101 == CPP_CLOSE_PAREN
))
22103 /* Consume the `void' token. */
22104 cp_lexer_consume_token (parser
->lexer
);
22105 /* There are no parameters. */
22106 return void_list_node
;
22109 /* Parse the parameter-declaration-list. */
22110 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
22111 /* If a parse error occurred while parsing the
22112 parameter-declaration-list, then the entire
22113 parameter-declaration-clause is erroneous. */
22114 if (parameters
== error_mark_node
)
22117 /* Peek at the next token. */
22118 token
= cp_lexer_peek_token (parser
->lexer
);
22119 /* If it's a `,', the clause should terminate with an ellipsis. */
22120 if (token
->type
== CPP_COMMA
)
22122 /* Consume the `,'. */
22123 cp_lexer_consume_token (parser
->lexer
);
22124 /* Expect an ellipsis. */
22126 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
22128 /* It might also be `...' if the optional trailing `,' was
22130 else if (token
->type
== CPP_ELLIPSIS
)
22132 /* Consume the `...' token. */
22133 cp_lexer_consume_token (parser
->lexer
);
22134 /* And remember that we saw it. */
22138 ellipsis_p
= false;
22140 /* Finish the parameter list. */
22142 parameters
= chainon (parameters
, void_list_node
);
22147 /* Parse a parameter-declaration-list.
22149 parameter-declaration-list:
22150 parameter-declaration
22151 parameter-declaration-list , parameter-declaration
22153 The parser flags FLAGS is used to control type-specifier parsing.
22155 Returns a representation of the parameter-declaration-list, as for
22156 cp_parser_parameter_declaration_clause. However, the
22157 `void_list_node' is never appended to the list. */
22160 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22162 tree parameters
= NULL_TREE
;
22163 tree
*tail
= ¶meters
;
22164 bool saved_in_unbraced_linkage_specification_p
;
22167 /* The special considerations that apply to a function within an
22168 unbraced linkage specifications do not apply to the parameters
22169 to the function. */
22170 saved_in_unbraced_linkage_specification_p
22171 = parser
->in_unbraced_linkage_specification_p
;
22172 parser
->in_unbraced_linkage_specification_p
= false;
22174 /* Look for more parameters. */
22177 cp_parameter_declarator
*parameter
;
22178 tree decl
= error_mark_node
;
22179 bool parenthesized_p
= false;
22181 /* Parse the parameter. */
22183 = cp_parser_parameter_declaration (parser
, flags
,
22184 /*template_parm_p=*/false,
22187 /* We don't know yet if the enclosing context is deprecated, so wait
22188 and warn in grokparms if appropriate. */
22189 deprecated_state
= DEPRECATED_SUPPRESS
;
22193 decl
= grokdeclarator (parameter
->declarator
,
22194 ¶meter
->decl_specifiers
,
22196 parameter
->default_argument
!= NULL_TREE
,
22197 ¶meter
->decl_specifiers
.attributes
);
22198 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22199 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22202 deprecated_state
= DEPRECATED_NORMAL
;
22204 /* If a parse error occurred parsing the parameter declaration,
22205 then the entire parameter-declaration-list is erroneous. */
22206 if (decl
== error_mark_node
)
22208 parameters
= error_mark_node
;
22212 if (parameter
->decl_specifiers
.attributes
)
22213 cplus_decl_attributes (&decl
,
22214 parameter
->decl_specifiers
.attributes
,
22216 if (DECL_NAME (decl
))
22217 decl
= pushdecl (decl
);
22219 if (decl
!= error_mark_node
)
22221 retrofit_lang_decl (decl
);
22222 DECL_PARM_INDEX (decl
) = ++index
;
22223 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22226 /* Add the new parameter to the list. */
22227 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22228 tail
= &TREE_CHAIN (*tail
);
22230 /* Peek at the next token. */
22231 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22232 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22233 /* These are for Objective-C++ */
22234 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22235 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22236 /* The parameter-declaration-list is complete. */
22238 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22242 /* Peek at the next token. */
22243 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22244 /* If it's an ellipsis, then the list is complete. */
22245 if (token
->type
== CPP_ELLIPSIS
)
22247 /* Otherwise, there must be more parameters. Consume the
22249 cp_lexer_consume_token (parser
->lexer
);
22250 /* When parsing something like:
22252 int i(float f, double d)
22254 we can tell after seeing the declaration for "f" that we
22255 are not looking at an initialization of a variable "i",
22256 but rather at the declaration of a function "i".
22258 Due to the fact that the parsing of template arguments
22259 (as specified to a template-id) requires backtracking we
22260 cannot use this technique when inside a template argument
22262 if (!parser
->in_template_argument_list_p
22263 && !parser
->in_type_id_in_expr_p
22264 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22265 /* However, a parameter-declaration of the form
22266 "float(f)" (which is a valid declaration of a
22267 parameter "f") can also be interpreted as an
22268 expression (the conversion of "f" to "float"). */
22269 && !parenthesized_p
)
22270 cp_parser_commit_to_tentative_parse (parser
);
22274 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22275 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22276 cp_parser_skip_to_closing_parenthesis (parser
,
22277 /*recovering=*/true,
22278 /*or_comma=*/false,
22279 /*consume_paren=*/false);
22284 parser
->in_unbraced_linkage_specification_p
22285 = saved_in_unbraced_linkage_specification_p
;
22287 /* Reset implicit_template_scope if we are about to leave the function
22288 parameter list that introduced it. Note that for out-of-line member
22289 definitions, there will be one or more class scopes before we get to
22290 the template parameter scope. */
22292 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22293 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22295 while (maybe_its
->kind
== sk_class
)
22296 maybe_its
= maybe_its
->level_chain
;
22297 if (maybe_its
== its
)
22299 parser
->implicit_template_parms
= 0;
22300 parser
->implicit_template_scope
= 0;
22307 /* Parse a parameter declaration.
22309 parameter-declaration:
22310 decl-specifier-seq ... [opt] declarator
22311 decl-specifier-seq declarator = assignment-expression
22312 decl-specifier-seq ... [opt] abstract-declarator [opt]
22313 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22315 The parser flags FLAGS is used to control type-specifier parsing.
22317 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22318 declares a template parameter. (In that case, a non-nested `>'
22319 token encountered during the parsing of the assignment-expression
22320 is not interpreted as a greater-than operator.)
22322 Returns a representation of the parameter, or NULL if an error
22323 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22324 true iff the declarator is of the form "(p)". */
22326 static cp_parameter_declarator
*
22327 cp_parser_parameter_declaration (cp_parser
*parser
,
22328 cp_parser_flags flags
,
22329 bool template_parm_p
,
22330 bool *parenthesized_p
)
22332 int declares_class_or_enum
;
22333 cp_decl_specifier_seq decl_specifiers
;
22334 cp_declarator
*declarator
;
22335 tree default_argument
;
22336 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22337 const char *saved_message
;
22338 bool template_parameter_pack_p
= false;
22340 /* In a template parameter, `>' is not an operator.
22344 When parsing a default template-argument for a non-type
22345 template-parameter, the first non-nested `>' is taken as the end
22346 of the template parameter-list rather than a greater-than
22349 /* Type definitions may not appear in parameter types. */
22350 saved_message
= parser
->type_definition_forbidden_message
;
22351 parser
->type_definition_forbidden_message
22352 = G_("types may not be defined in parameter types");
22354 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22355 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22356 (current_template_parms
)) : 0);
22358 /* Parse the declaration-specifiers. */
22359 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22360 cp_parser_decl_specifier_seq (parser
,
22363 &declares_class_or_enum
);
22365 /* Complain about missing 'typename' or other invalid type names. */
22366 if (!decl_specifiers
.any_type_specifiers_p
22367 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22368 decl_specifiers
.type
= error_mark_node
;
22370 /* If an error occurred, there's no reason to attempt to parse the
22371 rest of the declaration. */
22372 if (cp_parser_error_occurred (parser
))
22374 parser
->type_definition_forbidden_message
= saved_message
;
22378 /* Peek at the next token. */
22379 token
= cp_lexer_peek_token (parser
->lexer
);
22381 /* If the next token is a `)', `,', `=', `>', or `...', then there
22382 is no declarator. However, when variadic templates are enabled,
22383 there may be a declarator following `...'. */
22384 if (token
->type
== CPP_CLOSE_PAREN
22385 || token
->type
== CPP_COMMA
22386 || token
->type
== CPP_EQ
22387 || token
->type
== CPP_GREATER
)
22390 if (parenthesized_p
)
22391 *parenthesized_p
= false;
22393 /* Otherwise, there should be a declarator. */
22396 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22397 parser
->default_arg_ok_p
= false;
22399 /* After seeing a decl-specifier-seq, if the next token is not a
22400 "(", there is no possibility that the code is a valid
22401 expression. Therefore, if parsing tentatively, we commit at
22403 if (!parser
->in_template_argument_list_p
22404 /* In an expression context, having seen:
22408 we cannot be sure whether we are looking at a
22409 function-type (taking a "char" as a parameter) or a cast
22410 of some object of type "char" to "int". */
22411 && !parser
->in_type_id_in_expr_p
22412 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22413 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22414 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22415 cp_parser_commit_to_tentative_parse (parser
);
22416 /* Parse the declarator. */
22417 declarator_token_start
= token
;
22418 declarator
= cp_parser_declarator (parser
,
22419 CP_PARSER_DECLARATOR_EITHER
,
22420 CP_PARSER_FLAGS_NONE
,
22421 /*ctor_dtor_or_conv_p=*/NULL
,
22423 /*member_p=*/false,
22424 /*friend_p=*/false,
22425 /*static_p=*/false);
22426 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22427 /* After the declarator, allow more attributes. */
22428 decl_specifiers
.attributes
22429 = attr_chainon (decl_specifiers
.attributes
,
22430 cp_parser_attributes_opt (parser
));
22432 /* If the declarator is a template parameter pack, remember that and
22433 clear the flag in the declarator itself so we don't get errors
22434 from grokdeclarator. */
22435 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22437 declarator
->parameter_pack_p
= false;
22438 template_parameter_pack_p
= true;
22442 /* If the next token is an ellipsis, and we have not seen a declarator
22443 name, and if either the type of the declarator contains parameter
22444 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22445 for, eg, abbreviated integral type names), then we actually have a
22446 parameter pack expansion expression. Otherwise, leave the ellipsis
22447 for a C-style variadic function. */
22448 token
= cp_lexer_peek_token (parser
->lexer
);
22450 /* If a function parameter pack was specified and an implicit template
22451 parameter was introduced during cp_parser_parameter_declaration,
22452 change any implicit parameters introduced into packs. */
22453 if (parser
->implicit_template_parms
22454 && ((token
->type
== CPP_ELLIPSIS
22455 && declarator_can_be_parameter_pack (declarator
))
22456 || (declarator
&& declarator
->parameter_pack_p
)))
22458 int latest_template_parm_idx
= TREE_VEC_LENGTH
22459 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22461 if (latest_template_parm_idx
!= template_parm_idx
)
22462 decl_specifiers
.type
= convert_generic_types_to_packs
22463 (decl_specifiers
.type
,
22464 template_parm_idx
, latest_template_parm_idx
);
22467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22469 tree type
= decl_specifiers
.type
;
22471 if (type
&& DECL_P (type
))
22472 type
= TREE_TYPE (type
);
22475 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22476 && (template_parm_p
|| uses_parameter_packs (type
)))
22477 || (!type
&& template_parm_p
))
22478 && declarator_can_be_parameter_pack (declarator
))
22480 /* Consume the `...'. */
22481 cp_lexer_consume_token (parser
->lexer
);
22482 maybe_warn_variadic_templates ();
22484 /* Build a pack expansion type */
22485 if (template_parm_p
)
22486 template_parameter_pack_p
= true;
22487 else if (declarator
)
22488 declarator
->parameter_pack_p
= true;
22490 decl_specifiers
.type
= make_pack_expansion (type
);
22494 /* The restriction on defining new types applies only to the type
22495 of the parameter, not to the default argument. */
22496 parser
->type_definition_forbidden_message
= saved_message
;
22498 /* If the next token is `=', then process a default argument. */
22499 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22501 tree type
= decl_specifiers
.type
;
22502 token
= cp_lexer_peek_token (parser
->lexer
);
22503 /* If we are defining a class, then the tokens that make up the
22504 default argument must be saved and processed later. */
22505 if (!template_parm_p
&& at_class_scope_p ()
22506 && TYPE_BEING_DEFINED (current_class_type
)
22507 && !LAMBDA_TYPE_P (current_class_type
))
22508 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22510 // A constrained-type-specifier may declare a type template-parameter.
22511 else if (declares_constrained_type_template_parameter (type
))
22513 = cp_parser_default_type_template_argument (parser
);
22515 // A constrained-type-specifier may declare a template-template-parameter.
22516 else if (declares_constrained_template_template_parameter (type
))
22518 = cp_parser_default_template_template_argument (parser
);
22520 /* Outside of a class definition, we can just parse the
22521 assignment-expression. */
22524 = cp_parser_default_argument (parser
, template_parm_p
);
22526 if (!parser
->default_arg_ok_p
)
22528 permerror (token
->location
,
22529 "default arguments are only "
22530 "permitted for function parameters");
22532 else if ((declarator
&& declarator
->parameter_pack_p
)
22533 || template_parameter_pack_p
22534 || (decl_specifiers
.type
22535 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22537 /* Find the name of the parameter pack. */
22538 cp_declarator
*id_declarator
= declarator
;
22539 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22540 id_declarator
= id_declarator
->declarator
;
22542 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22543 error_at (declarator_token_start
->location
,
22545 ? G_("template parameter pack %qD "
22546 "cannot have a default argument")
22547 : G_("parameter pack %qD cannot have "
22548 "a default argument"),
22549 id_declarator
->u
.id
.unqualified_name
);
22551 error_at (declarator_token_start
->location
,
22553 ? G_("template parameter pack cannot have "
22554 "a default argument")
22555 : G_("parameter pack cannot have a "
22556 "default argument"));
22558 default_argument
= NULL_TREE
;
22562 default_argument
= NULL_TREE
;
22564 if (default_argument
)
22565 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
22567 /* Generate a location for the parameter, ranging from the start of the
22568 initial token to the end of the final token (using input_location for
22569 the latter, set up by cp_lexer_set_source_position_from_token when
22572 If we have a identifier, then use it for the caret location, e.g.
22574 extern int callee (int one, int (*two)(int, int), float three);
22575 ~~~~~~^~~~~~~~~~~~~~
22577 otherwise, reuse the start location for the caret location e.g.:
22579 extern int callee (int one, int (*)(int, int), float three);
22583 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22584 ? declarator
->id_loc
22585 : decl_spec_token_start
->location
);
22586 location_t param_loc
= make_location (caret_loc
,
22587 decl_spec_token_start
->location
,
22590 return make_parameter_declarator (&decl_specifiers
,
22594 template_parameter_pack_p
);
22597 /* Parse a default argument and return it.
22599 TEMPLATE_PARM_P is true if this is a default argument for a
22600 non-type template parameter. */
22602 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22604 tree default_argument
= NULL_TREE
;
22605 bool saved_greater_than_is_operator_p
;
22606 unsigned char saved_local_variables_forbidden_p
;
22607 bool non_constant_p
, is_direct_init
;
22609 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22611 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22612 parser
->greater_than_is_operator_p
= !template_parm_p
;
22613 /* Local variable names (and the `this' keyword) may not
22614 appear in a default argument. */
22615 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22616 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
22617 /* Parse the assignment-expression. */
22618 if (template_parm_p
)
22619 push_deferring_access_checks (dk_no_deferred
);
22620 tree saved_class_ptr
= NULL_TREE
;
22621 tree saved_class_ref
= NULL_TREE
;
22622 /* The "this" pointer is not valid in a default argument. */
22625 saved_class_ptr
= current_class_ptr
;
22626 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22627 saved_class_ref
= current_class_ref
;
22628 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22631 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22632 /* Restore the "this" pointer. */
22635 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22636 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22638 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22639 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22640 if (template_parm_p
)
22641 pop_deferring_access_checks ();
22642 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22643 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22645 return default_argument
;
22648 /* Parse a function-body.
22651 compound_statement */
22654 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22656 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22657 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22661 /* Parse a ctor-initializer-opt followed by a function-body. Return
22662 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22663 is true we are parsing a function-try-block. */
22666 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22667 bool in_function_try_block
)
22670 const bool check_body_p
22671 = (DECL_CONSTRUCTOR_P (current_function_decl
)
22672 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
22675 if (in_function_try_block
22676 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
22677 && cxx_dialect
< cxx2a
)
22679 if (DECL_CONSTRUCTOR_P (current_function_decl
))
22680 pedwarn (input_location
, 0,
22681 "function-try-block body of %<constexpr%> constructor only "
22682 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22684 pedwarn (input_location
, 0,
22685 "function-try-block body of %<constexpr%> function only "
22686 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22689 /* Begin the function body. */
22690 body
= begin_function_body ();
22691 /* Parse the optional ctor-initializer. */
22692 cp_parser_ctor_initializer_opt (parser
);
22694 /* If we're parsing a constexpr constructor definition, we need
22695 to check that the constructor body is indeed empty. However,
22696 before we get to cp_parser_function_body lot of junk has been
22697 generated, so we can't just check that we have an empty block.
22698 Rather we take a snapshot of the outermost block, and check whether
22699 cp_parser_function_body changed its state. */
22702 list
= cur_stmt_list
;
22703 if (STATEMENT_LIST_TAIL (list
))
22704 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22706 /* Parse the function-body. */
22707 cp_parser_function_body (parser
, in_function_try_block
);
22709 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22710 /* Finish the function body. */
22711 finish_function_body (body
);
22714 /* Parse an initializer.
22717 = initializer-clause
22718 ( expression-list )
22720 Returns an expression representing the initializer. If no
22721 initializer is present, NULL_TREE is returned.
22723 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22724 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22725 set to TRUE if there is no initializer present. If there is an
22726 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22727 is set to true; otherwise it is set to false. */
22730 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22731 bool* non_constant_p
, bool subexpression_p
)
22736 /* Peek at the next token. */
22737 token
= cp_lexer_peek_token (parser
->lexer
);
22739 /* Let our caller know whether or not this initializer was
22741 *is_direct_init
= (token
->type
!= CPP_EQ
);
22742 /* Assume that the initializer is constant. */
22743 *non_constant_p
= false;
22745 if (token
->type
== CPP_EQ
)
22747 /* Consume the `='. */
22748 cp_lexer_consume_token (parser
->lexer
);
22749 /* Parse the initializer-clause. */
22750 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22752 else if (token
->type
== CPP_OPEN_PAREN
)
22754 vec
<tree
, va_gc
> *vec
;
22755 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22757 /*allow_expansion_p=*/true,
22760 return error_mark_node
;
22761 init
= build_tree_list_vec (vec
);
22762 release_tree_vector (vec
);
22764 else if (token
->type
== CPP_OPEN_BRACE
)
22766 cp_lexer_set_source_position (parser
->lexer
);
22767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22768 init
= cp_parser_braced_list (parser
, non_constant_p
);
22769 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22773 /* Anything else is an error. */
22774 cp_parser_error (parser
, "expected initializer");
22775 init
= error_mark_node
;
22778 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22779 init
= error_mark_node
;
22784 /* Parse an initializer-clause.
22786 initializer-clause:
22787 assignment-expression
22790 Returns an expression representing the initializer.
22792 If the `assignment-expression' production is used the value
22793 returned is simply a representation for the expression.
22795 Otherwise, calls cp_parser_braced_list. */
22798 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22800 cp_expr initializer
;
22802 /* Assume the expression is constant. */
22803 *non_constant_p
= false;
22805 /* If it is not a `{', then we are looking at an
22806 assignment-expression. */
22807 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22810 = cp_parser_constant_expression (parser
,
22811 /*allow_non_constant_p=*/true,
22815 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22817 return initializer
;
22820 /* Parse a brace-enclosed initializer list.
22823 { initializer-list , [opt] }
22824 { designated-initializer-list , [opt] }
22827 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22828 the elements of the initializer-list (or NULL, if the last
22829 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22830 NULL_TREE. There is no way to detect whether or not the optional
22831 trailing `,' was provided. NON_CONSTANT_P is as for
22832 cp_parser_initializer. */
22835 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22838 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22840 /* Consume the `{' token. */
22841 matching_braces braces
;
22842 braces
.require_open (parser
);
22843 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22844 initializer
= make_node (CONSTRUCTOR
);
22845 /* If it's not a `}', then there is a non-trivial initializer. */
22846 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22849 /* Parse the initializer list. */
22850 CONSTRUCTOR_ELTS (initializer
)
22851 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
22852 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
22853 /* A trailing `,' token is allowed. */
22854 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22855 cp_lexer_consume_token (parser
->lexer
);
22858 *non_constant_p
= false;
22859 /* Now, there should be a trailing `}'. */
22860 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22861 braces
.require_close (parser
);
22862 TREE_TYPE (initializer
) = init_list_type_node
;
22864 cp_expr
result (initializer
);
22865 /* Build a location of the form:
22868 with caret==start at the open brace, finish at the close brace. */
22869 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22870 result
.set_location (combined_loc
);
22874 /* Consume tokens up to, and including, the next non-nested closing `]'.
22875 Returns true iff we found a closing `]'. */
22878 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22880 unsigned square_depth
= 0;
22884 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22886 switch (token
->type
)
22888 case CPP_PRAGMA_EOL
:
22889 if (!parser
->lexer
->in_pragma
)
22893 /* If we've run out of tokens, then there is no closing `]'. */
22896 case CPP_OPEN_SQUARE
:
22900 case CPP_CLOSE_SQUARE
:
22901 if (!square_depth
--)
22903 cp_lexer_consume_token (parser
->lexer
);
22912 /* Consume the token. */
22913 cp_lexer_consume_token (parser
->lexer
);
22917 /* Return true if we are looking at an array-designator, false otherwise. */
22920 cp_parser_array_designator_p (cp_parser
*parser
)
22922 /* Consume the `['. */
22923 cp_lexer_consume_token (parser
->lexer
);
22925 cp_lexer_save_tokens (parser
->lexer
);
22927 /* Skip tokens until the next token is a closing square bracket.
22928 If we find the closing `]', and the next token is a `=', then
22929 we are looking at an array designator. */
22930 bool array_designator_p
22931 = (cp_parser_skip_to_closing_square_bracket (parser
)
22932 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22934 /* Roll back the tokens we skipped. */
22935 cp_lexer_rollback_tokens (parser
->lexer
);
22937 return array_designator_p
;
22940 /* Parse an initializer-list.
22943 initializer-clause ... [opt]
22944 initializer-list , initializer-clause ... [opt]
22948 designated-initializer-list:
22949 designated-initializer-clause
22950 designated-initializer-list , designated-initializer-clause
22952 designated-initializer-clause:
22953 designator brace-or-equal-initializer
22961 designation initializer-clause ...[opt]
22962 initializer-list , designation initializer-clause ...[opt]
22967 [ constant-expression ] =
22969 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22970 for the initializer. If the INDEX of the elt is non-NULL, it is the
22971 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22972 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22973 are any designators. */
22975 static vec
<constructor_elt
, va_gc
> *
22976 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
22979 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22980 bool first_p
= true;
22981 tree first_designator
= NULL_TREE
;
22983 /* Assume all of the expressions are constant. */
22984 *non_constant_p
= false;
22986 /* Parse the rest of the list. */
22992 bool clause_non_constant_p
;
22993 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22995 /* Handle the C++2A syntax, '. id ='. */
22996 if ((cxx_dialect
>= cxx2a
22997 || cp_parser_allow_gnu_extensions_p (parser
))
22998 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22999 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
23000 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
23001 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
23002 == CPP_OPEN_BRACE
)))
23004 if (cxx_dialect
< cxx2a
)
23005 pedwarn (loc
, OPT_Wpedantic
,
23006 "C++ designated initializers only available with "
23007 "%<-std=c++2a%> or %<-std=gnu++2a%>");
23008 /* Consume the `.'. */
23009 cp_lexer_consume_token (parser
->lexer
);
23010 /* Consume the identifier. */
23011 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23013 /* Consume the `='. */
23014 cp_lexer_consume_token (parser
->lexer
);
23016 /* Also, if the next token is an identifier and the following one is a
23017 colon, we are looking at the GNU designated-initializer
23019 else if (cp_parser_allow_gnu_extensions_p (parser
)
23020 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
23021 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23024 /* Warn the user that they are using an extension. */
23025 pedwarn (loc
, OPT_Wpedantic
,
23026 "ISO C++ does not allow GNU designated initializers");
23027 /* Consume the identifier. */
23028 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23029 /* Consume the `:'. */
23030 cp_lexer_consume_token (parser
->lexer
);
23032 /* Also handle C99 array designators, '[ const ] ='. */
23033 else if (cp_parser_allow_gnu_extensions_p (parser
)
23034 && !c_dialect_objc ()
23035 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
23037 /* In C++11, [ could start a lambda-introducer. */
23038 bool non_const
= false;
23040 cp_parser_parse_tentatively (parser
);
23042 if (!cp_parser_array_designator_p (parser
))
23044 cp_parser_simulate_error (parser
);
23045 designator
= NULL_TREE
;
23049 designator
= cp_parser_constant_expression (parser
, true,
23051 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23052 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23055 if (!cp_parser_parse_definitely (parser
))
23056 designator
= NULL_TREE
;
23058 && (!require_potential_rvalue_constant_expression
23060 designator
= NULL_TREE
;
23062 /* Warn the user that they are using an extension. */
23063 pedwarn (loc
, OPT_Wpedantic
,
23064 "ISO C++ does not allow C99 designated initializers");
23067 designator
= NULL_TREE
;
23071 first_designator
= designator
;
23074 else if (cxx_dialect
>= cxx2a
23075 && first_designator
!= error_mark_node
23076 && (!first_designator
!= !designator
))
23078 error_at (loc
, "either all initializer clauses should be designated "
23079 "or none of them should be");
23080 first_designator
= error_mark_node
;
23082 else if (cxx_dialect
< cxx2a
&& !first_designator
)
23083 first_designator
= designator
;
23085 /* Parse the initializer. */
23086 initializer
= cp_parser_initializer_clause (parser
,
23087 &clause_non_constant_p
);
23088 /* If any clause is non-constant, so is the entire initializer. */
23089 if (clause_non_constant_p
)
23090 *non_constant_p
= true;
23092 /* If we have an ellipsis, this is an initializer pack
23094 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23096 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23098 /* Consume the `...'. */
23099 cp_lexer_consume_token (parser
->lexer
);
23101 if (designator
&& cxx_dialect
>= cxx2a
)
23103 "%<...%> not allowed in designated initializer list");
23105 /* Turn the initializer into an initializer expansion. */
23106 initializer
= make_pack_expansion (initializer
);
23109 /* Add it to the vector. */
23110 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
23112 /* If the next token is not a comma, we have reached the end of
23114 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23117 /* Peek at the next token. */
23118 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23119 /* If the next token is a `}', then we're still done. An
23120 initializer-clause can have a trailing `,' after the
23121 initializer-list and before the closing `}'. */
23122 if (token
->type
== CPP_CLOSE_BRACE
)
23125 /* Consume the `,' token. */
23126 cp_lexer_consume_token (parser
->lexer
);
23129 /* The same identifier shall not appear in multiple designators
23130 of a designated-initializer-list. */
23131 if (first_designator
)
23134 tree designator
, val
;
23135 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23136 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23138 if (IDENTIFIER_MARKED (designator
))
23140 error_at (cp_expr_loc_or_loc (val
, input_location
),
23141 "%<.%s%> designator used multiple times in "
23142 "the same initializer list",
23143 IDENTIFIER_POINTER (designator
));
23144 (*v
)[i
].index
= error_mark_node
;
23147 IDENTIFIER_MARKED (designator
) = 1;
23149 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23150 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23151 IDENTIFIER_MARKED (designator
) = 0;
23154 *designated
= first_designator
!= NULL_TREE
;
23158 /* Classes [gram.class] */
23160 /* Parse a class-name.
23166 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23167 to indicate that names looked up in dependent types should be
23168 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23169 keyword has been used to indicate that the name that appears next
23170 is a template. TAG_TYPE indicates the explicit tag given before
23171 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23172 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23173 is the class being defined in a class-head. If ENUM_OK is TRUE,
23174 enum-names are also accepted.
23176 Returns the TYPE_DECL representing the class. */
23179 cp_parser_class_name (cp_parser
*parser
,
23180 bool typename_keyword_p
,
23181 bool template_keyword_p
,
23182 enum tag_types tag_type
,
23183 bool check_dependency_p
,
23185 bool is_declaration
,
23192 tree identifier
= NULL_TREE
;
23194 /* All class-names start with an identifier. */
23195 token
= cp_lexer_peek_token (parser
->lexer
);
23196 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23198 cp_parser_error (parser
, "expected class-name");
23199 return error_mark_node
;
23202 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23203 to a template-id, so we save it here. */
23204 scope
= parser
->scope
;
23205 if (scope
== error_mark_node
)
23206 return error_mark_node
;
23208 /* Any name names a type if we're following the `typename' keyword
23209 in a qualified name where the enclosing scope is type-dependent. */
23210 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
23211 && dependent_type_p (scope
));
23212 /* Handle the common case (an identifier, but not a template-id)
23214 if (token
->type
== CPP_NAME
23215 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
23217 cp_token
*identifier_token
;
23220 /* Look for the identifier. */
23221 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
23222 ambiguous_p
= identifier_token
->error_reported
;
23223 identifier
= cp_parser_identifier (parser
);
23224 /* If the next token isn't an identifier, we are certainly not
23225 looking at a class-name. */
23226 if (identifier
== error_mark_node
)
23227 decl
= error_mark_node
;
23228 /* If we know this is a type-name, there's no need to look it
23230 else if (typename_p
)
23234 tree ambiguous_decls
;
23235 /* If we already know that this lookup is ambiguous, then
23236 we've already issued an error message; there's no reason
23240 cp_parser_simulate_error (parser
);
23241 return error_mark_node
;
23243 /* If the next token is a `::', then the name must be a type
23246 [basic.lookup.qual]
23248 During the lookup for a name preceding the :: scope
23249 resolution operator, object, function, and enumerator
23250 names are ignored. */
23251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23252 tag_type
= scope_type
;
23253 /* Look up the name. */
23254 decl
= cp_parser_lookup_name (parser
, identifier
,
23256 /*is_template=*/false,
23257 /*is_namespace=*/false,
23258 check_dependency_p
,
23260 identifier_token
->location
);
23261 if (ambiguous_decls
)
23263 if (cp_parser_parsing_tentatively (parser
))
23264 cp_parser_simulate_error (parser
);
23265 return error_mark_node
;
23271 /* Try a template-id. */
23272 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23273 check_dependency_p
,
23276 if (decl
== error_mark_node
)
23277 return error_mark_node
;
23280 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23282 /* If this is a typename, create a TYPENAME_TYPE. */
23284 && decl
!= error_mark_node
23285 && !is_overloaded_fn (decl
))
23287 decl
= make_typename_type (scope
, decl
, typename_type
,
23288 /*complain=*/tf_error
);
23289 if (decl
!= error_mark_node
)
23290 decl
= TYPE_NAME (decl
);
23293 decl
= strip_using_decl (decl
);
23295 /* Check to see that it is really the name of a class. */
23296 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23297 && identifier_p (TREE_OPERAND (decl
, 0))
23298 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23299 /* Situations like this:
23301 template <typename T> struct A {
23302 typename T::template X<int>::I i;
23305 are problematic. Is `T::template X<int>' a class-name? The
23306 standard does not seem to be definitive, but there is no other
23307 valid interpretation of the following `::'. Therefore, those
23308 names are considered class-names. */
23310 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23311 if (decl
!= error_mark_node
)
23312 decl
= TYPE_NAME (decl
);
23314 else if (TREE_CODE (decl
) != TYPE_DECL
23315 || TREE_TYPE (decl
) == error_mark_node
23316 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23317 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23318 /* In Objective-C 2.0, a classname followed by '.' starts a
23319 dot-syntax expression, and it's not a type-name. */
23320 || (c_dialect_objc ()
23321 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23322 && objc_is_class_name (decl
)))
23323 decl
= error_mark_node
;
23325 if (decl
== error_mark_node
)
23326 cp_parser_error (parser
, "expected class-name");
23327 else if (identifier
&& !parser
->scope
)
23328 maybe_note_name_used_in_class (identifier
, decl
);
23333 /* Parse a class-specifier.
23336 class-head { member-specification [opt] }
23338 Returns the TREE_TYPE representing the class. */
23341 cp_parser_class_specifier_1 (cp_parser
* parser
)
23344 tree attributes
= NULL_TREE
;
23345 bool nested_name_specifier_p
;
23346 unsigned saved_num_template_parameter_lists
;
23347 bool saved_in_function_body
;
23348 unsigned char in_statement
;
23349 bool in_switch_statement_p
;
23350 bool saved_in_unbraced_linkage_specification_p
;
23351 tree old_scope
= NULL_TREE
;
23352 tree scope
= NULL_TREE
;
23353 cp_token
*closing_brace
;
23355 push_deferring_access_checks (dk_no_deferred
);
23357 /* Parse the class-head. */
23358 type
= cp_parser_class_head (parser
,
23359 &nested_name_specifier_p
);
23360 /* If the class-head was a semantic disaster, skip the entire body
23364 cp_parser_skip_to_end_of_block_or_statement (parser
);
23365 pop_deferring_access_checks ();
23366 return error_mark_node
;
23369 /* Look for the `{'. */
23370 matching_braces braces
;
23371 if (!braces
.require_open (parser
))
23373 pop_deferring_access_checks ();
23374 return error_mark_node
;
23377 cp_ensure_no_omp_declare_simd (parser
);
23378 cp_ensure_no_oacc_routine (parser
);
23380 /* Issue an error message if type-definitions are forbidden here. */
23381 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
23382 /* Remember that we are defining one more class. */
23383 ++parser
->num_classes_being_defined
;
23384 /* Inside the class, surrounding template-parameter-lists do not
23386 saved_num_template_parameter_lists
23387 = parser
->num_template_parameter_lists
;
23388 parser
->num_template_parameter_lists
= 0;
23389 /* We are not in a function body. */
23390 saved_in_function_body
= parser
->in_function_body
;
23391 parser
->in_function_body
= false;
23392 /* Or in a loop. */
23393 in_statement
= parser
->in_statement
;
23394 parser
->in_statement
= 0;
23395 /* Or in a switch. */
23396 in_switch_statement_p
= parser
->in_switch_statement_p
;
23397 parser
->in_switch_statement_p
= false;
23398 /* We are not immediately inside an extern "lang" block. */
23399 saved_in_unbraced_linkage_specification_p
23400 = parser
->in_unbraced_linkage_specification_p
;
23401 parser
->in_unbraced_linkage_specification_p
= false;
23403 // Associate constraints with the type.
23405 type
= associate_classtype_constraints (type
);
23407 /* Start the class. */
23408 if (nested_name_specifier_p
)
23410 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23411 old_scope
= push_inner_scope (scope
);
23413 type
= begin_class_definition (type
);
23415 if (type
== error_mark_node
)
23416 /* If the type is erroneous, skip the entire body of the class. */
23417 cp_parser_skip_to_closing_brace (parser
);
23419 /* Parse the member-specification. */
23420 cp_parser_member_specification_opt (parser
);
23422 /* Look for the trailing `}'. */
23423 closing_brace
= braces
.require_close (parser
);
23424 /* Look for trailing attributes to apply to this class. */
23425 if (cp_parser_allow_gnu_extensions_p (parser
))
23426 attributes
= cp_parser_gnu_attributes_opt (parser
);
23427 if (type
!= error_mark_node
)
23428 type
= finish_struct (type
, attributes
);
23429 if (nested_name_specifier_p
)
23430 pop_inner_scope (old_scope
, scope
);
23432 /* We've finished a type definition. Check for the common syntax
23433 error of forgetting a semicolon after the definition. We need to
23434 be careful, as we can't just check for not-a-semicolon and be done
23435 with it; the user might have typed:
23437 class X { } c = ...;
23438 class X { } *p = ...;
23440 and so forth. Instead, enumerate all the possible tokens that
23441 might follow this production; if we don't see one of them, then
23442 complain and silently insert the semicolon. */
23444 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23445 bool want_semicolon
= true;
23447 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23448 /* Don't try to parse c++11 attributes here. As per the
23449 grammar, that should be a task for
23450 cp_parser_decl_specifier_seq. */
23451 want_semicolon
= false;
23453 switch (token
->type
)
23456 case CPP_SEMICOLON
:
23459 case CPP_OPEN_PAREN
:
23460 case CPP_CLOSE_PAREN
:
23462 want_semicolon
= false;
23465 /* While it's legal for type qualifiers and storage class
23466 specifiers to follow type definitions in the grammar, only
23467 compiler testsuites contain code like that. Assume that if
23468 we see such code, then what we're really seeing is a case
23472 const <type> var = ...;
23477 static <type> func (...) ...
23479 i.e. the qualifier or specifier applies to the next
23480 declaration. To do so, however, we need to look ahead one
23481 more token to see if *that* token is a type specifier.
23483 This code could be improved to handle:
23486 static const <type> var = ...; */
23488 if (keyword_is_decl_specifier (token
->keyword
))
23490 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23492 /* Handling user-defined types here would be nice, but very
23495 = (lookahead
->type
== CPP_KEYWORD
23496 && keyword_begins_type_specifier (lookahead
->keyword
));
23503 /* If we don't have a type, then something is very wrong and we
23504 shouldn't try to do anything clever. Likewise for not seeing the
23506 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23508 /* Locate the closing brace. */
23509 cp_token_position prev
23510 = cp_lexer_previous_token_position (parser
->lexer
);
23511 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23512 location_t loc
= prev_token
->location
;
23514 /* We want to suggest insertion of a ';' immediately *after* the
23515 closing brace, so, if we can, offset the location by 1 column. */
23516 location_t next_loc
= loc
;
23517 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23518 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23520 rich_location
richloc (line_table
, next_loc
);
23522 /* If we successfully offset the location, suggest the fix-it. */
23523 if (next_loc
!= loc
)
23524 richloc
.add_fixit_insert_before (next_loc
, ";");
23526 if (CLASSTYPE_DECLARED_CLASS (type
))
23527 error_at (&richloc
,
23528 "expected %<;%> after class definition");
23529 else if (TREE_CODE (type
) == RECORD_TYPE
)
23530 error_at (&richloc
,
23531 "expected %<;%> after struct definition");
23532 else if (TREE_CODE (type
) == UNION_TYPE
)
23533 error_at (&richloc
,
23534 "expected %<;%> after union definition");
23536 gcc_unreachable ();
23538 /* Unget one token and smash it to look as though we encountered
23539 a semicolon in the input stream. */
23540 cp_lexer_set_token_position (parser
->lexer
, prev
);
23541 token
= cp_lexer_peek_token (parser
->lexer
);
23542 token
->type
= CPP_SEMICOLON
;
23543 token
->keyword
= RID_MAX
;
23547 /* If this class is not itself within the scope of another class,
23548 then we need to parse the bodies of all of the queued function
23549 definitions. Note that the queued functions defined in a class
23550 are not always processed immediately following the
23551 class-specifier for that class. Consider:
23554 struct B { void f() { sizeof (A); } };
23557 If `f' were processed before the processing of `A' were
23558 completed, there would be no way to compute the size of `A'.
23559 Note that the nesting we are interested in here is lexical --
23560 not the semantic nesting given by TYPE_CONTEXT. In particular,
23563 struct A { struct B; };
23564 struct A::B { void f() { } };
23566 there is no need to delay the parsing of `A::B::f'. */
23567 if (--parser
->num_classes_being_defined
== 0)
23570 tree class_type
= NULL_TREE
;
23571 tree pushed_scope
= NULL_TREE
;
23573 cp_default_arg_entry
*e
;
23574 tree save_ccp
, save_ccr
;
23576 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
23578 /* Skip default arguments, NSDMIs, etc, in order to improve
23579 error recovery (c++/71169, c++/71832). */
23580 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23581 vec_safe_truncate (unparsed_nsdmis
, 0);
23582 vec_safe_truncate (unparsed_classes
, 0);
23583 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23586 /* In a first pass, parse default arguments to the functions.
23587 Then, in a second pass, parse the bodies of the functions.
23588 This two-phased approach handles cases like:
23596 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23599 /* If there are default arguments that have not yet been processed,
23600 take care of them now. */
23601 if (class_type
!= e
->class_type
)
23604 pop_scope (pushed_scope
);
23605 class_type
= e
->class_type
;
23606 pushed_scope
= push_scope (class_type
);
23608 /* Make sure that any template parameters are in scope. */
23609 maybe_begin_member_template_processing (decl
);
23610 /* Parse the default argument expressions. */
23611 cp_parser_late_parsing_default_args (parser
, decl
);
23612 /* Remove any template parameters from the symbol table. */
23613 maybe_end_member_template_processing ();
23615 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23616 /* Now parse any NSDMIs. */
23617 save_ccp
= current_class_ptr
;
23618 save_ccr
= current_class_ref
;
23619 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23621 if (class_type
!= DECL_CONTEXT (decl
))
23624 pop_scope (pushed_scope
);
23625 class_type
= DECL_CONTEXT (decl
);
23626 pushed_scope
= push_scope (class_type
);
23628 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23629 cp_parser_late_parsing_nsdmi (parser
, decl
);
23631 vec_safe_truncate (unparsed_nsdmis
, 0);
23632 current_class_ptr
= save_ccp
;
23633 current_class_ref
= save_ccr
;
23635 pop_scope (pushed_scope
);
23637 /* Now do some post-NSDMI bookkeeping. */
23638 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
23639 after_nsdmi_defaulted_late_checks (class_type
);
23640 vec_safe_truncate (unparsed_classes
, 0);
23641 after_nsdmi_defaulted_late_checks (type
);
23643 /* Now parse the body of the functions. */
23646 /* OpenMP UDRs need to be parsed before all other functions. */
23647 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23648 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23649 cp_parser_late_parsing_for_member (parser
, decl
);
23650 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23651 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23652 cp_parser_late_parsing_for_member (parser
, decl
);
23655 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23656 cp_parser_late_parsing_for_member (parser
, decl
);
23657 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23660 vec_safe_push (unparsed_classes
, type
);
23662 /* Put back any saved access checks. */
23663 pop_deferring_access_checks ();
23665 /* Restore saved state. */
23666 parser
->in_switch_statement_p
= in_switch_statement_p
;
23667 parser
->in_statement
= in_statement
;
23668 parser
->in_function_body
= saved_in_function_body
;
23669 parser
->num_template_parameter_lists
23670 = saved_num_template_parameter_lists
;
23671 parser
->in_unbraced_linkage_specification_p
23672 = saved_in_unbraced_linkage_specification_p
;
23678 cp_parser_class_specifier (cp_parser
* parser
)
23681 timevar_push (TV_PARSE_STRUCT
);
23682 ret
= cp_parser_class_specifier_1 (parser
);
23683 timevar_pop (TV_PARSE_STRUCT
);
23687 /* Parse a class-head.
23690 class-key identifier [opt] base-clause [opt]
23691 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23692 class-key nested-name-specifier [opt] template-id
23695 class-virt-specifier:
23699 class-key attributes identifier [opt] base-clause [opt]
23700 class-key attributes nested-name-specifier identifier base-clause [opt]
23701 class-key attributes nested-name-specifier [opt] template-id
23704 Upon return BASES is initialized to the list of base classes (or
23705 NULL, if there are none) in the same form returned by
23706 cp_parser_base_clause.
23708 Returns the TYPE of the indicated class. Sets
23709 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23710 involving a nested-name-specifier was used, and FALSE otherwise.
23712 Returns error_mark_node if this is not a class-head.
23714 Returns NULL_TREE if the class-head is syntactically valid, but
23715 semantically invalid in a way that means we should skip the entire
23716 body of the class. */
23719 cp_parser_class_head (cp_parser
* parser
,
23720 bool* nested_name_specifier_p
)
23722 tree nested_name_specifier
;
23723 enum tag_types class_key
;
23724 tree id
= NULL_TREE
;
23725 tree type
= NULL_TREE
;
23728 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
23729 bool template_id_p
= false;
23730 bool qualified_p
= false;
23731 bool invalid_nested_name_p
= false;
23732 bool invalid_explicit_specialization_p
= false;
23733 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23734 tree pushed_scope
= NULL_TREE
;
23735 unsigned num_templates
;
23736 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
23737 /* Assume no nested-name-specifier will be present. */
23738 *nested_name_specifier_p
= false;
23739 /* Assume no template parameter lists will be used in defining the
23742 parser
->colon_corrects_to_scope_p
= false;
23744 /* Look for the class-key. */
23745 class_key
= cp_parser_class_key (parser
);
23746 if (class_key
== none_type
)
23747 return error_mark_node
;
23749 location_t class_head_start_location
= input_location
;
23751 /* Parse the attributes. */
23752 attributes
= cp_parser_attributes_opt (parser
);
23754 /* If the next token is `::', that is invalid -- but sometimes
23755 people do try to write:
23759 Handle this gracefully by accepting the extra qualifier, and then
23760 issuing an error about it later if this really is a
23761 class-head. If it turns out just to be an elaborated type
23762 specifier, remain silent. */
23763 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23764 qualified_p
= true;
23766 push_deferring_access_checks (dk_no_check
);
23768 /* Determine the name of the class. Begin by looking for an
23769 optional nested-name-specifier. */
23770 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23771 nested_name_specifier
23772 = cp_parser_nested_name_specifier_opt (parser
,
23773 /*typename_keyword_p=*/false,
23774 /*check_dependency_p=*/false,
23776 /*is_declaration=*/false);
23777 /* If there was a nested-name-specifier, then there *must* be an
23780 cp_token
*bad_template_keyword
= NULL
;
23782 if (nested_name_specifier
)
23784 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23785 /* Although the grammar says `identifier', it really means
23786 `class-name' or `template-name'. You are only allowed to
23787 define a class that has already been declared with this
23790 The proposed resolution for Core Issue 180 says that wherever
23791 you see `class T::X' you should treat `X' as a type-name.
23793 It is OK to define an inaccessible class; for example:
23795 class A { class B; };
23798 We do not know if we will see a class-name, or a
23799 template-name. We look for a class-name first, in case the
23800 class-name is a template-id; if we looked for the
23801 template-name first we would stop after the template-name. */
23802 cp_parser_parse_tentatively (parser
);
23803 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23804 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23805 type
= cp_parser_class_name (parser
,
23806 /*typename_keyword_p=*/false,
23807 /*template_keyword_p=*/false,
23809 /*check_dependency_p=*/false,
23810 /*class_head_p=*/true,
23811 /*is_declaration=*/false);
23812 /* If that didn't work, ignore the nested-name-specifier. */
23813 if (!cp_parser_parse_definitely (parser
))
23815 invalid_nested_name_p
= true;
23816 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23817 id
= cp_parser_identifier (parser
);
23818 if (id
== error_mark_node
)
23821 /* If we could not find a corresponding TYPE, treat this
23822 declaration like an unqualified declaration. */
23823 if (type
== error_mark_node
)
23824 nested_name_specifier
= NULL_TREE
;
23825 /* Otherwise, count the number of templates used in TYPE and its
23826 containing scopes. */
23828 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23830 /* Otherwise, the identifier is optional. */
23833 /* We don't know whether what comes next is a template-id,
23834 an identifier, or nothing at all. */
23835 cp_parser_parse_tentatively (parser
);
23836 /* Check for a template-id. */
23837 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23838 id
= cp_parser_template_id (parser
,
23839 /*template_keyword_p=*/false,
23840 /*check_dependency_p=*/true,
23842 /*is_declaration=*/true);
23843 /* If that didn't work, it could still be an identifier. */
23844 if (!cp_parser_parse_definitely (parser
))
23846 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23848 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23849 id
= cp_parser_identifier (parser
);
23856 template_id_p
= true;
23861 pop_deferring_access_checks ();
23865 cp_parser_check_for_invalid_template_id (parser
, id
,
23867 type_start_token
->location
);
23869 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23871 /* If it's not a `:' or a `{' then we can't really be looking at a
23872 class-head, since a class-head only appears as part of a
23873 class-specifier. We have to detect this situation before calling
23874 xref_tag, since that has irreversible side-effects. */
23875 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23877 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23878 type
= error_mark_node
;
23882 /* At this point, we're going ahead with the class-specifier, even
23883 if some other problem occurs. */
23884 cp_parser_commit_to_tentative_parse (parser
);
23885 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23887 cp_parser_error (parser
,
23888 "cannot specify %<override%> for a class");
23889 type
= error_mark_node
;
23892 /* Issue the error about the overly-qualified name now. */
23895 cp_parser_error (parser
,
23896 "global qualification of class name is invalid");
23897 type
= error_mark_node
;
23900 else if (invalid_nested_name_p
)
23902 cp_parser_error (parser
,
23903 "qualified name does not name a class");
23904 type
= error_mark_node
;
23907 else if (nested_name_specifier
)
23911 if (bad_template_keyword
)
23912 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23913 keyword template shall not appear at the top level. */
23914 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23915 "keyword %<template%> not allowed in class-head-name");
23917 /* Reject typedef-names in class heads. */
23918 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23920 error_at (type_start_token
->location
,
23921 "invalid class name in declaration of %qD",
23927 /* Figure out in what scope the declaration is being placed. */
23928 scope
= current_scope ();
23929 /* If that scope does not contain the scope in which the
23930 class was originally declared, the program is invalid. */
23931 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23933 if (at_namespace_scope_p ())
23934 error_at (type_start_token
->location
,
23935 "declaration of %qD in namespace %qD which does not "
23937 type
, scope
, nested_name_specifier
);
23939 error_at (type_start_token
->location
,
23940 "declaration of %qD in %qD which does not enclose %qD",
23941 type
, scope
, nested_name_specifier
);
23947 A declarator-id shall not be qualified except for the
23948 definition of a ... nested class outside of its class
23949 ... [or] the definition or explicit instantiation of a
23950 class member of a namespace outside of its namespace. */
23951 if (scope
== nested_name_specifier
)
23953 permerror (nested_name_specifier_token_start
->location
,
23954 "extra qualification not allowed");
23955 nested_name_specifier
= NULL_TREE
;
23959 /* An explicit-specialization must be preceded by "template <>". If
23960 it is not, try to recover gracefully. */
23961 if (at_namespace_scope_p ()
23962 && parser
->num_template_parameter_lists
== 0
23963 && !processing_template_parmlist
23966 /* Build a location of this form:
23967 struct typename <ARGS>
23968 ^~~~~~~~~~~~~~~~~~~~~~
23969 with caret==start at the start token, and
23970 finishing at the end of the type. */
23971 location_t reported_loc
23972 = make_location (class_head_start_location
,
23973 class_head_start_location
,
23974 get_finish (type_start_token
->location
));
23975 rich_location
richloc (line_table
, reported_loc
);
23976 richloc
.add_fixit_insert_before (class_head_start_location
,
23978 error_at (&richloc
,
23979 "an explicit specialization must be preceded by"
23980 " %<template <>%>");
23981 invalid_explicit_specialization_p
= true;
23982 /* Take the same action that would have been taken by
23983 cp_parser_explicit_specialization. */
23984 ++parser
->num_template_parameter_lists
;
23985 begin_specialization ();
23987 /* There must be no "return" statements between this point and the
23988 end of this function; set "type "to the correct return value and
23989 use "goto done;" to return. */
23990 /* Make sure that the right number of template parameters were
23992 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23994 type_start_token
->location
,
23995 /*declarator=*/NULL
))
23997 /* If something went wrong, there is no point in even trying to
23998 process the class-definition. */
24003 /* Look up the type. */
24006 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
24007 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
24008 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
24010 error_at (type_start_token
->location
,
24011 "function template %qD redeclared as a class template", id
);
24012 type
= error_mark_node
;
24016 type
= TREE_TYPE (id
);
24017 type
= maybe_process_partial_specialization (type
);
24019 /* Check the scope while we still know whether or not we had a
24020 nested-name-specifier. */
24021 if (type
!= error_mark_node
)
24022 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
24024 if (nested_name_specifier
)
24025 pushed_scope
= push_scope (nested_name_specifier
);
24027 else if (nested_name_specifier
)
24033 template <typename T> struct S { struct T };
24034 template <typename T> struct S<T>::T { };
24036 we will get a TYPENAME_TYPE when processing the definition of
24037 `S::T'. We need to resolve it to the actual type before we
24038 try to define it. */
24039 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
24041 class_type
= resolve_typename_type (TREE_TYPE (type
),
24042 /*only_current_p=*/false);
24043 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
24044 type
= TYPE_NAME (class_type
);
24047 cp_parser_error (parser
, "could not resolve typename type");
24048 type
= error_mark_node
;
24052 if (maybe_process_partial_specialization (TREE_TYPE (type
))
24053 == error_mark_node
)
24059 class_type
= current_class_type
;
24060 /* Enter the scope indicated by the nested-name-specifier. */
24061 pushed_scope
= push_scope (nested_name_specifier
);
24062 /* Get the canonical version of this type. */
24063 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
24064 /* Call push_template_decl if it seems like we should be defining a
24065 template either from the template headers or the type we're
24066 defining, so that we diagnose both extra and missing headers. */
24067 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24068 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
24069 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
24071 type
= push_template_decl (type
);
24072 if (type
== error_mark_node
)
24079 type
= TREE_TYPE (type
);
24080 *nested_name_specifier_p
= true;
24082 else /* The name is not a nested name. */
24084 /* If the class was unnamed, create a dummy name. */
24086 id
= make_anon_name ();
24087 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
24088 ? ts_within_enclosing_non_class
24090 type
= xref_tag (class_key
, id
, tag_scope
,
24091 parser
->num_template_parameter_lists
);
24094 /* Indicate whether this class was declared as a `class' or as a
24096 if (TREE_CODE (type
) == RECORD_TYPE
)
24097 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
24098 cp_parser_check_class_key (class_key
, type
);
24100 /* If this type was already complete, and we see another definition,
24101 that's an error. Likewise if the type is already being defined:
24102 this can happen, eg, when it's defined from within an expression
24104 if (type
!= error_mark_node
24105 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
24107 error_at (type_start_token
->location
, "redefinition of %q#T",
24109 inform (location_of (type
), "previous definition of %q#T",
24114 else if (type
== error_mark_node
)
24119 /* Apply attributes now, before any use of the class as a template
24120 argument in its base list. */
24121 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
24122 fixup_attribute_variants (type
);
24125 /* We will have entered the scope containing the class; the names of
24126 base classes should be looked up in that context. For example:
24128 struct A { struct B {}; struct C; };
24129 struct A::C : B {};
24133 /* Get the list of base-classes, if there is one. */
24134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
24136 /* PR59482: enter the class scope so that base-specifiers are looked
24140 bases
= cp_parser_base_clause (parser
);
24141 /* PR59482: get out of the previously pushed class scope so that the
24142 subsequent pops pop the right thing. */
24149 /* If we're really defining a class, process the base classes.
24150 If they're invalid, fail. */
24151 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24152 xref_basetypes (type
, bases
);
24155 /* Leave the scope given by the nested-name-specifier. We will
24156 enter the class scope itself while processing the members. */
24158 pop_scope (pushed_scope
);
24160 if (invalid_explicit_specialization_p
)
24162 end_specialization ();
24163 --parser
->num_template_parameter_lists
;
24167 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
24168 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
24169 CLASSTYPE_FINAL (type
) = 1;
24171 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24175 /* Parse a class-key.
24182 Returns the kind of class-key specified, or none_type to indicate
24185 static enum tag_types
24186 cp_parser_class_key (cp_parser
* parser
)
24189 enum tag_types tag_type
;
24191 /* Look for the class-key. */
24192 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
24196 /* Check to see if the TOKEN is a class-key. */
24197 tag_type
= cp_parser_token_is_class_key (token
);
24199 cp_parser_error (parser
, "expected class-key");
24203 /* Parse a type-parameter-key.
24205 type-parameter-key:
24211 cp_parser_type_parameter_key (cp_parser
* parser
)
24213 /* Look for the type-parameter-key. */
24214 enum tag_types tag_type
= none_type
;
24215 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24216 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
24218 cp_lexer_consume_token (parser
->lexer
);
24219 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
24220 /* typename is not allowed in a template template parameter
24221 by the standard until C++17. */
24222 pedwarn (token
->location
, OPT_Wpedantic
,
24223 "ISO C++ forbids typename key in template template parameter;"
24224 " use %<-std=c++17%> or %<-std=gnu++17%>");
24227 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
24232 /* Parse an (optional) member-specification.
24234 member-specification:
24235 member-declaration member-specification [opt]
24236 access-specifier : member-specification [opt] */
24239 cp_parser_member_specification_opt (cp_parser
* parser
)
24246 /* Peek at the next token. */
24247 token
= cp_lexer_peek_token (parser
->lexer
);
24248 /* If it's a `}', or EOF then we've seen all the members. */
24249 if (token
->type
== CPP_CLOSE_BRACE
24250 || token
->type
== CPP_EOF
24251 || token
->type
== CPP_PRAGMA_EOL
)
24254 /* See if this token is a keyword. */
24255 keyword
= token
->keyword
;
24259 case RID_PROTECTED
:
24261 /* Consume the access-specifier. */
24262 cp_lexer_consume_token (parser
->lexer
);
24263 /* Remember which access-specifier is active. */
24264 current_access_specifier
= token
->u
.value
;
24265 /* Look for the `:'. */
24266 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24270 /* Accept #pragmas at class scope. */
24271 if (token
->type
== CPP_PRAGMA
)
24273 cp_parser_pragma (parser
, pragma_member
, NULL
);
24277 /* Otherwise, the next construction must be a
24278 member-declaration. */
24279 cp_parser_member_declaration (parser
);
24284 /* Parse a member-declaration.
24286 member-declaration:
24287 decl-specifier-seq [opt] member-declarator-list [opt] ;
24288 function-definition ; [opt]
24289 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24291 template-declaration
24294 member-declarator-list:
24296 member-declarator-list , member-declarator
24299 declarator pure-specifier [opt]
24300 declarator constant-initializer [opt]
24301 identifier [opt] : constant-expression
24305 member-declaration:
24306 __extension__ member-declaration
24309 declarator attributes [opt] pure-specifier [opt]
24310 declarator attributes [opt] constant-initializer [opt]
24311 identifier [opt] attributes [opt] : constant-expression
24315 member-declaration:
24316 static_assert-declaration */
24319 cp_parser_member_declaration (cp_parser
* parser
)
24321 cp_decl_specifier_seq decl_specifiers
;
24322 tree prefix_attributes
;
24324 int declares_class_or_enum
;
24326 cp_token
*token
= NULL
;
24327 cp_token
*decl_spec_token_start
= NULL
;
24328 cp_token
*initializer_token_start
= NULL
;
24329 int saved_pedantic
;
24330 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24332 /* Check for the `__extension__' keyword. */
24333 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24336 cp_parser_member_declaration (parser
);
24337 /* Restore the old value of the PEDANTIC flag. */
24338 pedantic
= saved_pedantic
;
24343 /* Check for a template-declaration. */
24344 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24346 /* An explicit specialization here is an error condition, and we
24347 expect the specialization handler to detect and report this. */
24348 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24349 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24350 cp_parser_explicit_specialization (parser
);
24352 cp_parser_template_declaration (parser
, /*member_p=*/true);
24356 /* Check for a template introduction. */
24357 else if (cp_parser_template_declaration_after_export (parser
, true))
24360 /* Check for a using-declaration. */
24361 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24363 if (cxx_dialect
< cxx11
)
24365 /* Parse the using-declaration. */
24366 cp_parser_using_declaration (parser
,
24367 /*access_declaration_p=*/false);
24373 bool alias_decl_expected
;
24374 cp_parser_parse_tentatively (parser
);
24375 decl
= cp_parser_alias_declaration (parser
);
24376 /* Note that if we actually see the '=' token after the
24377 identifier, cp_parser_alias_declaration commits the
24378 tentative parse. In that case, we really expect an
24379 alias-declaration. Otherwise, we expect a using
24381 alias_decl_expected
=
24382 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24383 cp_parser_parse_definitely (parser
);
24385 if (alias_decl_expected
)
24386 finish_member_declaration (decl
);
24388 cp_parser_using_declaration (parser
,
24389 /*access_declaration_p=*/false);
24394 /* Check for @defs. */
24395 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24398 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24399 ivar
= ivar_chains
;
24403 ivar
= TREE_CHAIN (member
);
24404 TREE_CHAIN (member
) = NULL_TREE
;
24405 finish_member_declaration (member
);
24410 /* If the next token is `static_assert' we have a static assertion. */
24411 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24413 cp_parser_static_assert (parser
, /*member_p=*/true);
24417 parser
->colon_corrects_to_scope_p
= false;
24419 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24422 /* Parse the decl-specifier-seq. */
24423 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24424 cp_parser_decl_specifier_seq (parser
,
24425 (CP_PARSER_FLAGS_OPTIONAL
24426 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
24428 &declares_class_or_enum
);
24429 /* Check for an invalid type-name. */
24430 if (!decl_specifiers
.any_type_specifiers_p
24431 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24433 /* If there is no declarator, then the decl-specifier-seq should
24435 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24437 /* If there was no decl-specifier-seq, and the next token is a
24438 `;', then we have something like:
24444 Each member-declaration shall declare at least one member
24445 name of the class. */
24446 if (!decl_specifiers
.any_specifiers_p
)
24448 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24449 if (!in_system_header_at (token
->location
))
24451 gcc_rich_location
richloc (token
->location
);
24452 richloc
.add_fixit_remove ();
24453 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24460 /* See if this declaration is a friend. */
24461 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24462 /* If there were decl-specifiers, check to see if there was
24463 a class-declaration. */
24464 type
= check_tag_decl (&decl_specifiers
,
24465 /*explicit_type_instantiation_p=*/false);
24466 /* Nested classes have already been added to the class, but
24467 a `friend' needs to be explicitly registered. */
24470 /* If the `friend' keyword was present, the friend must
24471 be introduced with a class-key. */
24472 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24473 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24474 "in C++03 a class-key must be used "
24475 "when declaring a friend");
24478 template <typename T> struct A {
24479 friend struct A<T>::B;
24482 A<T>::B will be represented by a TYPENAME_TYPE, and
24483 therefore not recognized by check_tag_decl. */
24486 type
= decl_specifiers
.type
;
24487 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24488 type
= TREE_TYPE (type
);
24490 if (!type
|| !TYPE_P (type
))
24491 error_at (decl_spec_token_start
->location
,
24492 "friend declaration does not name a class or "
24495 make_friend_class (current_class_type
, type
,
24496 /*complain=*/true);
24498 /* If there is no TYPE, an error message will already have
24500 else if (!type
|| type
== error_mark_node
)
24502 /* An anonymous aggregate has to be handled specially; such
24503 a declaration really declares a data member (with a
24504 particular type), as opposed to a nested class. */
24505 else if (ANON_AGGR_TYPE_P (type
))
24508 if (decl_specifiers
.storage_class
!= sc_none
)
24509 error_at (decl_spec_token_start
->location
,
24510 "a storage class on an anonymous aggregate "
24511 "in class scope is not allowed");
24513 /* Remove constructors and such from TYPE, now that we
24514 know it is an anonymous aggregate. */
24515 fixup_anonymous_aggr (type
);
24516 /* And make the corresponding data member. */
24517 decl
= build_decl (decl_spec_token_start
->location
,
24518 FIELD_DECL
, NULL_TREE
, type
);
24519 /* Add it to the class. */
24520 finish_member_declaration (decl
);
24523 cp_parser_check_access_in_redeclaration
24525 decl_spec_token_start
->location
);
24530 bool assume_semicolon
= false;
24532 /* Clear attributes from the decl_specifiers but keep them
24533 around as prefix attributes that apply them to the entity
24535 prefix_attributes
= decl_specifiers
.attributes
;
24536 decl_specifiers
.attributes
= NULL_TREE
;
24538 /* See if these declarations will be friends. */
24539 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24541 /* Keep going until we hit the `;' at the end of the
24543 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24545 tree attributes
= NULL_TREE
;
24546 tree first_attribute
;
24548 bool named_bitfld
= false;
24550 /* Peek at the next token. */
24551 token
= cp_lexer_peek_token (parser
->lexer
);
24553 /* The following code wants to know early if it is a bit-field
24554 or some other declaration. Attributes can appear before
24555 the `:' token. Skip over them without consuming any tokens
24556 to peek if they are followed by `:'. */
24557 if (cp_next_tokens_can_be_attribute_p (parser
)
24558 || (token
->type
== CPP_NAME
24559 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24560 && (named_bitfld
= true)))
24563 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24564 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24567 /* Check for a bitfield declaration. */
24568 if (token
->type
== CPP_COLON
24569 || (token
->type
== CPP_NAME
24570 && token
== cp_lexer_peek_token (parser
->lexer
)
24571 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24572 && (named_bitfld
= true)))
24576 tree late_attributes
= NULL_TREE
;
24577 location_t id_location
24578 = cp_lexer_peek_token (parser
->lexer
)->location
;
24581 identifier
= cp_parser_identifier (parser
);
24583 identifier
= NULL_TREE
;
24585 /* Look for attributes that apply to the bitfield. */
24586 attributes
= cp_parser_attributes_opt (parser
);
24588 /* Consume the `:' token. */
24589 cp_lexer_consume_token (parser
->lexer
);
24591 /* Get the width of the bitfield. */
24592 width
= cp_parser_constant_expression (parser
, false, NULL
,
24593 cxx_dialect
>= cxx11
);
24595 /* In C++2A and as extension for C++11 and above we allow
24596 default member initializers for bit-fields. */
24597 initializer
= NULL_TREE
;
24598 if (cxx_dialect
>= cxx11
24599 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24600 || cp_lexer_next_token_is (parser
->lexer
,
24604 = cp_lexer_peek_token (parser
->lexer
)->location
;
24605 if (cxx_dialect
< cxx2a
24606 && !in_system_header_at (loc
)
24607 && identifier
!= NULL_TREE
)
24609 "default member initializers for bit-fields "
24610 "only available with %<-std=c++2a%> or "
24611 "%<-std=gnu++2a%>");
24613 initializer
= cp_parser_save_nsdmi (parser
);
24614 if (identifier
== NULL_TREE
)
24616 error_at (loc
, "default member initializer for "
24617 "unnamed bit-field");
24618 initializer
= NULL_TREE
;
24623 /* Look for attributes that apply to the bitfield after
24624 the `:' token and width. This is where GCC used to
24625 parse attributes in the past, pedwarn if there is
24626 a std attribute. */
24627 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24628 pedwarn (input_location
, OPT_Wpedantic
,
24629 "ISO C++ allows bit-field attributes only "
24630 "before the %<:%> token");
24632 late_attributes
= cp_parser_attributes_opt (parser
);
24635 attributes
= attr_chainon (attributes
, late_attributes
);
24637 /* Remember which attributes are prefix attributes and
24639 first_attribute
= attributes
;
24640 /* Combine the attributes. */
24641 attributes
= attr_chainon (prefix_attributes
, attributes
);
24643 /* Create the bitfield declaration. */
24644 decl
= grokbitfield (identifier
24645 ? make_id_declarator (NULL_TREE
,
24651 width
, initializer
,
24656 cp_declarator
*declarator
;
24657 tree asm_specification
;
24658 int ctor_dtor_or_conv_p
;
24659 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
24661 /* Parse the declarator. */
24663 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24664 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
24665 &ctor_dtor_or_conv_p
,
24666 /*parenthesized_p=*/NULL
,
24668 friend_p
, static_p
);
24670 /* If something went wrong parsing the declarator, make sure
24671 that we at least consume some tokens. */
24672 if (declarator
== cp_error_declarator
)
24674 /* Skip to the end of the statement. */
24675 cp_parser_skip_to_end_of_statement (parser
);
24676 /* If the next token is not a semicolon, that is
24677 probably because we just skipped over the body of
24678 a function. So, we consume a semicolon if
24679 present, but do not issue an error message if it
24681 if (cp_lexer_next_token_is (parser
->lexer
,
24683 cp_lexer_consume_token (parser
->lexer
);
24687 if (declares_class_or_enum
& 2)
24688 cp_parser_check_for_definition_in_return_type
24689 (declarator
, decl_specifiers
.type
,
24690 decl_specifiers
.locations
[ds_type_spec
]);
24692 /* Look for an asm-specification. */
24693 asm_specification
= cp_parser_asm_specification_opt (parser
);
24694 /* Look for attributes that apply to the declaration. */
24695 attributes
= cp_parser_attributes_opt (parser
);
24696 /* Remember which attributes are prefix attributes and
24698 first_attribute
= attributes
;
24699 /* Combine the attributes. */
24700 attributes
= attr_chainon (prefix_attributes
, attributes
);
24702 /* If it's an `=', then we have a constant-initializer or a
24703 pure-specifier. It is not correct to parse the
24704 initializer before registering the member declaration
24705 since the member declaration should be in scope while
24706 its initializer is processed. However, the rest of the
24707 front end does not yet provide an interface that allows
24708 us to handle this correctly. */
24709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24713 A pure-specifier shall be used only in the declaration of
24714 a virtual function.
24716 A member-declarator can contain a constant-initializer
24717 only if it declares a static member of integral or
24720 Therefore, if the DECLARATOR is for a function, we look
24721 for a pure-specifier; otherwise, we look for a
24722 constant-initializer. When we call `grokfield', it will
24723 perform more stringent semantics checks. */
24724 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
24725 if (function_declarator_p (declarator
)
24726 || (decl_specifiers
.type
24727 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
24728 && declarator
->kind
== cdk_id
24729 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
24730 == FUNCTION_TYPE
)))
24731 initializer
= cp_parser_pure_specifier (parser
);
24732 else if (decl_specifiers
.storage_class
!= sc_static
)
24733 initializer
= cp_parser_save_nsdmi (parser
);
24734 else if (cxx_dialect
>= cxx11
)
24737 /* Don't require a constant rvalue in C++11, since we
24738 might want a reference constant. We'll enforce
24739 constancy later. */
24740 cp_lexer_consume_token (parser
->lexer
);
24741 /* Parse the initializer. */
24742 initializer
= cp_parser_initializer_clause (parser
,
24746 /* Parse the initializer. */
24747 initializer
= cp_parser_constant_initializer (parser
);
24749 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
24750 && !function_declarator_p (declarator
))
24753 if (decl_specifiers
.storage_class
!= sc_static
)
24754 initializer
= cp_parser_save_nsdmi (parser
);
24756 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24758 /* Otherwise, there is no initializer. */
24760 initializer
= NULL_TREE
;
24762 /* See if we are probably looking at a function
24763 definition. We are certainly not looking at a
24764 member-declarator. Calling `grokfield' has
24765 side-effects, so we must not do it unless we are sure
24766 that we are looking at a member-declarator. */
24767 if (cp_parser_token_starts_function_definition_p
24768 (cp_lexer_peek_token (parser
->lexer
)))
24770 /* The grammar does not allow a pure-specifier to be
24771 used when a member function is defined. (It is
24772 possible that this fact is an oversight in the
24773 standard, since a pure function may be defined
24774 outside of the class-specifier. */
24775 if (initializer
&& initializer_token_start
)
24776 error_at (initializer_token_start
->location
,
24777 "pure-specifier on function-definition");
24778 decl
= cp_parser_save_member_function_body (parser
,
24782 if (parser
->fully_implicit_function_template_p
)
24783 decl
= finish_fully_implicit_template (parser
, decl
);
24784 /* If the member was not a friend, declare it here. */
24786 finish_member_declaration (decl
);
24787 /* Peek at the next token. */
24788 token
= cp_lexer_peek_token (parser
->lexer
);
24789 /* If the next token is a semicolon, consume it. */
24790 if (token
->type
== CPP_SEMICOLON
)
24792 location_t semicolon_loc
24793 = cp_lexer_consume_token (parser
->lexer
)->location
;
24794 gcc_rich_location
richloc (semicolon_loc
);
24795 richloc
.add_fixit_remove ();
24796 warning_at (&richloc
, OPT_Wextra_semi
,
24797 "extra %<;%> after in-class "
24798 "function definition");
24803 if (declarator
->kind
== cdk_function
)
24804 declarator
->id_loc
= token
->location
;
24805 /* Create the declaration. */
24806 decl
= grokfield (declarator
, &decl_specifiers
,
24807 initializer
, /*init_const_expr_p=*/true,
24808 asm_specification
, attributes
);
24809 if (parser
->fully_implicit_function_template_p
)
24812 finish_fully_implicit_template (parser
, 0);
24814 decl
= finish_fully_implicit_template (parser
, decl
);
24818 cp_finalize_omp_declare_simd (parser
, decl
);
24819 cp_finalize_oacc_routine (parser
, decl
, false);
24821 /* Reset PREFIX_ATTRIBUTES. */
24822 if (attributes
!= error_mark_node
)
24824 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24825 attributes
= TREE_CHAIN (attributes
);
24827 TREE_CHAIN (attributes
) = NULL_TREE
;
24830 /* If there is any qualification still in effect, clear it
24831 now; we will be starting fresh with the next declarator. */
24832 parser
->scope
= NULL_TREE
;
24833 parser
->qualifying_scope
= NULL_TREE
;
24834 parser
->object_scope
= NULL_TREE
;
24835 /* If it's a `,', then there are more declarators. */
24836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24838 cp_lexer_consume_token (parser
->lexer
);
24839 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24841 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24842 gcc_rich_location
richloc (token
->location
);
24843 richloc
.add_fixit_remove ();
24844 error_at (&richloc
, "stray %<,%> at end of "
24845 "member declaration");
24848 /* If the next token isn't a `;', then we have a parse error. */
24849 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24852 /* The next token might be a ways away from where the
24853 actual semicolon is missing. Find the previous token
24854 and use that for our error position. */
24855 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24856 gcc_rich_location
richloc (token
->location
);
24857 richloc
.add_fixit_insert_after (";");
24858 error_at (&richloc
, "expected %<;%> at end of "
24859 "member declaration");
24861 /* Assume that the user meant to provide a semicolon. If
24862 we were to cp_parser_skip_to_end_of_statement, we might
24863 skip to a semicolon inside a member function definition
24864 and issue nonsensical error messages. */
24865 assume_semicolon
= true;
24870 /* Add DECL to the list of members. */
24872 /* Explicitly include, eg, NSDMIs, for better error
24873 recovery (c++/58650). */
24874 || !DECL_DECLARES_FUNCTION_P (decl
))
24875 finish_member_declaration (decl
);
24877 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24878 cp_parser_save_default_args (parser
, decl
);
24879 else if (TREE_CODE (decl
) == FIELD_DECL
24880 && DECL_INITIAL (decl
))
24881 /* Add DECL to the queue of NSDMI to be parsed later. */
24882 vec_safe_push (unparsed_nsdmis
, decl
);
24885 if (assume_semicolon
)
24890 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24892 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24895 /* Parse a pure-specifier.
24900 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24901 Otherwise, ERROR_MARK_NODE is returned. */
24904 cp_parser_pure_specifier (cp_parser
* parser
)
24908 /* Look for the `=' token. */
24909 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24910 return error_mark_node
;
24911 /* Look for the `0' token. */
24912 token
= cp_lexer_peek_token (parser
->lexer
);
24914 if (token
->type
== CPP_EOF
24915 || token
->type
== CPP_PRAGMA_EOL
)
24916 return error_mark_node
;
24918 cp_lexer_consume_token (parser
->lexer
);
24920 /* Accept = default or = delete in c++0x mode. */
24921 if (token
->keyword
== RID_DEFAULT
24922 || token
->keyword
== RID_DELETE
)
24924 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24925 return token
->u
.value
;
24928 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24929 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24931 cp_parser_error (parser
,
24932 "invalid pure specifier (only %<= 0%> is allowed)");
24933 cp_parser_skip_to_end_of_statement (parser
);
24934 return error_mark_node
;
24936 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24938 error_at (token
->location
, "templates may not be %<virtual%>");
24939 return error_mark_node
;
24942 return integer_zero_node
;
24945 /* Parse a constant-initializer.
24947 constant-initializer:
24948 = constant-expression
24950 Returns a representation of the constant-expression. */
24953 cp_parser_constant_initializer (cp_parser
* parser
)
24955 /* Look for the `=' token. */
24956 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24957 return error_mark_node
;
24959 /* It is invalid to write:
24961 struct S { static const int i = { 7 }; };
24964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24966 cp_parser_error (parser
,
24967 "a brace-enclosed initializer is not allowed here");
24968 /* Consume the opening brace. */
24969 matching_braces braces
;
24970 braces
.consume_open (parser
);
24971 /* Skip the initializer. */
24972 cp_parser_skip_to_closing_brace (parser
);
24973 /* Look for the trailing `}'. */
24974 braces
.require_close (parser
);
24976 return error_mark_node
;
24979 return cp_parser_constant_expression (parser
);
24982 /* Derived classes [gram.class.derived] */
24984 /* Parse a base-clause.
24987 : base-specifier-list
24989 base-specifier-list:
24990 base-specifier ... [opt]
24991 base-specifier-list , base-specifier ... [opt]
24993 Returns a TREE_LIST representing the base-classes, in the order in
24994 which they were declared. The representation of each node is as
24995 described by cp_parser_base_specifier.
24997 In the case that no bases are specified, this function will return
24998 NULL_TREE, not ERROR_MARK_NODE. */
25001 cp_parser_base_clause (cp_parser
* parser
)
25003 tree bases
= NULL_TREE
;
25005 /* Look for the `:' that begins the list. */
25006 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25008 /* Scan the base-specifier-list. */
25013 bool pack_expansion_p
= false;
25015 /* Look for the base-specifier. */
25016 base
= cp_parser_base_specifier (parser
);
25017 /* Look for the (optional) ellipsis. */
25018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25020 /* Consume the `...'. */
25021 cp_lexer_consume_token (parser
->lexer
);
25023 pack_expansion_p
= true;
25026 /* Add BASE to the front of the list. */
25027 if (base
&& base
!= error_mark_node
)
25029 if (pack_expansion_p
)
25030 /* Make this a pack expansion type. */
25031 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
25033 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
25035 TREE_CHAIN (base
) = bases
;
25039 /* Peek at the next token. */
25040 token
= cp_lexer_peek_token (parser
->lexer
);
25041 /* If it's not a comma, then the list is complete. */
25042 if (token
->type
!= CPP_COMMA
)
25044 /* Consume the `,'. */
25045 cp_lexer_consume_token (parser
->lexer
);
25048 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25049 base class had a qualified name. However, the next name that
25050 appears is certainly not qualified. */
25051 parser
->scope
= NULL_TREE
;
25052 parser
->qualifying_scope
= NULL_TREE
;
25053 parser
->object_scope
= NULL_TREE
;
25055 return nreverse (bases
);
25058 /* Parse a base-specifier.
25061 :: [opt] nested-name-specifier [opt] class-name
25062 virtual access-specifier [opt] :: [opt] nested-name-specifier
25064 access-specifier virtual [opt] :: [opt] nested-name-specifier
25067 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25068 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25069 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25070 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25073 cp_parser_base_specifier (cp_parser
* parser
)
25077 bool virtual_p
= false;
25078 bool duplicate_virtual_error_issued_p
= false;
25079 bool duplicate_access_error_issued_p
= false;
25080 bool class_scope_p
, template_p
;
25081 tree access
= access_default_node
;
25084 /* Process the optional `virtual' and `access-specifier'. */
25087 /* Peek at the next token. */
25088 token
= cp_lexer_peek_token (parser
->lexer
);
25089 /* Process `virtual'. */
25090 switch (token
->keyword
)
25093 /* If `virtual' appears more than once, issue an error. */
25094 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
25096 cp_parser_error (parser
,
25097 "%<virtual%> specified more than once in base-specifier");
25098 duplicate_virtual_error_issued_p
= true;
25103 /* Consume the `virtual' token. */
25104 cp_lexer_consume_token (parser
->lexer
);
25109 case RID_PROTECTED
:
25111 /* If more than one access specifier appears, issue an
25113 if (access
!= access_default_node
25114 && !duplicate_access_error_issued_p
)
25116 cp_parser_error (parser
,
25117 "more than one access specifier in base-specifier");
25118 duplicate_access_error_issued_p
= true;
25121 access
= ridpointers
[(int) token
->keyword
];
25123 /* Consume the access-specifier. */
25124 cp_lexer_consume_token (parser
->lexer
);
25133 /* It is not uncommon to see programs mechanically, erroneously, use
25134 the 'typename' keyword to denote (dependent) qualified types
25135 as base classes. */
25136 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25138 token
= cp_lexer_peek_token (parser
->lexer
);
25139 if (!processing_template_decl
)
25140 error_at (token
->location
,
25141 "keyword %<typename%> not allowed outside of templates");
25143 error_at (token
->location
,
25144 "keyword %<typename%> not allowed in this context "
25145 "(the base class is implicitly a type)");
25146 cp_lexer_consume_token (parser
->lexer
);
25149 /* Look for the optional `::' operator. */
25150 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
25151 /* Look for the nested-name-specifier. The simplest way to
25156 The keyword `typename' is not permitted in a base-specifier or
25157 mem-initializer; in these contexts a qualified name that
25158 depends on a template-parameter is implicitly assumed to be a
25161 is to pretend that we have seen the `typename' keyword at this
25163 cp_parser_nested_name_specifier_opt (parser
,
25164 /*typename_keyword_p=*/true,
25165 /*check_dependency_p=*/true,
25167 /*is_declaration=*/true);
25168 /* If the base class is given by a qualified name, assume that names
25169 we see are type names or templates, as appropriate. */
25170 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
25171 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
25174 && cp_lexer_next_token_is_decltype (parser
->lexer
))
25175 /* DR 950 allows decltype as a base-specifier. */
25176 type
= cp_parser_decltype (parser
);
25179 /* Otherwise, look for the class-name. */
25180 type
= cp_parser_class_name (parser
,
25184 /*check_dependency_p=*/true,
25185 /*class_head_p=*/false,
25186 /*is_declaration=*/true);
25187 type
= TREE_TYPE (type
);
25190 if (type
== error_mark_node
)
25191 return error_mark_node
;
25193 return finish_base_specifier (type
, access
, virtual_p
);
25196 /* Exception handling [gram.exception] */
25198 /* Parse an (optional) noexcept-specification.
25200 noexcept-specification:
25201 noexcept ( constant-expression ) [opt]
25203 If no noexcept-specification is present, returns NULL_TREE.
25204 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25205 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25206 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25207 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25208 in which case a boolean condition is returned instead. */
25211 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
25212 bool require_constexpr
,
25213 bool* consumed_expr
,
25217 const char *saved_message
;
25219 /* Peek at the next token. */
25220 token
= cp_lexer_peek_token (parser
->lexer
);
25222 /* Is it a noexcept-specification? */
25223 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
25226 cp_lexer_consume_token (parser
->lexer
);
25228 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
25230 matching_parens parens
;
25231 parens
.consume_open (parser
);
25233 tree save_ccp
= current_class_ptr
;
25234 tree save_ccr
= current_class_ref
;
25236 if (current_class_type
)
25237 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
25239 if (require_constexpr
)
25241 /* Types may not be defined in an exception-specification. */
25242 saved_message
= parser
->type_definition_forbidden_message
;
25243 parser
->type_definition_forbidden_message
25244 = G_("types may not be defined in an exception-specification");
25246 bool non_constant_p
;
25248 = cp_parser_constant_expression (parser
,
25249 /*allow_non_constant=*/true,
25252 && !require_potential_rvalue_constant_expression (expr
))
25255 return_cond
= true;
25258 /* Restore the saved message. */
25259 parser
->type_definition_forbidden_message
= saved_message
;
25263 expr
= cp_parser_expression (parser
);
25264 *consumed_expr
= true;
25267 parens
.require_close (parser
);
25269 current_class_ptr
= save_ccp
;
25270 current_class_ref
= save_ccr
;
25274 expr
= boolean_true_node
;
25275 if (!require_constexpr
)
25276 *consumed_expr
= false;
25279 /* We cannot build a noexcept-spec right away because this will check
25280 that expr is a constexpr. */
25282 return build_noexcept_spec (expr
, tf_warning_or_error
);
25290 /* Parse an (optional) exception-specification.
25292 exception-specification:
25293 throw ( type-id-list [opt] )
25295 Returns a TREE_LIST representing the exception-specification. The
25296 TREE_VALUE of each node is a type. */
25299 cp_parser_exception_specification_opt (cp_parser
* parser
)
25303 const char *saved_message
;
25305 /* Peek at the next token. */
25306 token
= cp_lexer_peek_token (parser
->lexer
);
25308 /* Is it a noexcept-specification? */
25309 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
25311 if (type_id_list
!= NULL_TREE
)
25312 return type_id_list
;
25314 /* If it's not `throw', then there's no exception-specification. */
25315 if (!cp_parser_is_keyword (token
, RID_THROW
))
25318 location_t loc
= token
->location
;
25320 /* Consume the `throw'. */
25321 cp_lexer_consume_token (parser
->lexer
);
25323 /* Look for the `('. */
25324 matching_parens parens
;
25325 parens
.require_open (parser
);
25327 /* Peek at the next token. */
25328 token
= cp_lexer_peek_token (parser
->lexer
);
25329 /* If it's not a `)', then there is a type-id-list. */
25330 if (token
->type
!= CPP_CLOSE_PAREN
)
25332 /* Types may not be defined in an exception-specification. */
25333 saved_message
= parser
->type_definition_forbidden_message
;
25334 parser
->type_definition_forbidden_message
25335 = G_("types may not be defined in an exception-specification");
25336 /* Parse the type-id-list. */
25337 type_id_list
= cp_parser_type_id_list (parser
);
25338 /* Restore the saved message. */
25339 parser
->type_definition_forbidden_message
= saved_message
;
25341 if (cxx_dialect
>= cxx17
)
25343 error_at (loc
, "ISO C++17 does not allow dynamic exception "
25345 type_id_list
= NULL_TREE
;
25347 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
25348 warning_at (loc
, OPT_Wdeprecated
,
25349 "dynamic exception specifications are deprecated in "
25352 /* In C++17, throw() is equivalent to noexcept (true). throw()
25353 is deprecated in C++11 and above as well, but is still widely used,
25354 so don't warn about it yet. */
25355 else if (cxx_dialect
>= cxx17
)
25356 type_id_list
= noexcept_true_spec
;
25358 type_id_list
= empty_except_spec
;
25360 /* Look for the `)'. */
25361 parens
.require_close (parser
);
25363 return type_id_list
;
25366 /* Parse an (optional) type-id-list.
25370 type-id-list , type-id ... [opt]
25372 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25373 in the order that the types were presented. */
25376 cp_parser_type_id_list (cp_parser
* parser
)
25378 tree types
= NULL_TREE
;
25385 token
= cp_lexer_peek_token (parser
->lexer
);
25387 /* Get the next type-id. */
25388 type
= cp_parser_type_id (parser
);
25389 /* Check for invalid 'auto'. */
25390 if (flag_concepts
&& type_uses_auto (type
))
25392 error_at (token
->location
,
25393 "invalid use of %<auto%> in exception-specification");
25394 type
= error_mark_node
;
25396 /* Parse the optional ellipsis. */
25397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25399 /* Consume the `...'. */
25400 cp_lexer_consume_token (parser
->lexer
);
25402 /* Turn the type into a pack expansion expression. */
25403 type
= make_pack_expansion (type
);
25405 /* Add it to the list. */
25406 types
= add_exception_specifier (types
, type
, /*complain=*/1);
25407 /* Peek at the next token. */
25408 token
= cp_lexer_peek_token (parser
->lexer
);
25409 /* If it is not a `,', we are done. */
25410 if (token
->type
!= CPP_COMMA
)
25412 /* Consume the `,'. */
25413 cp_lexer_consume_token (parser
->lexer
);
25416 return nreverse (types
);
25419 /* Parse a try-block.
25422 try compound-statement handler-seq */
25425 cp_parser_try_block (cp_parser
* parser
)
25429 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25430 if (parser
->in_function_body
25431 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
25432 && cxx_dialect
< cxx2a
)
25433 pedwarn (input_location
, 0,
25434 "%<try%> in %<constexpr%> function only "
25435 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25437 try_block
= begin_try_block ();
25438 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25439 finish_try_block (try_block
);
25440 cp_parser_handler_seq (parser
);
25441 finish_handler_sequence (try_block
);
25446 /* Parse a function-try-block.
25448 function-try-block:
25449 try ctor-initializer [opt] function-body handler-seq */
25452 cp_parser_function_try_block (cp_parser
* parser
)
25454 tree compound_stmt
;
25457 /* Look for the `try' keyword. */
25458 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25460 /* Let the rest of the front end know where we are. */
25461 try_block
= begin_function_try_block (&compound_stmt
);
25462 /* Parse the function-body. */
25463 cp_parser_ctor_initializer_opt_and_function_body
25464 (parser
, /*in_function_try_block=*/true);
25465 /* We're done with the `try' part. */
25466 finish_function_try_block (try_block
);
25467 /* Parse the handlers. */
25468 cp_parser_handler_seq (parser
);
25469 /* We're done with the handlers. */
25470 finish_function_handler_sequence (try_block
, compound_stmt
);
25473 /* Parse a handler-seq.
25476 handler handler-seq [opt] */
25479 cp_parser_handler_seq (cp_parser
* parser
)
25485 /* Parse the handler. */
25486 cp_parser_handler (parser
);
25487 /* Peek at the next token. */
25488 token
= cp_lexer_peek_token (parser
->lexer
);
25489 /* If it's not `catch' then there are no more handlers. */
25490 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25495 /* Parse a handler.
25498 catch ( exception-declaration ) compound-statement */
25501 cp_parser_handler (cp_parser
* parser
)
25506 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25507 handler
= begin_handler ();
25508 matching_parens parens
;
25509 parens
.require_open (parser
);
25510 declaration
= cp_parser_exception_declaration (parser
);
25511 finish_handler_parms (declaration
, handler
);
25512 parens
.require_close (parser
);
25513 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25514 finish_handler (handler
);
25517 /* Parse an exception-declaration.
25519 exception-declaration:
25520 type-specifier-seq declarator
25521 type-specifier-seq abstract-declarator
25525 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25526 ellipsis variant is used. */
25529 cp_parser_exception_declaration (cp_parser
* parser
)
25531 cp_decl_specifier_seq type_specifiers
;
25532 cp_declarator
*declarator
;
25533 const char *saved_message
;
25535 /* If it's an ellipsis, it's easy to handle. */
25536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25538 /* Consume the `...' token. */
25539 cp_lexer_consume_token (parser
->lexer
);
25543 /* Types may not be defined in exception-declarations. */
25544 saved_message
= parser
->type_definition_forbidden_message
;
25545 parser
->type_definition_forbidden_message
25546 = G_("types may not be defined in exception-declarations");
25548 /* Parse the type-specifier-seq. */
25549 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
25550 /*is_declaration=*/true,
25551 /*is_trailing_return=*/false,
25553 /* If it's a `)', then there is no declarator. */
25554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25557 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25558 CP_PARSER_FLAGS_NONE
,
25559 /*ctor_dtor_or_conv_p=*/NULL
,
25560 /*parenthesized_p=*/NULL
,
25561 /*member_p=*/false,
25562 /*friend_p=*/false,
25563 /*static_p=*/false);
25565 /* Restore the saved message. */
25566 parser
->type_definition_forbidden_message
= saved_message
;
25568 if (!type_specifiers
.any_specifiers_p
)
25569 return error_mark_node
;
25571 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25574 /* Parse a throw-expression.
25577 throw assignment-expression [opt]
25579 Returns a THROW_EXPR representing the throw-expression. */
25582 cp_parser_throw_expression (cp_parser
* parser
)
25587 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
25588 token
= cp_lexer_peek_token (parser
->lexer
);
25589 /* Figure out whether or not there is an assignment-expression
25590 following the "throw" keyword. */
25591 if (token
->type
== CPP_COMMA
25592 || token
->type
== CPP_SEMICOLON
25593 || token
->type
== CPP_CLOSE_PAREN
25594 || token
->type
== CPP_CLOSE_SQUARE
25595 || token
->type
== CPP_CLOSE_BRACE
25596 || token
->type
== CPP_COLON
)
25597 expression
= NULL_TREE
;
25599 expression
= cp_parser_assignment_expression (parser
);
25601 return build_throw (expression
);
25604 /* GNU Extensions */
25606 /* Parse an (optional) asm-specification.
25609 asm ( string-literal )
25611 If the asm-specification is present, returns a STRING_CST
25612 corresponding to the string-literal. Otherwise, returns
25616 cp_parser_asm_specification_opt (cp_parser
* parser
)
25619 tree asm_specification
;
25621 /* Peek at the next token. */
25622 token
= cp_lexer_peek_token (parser
->lexer
);
25623 /* If the next token isn't the `asm' keyword, then there's no
25624 asm-specification. */
25625 if (!cp_parser_is_keyword (token
, RID_ASM
))
25628 /* Consume the `asm' token. */
25629 cp_lexer_consume_token (parser
->lexer
);
25630 /* Look for the `('. */
25631 matching_parens parens
;
25632 parens
.require_open (parser
);
25634 /* Look for the string-literal. */
25635 asm_specification
= cp_parser_string_literal (parser
, false, false);
25637 /* Look for the `)'. */
25638 parens
.require_close (parser
);
25640 return asm_specification
;
25643 /* Parse an asm-operand-list.
25647 asm-operand-list , asm-operand
25650 string-literal ( expression )
25651 [ string-literal ] string-literal ( expression )
25653 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25654 each node is the expression. The TREE_PURPOSE is itself a
25655 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25656 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25657 is a STRING_CST for the string literal before the parenthesis. Returns
25658 ERROR_MARK_NODE if any of the operands are invalid. */
25661 cp_parser_asm_operand_list (cp_parser
* parser
)
25663 tree asm_operands
= NULL_TREE
;
25664 bool invalid_operands
= false;
25668 tree string_literal
;
25672 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
25674 /* Consume the `[' token. */
25675 cp_lexer_consume_token (parser
->lexer
);
25676 /* Read the operand name. */
25677 name
= cp_parser_identifier (parser
);
25678 if (name
!= error_mark_node
)
25679 name
= build_string (IDENTIFIER_LENGTH (name
),
25680 IDENTIFIER_POINTER (name
));
25681 /* Look for the closing `]'. */
25682 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25686 /* Look for the string-literal. */
25687 string_literal
= cp_parser_string_literal (parser
, false, false);
25689 /* Look for the `('. */
25690 matching_parens parens
;
25691 parens
.require_open (parser
);
25692 /* Parse the expression. */
25693 expression
= cp_parser_expression (parser
);
25694 /* Look for the `)'. */
25695 parens
.require_close (parser
);
25697 if (name
== error_mark_node
25698 || string_literal
== error_mark_node
25699 || expression
== error_mark_node
)
25700 invalid_operands
= true;
25702 /* Add this operand to the list. */
25703 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
25706 /* If the next token is not a `,', there are no more
25708 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25710 /* Consume the `,'. */
25711 cp_lexer_consume_token (parser
->lexer
);
25714 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
25717 /* Parse an asm-clobber-list.
25721 asm-clobber-list , string-literal
25723 Returns a TREE_LIST, indicating the clobbers in the order that they
25724 appeared. The TREE_VALUE of each node is a STRING_CST. */
25727 cp_parser_asm_clobber_list (cp_parser
* parser
)
25729 tree clobbers
= NULL_TREE
;
25733 tree string_literal
;
25735 /* Look for the string literal. */
25736 string_literal
= cp_parser_string_literal (parser
, false, false);
25737 /* Add it to the list. */
25738 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
25739 /* If the next token is not a `,', then the list is
25741 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25743 /* Consume the `,' token. */
25744 cp_lexer_consume_token (parser
->lexer
);
25750 /* Parse an asm-label-list.
25754 asm-label-list , identifier
25756 Returns a TREE_LIST, indicating the labels in the order that they
25757 appeared. The TREE_VALUE of each node is a label. */
25760 cp_parser_asm_label_list (cp_parser
* parser
)
25762 tree labels
= NULL_TREE
;
25766 tree identifier
, label
, name
;
25768 /* Look for the identifier. */
25769 identifier
= cp_parser_identifier (parser
);
25770 if (!error_operand_p (identifier
))
25772 label
= lookup_label (identifier
);
25773 if (TREE_CODE (label
) == LABEL_DECL
)
25775 TREE_USED (label
) = 1;
25776 check_goto (label
);
25777 name
= build_string (IDENTIFIER_LENGTH (identifier
),
25778 IDENTIFIER_POINTER (identifier
));
25779 labels
= tree_cons (name
, label
, labels
);
25782 /* If the next token is not a `,', then the list is
25784 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25786 /* Consume the `,' token. */
25787 cp_lexer_consume_token (parser
->lexer
);
25790 return nreverse (labels
);
25793 /* Return TRUE iff the next tokens in the stream are possibly the
25794 beginning of a GNU extension attribute. */
25797 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25799 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25802 /* Return TRUE iff the next tokens in the stream are possibly the
25803 beginning of a standard C++-11 attribute specifier. */
25806 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25808 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25811 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25812 beginning of a standard C++-11 attribute specifier. */
25815 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25817 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25819 return (cxx_dialect
>= cxx11
25820 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25821 || (token
->type
== CPP_OPEN_SQUARE
25822 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25823 && token
->type
== CPP_OPEN_SQUARE
)));
25826 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25827 beginning of a GNU extension attribute. */
25830 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25832 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25834 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25837 /* Return true iff the next tokens can be the beginning of either a
25838 GNU attribute list, or a standard C++11 attribute sequence. */
25841 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25843 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25844 || cp_next_tokens_can_be_std_attribute_p (parser
));
25847 /* Return true iff the next Nth tokens can be the beginning of either
25848 a GNU attribute list, or a standard C++11 attribute sequence. */
25851 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25853 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25854 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25857 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25858 of GNU attributes, or return NULL. */
25861 cp_parser_attributes_opt (cp_parser
*parser
)
25863 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25864 return cp_parser_gnu_attributes_opt (parser
);
25865 return cp_parser_std_attribute_spec_seq (parser
);
25868 /* Parse an (optional) series of attributes.
25871 attributes attribute
25874 __attribute__ (( attribute-list [opt] ))
25876 The return value is as for cp_parser_gnu_attribute_list. */
25879 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25881 tree attributes
= NULL_TREE
;
25883 temp_override
<bool> cleanup
25884 (parser
->auto_is_implicit_function_template_parm_p
, false);
25889 tree attribute_list
;
25892 /* Peek at the next token. */
25893 token
= cp_lexer_peek_token (parser
->lexer
);
25894 /* If it's not `__attribute__', then we're done. */
25895 if (token
->keyword
!= RID_ATTRIBUTE
)
25898 /* Consume the `__attribute__' keyword. */
25899 cp_lexer_consume_token (parser
->lexer
);
25900 /* Look for the two `(' tokens. */
25901 matching_parens outer_parens
;
25902 if (!outer_parens
.require_open (parser
))
25904 matching_parens inner_parens
;
25905 if (!inner_parens
.require_open (parser
))
25908 /* Peek at the next token. */
25909 token
= cp_lexer_peek_token (parser
->lexer
);
25910 if (token
->type
!= CPP_CLOSE_PAREN
)
25911 /* Parse the attribute-list. */
25912 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25914 /* If the next token is a `)', then there is no attribute
25916 attribute_list
= NULL
;
25918 /* Look for the two `)' tokens. */
25919 if (!inner_parens
.require_close (parser
))
25921 if (!outer_parens
.require_close (parser
))
25924 cp_parser_skip_to_end_of_statement (parser
);
25926 /* Add these new attributes to the list. */
25927 attributes
= attr_chainon (attributes
, attribute_list
);
25933 /* Parse a GNU attribute-list.
25937 attribute-list , attribute
25941 identifier ( identifier )
25942 identifier ( identifier , expression-list )
25943 identifier ( expression-list )
25945 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25946 to an attribute. The TREE_PURPOSE of each node is the identifier
25947 indicating which attribute is in use. The TREE_VALUE represents
25948 the arguments, if any. */
25951 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
25953 tree attribute_list
= NULL_TREE
;
25954 bool save_translate_strings_p
= parser
->translate_strings_p
;
25956 /* Don't create wrapper nodes within attributes: the
25957 handlers don't know how to handle them. */
25958 auto_suppress_location_wrappers sentinel
;
25960 parser
->translate_strings_p
= false;
25967 /* Look for the identifier. We also allow keywords here; for
25968 example `__attribute__ ((const))' is legal. */
25969 token
= cp_lexer_peek_token (parser
->lexer
);
25970 if (token
->type
== CPP_NAME
25971 || token
->type
== CPP_KEYWORD
)
25973 tree arguments
= NULL_TREE
;
25975 /* Consume the token, but save it since we need it for the
25976 SIMD enabled function parsing. */
25977 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25979 /* Save away the identifier that indicates which attribute
25981 identifier
= (token
->type
== CPP_KEYWORD
)
25982 /* For keywords, use the canonical spelling, not the
25983 parsed identifier. */
25984 ? ridpointers
[(int) token
->keyword
]
25985 : id_token
->u
.value
;
25987 identifier
= canonicalize_attr_name (identifier
);
25988 attribute
= build_tree_list (identifier
, NULL_TREE
);
25990 /* Peek at the next token. */
25991 token
= cp_lexer_peek_token (parser
->lexer
);
25992 /* If it's an `(', then parse the attribute arguments. */
25993 if (token
->type
== CPP_OPEN_PAREN
)
25995 vec
<tree
, va_gc
> *vec
;
25996 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25997 ? id_attr
: normal_attr
);
25998 vec
= cp_parser_parenthesized_expression_list
25999 (parser
, attr_flag
, /*cast_p=*/false,
26000 /*allow_expansion_p=*/false,
26001 /*non_constant_p=*/NULL
);
26003 arguments
= error_mark_node
;
26006 arguments
= build_tree_list_vec (vec
);
26007 release_tree_vector (vec
);
26009 /* Save the arguments away. */
26010 TREE_VALUE (attribute
) = arguments
;
26013 if (arguments
!= error_mark_node
)
26015 /* Add this attribute to the list. */
26016 TREE_CHAIN (attribute
) = attribute_list
;
26017 attribute_list
= attribute
;
26020 token
= cp_lexer_peek_token (parser
->lexer
);
26022 /* Unless EXACTLY_ONE is set look for more attributes.
26023 If the next token isn't a `,', we're done. */
26024 if (exactly_one
|| token
->type
!= CPP_COMMA
)
26027 /* Consume the comma and keep going. */
26028 cp_lexer_consume_token (parser
->lexer
);
26030 parser
->translate_strings_p
= save_translate_strings_p
;
26032 /* We built up the list in reverse order. */
26033 return nreverse (attribute_list
);
26036 /* Parse a standard C++11 attribute.
26038 The returned representation is a TREE_LIST which TREE_PURPOSE is
26039 the scoped name of the attribute, and the TREE_VALUE is its
26042 Note that the scoped name of the attribute is itself a TREE_LIST
26043 which TREE_PURPOSE is the namespace of the attribute, and
26044 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26045 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26046 and which TREE_PURPOSE is directly the attribute name.
26048 Clients of the attribute code should use get_attribute_namespace
26049 and get_attribute_name to get the actual namespace and name of
26050 attributes, regardless of their being GNU or C++11 attributes.
26053 attribute-token attribute-argument-clause [opt]
26057 attribute-scoped-token
26059 attribute-scoped-token:
26060 attribute-namespace :: identifier
26062 attribute-namespace:
26065 attribute-argument-clause:
26066 ( balanced-token-seq )
26068 balanced-token-seq:
26069 balanced-token [opt]
26070 balanced-token-seq balanced-token
26073 ( balanced-token-seq )
26074 [ balanced-token-seq ]
26075 { balanced-token-seq }. */
26078 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
26080 tree attribute
, attr_id
= NULL_TREE
, arguments
;
26083 temp_override
<bool> cleanup
26084 (parser
->auto_is_implicit_function_template_parm_p
, false);
26086 /* First, parse name of the attribute, a.k.a attribute-token. */
26088 token
= cp_lexer_peek_token (parser
->lexer
);
26089 if (token
->type
== CPP_NAME
)
26090 attr_id
= token
->u
.value
;
26091 else if (token
->type
== CPP_KEYWORD
)
26092 attr_id
= ridpointers
[(int) token
->keyword
];
26093 else if (token
->flags
& NAMED_OP
)
26094 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26096 if (attr_id
== NULL_TREE
)
26099 cp_lexer_consume_token (parser
->lexer
);
26101 token
= cp_lexer_peek_token (parser
->lexer
);
26102 if (token
->type
== CPP_SCOPE
)
26104 /* We are seeing a scoped attribute token. */
26106 cp_lexer_consume_token (parser
->lexer
);
26108 error_at (token
->location
, "attribute using prefix used together "
26109 "with scoped attribute token");
26112 token
= cp_lexer_consume_token (parser
->lexer
);
26113 if (token
->type
== CPP_NAME
)
26114 attr_id
= token
->u
.value
;
26115 else if (token
->type
== CPP_KEYWORD
)
26116 attr_id
= ridpointers
[(int) token
->keyword
];
26117 else if (token
->flags
& NAMED_OP
)
26118 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26121 error_at (token
->location
,
26122 "expected an identifier for the attribute name");
26123 return error_mark_node
;
26126 attr_ns
= canonicalize_attr_name (attr_ns
);
26127 attr_id
= canonicalize_attr_name (attr_id
);
26128 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26130 token
= cp_lexer_peek_token (parser
->lexer
);
26134 attr_ns
= canonicalize_attr_name (attr_ns
);
26135 attr_id
= canonicalize_attr_name (attr_id
);
26136 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26141 attr_id
= canonicalize_attr_name (attr_id
);
26142 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
26144 /* C++11 noreturn attribute is equivalent to GNU's. */
26145 if (is_attribute_p ("noreturn", attr_id
))
26146 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26147 /* C++14 deprecated attribute is equivalent to GNU's. */
26148 else if (is_attribute_p ("deprecated", attr_id
))
26149 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26150 /* C++17 fallthrough attribute is equivalent to GNU's. */
26151 else if (is_attribute_p ("fallthrough", attr_id
))
26152 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26153 /* Transactional Memory TS optimize_for_synchronized attribute is
26154 equivalent to GNU transaction_callable. */
26155 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
26156 TREE_PURPOSE (attribute
)
26157 = get_identifier ("transaction_callable");
26158 /* Transactional Memory attributes are GNU attributes. */
26159 else if (tm_attr_to_mask (attr_id
))
26160 TREE_PURPOSE (attribute
) = attr_id
;
26163 /* Now parse the optional argument clause of the attribute. */
26165 if (token
->type
!= CPP_OPEN_PAREN
)
26169 vec
<tree
, va_gc
> *vec
;
26170 int attr_flag
= normal_attr
;
26172 if (attr_ns
== gnu_identifier
26173 && attribute_takes_identifier_p (attr_id
))
26174 /* A GNU attribute that takes an identifier in parameter. */
26175 attr_flag
= id_attr
;
26177 vec
= cp_parser_parenthesized_expression_list
26178 (parser
, attr_flag
, /*cast_p=*/false,
26179 /*allow_expansion_p=*/true,
26180 /*non_constant_p=*/NULL
);
26182 arguments
= error_mark_node
;
26185 arguments
= build_tree_list_vec (vec
);
26186 release_tree_vector (vec
);
26189 if (arguments
== error_mark_node
)
26190 attribute
= error_mark_node
;
26192 TREE_VALUE (attribute
) = arguments
;
26198 /* Check that the attribute ATTRIBUTE appears at most once in the
26199 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26200 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26201 isn't implemented yet in GCC. */
26204 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
26208 tree name
= get_attribute_name (attribute
);
26209 if (is_attribute_p ("noreturn", name
)
26210 && lookup_attribute ("noreturn", attributes
))
26211 error ("attribute %<noreturn%> can appear at most once "
26212 "in an attribute-list");
26213 else if (is_attribute_p ("deprecated", name
)
26214 && lookup_attribute ("deprecated", attributes
))
26215 error ("attribute %<deprecated%> can appear at most once "
26216 "in an attribute-list");
26220 /* Parse a list of standard C++-11 attributes.
26224 attribute-list , attribute[opt]
26226 attribute-list , attribute ...
26230 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
26232 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
26233 cp_token
*token
= NULL
;
26237 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
26238 if (attribute
== error_mark_node
)
26240 if (attribute
!= NULL_TREE
)
26242 cp_parser_check_std_attribute (attributes
, attribute
);
26243 TREE_CHAIN (attribute
) = attributes
;
26244 attributes
= attribute
;
26246 token
= cp_lexer_peek_token (parser
->lexer
);
26247 if (token
->type
== CPP_ELLIPSIS
)
26249 cp_lexer_consume_token (parser
->lexer
);
26250 if (attribute
== NULL_TREE
)
26251 error_at (token
->location
,
26252 "expected attribute before %<...%>");
26255 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
26256 if (pack
== error_mark_node
)
26257 return error_mark_node
;
26258 TREE_VALUE (attribute
) = pack
;
26260 token
= cp_lexer_peek_token (parser
->lexer
);
26262 if (token
->type
!= CPP_COMMA
)
26264 cp_lexer_consume_token (parser
->lexer
);
26266 attributes
= nreverse (attributes
);
26270 /* Parse a standard C++-11 attribute specifier.
26272 attribute-specifier:
26273 [ [ attribute-using-prefix [opt] attribute-list ] ]
26274 alignment-specifier
26276 attribute-using-prefix:
26277 using attribute-namespace :
26279 alignment-specifier:
26280 alignas ( type-id ... [opt] )
26281 alignas ( alignment-expression ... [opt] ). */
26284 cp_parser_std_attribute_spec (cp_parser
*parser
)
26286 tree attributes
= NULL_TREE
;
26287 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26289 if (token
->type
== CPP_OPEN_SQUARE
26290 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
26292 tree attr_ns
= NULL_TREE
;
26294 cp_lexer_consume_token (parser
->lexer
);
26295 cp_lexer_consume_token (parser
->lexer
);
26297 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26299 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26300 if (token
->type
== CPP_NAME
)
26301 attr_ns
= token
->u
.value
;
26302 else if (token
->type
== CPP_KEYWORD
)
26303 attr_ns
= ridpointers
[(int) token
->keyword
];
26304 else if (token
->flags
& NAMED_OP
)
26305 attr_ns
= get_identifier (cpp_type2name (token
->type
,
26308 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
26310 if (cxx_dialect
< cxx17
26311 && !in_system_header_at (input_location
))
26312 pedwarn (input_location
, 0,
26313 "attribute using prefix only available "
26314 "with %<-std=c++17%> or %<-std=gnu++17%>");
26316 cp_lexer_consume_token (parser
->lexer
);
26317 cp_lexer_consume_token (parser
->lexer
);
26318 cp_lexer_consume_token (parser
->lexer
);
26321 attr_ns
= NULL_TREE
;
26324 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
26326 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
26327 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
26328 cp_parser_skip_to_end_of_statement (parser
);
26330 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26331 when we are sure that we have actually parsed them. */
26332 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26338 /* Look for an alignment-specifier. */
26340 token
= cp_lexer_peek_token (parser
->lexer
);
26342 if (token
->type
!= CPP_KEYWORD
26343 || token
->keyword
!= RID_ALIGNAS
)
26346 cp_lexer_consume_token (parser
->lexer
);
26347 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26349 matching_parens parens
;
26350 if (!parens
.require_open (parser
))
26351 return error_mark_node
;
26353 cp_parser_parse_tentatively (parser
);
26354 alignas_expr
= cp_parser_type_id (parser
);
26356 if (!cp_parser_parse_definitely (parser
))
26358 alignas_expr
= cp_parser_assignment_expression (parser
);
26359 if (alignas_expr
== error_mark_node
)
26360 cp_parser_skip_to_end_of_statement (parser
);
26361 if (alignas_expr
== NULL_TREE
26362 || alignas_expr
== error_mark_node
)
26363 return alignas_expr
;
26366 alignas_expr
= cxx_alignas_expr (alignas_expr
);
26367 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
26369 /* Handle alignas (pack...). */
26370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26372 cp_lexer_consume_token (parser
->lexer
);
26373 alignas_expr
= make_pack_expansion (alignas_expr
);
26376 /* Something went wrong, so don't build the attribute. */
26377 if (alignas_expr
== error_mark_node
)
26378 return error_mark_node
;
26380 if (!parens
.require_close (parser
))
26381 return error_mark_node
;
26383 /* Build the C++-11 representation of an 'aligned'
26386 = build_tree_list (build_tree_list (gnu_identifier
,
26387 aligned_identifier
), alignas_expr
);
26393 /* Parse a standard C++-11 attribute-specifier-seq.
26395 attribute-specifier-seq:
26396 attribute-specifier-seq [opt] attribute-specifier
26400 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
26402 tree attr_specs
= NULL_TREE
;
26403 tree attr_last
= NULL_TREE
;
26405 /* Don't create wrapper nodes within attributes: the
26406 handlers don't know how to handle them. */
26407 auto_suppress_location_wrappers sentinel
;
26411 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
26412 if (attr_spec
== NULL_TREE
)
26414 if (attr_spec
== error_mark_node
)
26415 return error_mark_node
;
26418 TREE_CHAIN (attr_last
) = attr_spec
;
26420 attr_specs
= attr_last
= attr_spec
;
26421 attr_last
= tree_last (attr_last
);
26427 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26428 return index of the first token after balanced-token, or N on failure. */
26431 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26434 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26436 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26438 case CPP_PRAGMA_EOL
:
26439 if (!parser
->lexer
->in_pragma
)
26443 /* Ran out of tokens. */
26445 case CPP_OPEN_PAREN
:
26448 case CPP_OPEN_BRACE
:
26451 case CPP_OPEN_SQUARE
:
26454 case CPP_CLOSE_PAREN
:
26457 case CPP_CLOSE_BRACE
:
26460 case CPP_CLOSE_SQUARE
:
26466 while (nparens
|| nbraces
|| nsquares
);
26470 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26471 return index of the first token after the GNU attribute tokens, or N on
26475 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26479 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26480 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26481 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26484 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26487 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26494 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26495 next token), return index of the first token after the standard C++11
26496 attribute tokens, or N on failure. */
26499 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26503 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26504 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26506 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26509 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26513 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26514 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26516 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26527 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26528 as the next token), return index of the first token after the attribute
26529 tokens, or N on failure. */
26532 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
26534 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
26535 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
26536 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
26539 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26540 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26541 current value of the PEDANTIC flag, regardless of whether or not
26542 the `__extension__' keyword is present. The caller is responsible
26543 for restoring the value of the PEDANTIC flag. */
26546 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
26548 /* Save the old value of the PEDANTIC flag. */
26549 *saved_pedantic
= pedantic
;
26551 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
26553 /* Consume the `__extension__' token. */
26554 cp_lexer_consume_token (parser
->lexer
);
26555 /* We're not being pedantic while the `__extension__' keyword is
26565 /* Parse a label declaration.
26568 __label__ label-declarator-seq ;
26570 label-declarator-seq:
26571 identifier , label-declarator-seq
26575 cp_parser_label_declaration (cp_parser
* parser
)
26577 /* Look for the `__label__' keyword. */
26578 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
26584 /* Look for an identifier. */
26585 identifier
= cp_parser_identifier (parser
);
26586 /* If we failed, stop. */
26587 if (identifier
== error_mark_node
)
26589 /* Declare it as a label. */
26590 finish_label_decl (identifier
);
26591 /* If the next token is a `;', stop. */
26592 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26594 /* Look for the `,' separating the label declarations. */
26595 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
26598 /* Look for the final `;'. */
26599 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26602 // -------------------------------------------------------------------------- //
26605 // Parse a requires clause.
26607 // requires-clause:
26608 // 'requires' logical-or-expression
26610 // The required logical-or-expression must be a constant expression. Note
26611 // that we don't check that the expression is constepxr here. We defer until
26612 // we analyze constraints and then, we only check atomic constraints.
26614 cp_parser_requires_clause (cp_parser
*parser
)
26616 // Parse the requires clause so that it is not automatically folded.
26617 ++processing_template_decl
;
26618 tree expr
= cp_parser_binary_expression (parser
, false, false,
26619 PREC_NOT_OPERATOR
, NULL
);
26620 if (check_for_bare_parameter_packs (expr
))
26621 expr
= error_mark_node
;
26622 --processing_template_decl
;
26626 // Optionally parse a requires clause:
26628 cp_parser_requires_clause_opt (cp_parser
*parser
)
26630 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
26631 if (tok
->keyword
!= RID_REQUIRES
)
26633 if (!flag_concepts
&& tok
->type
== CPP_NAME
26634 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
26636 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26637 "%<requires%> only available with %<-fconcepts%>");
26638 /* Parse and discard the requires-clause. */
26639 cp_lexer_consume_token (parser
->lexer
);
26640 cp_parser_requires_clause (parser
);
26644 cp_lexer_consume_token (parser
->lexer
);
26645 return cp_parser_requires_clause (parser
);
26649 /*---------------------------------------------------------------------------
26650 Requires expressions
26651 ---------------------------------------------------------------------------*/
26653 /* Parse a requires expression
26655 requirement-expression:
26656 'requires' requirement-parameter-list [opt] requirement-body */
26658 cp_parser_requires_expression (cp_parser
*parser
)
26660 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
26661 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
26663 /* A requires-expression shall appear only within a concept
26664 definition or a requires-clause.
26666 TODO: Implement this diagnostic correctly. */
26667 if (!processing_template_decl
)
26669 error_at (loc
, "a requires expression cannot appear outside a template");
26670 cp_parser_skip_to_end_of_statement (parser
);
26671 return error_mark_node
;
26676 /* Local parameters are delared as variables within the scope
26677 of the expression. They are not visible past the end of
26678 the expression. Expressions within the requires-expression
26679 are unevaluated. */
26680 struct scope_sentinel
26684 ++cp_unevaluated_operand
;
26685 begin_scope (sk_block
, NULL_TREE
);
26690 pop_bindings_and_leave_scope ();
26691 --cp_unevaluated_operand
;
26695 /* Parse the optional parameter list. */
26696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26698 parms
= cp_parser_requirement_parameter_list (parser
);
26699 if (parms
== error_mark_node
)
26700 return error_mark_node
;
26705 /* Parse the requirement body. */
26706 reqs
= cp_parser_requirement_body (parser
);
26707 if (reqs
== error_mark_node
)
26708 return error_mark_node
;
26711 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26713 grokparms (parms
, &parms
);
26714 return finish_requires_expr (parms
, reqs
);
26717 /* Parse a parameterized requirement.
26719 requirement-parameter-list:
26720 '(' parameter-declaration-clause ')' */
26722 cp_parser_requirement_parameter_list (cp_parser
*parser
)
26724 matching_parens parens
;
26725 if (!parens
.require_open (parser
))
26726 return error_mark_node
;
26729 = cp_parser_parameter_declaration_clause (parser
, CP_PARSER_FLAGS_NONE
);
26731 if (!parens
.require_close (parser
))
26732 return error_mark_node
;
26737 /* Parse the body of a requirement.
26740 '{' requirement-list '}' */
26742 cp_parser_requirement_body (cp_parser
*parser
)
26744 matching_braces braces
;
26745 if (!braces
.require_open (parser
))
26746 return error_mark_node
;
26748 tree reqs
= cp_parser_requirement_list (parser
);
26750 if (!braces
.require_close (parser
))
26751 return error_mark_node
;
26756 /* Parse a list of requirements.
26760 requirement-list ';' requirement[opt] */
26762 cp_parser_requirement_list (cp_parser
*parser
)
26764 tree result
= NULL_TREE
;
26767 tree req
= cp_parser_requirement (parser
);
26768 if (req
== error_mark_node
)
26769 return error_mark_node
;
26771 result
= tree_cons (NULL_TREE
, req
, result
);
26773 /* If we see a semi-colon, consume it. */
26774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26775 cp_lexer_consume_token (parser
->lexer
);
26777 /* Stop processing at the end of the list. */
26778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26782 /* Reverse the order of requirements so they are analyzed in
26783 declaration order. */
26784 return nreverse (result
);
26787 /* Parse a syntactic requirement or type requirement.
26791 compound-requirement
26793 nested-requirement */
26795 cp_parser_requirement (cp_parser
*parser
)
26797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26798 return cp_parser_compound_requirement (parser
);
26799 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26800 return cp_parser_type_requirement (parser
);
26801 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26802 return cp_parser_nested_requirement (parser
);
26804 return cp_parser_simple_requirement (parser
);
26807 /* Parse a simple requirement.
26809 simple-requirement:
26812 cp_parser_simple_requirement (cp_parser
*parser
)
26814 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26815 if (!expr
|| expr
== error_mark_node
)
26816 return error_mark_node
;
26818 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26819 return error_mark_node
;
26821 return finish_simple_requirement (expr
);
26824 /* Parse a type requirement
26827 nested-name-specifier [opt] required-type-name ';'
26829 required-type-name:
26831 'template' [opt] simple-template-id */
26833 cp_parser_type_requirement (cp_parser
*parser
)
26835 cp_lexer_consume_token (parser
->lexer
);
26837 // Save the scope before parsing name specifiers.
26838 tree saved_scope
= parser
->scope
;
26839 tree saved_object_scope
= parser
->object_scope
;
26840 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26841 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26842 cp_parser_nested_name_specifier_opt (parser
,
26843 /*typename_keyword_p=*/true,
26844 /*check_dependency_p=*/false,
26846 /*is_declaration=*/false);
26849 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26851 cp_lexer_consume_token (parser
->lexer
);
26852 type
= cp_parser_template_id (parser
,
26853 /*template_keyword_p=*/true,
26854 /*check_dependency=*/false,
26855 /*tag_type=*/none_type
,
26856 /*is_declaration=*/false);
26857 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26858 /*complain=*/tf_error
);
26861 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26863 if (TREE_CODE (type
) == TYPE_DECL
)
26864 type
= TREE_TYPE (type
);
26866 parser
->scope
= saved_scope
;
26867 parser
->object_scope
= saved_object_scope
;
26868 parser
->qualifying_scope
= saved_qualifying_scope
;
26870 if (type
== error_mark_node
)
26871 cp_parser_skip_to_end_of_statement (parser
);
26873 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26874 return error_mark_node
;
26875 if (type
== error_mark_node
)
26876 return error_mark_node
;
26878 return finish_type_requirement (type
);
26881 /* Parse a compound requirement
26883 compound-requirement:
26884 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26886 cp_parser_compound_requirement (cp_parser
*parser
)
26888 /* Parse an expression enclosed in '{ }'s. */
26889 matching_braces braces
;
26890 if (!braces
.require_open (parser
))
26891 return error_mark_node
;
26893 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26894 if (!expr
|| expr
== error_mark_node
)
26895 return error_mark_node
;
26897 if (!braces
.require_close (parser
))
26898 return error_mark_node
;
26900 /* Parse the optional noexcept. */
26901 bool noexcept_p
= false;
26902 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26904 cp_lexer_consume_token (parser
->lexer
);
26908 /* Parse the optional trailing return type. */
26909 tree type
= NULL_TREE
;
26910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26912 cp_lexer_consume_token (parser
->lexer
);
26913 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26914 parser
->in_result_type_constraint_p
= true;
26915 type
= cp_parser_trailing_type_id (parser
);
26916 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26917 if (type
== error_mark_node
)
26918 return error_mark_node
;
26921 return finish_compound_requirement (expr
, type
, noexcept_p
);
26924 /* Parse a nested requirement. This is the same as a requires clause.
26926 nested-requirement:
26929 cp_parser_nested_requirement (cp_parser
*parser
)
26931 cp_lexer_consume_token (parser
->lexer
);
26932 tree req
= cp_parser_requires_clause (parser
);
26933 if (req
== error_mark_node
)
26934 return error_mark_node
;
26935 return finish_nested_requirement (req
);
26938 /* Support Functions */
26940 /* Return the appropriate prefer_type argument for lookup_name_real based on
26941 tag_type and template_mem_access. */
26944 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26946 /* DR 141: When looking in the current enclosing context for a template-name
26947 after -> or ., only consider class templates. */
26948 if (template_mem_access
)
26952 case none_type
: return 0; // No preference.
26953 case scope_type
: return 1; // Type or namespace.
26954 default: return 2; // Type only.
26958 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26959 NAME should have one of the representations used for an
26960 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26961 is returned. If PARSER->SCOPE is a dependent type, then a
26962 SCOPE_REF is returned.
26964 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26965 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26966 was formed. Abstractly, such entities should not be passed to this
26967 function, because they do not need to be looked up, but it is
26968 simpler to check for this special case here, rather than at the
26971 In cases not explicitly covered above, this function returns a
26972 DECL, OVERLOAD, or baselink representing the result of the lookup.
26973 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26976 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26977 (e.g., "struct") that was used. In that case bindings that do not
26978 refer to types are ignored.
26980 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26983 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26986 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26989 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26990 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26991 NULL_TREE otherwise. */
26994 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26995 enum tag_types tag_type
,
26998 bool check_dependency
,
26999 tree
*ambiguous_decls
,
27000 location_t name_location
)
27003 tree object_type
= parser
->context
->object_type
;
27005 /* Assume that the lookup will be unambiguous. */
27006 if (ambiguous_decls
)
27007 *ambiguous_decls
= NULL_TREE
;
27009 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27010 no longer valid. Note that if we are parsing tentatively, and
27011 the parse fails, OBJECT_TYPE will be automatically restored. */
27012 parser
->context
->object_type
= NULL_TREE
;
27014 if (name
== error_mark_node
)
27015 return error_mark_node
;
27017 /* A template-id has already been resolved; there is no lookup to
27019 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
27021 if (BASELINK_P (name
))
27023 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
27024 == TEMPLATE_ID_EXPR
);
27028 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27029 it should already have been checked to make sure that the name
27030 used matches the type being destroyed. */
27031 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
27035 /* Figure out to which type this destructor applies. */
27037 type
= parser
->scope
;
27038 else if (object_type
)
27039 type
= object_type
;
27041 type
= current_class_type
;
27042 /* If that's not a class type, there is no destructor. */
27043 if (!type
|| !CLASS_TYPE_P (type
))
27044 return error_mark_node
;
27046 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
27047 lazily_declare_fn (sfk_destructor
, type
);
27049 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
27052 return error_mark_node
;
27055 /* By this point, the NAME should be an ordinary identifier. If
27056 the id-expression was a qualified name, the qualifying scope is
27057 stored in PARSER->SCOPE at this point. */
27058 gcc_assert (identifier_p (name
));
27060 /* Perform the lookup. */
27065 if (parser
->scope
== error_mark_node
)
27066 return error_mark_node
;
27068 /* If the SCOPE is dependent, the lookup must be deferred until
27069 the template is instantiated -- unless we are explicitly
27070 looking up names in uninstantiated templates. Even then, we
27071 cannot look up the name if the scope is not a class type; it
27072 might, for example, be a template type parameter. */
27073 dependent_p
= (TYPE_P (parser
->scope
)
27074 && dependent_scope_p (parser
->scope
));
27075 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
27077 /* Defer lookup. */
27078 decl
= error_mark_node
;
27081 tree pushed_scope
= NULL_TREE
;
27083 /* If PARSER->SCOPE is a dependent type, then it must be a
27084 class type, and we must not be checking dependencies;
27085 otherwise, we would have processed this lookup above. So
27086 that PARSER->SCOPE is not considered a dependent base by
27087 lookup_member, we must enter the scope here. */
27089 pushed_scope
= push_scope (parser
->scope
);
27091 /* If the PARSER->SCOPE is a template specialization, it
27092 may be instantiated during name lookup. In that case,
27093 errors may be issued. Even if we rollback the current
27094 tentative parse, those errors are valid. */
27095 decl
= lookup_qualified_name (parser
->scope
, name
,
27096 prefer_type_arg (tag_type
),
27097 /*complain=*/true);
27099 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27100 lookup result and the nested-name-specifier nominates a class C:
27101 * if the name specified after the nested-name-specifier, when
27102 looked up in C, is the injected-class-name of C (Clause 9), or
27103 * if the name specified after the nested-name-specifier is the
27104 same as the identifier or the simple-template-id's template-
27105 name in the last component of the nested-name-specifier,
27106 the name is instead considered to name the constructor of
27107 class C. [ Note: for example, the constructor is not an
27108 acceptable lookup result in an elaborated-type-specifier so
27109 the constructor would not be used in place of the
27110 injected-class-name. --end note ] Such a constructor name
27111 shall be used only in the declarator-id of a declaration that
27112 names a constructor or in a using-declaration. */
27113 if (tag_type
== none_type
27114 && DECL_SELF_REFERENCE_P (decl
)
27115 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
27116 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
27117 prefer_type_arg (tag_type
),
27118 /*complain=*/true);
27120 /* If we have a single function from a using decl, pull it out. */
27121 if (TREE_CODE (decl
) == OVERLOAD
27122 && !really_overloaded_fn (decl
))
27123 decl
= OVL_FUNCTION (decl
);
27126 pop_scope (pushed_scope
);
27129 /* If the scope is a dependent type and either we deferred lookup or
27130 we did lookup but didn't find the name, rememeber the name. */
27131 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
27132 && dependent_type_p (parser
->scope
))
27138 /* The resolution to Core Issue 180 says that `struct
27139 A::B' should be considered a type-name, even if `A'
27141 type
= make_typename_type (parser
->scope
, name
, tag_type
,
27142 /*complain=*/tf_error
);
27143 if (type
!= error_mark_node
)
27144 decl
= TYPE_NAME (type
);
27146 else if (is_template
27147 && (cp_parser_next_token_ends_template_argument_p (parser
)
27148 || cp_lexer_next_token_is (parser
->lexer
,
27150 decl
= make_unbound_class_template (parser
->scope
,
27152 /*complain=*/tf_error
);
27154 decl
= build_qualified_name (/*type=*/NULL_TREE
,
27155 parser
->scope
, name
,
27158 parser
->qualifying_scope
= parser
->scope
;
27159 parser
->object_scope
= NULL_TREE
;
27161 else if (object_type
)
27163 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27164 OBJECT_TYPE is not a class. */
27165 if (CLASS_TYPE_P (object_type
))
27166 /* If the OBJECT_TYPE is a template specialization, it may
27167 be instantiated during name lookup. In that case, errors
27168 may be issued. Even if we rollback the current tentative
27169 parse, those errors are valid. */
27170 decl
= lookup_member (object_type
,
27173 prefer_type_arg (tag_type
),
27174 tf_warning_or_error
);
27179 /* Look it up in the enclosing context. DR 141: When looking for a
27180 template-name after -> or ., only consider class templates. */
27181 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
27183 /*block_p=*/true, is_namespace
, 0);
27184 if (object_type
== unknown_type_node
)
27185 /* The object is type-dependent, so we can't look anything up; we used
27186 this to get the DR 141 behavior. */
27187 object_type
= NULL_TREE
;
27188 parser
->object_scope
= object_type
;
27189 parser
->qualifying_scope
= NULL_TREE
;
27193 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
27195 /*block_p=*/true, is_namespace
, 0);
27196 parser
->qualifying_scope
= NULL_TREE
;
27197 parser
->object_scope
= NULL_TREE
;
27200 /* If the lookup failed, let our caller know. */
27201 if (!decl
|| decl
== error_mark_node
)
27202 return error_mark_node
;
27204 /* Pull out the template from an injected-class-name (or multiple). */
27206 decl
= maybe_get_template_decl_from_type_decl (decl
);
27208 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27209 if (TREE_CODE (decl
) == TREE_LIST
)
27211 if (ambiguous_decls
)
27212 *ambiguous_decls
= decl
;
27213 /* The error message we have to print is too complicated for
27214 cp_parser_error, so we incorporate its actions directly. */
27215 if (!cp_parser_simulate_error (parser
))
27217 error_at (name_location
, "reference to %qD is ambiguous",
27219 print_candidates (decl
);
27221 return error_mark_node
;
27224 gcc_assert (DECL_P (decl
)
27225 || TREE_CODE (decl
) == OVERLOAD
27226 || TREE_CODE (decl
) == SCOPE_REF
27227 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
27228 || BASELINK_P (decl
));
27230 /* If we have resolved the name of a member declaration, check to
27231 see if the declaration is accessible. When the name resolves to
27232 set of overloaded functions, accessibility is checked when
27233 overload resolution is done.
27235 During an explicit instantiation, access is not checked at all,
27236 as per [temp.explicit]. */
27238 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
27240 maybe_record_typedef_use (decl
);
27242 return cp_expr (decl
, name_location
);
27245 /* Like cp_parser_lookup_name, but for use in the typical case where
27246 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27247 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27250 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
27252 return cp_parser_lookup_name (parser
, name
,
27254 /*is_template=*/false,
27255 /*is_namespace=*/false,
27256 /*check_dependency=*/true,
27257 /*ambiguous_decls=*/NULL
,
27261 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27262 the current context, return the TYPE_DECL. If TAG_NAME_P is
27263 true, the DECL indicates the class being defined in a class-head,
27264 or declared in an elaborated-type-specifier.
27266 Otherwise, return DECL. */
27269 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
27271 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27272 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27275 template <typename T> struct B;
27278 template <typename T> struct A::B {};
27280 Similarly, in an elaborated-type-specifier:
27282 namespace N { struct X{}; }
27285 template <typename T> friend struct N::X;
27288 However, if the DECL refers to a class type, and we are in
27289 the scope of the class, then the name lookup automatically
27290 finds the TYPE_DECL created by build_self_reference rather
27291 than a TEMPLATE_DECL. For example, in:
27293 template <class T> struct S {
27297 there is no need to handle such case. */
27299 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
27300 return DECL_TEMPLATE_RESULT (decl
);
27305 /* If too many, or too few, template-parameter lists apply to the
27306 declarator, issue an error message. Returns TRUE if all went well,
27307 and FALSE otherwise. */
27310 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
27311 cp_declarator
*declarator
,
27312 location_t declarator_location
)
27314 switch (declarator
->kind
)
27318 unsigned num_templates
= 0;
27319 tree scope
= declarator
->u
.id
.qualifying_scope
;
27320 bool template_id_p
= false;
27323 num_templates
= num_template_headers_for_class (scope
);
27324 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
27325 == TEMPLATE_ID_EXPR
)
27327 /* If the DECLARATOR has the form `X<y>' then it uses one
27328 additional level of template parameters. */
27330 template_id_p
= true;
27333 return cp_parser_check_template_parameters
27334 (parser
, num_templates
, template_id_p
, declarator_location
,
27341 case cdk_reference
:
27343 return (cp_parser_check_declarator_template_parameters
27344 (parser
, declarator
->declarator
, declarator_location
));
27351 gcc_unreachable ();
27356 /* NUM_TEMPLATES were used in the current declaration. If that is
27357 invalid, return FALSE and issue an error messages. Otherwise,
27358 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27359 declarator and we can print more accurate diagnostics. */
27362 cp_parser_check_template_parameters (cp_parser
* parser
,
27363 unsigned num_templates
,
27364 bool template_id_p
,
27365 location_t location
,
27366 cp_declarator
*declarator
)
27368 /* If there are the same number of template classes and parameter
27369 lists, that's OK. */
27370 if (parser
->num_template_parameter_lists
== num_templates
)
27372 /* If there are more, but only one more, and the name ends in an identifier,
27373 then we are declaring a primary template. That's OK too. */
27375 && parser
->num_template_parameter_lists
== num_templates
+ 1)
27377 /* If there are more template classes than parameter lists, we have
27380 template <class T> void S<T>::R<T>::f (); */
27381 if (parser
->num_template_parameter_lists
< num_templates
)
27383 if (declarator
&& !current_function_decl
)
27384 error_at (location
, "specializing member %<%T::%E%> "
27385 "requires %<template<>%> syntax",
27386 declarator
->u
.id
.qualifying_scope
,
27387 declarator
->u
.id
.unqualified_name
);
27388 else if (declarator
)
27389 error_at (location
, "invalid declaration of %<%T::%E%>",
27390 declarator
->u
.id
.qualifying_scope
,
27391 declarator
->u
.id
.unqualified_name
);
27393 error_at (location
, "too few template-parameter-lists");
27396 /* Otherwise, there are too many template parameter lists. We have
27399 template <class T> template <class U> void S::f(); */
27400 error_at (location
, "too many template-parameter-lists");
27404 /* Parse an optional `::' token indicating that the following name is
27405 from the global namespace. If so, PARSER->SCOPE is set to the
27406 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27407 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27408 Returns the new value of PARSER->SCOPE, if the `::' token is
27409 present, and NULL_TREE otherwise. */
27412 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
27416 /* Peek at the next token. */
27417 token
= cp_lexer_peek_token (parser
->lexer
);
27418 /* If we're looking at a `::' token then we're starting from the
27419 global namespace, not our current location. */
27420 if (token
->type
== CPP_SCOPE
)
27422 /* Consume the `::' token. */
27423 cp_lexer_consume_token (parser
->lexer
);
27424 /* Set the SCOPE so that we know where to start the lookup. */
27425 parser
->scope
= global_namespace
;
27426 parser
->qualifying_scope
= global_namespace
;
27427 parser
->object_scope
= NULL_TREE
;
27429 return parser
->scope
;
27431 else if (!current_scope_valid_p
)
27433 parser
->scope
= NULL_TREE
;
27434 parser
->qualifying_scope
= NULL_TREE
;
27435 parser
->object_scope
= NULL_TREE
;
27441 /* Returns TRUE if the upcoming token sequence is the start of a
27442 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27443 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27444 is used to control type-specifier parsing. */
27447 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
27450 bool constructor_p
;
27451 bool outside_class_specifier_p
;
27452 tree nested_name_specifier
;
27453 cp_token
*next_token
;
27455 /* The common case is that this is not a constructor declarator, so
27456 try to avoid doing lots of work if at all possible. It's not
27457 valid declare a constructor at function scope. */
27458 if (parser
->in_function_body
)
27460 /* And only certain tokens can begin a constructor declarator. */
27461 next_token
= cp_lexer_peek_token (parser
->lexer
);
27462 if (next_token
->type
!= CPP_NAME
27463 && next_token
->type
!= CPP_SCOPE
27464 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
27465 && next_token
->type
!= CPP_TEMPLATE_ID
)
27468 /* Parse tentatively; we are going to roll back all of the tokens
27470 cp_parser_parse_tentatively (parser
);
27471 /* Assume that we are looking at a constructor declarator. */
27472 constructor_p
= true;
27474 /* Look for the optional `::' operator. */
27475 cp_parser_global_scope_opt (parser
,
27476 /*current_scope_valid_p=*/false);
27477 /* Look for the nested-name-specifier. */
27478 nested_name_specifier
27479 = (cp_parser_nested_name_specifier_opt (parser
,
27480 /*typename_keyword_p=*/false,
27481 /*check_dependency_p=*/false,
27483 /*is_declaration=*/false));
27485 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27486 if (nested_name_specifier
27487 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
27489 tree s
= resolve_typename_type (nested_name_specifier
,
27490 /*only_current_p=*/false);
27491 if (TREE_CODE (s
) != TYPENAME_TYPE
)
27492 nested_name_specifier
= s
;
27495 outside_class_specifier_p
= (!at_class_scope_p ()
27496 || !TYPE_BEING_DEFINED (current_class_type
)
27499 /* Outside of a class-specifier, there must be a
27500 nested-name-specifier. Except in C++17 mode, where we
27501 might be declaring a guiding declaration. */
27502 if (!nested_name_specifier
&& outside_class_specifier_p
27503 && cxx_dialect
< cxx17
)
27504 constructor_p
= false;
27505 else if (nested_name_specifier
== error_mark_node
)
27506 constructor_p
= false;
27508 /* If we have a class scope, this is easy; DR 147 says that S::S always
27509 names the constructor, and no other qualified name could. */
27510 if (constructor_p
&& nested_name_specifier
27511 && CLASS_TYPE_P (nested_name_specifier
))
27513 tree id
= cp_parser_unqualified_id (parser
,
27514 /*template_keyword_p=*/false,
27515 /*check_dependency_p=*/false,
27516 /*declarator_p=*/true,
27517 /*optional_p=*/false);
27518 if (is_overloaded_fn (id
))
27519 id
= DECL_NAME (get_first_fn (id
));
27520 if (!constructor_name_p (id
, nested_name_specifier
))
27521 constructor_p
= false;
27523 /* If we still think that this might be a constructor-declarator,
27524 look for a class-name. */
27525 else if (constructor_p
)
27529 template <typename T> struct S {
27533 we must recognize that the nested `S' names a class. */
27534 if (cxx_dialect
>= cxx17
)
27535 cp_parser_parse_tentatively (parser
);
27538 type_decl
= cp_parser_class_name (parser
,
27539 /*typename_keyword_p=*/false,
27540 /*template_keyword_p=*/false,
27542 /*check_dependency_p=*/false,
27543 /*class_head_p=*/false,
27544 /*is_declaration=*/false);
27546 if (cxx_dialect
>= cxx17
27547 && !cp_parser_parse_definitely (parser
))
27549 type_decl
= NULL_TREE
;
27550 tree tmpl
= cp_parser_template_name (parser
,
27551 /*template_keyword*/false,
27552 /*check_dependency_p*/false,
27553 /*is_declaration*/false,
27555 /*is_identifier*/NULL
);
27556 if (DECL_CLASS_TEMPLATE_P (tmpl
)
27557 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
27558 /* It's a deduction guide, return true. */;
27560 cp_parser_simulate_error (parser
);
27563 /* If there was no class-name, then this is not a constructor.
27564 Otherwise, if we are in a class-specifier and we aren't
27565 handling a friend declaration, check that its type matches
27566 current_class_type (c++/38313). Note: error_mark_node
27567 is left alone for error recovery purposes. */
27568 constructor_p
= (!cp_parser_error_occurred (parser
)
27569 && (outside_class_specifier_p
27570 || type_decl
== NULL_TREE
27571 || type_decl
== error_mark_node
27572 || same_type_p (current_class_type
,
27573 TREE_TYPE (type_decl
))));
27575 /* If we're still considering a constructor, we have to see a `(',
27576 to begin the parameter-declaration-clause, followed by either a
27577 `)', an `...', or a decl-specifier. We need to check for a
27578 type-specifier to avoid being fooled into thinking that:
27582 is a constructor. (It is actually a function named `f' that
27583 takes one parameter (of type `int') and returns a value of type
27586 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27587 constructor_p
= false;
27590 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
27591 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
27592 /* A parameter declaration begins with a decl-specifier,
27593 which is either the "attribute" keyword, a storage class
27594 specifier, or (usually) a type-specifier. */
27595 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
27598 tree pushed_scope
= NULL_TREE
;
27599 unsigned saved_num_template_parameter_lists
;
27601 /* Names appearing in the type-specifier should be looked up
27602 in the scope of the class. */
27603 if (current_class_type
)
27605 else if (type_decl
)
27607 type
= TREE_TYPE (type_decl
);
27608 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27610 type
= resolve_typename_type (type
,
27611 /*only_current_p=*/false);
27612 if (TREE_CODE (type
) == TYPENAME_TYPE
)
27614 cp_parser_abort_tentative_parse (parser
);
27618 pushed_scope
= push_scope (type
);
27621 /* Inside the constructor parameter list, surrounding
27622 template-parameter-lists do not apply. */
27623 saved_num_template_parameter_lists
27624 = parser
->num_template_parameter_lists
;
27625 parser
->num_template_parameter_lists
= 0;
27627 /* Look for the type-specifier. It's not optional, but its typename
27629 cp_parser_type_specifier (parser
,
27630 (flags
& ~CP_PARSER_FLAGS_OPTIONAL
),
27631 /*decl_specs=*/NULL
,
27632 /*is_declarator=*/true,
27633 /*declares_class_or_enum=*/NULL
,
27634 /*is_cv_qualifier=*/NULL
);
27636 parser
->num_template_parameter_lists
27637 = saved_num_template_parameter_lists
;
27639 /* Leave the scope of the class. */
27641 pop_scope (pushed_scope
);
27643 constructor_p
= !cp_parser_error_occurred (parser
);
27647 /* We did not really want to consume any tokens. */
27648 cp_parser_abort_tentative_parse (parser
);
27650 return constructor_p
;
27653 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27654 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27655 they must be performed once we are in the scope of the function.
27657 Returns the function defined. */
27660 cp_parser_function_definition_from_specifiers_and_declarator
27661 (cp_parser
* parser
,
27662 cp_decl_specifier_seq
*decl_specifiers
,
27664 const cp_declarator
*declarator
)
27669 /* Begin the function-definition. */
27670 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
27672 /* The things we're about to see are not directly qualified by any
27673 template headers we've seen thus far. */
27674 reset_specialization ();
27676 /* If there were names looked up in the decl-specifier-seq that we
27677 did not check, check them now. We must wait until we are in the
27678 scope of the function to perform the checks, since the function
27679 might be a friend. */
27680 perform_deferred_access_checks (tf_warning_or_error
);
27684 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
27685 parser
->omp_declare_simd
= NULL
;
27686 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
27687 parser
->oacc_routine
= NULL
;
27692 /* Skip the entire function. */
27693 cp_parser_skip_to_end_of_block_or_statement (parser
);
27694 fn
= error_mark_node
;
27696 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
27698 /* Seen already, skip it. An error message has already been output. */
27699 cp_parser_skip_to_end_of_block_or_statement (parser
);
27700 fn
= current_function_decl
;
27701 current_function_decl
= NULL_TREE
;
27702 /* If this is a function from a class, pop the nested class. */
27703 if (current_class_name
)
27704 pop_nested_class ();
27709 if (DECL_DECLARED_INLINE_P (current_function_decl
))
27710 tv
= TV_PARSE_INLINE
;
27712 tv
= TV_PARSE_FUNC
;
27714 fn
= cp_parser_function_definition_after_declarator (parser
,
27715 /*inline_p=*/false);
27722 /* Parse the part of a function-definition that follows the
27723 declarator. INLINE_P is TRUE iff this function is an inline
27724 function defined within a class-specifier.
27726 Returns the function defined. */
27729 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
27733 bool saved_in_unbraced_linkage_specification_p
;
27734 bool saved_in_function_body
;
27735 unsigned saved_num_template_parameter_lists
;
27737 bool fully_implicit_function_template_p
27738 = parser
->fully_implicit_function_template_p
;
27739 parser
->fully_implicit_function_template_p
= false;
27740 tree implicit_template_parms
27741 = parser
->implicit_template_parms
;
27742 parser
->implicit_template_parms
= 0;
27743 cp_binding_level
* implicit_template_scope
27744 = parser
->implicit_template_scope
;
27745 parser
->implicit_template_scope
= 0;
27747 saved_in_function_body
= parser
->in_function_body
;
27748 parser
->in_function_body
= true;
27749 /* If the next token is `return', then the code may be trying to
27750 make use of the "named return value" extension that G++ used to
27752 token
= cp_lexer_peek_token (parser
->lexer
);
27753 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
27755 /* Consume the `return' keyword. */
27756 cp_lexer_consume_token (parser
->lexer
);
27757 /* Look for the identifier that indicates what value is to be
27759 cp_parser_identifier (parser
);
27760 /* Issue an error message. */
27761 error_at (token
->location
,
27762 "named return values are no longer supported");
27763 /* Skip tokens until we reach the start of the function body. */
27766 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27767 if (token
->type
== CPP_OPEN_BRACE
27768 || token
->type
== CPP_EOF
27769 || token
->type
== CPP_PRAGMA_EOL
)
27771 cp_lexer_consume_token (parser
->lexer
);
27774 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27775 anything declared inside `f'. */
27776 saved_in_unbraced_linkage_specification_p
27777 = parser
->in_unbraced_linkage_specification_p
;
27778 parser
->in_unbraced_linkage_specification_p
= false;
27779 /* Inside the function, surrounding template-parameter-lists do not
27781 saved_num_template_parameter_lists
27782 = parser
->num_template_parameter_lists
;
27783 parser
->num_template_parameter_lists
= 0;
27785 /* If the next token is `try', `__transaction_atomic', or
27786 `__transaction_relaxed`, then we are looking at either function-try-block
27787 or function-transaction-block. Note that all of these include the
27789 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
27790 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
27791 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27792 RID_TRANSACTION_RELAXED
))
27793 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
27794 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27795 cp_parser_function_try_block (parser
);
27797 cp_parser_ctor_initializer_opt_and_function_body
27798 (parser
, /*in_function_try_block=*/false);
27800 /* Finish the function. */
27801 fn
= finish_function (inline_p
);
27802 /* Generate code for it, if necessary. */
27803 expand_or_defer_fn (fn
);
27804 /* Restore the saved values. */
27805 parser
->in_unbraced_linkage_specification_p
27806 = saved_in_unbraced_linkage_specification_p
;
27807 parser
->num_template_parameter_lists
27808 = saved_num_template_parameter_lists
;
27809 parser
->in_function_body
= saved_in_function_body
;
27811 parser
->fully_implicit_function_template_p
27812 = fully_implicit_function_template_p
;
27813 parser
->implicit_template_parms
27814 = implicit_template_parms
;
27815 parser
->implicit_template_scope
27816 = implicit_template_scope
;
27818 if (parser
->fully_implicit_function_template_p
)
27819 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27824 /* Parse a template-declaration body (following argument list). */
27827 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27828 tree parameter_list
,
27831 tree decl
= NULL_TREE
;
27832 bool friend_p
= false;
27834 /* We just processed one more parameter list. */
27835 ++parser
->num_template_parameter_lists
;
27837 /* Get the deferred access checks from the parameter list. These
27838 will be checked once we know what is being declared, as for a
27839 member template the checks must be performed in the scope of the
27840 class containing the member. */
27841 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27843 /* Tentatively parse for a new template parameter list, which can either be
27844 the template keyword or a template introduction. */
27845 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27847 else if (cxx_dialect
>= cxx11
27848 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27849 decl
= cp_parser_alias_declaration (parser
);
27852 /* There are no access checks when parsing a template, as we do not
27853 know if a specialization will be a friend. */
27854 push_deferring_access_checks (dk_no_check
);
27855 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27856 decl
= cp_parser_single_declaration (parser
,
27859 /*explicit_specialization_p=*/false,
27861 pop_deferring_access_checks ();
27863 /* If this is a member template declaration, let the front
27865 if (member_p
&& !friend_p
&& decl
)
27867 if (TREE_CODE (decl
) == TYPE_DECL
)
27868 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27870 decl
= finish_member_template_decl (decl
);
27872 else if (friend_p
&& decl
27873 && DECL_DECLARES_TYPE_P (decl
))
27874 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27875 /*complain=*/true);
27877 /* We are done with the current parameter list. */
27878 --parser
->num_template_parameter_lists
;
27880 pop_deferring_access_checks ();
27883 finish_template_decl (parameter_list
);
27885 /* Check the template arguments for a literal operator template. */
27887 && DECL_DECLARES_FUNCTION_P (decl
)
27888 && UDLIT_OPER_P (DECL_NAME (decl
)))
27891 if (parameter_list
== NULL_TREE
)
27895 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27896 if (num_parms
== 1)
27898 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27899 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27900 if (CLASS_TYPE_P (TREE_TYPE (parm
)))
27901 /* OK, C++20 string literal operator template. We don't need
27902 to warn in lower dialects here because we will have already
27903 warned about the template parameter. */;
27904 else if (TREE_TYPE (parm
) != char_type_node
27905 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27908 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27910 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27911 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27912 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27913 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27914 if (parm
== error_mark_node
27915 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27916 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27919 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27920 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
27921 "ISO C++ did not adopt string literal operator templa"
27922 "tes taking an argument pack of characters");
27929 if (cxx_dialect
> cxx17
)
27930 error ("literal operator template %qD has invalid parameter list;"
27931 " Expected non-type template parameter pack <char...> "
27932 "or single non-type parameter of class type",
27935 error ("literal operator template %qD has invalid parameter list."
27936 " Expected non-type template parameter pack <char...>",
27941 /* Register member declarations. */
27942 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27943 finish_member_declaration (decl
);
27944 /* If DECL is a function template, we must return to parse it later.
27945 (Even though there is no definition, there might be default
27946 arguments that need handling.) */
27947 if (member_p
&& decl
27948 && DECL_DECLARES_FUNCTION_P (decl
))
27949 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27952 /* Parse a template introduction header for a template-declaration. Returns
27953 false if tentative parse fails. */
27956 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27958 cp_parser_parse_tentatively (parser
);
27960 tree saved_scope
= parser
->scope
;
27961 tree saved_object_scope
= parser
->object_scope
;
27962 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27964 /* Look for the optional `::' operator. */
27965 cp_parser_global_scope_opt (parser
,
27966 /*current_scope_valid_p=*/false);
27967 /* Look for the nested-name-specifier. */
27968 cp_parser_nested_name_specifier_opt (parser
,
27969 /*typename_keyword_p=*/false,
27970 /*check_dependency_p=*/true,
27972 /*is_declaration=*/false);
27974 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27975 tree concept_name
= cp_parser_identifier (parser
);
27977 /* Look up the concept for which we will be matching
27978 template parameters. */
27979 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27981 parser
->scope
= saved_scope
;
27982 parser
->object_scope
= saved_object_scope
;
27983 parser
->qualifying_scope
= saved_qualifying_scope
;
27985 if (concept_name
== error_mark_node
)
27986 cp_parser_simulate_error (parser
);
27988 /* Look for opening brace for introduction. */
27989 matching_braces braces
;
27990 braces
.require_open (parser
);
27992 if (!cp_parser_parse_definitely (parser
))
27995 push_deferring_access_checks (dk_deferred
);
27997 /* Build vector of placeholder parameters and grab
27998 matching identifiers. */
27999 tree introduction_list
= cp_parser_introduction_list (parser
);
28001 /* Look for closing brace for introduction. */
28002 if (!braces
.require_close (parser
))
28005 /* The introduction-list shall not be empty. */
28006 int nargs
= TREE_VEC_LENGTH (introduction_list
);
28009 /* In cp_parser_introduction_list we have already issued an error. */
28013 if (tmpl_decl
== error_mark_node
)
28015 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
28020 /* Build and associate the constraint. */
28021 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
28022 if (parms
&& parms
!= error_mark_node
)
28024 cp_parser_template_declaration_after_parameters (parser
, parms
,
28029 error_at (token
->location
, "no matching concept for template-introduction");
28033 /* Parse a normal template-declaration following the template keyword. */
28036 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
28038 tree parameter_list
;
28039 bool need_lang_pop
;
28040 location_t location
= input_location
;
28042 /* Look for the `<' token. */
28043 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
28045 if (at_class_scope_p () && current_function_decl
)
28047 /* 14.5.2.2 [temp.mem]
28049 A local class shall not have member templates. */
28050 error_at (location
,
28051 "invalid declaration of member template in local class");
28052 cp_parser_skip_to_end_of_block_or_statement (parser
);
28057 A template ... shall not have C linkage. */
28058 if (current_lang_name
== lang_name_c
)
28060 error_at (location
, "template with C linkage");
28061 maybe_show_extern_c_location ();
28062 /* Give it C++ linkage to avoid confusing other parts of the
28064 push_lang_context (lang_name_cplusplus
);
28065 need_lang_pop
= true;
28068 need_lang_pop
= false;
28070 /* We cannot perform access checks on the template parameter
28071 declarations until we know what is being declared, just as we
28072 cannot check the decl-specifier list. */
28073 push_deferring_access_checks (dk_deferred
);
28075 /* If the next token is `>', then we have an invalid
28076 specialization. Rather than complain about an invalid template
28077 parameter, issue an error message here. */
28078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
28080 cp_parser_error (parser
, "invalid explicit specialization");
28081 begin_specialization ();
28082 parameter_list
= NULL_TREE
;
28086 /* Parse the template parameters. */
28087 parameter_list
= cp_parser_template_parameter_list (parser
);
28090 /* Look for the `>'. */
28091 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28093 /* Manage template requirements */
28096 tree reqs
= get_shorthand_constraints (current_template_parms
);
28097 if (tree r
= cp_parser_requires_clause_opt (parser
))
28098 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
28099 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
28102 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
28105 /* For the erroneous case of a template with C linkage, we pushed an
28106 implicit C++ linkage scope; exit that scope now. */
28108 pop_lang_context ();
28111 /* Parse a template-declaration, assuming that the `export' (and
28112 `extern') keywords, if present, has already been scanned. MEMBER_P
28113 is as for cp_parser_template_declaration. */
28116 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
28118 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28120 cp_lexer_consume_token (parser
->lexer
);
28121 cp_parser_explicit_template_declaration (parser
, member_p
);
28124 else if (flag_concepts
)
28125 return cp_parser_template_introduction (parser
, member_p
);
28130 /* Perform the deferred access checks from a template-parameter-list.
28131 CHECKS is a TREE_LIST of access checks, as returned by
28132 get_deferred_access_checks. */
28135 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
28137 ++processing_template_parmlist
;
28138 perform_access_checks (checks
, tf_warning_or_error
);
28139 --processing_template_parmlist
;
28142 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28143 `function-definition' sequence that follows a template header.
28144 If MEMBER_P is true, this declaration appears in a class scope.
28146 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28147 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28150 cp_parser_single_declaration (cp_parser
* parser
,
28151 vec
<deferred_access_check
, va_gc
> *checks
,
28153 bool explicit_specialization_p
,
28156 int declares_class_or_enum
;
28157 tree decl
= NULL_TREE
;
28158 cp_decl_specifier_seq decl_specifiers
;
28159 bool function_definition_p
= false;
28160 cp_token
*decl_spec_token_start
;
28162 /* This function is only used when processing a template
28164 gcc_assert (innermost_scope_kind () == sk_template_parms
28165 || innermost_scope_kind () == sk_template_spec
);
28167 /* Defer access checks until we know what is being declared. */
28168 push_deferring_access_checks (dk_deferred
);
28170 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28172 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
28173 cp_parser_decl_specifier_seq (parser
,
28174 (CP_PARSER_FLAGS_OPTIONAL
28175 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
28177 &declares_class_or_enum
);
28179 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
28181 /* There are no template typedefs. */
28182 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
28184 error_at (decl_spec_token_start
->location
,
28185 "template declaration of %<typedef%>");
28186 decl
= error_mark_node
;
28189 /* Gather up the access checks that occurred the
28190 decl-specifier-seq. */
28191 stop_deferring_access_checks ();
28193 /* Check for the declaration of a template class. */
28194 if (declares_class_or_enum
)
28196 if (cp_parser_declares_only_class_p (parser
)
28197 || (declares_class_or_enum
& 2))
28199 // If this is a declaration, but not a definition, associate
28200 // any constraints with the type declaration. Constraints
28201 // are associated with definitions in cp_parser_class_specifier.
28202 if (declares_class_or_enum
== 1)
28203 associate_classtype_constraints (decl_specifiers
.type
);
28205 decl
= shadow_tag (&decl_specifiers
);
28210 friend template <typename T> struct A<T>::B;
28213 A<T>::B will be represented by a TYPENAME_TYPE, and
28214 therefore not recognized by shadow_tag. */
28215 if (friend_p
&& *friend_p
28217 && decl_specifiers
.type
28218 && TYPE_P (decl_specifiers
.type
))
28219 decl
= decl_specifiers
.type
;
28221 if (decl
&& decl
!= error_mark_node
)
28222 decl
= TYPE_NAME (decl
);
28224 decl
= error_mark_node
;
28226 /* Perform access checks for template parameters. */
28227 cp_parser_perform_template_parameter_access_checks (checks
);
28229 /* Give a helpful diagnostic for
28230 template <class T> struct A { } a;
28231 if we aren't already recovering from an error. */
28232 if (!cp_parser_declares_only_class_p (parser
)
28235 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28236 "a class template declaration must not declare "
28238 cp_parser_skip_to_end_of_block_or_statement (parser
);
28244 /* Complain about missing 'typename' or other invalid type names. */
28245 if (!decl_specifiers
.any_type_specifiers_p
28246 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
28248 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28249 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28250 the rest of this declaration. */
28251 decl
= error_mark_node
;
28255 /* If it's not a template class, try for a template function. If
28256 the next token is a `;', then this declaration does not declare
28257 anything. But, if there were errors in the decl-specifiers, then
28258 the error might well have come from an attempted class-specifier.
28259 In that case, there's no need to warn about a missing declarator. */
28261 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
28262 || decl_specifiers
.type
!= error_mark_node
))
28264 decl
= cp_parser_init_declarator (parser
,
28265 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
28268 /*function_definition_allowed_p=*/true,
28270 declares_class_or_enum
,
28271 &function_definition_p
,
28274 /* 7.1.1-1 [dcl.stc]
28276 A storage-class-specifier shall not be specified in an explicit
28277 specialization... */
28279 && explicit_specialization_p
28280 && decl_specifiers
.storage_class
!= sc_none
)
28282 error_at (decl_spec_token_start
->location
,
28283 "explicit template specialization cannot have a storage class");
28284 decl
= error_mark_node
;
28287 if (decl
&& VAR_P (decl
))
28288 check_template_variable (decl
);
28291 /* Look for a trailing `;' after the declaration. */
28292 if (!function_definition_p
28293 && (decl
== error_mark_node
28294 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
28295 cp_parser_skip_to_end_of_block_or_statement (parser
);
28298 pop_deferring_access_checks ();
28300 /* Clear any current qualification; whatever comes next is the start
28301 of something new. */
28302 parser
->scope
= NULL_TREE
;
28303 parser
->qualifying_scope
= NULL_TREE
;
28304 parser
->object_scope
= NULL_TREE
;
28309 /* Parse a cast-expression that is not the operand of a unary "&". */
28312 cp_parser_simple_cast_expression (cp_parser
*parser
)
28314 return cp_parser_cast_expression (parser
, /*address_p=*/false,
28315 /*cast_p=*/false, /*decltype*/false, NULL
);
28318 /* Parse a functional cast to TYPE. Returns an expression
28319 representing the cast. */
28322 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
28324 vec
<tree
, va_gc
> *vec
;
28325 tree expression_list
;
28329 location_t start_loc
= input_location
;
28332 type
= error_mark_node
;
28334 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28336 cp_lexer_set_source_position (parser
->lexer
);
28337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28338 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
28339 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
28340 if (TREE_CODE (type
) == TYPE_DECL
)
28341 type
= TREE_TYPE (type
);
28343 cast
= finish_compound_literal (type
, expression_list
,
28344 tf_warning_or_error
, fcl_functional
);
28345 /* Create a location of the form:
28348 with caret == start at the start of the type name,
28349 finishing at the closing brace. */
28350 location_t finish_loc
28351 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28352 location_t combined_loc
= make_location (start_loc
, start_loc
,
28354 cast
.set_location (combined_loc
);
28359 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28361 /*allow_expansion_p=*/true,
28362 /*non_constant_p=*/NULL
);
28364 expression_list
= error_mark_node
;
28367 expression_list
= build_tree_list_vec (vec
);
28368 release_tree_vector (vec
);
28371 cast
= build_functional_cast (type
, expression_list
,
28372 tf_warning_or_error
);
28373 /* [expr.const]/1: In an integral constant expression "only type
28374 conversions to integral or enumeration type can be used". */
28375 if (TREE_CODE (type
) == TYPE_DECL
)
28376 type
= TREE_TYPE (type
);
28377 if (cast
!= error_mark_node
28378 && !cast_valid_in_integral_constant_expression_p (type
)
28379 && cp_parser_non_integral_constant_expression (parser
,
28381 return error_mark_node
;
28383 /* Create a location of the form:
28386 with caret == start at the start of the type name,
28387 finishing at the closing paren. */
28388 location_t finish_loc
28389 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
28390 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
28391 cast
.set_location (combined_loc
);
28395 /* Save the tokens that make up the body of a member function defined
28396 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28397 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28398 specifiers applied to the declaration. Returns the FUNCTION_DECL
28399 for the member function. */
28402 cp_parser_save_member_function_body (cp_parser
* parser
,
28403 cp_decl_specifier_seq
*decl_specifiers
,
28404 cp_declarator
*declarator
,
28410 bool function_try_block
= false;
28412 /* Create the FUNCTION_DECL. */
28413 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
28414 cp_finalize_omp_declare_simd (parser
, fn
);
28415 cp_finalize_oacc_routine (parser
, fn
, true);
28416 /* If something went badly wrong, bail out now. */
28417 if (fn
== error_mark_node
)
28419 /* If there's a function-body, skip it. */
28420 if (cp_parser_token_starts_function_definition_p
28421 (cp_lexer_peek_token (parser
->lexer
)))
28422 cp_parser_skip_to_end_of_block_or_statement (parser
);
28423 return error_mark_node
;
28426 /* Remember it, if there default args to post process. */
28427 cp_parser_save_default_args (parser
, fn
);
28429 /* Save away the tokens that make up the body of the
28431 first
= parser
->lexer
->next_token
;
28433 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
28434 cp_lexer_consume_token (parser
->lexer
);
28435 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
28436 RID_TRANSACTION_ATOMIC
))
28438 cp_lexer_consume_token (parser
->lexer
);
28439 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
28441 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
28442 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
28443 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
28444 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
28445 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
28447 cp_lexer_consume_token (parser
->lexer
);
28448 cp_lexer_consume_token (parser
->lexer
);
28449 cp_lexer_consume_token (parser
->lexer
);
28450 cp_lexer_consume_token (parser
->lexer
);
28451 cp_lexer_consume_token (parser
->lexer
);
28454 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
28455 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
28457 cp_lexer_consume_token (parser
->lexer
);
28458 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28463 /* Handle function try blocks. */
28464 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28466 cp_lexer_consume_token (parser
->lexer
);
28467 function_try_block
= true;
28469 /* We can have braced-init-list mem-initializers before the fn body. */
28470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
28472 cp_lexer_consume_token (parser
->lexer
);
28473 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
28475 /* cache_group will stop after an un-nested { } pair, too. */
28476 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
28479 /* variadic mem-inits have ... after the ')'. */
28480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28481 cp_lexer_consume_token (parser
->lexer
);
28484 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28485 /* Handle function try blocks. */
28486 if (function_try_block
)
28487 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
28488 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28489 last
= parser
->lexer
->next_token
;
28491 /* Save away the inline definition; we will process it when the
28492 class is complete. */
28493 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
28494 DECL_PENDING_INLINE_P (fn
) = 1;
28496 /* We need to know that this was defined in the class, so that
28497 friend templates are handled correctly. */
28498 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
28500 /* Add FN to the queue of functions to be parsed later. */
28501 vec_safe_push (unparsed_funs_with_definitions
, fn
);
28506 /* Save the tokens that make up the in-class initializer for a non-static
28507 data member. Returns a DEFAULT_ARG. */
28510 cp_parser_save_nsdmi (cp_parser
* parser
)
28512 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
28515 /* Parse a template-argument-list, as well as the trailing ">" (but
28516 not the opening "<"). See cp_parser_template_argument_list for the
28520 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
28524 tree saved_qualifying_scope
;
28525 tree saved_object_scope
;
28526 bool saved_greater_than_is_operator_p
;
28530 When parsing a template-id, the first non-nested `>' is taken as
28531 the end of the template-argument-list rather than a greater-than
28533 saved_greater_than_is_operator_p
28534 = parser
->greater_than_is_operator_p
;
28535 parser
->greater_than_is_operator_p
= false;
28536 /* Parsing the argument list may modify SCOPE, so we save it
28538 saved_scope
= parser
->scope
;
28539 saved_qualifying_scope
= parser
->qualifying_scope
;
28540 saved_object_scope
= parser
->object_scope
;
28541 /* We need to evaluate the template arguments, even though this
28542 template-id may be nested within a "sizeof". */
28544 /* Parse the template-argument-list itself. */
28545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
28546 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28547 arguments
= NULL_TREE
;
28549 arguments
= cp_parser_template_argument_list (parser
);
28550 /* Look for the `>' that ends the template-argument-list. If we find
28551 a '>>' instead, it's probably just a typo. */
28552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
28554 if (cxx_dialect
!= cxx98
)
28556 /* In C++0x, a `>>' in a template argument list or cast
28557 expression is considered to be two separate `>'
28558 tokens. So, change the current token to a `>', but don't
28559 consume it: it will be consumed later when the outer
28560 template argument list (or cast expression) is parsed.
28561 Note that this replacement of `>' for `>>' is necessary
28562 even if we are parsing tentatively: in the tentative
28563 case, after calling
28564 cp_parser_enclosed_template_argument_list we will always
28565 throw away all of the template arguments and the first
28566 closing `>', either because the template argument list
28567 was erroneous or because we are replacing those tokens
28568 with a CPP_TEMPLATE_ID token. The second `>' (which will
28569 not have been thrown away) is needed either to close an
28570 outer template argument list or to complete a new-style
28572 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28573 token
->type
= CPP_GREATER
;
28575 else if (!saved_greater_than_is_operator_p
)
28577 /* If we're in a nested template argument list, the '>>' has
28578 to be a typo for '> >'. We emit the error message, but we
28579 continue parsing and we push a '>' as next token, so that
28580 the argument list will be parsed correctly. Note that the
28581 global source location is still on the token before the
28582 '>>', so we need to say explicitly where we want it. */
28583 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28584 gcc_rich_location
richloc (token
->location
);
28585 richloc
.add_fixit_replace ("> >");
28586 error_at (&richloc
, "%<>>%> should be %<> >%> "
28587 "within a nested template argument list");
28589 token
->type
= CPP_GREATER
;
28593 /* If this is not a nested template argument list, the '>>'
28594 is a typo for '>'. Emit an error message and continue.
28595 Same deal about the token location, but here we can get it
28596 right by consuming the '>>' before issuing the diagnostic. */
28597 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28598 error_at (token
->location
,
28599 "spurious %<>>%>, use %<>%> to terminate "
28600 "a template argument list");
28604 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28605 /* The `>' token might be a greater-than operator again now. */
28606 parser
->greater_than_is_operator_p
28607 = saved_greater_than_is_operator_p
;
28608 /* Restore the SAVED_SCOPE. */
28609 parser
->scope
= saved_scope
;
28610 parser
->qualifying_scope
= saved_qualifying_scope
;
28611 parser
->object_scope
= saved_object_scope
;
28616 /* MEMBER_FUNCTION is a member function, or a friend. If default
28617 arguments, or the body of the function have not yet been parsed,
28621 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
28623 timevar_push (TV_PARSE_INMETH
);
28624 /* If this member is a template, get the underlying
28626 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
28627 member_function
= DECL_TEMPLATE_RESULT (member_function
);
28629 /* There should not be any class definitions in progress at this
28630 point; the bodies of members are only parsed outside of all class
28632 gcc_assert (parser
->num_classes_being_defined
== 0);
28633 /* While we're parsing the member functions we might encounter more
28634 classes. We want to handle them right away, but we don't want
28635 them getting mixed up with functions that are currently in the
28637 push_unparsed_function_queues (parser
);
28639 /* Make sure that any template parameters are in scope. */
28640 maybe_begin_member_template_processing (member_function
);
28642 /* If the body of the function has not yet been parsed, parse it
28644 if (DECL_PENDING_INLINE_P (member_function
))
28646 tree function_scope
;
28647 cp_token_cache
*tokens
;
28649 /* The function is no longer pending; we are processing it. */
28650 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
28651 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
28652 DECL_PENDING_INLINE_P (member_function
) = 0;
28654 /* If this is a local class, enter the scope of the containing
28656 function_scope
= current_function_decl
;
28657 if (function_scope
)
28658 push_function_context ();
28660 /* Push the body of the function onto the lexer stack. */
28661 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28663 /* Let the front end know that we going to be defining this
28665 start_preparsed_function (member_function
, NULL_TREE
,
28666 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
28668 /* Don't do access checking if it is a templated function. */
28669 if (processing_template_decl
)
28670 push_deferring_access_checks (dk_no_check
);
28672 /* #pragma omp declare reduction needs special parsing. */
28673 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
28675 parser
->lexer
->in_pragma
= true;
28676 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
28677 finish_function (/*inline_p=*/true);
28678 cp_check_omp_declare_reduction (member_function
);
28681 /* Now, parse the body of the function. */
28682 cp_parser_function_definition_after_declarator (parser
,
28683 /*inline_p=*/true);
28685 if (processing_template_decl
)
28686 pop_deferring_access_checks ();
28688 /* Leave the scope of the containing function. */
28689 if (function_scope
)
28690 pop_function_context ();
28691 cp_parser_pop_lexer (parser
);
28694 /* Remove any template parameters from the symbol table. */
28695 maybe_end_member_template_processing ();
28697 /* Restore the queue. */
28698 pop_unparsed_function_queues (parser
);
28699 timevar_pop (TV_PARSE_INMETH
);
28702 /* If DECL contains any default args, remember it on the unparsed
28703 functions queue. */
28706 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
28710 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
28712 probe
= TREE_CHAIN (probe
))
28713 if (TREE_PURPOSE (probe
))
28715 cp_default_arg_entry entry
= {current_class_type
, decl
};
28716 vec_safe_push (unparsed_funs_with_default_args
, entry
);
28721 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28722 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28723 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28724 from the parameter-type-list. */
28727 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
28728 tree default_arg
, tree parmtype
)
28730 cp_token_cache
*tokens
;
28734 if (default_arg
== error_mark_node
)
28735 return error_mark_node
;
28737 /* Push the saved tokens for the default argument onto the parser's
28739 tokens
= DEFARG_TOKENS (default_arg
);
28740 cp_parser_push_lexer_for_tokens (parser
, tokens
);
28742 start_lambda_scope (decl
);
28744 /* Parse the default argument. */
28745 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
28746 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
28747 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
28749 finish_lambda_scope ();
28751 if (parsed_arg
== error_mark_node
)
28752 cp_parser_skip_to_end_of_statement (parser
);
28754 if (!processing_template_decl
)
28756 /* In a non-template class, check conversions now. In a template,
28757 we'll wait and instantiate these as needed. */
28758 if (TREE_CODE (decl
) == PARM_DECL
)
28759 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
28760 tf_warning_or_error
);
28761 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
28762 parsed_arg
= error_mark_node
;
28764 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
28767 /* If the token stream has not been completely used up, then
28768 there was extra junk after the end of the default
28770 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
28772 if (TREE_CODE (decl
) == PARM_DECL
)
28773 cp_parser_error (parser
, "expected %<,%>");
28775 cp_parser_error (parser
, "expected %<;%>");
28778 /* Revert to the main lexer. */
28779 cp_parser_pop_lexer (parser
);
28784 /* FIELD is a non-static data member with an initializer which we saved for
28785 later; parse it now. */
28788 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
28792 maybe_begin_member_template_processing (field
);
28794 push_unparsed_function_queues (parser
);
28795 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
28796 DECL_INITIAL (field
),
28798 pop_unparsed_function_queues (parser
);
28800 maybe_end_member_template_processing ();
28802 DECL_INITIAL (field
) = def
;
28805 /* FN is a FUNCTION_DECL which may contains a parameter with an
28806 unparsed DEFAULT_ARG. Parse the default args now. This function
28807 assumes that the current scope is the scope in which the default
28808 argument should be processed. */
28811 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
28813 unsigned char saved_local_variables_forbidden_p
;
28814 tree parm
, parmdecl
;
28816 /* While we're parsing the default args, we might (due to the
28817 statement expression extension) encounter more classes. We want
28818 to handle them right away, but we don't want them getting mixed
28819 up with default args that are currently in the queue. */
28820 push_unparsed_function_queues (parser
);
28822 /* Local variable names (and the `this' keyword) may not appear
28823 in a default argument. */
28824 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28825 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
28827 push_defarg_context (fn
);
28829 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28830 parmdecl
= DECL_ARGUMENTS (fn
);
28831 parm
&& parm
!= void_list_node
;
28832 parm
= TREE_CHAIN (parm
),
28833 parmdecl
= DECL_CHAIN (parmdecl
))
28835 tree default_arg
= TREE_PURPOSE (parm
);
28837 vec
<tree
, va_gc
> *insts
;
28844 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28845 /* This can happen for a friend declaration for a function
28846 already declared with default arguments. */
28850 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28852 TREE_VALUE (parm
));
28853 TREE_PURPOSE (parm
) = parsed_arg
;
28855 /* Update any instantiations we've already created. */
28856 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28857 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28858 TREE_PURPOSE (copy
) = parsed_arg
;
28861 pop_defarg_context ();
28863 /* Make sure no default arg is missing. */
28864 check_default_args (fn
);
28866 /* Restore the state of local_variables_forbidden_p. */
28867 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28869 /* Restore the queue. */
28870 pop_unparsed_function_queues (parser
);
28873 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28875 sizeof ... ( identifier )
28877 where the 'sizeof' token has already been consumed. */
28880 cp_parser_sizeof_pack (cp_parser
*parser
)
28882 /* Consume the `...'. */
28883 cp_lexer_consume_token (parser
->lexer
);
28884 maybe_warn_variadic_templates ();
28886 matching_parens parens
;
28887 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28889 parens
.consume_open (parser
);
28891 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28892 "%<sizeof...%> argument must be surrounded by parentheses");
28894 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28895 tree name
= cp_parser_identifier (parser
);
28896 if (name
== error_mark_node
)
28897 return error_mark_node
;
28898 /* The name is not qualified. */
28899 parser
->scope
= NULL_TREE
;
28900 parser
->qualifying_scope
= NULL_TREE
;
28901 parser
->object_scope
= NULL_TREE
;
28902 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28903 if (expr
== error_mark_node
)
28904 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28906 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28907 expr
= TREE_TYPE (expr
);
28908 else if (TREE_CODE (expr
) == CONST_DECL
)
28909 expr
= DECL_INITIAL (expr
);
28910 expr
= make_pack_expansion (expr
);
28911 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28914 parens
.require_close (parser
);
28919 /* Parse the operand of `sizeof' (or a similar operator). Returns
28920 either a TYPE or an expression, depending on the form of the
28921 input. The KEYWORD indicates which kind of expression we have
28925 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28927 tree expr
= NULL_TREE
;
28928 const char *saved_message
;
28930 bool saved_integral_constant_expression_p
;
28931 bool saved_non_integral_constant_expression_p
;
28933 /* If it's a `...', then we are computing the length of a parameter
28935 if (keyword
== RID_SIZEOF
28936 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28937 return cp_parser_sizeof_pack (parser
);
28939 /* Types cannot be defined in a `sizeof' expression. Save away the
28941 saved_message
= parser
->type_definition_forbidden_message
;
28942 /* And create the new one. */
28943 tmp
= concat ("types may not be defined in %<",
28944 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28945 "%> expressions", NULL
);
28946 parser
->type_definition_forbidden_message
= tmp
;
28948 /* The restrictions on constant-expressions do not apply inside
28949 sizeof expressions. */
28950 saved_integral_constant_expression_p
28951 = parser
->integral_constant_expression_p
;
28952 saved_non_integral_constant_expression_p
28953 = parser
->non_integral_constant_expression_p
;
28954 parser
->integral_constant_expression_p
= false;
28956 /* Do not actually evaluate the expression. */
28957 ++cp_unevaluated_operand
;
28958 ++c_inhibit_evaluation_warnings
;
28959 /* If it's a `(', then we might be looking at the type-id
28961 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28963 tree type
= NULL_TREE
;
28965 /* We can't be sure yet whether we're looking at a type-id or an
28967 cp_parser_parse_tentatively (parser
);
28969 matching_parens parens
;
28970 parens
.consume_open (parser
);
28972 /* Note: as a GNU Extension, compound literals are considered
28973 postfix-expressions as they are in C99, so they are valid
28974 arguments to sizeof. See comment in cp_parser_cast_expression
28976 if (cp_parser_compound_literal_p (parser
))
28977 cp_parser_simulate_error (parser
);
28980 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28981 parser
->in_type_id_in_expr_p
= true;
28982 /* Look for the type-id. */
28983 type
= cp_parser_type_id (parser
);
28984 /* Look for the closing `)'. */
28985 parens
.require_close (parser
);
28986 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28989 /* If all went well, then we're done. */
28990 if (cp_parser_parse_definitely (parser
))
28994 /* If the type-id production did not work out, then we must be
28995 looking at the unary-expression production. */
28997 expr
= cp_parser_unary_expression (parser
);
28999 /* Go back to evaluating expressions. */
29000 --cp_unevaluated_operand
;
29001 --c_inhibit_evaluation_warnings
;
29003 /* Free the message we created. */
29005 /* And restore the old one. */
29006 parser
->type_definition_forbidden_message
= saved_message
;
29007 parser
->integral_constant_expression_p
29008 = saved_integral_constant_expression_p
;
29009 parser
->non_integral_constant_expression_p
29010 = saved_non_integral_constant_expression_p
;
29015 /* If the current declaration has no declarator, return true. */
29018 cp_parser_declares_only_class_p (cp_parser
*parser
)
29020 /* If the next token is a `;' or a `,' then there is no
29022 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29023 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
29026 /* Update the DECL_SPECS to reflect the storage class indicated by
29030 cp_parser_set_storage_class (cp_parser
*parser
,
29031 cp_decl_specifier_seq
*decl_specs
,
29035 cp_storage_class storage_class
;
29037 if (parser
->in_unbraced_linkage_specification_p
)
29039 error_at (token
->location
, "invalid use of %qD in linkage specification",
29040 ridpointers
[keyword
]);
29043 else if (decl_specs
->storage_class
!= sc_none
)
29045 decl_specs
->conflicting_specifiers_p
= true;
29049 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
29050 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
29051 && decl_specs
->gnu_thread_keyword_p
)
29053 pedwarn (decl_specs
->locations
[ds_thread
], 0,
29054 "%<__thread%> before %qD", ridpointers
[keyword
]);
29060 storage_class
= sc_auto
;
29063 storage_class
= sc_register
;
29066 storage_class
= sc_static
;
29069 storage_class
= sc_extern
;
29072 storage_class
= sc_mutable
;
29075 gcc_unreachable ();
29077 decl_specs
->storage_class
= storage_class
;
29078 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
29080 /* A storage class specifier cannot be applied alongside a typedef
29081 specifier. If there is a typedef specifier present then set
29082 conflicting_specifiers_p which will trigger an error later
29083 on in grokdeclarator. */
29084 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
29085 decl_specs
->conflicting_specifiers_p
= true;
29088 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29089 is true, the type is a class or enum definition. */
29092 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
29095 bool type_definition_p
)
29097 decl_specs
->any_specifiers_p
= true;
29099 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29100 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29101 this is what happened. In system headers, we ignore these
29102 declarations so that G++ can work with system headers that are not
29104 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
29105 && !type_definition_p
29106 && (type_spec
== boolean_type_node
29107 || type_spec
== char8_type_node
29108 || type_spec
== char16_type_node
29109 || type_spec
== char32_type_node
29110 || type_spec
== wchar_type_node
)
29111 && (decl_specs
->type
29112 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
29113 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
29114 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
29115 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
29117 decl_specs
->redefined_builtin_type
= type_spec
;
29118 set_and_check_decl_spec_loc (decl_specs
,
29119 ds_redefined_builtin_type_spec
,
29121 if (!decl_specs
->type
)
29123 decl_specs
->type
= type_spec
;
29124 decl_specs
->type_definition_p
= false;
29125 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
29128 else if (decl_specs
->type
)
29129 decl_specs
->multiple_types_p
= true;
29132 decl_specs
->type
= type_spec
;
29133 decl_specs
->type_definition_p
= type_definition_p
;
29134 decl_specs
->redefined_builtin_type
= NULL_TREE
;
29135 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
29139 /* True iff TOKEN is the GNU keyword __thread. */
29142 token_is__thread (cp_token
*token
)
29144 gcc_assert (token
->keyword
== RID_THREAD
);
29145 return id_equal (token
->u
.value
, "__thread");
29148 /* Set the location for a declarator specifier and check if it is
29151 DECL_SPECS is the sequence of declarator specifiers onto which to
29154 DS is the single declarator specifier to set which location is to
29155 be set onto the existing sequence of declarators.
29157 LOCATION is the location for the declarator specifier to
29161 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
29162 cp_decl_spec ds
, cp_token
*token
)
29164 gcc_assert (ds
< ds_last
);
29166 if (decl_specs
== NULL
)
29169 location_t location
= token
->location
;
29171 if (decl_specs
->locations
[ds
] == 0)
29173 decl_specs
->locations
[ds
] = location
;
29174 if (ds
== ds_thread
)
29175 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
29181 if (decl_specs
->locations
[ds_long_long
] != 0)
29182 error_at (location
,
29183 "%<long long long%> is too long for GCC");
29186 decl_specs
->locations
[ds_long_long
] = location
;
29187 pedwarn_cxx98 (location
,
29189 "ISO C++ 1998 does not support %<long long%>");
29192 else if (ds
== ds_thread
)
29194 bool gnu
= token_is__thread (token
);
29195 gcc_rich_location
richloc (location
);
29196 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
29198 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
29199 error_at (&richloc
,
29200 "both %<__thread%> and %<thread_local%> specified");
29204 richloc
.add_fixit_remove ();
29205 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
29210 static const char *const decl_spec_names
[] = {
29227 gcc_rich_location
richloc (location
);
29228 richloc
.add_fixit_remove ();
29229 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
29234 /* Return true iff the declarator specifier DS is present in the
29235 sequence of declarator specifiers DECL_SPECS. */
29238 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
29241 gcc_assert (ds
< ds_last
);
29243 if (decl_specs
== NULL
)
29246 return decl_specs
->locations
[ds
] != 0;
29249 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29250 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29253 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
29255 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
29258 /* Issue an error message indicating that TOKEN_DESC was expected.
29259 If KEYWORD is true, it indicated this function is called by
29260 cp_parser_require_keword and the required token can only be
29261 a indicated keyword.
29263 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29264 within any error as the location of an "opening" token matching
29265 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29266 RT_CLOSE_PAREN). */
29269 cp_parser_required_error (cp_parser
*parser
,
29270 required_token token_desc
,
29272 location_t matching_location
)
29274 if (cp_parser_simulate_error (parser
))
29277 const char *gmsgid
= NULL
;
29278 switch (token_desc
)
29281 gmsgid
= G_("expected %<new%>");
29284 gmsgid
= G_("expected %<delete%>");
29287 gmsgid
= G_("expected %<return%>");
29290 gmsgid
= G_("expected %<while%>");
29293 gmsgid
= G_("expected %<extern%>");
29295 case RT_STATIC_ASSERT
:
29296 gmsgid
= G_("expected %<static_assert%>");
29299 gmsgid
= G_("expected %<decltype%>");
29302 gmsgid
= G_("expected %<operator%>");
29305 gmsgid
= G_("expected %<class%>");
29308 gmsgid
= G_("expected %<template%>");
29311 gmsgid
= G_("expected %<namespace%>");
29314 gmsgid
= G_("expected %<using%>");
29317 gmsgid
= G_("expected %<asm%>");
29320 gmsgid
= G_("expected %<try%>");
29323 gmsgid
= G_("expected %<catch%>");
29326 gmsgid
= G_("expected %<throw%>");
29329 gmsgid
= G_("expected %<__label__%>");
29332 gmsgid
= G_("expected %<@try%>");
29334 case RT_AT_SYNCHRONIZED
:
29335 gmsgid
= G_("expected %<@synchronized%>");
29338 gmsgid
= G_("expected %<@throw%>");
29340 case RT_TRANSACTION_ATOMIC
:
29341 gmsgid
= G_("expected %<__transaction_atomic%>");
29343 case RT_TRANSACTION_RELAXED
:
29344 gmsgid
= G_("expected %<__transaction_relaxed%>");
29350 if (!gmsgid
&& !keyword
)
29352 switch (token_desc
)
29355 gmsgid
= G_("expected %<;%>");
29357 case RT_OPEN_PAREN
:
29358 gmsgid
= G_("expected %<(%>");
29360 case RT_CLOSE_BRACE
:
29361 gmsgid
= G_("expected %<}%>");
29363 case RT_OPEN_BRACE
:
29364 gmsgid
= G_("expected %<{%>");
29366 case RT_CLOSE_SQUARE
:
29367 gmsgid
= G_("expected %<]%>");
29369 case RT_OPEN_SQUARE
:
29370 gmsgid
= G_("expected %<[%>");
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 %<:%>");
29399 case RT_COLON_SCOPE
:
29400 gmsgid
= G_("expected %<:%> or %<::%>");
29402 case RT_CLOSE_PAREN
:
29403 gmsgid
= G_("expected %<)%>");
29405 case RT_COMMA_CLOSE_PAREN
:
29406 gmsgid
= G_("expected %<,%> or %<)%>");
29408 case RT_PRAGMA_EOL
:
29409 gmsgid
= G_("expected end of line");
29412 gmsgid
= G_("expected identifier");
29415 gmsgid
= G_("expected selection-statement");
29418 gmsgid
= G_("expected iteration-statement");
29421 gmsgid
= G_("expected jump-statement");
29424 gmsgid
= G_("expected class-key");
29426 case RT_CLASS_TYPENAME_TEMPLATE
:
29427 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
29430 gcc_unreachable ();
29435 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
29439 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29440 issue an error message indicating that TOKEN_DESC was expected.
29442 Returns the token consumed, if the token had the appropriate type.
29443 Otherwise, returns NULL.
29445 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29446 within any error as the location of an "opening" token matching
29447 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29448 RT_CLOSE_PAREN). */
29451 cp_parser_require (cp_parser
* parser
,
29452 enum cpp_ttype type
,
29453 required_token token_desc
,
29454 location_t matching_location
)
29456 if (cp_lexer_next_token_is (parser
->lexer
, type
))
29457 return cp_lexer_consume_token (parser
->lexer
);
29460 /* Output the MESSAGE -- unless we're parsing tentatively. */
29461 if (!cp_parser_simulate_error (parser
))
29462 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
29463 matching_location
);
29468 /* An error message is produced if the next token is not '>'.
29469 All further tokens are skipped until the desired token is
29470 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29473 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
29475 /* Current level of '< ... >'. */
29476 unsigned level
= 0;
29477 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29478 unsigned nesting_depth
= 0;
29480 /* Are we ready, yet? If not, issue error message. */
29481 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
29484 /* Skip tokens until the desired token is found. */
29487 /* Peek at the next token. */
29488 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29491 if (!nesting_depth
)
29496 if (cxx_dialect
== cxx98
)
29497 /* C++0x views the `>>' operator as two `>' tokens, but
29500 else if (!nesting_depth
&& level
-- == 0)
29502 /* We've hit a `>>' where the first `>' closes the
29503 template argument list, and the second `>' is
29504 spurious. Just consume the `>>' and stop; we've
29505 already produced at least one error. */
29506 cp_lexer_consume_token (parser
->lexer
);
29509 /* Fall through for C++0x, so we handle the second `>' in
29511 gcc_fallthrough ();
29514 if (!nesting_depth
&& level
-- == 0)
29516 /* We've reached the token we want, consume it and stop. */
29517 cp_lexer_consume_token (parser
->lexer
);
29522 case CPP_OPEN_PAREN
:
29523 case CPP_OPEN_SQUARE
:
29527 case CPP_CLOSE_PAREN
:
29528 case CPP_CLOSE_SQUARE
:
29529 if (nesting_depth
-- == 0)
29534 case CPP_PRAGMA_EOL
:
29535 case CPP_SEMICOLON
:
29536 case CPP_OPEN_BRACE
:
29537 case CPP_CLOSE_BRACE
:
29538 /* The '>' was probably forgotten, don't look further. */
29545 /* Consume this token. */
29546 cp_lexer_consume_token (parser
->lexer
);
29550 /* If the next token is the indicated keyword, consume it. Otherwise,
29551 issue an error message indicating that TOKEN_DESC was expected.
29553 Returns the token consumed, if the token had the appropriate type.
29554 Otherwise, returns NULL. */
29557 cp_parser_require_keyword (cp_parser
* parser
,
29559 required_token token_desc
)
29561 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
29563 if (token
&& token
->keyword
!= keyword
)
29565 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
29573 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29574 function-definition. */
29577 cp_parser_token_starts_function_definition_p (cp_token
* token
)
29579 return (/* An ordinary function-body begins with an `{'. */
29580 token
->type
== CPP_OPEN_BRACE
29581 /* A ctor-initializer begins with a `:'. */
29582 || token
->type
== CPP_COLON
29583 /* A function-try-block begins with `try'. */
29584 || token
->keyword
== RID_TRY
29585 /* A function-transaction-block begins with `__transaction_atomic'
29586 or `__transaction_relaxed'. */
29587 || token
->keyword
== RID_TRANSACTION_ATOMIC
29588 || token
->keyword
== RID_TRANSACTION_RELAXED
29589 /* The named return value extension begins with `return'. */
29590 || token
->keyword
== RID_RETURN
);
29593 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29597 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
29601 token
= cp_lexer_peek_token (parser
->lexer
);
29602 return (token
->type
== CPP_OPEN_BRACE
29603 || (token
->type
== CPP_COLON
29604 && !parser
->colon_doesnt_start_class_def_p
));
29607 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29608 C++0x) ending a template-argument. */
29611 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
29615 token
= cp_lexer_peek_token (parser
->lexer
);
29616 return (token
->type
== CPP_COMMA
29617 || token
->type
== CPP_GREATER
29618 || token
->type
== CPP_ELLIPSIS
29619 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
29622 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29623 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29626 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
29631 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
29632 if (token
->type
== CPP_LESS
)
29634 /* Check for the sequence `<::' in the original code. It would be lexed as
29635 `[:', where `[' is a digraph, and there is no whitespace before
29637 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
29640 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
29641 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
29647 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29648 or none_type otherwise. */
29650 static enum tag_types
29651 cp_parser_token_is_class_key (cp_token
* token
)
29653 switch (token
->keyword
)
29658 return record_type
;
29667 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29668 or none_type otherwise or if the token is null. */
29670 static enum tag_types
29671 cp_parser_token_is_type_parameter_key (cp_token
* token
)
29676 switch (token
->keyword
)
29681 return typename_type
;
29688 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29691 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
29693 if (type
== error_mark_node
)
29695 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
29697 if (permerror (input_location
, "%qs tag used in naming %q#T",
29698 class_key
== union_type
? "union"
29699 : class_key
== record_type
? "struct" : "class",
29701 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
29702 "%q#T was previously declared here", type
);
29706 /* Issue an error message if DECL is redeclared with different
29707 access than its original declaration [class.access.spec/3].
29708 This applies to nested classes, nested class templates and
29709 enumerations [class.mem/1]. */
29712 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
29715 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
29716 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
29719 if ((TREE_PRIVATE (decl
)
29720 != (current_access_specifier
== access_private_node
))
29721 || (TREE_PROTECTED (decl
)
29722 != (current_access_specifier
== access_protected_node
)))
29723 error_at (location
, "%qD redeclared with different access", decl
);
29726 /* Look for the `template' keyword, as a syntactic disambiguator.
29727 Return TRUE iff it is present, in which case it will be
29731 cp_parser_optional_template_keyword (cp_parser
*parser
)
29733 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29735 /* In C++98 the `template' keyword can only be used within templates;
29736 outside templates the parser can always figure out what is a
29737 template and what is not. In C++11, per the resolution of DR 468,
29738 `template' is allowed in cases where it is not strictly necessary. */
29739 if (!processing_template_decl
29740 && pedantic
&& cxx_dialect
== cxx98
)
29742 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29743 pedwarn (token
->location
, OPT_Wpedantic
,
29744 "in C++98 %<template%> (as a disambiguator) is only "
29745 "allowed within templates");
29746 /* If this part of the token stream is rescanned, the same
29747 error message would be generated. So, we purge the token
29748 from the stream. */
29749 cp_lexer_purge_token (parser
->lexer
);
29754 /* Consume the `template' keyword. */
29755 cp_lexer_consume_token (parser
->lexer
);
29762 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29763 set PARSER->SCOPE, and perform other related actions. */
29766 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
29768 struct tree_check
*check_value
;
29770 /* Get the stored value. */
29771 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
29772 /* Set the scope from the stored value. */
29773 parser
->scope
= saved_checks_value (check_value
);
29774 parser
->qualifying_scope
= check_value
->qualifying_scope
;
29775 parser
->object_scope
= NULL_TREE
;
29778 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29779 encounter the end of a block before what we were looking for. */
29782 cp_parser_cache_group (cp_parser
*parser
,
29783 enum cpp_ttype end
,
29788 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29790 /* Abort a parenthesized expression if we encounter a semicolon. */
29791 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
29792 && token
->type
== CPP_SEMICOLON
)
29794 /* If we've reached the end of the file, stop. */
29795 if (token
->type
== CPP_EOF
29796 || (end
!= CPP_PRAGMA_EOL
29797 && token
->type
== CPP_PRAGMA_EOL
))
29799 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
29800 /* We've hit the end of an enclosing block, so there's been some
29801 kind of syntax error. */
29804 /* Consume the token. */
29805 cp_lexer_consume_token (parser
->lexer
);
29806 /* See if it starts a new group. */
29807 if (token
->type
== CPP_OPEN_BRACE
)
29809 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29810 /* In theory this should probably check end == '}', but
29811 cp_parser_save_member_function_body needs it to exit
29812 after either '}' or ')' when called with ')'. */
29816 else if (token
->type
== CPP_OPEN_PAREN
)
29818 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29819 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29822 else if (token
->type
== CPP_PRAGMA
)
29823 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29824 else if (token
->type
== end
)
29829 /* Like above, for caching a default argument or NSDMI. Both of these are
29830 terminated by a non-nested comma, but it can be unclear whether or not a
29831 comma is nested in a template argument list unless we do more parsing.
29832 In order to handle this ambiguity, when we encounter a ',' after a '<'
29833 we try to parse what follows as a parameter-declaration-list (in the
29834 case of a default argument) or a member-declarator (in the case of an
29835 NSDMI). If that succeeds, then we stop caching. */
29838 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29840 unsigned depth
= 0;
29841 int maybe_template_id
= 0;
29842 cp_token
*first_token
;
29844 tree default_argument
;
29846 /* Add tokens until we have processed the entire default
29847 argument. We add the range [first_token, token). */
29848 first_token
= cp_lexer_peek_token (parser
->lexer
);
29849 if (first_token
->type
== CPP_OPEN_BRACE
)
29851 /* For list-initialization, this is straightforward. */
29852 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29853 token
= cp_lexer_peek_token (parser
->lexer
);
29859 /* Peek at the next token. */
29860 token
= cp_lexer_peek_token (parser
->lexer
);
29861 /* What we do depends on what token we have. */
29862 switch (token
->type
)
29864 /* In valid code, a default argument must be
29865 immediately followed by a `,' `)', or `...'. */
29867 if (depth
== 0 && maybe_template_id
)
29869 /* If we've seen a '<', we might be in a
29870 template-argument-list. Until Core issue 325 is
29871 resolved, we don't know how this situation ought
29872 to be handled, so try to DTRT. We check whether
29873 what comes after the comma is a valid parameter
29874 declaration list. If it is, then the comma ends
29875 the default argument; otherwise the default
29876 argument continues. */
29877 bool error
= false;
29880 /* Set ITALP so cp_parser_parameter_declaration_list
29881 doesn't decide to commit to this parse. */
29882 bool saved_italp
= parser
->in_template_argument_list_p
;
29883 parser
->in_template_argument_list_p
= true;
29885 cp_parser_parse_tentatively (parser
);
29889 /* Parse declarators until we reach a non-comma or
29890 somthing that cannot be an initializer.
29891 Just checking whether we're looking at a single
29892 declarator is insufficient. Consider:
29893 int var = tuple<T,U>::x;
29894 The template parameter 'U' looks exactly like a
29898 int ctor_dtor_or_conv_p
;
29899 cp_lexer_consume_token (parser
->lexer
);
29900 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29901 CP_PARSER_FLAGS_NONE
,
29902 &ctor_dtor_or_conv_p
,
29903 /*parenthesized_p=*/NULL
,
29905 /*friend_p=*/false,
29906 /*static_p=*/false);
29907 peek
= cp_lexer_peek_token (parser
->lexer
);
29908 if (cp_parser_error_occurred (parser
))
29911 while (peek
->type
== CPP_COMMA
);
29912 /* If we met an '=' or ';' then the original comma
29913 was the end of the NSDMI. Otherwise assume
29914 we're still in the NSDMI. */
29915 error
= (peek
->type
!= CPP_EQ
29916 && peek
->type
!= CPP_SEMICOLON
);
29920 cp_lexer_consume_token (parser
->lexer
);
29921 begin_scope (sk_function_parms
, NULL_TREE
);
29922 tree t
= cp_parser_parameter_declaration_list
29923 (parser
, CP_PARSER_FLAGS_NONE
);
29924 if (t
== error_mark_node
)
29926 pop_bindings_and_leave_scope ();
29928 if (!cp_parser_error_occurred (parser
) && !error
)
29930 cp_parser_abort_tentative_parse (parser
);
29932 parser
->in_template_argument_list_p
= saved_italp
;
29936 case CPP_CLOSE_PAREN
:
29938 /* If we run into a non-nested `;', `}', or `]',
29939 then the code is invalid -- but the default
29940 argument is certainly over. */
29941 case CPP_SEMICOLON
:
29942 case CPP_CLOSE_BRACE
:
29943 case CPP_CLOSE_SQUARE
:
29945 /* Handle correctly int n = sizeof ... ( p ); */
29946 && token
->type
!= CPP_ELLIPSIS
)
29948 /* Update DEPTH, if necessary. */
29949 else if (token
->type
== CPP_CLOSE_PAREN
29950 || token
->type
== CPP_CLOSE_BRACE
29951 || token
->type
== CPP_CLOSE_SQUARE
)
29955 case CPP_OPEN_PAREN
:
29956 case CPP_OPEN_SQUARE
:
29957 case CPP_OPEN_BRACE
:
29963 /* This might be the comparison operator, or it might
29964 start a template argument list. */
29965 ++maybe_template_id
;
29969 if (cxx_dialect
== cxx98
)
29971 /* Fall through for C++0x, which treats the `>>'
29972 operator like two `>' tokens in certain
29974 gcc_fallthrough ();
29979 /* This might be an operator, or it might close a
29980 template argument list. But if a previous '<'
29981 started a template argument list, this will have
29982 closed it, so we can't be in one anymore. */
29983 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29984 if (maybe_template_id
< 0)
29985 maybe_template_id
= 0;
29989 /* If we run out of tokens, issue an error message. */
29991 case CPP_PRAGMA_EOL
:
29992 error_at (token
->location
, "file ends in default argument");
29993 return error_mark_node
;
29997 /* In these cases, we should look for template-ids.
29998 For example, if the default argument is
29999 `X<int, double>()', we need to do name lookup to
30000 figure out whether or not `X' is a template; if
30001 so, the `,' does not end the default argument.
30003 That is not yet done. */
30010 /* If we've reached the end, stop. */
30014 /* Add the token to the token block. */
30015 token
= cp_lexer_consume_token (parser
->lexer
);
30018 /* Create a DEFAULT_ARG to represent the unparsed default
30020 default_argument
= make_node (DEFAULT_ARG
);
30021 DEFARG_TOKENS (default_argument
)
30022 = cp_token_cache_new (first_token
, token
);
30023 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
30025 return default_argument
;
30028 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
30031 defarg_location (tree default_argument
)
30033 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
30034 location_t start
= tokens
->first
->location
;
30035 location_t end
= tokens
->last
->location
;
30036 return make_location (start
, start
, end
);
30039 /* Begin parsing tentatively. We always save tokens while parsing
30040 tentatively so that if the tentative parsing fails we can restore the
30044 cp_parser_parse_tentatively (cp_parser
* parser
)
30046 /* Enter a new parsing context. */
30047 parser
->context
= cp_parser_context_new (parser
->context
);
30048 /* Begin saving tokens. */
30049 cp_lexer_save_tokens (parser
->lexer
);
30050 /* In order to avoid repetitive access control error messages,
30051 access checks are queued up until we are no longer parsing
30053 push_deferring_access_checks (dk_deferred
);
30056 /* Commit to the currently active tentative parse. */
30059 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
30061 cp_parser_context
*context
;
30064 /* Mark all of the levels as committed. */
30065 lexer
= parser
->lexer
;
30066 for (context
= parser
->context
; context
->next
; context
= context
->next
)
30068 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
30070 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
30071 while (!cp_lexer_saving_tokens (lexer
))
30072 lexer
= lexer
->next
;
30073 cp_lexer_commit_tokens (lexer
);
30077 /* Commit to the topmost currently active tentative parse.
30079 Note that this function shouldn't be called when there are
30080 irreversible side-effects while in a tentative state. For
30081 example, we shouldn't create a permanent entry in the symbol
30082 table, or issue an error message that might not apply if the
30083 tentative parse is aborted. */
30086 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
30088 cp_parser_context
*context
= parser
->context
;
30089 cp_lexer
*lexer
= parser
->lexer
;
30093 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
30095 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
30097 while (!cp_lexer_saving_tokens (lexer
))
30098 lexer
= lexer
->next
;
30099 cp_lexer_commit_tokens (lexer
);
30103 /* Abort the currently active tentative parse. All consumed tokens
30104 will be rolled back, and no diagnostics will be issued. */
30107 cp_parser_abort_tentative_parse (cp_parser
* parser
)
30109 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
30110 || errorcount
> 0);
30111 cp_parser_simulate_error (parser
);
30112 /* Now, pretend that we want to see if the construct was
30113 successfully parsed. */
30114 cp_parser_parse_definitely (parser
);
30117 /* Stop parsing tentatively. If a parse error has occurred, restore the
30118 token stream. Otherwise, commit to the tokens we have consumed.
30119 Returns true if no error occurred; false otherwise. */
30122 cp_parser_parse_definitely (cp_parser
* parser
)
30124 bool error_occurred
;
30125 cp_parser_context
*context
;
30127 /* Remember whether or not an error occurred, since we are about to
30128 destroy that information. */
30129 error_occurred
= cp_parser_error_occurred (parser
);
30130 /* Remove the topmost context from the stack. */
30131 context
= parser
->context
;
30132 parser
->context
= context
->next
;
30133 /* If no parse errors occurred, commit to the tentative parse. */
30134 if (!error_occurred
)
30136 /* Commit to the tokens read tentatively, unless that was
30138 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
30139 cp_lexer_commit_tokens (parser
->lexer
);
30141 pop_to_parent_deferring_access_checks ();
30143 /* Otherwise, if errors occurred, roll back our state so that things
30144 are just as they were before we began the tentative parse. */
30147 cp_lexer_rollback_tokens (parser
->lexer
);
30148 pop_deferring_access_checks ();
30150 /* Add the context to the front of the free list. */
30151 context
->next
= cp_parser_context_free_list
;
30152 cp_parser_context_free_list
= context
;
30154 return !error_occurred
;
30157 /* Returns true if we are parsing tentatively and are not committed to
30158 this tentative parse. */
30161 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
30163 return (cp_parser_parsing_tentatively (parser
)
30164 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
30167 /* Returns nonzero iff an error has occurred during the most recent
30168 tentative parse. */
30171 cp_parser_error_occurred (cp_parser
* parser
)
30173 return (cp_parser_parsing_tentatively (parser
)
30174 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
30177 /* Returns nonzero if GNU extensions are allowed. */
30180 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
30182 return parser
->allow_gnu_extensions_p
;
30185 /* Objective-C++ Productions */
30188 /* Parse an Objective-C expression, which feeds into a primary-expression
30192 objc-message-expression
30193 objc-string-literal
30194 objc-encode-expression
30195 objc-protocol-expression
30196 objc-selector-expression
30198 Returns a tree representation of the expression. */
30201 cp_parser_objc_expression (cp_parser
* parser
)
30203 /* Try to figure out what kind of declaration is present. */
30204 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30208 case CPP_OPEN_SQUARE
:
30209 return cp_parser_objc_message_expression (parser
);
30211 case CPP_OBJC_STRING
:
30212 kwd
= cp_lexer_consume_token (parser
->lexer
);
30213 return objc_build_string_object (kwd
->u
.value
);
30216 switch (kwd
->keyword
)
30218 case RID_AT_ENCODE
:
30219 return cp_parser_objc_encode_expression (parser
);
30221 case RID_AT_PROTOCOL
:
30222 return cp_parser_objc_protocol_expression (parser
);
30224 case RID_AT_SELECTOR
:
30225 return cp_parser_objc_selector_expression (parser
);
30232 error_at (kwd
->location
,
30233 "misplaced %<@%D%> Objective-C++ construct",
30235 cp_parser_skip_to_end_of_block_or_statement (parser
);
30238 return error_mark_node
;
30241 /* Parse an Objective-C message expression.
30243 objc-message-expression:
30244 [ objc-message-receiver objc-message-args ]
30246 Returns a representation of an Objective-C message. */
30249 cp_parser_objc_message_expression (cp_parser
* parser
)
30251 tree receiver
, messageargs
;
30253 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30254 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
30255 receiver
= cp_parser_objc_message_receiver (parser
);
30256 messageargs
= cp_parser_objc_message_args (parser
);
30257 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30258 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
30260 tree result
= objc_build_message_expr (receiver
, messageargs
);
30262 /* Construct a location e.g.
30265 ranging from the '[' to the ']', with the caret at the start. */
30266 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
30267 protected_set_expr_location (result
, combined_loc
);
30272 /* Parse an objc-message-receiver.
30274 objc-message-receiver:
30276 simple-type-specifier
30278 Returns a representation of the type or expression. */
30281 cp_parser_objc_message_receiver (cp_parser
* parser
)
30285 /* An Objective-C message receiver may be either (1) a type
30286 or (2) an expression. */
30287 cp_parser_parse_tentatively (parser
);
30288 rcv
= cp_parser_expression (parser
);
30290 /* If that worked out, fine. */
30291 if (cp_parser_parse_definitely (parser
))
30294 cp_parser_parse_tentatively (parser
);
30295 rcv
= cp_parser_simple_type_specifier (parser
,
30296 /*decl_specs=*/NULL
,
30297 CP_PARSER_FLAGS_NONE
);
30299 if (cp_parser_parse_definitely (parser
))
30300 return objc_get_class_reference (rcv
);
30302 cp_parser_error (parser
, "objective-c++ message receiver expected");
30303 return error_mark_node
;
30306 /* Parse the arguments and selectors comprising an Objective-C message.
30311 objc-selector-args , objc-comma-args
30313 objc-selector-args:
30314 objc-selector [opt] : assignment-expression
30315 objc-selector-args objc-selector [opt] : assignment-expression
30318 assignment-expression
30319 objc-comma-args , assignment-expression
30321 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30322 selector arguments and TREE_VALUE containing a list of comma
30326 cp_parser_objc_message_args (cp_parser
* parser
)
30328 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
30329 bool maybe_unary_selector_p
= true;
30330 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30332 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30334 tree selector
= NULL_TREE
, arg
;
30336 if (token
->type
!= CPP_COLON
)
30337 selector
= cp_parser_objc_selector (parser
);
30339 /* Detect if we have a unary selector. */
30340 if (maybe_unary_selector_p
30341 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30342 return build_tree_list (selector
, NULL_TREE
);
30344 maybe_unary_selector_p
= false;
30345 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30346 arg
= cp_parser_assignment_expression (parser
);
30349 = chainon (sel_args
,
30350 build_tree_list (selector
, arg
));
30352 token
= cp_lexer_peek_token (parser
->lexer
);
30355 /* Handle non-selector arguments, if any. */
30356 while (token
->type
== CPP_COMMA
)
30360 cp_lexer_consume_token (parser
->lexer
);
30361 arg
= cp_parser_assignment_expression (parser
);
30364 = chainon (addl_args
,
30365 build_tree_list (NULL_TREE
, arg
));
30367 token
= cp_lexer_peek_token (parser
->lexer
);
30370 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
30372 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
30373 return build_tree_list (error_mark_node
, error_mark_node
);
30376 return build_tree_list (sel_args
, addl_args
);
30379 /* Parse an Objective-C encode expression.
30381 objc-encode-expression:
30382 @encode objc-typename
30384 Returns an encoded representation of the type argument. */
30387 cp_parser_objc_encode_expression (cp_parser
* parser
)
30391 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30393 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
30394 matching_parens parens
;
30395 parens
.require_open (parser
);
30396 token
= cp_lexer_peek_token (parser
->lexer
);
30397 type
= complete_type (cp_parser_type_id (parser
));
30398 parens
.require_close (parser
);
30402 error_at (token
->location
,
30403 "%<@encode%> must specify a type as an argument");
30404 return error_mark_node
;
30407 /* This happens if we find @encode(T) (where T is a template
30408 typename or something dependent on a template typename) when
30409 parsing a template. In that case, we can't compile it
30410 immediately, but we rather create an AT_ENCODE_EXPR which will
30411 need to be instantiated when the template is used.
30413 if (dependent_type_p (type
))
30415 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
30416 TREE_READONLY (value
) = 1;
30421 /* Build a location of the form:
30424 with caret==start at the @ token, finishing at the close paren. */
30425 location_t combined_loc
30426 = make_location (start_loc
, start_loc
,
30427 cp_lexer_previous_token (parser
->lexer
)->location
);
30429 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
30432 /* Parse an Objective-C @defs expression. */
30435 cp_parser_objc_defs_expression (cp_parser
*parser
)
30439 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
30440 matching_parens parens
;
30441 parens
.require_open (parser
);
30442 name
= cp_parser_identifier (parser
);
30443 parens
.require_close (parser
);
30445 return objc_get_class_ivars (name
);
30448 /* Parse an Objective-C protocol expression.
30450 objc-protocol-expression:
30451 @protocol ( identifier )
30453 Returns a representation of the protocol expression. */
30456 cp_parser_objc_protocol_expression (cp_parser
* parser
)
30459 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30461 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30462 matching_parens parens
;
30463 parens
.require_open (parser
);
30464 proto
= cp_parser_identifier (parser
);
30465 parens
.require_close (parser
);
30467 /* Build a location of the form:
30470 with caret==start at the @ token, finishing at the close paren. */
30471 location_t combined_loc
30472 = make_location (start_loc
, start_loc
,
30473 cp_lexer_previous_token (parser
->lexer
)->location
);
30474 tree result
= objc_build_protocol_expr (proto
);
30475 protected_set_expr_location (result
, combined_loc
);
30479 /* Parse an Objective-C selector expression.
30481 objc-selector-expression:
30482 @selector ( objc-method-signature )
30484 objc-method-signature:
30490 objc-selector-seq objc-selector :
30492 Returns a representation of the method selector. */
30495 cp_parser_objc_selector_expression (cp_parser
* parser
)
30497 tree sel_seq
= NULL_TREE
;
30498 bool maybe_unary_selector_p
= true;
30500 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30502 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
30503 matching_parens parens
;
30504 parens
.require_open (parser
);
30505 token
= cp_lexer_peek_token (parser
->lexer
);
30507 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
30508 || token
->type
== CPP_SCOPE
)
30510 tree selector
= NULL_TREE
;
30512 if (token
->type
!= CPP_COLON
30513 || token
->type
== CPP_SCOPE
)
30514 selector
= cp_parser_objc_selector (parser
);
30516 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
30517 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
30519 /* Detect if we have a unary selector. */
30520 if (maybe_unary_selector_p
)
30522 sel_seq
= selector
;
30523 goto finish_selector
;
30527 cp_parser_error (parser
, "expected %<:%>");
30530 maybe_unary_selector_p
= false;
30531 token
= cp_lexer_consume_token (parser
->lexer
);
30533 if (token
->type
== CPP_SCOPE
)
30536 = chainon (sel_seq
,
30537 build_tree_list (selector
, NULL_TREE
));
30539 = chainon (sel_seq
,
30540 build_tree_list (NULL_TREE
, NULL_TREE
));
30544 = chainon (sel_seq
,
30545 build_tree_list (selector
, NULL_TREE
));
30547 token
= cp_lexer_peek_token (parser
->lexer
);
30551 parens
.require_close (parser
);
30554 /* Build a location of the form:
30557 with caret==start at the @ token, finishing at the close paren. */
30558 location_t combined_loc
30559 = make_location (loc
, loc
,
30560 cp_lexer_previous_token (parser
->lexer
)->location
);
30561 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
30562 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30563 protected_set_expr_location (result
, combined_loc
);
30567 /* Parse a list of identifiers.
30569 objc-identifier-list:
30571 objc-identifier-list , identifier
30573 Returns a TREE_LIST of identifier nodes. */
30576 cp_parser_objc_identifier_list (cp_parser
* parser
)
30582 identifier
= cp_parser_identifier (parser
);
30583 if (identifier
== error_mark_node
)
30584 return error_mark_node
;
30586 list
= build_tree_list (NULL_TREE
, identifier
);
30587 sep
= cp_lexer_peek_token (parser
->lexer
);
30589 while (sep
->type
== CPP_COMMA
)
30591 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30592 identifier
= cp_parser_identifier (parser
);
30593 if (identifier
== error_mark_node
)
30596 list
= chainon (list
, build_tree_list (NULL_TREE
,
30598 sep
= cp_lexer_peek_token (parser
->lexer
);
30604 /* Parse an Objective-C alias declaration.
30606 objc-alias-declaration:
30607 @compatibility_alias identifier identifier ;
30609 This function registers the alias mapping with the Objective-C front end.
30610 It returns nothing. */
30613 cp_parser_objc_alias_declaration (cp_parser
* parser
)
30617 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
30618 alias
= cp_parser_identifier (parser
);
30619 orig
= cp_parser_identifier (parser
);
30620 objc_declare_alias (alias
, orig
);
30621 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30624 /* Parse an Objective-C class forward-declaration.
30626 objc-class-declaration:
30627 @class objc-identifier-list ;
30629 The function registers the forward declarations with the Objective-C
30630 front end. It returns nothing. */
30633 cp_parser_objc_class_declaration (cp_parser
* parser
)
30635 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
30640 id
= cp_parser_identifier (parser
);
30641 if (id
== error_mark_node
)
30644 objc_declare_class (id
);
30646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30647 cp_lexer_consume_token (parser
->lexer
);
30651 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30654 /* Parse a list of Objective-C protocol references.
30656 objc-protocol-refs-opt:
30657 objc-protocol-refs [opt]
30659 objc-protocol-refs:
30660 < objc-identifier-list >
30662 Returns a TREE_LIST of identifiers, if any. */
30665 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
30667 tree protorefs
= NULL_TREE
;
30669 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
30671 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
30672 protorefs
= cp_parser_objc_identifier_list (parser
);
30673 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
30679 /* Parse a Objective-C visibility specification. */
30682 cp_parser_objc_visibility_spec (cp_parser
* parser
)
30684 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
30686 switch (vis
->keyword
)
30688 case RID_AT_PRIVATE
:
30689 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
30691 case RID_AT_PROTECTED
:
30692 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
30694 case RID_AT_PUBLIC
:
30695 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
30697 case RID_AT_PACKAGE
:
30698 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
30704 /* Eat '@private'/'@protected'/'@public'. */
30705 cp_lexer_consume_token (parser
->lexer
);
30708 /* Parse an Objective-C method type. Return 'true' if it is a class
30709 (+) method, and 'false' if it is an instance (-) method. */
30712 cp_parser_objc_method_type (cp_parser
* parser
)
30714 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
30720 /* Parse an Objective-C protocol qualifier. */
30723 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
30725 tree quals
= NULL_TREE
, node
;
30726 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30728 node
= token
->u
.value
;
30730 while (node
&& identifier_p (node
)
30731 && (node
== ridpointers
[(int) RID_IN
]
30732 || node
== ridpointers
[(int) RID_OUT
]
30733 || node
== ridpointers
[(int) RID_INOUT
]
30734 || node
== ridpointers
[(int) RID_BYCOPY
]
30735 || node
== ridpointers
[(int) RID_BYREF
]
30736 || node
== ridpointers
[(int) RID_ONEWAY
]))
30738 quals
= tree_cons (NULL_TREE
, node
, quals
);
30739 cp_lexer_consume_token (parser
->lexer
);
30740 token
= cp_lexer_peek_token (parser
->lexer
);
30741 node
= token
->u
.value
;
30747 /* Parse an Objective-C typename. */
30750 cp_parser_objc_typename (cp_parser
* parser
)
30752 tree type_name
= NULL_TREE
;
30754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30756 tree proto_quals
, cp_type
= NULL_TREE
;
30758 matching_parens parens
;
30759 parens
.consume_open (parser
); /* Eat '('. */
30760 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
30762 /* An ObjC type name may consist of just protocol qualifiers, in which
30763 case the type shall default to 'id'. */
30764 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30766 cp_type
= cp_parser_type_id (parser
);
30768 /* If the type could not be parsed, an error has already
30769 been produced. For error recovery, behave as if it had
30770 not been specified, which will use the default type
30772 if (cp_type
== error_mark_node
)
30774 cp_type
= NULL_TREE
;
30775 /* We need to skip to the closing parenthesis as
30776 cp_parser_type_id() does not seem to do it for
30778 cp_parser_skip_to_closing_parenthesis (parser
,
30779 /*recovering=*/true,
30780 /*or_comma=*/false,
30781 /*consume_paren=*/false);
30785 parens
.require_close (parser
);
30786 type_name
= build_tree_list (proto_quals
, cp_type
);
30792 /* Check to see if TYPE refers to an Objective-C selector name. */
30795 cp_parser_objc_selector_p (enum cpp_ttype type
)
30797 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
30798 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
30799 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
30800 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
30801 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
30804 /* Parse an Objective-C selector. */
30807 cp_parser_objc_selector (cp_parser
* parser
)
30809 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30811 if (!cp_parser_objc_selector_p (token
->type
))
30813 error_at (token
->location
, "invalid Objective-C++ selector name");
30814 return error_mark_node
;
30817 /* C++ operator names are allowed to appear in ObjC selectors. */
30818 switch (token
->type
)
30820 case CPP_AND_AND
: return get_identifier ("and");
30821 case CPP_AND_EQ
: return get_identifier ("and_eq");
30822 case CPP_AND
: return get_identifier ("bitand");
30823 case CPP_OR
: return get_identifier ("bitor");
30824 case CPP_COMPL
: return get_identifier ("compl");
30825 case CPP_NOT
: return get_identifier ("not");
30826 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30827 case CPP_OR_OR
: return get_identifier ("or");
30828 case CPP_OR_EQ
: return get_identifier ("or_eq");
30829 case CPP_XOR
: return get_identifier ("xor");
30830 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30831 default: return token
->u
.value
;
30835 /* Parse an Objective-C params list. */
30838 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30840 tree params
= NULL_TREE
;
30841 bool maybe_unary_selector_p
= true;
30842 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30844 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30846 tree selector
= NULL_TREE
, type_name
, identifier
;
30847 tree parm_attr
= NULL_TREE
;
30849 if (token
->keyword
== RID_ATTRIBUTE
)
30852 if (token
->type
!= CPP_COLON
)
30853 selector
= cp_parser_objc_selector (parser
);
30855 /* Detect if we have a unary selector. */
30856 if (maybe_unary_selector_p
30857 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30859 params
= selector
; /* Might be followed by attributes. */
30863 maybe_unary_selector_p
= false;
30864 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30866 /* Something went quite wrong. There should be a colon
30867 here, but there is not. Stop parsing parameters. */
30870 type_name
= cp_parser_objc_typename (parser
);
30871 /* New ObjC allows attributes on parameters too. */
30872 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30873 parm_attr
= cp_parser_attributes_opt (parser
);
30874 identifier
= cp_parser_identifier (parser
);
30878 objc_build_keyword_decl (selector
,
30883 token
= cp_lexer_peek_token (parser
->lexer
);
30886 if (params
== NULL_TREE
)
30888 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30889 return error_mark_node
;
30892 /* We allow tail attributes for the method. */
30893 if (token
->keyword
== RID_ATTRIBUTE
)
30895 *attributes
= cp_parser_attributes_opt (parser
);
30896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30897 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30899 cp_parser_error (parser
,
30900 "method attributes must be specified at the end");
30901 return error_mark_node
;
30904 if (params
== NULL_TREE
)
30906 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30907 return error_mark_node
;
30912 /* Parse the non-keyword Objective-C params. */
30915 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30918 tree params
= make_node (TREE_LIST
);
30919 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30920 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30922 while (token
->type
== CPP_COMMA
)
30924 cp_parameter_declarator
*parmdecl
;
30927 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30928 token
= cp_lexer_peek_token (parser
->lexer
);
30930 if (token
->type
== CPP_ELLIPSIS
)
30932 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30934 token
= cp_lexer_peek_token (parser
->lexer
);
30938 /* TODO: parse attributes for tail parameters. */
30939 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
30941 parm
= grokdeclarator (parmdecl
->declarator
,
30942 &parmdecl
->decl_specifiers
,
30943 PARM
, /*initialized=*/0,
30944 /*attrlist=*/NULL
);
30946 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30947 token
= cp_lexer_peek_token (parser
->lexer
);
30950 /* We allow tail attributes for the method. */
30951 if (token
->keyword
== RID_ATTRIBUTE
)
30953 if (*attributes
== NULL_TREE
)
30955 *attributes
= cp_parser_attributes_opt (parser
);
30956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30957 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30961 /* We have an error, but parse the attributes, so that we can
30963 *attributes
= cp_parser_attributes_opt (parser
);
30965 cp_parser_error (parser
,
30966 "method attributes must be specified at the end");
30967 return error_mark_node
;
30973 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30976 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30978 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30980 /* If the next token is `extern' and the following token is a string
30981 literal, then we have a linkage specification. */
30982 if (token
->keyword
== RID_EXTERN
30983 && cp_parser_is_pure_string_literal
30984 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30985 cp_parser_linkage_specification (parser
);
30986 /* Handle #pragma, if any. */
30987 else if (token
->type
== CPP_PRAGMA
)
30988 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30989 /* Allow stray semicolons. */
30990 else if (token
->type
== CPP_SEMICOLON
)
30991 cp_lexer_consume_token (parser
->lexer
);
30992 /* Mark methods as optional or required, when building protocols. */
30993 else if (token
->keyword
== RID_AT_OPTIONAL
)
30995 cp_lexer_consume_token (parser
->lexer
);
30996 objc_set_method_opt (true);
30998 else if (token
->keyword
== RID_AT_REQUIRED
)
31000 cp_lexer_consume_token (parser
->lexer
);
31001 objc_set_method_opt (false);
31003 else if (token
->keyword
== RID_NAMESPACE
)
31004 cp_parser_namespace_definition (parser
);
31005 /* Other stray characters must generate errors. */
31006 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
31008 cp_lexer_consume_token (parser
->lexer
);
31009 error ("stray %qs between Objective-C++ methods",
31010 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
31012 /* Finally, try to parse a block-declaration, or a function-definition. */
31014 cp_parser_block_declaration (parser
, /*statement_p=*/false);
31017 /* Parse a method signature. */
31020 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
31022 tree rettype
, kwdparms
, optparms
;
31023 bool ellipsis
= false;
31024 bool is_class_method
;
31026 is_class_method
= cp_parser_objc_method_type (parser
);
31027 rettype
= cp_parser_objc_typename (parser
);
31028 *attributes
= NULL_TREE
;
31029 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
31030 if (kwdparms
== error_mark_node
)
31031 return error_mark_node
;
31032 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
31033 if (optparms
== error_mark_node
)
31034 return error_mark_node
;
31036 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
31040 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
31043 cp_lexer_save_tokens (parser
->lexer
);
31044 tattr
= cp_parser_attributes_opt (parser
);
31045 gcc_assert (tattr
) ;
31047 /* If the attributes are followed by a method introducer, this is not allowed.
31048 Dump the attributes and flag the situation. */
31049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
31050 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
31053 /* Otherwise, the attributes introduce some interstitial code, possibly so
31054 rewind to allow that check. */
31055 cp_lexer_rollback_tokens (parser
->lexer
);
31059 /* Parse an Objective-C method prototype list. */
31062 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
31064 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31066 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31068 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
31070 tree attributes
, sig
;
31071 bool is_class_method
;
31072 if (token
->type
== CPP_PLUS
)
31073 is_class_method
= true;
31075 is_class_method
= false;
31076 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
31077 if (sig
== error_mark_node
)
31079 cp_parser_skip_to_end_of_block_or_statement (parser
);
31080 token
= cp_lexer_peek_token (parser
->lexer
);
31083 objc_add_method_declaration (is_class_method
, sig
, attributes
);
31084 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31086 else if (token
->keyword
== RID_AT_PROPERTY
)
31087 cp_parser_objc_at_property_declaration (parser
);
31088 else if (token
->keyword
== RID_ATTRIBUTE
31089 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31090 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
31092 "prefix attributes are ignored for methods");
31094 /* Allow for interspersed non-ObjC++ code. */
31095 cp_parser_objc_interstitial_code (parser
);
31097 token
= cp_lexer_peek_token (parser
->lexer
);
31100 if (token
->type
!= CPP_EOF
)
31101 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31103 cp_parser_error (parser
, "expected %<@end%>");
31105 objc_finish_interface ();
31108 /* Parse an Objective-C method definition list. */
31111 cp_parser_objc_method_definition_list (cp_parser
* parser
)
31113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31115 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31119 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
31122 tree sig
, attribute
;
31123 bool is_class_method
;
31124 if (token
->type
== CPP_PLUS
)
31125 is_class_method
= true;
31127 is_class_method
= false;
31128 push_deferring_access_checks (dk_deferred
);
31129 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
31130 if (sig
== error_mark_node
)
31132 cp_parser_skip_to_end_of_block_or_statement (parser
);
31133 token
= cp_lexer_peek_token (parser
->lexer
);
31136 objc_start_method_definition (is_class_method
, sig
, attribute
,
31139 /* For historical reasons, we accept an optional semicolon. */
31140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31141 cp_lexer_consume_token (parser
->lexer
);
31143 ptk
= cp_lexer_peek_token (parser
->lexer
);
31144 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
31145 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
31147 perform_deferred_access_checks (tf_warning_or_error
);
31148 stop_deferring_access_checks ();
31149 meth
= cp_parser_function_definition_after_declarator (parser
,
31151 pop_deferring_access_checks ();
31152 objc_finish_method_definition (meth
);
31155 /* The following case will be removed once @synthesize is
31156 completely implemented. */
31157 else if (token
->keyword
== RID_AT_PROPERTY
)
31158 cp_parser_objc_at_property_declaration (parser
);
31159 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
31160 cp_parser_objc_at_synthesize_declaration (parser
);
31161 else if (token
->keyword
== RID_AT_DYNAMIC
)
31162 cp_parser_objc_at_dynamic_declaration (parser
);
31163 else if (token
->keyword
== RID_ATTRIBUTE
31164 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31165 warning_at (token
->location
, OPT_Wattributes
,
31166 "prefix attributes are ignored for methods");
31168 /* Allow for interspersed non-ObjC++ code. */
31169 cp_parser_objc_interstitial_code (parser
);
31171 token
= cp_lexer_peek_token (parser
->lexer
);
31174 if (token
->type
!= CPP_EOF
)
31175 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31177 cp_parser_error (parser
, "expected %<@end%>");
31179 objc_finish_implementation ();
31182 /* Parse Objective-C ivars. */
31185 cp_parser_objc_class_ivars (cp_parser
* parser
)
31187 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31189 if (token
->type
!= CPP_OPEN_BRACE
)
31190 return; /* No ivars specified. */
31192 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
31193 token
= cp_lexer_peek_token (parser
->lexer
);
31195 while (token
->type
!= CPP_CLOSE_BRACE
31196 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31198 cp_decl_specifier_seq declspecs
;
31199 int decl_class_or_enum_p
;
31200 tree prefix_attributes
;
31202 cp_parser_objc_visibility_spec (parser
);
31204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
31207 cp_parser_decl_specifier_seq (parser
,
31208 CP_PARSER_FLAGS_OPTIONAL
,
31210 &decl_class_or_enum_p
);
31212 /* auto, register, static, extern, mutable. */
31213 if (declspecs
.storage_class
!= sc_none
)
31215 cp_parser_error (parser
, "invalid type for instance variable");
31216 declspecs
.storage_class
= sc_none
;
31219 /* thread_local. */
31220 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31222 cp_parser_error (parser
, "invalid type for instance variable");
31223 declspecs
.locations
[ds_thread
] = 0;
31227 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31229 cp_parser_error (parser
, "invalid type for instance variable");
31230 declspecs
.locations
[ds_typedef
] = 0;
31233 prefix_attributes
= declspecs
.attributes
;
31234 declspecs
.attributes
= NULL_TREE
;
31236 /* Keep going until we hit the `;' at the end of the
31238 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31240 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
31241 cp_declarator
*declarator
= NULL
;
31242 int ctor_dtor_or_conv_p
;
31244 /* Check for a (possibly unnamed) bitfield declaration. */
31245 token
= cp_lexer_peek_token (parser
->lexer
);
31246 if (token
->type
== CPP_COLON
)
31249 if (token
->type
== CPP_NAME
31250 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31253 /* Get the name of the bitfield. */
31254 declarator
= make_id_declarator (NULL_TREE
,
31255 cp_parser_identifier (parser
),
31256 sfk_none
, token
->location
);
31259 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31260 /* Get the width of the bitfield. */
31262 = cp_parser_constant_expression (parser
);
31266 /* Parse the declarator. */
31268 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31269 CP_PARSER_FLAGS_NONE
,
31270 &ctor_dtor_or_conv_p
,
31271 /*parenthesized_p=*/NULL
,
31272 /*member_p=*/false,
31273 /*friend_p=*/false,
31274 /*static_p=*/false);
31277 /* Look for attributes that apply to the ivar. */
31278 attributes
= cp_parser_attributes_opt (parser
);
31279 /* Remember which attributes are prefix attributes and
31281 first_attribute
= attributes
;
31282 /* Combine the attributes. */
31283 attributes
= attr_chainon (prefix_attributes
, attributes
);
31286 /* Create the bitfield declaration. */
31287 decl
= grokbitfield (declarator
, &declspecs
,
31288 width
, NULL_TREE
, attributes
);
31290 decl
= grokfield (declarator
, &declspecs
,
31291 NULL_TREE
, /*init_const_expr_p=*/false,
31292 NULL_TREE
, attributes
);
31294 /* Add the instance variable. */
31295 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
31296 objc_add_instance_variable (decl
);
31298 /* Reset PREFIX_ATTRIBUTES. */
31299 if (attributes
!= error_mark_node
)
31301 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31302 attributes
= TREE_CHAIN (attributes
);
31304 TREE_CHAIN (attributes
) = NULL_TREE
;
31307 token
= cp_lexer_peek_token (parser
->lexer
);
31309 if (token
->type
== CPP_COMMA
)
31311 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31317 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31318 token
= cp_lexer_peek_token (parser
->lexer
);
31321 if (token
->keyword
== RID_AT_END
)
31322 cp_parser_error (parser
, "expected %<}%>");
31324 /* Do not consume the RID_AT_END, so it will be read again as terminating
31325 the @interface of @implementation. */
31326 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31327 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
31329 /* For historical reasons, we accept an optional semicolon. */
31330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31331 cp_lexer_consume_token (parser
->lexer
);
31334 /* Parse an Objective-C protocol declaration. */
31337 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
31339 tree proto
, protorefs
;
31342 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
31343 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31345 tok
= cp_lexer_peek_token (parser
->lexer
);
31346 error_at (tok
->location
, "identifier expected after %<@protocol%>");
31347 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31351 /* See if we have a forward declaration or a definition. */
31352 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
31354 /* Try a forward declaration first. */
31355 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
31361 id
= cp_parser_identifier (parser
);
31362 if (id
== error_mark_node
)
31365 objc_declare_protocol (id
, attributes
);
31367 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31368 cp_lexer_consume_token (parser
->lexer
);
31372 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31375 /* Ok, we got a full-fledged definition (or at least should). */
31378 proto
= cp_parser_identifier (parser
);
31379 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
31380 objc_start_protocol (proto
, protorefs
, attributes
);
31381 cp_parser_objc_method_prototype_list (parser
);
31385 /* Parse an Objective-C superclass or category. */
31388 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
31391 tree
*categ
, bool *is_class_extension
)
31393 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
31395 *super
= *categ
= NULL_TREE
;
31396 *is_class_extension
= false;
31397 if (next
->type
== CPP_COLON
)
31399 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
31400 *super
= cp_parser_identifier (parser
);
31402 else if (next
->type
== CPP_OPEN_PAREN
)
31404 matching_parens parens
;
31405 parens
.consume_open (parser
); /* Eat '('. */
31407 /* If there is no category name, and this is an @interface, we
31408 have a class extension. */
31409 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31411 *categ
= NULL_TREE
;
31412 *is_class_extension
= true;
31415 *categ
= cp_parser_identifier (parser
);
31417 parens
.require_close (parser
);
31421 /* Parse an Objective-C class interface. */
31424 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
31426 tree name
, super
, categ
, protos
;
31427 bool is_class_extension
;
31429 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
31430 name
= cp_parser_identifier (parser
);
31431 if (name
== error_mark_node
)
31433 /* It's hard to recover because even if valid @interface stuff
31434 is to follow, we can't compile it (or validate it) if we
31435 don't even know which class it refers to. Let's assume this
31436 was a stray '@interface' token in the stream and skip it.
31440 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
31441 &is_class_extension
);
31442 protos
= cp_parser_objc_protocol_refs_opt (parser
);
31444 /* We have either a class or a category on our hands. */
31445 if (categ
|| is_class_extension
)
31446 objc_start_category_interface (name
, categ
, protos
, attributes
);
31449 objc_start_class_interface (name
, super
, protos
, attributes
);
31450 /* Handle instance variable declarations, if any. */
31451 cp_parser_objc_class_ivars (parser
);
31452 objc_continue_interface ();
31455 cp_parser_objc_method_prototype_list (parser
);
31458 /* Parse an Objective-C class implementation. */
31461 cp_parser_objc_class_implementation (cp_parser
* parser
)
31463 tree name
, super
, categ
;
31464 bool is_class_extension
;
31466 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
31467 name
= cp_parser_identifier (parser
);
31468 if (name
== error_mark_node
)
31470 /* It's hard to recover because even if valid @implementation
31471 stuff is to follow, we can't compile it (or validate it) if
31472 we don't even know which class it refers to. Let's assume
31473 this was a stray '@implementation' token in the stream and
31478 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
31479 &is_class_extension
);
31481 /* We have either a class or a category on our hands. */
31483 objc_start_category_implementation (name
, categ
);
31486 objc_start_class_implementation (name
, super
);
31487 /* Handle instance variable declarations, if any. */
31488 cp_parser_objc_class_ivars (parser
);
31489 objc_continue_implementation ();
31492 cp_parser_objc_method_definition_list (parser
);
31495 /* Consume the @end token and finish off the implementation. */
31498 cp_parser_objc_end_implementation (cp_parser
* parser
)
31500 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
31501 objc_finish_implementation ();
31504 /* Parse an Objective-C declaration. */
31507 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
31509 /* Try to figure out what kind of declaration is present. */
31510 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31513 switch (kwd
->keyword
)
31518 error_at (kwd
->location
, "attributes may not be specified before"
31519 " the %<@%D%> Objective-C++ keyword",
31523 case RID_AT_IMPLEMENTATION
:
31524 warning_at (kwd
->location
, OPT_Wattributes
,
31525 "prefix attributes are ignored before %<@%D%>",
31532 switch (kwd
->keyword
)
31535 cp_parser_objc_alias_declaration (parser
);
31538 cp_parser_objc_class_declaration (parser
);
31540 case RID_AT_PROTOCOL
:
31541 cp_parser_objc_protocol_declaration (parser
, attributes
);
31543 case RID_AT_INTERFACE
:
31544 cp_parser_objc_class_interface (parser
, attributes
);
31546 case RID_AT_IMPLEMENTATION
:
31547 cp_parser_objc_class_implementation (parser
);
31550 cp_parser_objc_end_implementation (parser
);
31553 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31555 cp_parser_skip_to_end_of_block_or_statement (parser
);
31559 /* Parse an Objective-C try-catch-finally statement.
31561 objc-try-catch-finally-stmt:
31562 @try compound-statement objc-catch-clause-seq [opt]
31563 objc-finally-clause [opt]
31565 objc-catch-clause-seq:
31566 objc-catch-clause objc-catch-clause-seq [opt]
31569 @catch ( objc-exception-declaration ) compound-statement
31571 objc-finally-clause:
31572 @finally compound-statement
31574 objc-exception-declaration:
31575 parameter-declaration
31578 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31582 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31583 for C. Keep them in sync. */
31586 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
31588 location_t location
;
31591 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
31592 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31593 objc_maybe_warn_exceptions (location
);
31594 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31595 node, lest it get absorbed into the surrounding block. */
31596 stmt
= push_stmt_list ();
31597 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31598 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
31600 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
31602 cp_parameter_declarator
*parm
;
31603 tree parameter_declaration
= error_mark_node
;
31604 bool seen_open_paren
= false;
31605 matching_parens parens
;
31607 cp_lexer_consume_token (parser
->lexer
);
31608 if (parens
.require_open (parser
))
31609 seen_open_paren
= true;
31610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31612 /* We have "@catch (...)" (where the '...' are literally
31613 what is in the code). Skip the '...'.
31614 parameter_declaration is set to NULL_TREE, and
31615 objc_being_catch_clauses() knows that that means
31617 cp_lexer_consume_token (parser
->lexer
);
31618 parameter_declaration
= NULL_TREE
;
31622 /* We have "@catch (NSException *exception)" or something
31623 like that. Parse the parameter declaration. */
31624 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
31627 parameter_declaration
= error_mark_node
;
31629 parameter_declaration
= grokdeclarator (parm
->declarator
,
31630 &parm
->decl_specifiers
,
31631 PARM
, /*initialized=*/0,
31632 /*attrlist=*/NULL
);
31634 if (seen_open_paren
)
31635 parens
.require_close (parser
);
31638 /* If there was no open parenthesis, we are recovering from
31639 an error, and we are trying to figure out what mistake
31640 the user has made. */
31642 /* If there is an immediate closing parenthesis, the user
31643 probably forgot the opening one (ie, they typed "@catch
31644 NSException *e)". Parse the closing parenthesis and keep
31646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
31647 cp_lexer_consume_token (parser
->lexer
);
31649 /* If these is no immediate closing parenthesis, the user
31650 probably doesn't know that parenthesis are required at
31651 all (ie, they typed "@catch NSException *e"). So, just
31652 forget about the closing parenthesis and keep going. */
31654 objc_begin_catch_clause (parameter_declaration
);
31655 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31656 objc_finish_catch_clause ();
31658 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
31660 cp_lexer_consume_token (parser
->lexer
);
31661 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31662 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31663 node, lest it get absorbed into the surrounding block. */
31664 stmt
= push_stmt_list ();
31665 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31666 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
31669 return objc_finish_try_stmt ();
31672 /* Parse an Objective-C synchronized statement.
31674 objc-synchronized-stmt:
31675 @synchronized ( expression ) compound-statement
31677 Returns NULL_TREE. */
31680 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
31682 location_t location
;
31685 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
31687 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
31688 objc_maybe_warn_exceptions (location
);
31689 matching_parens parens
;
31690 parens
.require_open (parser
);
31691 lock
= cp_parser_expression (parser
);
31692 parens
.require_close (parser
);
31694 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31695 node, lest it get absorbed into the surrounding block. */
31696 stmt
= push_stmt_list ();
31697 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
31699 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
31702 /* Parse an Objective-C throw statement.
31705 @throw assignment-expression [opt] ;
31707 Returns a constructed '@throw' statement. */
31710 cp_parser_objc_throw_statement (cp_parser
*parser
)
31712 tree expr
= NULL_TREE
;
31713 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31715 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
31717 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31718 expr
= cp_parser_expression (parser
);
31720 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31722 return objc_build_throw_stmt (loc
, expr
);
31725 /* Parse an Objective-C statement. */
31728 cp_parser_objc_statement (cp_parser
* parser
)
31730 /* Try to figure out what kind of declaration is present. */
31731 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31733 switch (kwd
->keyword
)
31736 return cp_parser_objc_try_catch_finally_statement (parser
);
31737 case RID_AT_SYNCHRONIZED
:
31738 return cp_parser_objc_synchronized_statement (parser
);
31740 return cp_parser_objc_throw_statement (parser
);
31742 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
31744 cp_parser_skip_to_end_of_block_or_statement (parser
);
31747 return error_mark_node
;
31750 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31751 look ahead to see if an objc keyword follows the attributes. This
31752 is to detect the use of prefix attributes on ObjC @interface and
31756 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
31758 cp_lexer_save_tokens (parser
->lexer
);
31759 *attrib
= cp_parser_attributes_opt (parser
);
31760 gcc_assert (*attrib
);
31761 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
31763 cp_lexer_commit_tokens (parser
->lexer
);
31766 cp_lexer_rollback_tokens (parser
->lexer
);
31770 /* This routine is a minimal replacement for
31771 c_parser_struct_declaration () used when parsing the list of
31772 types/names or ObjC++ properties. For example, when parsing the
31775 @property (readonly) int a, b, c;
31777 this function is responsible for parsing "int a, int b, int c" and
31778 returning the declarations as CHAIN of DECLs.
31780 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31781 similar parsing. */
31783 cp_parser_objc_struct_declaration (cp_parser
*parser
)
31785 tree decls
= NULL_TREE
;
31786 cp_decl_specifier_seq declspecs
;
31787 int decl_class_or_enum_p
;
31788 tree prefix_attributes
;
31790 cp_parser_decl_specifier_seq (parser
,
31791 CP_PARSER_FLAGS_NONE
,
31793 &decl_class_or_enum_p
);
31795 if (declspecs
.type
== error_mark_node
)
31796 return error_mark_node
;
31798 /* auto, register, static, extern, mutable. */
31799 if (declspecs
.storage_class
!= sc_none
)
31801 cp_parser_error (parser
, "invalid type for property");
31802 declspecs
.storage_class
= sc_none
;
31805 /* thread_local. */
31806 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
31808 cp_parser_error (parser
, "invalid type for property");
31809 declspecs
.locations
[ds_thread
] = 0;
31813 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31815 cp_parser_error (parser
, "invalid type for property");
31816 declspecs
.locations
[ds_typedef
] = 0;
31819 prefix_attributes
= declspecs
.attributes
;
31820 declspecs
.attributes
= NULL_TREE
;
31822 /* Keep going until we hit the `;' at the end of the declaration. */
31823 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31825 tree attributes
, first_attribute
, decl
;
31826 cp_declarator
*declarator
;
31829 /* Parse the declarator. */
31830 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31831 CP_PARSER_FLAGS_NONE
,
31832 NULL
, NULL
, false, false, false);
31834 /* Look for attributes that apply to the ivar. */
31835 attributes
= cp_parser_attributes_opt (parser
);
31836 /* Remember which attributes are prefix attributes and
31838 first_attribute
= attributes
;
31839 /* Combine the attributes. */
31840 attributes
= attr_chainon (prefix_attributes
, attributes
);
31842 decl
= grokfield (declarator
, &declspecs
,
31843 NULL_TREE
, /*init_const_expr_p=*/false,
31844 NULL_TREE
, attributes
);
31846 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31847 return error_mark_node
;
31849 /* Reset PREFIX_ATTRIBUTES. */
31850 if (attributes
!= error_mark_node
)
31852 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31853 attributes
= TREE_CHAIN (attributes
);
31855 TREE_CHAIN (attributes
) = NULL_TREE
;
31858 DECL_CHAIN (decl
) = decls
;
31861 token
= cp_lexer_peek_token (parser
->lexer
);
31862 if (token
->type
== CPP_COMMA
)
31864 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31873 /* Parse an Objective-C @property declaration. The syntax is:
31875 objc-property-declaration:
31876 '@property' objc-property-attributes[opt] struct-declaration ;
31878 objc-property-attributes:
31879 '(' objc-property-attribute-list ')'
31881 objc-property-attribute-list:
31882 objc-property-attribute
31883 objc-property-attribute-list, objc-property-attribute
31885 objc-property-attribute
31886 'getter' = identifier
31887 'setter' = identifier
31896 @property NSString *name;
31897 @property (readonly) id object;
31898 @property (retain, nonatomic, getter=getTheName) id name;
31899 @property int a, b, c;
31901 PS: This function is identical to
31902 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31904 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31906 /* The following variables hold the attributes of the properties as
31907 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31908 seen. When we see an attribute, we set them to 'true' (if they
31909 are boolean properties) or to the identifier (if they have an
31910 argument, ie, for getter and setter). Note that here we only
31911 parse the list of attributes, check the syntax and accumulate the
31912 attributes that we find. objc_add_property_declaration() will
31913 then process the information. */
31914 bool property_assign
= false;
31915 bool property_copy
= false;
31916 tree property_getter_ident
= NULL_TREE
;
31917 bool property_nonatomic
= false;
31918 bool property_readonly
= false;
31919 bool property_readwrite
= false;
31920 bool property_retain
= false;
31921 tree property_setter_ident
= NULL_TREE
;
31923 /* 'properties' is the list of properties that we read. Usually a
31924 single one, but maybe more (eg, in "@property int a, b, c;" there
31929 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31931 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31933 /* Parse the optional attribute list... */
31934 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31937 matching_parens parens
;
31938 parens
.consume_open (parser
);
31942 bool syntax_error
= false;
31943 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31946 if (token
->type
!= CPP_NAME
)
31948 cp_parser_error (parser
, "expected identifier");
31951 keyword
= C_RID_CODE (token
->u
.value
);
31952 cp_lexer_consume_token (parser
->lexer
);
31955 case RID_ASSIGN
: property_assign
= true; break;
31956 case RID_COPY
: property_copy
= true; break;
31957 case RID_NONATOMIC
: property_nonatomic
= true; break;
31958 case RID_READONLY
: property_readonly
= true; break;
31959 case RID_READWRITE
: property_readwrite
= true; break;
31960 case RID_RETAIN
: property_retain
= true; break;
31964 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31966 if (keyword
== RID_GETTER
)
31967 cp_parser_error (parser
,
31968 "missing %<=%> (after %<getter%> attribute)");
31970 cp_parser_error (parser
,
31971 "missing %<=%> (after %<setter%> attribute)");
31972 syntax_error
= true;
31975 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31976 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31978 cp_parser_error (parser
, "expected identifier");
31979 syntax_error
= true;
31982 if (keyword
== RID_SETTER
)
31984 if (property_setter_ident
!= NULL_TREE
)
31986 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31987 cp_lexer_consume_token (parser
->lexer
);
31990 property_setter_ident
= cp_parser_objc_selector (parser
);
31991 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31992 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31994 cp_lexer_consume_token (parser
->lexer
);
31998 if (property_getter_ident
!= NULL_TREE
)
32000 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
32001 cp_lexer_consume_token (parser
->lexer
);
32004 property_getter_ident
= cp_parser_objc_selector (parser
);
32008 cp_parser_error (parser
, "unknown property attribute");
32009 syntax_error
= true;
32016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32017 cp_lexer_consume_token (parser
->lexer
);
32022 /* FIXME: "@property (setter, assign);" will generate a spurious
32023 "error: expected ‘)’ before ‘,’ token". This is because
32024 cp_parser_require, unlike the C counterpart, will produce an
32025 error even if we are in error recovery. */
32026 if (!parens
.require_close (parser
))
32028 cp_parser_skip_to_closing_parenthesis (parser
,
32029 /*recovering=*/true,
32030 /*or_comma=*/false,
32031 /*consume_paren=*/true);
32035 /* ... and the property declaration(s). */
32036 properties
= cp_parser_objc_struct_declaration (parser
);
32038 if (properties
== error_mark_node
)
32040 cp_parser_skip_to_end_of_statement (parser
);
32041 /* If the next token is now a `;', consume it. */
32042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32043 cp_lexer_consume_token (parser
->lexer
);
32047 if (properties
== NULL_TREE
)
32048 cp_parser_error (parser
, "expected identifier");
32051 /* Comma-separated properties are chained together in
32052 reverse order; add them one by one. */
32053 properties
= nreverse (properties
);
32055 for (; properties
; properties
= TREE_CHAIN (properties
))
32056 objc_add_property_declaration (loc
, copy_node (properties
),
32057 property_readonly
, property_readwrite
,
32058 property_assign
, property_retain
,
32059 property_copy
, property_nonatomic
,
32060 property_getter_ident
, property_setter_ident
);
32063 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32066 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32068 objc-synthesize-declaration:
32069 @synthesize objc-synthesize-identifier-list ;
32071 objc-synthesize-identifier-list:
32072 objc-synthesize-identifier
32073 objc-synthesize-identifier-list, objc-synthesize-identifier
32075 objc-synthesize-identifier
32077 identifier = identifier
32080 @synthesize MyProperty;
32081 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32083 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32084 for C. Keep them in sync.
32087 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
32089 tree list
= NULL_TREE
;
32091 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32093 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
32096 tree property
, ivar
;
32097 property
= cp_parser_identifier (parser
);
32098 if (property
== error_mark_node
)
32100 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
32105 cp_lexer_consume_token (parser
->lexer
);
32106 ivar
= cp_parser_identifier (parser
);
32107 if (ivar
== error_mark_node
)
32109 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32115 list
= chainon (list
, build_tree_list (ivar
, property
));
32116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32117 cp_lexer_consume_token (parser
->lexer
);
32121 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32122 objc_add_synthesize_declaration (loc
, list
);
32125 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32127 objc-dynamic-declaration:
32128 @dynamic identifier-list ;
32131 @dynamic MyProperty;
32132 @dynamic MyProperty, AnotherProperty;
32134 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32135 for C. Keep them in sync.
32138 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
32140 tree list
= NULL_TREE
;
32142 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32144 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
32148 property
= cp_parser_identifier (parser
);
32149 if (property
== error_mark_node
)
32151 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32154 list
= chainon (list
, build_tree_list (NULL
, property
));
32155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32156 cp_lexer_consume_token (parser
->lexer
);
32160 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32161 objc_add_dynamic_declaration (loc
, list
);
32165 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32167 /* Returns name of the next clause.
32168 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32169 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32170 returned and the token is consumed. */
32172 static pragma_omp_clause
32173 cp_parser_omp_clause_name (cp_parser
*parser
)
32175 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
32177 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32178 result
= PRAGMA_OACC_CLAUSE_AUTO
;
32179 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
32180 result
= PRAGMA_OMP_CLAUSE_IF
;
32181 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
32182 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
32183 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
32184 result
= PRAGMA_OACC_CLAUSE_DELETE
;
32185 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
32186 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
32187 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
32188 result
= PRAGMA_OMP_CLAUSE_FOR
;
32189 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32191 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32192 const char *p
= IDENTIFIER_POINTER (id
);
32197 if (!strcmp ("aligned", p
))
32198 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
32199 else if (!strcmp ("async", p
))
32200 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
32203 if (!strcmp ("collapse", p
))
32204 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
32205 else if (!strcmp ("copy", p
))
32206 result
= PRAGMA_OACC_CLAUSE_COPY
;
32207 else if (!strcmp ("copyin", p
))
32208 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
32209 else if (!strcmp ("copyout", p
))
32210 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32211 else if (!strcmp ("copyprivate", p
))
32212 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
32213 else if (!strcmp ("create", p
))
32214 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32217 if (!strcmp ("defaultmap", p
))
32218 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
32219 else if (!strcmp ("depend", p
))
32220 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
32221 else if (!strcmp ("device", p
))
32222 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
32223 else if (!strcmp ("deviceptr", p
))
32224 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
32225 else if (!strcmp ("device_resident", p
))
32226 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
32227 else if (!strcmp ("dist_schedule", p
))
32228 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
32231 if (!strcmp ("final", p
))
32232 result
= PRAGMA_OMP_CLAUSE_FINAL
;
32233 else if (!strcmp ("finalize", p
))
32234 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
32235 else if (!strcmp ("firstprivate", p
))
32236 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
32237 else if (!strcmp ("from", p
))
32238 result
= PRAGMA_OMP_CLAUSE_FROM
;
32241 if (!strcmp ("gang", p
))
32242 result
= PRAGMA_OACC_CLAUSE_GANG
;
32243 else if (!strcmp ("grainsize", p
))
32244 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
32247 if (!strcmp ("hint", p
))
32248 result
= PRAGMA_OMP_CLAUSE_HINT
;
32249 else if (!strcmp ("host", p
))
32250 result
= PRAGMA_OACC_CLAUSE_HOST
;
32253 if (!strcmp ("if_present", p
))
32254 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
32255 else if (!strcmp ("in_reduction", p
))
32256 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
32257 else if (!strcmp ("inbranch", p
))
32258 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
32259 else if (!strcmp ("independent", p
))
32260 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
32261 else if (!strcmp ("is_device_ptr", p
))
32262 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
32265 if (!strcmp ("lastprivate", p
))
32266 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
32267 else if (!strcmp ("linear", p
))
32268 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
32269 else if (!strcmp ("link", p
))
32270 result
= PRAGMA_OMP_CLAUSE_LINK
;
32273 if (!strcmp ("map", p
))
32274 result
= PRAGMA_OMP_CLAUSE_MAP
;
32275 else if (!strcmp ("mergeable", p
))
32276 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
32279 if (!strcmp ("nogroup", p
))
32280 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
32281 else if (!strcmp ("nontemporal", p
))
32282 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
32283 else if (!strcmp ("notinbranch", p
))
32284 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
32285 else if (!strcmp ("nowait", p
))
32286 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
32287 else if (!strcmp ("num_gangs", p
))
32288 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
32289 else if (!strcmp ("num_tasks", p
))
32290 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
32291 else if (!strcmp ("num_teams", p
))
32292 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
32293 else if (!strcmp ("num_threads", p
))
32294 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
32295 else if (!strcmp ("num_workers", p
))
32296 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
32299 if (!strcmp ("ordered", p
))
32300 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
32303 if (!strcmp ("parallel", p
))
32304 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
32305 else if (!strcmp ("present", p
))
32306 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
32307 else if (!strcmp ("present_or_copy", p
)
32308 || !strcmp ("pcopy", p
))
32309 result
= PRAGMA_OACC_CLAUSE_COPY
;
32310 else if (!strcmp ("present_or_copyin", p
)
32311 || !strcmp ("pcopyin", p
))
32312 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
32313 else if (!strcmp ("present_or_copyout", p
)
32314 || !strcmp ("pcopyout", p
))
32315 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
32316 else if (!strcmp ("present_or_create", p
)
32317 || !strcmp ("pcreate", p
))
32318 result
= PRAGMA_OACC_CLAUSE_CREATE
;
32319 else if (!strcmp ("priority", p
))
32320 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
32321 else if (!strcmp ("proc_bind", p
))
32322 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
32325 if (!strcmp ("reduction", p
))
32326 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
32329 if (!strcmp ("safelen", p
))
32330 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
32331 else if (!strcmp ("schedule", p
))
32332 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
32333 else if (!strcmp ("sections", p
))
32334 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
32335 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
32336 result
= PRAGMA_OACC_CLAUSE_HOST
;
32337 else if (!strcmp ("seq", p
))
32338 result
= PRAGMA_OACC_CLAUSE_SEQ
;
32339 else if (!strcmp ("shared", p
))
32340 result
= PRAGMA_OMP_CLAUSE_SHARED
;
32341 else if (!strcmp ("simd", p
))
32342 result
= PRAGMA_OMP_CLAUSE_SIMD
;
32343 else if (!strcmp ("simdlen", p
))
32344 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
32347 if (!strcmp ("task_reduction", p
))
32348 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
32349 else if (!strcmp ("taskgroup", p
))
32350 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
32351 else if (!strcmp ("thread_limit", p
))
32352 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
32353 else if (!strcmp ("threads", p
))
32354 result
= PRAGMA_OMP_CLAUSE_THREADS
;
32355 else if (!strcmp ("tile", p
))
32356 result
= PRAGMA_OACC_CLAUSE_TILE
;
32357 else if (!strcmp ("to", p
))
32358 result
= PRAGMA_OMP_CLAUSE_TO
;
32361 if (!strcmp ("uniform", p
))
32362 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
32363 else if (!strcmp ("untied", p
))
32364 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
32365 else if (!strcmp ("use_device", p
))
32366 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
32367 else if (!strcmp ("use_device_ptr", p
))
32368 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
32371 if (!strcmp ("vector", p
))
32372 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
32373 else if (!strcmp ("vector_length", p
))
32374 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
32377 if (!strcmp ("wait", p
))
32378 result
= PRAGMA_OACC_CLAUSE_WAIT
;
32379 else if (!strcmp ("worker", p
))
32380 result
= PRAGMA_OACC_CLAUSE_WORKER
;
32385 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
32386 cp_lexer_consume_token (parser
->lexer
);
32391 /* Validate that a clause of the given type does not already exist. */
32394 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
32395 const char *name
, location_t location
)
32399 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32400 if (OMP_CLAUSE_CODE (c
) == code
)
32402 error_at (location
, "too many %qs clauses", name
);
32410 variable-list , identifier
32412 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32413 colon). An opening parenthesis will have been consumed by the caller.
32415 If KIND is nonzero, create the appropriate node and install the decl
32416 in OMP_CLAUSE_DECL and add the node to the head of the list.
32418 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32419 return the list created.
32421 COLON can be NULL if only closing parenthesis should end the list,
32422 or pointer to bool which will receive false if the list is terminated
32423 by closing parenthesis or true if the list is terminated by colon. */
32426 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
32427 tree list
, bool *colon
)
32430 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32433 parser
->colon_corrects_to_scope_p
= false;
32440 if (kind
== OMP_CLAUSE_DEPEND
)
32441 cp_parser_parse_tentatively (parser
);
32442 token
= cp_lexer_peek_token (parser
->lexer
);
32444 && current_class_ptr
32445 && cp_parser_is_keyword (token
, RID_THIS
))
32447 decl
= finish_this_expr ();
32448 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
32449 || CONVERT_EXPR_P (decl
))
32450 decl
= TREE_OPERAND (decl
, 0);
32451 cp_lexer_consume_token (parser
->lexer
);
32455 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32456 /*check_dependency_p=*/true,
32457 /*template_p=*/NULL
,
32458 /*declarator_p=*/false,
32459 /*optional_p=*/false);
32460 if (name
== error_mark_node
)
32462 if (kind
== OMP_CLAUSE_DEPEND
32463 && cp_parser_simulate_error (parser
))
32464 goto depend_lvalue
;
32468 if (identifier_p (name
))
32469 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
32472 if (decl
== error_mark_node
)
32474 if (kind
== OMP_CLAUSE_DEPEND
32475 && cp_parser_simulate_error (parser
))
32476 goto depend_lvalue
;
32477 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
32481 if (decl
== error_mark_node
)
32483 else if (kind
!= 0)
32487 case OMP_CLAUSE__CACHE_
:
32488 /* The OpenACC cache directive explicitly only allows "array
32489 elements or subarrays". */
32490 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
32492 error_at (token
->location
, "expected %<[%>");
32493 decl
= error_mark_node
;
32497 case OMP_CLAUSE_MAP
:
32498 case OMP_CLAUSE_FROM
:
32499 case OMP_CLAUSE_TO
:
32500 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
32503 = cp_lexer_peek_token (parser
->lexer
)->location
;
32504 cp_id_kind idk
= CP_ID_KIND_NONE
;
32505 cp_lexer_consume_token (parser
->lexer
);
32506 decl
= convert_from_reference (decl
);
32508 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
32513 case OMP_CLAUSE_DEPEND
:
32514 case OMP_CLAUSE_REDUCTION
:
32515 case OMP_CLAUSE_IN_REDUCTION
:
32516 case OMP_CLAUSE_TASK_REDUCTION
:
32517 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
32519 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
32521 parser
->colon_corrects_to_scope_p
= false;
32522 cp_lexer_consume_token (parser
->lexer
);
32523 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32524 low_bound
= cp_parser_expression (parser
);
32526 parser
->colon_corrects_to_scope_p
32527 = saved_colon_corrects_to_scope_p
;
32528 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
32529 length
= integer_one_node
;
32532 /* Look for `:'. */
32533 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32535 if (kind
== OMP_CLAUSE_DEPEND
32536 && cp_parser_simulate_error (parser
))
32537 goto depend_lvalue
;
32540 if (kind
== OMP_CLAUSE_DEPEND
)
32541 cp_parser_commit_to_tentative_parse (parser
);
32542 if (!cp_lexer_next_token_is (parser
->lexer
,
32544 length
= cp_parser_expression (parser
);
32546 /* Look for the closing `]'. */
32547 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
32550 if (kind
== OMP_CLAUSE_DEPEND
32551 && cp_parser_simulate_error (parser
))
32552 goto depend_lvalue
;
32556 decl
= tree_cons (low_bound
, length
, decl
);
32563 if (kind
== OMP_CLAUSE_DEPEND
)
32565 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
32566 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
32567 && cp_parser_simulate_error (parser
))
32570 cp_parser_abort_tentative_parse (parser
);
32571 decl
= cp_parser_assignment_expression (parser
, NULL
,
32575 cp_parser_parse_definitely (parser
);
32578 tree u
= build_omp_clause (token
->location
, kind
);
32579 OMP_CLAUSE_DECL (u
) = decl
;
32580 OMP_CLAUSE_CHAIN (u
) = list
;
32584 list
= tree_cons (decl
, NULL_TREE
, list
);
32587 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
32589 cp_lexer_consume_token (parser
->lexer
);
32593 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32595 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
32598 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32602 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32606 /* Try to resync to an unnested comma. Copied from
32607 cp_parser_parenthesized_expression_list. */
32610 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32611 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
32612 /*recovering=*/true,
32614 /*consume_paren=*/true);
32622 /* Similarly, but expect leading and trailing parenthesis. This is a very
32623 common case for omp clauses. */
32626 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
32628 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32629 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
32634 copy ( variable-list )
32635 copyin ( variable-list )
32636 copyout ( variable-list )
32637 create ( variable-list )
32638 delete ( variable-list )
32639 present ( variable-list ) */
32642 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
32645 enum gomp_map_kind kind
;
32648 case PRAGMA_OACC_CLAUSE_COPY
:
32649 kind
= GOMP_MAP_TOFROM
;
32651 case PRAGMA_OACC_CLAUSE_COPYIN
:
32652 kind
= GOMP_MAP_TO
;
32654 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32655 kind
= GOMP_MAP_FROM
;
32657 case PRAGMA_OACC_CLAUSE_CREATE
:
32658 kind
= GOMP_MAP_ALLOC
;
32660 case PRAGMA_OACC_CLAUSE_DELETE
:
32661 kind
= GOMP_MAP_RELEASE
;
32663 case PRAGMA_OACC_CLAUSE_DEVICE
:
32664 kind
= GOMP_MAP_FORCE_TO
;
32666 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32667 kind
= GOMP_MAP_DEVICE_RESIDENT
;
32669 case PRAGMA_OACC_CLAUSE_HOST
:
32670 kind
= GOMP_MAP_FORCE_FROM
;
32672 case PRAGMA_OACC_CLAUSE_LINK
:
32673 kind
= GOMP_MAP_LINK
;
32675 case PRAGMA_OACC_CLAUSE_PRESENT
:
32676 kind
= GOMP_MAP_FORCE_PRESENT
;
32679 gcc_unreachable ();
32682 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
32684 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32685 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
32691 deviceptr ( variable-list ) */
32694 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
32696 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32699 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32700 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32701 variable-list must only allow for pointer variables. */
32702 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32703 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
32705 tree v
= TREE_PURPOSE (t
);
32706 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
32707 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
32708 OMP_CLAUSE_DECL (u
) = v
;
32709 OMP_CLAUSE_CHAIN (u
) = list
;
32724 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
32727 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
32729 tree c
= build_omp_clause (loc
, code
);
32730 OMP_CLAUSE_CHAIN (c
) = list
;
32736 num_gangs ( expression )
32737 num_workers ( expression )
32738 vector_length ( expression ) */
32741 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
32742 const char *str
, tree list
)
32744 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32746 matching_parens parens
;
32747 if (!parens
.require_open (parser
))
32750 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
32752 if (t
== error_mark_node
32753 || !parens
.require_close (parser
))
32755 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32756 /*or_comma=*/false,
32757 /*consume_paren=*/true);
32761 check_no_duplicate_clause (list
, code
, str
, loc
);
32763 tree c
= build_omp_clause (loc
, code
);
32764 OMP_CLAUSE_OPERAND (c
, 0) = t
;
32765 OMP_CLAUSE_CHAIN (c
) = list
;
32771 gang [( gang-arg-list )]
32772 worker [( [num:] int-expr )]
32773 vector [( [length:] int-expr )]
32775 where gang-arg is one of:
32780 and size-expr may be:
32787 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
32788 omp_clause_code kind
,
32789 const char *str
, tree list
)
32791 const char *id
= "num";
32792 cp_lexer
*lexer
= parser
->lexer
;
32793 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
32795 if (kind
== OMP_CLAUSE_VECTOR
)
32798 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
32800 matching_parens parens
;
32801 parens
.consume_open (parser
);
32805 cp_token
*next
= cp_lexer_peek_token (lexer
);
32808 /* Gang static argument. */
32809 if (kind
== OMP_CLAUSE_GANG
32810 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
32812 cp_lexer_consume_token (lexer
);
32814 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32815 goto cleanup_error
;
32818 if (ops
[idx
] != NULL
)
32820 cp_parser_error (parser
, "too many %<static%> arguments");
32821 goto cleanup_error
;
32824 /* Check for the '*' argument. */
32825 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
32826 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32827 || cp_lexer_nth_token_is (parser
->lexer
, 2,
32830 cp_lexer_consume_token (lexer
);
32831 ops
[idx
] = integer_minus_one_node
;
32833 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32835 cp_lexer_consume_token (lexer
);
32841 /* Worker num: argument and vector length: arguments. */
32842 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
32843 && id_equal (next
->u
.value
, id
)
32844 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
32846 cp_lexer_consume_token (lexer
); /* id */
32847 cp_lexer_consume_token (lexer
); /* ':' */
32850 /* Now collect the actual argument. */
32851 if (ops
[idx
] != NULL_TREE
)
32853 cp_parser_error (parser
, "unexpected argument");
32854 goto cleanup_error
;
32857 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32859 if (expr
== error_mark_node
)
32860 goto cleanup_error
;
32862 mark_exp_read (expr
);
32865 if (kind
== OMP_CLAUSE_GANG
32866 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32868 cp_lexer_consume_token (lexer
);
32875 if (!parens
.require_close (parser
))
32876 goto cleanup_error
;
32879 check_no_duplicate_clause (list
, kind
, str
, loc
);
32881 c
= build_omp_clause (loc
, kind
);
32884 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32886 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32887 OMP_CLAUSE_CHAIN (c
) = list
;
32892 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32897 tile ( size-expr-list ) */
32900 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32902 tree c
, expr
= error_mark_node
;
32903 tree tile
= NULL_TREE
;
32905 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32906 so, but the spec authors never considered such a case and have
32907 differing opinions on what it might mean, including 'not
32909 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32910 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32913 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32918 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32921 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32922 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32923 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32925 cp_lexer_consume_token (parser
->lexer
);
32926 expr
= integer_zero_node
;
32929 expr
= cp_parser_constant_expression (parser
);
32931 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32933 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32935 /* Consume the trailing ')'. */
32936 cp_lexer_consume_token (parser
->lexer
);
32938 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32939 tile
= nreverse (tile
);
32940 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32941 OMP_CLAUSE_CHAIN (c
) = list
;
32946 Parse wait clause or directive parameters. */
32949 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32951 vec
<tree
, va_gc
> *args
;
32954 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32956 /*allow_expansion_p=*/true,
32957 /*non_constant_p=*/NULL
);
32959 if (args
== NULL
|| args
->length () == 0)
32963 cp_parser_error (parser
, "expected integer expression list");
32964 release_tree_vector (args
);
32969 args_tree
= build_tree_list_vec (args
);
32971 release_tree_vector (args
);
32973 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32975 tree targ
= TREE_VALUE (t
);
32977 if (targ
!= error_mark_node
)
32979 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32980 error ("%<wait%> expression must be integral");
32983 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32985 targ
= mark_rvalue_use (targ
);
32986 OMP_CLAUSE_DECL (c
) = targ
;
32987 OMP_CLAUSE_CHAIN (c
) = list
;
32997 wait [( int-expr-list )] */
33000 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
33002 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33004 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33005 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
33008 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
33010 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33011 OMP_CLAUSE_CHAIN (c
) = list
;
33019 collapse ( constant-expression ) */
33022 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
33028 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33029 matching_parens parens
;
33030 if (!parens
.require_open (parser
))
33033 num
= cp_parser_constant_expression (parser
);
33035 if (!parens
.require_close (parser
))
33036 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33037 /*or_comma=*/false,
33038 /*consume_paren=*/true);
33040 if (num
== error_mark_node
)
33042 num
= fold_non_dependent_expr (num
);
33043 if (!tree_fits_shwi_p (num
)
33044 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33045 || (n
= tree_to_shwi (num
)) <= 0
33048 error_at (loc
, "collapse argument needs positive constant integer expression");
33052 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
33053 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
33054 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
33055 OMP_CLAUSE_CHAIN (c
) = list
;
33056 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
33062 default ( none | shared )
33065 default ( none | present ) */
33068 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
33069 location_t location
, bool is_oacc
)
33071 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
33074 matching_parens parens
;
33075 if (!parens
.require_open (parser
))
33077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33079 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33080 const char *p
= IDENTIFIER_POINTER (id
);
33085 if (strcmp ("none", p
) != 0)
33087 kind
= OMP_CLAUSE_DEFAULT_NONE
;
33091 if (strcmp ("present", p
) != 0 || !is_oacc
)
33093 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
33097 if (strcmp ("shared", p
) != 0 || is_oacc
)
33099 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
33106 cp_lexer_consume_token (parser
->lexer
);
33112 cp_parser_error (parser
, "expected %<none%> or %<present%>");
33114 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
33117 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
33118 || !parens
.require_close (parser
))
33119 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33120 /*or_comma=*/false,
33121 /*consume_paren=*/true);
33123 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
33126 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
33127 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
33128 OMP_CLAUSE_CHAIN (c
) = list
;
33129 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
33135 final ( expression ) */
33138 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
33142 matching_parens parens
;
33143 if (!parens
.require_open (parser
))
33146 t
= cp_parser_assignment_expression (parser
);
33148 if (t
== error_mark_node
33149 || !parens
.require_close (parser
))
33150 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33151 /*or_comma=*/false,
33152 /*consume_paren=*/true);
33154 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
33156 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
33157 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
33158 OMP_CLAUSE_CHAIN (c
) = list
;
33167 if ( directive-name-modifier : expression )
33169 directive-name-modifier:
33170 parallel | task | taskloop | target data | target | target update
33171 | target enter data | target exit data
33174 directive-name-modifier:
33175 ... | simd | cancel */
33178 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
33182 enum tree_code if_modifier
= ERROR_MARK
;
33184 matching_parens parens
;
33185 if (!parens
.require_open (parser
))
33188 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33190 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33191 const char *p
= IDENTIFIER_POINTER (id
);
33194 if (strcmp ("cancel", p
) == 0)
33195 if_modifier
= VOID_CST
;
33196 else if (strcmp ("parallel", p
) == 0)
33197 if_modifier
= OMP_PARALLEL
;
33198 else if (strcmp ("simd", p
) == 0)
33199 if_modifier
= OMP_SIMD
;
33200 else if (strcmp ("task", p
) == 0)
33201 if_modifier
= OMP_TASK
;
33202 else if (strcmp ("taskloop", p
) == 0)
33203 if_modifier
= OMP_TASKLOOP
;
33204 else if (strcmp ("target", p
) == 0)
33206 if_modifier
= OMP_TARGET
;
33207 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
33209 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
33210 p
= IDENTIFIER_POINTER (id
);
33211 if (strcmp ("data", p
) == 0)
33212 if_modifier
= OMP_TARGET_DATA
;
33213 else if (strcmp ("update", p
) == 0)
33214 if_modifier
= OMP_TARGET_UPDATE
;
33215 else if (strcmp ("enter", p
) == 0)
33216 if_modifier
= OMP_TARGET_ENTER_DATA
;
33217 else if (strcmp ("exit", p
) == 0)
33218 if_modifier
= OMP_TARGET_EXIT_DATA
;
33219 if (if_modifier
!= OMP_TARGET
)
33224 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
33225 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
33227 if_modifier
= ERROR_MARK
;
33229 if (if_modifier
== OMP_TARGET_ENTER_DATA
33230 || if_modifier
== OMP_TARGET_EXIT_DATA
)
33232 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
33234 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
33235 p
= IDENTIFIER_POINTER (id
);
33236 if (strcmp ("data", p
) == 0)
33242 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
33243 error_at (loc
, "expected %<data%>");
33244 if_modifier
= ERROR_MARK
;
33249 if (if_modifier
!= ERROR_MARK
)
33251 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
33254 cp_lexer_consume_token (parser
->lexer
);
33261 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
33262 error_at (loc
, "expected %<:%>");
33264 if_modifier
= ERROR_MARK
;
33269 t
= cp_parser_assignment_expression (parser
);
33271 if (t
== error_mark_node
33272 || !parens
.require_close (parser
))
33273 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33274 /*or_comma=*/false,
33275 /*consume_paren=*/true);
33277 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33278 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
33280 if (if_modifier
!= ERROR_MARK
33281 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33283 const char *p
= NULL
;
33284 switch (if_modifier
)
33286 case VOID_CST
: p
= "cancel"; break;
33287 case OMP_PARALLEL
: p
= "parallel"; break;
33288 case OMP_SIMD
: p
= "simd"; break;
33289 case OMP_TASK
: p
= "task"; break;
33290 case OMP_TASKLOOP
: p
= "taskloop"; break;
33291 case OMP_TARGET_DATA
: p
= "target data"; break;
33292 case OMP_TARGET
: p
= "target"; break;
33293 case OMP_TARGET_UPDATE
: p
= "target update"; break;
33294 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
33295 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
33296 default: gcc_unreachable ();
33298 error_at (location
, "too many %<if%> clauses with %qs modifier",
33302 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
33305 error_at (location
, "too many %<if%> clauses");
33307 error_at (location
, "too many %<if%> clauses without modifier");
33310 else if (if_modifier
== ERROR_MARK
33311 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
33313 error_at (location
, "if any %<if%> clause has modifier, then all "
33314 "%<if%> clauses have to use modifier");
33319 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
33320 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
33321 OMP_CLAUSE_IF_EXPR (c
) = t
;
33322 OMP_CLAUSE_CHAIN (c
) = list
;
33331 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
33332 tree list
, location_t location
)
33336 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
33339 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
33340 OMP_CLAUSE_CHAIN (c
) = list
;
33348 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
33349 tree list
, location_t location
)
33353 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
33355 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
33356 OMP_CLAUSE_CHAIN (c
) = list
;
33361 num_threads ( expression ) */
33364 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
33365 location_t location
)
33369 matching_parens parens
;
33370 if (!parens
.require_open (parser
))
33373 t
= cp_parser_assignment_expression (parser
);
33375 if (t
== error_mark_node
33376 || !parens
.require_close (parser
))
33377 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33378 /*or_comma=*/false,
33379 /*consume_paren=*/true);
33381 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
33382 "num_threads", location
);
33384 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
33385 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
33386 OMP_CLAUSE_CHAIN (c
) = list
;
33392 num_tasks ( expression ) */
33395 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
33396 location_t location
)
33400 matching_parens parens
;
33401 if (!parens
.require_open (parser
))
33404 t
= cp_parser_assignment_expression (parser
);
33406 if (t
== error_mark_node
33407 || !parens
.require_close (parser
))
33408 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33409 /*or_comma=*/false,
33410 /*consume_paren=*/true);
33412 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
33413 "num_tasks", location
);
33415 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
33416 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
33417 OMP_CLAUSE_CHAIN (c
) = list
;
33423 grainsize ( expression ) */
33426 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
33427 location_t location
)
33431 matching_parens parens
;
33432 if (!parens
.require_open (parser
))
33435 t
= cp_parser_assignment_expression (parser
);
33437 if (t
== error_mark_node
33438 || !parens
.require_close (parser
))
33439 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33440 /*or_comma=*/false,
33441 /*consume_paren=*/true);
33443 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
33444 "grainsize", location
);
33446 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
33447 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
33448 OMP_CLAUSE_CHAIN (c
) = list
;
33454 priority ( expression ) */
33457 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
33458 location_t location
)
33462 matching_parens parens
;
33463 if (!parens
.require_open (parser
))
33466 t
= cp_parser_assignment_expression (parser
);
33468 if (t
== error_mark_node
33469 || !parens
.require_close (parser
))
33470 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33471 /*or_comma=*/false,
33472 /*consume_paren=*/true);
33474 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
33475 "priority", location
);
33477 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
33478 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
33479 OMP_CLAUSE_CHAIN (c
) = list
;
33485 hint ( expression ) */
33488 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
33492 matching_parens parens
;
33493 if (!parens
.require_open (parser
))
33496 t
= cp_parser_assignment_expression (parser
);
33498 if (t
== error_mark_node
33499 || !parens
.require_close (parser
))
33500 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33501 /*or_comma=*/false,
33502 /*consume_paren=*/true);
33504 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
33506 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
33507 OMP_CLAUSE_HINT_EXPR (c
) = t
;
33508 OMP_CLAUSE_CHAIN (c
) = list
;
33514 defaultmap ( tofrom : scalar )
33517 defaultmap ( implicit-behavior [ : variable-category ] ) */
33520 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
33521 location_t location
)
33525 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33526 enum omp_clause_defaultmap_kind category
33527 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
33529 matching_parens parens
;
33530 if (!parens
.require_open (parser
))
33533 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33535 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33538 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
33539 "%<tofrom%>, %<firstprivate%>, %<none%> "
33545 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33546 p
= IDENTIFIER_POINTER (id
);
33552 if (strcmp ("alloc", p
) == 0)
33553 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
33555 goto invalid_behavior
;
33559 if (strcmp ("default", p
) == 0)
33560 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
33562 goto invalid_behavior
;
33566 if (strcmp ("firstprivate", p
) == 0)
33567 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
33568 else if (strcmp ("from", p
) == 0)
33569 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
33571 goto invalid_behavior
;
33575 if (strcmp ("none", p
) == 0)
33576 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
33578 goto invalid_behavior
;
33582 if (strcmp ("tofrom", p
) == 0)
33583 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
33584 else if (strcmp ("to", p
) == 0)
33585 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
33587 goto invalid_behavior
;
33591 goto invalid_behavior
;
33593 cp_lexer_consume_token (parser
->lexer
);
33595 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33597 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33600 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33603 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
33607 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33608 p
= IDENTIFIER_POINTER (id
);
33613 if (strcmp ("aggregate", p
) == 0)
33614 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
33616 goto invalid_category
;
33620 if (strcmp ("pointer", p
) == 0)
33621 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
33623 goto invalid_category
;
33627 if (strcmp ("scalar", p
) == 0)
33628 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
33630 goto invalid_category
;
33634 goto invalid_category
;
33637 cp_lexer_consume_token (parser
->lexer
);
33639 if (!parens
.require_close (parser
))
33642 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33643 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
33644 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33645 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
33646 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
33647 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
33649 enum omp_clause_defaultmap_kind cat
= category
;
33650 location_t loc
= OMP_CLAUSE_LOCATION (c
);
33651 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
33652 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
33656 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
33659 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
33662 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
33665 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
33669 gcc_unreachable ();
33672 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
33675 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
33680 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
33681 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
33682 OMP_CLAUSE_CHAIN (c
) = list
;
33686 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33687 /*or_comma=*/false,
33688 /*consume_paren=*/true);
33696 ordered ( constant-expression ) */
33699 cp_parser_omp_clause_ordered (cp_parser
*parser
,
33700 tree list
, location_t location
)
33702 tree c
, num
= NULL_TREE
;
33705 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
33706 "ordered", location
);
33708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33710 matching_parens parens
;
33711 parens
.consume_open (parser
);
33713 num
= cp_parser_constant_expression (parser
);
33715 if (!parens
.require_close (parser
))
33716 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33717 /*or_comma=*/false,
33718 /*consume_paren=*/true);
33720 if (num
== error_mark_node
)
33722 num
= fold_non_dependent_expr (num
);
33723 if (!tree_fits_shwi_p (num
)
33724 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33725 || (n
= tree_to_shwi (num
)) <= 0
33728 error_at (location
,
33729 "ordered argument needs positive constant integer "
33735 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
33736 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
33737 OMP_CLAUSE_CHAIN (c
) = list
;
33742 reduction ( reduction-operator : variable-list )
33744 reduction-operator:
33745 One of: + * - & ^ | && ||
33749 reduction-operator:
33750 One of: + * - & ^ | && || min max
33754 reduction-operator:
33755 One of: + * - & ^ | && ||
33759 reduction ( reduction-modifier, reduction-operator : variable-list )
33760 in_reduction ( reduction-operator : variable-list )
33761 task_reduction ( reduction-operator : variable-list ) */
33764 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
33765 bool is_omp
, tree list
)
33767 enum tree_code code
= ERROR_MARK
;
33768 tree nlist
, c
, id
= NULL_TREE
;
33770 bool inscan
= false;
33772 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33775 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
33777 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
33778 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33780 cp_lexer_consume_token (parser
->lexer
);
33781 cp_lexer_consume_token (parser
->lexer
);
33783 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33784 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
33786 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33787 const char *p
= IDENTIFIER_POINTER (id
);
33788 if (strcmp (p
, "task") == 0)
33790 else if (strcmp (p
, "inscan") == 0)
33793 sorry ("%<inscan%> modifier on %<reduction%> clause "
33794 "not supported yet");
33796 if (task
|| inscan
)
33798 cp_lexer_consume_token (parser
->lexer
);
33799 cp_lexer_consume_token (parser
->lexer
);
33804 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
33806 case CPP_PLUS
: code
= PLUS_EXPR
; break;
33807 case CPP_MULT
: code
= MULT_EXPR
; break;
33808 case CPP_MINUS
: code
= MINUS_EXPR
; break;
33809 case CPP_AND
: code
= BIT_AND_EXPR
; break;
33810 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
33811 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
33812 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
33813 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
33817 if (code
!= ERROR_MARK
)
33818 cp_lexer_consume_token (parser
->lexer
);
33821 bool saved_colon_corrects_to_scope_p
;
33822 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33823 parser
->colon_corrects_to_scope_p
= false;
33824 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
33825 /*check_dependency_p=*/true,
33826 /*template_p=*/NULL
,
33827 /*declarator_p=*/false,
33828 /*optional_p=*/false);
33829 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33830 if (identifier_p (id
))
33832 const char *p
= IDENTIFIER_POINTER (id
);
33834 if (strcmp (p
, "min") == 0)
33836 else if (strcmp (p
, "max") == 0)
33838 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
33840 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
33842 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
33844 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
33845 code
= BIT_AND_EXPR
;
33846 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
33847 code
= BIT_IOR_EXPR
;
33848 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
33849 code
= BIT_XOR_EXPR
;
33850 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
33851 code
= TRUTH_ANDIF_EXPR
;
33852 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
33853 code
= TRUTH_ORIF_EXPR
;
33854 id
= omp_reduction_id (code
, id
, NULL_TREE
);
33855 tree scope
= parser
->scope
;
33857 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
33858 parser
->scope
= NULL_TREE
;
33859 parser
->qualifying_scope
= NULL_TREE
;
33860 parser
->object_scope
= NULL_TREE
;
33864 error ("invalid reduction-identifier");
33866 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33867 /*or_comma=*/false,
33868 /*consume_paren=*/true);
33873 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33876 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
33878 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33880 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
33882 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
33884 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
33885 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
33892 schedule ( schedule-kind )
33893 schedule ( schedule-kind , expression )
33896 static | dynamic | guided | runtime | auto
33899 schedule ( schedule-modifier : schedule-kind )
33900 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33908 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
33911 int modifiers
= 0, nmodifiers
= 0;
33913 matching_parens parens
;
33914 if (!parens
.require_open (parser
))
33917 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
33919 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33921 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33922 const char *p
= IDENTIFIER_POINTER (id
);
33923 if (strcmp ("simd", p
) == 0)
33924 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
33925 else if (strcmp ("monotonic", p
) == 0)
33926 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
33927 else if (strcmp ("nonmonotonic", p
) == 0)
33928 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
33931 cp_lexer_consume_token (parser
->lexer
);
33932 if (nmodifiers
++ == 0
33933 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33934 cp_lexer_consume_token (parser
->lexer
);
33937 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33944 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33945 const char *p
= IDENTIFIER_POINTER (id
);
33950 if (strcmp ("dynamic", p
) != 0)
33952 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
33956 if (strcmp ("guided", p
) != 0)
33958 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
33962 if (strcmp ("runtime", p
) != 0)
33964 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
33971 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33972 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
33973 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33974 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
33977 cp_lexer_consume_token (parser
->lexer
);
33979 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
33980 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33981 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33982 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
33984 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33992 cp_lexer_consume_token (parser
->lexer
);
33994 token
= cp_lexer_peek_token (parser
->lexer
);
33995 t
= cp_parser_assignment_expression (parser
);
33997 if (t
== error_mark_node
)
33999 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
34000 error_at (token
->location
, "schedule %<runtime%> does not take "
34001 "a %<chunk_size%> parameter");
34002 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
34003 error_at (token
->location
, "schedule %<auto%> does not take "
34004 "a %<chunk_size%> parameter");
34006 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
34008 if (!parens
.require_close (parser
))
34011 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34014 OMP_CLAUSE_SCHEDULE_KIND (c
)
34015 = (enum omp_clause_schedule_kind
)
34016 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
34018 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
34019 OMP_CLAUSE_CHAIN (c
) = list
;
34023 cp_parser_error (parser
, "invalid schedule kind");
34025 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34026 /*or_comma=*/false,
34027 /*consume_paren=*/true);
34035 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
34036 tree list
, location_t location
)
34040 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
34042 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
34043 OMP_CLAUSE_CHAIN (c
) = list
;
34052 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
34053 tree list
, location_t location
)
34055 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
34056 tree c
= build_omp_clause (location
, code
);
34057 OMP_CLAUSE_CHAIN (c
) = list
;
34068 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
34069 enum omp_clause_code code
,
34070 tree list
, location_t location
)
34072 tree c
= build_omp_clause (location
, code
);
34073 OMP_CLAUSE_CHAIN (c
) = list
;
34081 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
34082 tree list
, location_t location
)
34084 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
34085 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
34086 OMP_CLAUSE_CHAIN (c
) = list
;
34095 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
34096 enum omp_clause_code code
,
34097 tree list
, location_t location
)
34099 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
34100 tree c
= build_omp_clause (location
, code
);
34101 OMP_CLAUSE_CHAIN (c
) = list
;
34106 num_teams ( expression ) */
34109 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
34110 location_t location
)
34114 matching_parens parens
;
34115 if (!parens
.require_open (parser
))
34118 t
= cp_parser_assignment_expression (parser
);
34120 if (t
== error_mark_node
34121 || !parens
.require_close (parser
))
34122 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34123 /*or_comma=*/false,
34124 /*consume_paren=*/true);
34126 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
34127 "num_teams", location
);
34129 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
34130 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
34131 OMP_CLAUSE_CHAIN (c
) = list
;
34137 thread_limit ( expression ) */
34140 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
34141 location_t location
)
34145 matching_parens parens
;
34146 if (!parens
.require_open (parser
))
34149 t
= cp_parser_assignment_expression (parser
);
34151 if (t
== error_mark_node
34152 || !parens
.require_close (parser
))
34153 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34154 /*or_comma=*/false,
34155 /*consume_paren=*/true);
34157 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
34158 "thread_limit", location
);
34160 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
34161 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
34162 OMP_CLAUSE_CHAIN (c
) = list
;
34168 aligned ( variable-list )
34169 aligned ( variable-list : constant-expression ) */
34172 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
34174 tree nlist
, c
, alignment
= NULL_TREE
;
34177 matching_parens parens
;
34178 if (!parens
.require_open (parser
))
34181 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
34186 alignment
= cp_parser_constant_expression (parser
);
34188 if (!parens
.require_close (parser
))
34189 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34190 /*or_comma=*/false,
34191 /*consume_paren=*/true);
34193 if (alignment
== error_mark_node
)
34194 alignment
= NULL_TREE
;
34197 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34198 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
34204 lastprivate ( variable-list )
34207 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34210 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
34212 bool conditional
= false;
34214 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34218 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
34220 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34221 const char *p
= IDENTIFIER_POINTER (id
);
34223 if (strcmp ("conditional", p
) == 0)
34225 conditional
= true;
34226 cp_lexer_consume_token (parser
->lexer
);
34227 cp_lexer_consume_token (parser
->lexer
);
34231 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
34235 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34236 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
34241 linear ( variable-list )
34242 linear ( variable-list : expression )
34245 linear ( modifier ( variable-list ) )
34246 linear ( modifier ( variable-list ) : expression ) */
34249 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
34252 tree nlist
, c
, step
= integer_one_node
;
34254 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34256 matching_parens parens
;
34257 if (!parens
.require_open (parser
))
34260 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34262 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34263 const char *p
= IDENTIFIER_POINTER (id
);
34265 if (strcmp ("ref", p
) == 0)
34266 kind
= OMP_CLAUSE_LINEAR_REF
;
34267 else if (strcmp ("val", p
) == 0)
34268 kind
= OMP_CLAUSE_LINEAR_VAL
;
34269 else if (strcmp ("uval", p
) == 0)
34270 kind
= OMP_CLAUSE_LINEAR_UVAL
;
34271 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
34272 cp_lexer_consume_token (parser
->lexer
);
34274 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
34277 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
34278 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
34282 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
34283 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
34285 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34286 else if (!parens
.require_close (parser
))
34287 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34288 /*or_comma=*/false,
34289 /*consume_paren=*/true);
34296 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34297 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
34299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34300 cp_parser_parse_tentatively (parser
);
34301 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
34302 /*check_dependency_p=*/true,
34303 /*template_p=*/NULL
,
34304 /*declarator_p=*/false,
34305 /*optional_p=*/false);
34306 if (step
!= error_mark_node
)
34307 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
34308 if (step
== error_mark_node
)
34311 cp_parser_abort_tentative_parse (parser
);
34313 else if (!cp_parser_parse_definitely (parser
))
34317 step
= cp_parser_assignment_expression (parser
);
34319 if (!parens
.require_close (parser
))
34320 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34321 /*or_comma=*/false,
34322 /*consume_paren=*/true);
34324 if (step
== error_mark_node
)
34328 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34330 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
34331 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
34338 safelen ( constant-expression ) */
34341 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
34342 location_t location
)
34346 matching_parens parens
;
34347 if (!parens
.require_open (parser
))
34350 t
= cp_parser_constant_expression (parser
);
34352 if (t
== error_mark_node
34353 || !parens
.require_close (parser
))
34354 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34355 /*or_comma=*/false,
34356 /*consume_paren=*/true);
34358 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
34360 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
34361 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
34362 OMP_CLAUSE_CHAIN (c
) = list
;
34368 simdlen ( constant-expression ) */
34371 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
34372 location_t location
)
34376 matching_parens parens
;
34377 if (!parens
.require_open (parser
))
34380 t
= cp_parser_constant_expression (parser
);
34382 if (t
== error_mark_node
34383 || !parens
.require_close (parser
))
34384 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34385 /*or_comma=*/false,
34386 /*consume_paren=*/true);
34388 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
34390 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
34391 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
34392 OMP_CLAUSE_CHAIN (c
) = list
;
34399 identifier [+/- integer]
34400 vec , identifier [+/- integer]
34404 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
34409 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34411 cp_parser_error (parser
, "expected identifier");
34415 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34417 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34418 tree t
, identifier
= cp_parser_identifier (parser
);
34419 tree addend
= NULL
;
34421 if (identifier
== error_mark_node
)
34422 t
= error_mark_node
;
34425 t
= cp_parser_lookup_name_simple
34426 (parser
, identifier
,
34427 cp_lexer_peek_token (parser
->lexer
)->location
);
34428 if (t
== error_mark_node
)
34429 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
34434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
34436 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
34438 addend
= integer_zero_node
;
34439 goto add_to_vector
;
34441 cp_lexer_consume_token (parser
->lexer
);
34443 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
34445 cp_parser_error (parser
, "expected integer");
34449 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34450 if (TREE_CODE (addend
) != INTEGER_CST
)
34452 cp_parser_error (parser
, "expected integer");
34455 cp_lexer_consume_token (parser
->lexer
);
34458 if (t
!= error_mark_node
)
34460 vec
= tree_cons (addend
, t
, vec
);
34462 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
34465 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34468 cp_lexer_consume_token (parser
->lexer
);
34471 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
34473 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
34474 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
34475 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
34476 OMP_CLAUSE_CHAIN (u
) = list
;
34483 iterators ( iterators-definition )
34485 iterators-definition:
34487 iterator-specifier , iterators-definition
34489 iterator-specifier:
34490 identifier = range-specification
34491 iterator-type identifier = range-specification
34493 range-specification:
34495 begin : end : step */
34498 cp_parser_omp_iterators (cp_parser
*parser
)
34500 tree ret
= NULL_TREE
, *last
= &ret
;
34501 cp_lexer_consume_token (parser
->lexer
);
34503 matching_parens parens
;
34504 if (!parens
.require_open (parser
))
34505 return error_mark_node
;
34507 bool saved_colon_corrects_to_scope_p
34508 = parser
->colon_corrects_to_scope_p
;
34509 bool saved_colon_doesnt_start_class_def_p
34510 = parser
->colon_doesnt_start_class_def_p
;
34515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34516 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
34517 iter_type
= integer_type_node
;
34520 const char *saved_message
34521 = parser
->type_definition_forbidden_message
;
34522 parser
->type_definition_forbidden_message
34523 = G_("types may not be defined in iterator type");
34525 iter_type
= cp_parser_type_id (parser
);
34527 parser
->type_definition_forbidden_message
= saved_message
;
34530 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34531 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34533 cp_parser_error (parser
, "expected identifier");
34537 tree id
= cp_parser_identifier (parser
);
34538 if (id
== error_mark_node
)
34541 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34544 parser
->colon_corrects_to_scope_p
= false;
34545 parser
->colon_doesnt_start_class_def_p
= true;
34546 tree begin
= cp_parser_assignment_expression (parser
);
34548 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34551 tree end
= cp_parser_assignment_expression (parser
);
34553 tree step
= integer_one_node
;
34554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34556 cp_lexer_consume_token (parser
->lexer
);
34557 step
= cp_parser_assignment_expression (parser
);
34560 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
34561 DECL_ARTIFICIAL (iter_var
) = 1;
34562 DECL_CONTEXT (iter_var
) = current_function_decl
;
34563 pushdecl (iter_var
);
34565 *last
= make_tree_vec (6);
34566 TREE_VEC_ELT (*last
, 0) = iter_var
;
34567 TREE_VEC_ELT (*last
, 1) = begin
;
34568 TREE_VEC_ELT (*last
, 2) = end
;
34569 TREE_VEC_ELT (*last
, 3) = step
;
34570 last
= &TREE_CHAIN (*last
);
34572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34574 cp_lexer_consume_token (parser
->lexer
);
34581 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34582 parser
->colon_doesnt_start_class_def_p
34583 = saved_colon_doesnt_start_class_def_p
;
34585 if (!parens
.require_close (parser
))
34586 cp_parser_skip_to_closing_parenthesis (parser
,
34587 /*recovering=*/true,
34588 /*or_comma=*/false,
34589 /*consume_paren=*/true);
34591 return ret
? ret
: error_mark_node
;
34595 depend ( depend-kind : variable-list )
34603 depend ( sink : vec )
34606 depend ( depend-modifier , depend-kind: variable-list )
34609 in | out | inout | mutexinoutset | depobj
34612 iterator ( iterators-definition ) */
34615 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
34617 tree nlist
, c
, iterators
= NULL_TREE
;
34618 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
34620 matching_parens parens
;
34621 if (!parens
.require_open (parser
))
34626 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34629 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34630 const char *p
= IDENTIFIER_POINTER (id
);
34632 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
34634 begin_scope (sk_omp
, NULL
);
34635 iterators
= cp_parser_omp_iterators (parser
);
34636 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
34639 if (strcmp ("in", p
) == 0)
34640 kind
= OMP_CLAUSE_DEPEND_IN
;
34641 else if (strcmp ("inout", p
) == 0)
34642 kind
= OMP_CLAUSE_DEPEND_INOUT
;
34643 else if (strcmp ("mutexinoutset", p
) == 0)
34644 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
34645 else if (strcmp ("out", p
) == 0)
34646 kind
= OMP_CLAUSE_DEPEND_OUT
;
34647 else if (strcmp ("depobj", p
) == 0)
34648 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
34649 else if (strcmp ("sink", p
) == 0)
34650 kind
= OMP_CLAUSE_DEPEND_SINK
;
34651 else if (strcmp ("source", p
) == 0)
34652 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
34659 cp_lexer_consume_token (parser
->lexer
);
34662 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
34664 poplevel (0, 1, 0);
34665 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
34666 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
34667 iterators
= NULL_TREE
;
34670 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
34672 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
34673 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34674 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
34675 OMP_CLAUSE_CHAIN (c
) = list
;
34676 if (!parens
.require_close (parser
))
34677 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34678 /*or_comma=*/false,
34679 /*consume_paren=*/true);
34683 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34686 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
34687 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
34690 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
34695 tree block
= poplevel (1, 1, 0);
34696 if (iterators
== error_mark_node
)
34697 iterators
= NULL_TREE
;
34699 TREE_VEC_ELT (iterators
, 5) = block
;
34702 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34704 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
34706 OMP_CLAUSE_DECL (c
)
34707 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
34713 cp_parser_error (parser
, "invalid depend kind");
34716 poplevel (0, 1, 0);
34717 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34718 /*or_comma=*/false,
34719 /*consume_paren=*/true);
34724 map ( map-kind : variable-list )
34725 map ( variable-list )
34728 alloc | to | from | tofrom
34732 alloc | to | from | tofrom | release | delete
34734 map ( always [,] map-kind: variable-list ) */
34737 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
34740 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
34741 bool always
= false;
34743 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34748 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34749 const char *p
= IDENTIFIER_POINTER (id
);
34751 if (strcmp ("always", p
) == 0)
34754 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
34756 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
34757 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
34759 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
34763 cp_lexer_consume_token (parser
->lexer
);
34765 cp_lexer_consume_token (parser
->lexer
);
34770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34771 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34773 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34774 const char *p
= IDENTIFIER_POINTER (id
);
34776 if (strcmp ("alloc", p
) == 0)
34777 kind
= GOMP_MAP_ALLOC
;
34778 else if (strcmp ("to", p
) == 0)
34779 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
34780 else if (strcmp ("from", p
) == 0)
34781 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
34782 else if (strcmp ("tofrom", p
) == 0)
34783 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
34784 else if (strcmp ("release", p
) == 0)
34785 kind
= GOMP_MAP_RELEASE
;
34788 cp_parser_error (parser
, "invalid map kind");
34789 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34790 /*or_comma=*/false,
34791 /*consume_paren=*/true);
34794 cp_lexer_consume_token (parser
->lexer
);
34795 cp_lexer_consume_token (parser
->lexer
);
34797 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
34798 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
34800 kind
= GOMP_MAP_DELETE
;
34801 cp_lexer_consume_token (parser
->lexer
);
34802 cp_lexer_consume_token (parser
->lexer
);
34805 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
34808 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34809 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34815 device ( expression ) */
34818 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
34819 location_t location
)
34823 matching_parens parens
;
34824 if (!parens
.require_open (parser
))
34827 t
= cp_parser_assignment_expression (parser
);
34829 if (t
== error_mark_node
34830 || !parens
.require_close (parser
))
34831 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34832 /*or_comma=*/false,
34833 /*consume_paren=*/true);
34835 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
34836 "device", location
);
34838 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
34839 OMP_CLAUSE_DEVICE_ID (c
) = t
;
34840 OMP_CLAUSE_CHAIN (c
) = list
;
34846 dist_schedule ( static )
34847 dist_schedule ( static , expression ) */
34850 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
34851 location_t location
)
34855 matching_parens parens
;
34856 if (!parens
.require_open (parser
))
34859 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
34861 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34863 cp_lexer_consume_token (parser
->lexer
);
34865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34867 cp_lexer_consume_token (parser
->lexer
);
34869 t
= cp_parser_assignment_expression (parser
);
34871 if (t
== error_mark_node
)
34873 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
34875 if (!parens
.require_close (parser
))
34878 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34881 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
34883 OMP_CLAUSE_CHAIN (c
) = list
;
34887 cp_parser_error (parser
, "invalid dist_schedule kind");
34889 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34890 /*or_comma=*/false,
34891 /*consume_paren=*/true);
34896 proc_bind ( proc-bind-kind )
34899 master | close | spread */
34902 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
34903 location_t location
)
34906 enum omp_clause_proc_bind_kind kind
;
34908 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34913 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34914 const char *p
= IDENTIFIER_POINTER (id
);
34916 if (strcmp ("master", p
) == 0)
34917 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
34918 else if (strcmp ("close", p
) == 0)
34919 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
34920 else if (strcmp ("spread", p
) == 0)
34921 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
34928 cp_lexer_consume_token (parser
->lexer
);
34929 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
34932 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
34933 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
34935 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
34936 OMP_CLAUSE_CHAIN (c
) = list
;
34940 cp_parser_error (parser
, "invalid depend kind");
34942 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34943 /*or_comma=*/false,
34944 /*consume_paren=*/true);
34949 async [( int-expr )] */
34952 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
34955 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34957 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34959 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34961 matching_parens parens
;
34962 parens
.consume_open (parser
);
34964 t
= cp_parser_expression (parser
);
34965 if (t
== error_mark_node
34966 || !parens
.require_close (parser
))
34967 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34968 /*or_comma=*/false,
34969 /*consume_paren=*/true);
34972 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
34974 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
34975 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
34976 OMP_CLAUSE_CHAIN (c
) = list
;
34982 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34983 is a bitmask in MASK. Return the list of clauses found. */
34986 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
34987 const char *where
, cp_token
*pragma_tok
,
34988 bool finish_p
= true)
34990 tree clauses
= NULL
;
34993 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34996 pragma_omp_clause c_kind
;
34997 omp_clause_code code
;
34998 const char *c_name
;
34999 tree prev
= clauses
;
35001 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35002 cp_lexer_consume_token (parser
->lexer
);
35004 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
35005 c_kind
= cp_parser_omp_clause_name (parser
);
35009 case PRAGMA_OACC_CLAUSE_ASYNC
:
35010 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
35013 case PRAGMA_OACC_CLAUSE_AUTO
:
35014 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
35018 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
35019 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
35020 c_name
= "collapse";
35022 case PRAGMA_OACC_CLAUSE_COPY
:
35023 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35026 case PRAGMA_OACC_CLAUSE_COPYIN
:
35027 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35030 case PRAGMA_OACC_CLAUSE_COPYOUT
:
35031 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35032 c_name
= "copyout";
35034 case PRAGMA_OACC_CLAUSE_CREATE
:
35035 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35038 case PRAGMA_OACC_CLAUSE_DELETE
:
35039 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35042 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35043 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
35044 c_name
= "default";
35046 case PRAGMA_OACC_CLAUSE_DEVICE
:
35047 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35050 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
35051 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
35052 c_name
= "deviceptr";
35054 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
35055 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35056 c_name
= "device_resident";
35058 case PRAGMA_OACC_CLAUSE_FINALIZE
:
35059 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
35061 c_name
= "finalize";
35063 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
35064 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35066 c_name
= "firstprivate";
35068 case PRAGMA_OACC_CLAUSE_GANG
:
35070 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
35073 case PRAGMA_OACC_CLAUSE_HOST
:
35074 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35077 case PRAGMA_OACC_CLAUSE_IF
:
35078 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
35081 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
35082 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
35084 c_name
= "if_present";
35086 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
35087 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
35089 c_name
= "independent";
35091 case PRAGMA_OACC_CLAUSE_LINK
:
35092 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35095 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
35096 code
= OMP_CLAUSE_NUM_GANGS
;
35097 c_name
= "num_gangs";
35098 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35101 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
35102 c_name
= "num_workers";
35103 code
= OMP_CLAUSE_NUM_WORKERS
;
35104 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35107 case PRAGMA_OACC_CLAUSE_PRESENT
:
35108 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
35109 c_name
= "present";
35111 case PRAGMA_OACC_CLAUSE_PRIVATE
:
35112 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35114 c_name
= "private";
35116 case PRAGMA_OACC_CLAUSE_REDUCTION
:
35118 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35120 c_name
= "reduction";
35122 case PRAGMA_OACC_CLAUSE_SEQ
:
35123 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
35127 case PRAGMA_OACC_CLAUSE_TILE
:
35128 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
35131 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
35132 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35134 c_name
= "use_device";
35136 case PRAGMA_OACC_CLAUSE_VECTOR
:
35138 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
35142 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
35143 c_name
= "vector_length";
35144 code
= OMP_CLAUSE_VECTOR_LENGTH
;
35145 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
35148 case PRAGMA_OACC_CLAUSE_WAIT
:
35149 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
35152 case PRAGMA_OACC_CLAUSE_WORKER
:
35154 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
35159 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
35165 if (((mask
>> c_kind
) & 1) == 0)
35167 /* Remove the invalid clause(s) from the list to avoid
35168 confusing the rest of the compiler. */
35170 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
35175 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35178 return finish_omp_clauses (clauses
, C_ORT_ACC
);
35183 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35184 is a bitmask in MASK. Return the list of clauses found; the result
35185 of clause default goes in *pdefault. */
35188 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
35189 const char *where
, cp_token
*pragma_tok
,
35190 bool finish_p
= true)
35192 tree clauses
= NULL
;
35194 cp_token
*token
= NULL
;
35196 /* Don't create location wrapper nodes within OpenMP clauses. */
35197 auto_suppress_location_wrappers sentinel
;
35199 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35201 pragma_omp_clause c_kind
;
35202 const char *c_name
;
35203 tree prev
= clauses
;
35205 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35206 cp_lexer_consume_token (parser
->lexer
);
35208 token
= cp_lexer_peek_token (parser
->lexer
);
35209 c_kind
= cp_parser_omp_clause_name (parser
);
35213 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
35214 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
35216 c_name
= "collapse";
35218 case PRAGMA_OMP_CLAUSE_COPYIN
:
35219 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
35222 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
35223 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
35225 c_name
= "copyprivate";
35227 case PRAGMA_OMP_CLAUSE_DEFAULT
:
35228 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
35229 token
->location
, false);
35230 c_name
= "default";
35232 case PRAGMA_OMP_CLAUSE_FINAL
:
35233 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
35236 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
35237 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
35239 c_name
= "firstprivate";
35241 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
35242 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
35244 c_name
= "grainsize";
35246 case PRAGMA_OMP_CLAUSE_HINT
:
35247 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
35251 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
35252 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
35254 c_name
= "defaultmap";
35256 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
35257 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
35259 c_name
= "use_device_ptr";
35261 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
35262 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
35264 c_name
= "is_device_ptr";
35266 case PRAGMA_OMP_CLAUSE_IF
:
35267 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
35271 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
35273 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
35275 c_name
= "in_reduction";
35277 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
35278 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
35279 c_name
= "lastprivate";
35281 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
35282 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
35284 c_name
= "mergeable";
35286 case PRAGMA_OMP_CLAUSE_NOWAIT
:
35287 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
35290 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
35291 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
35293 c_name
= "num_tasks";
35295 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
35296 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
35298 c_name
= "num_threads";
35300 case PRAGMA_OMP_CLAUSE_ORDERED
:
35301 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
35303 c_name
= "ordered";
35305 case PRAGMA_OMP_CLAUSE_PRIORITY
:
35306 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
35308 c_name
= "priority";
35310 case PRAGMA_OMP_CLAUSE_PRIVATE
:
35311 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
35313 c_name
= "private";
35315 case PRAGMA_OMP_CLAUSE_REDUCTION
:
35317 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
35319 c_name
= "reduction";
35321 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
35322 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
35324 c_name
= "schedule";
35326 case PRAGMA_OMP_CLAUSE_SHARED
:
35327 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
35331 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
35333 = cp_parser_omp_clause_reduction (parser
,
35334 OMP_CLAUSE_TASK_REDUCTION
,
35336 c_name
= "task_reduction";
35338 case PRAGMA_OMP_CLAUSE_UNTIED
:
35339 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
35343 case PRAGMA_OMP_CLAUSE_INBRANCH
:
35344 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
35345 clauses
, token
->location
);
35346 c_name
= "inbranch";
35348 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
35349 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
35351 c_name
= "nontemporal";
35353 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
35354 clauses
= cp_parser_omp_clause_branch (parser
,
35355 OMP_CLAUSE_NOTINBRANCH
,
35356 clauses
, token
->location
);
35357 c_name
= "notinbranch";
35359 case PRAGMA_OMP_CLAUSE_PARALLEL
:
35360 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
35361 clauses
, token
->location
);
35362 c_name
= "parallel";
35366 error_at (token
->location
, "%qs must be the first clause of %qs",
35371 case PRAGMA_OMP_CLAUSE_FOR
:
35372 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
35373 clauses
, token
->location
);
35376 goto clause_not_first
;
35378 case PRAGMA_OMP_CLAUSE_SECTIONS
:
35379 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
35380 clauses
, token
->location
);
35381 c_name
= "sections";
35383 goto clause_not_first
;
35385 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
35386 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
35387 clauses
, token
->location
);
35388 c_name
= "taskgroup";
35390 goto clause_not_first
;
35392 case PRAGMA_OMP_CLAUSE_LINK
:
35393 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
35396 case PRAGMA_OMP_CLAUSE_TO
:
35397 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
35398 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35401 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
35404 case PRAGMA_OMP_CLAUSE_FROM
:
35405 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
35408 case PRAGMA_OMP_CLAUSE_UNIFORM
:
35409 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
35411 c_name
= "uniform";
35413 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
35414 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
35416 c_name
= "num_teams";
35418 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
35419 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
35421 c_name
= "thread_limit";
35423 case PRAGMA_OMP_CLAUSE_ALIGNED
:
35424 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
35425 c_name
= "aligned";
35427 case PRAGMA_OMP_CLAUSE_LINEAR
:
35429 bool declare_simd
= false;
35430 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
35431 declare_simd
= true;
35432 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
35436 case PRAGMA_OMP_CLAUSE_DEPEND
:
35437 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
35441 case PRAGMA_OMP_CLAUSE_MAP
:
35442 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
35445 case PRAGMA_OMP_CLAUSE_DEVICE
:
35446 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
35450 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
35451 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
35453 c_name
= "dist_schedule";
35455 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
35456 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
35458 c_name
= "proc_bind";
35460 case PRAGMA_OMP_CLAUSE_SAFELEN
:
35461 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
35463 c_name
= "safelen";
35465 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
35466 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
35468 c_name
= "simdlen";
35470 case PRAGMA_OMP_CLAUSE_NOGROUP
:
35471 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
35473 c_name
= "nogroup";
35475 case PRAGMA_OMP_CLAUSE_THREADS
:
35477 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
35478 clauses
, token
->location
);
35479 c_name
= "threads";
35481 case PRAGMA_OMP_CLAUSE_SIMD
:
35483 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
35484 clauses
, token
->location
);
35488 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
35494 if (((mask
>> c_kind
) & 1) == 0)
35496 /* Remove the invalid clause(s) from the list to avoid
35497 confusing the rest of the compiler. */
35499 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
35503 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35506 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
35507 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
35509 return finish_omp_clauses (clauses
, C_ORT_OMP
);
35518 In practice, we're also interested in adding the statement to an
35519 outer node. So it is convenient if we work around the fact that
35520 cp_parser_statement calls add_stmt. */
35523 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
35525 unsigned save
= parser
->in_statement
;
35527 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35528 This preserves the "not within loop or switch" style error messages
35529 for nonsense cases like
35535 if (parser
->in_statement
)
35536 parser
->in_statement
= IN_OMP_BLOCK
;
35542 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
35544 parser
->in_statement
= save
;
35548 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
35550 tree stmt
= begin_omp_structured_block ();
35551 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35553 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35555 cp_parser_end_omp_structured_block (parser
, save
);
35556 return finish_omp_structured_block (stmt
);
35560 # pragma omp atomic new-line
35564 x binop= expr | x++ | ++x | x-- | --x
35566 +, *, -, /, &, ^, |, <<, >>
35568 where x is an lvalue expression with scalar type.
35571 # pragma omp atomic new-line
35574 # pragma omp atomic read new-line
35577 # pragma omp atomic write new-line
35580 # pragma omp atomic update new-line
35583 # pragma omp atomic capture new-line
35586 # pragma omp atomic capture new-line
35594 expression-stmt | x = x binop expr
35596 v = expression-stmt
35598 { v = x; update-stmt; } | { update-stmt; v = x; }
35602 expression-stmt | x = x binop expr | x = expr binop x
35606 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35608 where x and v are lvalue expressions with scalar type. */
35611 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
35613 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
35614 tree rhs1
= NULL_TREE
, orig_lhs
;
35615 location_t loc
= pragma_tok
->location
;
35616 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
35617 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
35618 bool structured_block
= false;
35620 tree clauses
= NULL_TREE
;
35622 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35624 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35625 cp_lexer_consume_token (parser
->lexer
);
35629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35631 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35632 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35633 const char *p
= IDENTIFIER_POINTER (id
);
35634 enum tree_code new_code
= ERROR_MARK
;
35635 enum omp_memory_order new_memory_order
35636 = OMP_MEMORY_ORDER_UNSPECIFIED
;
35638 if (!strcmp (p
, "read"))
35639 new_code
= OMP_ATOMIC_READ
;
35640 else if (!strcmp (p
, "write"))
35641 new_code
= NOP_EXPR
;
35642 else if (!strcmp (p
, "update"))
35643 new_code
= OMP_ATOMIC
;
35644 else if (!strcmp (p
, "capture"))
35645 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
35646 else if (!strcmp (p
, "seq_cst"))
35647 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35648 else if (!strcmp (p
, "acq_rel"))
35649 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35650 else if (!strcmp (p
, "release"))
35651 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35652 else if (!strcmp (p
, "acquire"))
35653 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35654 else if (!strcmp (p
, "relaxed"))
35655 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35656 else if (!strcmp (p
, "hint"))
35658 cp_lexer_consume_token (parser
->lexer
);
35659 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
35665 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
35666 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35667 "%<release%>, %<relaxed%> or %<hint%> clause");
35671 if (new_code
!= ERROR_MARK
)
35673 if (code
!= ERROR_MARK
)
35674 error_at (cloc
, "too many atomic clauses");
35678 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35680 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
35681 error_at (cloc
, "too many memory order clauses");
35683 memory_order
= new_memory_order
;
35685 cp_lexer_consume_token (parser
->lexer
);
35691 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35693 if (code
== ERROR_MARK
)
35695 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
35698 = (enum omp_requires
) (omp_requires_mask
35699 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
35700 switch ((enum omp_memory_order
)
35701 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
35703 case OMP_MEMORY_ORDER_UNSPECIFIED
:
35704 case OMP_MEMORY_ORDER_RELAXED
:
35705 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
35707 case OMP_MEMORY_ORDER_SEQ_CST
:
35708 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35710 case OMP_MEMORY_ORDER_ACQ_REL
:
35713 case OMP_ATOMIC_READ
:
35714 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
35716 case NOP_EXPR
: /* atomic write */
35718 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
35721 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
35726 gcc_unreachable ();
35732 case OMP_ATOMIC_READ
:
35733 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35734 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
35736 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
35737 "%<acq_rel%> or %<release%> clauses");
35738 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35741 case NOP_EXPR
: /* atomic write */
35742 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35743 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35745 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
35746 "%<acq_rel%> or %<acquire%> clauses");
35747 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35751 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
35752 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
35754 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
35755 "%<acq_rel%> or %<acquire%> clauses");
35756 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
35765 case OMP_ATOMIC_READ
:
35766 case NOP_EXPR
: /* atomic write */
35767 v
= cp_parser_unary_expression (parser
);
35768 if (v
== error_mark_node
)
35770 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35772 if (code
== NOP_EXPR
)
35773 lhs
= cp_parser_expression (parser
);
35775 lhs
= cp_parser_unary_expression (parser
);
35776 if (lhs
== error_mark_node
)
35778 if (code
== NOP_EXPR
)
35780 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35788 case OMP_ATOMIC_CAPTURE_NEW
:
35789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35791 cp_lexer_consume_token (parser
->lexer
);
35792 structured_block
= true;
35796 v
= cp_parser_unary_expression (parser
);
35797 if (v
== error_mark_node
)
35799 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35807 lhs
= cp_parser_unary_expression (parser
);
35809 switch (TREE_CODE (lhs
))
35814 case POSTINCREMENT_EXPR
:
35815 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35816 code
= OMP_ATOMIC_CAPTURE_OLD
;
35818 case PREINCREMENT_EXPR
:
35819 lhs
= TREE_OPERAND (lhs
, 0);
35820 opcode
= PLUS_EXPR
;
35821 rhs
= integer_one_node
;
35824 case POSTDECREMENT_EXPR
:
35825 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
35826 code
= OMP_ATOMIC_CAPTURE_OLD
;
35828 case PREDECREMENT_EXPR
:
35829 lhs
= TREE_OPERAND (lhs
, 0);
35830 opcode
= MINUS_EXPR
;
35831 rhs
= integer_one_node
;
35834 case COMPOUND_EXPR
:
35835 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
35836 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
35837 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
35838 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
35839 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35840 (TREE_OPERAND (lhs
, 1), 0), 0)))
35842 /* Undo effects of boolean_increment for post {in,de}crement. */
35843 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
35846 if (TREE_CODE (lhs
) == MODIFY_EXPR
35847 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
35849 /* Undo effects of boolean_increment. */
35850 if (integer_onep (TREE_OPERAND (lhs
, 1)))
35852 /* This is pre or post increment. */
35853 rhs
= TREE_OPERAND (lhs
, 1);
35854 lhs
= TREE_OPERAND (lhs
, 0);
35856 if (code
== OMP_ATOMIC_CAPTURE_NEW
35857 && !structured_block
35858 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
35859 code
= OMP_ATOMIC_CAPTURE_OLD
;
35865 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35868 opcode
= MULT_EXPR
;
35871 opcode
= TRUNC_DIV_EXPR
;
35874 opcode
= PLUS_EXPR
;
35877 opcode
= MINUS_EXPR
;
35879 case CPP_LSHIFT_EQ
:
35880 opcode
= LSHIFT_EXPR
;
35882 case CPP_RSHIFT_EQ
:
35883 opcode
= RSHIFT_EXPR
;
35886 opcode
= BIT_AND_EXPR
;
35889 opcode
= BIT_IOR_EXPR
;
35892 opcode
= BIT_XOR_EXPR
;
35895 enum cp_parser_prec oprec
;
35897 cp_lexer_consume_token (parser
->lexer
);
35898 cp_parser_parse_tentatively (parser
);
35899 rhs1
= cp_parser_simple_cast_expression (parser
);
35900 if (rhs1
== error_mark_node
)
35902 cp_parser_abort_tentative_parse (parser
);
35903 cp_parser_simple_cast_expression (parser
);
35906 token
= cp_lexer_peek_token (parser
->lexer
);
35907 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
35909 cp_parser_abort_tentative_parse (parser
);
35910 cp_parser_parse_tentatively (parser
);
35911 rhs
= cp_parser_binary_expression (parser
, false, true,
35912 PREC_NOT_OPERATOR
, NULL
);
35913 if (rhs
== error_mark_node
)
35915 cp_parser_abort_tentative_parse (parser
);
35916 cp_parser_binary_expression (parser
, false, true,
35917 PREC_NOT_OPERATOR
, NULL
);
35920 switch (TREE_CODE (rhs
))
35923 case TRUNC_DIV_EXPR
:
35932 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
35934 if (cp_parser_parse_definitely (parser
))
35936 opcode
= TREE_CODE (rhs
);
35937 rhs1
= TREE_OPERAND (rhs
, 0);
35938 rhs
= TREE_OPERAND (rhs
, 1);
35948 cp_parser_abort_tentative_parse (parser
);
35949 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
35951 rhs
= cp_parser_expression (parser
);
35952 if (rhs
== error_mark_node
)
35958 cp_parser_error (parser
,
35959 "invalid form of %<#pragma omp atomic%>");
35962 if (!cp_parser_parse_definitely (parser
))
35964 switch (token
->type
)
35966 case CPP_SEMICOLON
:
35967 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
35969 code
= OMP_ATOMIC_CAPTURE_OLD
;
35974 cp_lexer_consume_token (parser
->lexer
);
35977 else if (structured_block
)
35984 cp_parser_error (parser
,
35985 "invalid form of %<#pragma omp atomic%>");
35988 opcode
= MULT_EXPR
;
35991 opcode
= TRUNC_DIV_EXPR
;
35994 opcode
= PLUS_EXPR
;
35997 opcode
= MINUS_EXPR
;
36000 opcode
= LSHIFT_EXPR
;
36003 opcode
= RSHIFT_EXPR
;
36006 opcode
= BIT_AND_EXPR
;
36009 opcode
= BIT_IOR_EXPR
;
36012 opcode
= BIT_XOR_EXPR
;
36015 cp_parser_error (parser
,
36016 "invalid operator for %<#pragma omp atomic%>");
36019 oprec
= TOKEN_PRECEDENCE (token
);
36020 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
36021 if (commutative_tree_code (opcode
))
36022 oprec
= (enum cp_parser_prec
) (oprec
- 1);
36023 cp_lexer_consume_token (parser
->lexer
);
36024 rhs
= cp_parser_binary_expression (parser
, false, false,
36026 if (rhs
== error_mark_node
)
36031 cp_parser_error (parser
,
36032 "invalid operator for %<#pragma omp atomic%>");
36035 cp_lexer_consume_token (parser
->lexer
);
36037 rhs
= cp_parser_expression (parser
);
36038 if (rhs
== error_mark_node
)
36043 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
36045 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
36047 v
= cp_parser_unary_expression (parser
);
36048 if (v
== error_mark_node
)
36050 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36052 lhs1
= cp_parser_unary_expression (parser
);
36053 if (lhs1
== error_mark_node
)
36056 if (structured_block
)
36058 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36059 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
36062 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
36063 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
36064 rhs1
, clauses
, memory_order
);
36065 if (!structured_block
)
36066 cp_parser_consume_semicolon_at_end_of_statement (parser
);
36070 cp_parser_skip_to_end_of_block_or_statement (parser
);
36071 if (structured_block
)
36073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36074 cp_lexer_consume_token (parser
->lexer
);
36075 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
36077 cp_parser_skip_to_end_of_block_or_statement (parser
);
36078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
36079 cp_lexer_consume_token (parser
->lexer
);
36086 # pragma omp barrier new-line */
36089 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
36091 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36092 finish_omp_barrier ();
36096 # pragma omp critical [(name)] new-line
36100 # pragma omp critical [(name) [hint(expression)]] new-line
36101 structured-block */
36103 #define OMP_CRITICAL_CLAUSE_MASK \
36104 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36107 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36109 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
36111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36113 matching_parens parens
;
36114 parens
.consume_open (parser
);
36116 name
= cp_parser_identifier (parser
);
36118 if (name
== error_mark_node
36119 || !parens
.require_close (parser
))
36120 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36121 /*or_comma=*/false,
36122 /*consume_paren=*/true);
36123 if (name
== error_mark_node
)
36126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
36127 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
36128 cp_lexer_consume_token (parser
->lexer
);
36130 clauses
= cp_parser_omp_all_clauses (parser
,
36131 OMP_CRITICAL_CLAUSE_MASK
,
36132 "#pragma omp critical", pragma_tok
);
36135 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36137 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
36138 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
36142 # pragma omp depobj ( depobj ) depobj-clause new-line
36145 depend (dependence-type : locator)
36147 update (dependence-type)
36156 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
36158 location_t loc
= pragma_tok
->location
;
36159 matching_parens parens
;
36160 if (!parens
.require_open (parser
))
36162 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36166 tree depobj
= cp_parser_assignment_expression (parser
);
36168 if (!parens
.require_close (parser
))
36169 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36170 /*or_comma=*/false,
36171 /*consume_paren=*/true);
36173 tree clause
= NULL_TREE
;
36174 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
36175 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36178 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36179 const char *p
= IDENTIFIER_POINTER (id
);
36181 cp_lexer_consume_token (parser
->lexer
);
36182 if (!strcmp ("depend", p
))
36184 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
36186 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
36188 clause
= error_mark_node
;
36190 else if (!strcmp ("destroy", p
))
36191 kind
= OMP_CLAUSE_DEPEND_LAST
;
36192 else if (!strcmp ("update", p
))
36194 matching_parens c_parens
;
36195 if (c_parens
.require_open (parser
))
36198 = cp_lexer_peek_token (parser
->lexer
)->location
;
36199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36201 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36202 const char *p2
= IDENTIFIER_POINTER (id2
);
36204 cp_lexer_consume_token (parser
->lexer
);
36205 if (!strcmp ("in", p2
))
36206 kind
= OMP_CLAUSE_DEPEND_IN
;
36207 else if (!strcmp ("out", p2
))
36208 kind
= OMP_CLAUSE_DEPEND_OUT
;
36209 else if (!strcmp ("inout", p2
))
36210 kind
= OMP_CLAUSE_DEPEND_INOUT
;
36211 else if (!strcmp ("mutexinoutset", p2
))
36212 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
36214 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36216 clause
= error_mark_node
;
36217 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
36218 "%<mutexinoutset%>");
36220 if (!c_parens
.require_close (parser
))
36221 cp_parser_skip_to_closing_parenthesis (parser
,
36222 /*recovering=*/true,
36223 /*or_comma=*/false,
36224 /*consume_paren=*/true);
36227 clause
= error_mark_node
;
36230 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36232 clause
= error_mark_node
;
36233 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
36235 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36237 finish_omp_depobj (loc
, depobj
, kind
, clause
);
36242 # pragma omp flush flush-vars[opt] new-line
36248 # pragma omp flush memory-order-clause new-line */
36251 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
36253 enum memmodel mo
= MEMMODEL_LAST
;
36254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36256 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36257 const char *p
= IDENTIFIER_POINTER (id
);
36258 if (!strcmp (p
, "acq_rel"))
36259 mo
= MEMMODEL_ACQ_REL
;
36260 else if (!strcmp (p
, "release"))
36261 mo
= MEMMODEL_RELEASE
;
36262 else if (!strcmp (p
, "acquire"))
36263 mo
= MEMMODEL_ACQUIRE
;
36265 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36266 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36267 cp_lexer_consume_token (parser
->lexer
);
36269 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36271 if (mo
!= MEMMODEL_LAST
)
36272 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36273 "%<flush%> list specified together with memory order "
36275 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36277 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36279 finish_omp_flush (mo
);
36282 /* Helper function, to parse omp for increment expression. */
36285 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
36287 tree cond
= cp_parser_binary_expression (parser
, false, true,
36288 PREC_NOT_OPERATOR
, NULL
);
36289 if (cond
== error_mark_node
36290 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
36292 cp_parser_skip_to_end_of_statement (parser
);
36293 return error_mark_node
;
36296 switch (TREE_CODE (cond
))
36304 if (code
!= OACC_LOOP
)
36306 gcc_fallthrough ();
36308 return error_mark_node
;
36311 /* If decl is an iterator, preserve LHS and RHS of the relational
36312 expr until finish_omp_for. */
36314 && (type_dependent_expression_p (decl
)
36315 || CLASS_TYPE_P (TREE_TYPE (decl
))))
36318 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
36320 TREE_OPERAND (cond
, 0), ERROR_MARK
,
36321 TREE_OPERAND (cond
, 1), ERROR_MARK
,
36322 /*overload=*/NULL
, tf_warning_or_error
);
36325 /* Helper function, to parse omp for increment expression. */
36328 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
36330 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36336 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36338 op
= (token
->type
== CPP_PLUS_PLUS
36339 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
36340 cp_lexer_consume_token (parser
->lexer
);
36341 lhs
= cp_parser_simple_cast_expression (parser
);
36343 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36344 return error_mark_node
;
36345 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36348 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
36350 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
36351 return error_mark_node
;
36353 token
= cp_lexer_peek_token (parser
->lexer
);
36354 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
36356 op
= (token
->type
== CPP_PLUS_PLUS
36357 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
36358 cp_lexer_consume_token (parser
->lexer
);
36359 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
36362 op
= cp_parser_assignment_operator_opt (parser
);
36363 if (op
== ERROR_MARK
)
36364 return error_mark_node
;
36366 if (op
!= NOP_EXPR
)
36368 rhs
= cp_parser_assignment_expression (parser
);
36369 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
36370 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36373 lhs
= cp_parser_binary_expression (parser
, false, false,
36374 PREC_ADDITIVE_EXPRESSION
, NULL
);
36375 token
= cp_lexer_peek_token (parser
->lexer
);
36376 decl_first
= (lhs
== decl
36377 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
36380 if (token
->type
!= CPP_PLUS
36381 && token
->type
!= CPP_MINUS
)
36382 return error_mark_node
;
36386 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
36387 cp_lexer_consume_token (parser
->lexer
);
36388 rhs
= cp_parser_binary_expression (parser
, false, false,
36389 PREC_ADDITIVE_EXPRESSION
, NULL
);
36390 token
= cp_lexer_peek_token (parser
->lexer
);
36391 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
36393 if (lhs
== NULL_TREE
)
36395 if (op
== PLUS_EXPR
)
36398 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
36399 tf_warning_or_error
);
36402 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
36403 ERROR_MARK
, NULL
, tf_warning_or_error
);
36406 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
36411 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
36412 || op
== MINUS_EXPR
)
36413 return error_mark_node
;
36414 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
36417 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
36419 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
36422 /* Parse the initialization statement of an OpenMP for loop.
36424 Return true if the resulting construct should have an
36425 OMP_CLAUSE_PRIVATE added to it. */
36428 cp_parser_omp_for_loop_init (cp_parser
*parser
,
36429 tree
&this_pre_body
,
36430 vec
<tree
, va_gc
> *&for_block
,
36436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
36439 tree add_private_clause
= NULL_TREE
;
36441 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36445 integer-type var = lb
36446 random-access-iterator-type var = lb
36447 pointer-type var = lb
36449 cp_decl_specifier_seq type_specifiers
;
36451 /* First, try to parse as an initialized declaration. See
36452 cp_parser_condition, from whence the bulk of this is copied. */
36454 cp_parser_parse_tentatively (parser
);
36455 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
36456 /*is_declaration=*/true,
36457 /*is_trailing_return=*/false,
36459 if (cp_parser_parse_definitely (parser
))
36461 /* If parsing a type specifier seq succeeded, then this
36462 MUST be a initialized declaration. */
36463 tree asm_specification
, attributes
;
36464 cp_declarator
*declarator
;
36466 declarator
= cp_parser_declarator (parser
,
36467 CP_PARSER_DECLARATOR_NAMED
,
36468 CP_PARSER_FLAGS_NONE
,
36469 /*ctor_dtor_or_conv_p=*/NULL
,
36470 /*parenthesized_p=*/NULL
,
36471 /*member_p=*/false,
36472 /*friend_p=*/false,
36473 /*static_p=*/false);
36474 attributes
= cp_parser_attributes_opt (parser
);
36475 asm_specification
= cp_parser_asm_specification_opt (parser
);
36477 if (declarator
== cp_error_declarator
)
36478 cp_parser_skip_to_end_of_statement (parser
);
36482 tree pushed_scope
, auto_node
;
36484 decl
= start_decl (declarator
, &type_specifiers
,
36485 SD_INITIALIZED
, attributes
,
36486 /*prefix_attributes=*/NULL_TREE
,
36489 auto_node
= type_uses_auto (TREE_TYPE (decl
));
36490 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
36492 if (cp_lexer_next_token_is (parser
->lexer
,
36494 error ("parenthesized initialization is not allowed in "
36495 "OpenMP %<for%> loop");
36497 /* Trigger an error. */
36498 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36500 init
= error_mark_node
;
36501 cp_parser_skip_to_end_of_statement (parser
);
36503 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
36504 || type_dependent_expression_p (decl
)
36507 bool is_direct_init
, is_non_constant_init
;
36509 init
= cp_parser_initializer (parser
,
36511 &is_non_constant_init
);
36516 = do_auto_deduction (TREE_TYPE (decl
), init
,
36519 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
36520 && !type_dependent_expression_p (decl
))
36524 cp_finish_decl (decl
, init
, !is_non_constant_init
,
36526 LOOKUP_ONLYCONVERTING
);
36528 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
36530 vec_safe_push (for_block
, this_pre_body
);
36535 init
= pop_stmt_list (this_pre_body
);
36536 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
36538 tree_stmt_iterator i
= tsi_start (init
);
36539 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36540 while (!tsi_end_p (i
))
36542 tree t
= tsi_stmt (i
);
36543 if (TREE_CODE (t
) == DECL_EXPR
36544 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
36547 vec_safe_push (for_block
, t
);
36552 if (tsi_one_before_end_p (i
))
36554 tree t
= tsi_stmt (i
);
36556 free_stmt_list (init
);
36561 this_pre_body
= NULL_TREE
;
36566 cp_lexer_consume_token (parser
->lexer
);
36567 init
= cp_parser_assignment_expression (parser
);
36570 if (TYPE_REF_P (TREE_TYPE (decl
)))
36571 init
= error_mark_node
;
36573 cp_finish_decl (decl
, NULL_TREE
,
36574 /*init_const_expr_p=*/false,
36576 LOOKUP_ONLYCONVERTING
);
36580 pop_scope (pushed_scope
);
36586 /* If parsing a type specifier sequence failed, then
36587 this MUST be a simple expression. */
36588 cp_parser_parse_tentatively (parser
);
36589 decl
= cp_parser_primary_expression (parser
, false, false,
36591 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
36592 if (!cp_parser_error_occurred (parser
)
36594 && (TREE_CODE (decl
) == COMPONENT_REF
36595 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
36597 cp_parser_abort_tentative_parse (parser
);
36598 cp_parser_parse_tentatively (parser
);
36599 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36600 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
36601 /*check_dependency_p=*/true,
36602 /*template_p=*/NULL
,
36603 /*declarator_p=*/false,
36604 /*optional_p=*/false);
36605 if (name
!= error_mark_node
36606 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
36608 decl
= cp_parser_lookup_name_simple (parser
, name
,
36610 if (TREE_CODE (decl
) == FIELD_DECL
)
36611 add_private_clause
= omp_privatize_field (decl
, false);
36613 cp_parser_abort_tentative_parse (parser
);
36614 cp_parser_parse_tentatively (parser
);
36615 decl
= cp_parser_primary_expression (parser
, false, false,
36618 if (!cp_parser_error_occurred (parser
)
36621 && CLASS_TYPE_P (TREE_TYPE (decl
)))
36625 cp_parser_parse_definitely (parser
);
36626 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
36627 rhs
= cp_parser_assignment_expression (parser
);
36629 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
36632 tf_warning_or_error
));
36633 if (!add_private_clause
)
36634 add_private_clause
= decl
;
36639 cp_parser_abort_tentative_parse (parser
);
36640 init
= cp_parser_expression (parser
);
36643 if (TREE_CODE (init
) == MODIFY_EXPR
36644 || TREE_CODE (init
) == MODOP_EXPR
)
36645 real_decl
= TREE_OPERAND (init
, 0);
36649 return add_private_clause
;
36652 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36655 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
36656 tree
&decl
, tree
&orig_decl
, tree
&init
,
36657 tree
&orig_init
, tree
&cond
, tree
&incr
)
36659 tree begin
, end
, range_temp_decl
= NULL_TREE
;
36660 tree iter_type
, begin_expr
, end_expr
;
36662 if (processing_template_decl
)
36664 if (check_for_bare_parameter_packs (init
))
36665 init
= error_mark_node
;
36666 if (!type_dependent_expression_p (init
)
36667 /* do_auto_deduction doesn't mess with template init-lists. */
36668 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
36671 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
36673 tree v
= DECL_VALUE_EXPR (decl
);
36674 if (TREE_CODE (v
) == ARRAY_REF
36675 && VAR_P (TREE_OPERAND (v
, 0))
36676 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36677 d
= TREE_OPERAND (v
, 0);
36679 do_range_for_auto_deduction (d
, init
);
36681 cond
= global_namespace
;
36685 this_pre_body
= pop_stmt_list (this_pre_body
);
36689 init
= mark_lvalue_use (init
);
36691 if (decl
== error_mark_node
|| init
== error_mark_node
)
36692 /* If an error happened previously do nothing or else a lot of
36693 unhelpful errors would be issued. */
36694 begin_expr
= end_expr
= iter_type
= error_mark_node
;
36700 && array_of_runtime_bound_p (TREE_TYPE (init
)))
36701 /* Can't bind a reference to an array of runtime bound. */
36705 range_temp
= build_range_temp (init
);
36706 DECL_NAME (range_temp
) = NULL_TREE
;
36707 pushdecl (range_temp
);
36708 cp_finish_decl (range_temp
, init
,
36709 /*is_constant_init*/false, NULL_TREE
,
36710 LOOKUP_ONLYCONVERTING
);
36711 range_temp_decl
= range_temp
;
36712 range_temp
= convert_from_reference (range_temp
);
36714 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
36715 &begin_expr
, &end_expr
);
36718 tree end_iter_type
= iter_type
;
36719 if (cxx_dialect
>= cxx17
)
36720 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
36721 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
36722 TREE_USED (end
) = 1;
36723 DECL_ARTIFICIAL (end
) = 1;
36725 cp_finish_decl (end
, end_expr
,
36726 /*is_constant_init*/false, NULL_TREE
,
36727 LOOKUP_ONLYCONVERTING
);
36729 /* The new for initialization statement. */
36730 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
36731 TREE_USED (begin
) = 1;
36732 DECL_ARTIFICIAL (begin
) = 1;
36735 if (CLASS_TYPE_P (iter_type
))
36740 begin_expr
= NULL_TREE
;
36742 cp_finish_decl (begin
, begin_expr
,
36743 /*is_constant_init*/false, NULL_TREE
,
36744 LOOKUP_ONLYCONVERTING
);
36746 /* The new for condition. */
36747 if (CLASS_TYPE_P (iter_type
))
36748 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
36750 cond
= build_x_binary_op (input_location
, NE_EXPR
,
36753 NULL
, tf_warning_or_error
);
36755 /* The new increment expression. */
36756 if (CLASS_TYPE_P (iter_type
))
36757 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
36759 incr
= finish_unary_op_expr (input_location
,
36760 PREINCREMENT_EXPR
, begin
,
36761 tf_warning_or_error
);
36767 vec_safe_push (for_block
, this_pre_body
);
36768 this_pre_body
= NULL_TREE
;
36771 tree decomp_first_name
= NULL_TREE
;
36772 unsigned decomp_cnt
= 0;
36773 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
36775 tree v
= DECL_VALUE_EXPR (orig_decl
);
36776 if (TREE_CODE (v
) == ARRAY_REF
36777 && VAR_P (TREE_OPERAND (v
, 0))
36778 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
36780 tree d
= orig_decl
;
36781 orig_decl
= TREE_OPERAND (v
, 0);
36782 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
36783 decomp_first_name
= d
;
36787 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
36790 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36792 if (!error_operand_p (t
))
36793 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
36797 tree v
= make_tree_vec (decomp_cnt
+ 3);
36798 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
36799 TREE_VEC_ELT (v
, 1) = end
;
36800 TREE_VEC_ELT (v
, 2) = orig_decl
;
36801 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
36803 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
36804 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
36806 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
36809 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36810 inside of the collapsed body. */
36813 cp_finish_omp_range_for (tree orig
, tree begin
)
36815 gcc_assert (TREE_CODE (orig
) == TREE_LIST
36816 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
36817 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
36818 tree decomp_first_name
= NULL_TREE
;
36819 unsigned int decomp_cnt
= 0;
36821 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36823 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
36824 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
36825 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
36828 /* The declaration is initialized with *__begin inside the loop body. */
36829 cp_finish_decl (decl
,
36830 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
36831 tf_warning_or_error
),
36832 /*is_constant_init*/false, NULL_TREE
,
36833 LOOKUP_ONLYCONVERTING
);
36834 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
36835 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
36838 /* Parse the restricted form of the for statement allowed by OpenMP. */
36841 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
36842 tree
*cclauses
, bool *if_p
)
36844 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
36846 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
36847 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
36848 location_t loc_first
;
36849 bool collapse_err
= false;
36850 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
36851 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
36852 auto_vec
<tree
, 4> orig_inits
;
36853 bool tiling
= false;
36855 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
36856 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
36857 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
36858 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
36861 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
36863 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
36864 && OMP_CLAUSE_ORDERED_EXPR (cl
))
36867 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
36870 if (ordered
&& ordered
< collapse
)
36872 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36873 "%<ordered%> clause parameter is less than %<collapse%>");
36874 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
36875 = build_int_cst (NULL_TREE
, collapse
);
36876 ordered
= collapse
;
36880 for (tree
*pc
= &clauses
; *pc
; )
36881 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
36883 error_at (OMP_CLAUSE_LOCATION (*pc
),
36884 "%<linear%> clause may not be specified together "
36885 "with %<ordered%> clause with a parameter");
36886 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36889 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36892 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
36893 count
= ordered
? ordered
: collapse
;
36895 declv
= make_tree_vec (count
);
36896 initv
= make_tree_vec (count
);
36897 condv
= make_tree_vec (count
);
36898 incrv
= make_tree_vec (count
);
36899 orig_declv
= NULL_TREE
;
36901 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
36903 for (i
= 0; i
< count
; i
++)
36905 int bracecount
= 0;
36906 tree add_private_clause
= NULL_TREE
;
36909 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
36912 cp_parser_error (parser
, "for statement expected");
36915 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
36917 /* Don't create location wrapper nodes within an OpenMP "for"
36919 auto_suppress_location_wrappers sentinel
;
36921 matching_parens parens
;
36922 if (!parens
.require_open (parser
))
36925 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
36926 this_pre_body
= push_stmt_list ();
36928 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
36930 /* Save tokens so that we can put them back. */
36931 cp_lexer_save_tokens (parser
->lexer
);
36933 /* Look for ':' that is not nested in () or {}. */
36935 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
36936 /*recovering=*/false,
36941 /* Roll back the tokens we skipped. */
36942 cp_lexer_rollback_tokens (parser
->lexer
);
36946 bool saved_colon_corrects_to_scope_p
36947 = parser
->colon_corrects_to_scope_p
;
36949 /* A colon is used in range-based for. */
36950 parser
->colon_corrects_to_scope_p
= false;
36952 /* Parse the declaration. */
36953 cp_parser_simple_declaration (parser
,
36954 /*function_definition_allowed_p=*/
36956 parser
->colon_corrects_to_scope_p
36957 = saved_colon_corrects_to_scope_p
;
36959 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36961 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
36964 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
36965 orig_decl
, init
, orig_init
,
36972 pre_body
= push_stmt_list ();
36974 add_stmt (this_pre_body
);
36975 pre_body
= pop_stmt_list (pre_body
);
36978 pre_body
= this_pre_body
;
36982 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
36983 "%<ordered%> clause with parameter on "
36984 "range-based %<for%> loop");
36986 goto parse_close_paren
;
36991 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
36992 init
, orig_init
, decl
, real_decl
);
36994 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36997 this_pre_body
= pop_stmt_list (this_pre_body
);
37001 pre_body
= push_stmt_list ();
37003 add_stmt (this_pre_body
);
37004 pre_body
= pop_stmt_list (pre_body
);
37007 pre_body
= this_pre_body
;
37012 if (cclauses
!= NULL
37013 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
37014 && real_decl
!= NULL_TREE
)
37017 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
37018 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
37019 && OMP_CLAUSE_DECL (*c
) == real_decl
)
37021 error_at (loc
, "iteration variable %qD"
37022 " should not be firstprivate", real_decl
);
37023 *c
= OMP_CLAUSE_CHAIN (*c
);
37025 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
37026 && OMP_CLAUSE_DECL (*c
) == real_decl
)
37028 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
37030 *c
= OMP_CLAUSE_CHAIN (*c
);
37031 if (code
== OMP_SIMD
)
37033 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37034 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
37038 OMP_CLAUSE_CHAIN (l
) = clauses
;
37041 add_private_clause
= NULL_TREE
;
37045 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
37046 && OMP_CLAUSE_DECL (*c
) == real_decl
)
37047 add_private_clause
= NULL_TREE
;
37048 c
= &OMP_CLAUSE_CHAIN (*c
);
37052 if (add_private_clause
)
37055 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37057 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
37058 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
37059 && OMP_CLAUSE_DECL (c
) == decl
)
37061 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
37062 && OMP_CLAUSE_DECL (c
) == decl
)
37063 error_at (loc
, "iteration variable %qD "
37064 "should not be firstprivate",
37066 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
37067 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
37068 && OMP_CLAUSE_DECL (c
) == decl
)
37069 error_at (loc
, "iteration variable %qD should not be reduction",
37074 if (code
!= OMP_SIMD
)
37075 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
37076 else if (collapse
== 1)
37077 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
37079 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
37080 OMP_CLAUSE_DECL (c
) = add_private_clause
;
37081 c
= finish_omp_clauses (c
, C_ORT_OMP
);
37084 OMP_CLAUSE_CHAIN (c
) = clauses
;
37086 /* For linear, signal that we need to fill up
37087 the so far unknown linear step. */
37088 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
37089 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
37095 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
37096 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
37097 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
37100 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
37102 /* If decl is an iterator, preserve the operator on decl
37103 until finish_omp_for. */
37105 && ((processing_template_decl
37106 && (TREE_TYPE (real_decl
) == NULL_TREE
37107 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
37108 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
37109 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
37111 incr
= cp_parser_expression (parser
);
37112 if (!EXPR_HAS_LOCATION (incr
))
37113 protected_set_expr_location (incr
, input_location
);
37117 if (!parens
.require_close (parser
))
37118 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37119 /*or_comma=*/false,
37120 /*consume_paren=*/true);
37122 TREE_VEC_ELT (declv
, i
) = decl
;
37123 TREE_VEC_ELT (initv
, i
) = init
;
37124 TREE_VEC_ELT (condv
, i
) = cond
;
37125 TREE_VEC_ELT (incrv
, i
) = incr
;
37128 orig_inits
.safe_grow_cleared (i
+ 1);
37129 orig_inits
[i
] = orig_init
;
37134 orig_declv
= copy_node (declv
);
37135 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
37137 else if (orig_declv
)
37138 TREE_VEC_ELT (orig_declv
, i
) = decl
;
37140 if (i
== count
- 1)
37143 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37144 in between the collapsed for loops to be still considered perfectly
37145 nested. Hopefully the final version clarifies this.
37146 For now handle (multiple) {'s and empty statements. */
37147 cp_parser_parse_tentatively (parser
);
37150 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37152 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
37154 cp_lexer_consume_token (parser
->lexer
);
37157 else if (bracecount
37158 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37159 cp_lexer_consume_token (parser
->lexer
);
37162 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37163 error_at (loc
, "not enough for loops to collapse");
37164 collapse_err
= true;
37165 cp_parser_abort_tentative_parse (parser
);
37173 cp_parser_parse_definitely (parser
);
37174 nbraces
+= bracecount
;
37181 /* Note that we saved the original contents of this flag when we entered
37182 the structured block, and so we don't need to re-save it here. */
37183 parser
->in_statement
= IN_OMP_FOR
;
37185 /* Note that the grammar doesn't call for a structured block here,
37186 though the loop as a whole is a structured block. */
37189 body
= begin_omp_structured_block ();
37190 for (i
= 0; i
< count
; i
++)
37191 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
37192 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
37193 TREE_VEC_ELT (declv
, i
));
37196 body
= push_stmt_list ();
37197 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37199 body
= finish_omp_structured_block (body
);
37201 body
= pop_stmt_list (body
);
37203 if (declv
== NULL_TREE
)
37206 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
37207 incrv
, body
, pre_body
, &orig_inits
, clauses
);
37211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
37213 cp_lexer_consume_token (parser
->lexer
);
37216 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37217 cp_lexer_consume_token (parser
->lexer
);
37222 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37223 "collapsed loops not perfectly nested");
37225 collapse_err
= true;
37226 cp_parser_statement_seq_opt (parser
, NULL
);
37227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
37232 while (!for_block
->is_empty ())
37234 tree t
= for_block
->pop ();
37235 if (TREE_CODE (t
) == STATEMENT_LIST
)
37236 add_stmt (pop_stmt_list (t
));
37240 release_tree_vector (for_block
);
37245 /* Helper function for OpenMP parsing, split clauses and call
37246 finish_omp_clauses on each of the set of clauses afterwards. */
37249 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
37250 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
37253 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
37254 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
37256 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
37260 #pragma omp simd simd-clause[optseq] new-line
37263 #define OMP_SIMD_CLAUSE_MASK \
37264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37276 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37277 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37280 tree clauses
, sb
, ret
;
37282 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37284 strcat (p_name
, " simd");
37285 mask
|= OMP_SIMD_CLAUSE_MASK
;
37287 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37291 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
37292 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
37293 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
37294 OMP_CLAUSE_ORDERED
);
37295 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
37297 error_at (OMP_CLAUSE_LOCATION (c
),
37298 "%<ordered%> clause with parameter may not be specified "
37299 "on %qs construct", p_name
);
37300 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
37304 keep_next_level (true);
37305 sb
= begin_omp_structured_block ();
37306 save
= cp_parser_begin_omp_structured_block (parser
);
37308 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
37310 cp_parser_end_omp_structured_block (parser
, save
);
37311 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37317 #pragma omp for for-clause[optseq] new-line
37321 #pragma omp for simd for-simd-clause[optseq] new-line
37324 #define OMP_FOR_CLAUSE_MASK \
37325 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37336 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
37337 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37340 tree clauses
, sb
, ret
;
37342 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37344 strcat (p_name
, " for");
37345 mask
|= OMP_FOR_CLAUSE_MASK
;
37346 /* parallel for{, simd} disallows nowait clause, but for
37347 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37348 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
37349 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37350 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37351 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37352 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
37354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37356 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37357 const char *p
= IDENTIFIER_POINTER (id
);
37359 if (strcmp (p
, "simd") == 0)
37361 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37362 if (cclauses
== NULL
)
37363 cclauses
= cclauses_buf
;
37365 cp_lexer_consume_token (parser
->lexer
);
37366 if (!flag_openmp
) /* flag_openmp_simd */
37367 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37369 sb
= begin_omp_structured_block ();
37370 save
= cp_parser_begin_omp_structured_block (parser
);
37371 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37373 cp_parser_end_omp_structured_block (parser
, save
);
37374 tree body
= finish_omp_structured_block (sb
);
37377 ret
= make_node (OMP_FOR
);
37378 TREE_TYPE (ret
) = void_type_node
;
37379 OMP_FOR_BODY (ret
) = body
;
37380 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37381 SET_EXPR_LOCATION (ret
, loc
);
37386 if (!flag_openmp
) /* flag_openmp_simd */
37388 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37392 /* Composite distribute parallel for disallows linear clause. */
37393 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37394 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
37396 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37400 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
37401 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
37404 keep_next_level (true);
37405 sb
= begin_omp_structured_block ();
37406 save
= cp_parser_begin_omp_structured_block (parser
);
37408 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
37410 cp_parser_end_omp_structured_block (parser
, save
);
37411 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
37416 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
37417 omp_clause_mask
, tree
*, bool *);
37420 # pragma omp master new-line
37421 structured-block */
37424 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
37425 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37428 tree clauses
, sb
, ret
;
37430 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37432 strcat (p_name
, " master");
37434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37436 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37437 const char *p
= IDENTIFIER_POINTER (id
);
37439 if (strcmp (p
, "taskloop") == 0)
37441 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37442 if (cclauses
== NULL
)
37443 cclauses
= cclauses_buf
;
37445 cp_lexer_consume_token (parser
->lexer
);
37446 if (!flag_openmp
) /* flag_openmp_simd */
37447 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37449 sb
= begin_omp_structured_block ();
37450 save
= cp_parser_begin_omp_structured_block (parser
);
37451 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
37453 cp_parser_end_omp_structured_block (parser
, save
);
37454 tree body
= finish_omp_structured_block (sb
);
37457 return c_finish_omp_master (loc
, body
);
37460 if (!flag_openmp
) /* flag_openmp_simd */
37462 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37468 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37470 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
37473 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37475 return c_finish_omp_master (loc
,
37476 cp_parser_omp_structured_block (parser
, if_p
));
37480 # pragma omp ordered new-line
37484 # pragma omp ordered ordered-clauses new-line
37485 structured-block */
37487 #define OMP_ORDERED_CLAUSE_MASK \
37488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37491 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37492 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37495 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
37496 enum pragma_context context
, bool *if_p
)
37498 location_t loc
= pragma_tok
->location
;
37500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37502 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37503 const char *p
= IDENTIFIER_POINTER (id
);
37505 if (strcmp (p
, "depend") == 0)
37507 if (!flag_openmp
) /* flag_openmp_simd */
37509 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37512 if (context
== pragma_stmt
)
37514 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
37515 "%<depend%> clause may only be used in compound "
37517 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37521 = cp_parser_omp_all_clauses (parser
,
37522 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
37523 "#pragma omp ordered", pragma_tok
);
37524 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
37530 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
37531 "#pragma omp ordered", pragma_tok
);
37533 if (!flag_openmp
/* flag_openmp_simd */
37534 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
37537 c_finish_omp_ordered (loc
, clauses
,
37538 cp_parser_omp_structured_block (parser
, if_p
));
37545 { section-sequence }
37548 section-directive[opt] structured-block
37549 section-sequence section-directive structured-block */
37552 cp_parser_omp_sections_scope (cp_parser
*parser
)
37554 tree stmt
, substmt
;
37555 bool error_suppress
= false;
37558 matching_braces braces
;
37559 if (!braces
.require_open (parser
))
37562 stmt
= push_stmt_list ();
37564 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
37565 != PRAGMA_OMP_SECTION
)
37567 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37568 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37569 add_stmt (substmt
);
37574 tok
= cp_lexer_peek_token (parser
->lexer
);
37575 if (tok
->type
== CPP_CLOSE_BRACE
)
37577 if (tok
->type
== CPP_EOF
)
37580 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
37582 cp_lexer_consume_token (parser
->lexer
);
37583 cp_parser_require_pragma_eol (parser
, tok
);
37584 error_suppress
= false;
37586 else if (!error_suppress
)
37588 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
37589 error_suppress
= true;
37592 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37593 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
37594 add_stmt (substmt
);
37596 braces
.require_close (parser
);
37598 substmt
= pop_stmt_list (stmt
);
37600 stmt
= make_node (OMP_SECTIONS
);
37601 TREE_TYPE (stmt
) = void_type_node
;
37602 OMP_SECTIONS_BODY (stmt
) = substmt
;
37609 # pragma omp sections sections-clause[optseq] newline
37612 #define OMP_SECTIONS_CLAUSE_MASK \
37613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37620 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
37621 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
37624 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37626 strcat (p_name
, " sections");
37627 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
37629 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
37631 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37635 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
37636 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
37639 ret
= cp_parser_omp_sections_scope (parser
);
37641 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
37647 # pragma omp parallel parallel-clause[optseq] new-line
37649 # pragma omp parallel for parallel-for-clause[optseq] new-line
37651 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37655 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37656 structured-block */
37658 #define OMP_PARALLEL_CLAUSE_MASK \
37659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37670 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37671 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37674 tree stmt
, clauses
, block
;
37676 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37678 strcat (p_name
, " parallel");
37679 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
37680 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37681 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
37682 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
37683 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
37685 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
37687 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37688 if (cclauses
== NULL
)
37689 cclauses
= cclauses_buf
;
37691 cp_lexer_consume_token (parser
->lexer
);
37692 if (!flag_openmp
) /* flag_openmp_simd */
37693 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37695 block
= begin_omp_parallel ();
37696 save
= cp_parser_begin_omp_structured_block (parser
);
37697 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
37699 cp_parser_end_omp_structured_block (parser
, save
);
37700 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37702 if (ret
== NULL_TREE
)
37704 OMP_PARALLEL_COMBINED (stmt
) = 1;
37707 /* When combined with distribute, parallel has to be followed by for.
37708 #pragma omp target parallel is allowed though. */
37710 && (mask
& (OMP_CLAUSE_MASK_1
37711 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
37713 error_at (loc
, "expected %<for%> after %qs", p_name
);
37714 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37717 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37719 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37720 const char *p
= IDENTIFIER_POINTER (id
);
37721 if (strcmp (p
, "master") == 0)
37723 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37724 cclauses
= cclauses_buf
;
37726 cp_lexer_consume_token (parser
->lexer
);
37727 block
= begin_omp_parallel ();
37728 save
= cp_parser_begin_omp_structured_block (parser
);
37729 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
37731 cp_parser_end_omp_structured_block (parser
, save
);
37732 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37734 OMP_PARALLEL_COMBINED (stmt
) = 1;
37735 if (ret
== NULL_TREE
)
37739 else if (!flag_openmp
) /* flag_openmp_simd */
37741 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37744 else if (strcmp (p
, "sections") == 0)
37746 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37747 cclauses
= cclauses_buf
;
37749 cp_lexer_consume_token (parser
->lexer
);
37750 block
= begin_omp_parallel ();
37751 save
= cp_parser_begin_omp_structured_block (parser
);
37752 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
37753 cp_parser_end_omp_structured_block (parser
, save
);
37754 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
37756 OMP_PARALLEL_COMBINED (stmt
) = 1;
37760 else if (!flag_openmp
) /* flag_openmp_simd */
37762 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37766 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37770 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
37771 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
37774 block
= begin_omp_parallel ();
37775 save
= cp_parser_begin_omp_structured_block (parser
);
37776 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37777 cp_parser_end_omp_structured_block (parser
, save
);
37778 stmt
= finish_omp_parallel (clauses
, block
);
37783 # pragma omp single single-clause[optseq] new-line
37784 structured-block */
37786 #define OMP_SINGLE_CLAUSE_MASK \
37787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37793 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37795 tree stmt
= make_node (OMP_SINGLE
);
37796 TREE_TYPE (stmt
) = void_type_node
;
37797 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37799 OMP_SINGLE_CLAUSES (stmt
)
37800 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
37801 "#pragma omp single", pragma_tok
);
37802 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
37804 return add_stmt (stmt
);
37808 # pragma omp task task-clause[optseq] new-line
37809 structured-block */
37811 #define OMP_TASK_CLAUSE_MASK \
37812 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37825 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37827 tree clauses
, block
;
37830 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
37831 "#pragma omp task", pragma_tok
);
37832 block
= begin_omp_task ();
37833 save
= cp_parser_begin_omp_structured_block (parser
);
37834 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37835 cp_parser_end_omp_structured_block (parser
, save
);
37836 return finish_omp_task (clauses
, block
);
37840 # pragma omp taskwait new-line
37843 # pragma omp taskwait taskwait-clause[opt] new-line */
37845 #define OMP_TASKWAIT_CLAUSE_MASK \
37846 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37849 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
37852 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
37853 "#pragma omp taskwait", pragma_tok
);
37857 tree stmt
= make_node (OMP_TASK
);
37858 TREE_TYPE (stmt
) = void_node
;
37859 OMP_TASK_CLAUSES (stmt
) = clauses
;
37860 OMP_TASK_BODY (stmt
) = NULL_TREE
;
37861 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37865 finish_omp_taskwait ();
37869 # pragma omp taskyield new-line */
37872 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
37874 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37875 finish_omp_taskyield ();
37879 # pragma omp taskgroup new-line
37883 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37885 #define OMP_TASKGROUP_CLAUSE_MASK \
37886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37889 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37892 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
37893 "#pragma omp taskgroup", pragma_tok
);
37894 return c_finish_omp_taskgroup (input_location
,
37895 cp_parser_omp_structured_block (parser
,
37902 # pragma omp threadprivate (variable-list) */
37905 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
37909 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37910 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37912 finish_omp_threadprivate (vars
);
37916 # pragma omp cancel cancel-clause[optseq] new-line */
37918 #define OMP_CANCEL_CLAUSE_MASK \
37919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37926 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
37928 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
37929 "#pragma omp cancel", pragma_tok
);
37930 finish_omp_cancel (clauses
);
37934 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37936 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37943 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
37944 enum pragma_context context
)
37947 bool point_seen
= false;
37949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37951 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37952 const char *p
= IDENTIFIER_POINTER (id
);
37954 if (strcmp (p
, "point") == 0)
37956 cp_lexer_consume_token (parser
->lexer
);
37962 cp_parser_error (parser
, "expected %<point%>");
37963 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37967 if (context
!= pragma_compound
)
37969 if (context
== pragma_stmt
)
37970 error_at (pragma_tok
->location
,
37971 "%<#pragma %s%> may only be used in compound statements",
37972 "omp cancellation point");
37974 cp_parser_error (parser
, "expected declaration specifiers");
37975 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37979 clauses
= cp_parser_omp_all_clauses (parser
,
37980 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
37981 "#pragma omp cancellation point",
37983 finish_omp_cancellation_point (clauses
);
37987 #pragma omp distribute distribute-clause[optseq] new-line
37990 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37998 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
37999 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38002 tree clauses
, sb
, ret
;
38004 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38006 strcat (p_name
, " distribute");
38007 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
38009 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38011 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38012 const char *p
= IDENTIFIER_POINTER (id
);
38014 bool parallel
= false;
38016 if (strcmp (p
, "simd") == 0)
38019 parallel
= strcmp (p
, "parallel") == 0;
38020 if (parallel
|| simd
)
38022 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38023 if (cclauses
== NULL
)
38024 cclauses
= cclauses_buf
;
38025 cp_lexer_consume_token (parser
->lexer
);
38026 if (!flag_openmp
) /* flag_openmp_simd */
38029 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38032 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
38035 sb
= begin_omp_structured_block ();
38036 save
= cp_parser_begin_omp_structured_block (parser
);
38038 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38041 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
38043 cp_parser_end_omp_structured_block (parser
, save
);
38044 tree body
= finish_omp_structured_block (sb
);
38047 ret
= make_node (OMP_DISTRIBUTE
);
38048 TREE_TYPE (ret
) = void_type_node
;
38049 OMP_FOR_BODY (ret
) = body
;
38050 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
38051 SET_EXPR_LOCATION (ret
, loc
);
38056 if (!flag_openmp
) /* flag_openmp_simd */
38058 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38062 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38066 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
38067 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
38070 keep_next_level (true);
38071 sb
= begin_omp_structured_block ();
38072 save
= cp_parser_begin_omp_structured_block (parser
);
38074 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
38076 cp_parser_end_omp_structured_block (parser
, save
);
38077 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
38083 # pragma omp teams teams-clause[optseq] new-line
38084 structured-block */
38086 #define OMP_TEAMS_CLAUSE_MASK \
38087 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38096 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
38097 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38100 tree clauses
, sb
, ret
;
38102 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38104 strcat (p_name
, " teams");
38105 mask
|= OMP_TEAMS_CLAUSE_MASK
;
38107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38109 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38110 const char *p
= IDENTIFIER_POINTER (id
);
38111 if (strcmp (p
, "distribute") == 0)
38113 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38114 if (cclauses
== NULL
)
38115 cclauses
= cclauses_buf
;
38117 cp_lexer_consume_token (parser
->lexer
);
38118 if (!flag_openmp
) /* flag_openmp_simd */
38119 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
38121 keep_next_level (true);
38122 sb
= begin_omp_structured_block ();
38123 save
= cp_parser_begin_omp_structured_block (parser
);
38124 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
38126 cp_parser_end_omp_structured_block (parser
, save
);
38127 tree body
= finish_omp_structured_block (sb
);
38130 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38131 ret
= make_node (OMP_TEAMS
);
38132 TREE_TYPE (ret
) = void_type_node
;
38133 OMP_TEAMS_CLAUSES (ret
) = clauses
;
38134 OMP_TEAMS_BODY (ret
) = body
;
38135 OMP_TEAMS_COMBINED (ret
) = 1;
38136 SET_EXPR_LOCATION (ret
, loc
);
38137 return add_stmt (ret
);
38140 if (!flag_openmp
) /* flag_openmp_simd */
38142 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38146 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38150 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
38151 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38154 tree stmt
= make_node (OMP_TEAMS
);
38155 TREE_TYPE (stmt
) = void_type_node
;
38156 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
38157 keep_next_level (true);
38158 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38159 SET_EXPR_LOCATION (stmt
, loc
);
38161 return add_stmt (stmt
);
38165 # pragma omp target data target-data-clause[optseq] new-line
38166 structured-block */
38168 #define OMP_TARGET_DATA_CLAUSE_MASK \
38169 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38175 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38178 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
38179 "#pragma omp target data", pragma_tok
);
38181 for (tree
*pc
= &clauses
; *pc
;)
38183 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38184 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38187 case GOMP_MAP_ALWAYS_TO
:
38188 case GOMP_MAP_FROM
:
38189 case GOMP_MAP_ALWAYS_FROM
:
38190 case GOMP_MAP_TOFROM
:
38191 case GOMP_MAP_ALWAYS_TOFROM
:
38192 case GOMP_MAP_ALLOC
:
38195 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38196 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38197 case GOMP_MAP_ALWAYS_POINTER
:
38201 error_at (OMP_CLAUSE_LOCATION (*pc
),
38202 "%<#pragma omp target data%> with map-type other "
38203 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38204 "on %<map%> clause");
38205 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38208 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
)
38210 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38216 error_at (pragma_tok
->location
,
38217 "%<#pragma omp target data%> must contain at least "
38218 "one %<map%> or %<use_device_ptr%> clause");
38222 tree stmt
= make_node (OMP_TARGET_DATA
);
38223 TREE_TYPE (stmt
) = void_type_node
;
38224 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
38226 keep_next_level (true);
38227 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38229 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38230 return add_stmt (stmt
);
38234 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38235 structured-block */
38237 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38238 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38245 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38246 enum pragma_context context
)
38248 bool data_seen
= false;
38249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38251 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38252 const char *p
= IDENTIFIER_POINTER (id
);
38254 if (strcmp (p
, "data") == 0)
38256 cp_lexer_consume_token (parser
->lexer
);
38262 cp_parser_error (parser
, "expected %<data%>");
38263 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38267 if (context
== pragma_stmt
)
38269 error_at (pragma_tok
->location
,
38270 "%<#pragma %s%> may only be used in compound statements",
38271 "omp target enter data");
38272 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38277 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
38278 "#pragma omp target enter data", pragma_tok
);
38280 for (tree
*pc
= &clauses
; *pc
;)
38282 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38283 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38286 case GOMP_MAP_ALWAYS_TO
:
38287 case GOMP_MAP_ALLOC
:
38290 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38291 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38292 case GOMP_MAP_ALWAYS_POINTER
:
38296 error_at (OMP_CLAUSE_LOCATION (*pc
),
38297 "%<#pragma omp target enter data%> with map-type other "
38298 "than %<to%> or %<alloc%> on %<map%> clause");
38299 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38302 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38308 error_at (pragma_tok
->location
,
38309 "%<#pragma omp target enter data%> must contain at least "
38310 "one %<map%> clause");
38314 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
38315 TREE_TYPE (stmt
) = void_type_node
;
38316 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
38317 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38318 return add_stmt (stmt
);
38322 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38323 structured-block */
38325 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38326 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38333 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38334 enum pragma_context context
)
38336 bool data_seen
= false;
38337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38339 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38340 const char *p
= IDENTIFIER_POINTER (id
);
38342 if (strcmp (p
, "data") == 0)
38344 cp_lexer_consume_token (parser
->lexer
);
38350 cp_parser_error (parser
, "expected %<data%>");
38351 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38355 if (context
== pragma_stmt
)
38357 error_at (pragma_tok
->location
,
38358 "%<#pragma %s%> may only be used in compound statements",
38359 "omp target exit data");
38360 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38365 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
38366 "#pragma omp target exit data", pragma_tok
);
38368 for (tree
*pc
= &clauses
; *pc
;)
38370 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38371 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38373 case GOMP_MAP_FROM
:
38374 case GOMP_MAP_ALWAYS_FROM
:
38375 case GOMP_MAP_RELEASE
:
38376 case GOMP_MAP_DELETE
:
38379 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38380 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38381 case GOMP_MAP_ALWAYS_POINTER
:
38385 error_at (OMP_CLAUSE_LOCATION (*pc
),
38386 "%<#pragma omp target exit data%> with map-type other "
38387 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38389 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38392 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38398 error_at (pragma_tok
->location
,
38399 "%<#pragma omp target exit data%> must contain at least "
38400 "one %<map%> clause");
38404 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
38405 TREE_TYPE (stmt
) = void_type_node
;
38406 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
38407 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38408 return add_stmt (stmt
);
38412 # pragma omp target update target-update-clause[optseq] new-line */
38414 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38423 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
38424 enum pragma_context context
)
38426 if (context
== pragma_stmt
)
38428 error_at (pragma_tok
->location
,
38429 "%<#pragma %s%> may only be used in compound statements",
38430 "omp target update");
38431 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38436 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
38437 "#pragma omp target update", pragma_tok
);
38438 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
38439 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
38441 error_at (pragma_tok
->location
,
38442 "%<#pragma omp target update%> must contain at least one "
38443 "%<from%> or %<to%> clauses");
38447 tree stmt
= make_node (OMP_TARGET_UPDATE
);
38448 TREE_TYPE (stmt
) = void_type_node
;
38449 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
38450 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38456 # pragma omp target target-clause[optseq] new-line
38457 structured-block */
38459 #define OMP_TARGET_CLAUSE_MASK \
38460 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38471 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
38472 enum pragma_context context
, bool *if_p
)
38474 tree
*pc
= NULL
, stmt
;
38478 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
38480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38482 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38483 const char *p
= IDENTIFIER_POINTER (id
);
38484 enum tree_code ccode
= ERROR_MARK
;
38486 if (strcmp (p
, "teams") == 0)
38488 else if (strcmp (p
, "parallel") == 0)
38489 ccode
= OMP_PARALLEL
;
38490 else if (strcmp (p
, "simd") == 0)
38492 if (ccode
!= ERROR_MARK
)
38494 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
38495 char p_name
[sizeof ("#pragma omp target teams distribute "
38496 "parallel for simd")];
38498 cp_lexer_consume_token (parser
->lexer
);
38499 strcpy (p_name
, "#pragma omp target");
38500 if (!flag_openmp
) /* flag_openmp_simd */
38506 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38507 OMP_TARGET_CLAUSE_MASK
,
38511 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38512 OMP_TARGET_CLAUSE_MASK
,
38516 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38517 OMP_TARGET_CLAUSE_MASK
,
38521 gcc_unreachable ();
38523 return stmt
!= NULL_TREE
;
38525 keep_next_level (true);
38526 tree sb
= begin_omp_structured_block (), ret
;
38527 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
38531 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
38532 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38536 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
38537 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38541 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
38542 OMP_TARGET_CLAUSE_MASK
, cclauses
,
38546 gcc_unreachable ();
38548 cp_parser_end_omp_structured_block (parser
, save
);
38549 tree body
= finish_omp_structured_block (sb
);
38550 if (ret
== NULL_TREE
)
38552 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
38554 /* For combined target teams, ensure the num_teams and
38555 thread_limit clause expressions are evaluated on the host,
38556 before entering the target construct. */
38558 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
38559 c
; c
= OMP_CLAUSE_CHAIN (c
))
38560 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
38561 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
38562 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
38564 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
38565 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
38566 if (expr
== error_mark_node
)
38568 tree tmp
= TARGET_EXPR_SLOT (expr
);
38570 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
38571 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
38572 OMP_CLAUSE_FIRSTPRIVATE
);
38573 OMP_CLAUSE_DECL (tc
) = tmp
;
38574 OMP_CLAUSE_CHAIN (tc
)
38575 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38576 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
38579 tree stmt
= make_node (OMP_TARGET
);
38580 TREE_TYPE (stmt
) = void_type_node
;
38581 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
38582 OMP_TARGET_BODY (stmt
) = body
;
38583 OMP_TARGET_COMBINED (stmt
) = 1;
38584 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38586 pc
= &OMP_TARGET_CLAUSES (stmt
);
38587 goto check_clauses
;
38589 else if (!flag_openmp
) /* flag_openmp_simd */
38591 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38594 else if (strcmp (p
, "data") == 0)
38596 cp_lexer_consume_token (parser
->lexer
);
38597 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
38600 else if (strcmp (p
, "enter") == 0)
38602 cp_lexer_consume_token (parser
->lexer
);
38603 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
38606 else if (strcmp (p
, "exit") == 0)
38608 cp_lexer_consume_token (parser
->lexer
);
38609 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
38612 else if (strcmp (p
, "update") == 0)
38614 cp_lexer_consume_token (parser
->lexer
);
38615 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
38618 if (!flag_openmp
) /* flag_openmp_simd */
38620 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38624 stmt
= make_node (OMP_TARGET
);
38625 TREE_TYPE (stmt
) = void_type_node
;
38627 OMP_TARGET_CLAUSES (stmt
)
38628 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
38629 "#pragma omp target", pragma_tok
);
38630 pc
= &OMP_TARGET_CLAUSES (stmt
);
38631 keep_next_level (true);
38632 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
38634 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38640 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
38641 switch (OMP_CLAUSE_MAP_KIND (*pc
))
38644 case GOMP_MAP_ALWAYS_TO
:
38645 case GOMP_MAP_FROM
:
38646 case GOMP_MAP_ALWAYS_FROM
:
38647 case GOMP_MAP_TOFROM
:
38648 case GOMP_MAP_ALWAYS_TOFROM
:
38649 case GOMP_MAP_ALLOC
:
38650 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38651 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
38652 case GOMP_MAP_ALWAYS_POINTER
:
38655 error_at (OMP_CLAUSE_LOCATION (*pc
),
38656 "%<#pragma omp target%> with map-type other "
38657 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38658 "on %<map%> clause");
38659 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38662 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38668 # pragma acc cache (variable-list) new-line
38672 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
38674 tree stmt
, clauses
;
38676 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
38677 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38679 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
38681 stmt
= make_node (OACC_CACHE
);
38682 TREE_TYPE (stmt
) = void_type_node
;
38683 OACC_CACHE_CLAUSES (stmt
) = clauses
;
38684 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38691 # pragma acc data oacc-data-clause[optseq] new-line
38692 structured-block */
38694 #define OACC_DATA_CLAUSE_MASK \
38695 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38704 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38706 tree stmt
, clauses
, block
;
38709 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
38710 "#pragma acc data", pragma_tok
);
38712 block
= begin_omp_parallel ();
38713 save
= cp_parser_begin_omp_structured_block (parser
);
38714 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38715 cp_parser_end_omp_structured_block (parser
, save
);
38716 stmt
= finish_oacc_data (clauses
, block
);
38721 # pragma acc host_data <clauses> new-line
38722 structured-block */
38724 #define OACC_HOST_DATA_CLAUSE_MASK \
38725 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38728 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38730 tree stmt
, clauses
, block
;
38733 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
38734 "#pragma acc host_data", pragma_tok
);
38736 block
= begin_omp_parallel ();
38737 save
= cp_parser_begin_omp_structured_block (parser
);
38738 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38739 cp_parser_end_omp_structured_block (parser
, save
);
38740 stmt
= finish_oacc_host_data (clauses
, block
);
38745 # pragma acc declare oacc-data-clause[optseq] new-line
38748 #define OACC_DECLARE_CLAUSE_MASK \
38749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38759 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
38761 tree clauses
, stmt
;
38762 bool error
= false;
38764 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
38765 "#pragma acc declare", pragma_tok
, true);
38768 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38770 error_at (pragma_tok
->location
,
38771 "no valid clauses specified in %<#pragma acc declare%>");
38775 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
38777 location_t loc
= OMP_CLAUSE_LOCATION (t
);
38778 tree decl
= OMP_CLAUSE_DECL (t
);
38779 if (!DECL_P (decl
))
38781 error_at (loc
, "array section in %<#pragma acc declare%>");
38785 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
38786 switch (OMP_CLAUSE_MAP_KIND (t
))
38788 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
38789 case GOMP_MAP_ALLOC
:
38791 case GOMP_MAP_FORCE_DEVICEPTR
:
38792 case GOMP_MAP_DEVICE_RESIDENT
:
38795 case GOMP_MAP_LINK
:
38796 if (!global_bindings_p ()
38797 && (TREE_STATIC (decl
)
38798 || !DECL_EXTERNAL (decl
)))
38801 "%qD must be a global variable in "
38802 "%<#pragma acc declare link%>",
38810 if (global_bindings_p ())
38812 error_at (loc
, "invalid OpenACC clause at file scope");
38816 if (DECL_EXTERNAL (decl
))
38819 "invalid use of %<extern%> variable %qD "
38820 "in %<#pragma acc declare%>", decl
);
38824 else if (TREE_PUBLIC (decl
))
38827 "invalid use of %<global%> variable %qD "
38828 "in %<#pragma acc declare%>", decl
);
38835 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
38836 || lookup_attribute ("omp declare target link",
38837 DECL_ATTRIBUTES (decl
)))
38839 error_at (loc
, "variable %qD used more than once with "
38840 "%<#pragma acc declare%>", decl
);
38849 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
38850 id
= get_identifier ("omp declare target link");
38852 id
= get_identifier ("omp declare target");
38854 DECL_ATTRIBUTES (decl
)
38855 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
38856 if (global_bindings_p ())
38858 symtab_node
*node
= symtab_node::get (decl
);
38861 node
->offloadable
= 1;
38862 if (ENABLE_OFFLOADING
)
38864 g
->have_offload
= true;
38865 if (is_a
<varpool_node
*> (node
))
38866 vec_safe_push (offload_vars
, decl
);
38873 if (error
|| global_bindings_p ())
38876 stmt
= make_node (OACC_DECLARE
);
38877 TREE_TYPE (stmt
) = void_type_node
;
38878 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
38879 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
38887 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38891 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38893 LOC is the location of the #pragma token.
38896 #define OACC_ENTER_DATA_CLAUSE_MASK \
38897 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38903 #define OACC_EXIT_DATA_CLAUSE_MASK \
38904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38912 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
38915 location_t loc
= pragma_tok
->location
;
38916 tree stmt
, clauses
;
38917 const char *p
= "";
38919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38920 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
38922 if (strcmp (p
, "data") != 0)
38924 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
38925 enter
? "enter" : "exit");
38926 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38930 cp_lexer_consume_token (parser
->lexer
);
38933 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
38934 "#pragma acc enter data", pragma_tok
);
38936 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
38937 "#pragma acc exit data", pragma_tok
);
38939 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
38941 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
38942 enter
? "enter" : "exit");
38946 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
38947 TREE_TYPE (stmt
) = void_type_node
;
38948 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
38949 SET_EXPR_LOCATION (stmt
, loc
);
38955 # pragma acc loop oacc-loop-clause[optseq] new-line
38956 structured-block */
38958 #define OACC_LOOP_CLAUSE_MASK \
38959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38971 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
38972 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
38974 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
38976 strcat (p_name
, " loop");
38977 mask
|= OACC_LOOP_CLAUSE_MASK
;
38979 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38983 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
38985 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
38987 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
38990 tree block
= begin_omp_structured_block ();
38991 int save
= cp_parser_begin_omp_structured_block (parser
);
38992 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
38993 cp_parser_end_omp_structured_block (parser
, save
);
38994 add_stmt (finish_omp_structured_block (block
));
39000 # pragma acc kernels oacc-kernels-clause[optseq] new-line
39005 # pragma acc parallel oacc-parallel-clause[optseq] new-line
39009 #define OACC_KERNELS_CLAUSE_MASK \
39010 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39024 #define OACC_PARALLEL_CLAUSE_MASK \
39025 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39043 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
39044 char *p_name
, bool *if_p
)
39046 omp_clause_mask mask
;
39047 enum tree_code code
;
39048 switch (cp_parser_pragma_kind (pragma_tok
))
39050 case PRAGMA_OACC_KERNELS
:
39051 strcat (p_name
, " kernels");
39052 mask
= OACC_KERNELS_CLAUSE_MASK
;
39053 code
= OACC_KERNELS
;
39055 case PRAGMA_OACC_PARALLEL
:
39056 strcat (p_name
, " parallel");
39057 mask
= OACC_PARALLEL_CLAUSE_MASK
;
39058 code
= OACC_PARALLEL
;
39061 gcc_unreachable ();
39064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39067 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
39068 if (strcmp (p
, "loop") == 0)
39070 cp_lexer_consume_token (parser
->lexer
);
39071 tree block
= begin_omp_parallel ();
39073 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
39075 protected_set_expr_location (stmt
, pragma_tok
->location
);
39076 return finish_omp_construct (code
, block
, clauses
);
39080 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
39082 tree block
= begin_omp_parallel ();
39083 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
39084 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39085 cp_parser_end_omp_structured_block (parser
, save
);
39086 return finish_omp_construct (code
, block
, clauses
);
39090 # pragma acc update oacc-update-clause[optseq] new-line
39093 #define OACC_UPDATE_CLAUSE_MASK \
39094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39102 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
39104 tree stmt
, clauses
;
39106 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
39107 "#pragma acc update", pragma_tok
);
39109 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
39111 error_at (pragma_tok
->location
,
39112 "%<#pragma acc update%> must contain at least one "
39113 "%<device%> or %<host%> or %<self%> clause");
39117 stmt
= make_node (OACC_UPDATE
);
39118 TREE_TYPE (stmt
) = void_type_node
;
39119 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
39120 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39126 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39128 LOC is the location of the #pragma token.
39131 #define OACC_WAIT_CLAUSE_MASK \
39132 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39135 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
39137 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
39138 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39140 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
39141 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
39143 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
39144 "#pragma acc wait", pragma_tok
);
39146 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
39147 stmt
= finish_expr_stmt (stmt
);
39153 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39155 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39164 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
39165 enum pragma_context context
)
39167 bool first_p
= parser
->omp_declare_simd
== NULL
;
39168 cp_omp_declare_simd_data data
;
39171 data
.error_seen
= false;
39172 data
.fndecl_seen
= false;
39173 data
.tokens
= vNULL
;
39174 data
.clauses
= NULL_TREE
;
39175 /* It is safe to take the address of a local variable; it will only be
39176 used while this scope is live. */
39177 parser
->omp_declare_simd
= &data
;
39180 /* Store away all pragma tokens. */
39181 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39182 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39183 cp_lexer_consume_token (parser
->lexer
);
39184 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39185 parser
->omp_declare_simd
->error_seen
= true;
39186 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39187 struct cp_token_cache
*cp
39188 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
39189 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
39193 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
39194 cp_parser_pragma (parser
, context
, NULL
);
39197 case pragma_external
:
39198 cp_parser_declaration (parser
);
39200 case pragma_member
:
39201 cp_parser_member_declaration (parser
);
39203 case pragma_objc_icode
:
39204 cp_parser_block_declaration (parser
, /*statement_p=*/false);
39207 cp_parser_declaration_statement (parser
);
39210 if (parser
->omp_declare_simd
39211 && !parser
->omp_declare_simd
->error_seen
39212 && !parser
->omp_declare_simd
->fndecl_seen
)
39213 error_at (pragma_tok
->location
,
39214 "%<#pragma omp declare simd%> not immediately followed by "
39215 "function declaration or definition");
39216 data
.tokens
.release ();
39217 parser
->omp_declare_simd
= NULL
;
39221 /* Finalize #pragma omp declare simd clauses after direct declarator has
39222 been parsed, and put that into "omp declare simd" attribute. */
39225 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
39227 struct cp_token_cache
*ce
;
39228 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
39231 if (!data
->error_seen
&& data
->fndecl_seen
)
39233 error ("%<#pragma omp declare simd%> not immediately followed by "
39234 "a single function declaration or definition");
39235 data
->error_seen
= true;
39237 if (data
->error_seen
)
39240 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
39244 cp_parser_push_lexer_for_tokens (parser
, ce
);
39245 parser
->lexer
->in_pragma
= true;
39246 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
39247 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
39248 cp_lexer_consume_token (parser
->lexer
);
39249 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
39250 "#pragma omp declare simd", pragma_tok
);
39251 cp_parser_pop_lexer (parser
);
39253 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
39254 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
39255 TREE_CHAIN (c
) = attrs
;
39256 if (processing_template_decl
)
39257 ATTR_IS_DEPENDENT (c
) = 1;
39261 data
->fndecl_seen
= true;
39267 # pragma omp declare target new-line
39268 declarations and definitions
39269 # pragma omp end declare target new-line
39272 # pragma omp declare target ( extended-list ) new-line
39274 # pragma omp declare target declare-target-clauses[seq] new-line */
39276 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39281 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39283 tree clauses
= NULL_TREE
;
39284 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39286 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
39287 "#pragma omp declare target", pragma_tok
);
39288 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39290 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
39292 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39293 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39297 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39298 scope_chain
->omp_declare_target_attribute
++;
39301 if (scope_chain
->omp_declare_target_attribute
)
39302 error_at (pragma_tok
->location
,
39303 "%<#pragma omp declare target%> with clauses in between "
39304 "%<#pragma omp declare target%> without clauses and "
39305 "%<#pragma omp end declare target%>");
39306 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39308 tree t
= OMP_CLAUSE_DECL (c
), id
;
39309 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
39310 tree at2
= lookup_attribute ("omp declare target link",
39311 DECL_ATTRIBUTES (t
));
39312 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
39314 id
= get_identifier ("omp declare target link");
39315 std::swap (at1
, at2
);
39318 id
= get_identifier ("omp declare target");
39321 error_at (OMP_CLAUSE_LOCATION (c
),
39322 "%qD specified both in declare target %<link%> and %<to%>"
39328 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
39329 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
39332 symtab_node
*node
= symtab_node::get (t
);
39335 node
->offloadable
= 1;
39336 if (ENABLE_OFFLOADING
)
39338 g
->have_offload
= true;
39339 if (is_a
<varpool_node
*> (node
))
39340 vec_safe_push (offload_vars
, t
);
39348 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
39350 const char *p
= "";
39351 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39353 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39354 p
= IDENTIFIER_POINTER (id
);
39356 if (strcmp (p
, "declare") == 0)
39358 cp_lexer_consume_token (parser
->lexer
);
39360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39362 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39363 p
= IDENTIFIER_POINTER (id
);
39365 if (strcmp (p
, "target") == 0)
39366 cp_lexer_consume_token (parser
->lexer
);
39369 cp_parser_error (parser
, "expected %<target%>");
39370 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39376 cp_parser_error (parser
, "expected %<declare%>");
39377 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39380 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39381 if (!scope_chain
->omp_declare_target_attribute
)
39382 error_at (pragma_tok
->location
,
39383 "%<#pragma omp end declare target%> without corresponding "
39384 "%<#pragma omp declare target%>");
39386 scope_chain
->omp_declare_target_attribute
--;
39389 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39390 expression and optional initializer clause of
39391 #pragma omp declare reduction. We store the expression(s) as
39392 either 3, 6 or 7 special statements inside of the artificial function's
39393 body. The first two statements are DECL_EXPRs for the artificial
39394 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39395 expression that uses those variables.
39396 If there was any INITIALIZER clause, this is followed by further statements,
39397 the fourth and fifth statements are DECL_EXPRs for the artificial
39398 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39399 constructor variant (first token after open paren is not omp_priv),
39400 then the sixth statement is a statement with the function call expression
39401 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39402 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39403 to initialize the OMP_PRIV artificial variable and there is seventh
39404 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39407 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
39409 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
39410 gcc_assert (TYPE_REF_P (type
));
39411 type
= TREE_TYPE (type
);
39412 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
39413 DECL_ARTIFICIAL (omp_out
) = 1;
39414 pushdecl (omp_out
);
39415 add_decl_expr (omp_out
);
39416 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
39417 DECL_ARTIFICIAL (omp_in
) = 1;
39419 add_decl_expr (omp_in
);
39421 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
39423 keep_next_level (true);
39424 tree block
= begin_omp_structured_block ();
39425 combiner
= cp_parser_expression (parser
);
39426 finish_expr_stmt (combiner
);
39427 block
= finish_omp_structured_block (block
);
39430 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
39433 const char *p
= "";
39434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39436 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39437 p
= IDENTIFIER_POINTER (id
);
39440 if (strcmp (p
, "initializer") == 0)
39442 cp_lexer_consume_token (parser
->lexer
);
39443 matching_parens parens
;
39444 if (!parens
.require_open (parser
))
39448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39450 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39451 p
= IDENTIFIER_POINTER (id
);
39454 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
39455 DECL_ARTIFICIAL (omp_priv
) = 1;
39456 pushdecl (omp_priv
);
39457 add_decl_expr (omp_priv
);
39458 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
39459 DECL_ARTIFICIAL (omp_orig
) = 1;
39460 pushdecl (omp_orig
);
39461 add_decl_expr (omp_orig
);
39463 keep_next_level (true);
39464 block
= begin_omp_structured_block ();
39467 if (strcmp (p
, "omp_priv") == 0)
39469 bool is_direct_init
, is_non_constant_init
;
39471 cp_lexer_consume_token (parser
->lexer
);
39472 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
39474 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39475 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
39477 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
39478 == CPP_CLOSE_PAREN
))
39480 finish_omp_structured_block (block
);
39481 error ("invalid initializer clause");
39484 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
39485 &is_non_constant_init
);
39486 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
39487 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
39491 cp_parser_parse_tentatively (parser
);
39492 /* Don't create location wrapper nodes here. */
39493 auto_suppress_location_wrappers sentinel
;
39494 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39495 /*check_dependency_p=*/true,
39496 /*template_p=*/NULL
,
39497 /*declarator_p=*/false,
39498 /*optional_p=*/false);
39499 vec
<tree
, va_gc
> *args
;
39500 if (fn_name
== error_mark_node
39501 || cp_parser_error_occurred (parser
)
39502 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
39503 || ((args
= cp_parser_parenthesized_expression_list
39504 (parser
, non_attr
, /*cast_p=*/false,
39505 /*allow_expansion_p=*/true,
39506 /*non_constant_p=*/NULL
)),
39507 cp_parser_error_occurred (parser
)))
39509 finish_omp_structured_block (block
);
39510 cp_parser_abort_tentative_parse (parser
);
39511 cp_parser_error (parser
, "expected id-expression (arguments)");
39516 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
39517 if (arg
== omp_priv
39518 || (TREE_CODE (arg
) == ADDR_EXPR
39519 && TREE_OPERAND (arg
, 0) == omp_priv
))
39521 cp_parser_abort_tentative_parse (parser
);
39522 if (arg
== NULL_TREE
)
39523 error ("one of the initializer call arguments should be %<omp_priv%>"
39524 " or %<&omp_priv%>");
39525 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
39527 finish_expr_stmt (initializer
);
39530 block
= finish_omp_structured_block (block
);
39531 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
39535 add_decl_expr (omp_orig
);
39537 if (!parens
.require_close (parser
))
39541 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
39542 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
39549 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39550 initializer-clause[opt] new-line
39552 initializer-clause:
39553 initializer (omp_priv initializer)
39554 initializer (function-name (argument-list)) */
39557 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
39558 enum pragma_context
)
39560 auto_vec
<tree
> types
;
39561 enum tree_code reduc_code
= ERROR_MARK
;
39562 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
39564 cp_token
*first_token
;
39565 cp_token_cache
*cp
;
39569 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39570 p
= obstack_alloc (&declarator_obstack
, 0);
39572 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
39575 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39578 reduc_code
= PLUS_EXPR
;
39581 reduc_code
= MULT_EXPR
;
39584 reduc_code
= MINUS_EXPR
;
39587 reduc_code
= BIT_AND_EXPR
;
39590 reduc_code
= BIT_XOR_EXPR
;
39593 reduc_code
= BIT_IOR_EXPR
;
39596 reduc_code
= TRUTH_ANDIF_EXPR
;
39599 reduc_code
= TRUTH_ORIF_EXPR
;
39602 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
39605 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39606 "%<|%>, %<&&%>, %<||%> or identifier");
39610 if (reduc_code
!= ERROR_MARK
)
39611 cp_lexer_consume_token (parser
->lexer
);
39613 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
39614 if (reduc_id
== error_mark_node
)
39617 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
39620 /* Types may not be defined in declare reduction type list. */
39621 const char *saved_message
;
39622 saved_message
= parser
->type_definition_forbidden_message
;
39623 parser
->type_definition_forbidden_message
39624 = G_("types may not be defined in declare reduction type list");
39625 bool saved_colon_corrects_to_scope_p
;
39626 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
39627 parser
->colon_corrects_to_scope_p
= false;
39628 bool saved_colon_doesnt_start_class_def_p
;
39629 saved_colon_doesnt_start_class_def_p
39630 = parser
->colon_doesnt_start_class_def_p
;
39631 parser
->colon_doesnt_start_class_def_p
= true;
39635 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39636 type
= cp_parser_type_id (parser
);
39637 if (type
== error_mark_node
)
39639 else if (ARITHMETIC_TYPE_P (type
)
39640 && (orig_reduc_id
== NULL_TREE
39641 || (TREE_CODE (type
) != COMPLEX_TYPE
39642 && (id_equal (orig_reduc_id
, "min")
39643 || id_equal (orig_reduc_id
, "max")))))
39644 error_at (loc
, "predeclared arithmetic type %qT in "
39645 "%<#pragma omp declare reduction%>", type
);
39646 else if (TREE_CODE (type
) == FUNCTION_TYPE
39647 || TREE_CODE (type
) == METHOD_TYPE
39648 || TREE_CODE (type
) == ARRAY_TYPE
)
39649 error_at (loc
, "function or array type %qT in "
39650 "%<#pragma omp declare reduction%>", type
);
39651 else if (TYPE_REF_P (type
))
39652 error_at (loc
, "reference type %qT in "
39653 "%<#pragma omp declare reduction%>", type
);
39654 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
39655 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
39656 "%<#pragma omp declare reduction%>", type
);
39658 types
.safe_push (type
);
39660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39661 cp_lexer_consume_token (parser
->lexer
);
39666 /* Restore the saved message. */
39667 parser
->type_definition_forbidden_message
= saved_message
;
39668 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
39669 parser
->colon_doesnt_start_class_def_p
39670 = saved_colon_doesnt_start_class_def_p
;
39672 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
39673 || types
.is_empty ())
39676 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39680 first_token
= cp_lexer_peek_token (parser
->lexer
);
39683 FOR_EACH_VEC_ELT (types
, i
, type
)
39686 = build_function_type_list (void_type_node
,
39687 cp_build_reference_type (type
, false),
39689 tree this_reduc_id
= reduc_id
;
39690 if (!dependent_type_p (type
))
39691 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
39692 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
39693 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
39694 DECL_ARTIFICIAL (fndecl
) = 1;
39695 DECL_EXTERNAL (fndecl
) = 1;
39696 DECL_DECLARED_INLINE_P (fndecl
) = 1;
39697 DECL_IGNORED_P (fndecl
) = 1;
39698 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
39699 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
39700 DECL_ATTRIBUTES (fndecl
)
39701 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
39702 DECL_ATTRIBUTES (fndecl
));
39703 if (processing_template_decl
)
39704 fndecl
= push_template_decl (fndecl
);
39705 bool block_scope
= false;
39706 tree block
= NULL_TREE
;
39707 if (current_function_decl
)
39709 block_scope
= true;
39710 DECL_CONTEXT (fndecl
) = global_namespace
;
39711 if (!processing_template_decl
)
39714 else if (current_class_type
)
39718 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
39719 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
39720 cp_lexer_consume_token (parser
->lexer
);
39721 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39723 cp
= cp_token_cache_new (first_token
,
39724 cp_lexer_peek_nth_token (parser
->lexer
,
39727 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
39728 finish_member_declaration (fndecl
);
39729 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
39730 DECL_PENDING_INLINE_P (fndecl
) = 1;
39731 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
39736 DECL_CONTEXT (fndecl
) = current_namespace
;
39740 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
39742 block
= begin_omp_structured_block ();
39745 cp_parser_push_lexer_for_tokens (parser
, cp
);
39746 parser
->lexer
->in_pragma
= true;
39748 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
39751 finish_function (/*inline_p=*/false);
39753 DECL_CONTEXT (fndecl
) = current_function_decl
;
39755 cp_parser_pop_lexer (parser
);
39759 cp_parser_pop_lexer (parser
);
39761 finish_function (/*inline_p=*/false);
39764 DECL_CONTEXT (fndecl
) = current_function_decl
;
39765 block
= finish_omp_structured_block (block
);
39766 if (TREE_CODE (block
) == BIND_EXPR
)
39767 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
39768 else if (TREE_CODE (block
) == STATEMENT_LIST
)
39769 DECL_SAVED_TREE (fndecl
) = block
;
39770 if (processing_template_decl
)
39771 add_decl_expr (fndecl
);
39773 cp_check_omp_declare_reduction (fndecl
);
39774 if (cp
== NULL
&& types
.length () > 1)
39775 cp
= cp_token_cache_new (first_token
,
39776 cp_lexer_peek_nth_token (parser
->lexer
, 2));
39777 if (errs
!= errorcount
)
39781 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39784 /* Free any declarators allocated. */
39785 obstack_free (&declarator_obstack
, p
);
39789 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39790 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39791 initializer-clause[opt] new-line
39792 #pragma omp declare target new-line */
39795 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
39796 enum pragma_context context
)
39798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39800 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39801 const char *p
= IDENTIFIER_POINTER (id
);
39803 if (strcmp (p
, "simd") == 0)
39805 cp_lexer_consume_token (parser
->lexer
);
39806 cp_parser_omp_declare_simd (parser
, pragma_tok
,
39810 cp_ensure_no_omp_declare_simd (parser
);
39811 if (strcmp (p
, "reduction") == 0)
39813 cp_lexer_consume_token (parser
->lexer
);
39814 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
39818 if (!flag_openmp
) /* flag_openmp_simd */
39820 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39823 if (strcmp (p
, "target") == 0)
39825 cp_lexer_consume_token (parser
->lexer
);
39826 cp_parser_omp_declare_target (parser
, pragma_tok
);
39830 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
39832 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39837 #pragma omp requires clauses[optseq] new-line */
39840 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
39843 enum omp_requires new_req
= (enum omp_requires
) 0;
39845 location_t loc
= pragma_tok
->location
;
39846 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
39848 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
39849 cp_lexer_consume_token (parser
->lexer
);
39853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39855 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39856 const char *p
= IDENTIFIER_POINTER (id
);
39857 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39858 enum omp_requires this_req
= (enum omp_requires
) 0;
39860 if (!strcmp (p
, "unified_address"))
39861 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
39862 else if (!strcmp (p
, "unified_shared_memory"))
39863 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
39864 else if (!strcmp (p
, "dynamic_allocators"))
39865 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
39866 else if (!strcmp (p
, "reverse_offload"))
39867 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
39868 else if (!strcmp (p
, "atomic_default_mem_order"))
39870 cp_lexer_consume_token (parser
->lexer
);
39872 matching_parens parens
;
39873 if (parens
.require_open (parser
))
39875 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39877 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39878 p
= IDENTIFIER_POINTER (id
);
39880 if (!strcmp (p
, "seq_cst"))
39882 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
39883 else if (!strcmp (p
, "relaxed"))
39885 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
39886 else if (!strcmp (p
, "acq_rel"))
39888 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
39892 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39893 "expected %<seq_cst%>, %<relaxed%> or "
39895 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
39897 cp_lexer_consume_token (parser
->lexer
);
39900 cp_lexer_consume_token (parser
->lexer
);
39902 if (!parens
.require_close (parser
))
39903 cp_parser_skip_to_closing_parenthesis (parser
,
39904 /*recovering=*/true,
39905 /*or_comma=*/false,
39911 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39919 error_at (cloc
, "expected %<unified_address%>, "
39920 "%<unified_shared_memory%>, "
39921 "%<dynamic_allocators%>, "
39922 "%<reverse_offload%> "
39923 "or %<atomic_default_mem_order%> clause");
39924 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39928 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
39929 "supported yet", p
);
39931 cp_lexer_consume_token (parser
->lexer
);
39934 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39936 if ((this_req
& new_req
) != 0)
39937 error_at (cloc
, "too many %qs clauses", p
);
39938 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
39939 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
39940 error_at (cloc
, "%qs clause used lexically after first "
39941 "target construct or offloading API", p
);
39943 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39945 error_at (cloc
, "too many %qs clauses",
39946 "atomic_default_mem_order");
39947 this_req
= (enum omp_requires
) 0;
39949 else if ((omp_requires_mask
39950 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
39952 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
39953 " clause in a single compilation unit");
39955 = (enum omp_requires
)
39957 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
39959 else if ((omp_requires_mask
39960 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
39961 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
39962 "lexically after first %<atomic%> construct "
39963 "without memory order clause");
39964 new_req
= (enum omp_requires
) (new_req
| this_req
);
39966 = (enum omp_requires
) (omp_requires_mask
| this_req
);
39972 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39975 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
39981 #pragma omp taskloop taskloop-clause[optseq] new-line
39984 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39987 #define OMP_TASKLOOP_CLAUSE_MASK \
39988 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
40001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40006 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
40007 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40010 tree clauses
, sb
, ret
;
40012 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40014 strcat (p_name
, " taskloop");
40015 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
40016 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
40018 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
40019 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
40021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40023 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40024 const char *p
= IDENTIFIER_POINTER (id
);
40026 if (strcmp (p
, "simd") == 0)
40028 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40029 if (cclauses
== NULL
)
40030 cclauses
= cclauses_buf
;
40032 cp_lexer_consume_token (parser
->lexer
);
40033 if (!flag_openmp
) /* flag_openmp_simd */
40034 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40036 sb
= begin_omp_structured_block ();
40037 save
= cp_parser_begin_omp_structured_block (parser
);
40038 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40040 cp_parser_end_omp_structured_block (parser
, save
);
40041 tree body
= finish_omp_structured_block (sb
);
40044 ret
= make_node (OMP_TASKLOOP
);
40045 TREE_TYPE (ret
) = void_type_node
;
40046 OMP_FOR_BODY (ret
) = body
;
40047 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
40048 SET_EXPR_LOCATION (ret
, loc
);
40053 if (!flag_openmp
) /* flag_openmp_simd */
40055 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40059 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40063 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
40064 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
40067 keep_next_level (true);
40068 sb
= begin_omp_structured_block ();
40069 save
= cp_parser_begin_omp_structured_block (parser
);
40071 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
40074 cp_parser_end_omp_structured_block (parser
, save
);
40075 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
40082 # pragma acc routine oacc-routine-clause[optseq] new-line
40083 function-definition
40085 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40088 #define OACC_ROUTINE_CLAUSE_MASK \
40089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40095 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40096 component, which must resolve to a declared namespace-scope
40097 function. The clauses are either processed directly (for a named
40098 function), or defered until the immediatley following declaration
40102 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
40103 enum pragma_context context
)
40105 gcc_checking_assert (context
== pragma_external
);
40106 /* The checking for "another pragma following this one" in the "no optional
40107 '( name )'" case makes sure that we dont re-enter. */
40108 gcc_checking_assert (parser
->oacc_routine
== NULL
);
40110 cp_oacc_routine_data data
;
40111 data
.error_seen
= false;
40112 data
.fndecl_seen
= false;
40113 data
.tokens
= vNULL
;
40114 data
.clauses
= NULL_TREE
;
40115 data
.loc
= pragma_tok
->location
;
40116 /* It is safe to take the address of a local variable; it will only be
40117 used while this scope is live. */
40118 parser
->oacc_routine
= &data
;
40120 /* Look for optional '( name )'. */
40121 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
40123 matching_parens parens
;
40124 parens
.consume_open (parser
); /* '(' */
40126 /* We parse the name as an id-expression. If it resolves to
40127 anything other than a non-overloaded function at namespace
40128 scope, it's an error. */
40129 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40130 tree name
= cp_parser_id_expression (parser
,
40131 /*template_keyword_p=*/false,
40132 /*check_dependency_p=*/false,
40133 /*template_p=*/NULL
,
40134 /*declarator_p=*/false,
40135 /*optional_p=*/false);
40136 tree decl
= (identifier_p (name
)
40137 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
40139 if (name
!= error_mark_node
&& decl
== error_mark_node
)
40140 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
40142 if (decl
== error_mark_node
40143 || !parens
.require_close (parser
))
40145 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40146 parser
->oacc_routine
= NULL
;
40151 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40152 "#pragma acc routine",
40153 cp_lexer_peek_token (parser
->lexer
));
40155 if (decl
&& is_overloaded_fn (decl
)
40156 && (TREE_CODE (decl
) != FUNCTION_DECL
40157 || DECL_FUNCTION_TEMPLATE_P (decl
)))
40159 error_at (name_loc
,
40160 "%<#pragma acc routine%> names a set of overloads");
40161 parser
->oacc_routine
= NULL
;
40165 /* Perhaps we should use the same rule as declarations in different
40167 if (!DECL_NAMESPACE_SCOPE_P (decl
))
40169 error_at (name_loc
,
40170 "%qD does not refer to a namespace scope function", decl
);
40171 parser
->oacc_routine
= NULL
;
40175 if (TREE_CODE (decl
) != FUNCTION_DECL
)
40177 error_at (name_loc
, "%qD does not refer to a function", decl
);
40178 parser
->oacc_routine
= NULL
;
40182 cp_finalize_oacc_routine (parser
, decl
, false);
40183 parser
->oacc_routine
= NULL
;
40185 else /* No optional '( name )'. */
40187 /* Store away all pragma tokens. */
40188 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
40189 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
40190 cp_lexer_consume_token (parser
->lexer
);
40191 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40192 parser
->oacc_routine
->error_seen
= true;
40193 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40194 struct cp_token_cache
*cp
40195 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
40196 parser
->oacc_routine
->tokens
.safe_push (cp
);
40198 /* Emit a helpful diagnostic if there's another pragma following this
40200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
40202 cp_ensure_no_oacc_routine (parser
);
40203 data
.tokens
.release ();
40204 /* ..., and then just keep going. */
40208 /* We only have to consider the pragma_external case here. */
40209 cp_parser_declaration (parser
);
40210 if (parser
->oacc_routine
40211 && !parser
->oacc_routine
->fndecl_seen
)
40212 cp_ensure_no_oacc_routine (parser
);
40214 parser
->oacc_routine
= NULL
;
40215 data
.tokens
.release ();
40219 /* Finalize #pragma acc routine clauses after direct declarator has
40223 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
40225 struct cp_token_cache
*ce
;
40226 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
40228 if (!data
->error_seen
&& data
->fndecl_seen
)
40230 error_at (data
->loc
,
40231 "%<#pragma acc routine%> not immediately followed by "
40232 "a single function declaration or definition");
40233 data
->error_seen
= true;
40235 if (data
->error_seen
)
40238 gcc_checking_assert (data
->tokens
.length () == 1);
40239 ce
= data
->tokens
[0];
40241 cp_parser_push_lexer_for_tokens (parser
, ce
);
40242 parser
->lexer
->in_pragma
= true;
40243 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
40245 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40246 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
40247 parser
->oacc_routine
->clauses
40248 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
40249 "#pragma acc routine", pragma_tok
);
40250 cp_parser_pop_lexer (parser
);
40251 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40257 /* Apply any saved OpenACC routine clauses to a just-parsed
40261 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
40263 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
40265 /* Keep going if we're in error reporting mode. */
40266 if (parser
->oacc_routine
->error_seen
40267 || fndecl
== error_mark_node
)
40270 if (parser
->oacc_routine
->fndecl_seen
)
40272 error_at (parser
->oacc_routine
->loc
,
40273 "%<#pragma acc routine%> not immediately followed by"
40274 " a single function declaration or definition");
40275 parser
->oacc_routine
= NULL
;
40278 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
40280 cp_ensure_no_oacc_routine (parser
);
40284 if (oacc_get_fn_attrib (fndecl
))
40286 error_at (parser
->oacc_routine
->loc
,
40287 "%<#pragma acc routine%> already applied to %qD", fndecl
);
40288 parser
->oacc_routine
= NULL
;
40292 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
40294 error_at (parser
->oacc_routine
->loc
,
40296 ? G_("%<#pragma acc routine%> must be applied before use")
40297 : G_("%<#pragma acc routine%> must be applied before "
40299 parser
->oacc_routine
= NULL
;
40303 /* Process the routine's dimension clauses. */
40304 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
40305 oacc_replace_fn_attrib (fndecl
, dims
);
40307 /* Add an "omp declare target" attribute. */
40308 DECL_ATTRIBUTES (fndecl
)
40309 = tree_cons (get_identifier ("omp declare target"),
40310 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
40312 /* Don't unset parser->oacc_routine here: we may still need it to
40313 diagnose wrong usage. But, remember that we've used this "#pragma acc
40315 parser
->oacc_routine
->fndecl_seen
= true;
40319 /* Main entry point to OpenMP statement pragmas. */
40322 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40325 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
40326 omp_clause_mask
mask (0);
40328 switch (cp_parser_pragma_kind (pragma_tok
))
40330 case PRAGMA_OACC_ATOMIC
:
40331 cp_parser_omp_atomic (parser
, pragma_tok
);
40333 case PRAGMA_OACC_CACHE
:
40334 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
40336 case PRAGMA_OACC_DATA
:
40337 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
40339 case PRAGMA_OACC_ENTER_DATA
:
40340 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
40342 case PRAGMA_OACC_EXIT_DATA
:
40343 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
40345 case PRAGMA_OACC_HOST_DATA
:
40346 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
40348 case PRAGMA_OACC_KERNELS
:
40349 case PRAGMA_OACC_PARALLEL
:
40350 strcpy (p_name
, "#pragma acc");
40351 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
40354 case PRAGMA_OACC_LOOP
:
40355 strcpy (p_name
, "#pragma acc");
40356 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40359 case PRAGMA_OACC_UPDATE
:
40360 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
40362 case PRAGMA_OACC_WAIT
:
40363 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
40365 case PRAGMA_OMP_ATOMIC
:
40366 cp_parser_omp_atomic (parser
, pragma_tok
);
40368 case PRAGMA_OMP_CRITICAL
:
40369 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
40371 case PRAGMA_OMP_DISTRIBUTE
:
40372 strcpy (p_name
, "#pragma omp");
40373 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
40376 case PRAGMA_OMP_FOR
:
40377 strcpy (p_name
, "#pragma omp");
40378 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
40381 case PRAGMA_OMP_MASTER
:
40382 strcpy (p_name
, "#pragma omp");
40383 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
40386 case PRAGMA_OMP_PARALLEL
:
40387 strcpy (p_name
, "#pragma omp");
40388 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
40391 case PRAGMA_OMP_SECTIONS
:
40392 strcpy (p_name
, "#pragma omp");
40393 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
40395 case PRAGMA_OMP_SIMD
:
40396 strcpy (p_name
, "#pragma omp");
40397 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
40400 case PRAGMA_OMP_SINGLE
:
40401 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
40403 case PRAGMA_OMP_TASK
:
40404 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
40406 case PRAGMA_OMP_TASKGROUP
:
40407 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
40409 case PRAGMA_OMP_TASKLOOP
:
40410 strcpy (p_name
, "#pragma omp");
40411 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
40414 case PRAGMA_OMP_TEAMS
:
40415 strcpy (p_name
, "#pragma omp");
40416 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
40420 gcc_unreachable ();
40423 protected_set_expr_location (stmt
, pragma_tok
->location
);
40426 /* Transactional Memory parsing routines. */
40428 /* Parse a transaction attribute.
40434 We use this instead of cp_parser_attributes_opt for transactions to avoid
40435 the pedwarn in C++98 mode. */
40438 cp_parser_txn_attribute_opt (cp_parser
*parser
)
40441 tree attr_name
, attr
= NULL
;
40443 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
40444 return cp_parser_attributes_opt (parser
);
40446 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
40448 cp_lexer_consume_token (parser
->lexer
);
40449 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
40452 token
= cp_lexer_peek_token (parser
->lexer
);
40453 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
40455 token
= cp_lexer_consume_token (parser
->lexer
);
40457 attr_name
= (token
->type
== CPP_KEYWORD
40458 /* For keywords, use the canonical spelling,
40459 not the parsed identifier. */
40460 ? ridpointers
[(int) token
->keyword
]
40462 attr
= build_tree_list (attr_name
, NULL_TREE
);
40465 cp_parser_error (parser
, "expected identifier");
40467 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40469 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
40473 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40475 transaction-statement:
40476 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40478 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40482 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
40484 unsigned char old_in
= parser
->in_transaction
;
40485 unsigned char this_in
= 1, new_in
;
40486 enum rid keyword
= token
->keyword
;
40487 tree stmt
, attrs
, noex
;
40489 cp_lexer_consume_token (parser
->lexer
);
40491 if (keyword
== RID_TRANSACTION_RELAXED
40492 || keyword
== RID_SYNCHRONIZED
)
40493 this_in
|= TM_STMT_ATTR_RELAXED
;
40496 attrs
= cp_parser_txn_attribute_opt (parser
);
40498 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40501 /* Parse a noexcept specification. */
40502 if (keyword
== RID_ATOMIC_NOEXCEPT
)
40503 noex
= boolean_true_node
;
40504 else if (keyword
== RID_ATOMIC_CANCEL
)
40506 /* cancel-and-throw is unimplemented. */
40507 sorry ("atomic_cancel");
40511 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
40513 /* Keep track if we're in the lexical scope of an outer transaction. */
40514 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
40516 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
40518 parser
->in_transaction
= new_in
;
40519 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
40520 parser
->in_transaction
= old_in
;
40522 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
40527 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40529 transaction-expression:
40530 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40531 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40535 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
40537 unsigned char old_in
= parser
->in_transaction
;
40538 unsigned char this_in
= 1;
40542 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40544 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40545 || keyword
== RID_TRANSACTION_RELAXED
);
40549 keyword
== RID_TRANSACTION_RELAXED
40550 ? G_("%<__transaction_relaxed%> without transactional memory "
40552 : G_("%<__transaction_atomic%> without transactional memory "
40553 "support enabled"));
40555 token
= cp_parser_require_keyword (parser
, keyword
,
40556 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40557 : RT_TRANSACTION_RELAXED
));
40558 gcc_assert (token
!= NULL
);
40560 if (keyword
== RID_TRANSACTION_RELAXED
)
40561 this_in
|= TM_STMT_ATTR_RELAXED
;
40563 /* Set this early. This might mean that we allow transaction_cancel in
40564 an expression that we find out later actually has to be a constexpr.
40565 However, we expect that cxx_constant_value will be able to deal with
40566 this; also, if the noexcept has no constexpr, then what we parse next
40567 really is a transaction's body. */
40568 parser
->in_transaction
= this_in
;
40570 /* Parse a noexcept specification. */
40571 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
40574 if (!noex
|| !noex_expr
40575 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40577 matching_parens parens
;
40578 parens
.require_open (parser
);
40580 expr
= cp_parser_expression (parser
);
40581 expr
= finish_parenthesized_expr (expr
);
40583 parens
.require_close (parser
);
40587 /* The only expression that is available got parsed for the noexcept
40588 already. noexcept is true then. */
40590 noex
= boolean_true_node
;
40593 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
40594 parser
->in_transaction
= old_in
;
40596 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
40597 return error_mark_node
;
40599 return (flag_tm
? expr
: error_mark_node
);
40602 /* Parse a function-transaction-block.
40604 function-transaction-block:
40605 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40607 __transaction_atomic txn-attribute[opt] function-try-block
40608 __transaction_relaxed ctor-initializer[opt] function-body
40609 __transaction_relaxed function-try-block
40613 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
40615 unsigned char old_in
= parser
->in_transaction
;
40616 unsigned char new_in
= 1;
40617 tree compound_stmt
, stmt
, attrs
;
40620 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
40621 || keyword
== RID_TRANSACTION_RELAXED
);
40622 token
= cp_parser_require_keyword (parser
, keyword
,
40623 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
40624 : RT_TRANSACTION_RELAXED
));
40625 gcc_assert (token
!= NULL
);
40627 if (keyword
== RID_TRANSACTION_RELAXED
)
40628 new_in
|= TM_STMT_ATTR_RELAXED
;
40631 attrs
= cp_parser_txn_attribute_opt (parser
);
40633 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
40636 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
40638 parser
->in_transaction
= new_in
;
40640 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
40641 cp_parser_function_try_block (parser
);
40643 cp_parser_ctor_initializer_opt_and_function_body
40644 (parser
, /*in_function_try_block=*/false);
40646 parser
->in_transaction
= old_in
;
40648 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
40651 /* Parse a __transaction_cancel statement.
40654 __transaction_cancel txn-attribute[opt] ;
40655 __transaction_cancel txn-attribute[opt] throw-expression ;
40657 ??? Cancel and throw is not yet implemented. */
40660 cp_parser_transaction_cancel (cp_parser
*parser
)
40663 bool is_outer
= false;
40666 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
40667 RT_TRANSACTION_CANCEL
);
40668 gcc_assert (token
!= NULL
);
40670 attrs
= cp_parser_txn_attribute_opt (parser
);
40672 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
40674 /* ??? Parse cancel-and-throw here. */
40676 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40680 error_at (token
->location
, "%<__transaction_cancel%> without "
40681 "transactional memory support enabled");
40682 return error_mark_node
;
40684 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
40686 error_at (token
->location
, "%<__transaction_cancel%> within a "
40687 "%<__transaction_relaxed%>");
40688 return error_mark_node
;
40692 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
40693 && !is_tm_may_cancel_outer (current_function_decl
))
40695 error_at (token
->location
, "outer %<__transaction_cancel%> not "
40696 "within outer %<__transaction_atomic%>");
40697 error_at (token
->location
,
40698 " or a %<transaction_may_cancel_outer%> function");
40699 return error_mark_node
;
40702 else if (parser
->in_transaction
== 0)
40704 error_at (token
->location
, "%<__transaction_cancel%> not within "
40705 "%<__transaction_atomic%>");
40706 return error_mark_node
;
40709 stmt
= build_tm_abort_call (token
->location
, is_outer
);
40717 static GTY (()) cp_parser
*the_parser
;
40720 /* Special handling for the first token or line in the file. The first
40721 thing in the file might be #pragma GCC pch_preprocess, which loads a
40722 PCH file, which is a GC collection point. So we need to handle this
40723 first pragma without benefit of an existing lexer structure.
40725 Always returns one token to the caller in *FIRST_TOKEN. This is
40726 either the true first token of the file, or the first token after
40727 the initial pragma. */
40730 cp_parser_initial_pragma (cp_token
*first_token
)
40734 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40735 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
40738 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40739 if (first_token
->type
== CPP_STRING
)
40741 name
= first_token
->u
.value
;
40743 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40744 if (first_token
->type
!= CPP_PRAGMA_EOL
)
40745 error_at (first_token
->location
,
40746 "junk at end of %<#pragma GCC pch_preprocess%>");
40749 error_at (first_token
->location
, "expected string literal");
40751 /* Skip to the end of the pragma. */
40752 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
40753 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40755 /* Now actually load the PCH file. */
40757 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
40759 /* Read one more token to return to our caller. We have to do this
40760 after reading the PCH file in, since its pointers have to be
40762 cp_lexer_get_preprocessor_token (NULL
, first_token
);
40765 /* Parse a pragma GCC ivdep. */
40768 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
40770 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40774 /* Parse a pragma GCC unroll. */
40776 static unsigned short
40777 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
40779 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
40780 tree expr
= cp_parser_constant_expression (parser
);
40781 unsigned short unroll
;
40782 expr
= maybe_constant_value (expr
);
40783 HOST_WIDE_INT lunroll
= 0;
40784 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
40785 || TREE_CODE (expr
) != INTEGER_CST
40786 || (lunroll
= tree_to_shwi (expr
)) < 0
40787 || lunroll
>= USHRT_MAX
)
40789 error_at (location
, "%<#pragma GCC unroll%> requires an"
40790 " assignment-expression that evaluates to a non-negative"
40791 " integral constant less than %u", USHRT_MAX
);
40796 unroll
= (unsigned short)lunroll
;
40800 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40804 /* Normal parsing of a pragma token. Here we can (and must) use the
40808 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
40810 cp_token
*pragma_tok
;
40815 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
40816 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
40817 parser
->lexer
->in_pragma
= true;
40819 id
= cp_parser_pragma_kind (pragma_tok
);
40820 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
40821 cp_ensure_no_omp_declare_simd (parser
);
40824 case PRAGMA_GCC_PCH_PREPROCESS
:
40825 error_at (pragma_tok
->location
,
40826 "%<#pragma GCC pch_preprocess%> must be first");
40829 case PRAGMA_OMP_BARRIER
:
40832 case pragma_compound
:
40833 cp_parser_omp_barrier (parser
, pragma_tok
);
40836 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40837 "used in compound statements", "omp barrier");
40844 case PRAGMA_OMP_DEPOBJ
:
40847 case pragma_compound
:
40848 cp_parser_omp_depobj (parser
, pragma_tok
);
40851 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40852 "used in compound statements", "omp depobj");
40859 case PRAGMA_OMP_FLUSH
:
40862 case pragma_compound
:
40863 cp_parser_omp_flush (parser
, pragma_tok
);
40866 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
40867 "used in compound statements", "omp flush");
40874 case PRAGMA_OMP_TASKWAIT
:
40877 case pragma_compound
:
40878 cp_parser_omp_taskwait (parser
, pragma_tok
);
40881 error_at (pragma_tok
->location
,
40882 "%<#pragma %s%> may only be used in compound statements",
40890 case PRAGMA_OMP_TASKYIELD
:
40893 case pragma_compound
:
40894 cp_parser_omp_taskyield (parser
, pragma_tok
);
40897 error_at (pragma_tok
->location
,
40898 "%<#pragma %s%> may only be used in compound statements",
40906 case PRAGMA_OMP_CANCEL
:
40909 case pragma_compound
:
40910 cp_parser_omp_cancel (parser
, pragma_tok
);
40913 error_at (pragma_tok
->location
,
40914 "%<#pragma %s%> may only be used in compound statements",
40922 case PRAGMA_OMP_CANCELLATION_POINT
:
40923 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
40926 case PRAGMA_OMP_THREADPRIVATE
:
40927 cp_parser_omp_threadprivate (parser
, pragma_tok
);
40930 case PRAGMA_OMP_DECLARE
:
40931 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
40933 case PRAGMA_OACC_DECLARE
:
40934 cp_parser_oacc_declare (parser
, pragma_tok
);
40937 case PRAGMA_OACC_ENTER_DATA
:
40938 if (context
== pragma_stmt
)
40940 error_at (pragma_tok
->location
,
40941 "%<#pragma %s%> may only be used in compound statements",
40945 else if (context
!= pragma_compound
)
40947 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40950 case PRAGMA_OACC_EXIT_DATA
:
40951 if (context
== pragma_stmt
)
40953 error_at (pragma_tok
->location
,
40954 "%<#pragma %s%> may only be used in compound statements",
40958 else if (context
!= pragma_compound
)
40960 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40963 case PRAGMA_OACC_ROUTINE
:
40964 if (context
!= pragma_external
)
40966 error_at (pragma_tok
->location
,
40967 "%<#pragma acc routine%> must be at file scope");
40970 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
40973 case PRAGMA_OACC_UPDATE
:
40974 if (context
== pragma_stmt
)
40976 error_at (pragma_tok
->location
,
40977 "%<#pragma %s%> may only be used in compound statements",
40981 else if (context
!= pragma_compound
)
40983 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40986 case PRAGMA_OACC_WAIT
:
40987 if (context
== pragma_stmt
)
40989 error_at (pragma_tok
->location
,
40990 "%<#pragma %s%> may only be used in compound statements",
40994 else if (context
!= pragma_compound
)
40996 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
40999 case PRAGMA_OACC_ATOMIC
:
41000 case PRAGMA_OACC_CACHE
:
41001 case PRAGMA_OACC_DATA
:
41002 case PRAGMA_OACC_HOST_DATA
:
41003 case PRAGMA_OACC_KERNELS
:
41004 case PRAGMA_OACC_PARALLEL
:
41005 case PRAGMA_OACC_LOOP
:
41006 case PRAGMA_OMP_ATOMIC
:
41007 case PRAGMA_OMP_CRITICAL
:
41008 case PRAGMA_OMP_DISTRIBUTE
:
41009 case PRAGMA_OMP_FOR
:
41010 case PRAGMA_OMP_MASTER
:
41011 case PRAGMA_OMP_PARALLEL
:
41012 case PRAGMA_OMP_SECTIONS
:
41013 case PRAGMA_OMP_SIMD
:
41014 case PRAGMA_OMP_SINGLE
:
41015 case PRAGMA_OMP_TASK
:
41016 case PRAGMA_OMP_TASKGROUP
:
41017 case PRAGMA_OMP_TASKLOOP
:
41018 case PRAGMA_OMP_TEAMS
:
41019 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41021 stmt
= push_omp_privatization_clauses (false);
41022 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
41023 pop_omp_privatization_clauses (stmt
);
41026 case PRAGMA_OMP_REQUIRES
:
41027 return cp_parser_omp_requires (parser
, pragma_tok
);
41029 case PRAGMA_OMP_ORDERED
:
41030 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41032 stmt
= push_omp_privatization_clauses (false);
41033 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
41034 pop_omp_privatization_clauses (stmt
);
41037 case PRAGMA_OMP_TARGET
:
41038 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
41040 stmt
= push_omp_privatization_clauses (false);
41041 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
41042 pop_omp_privatization_clauses (stmt
);
41045 case PRAGMA_OMP_END_DECLARE_TARGET
:
41046 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
41049 case PRAGMA_OMP_SECTION
:
41050 error_at (pragma_tok
->location
,
41051 "%<#pragma omp section%> may only be used in "
41052 "%<#pragma omp sections%> construct");
41057 if (context
== pragma_external
)
41059 error_at (pragma_tok
->location
,
41060 "%<#pragma GCC ivdep%> must be inside a function");
41063 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
41064 unsigned short unroll
;
41065 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41066 if (tok
->type
== CPP_PRAGMA
41067 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
41069 tok
= cp_lexer_consume_token (parser
->lexer
);
41070 unroll
= cp_parser_pragma_unroll (parser
, tok
);
41071 tok
= cp_lexer_peek_token (the_parser
->lexer
);
41075 if (tok
->type
!= CPP_KEYWORD
41076 || (tok
->keyword
!= RID_FOR
41077 && tok
->keyword
!= RID_WHILE
41078 && tok
->keyword
!= RID_DO
))
41080 cp_parser_error (parser
, "for, while or do statement expected");
41083 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
41087 case PRAGMA_UNROLL
:
41089 if (context
== pragma_external
)
41091 error_at (pragma_tok
->location
,
41092 "%<#pragma GCC unroll%> must be inside a function");
41095 const unsigned short unroll
41096 = cp_parser_pragma_unroll (parser
, pragma_tok
);
41098 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41099 if (tok
->type
== CPP_PRAGMA
41100 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
41102 tok
= cp_lexer_consume_token (parser
->lexer
);
41103 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
41104 tok
= cp_lexer_peek_token (the_parser
->lexer
);
41108 if (tok
->type
!= CPP_KEYWORD
41109 || (tok
->keyword
!= RID_FOR
41110 && tok
->keyword
!= RID_WHILE
41111 && tok
->keyword
!= RID_DO
))
41113 cp_parser_error (parser
, "for, while or do statement expected");
41116 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
41121 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
41122 c_invoke_pragma_handler (id
);
41126 cp_parser_error (parser
, "expected declaration specifiers");
41130 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41134 /* The interface the pragma parsers have to the lexer. */
41137 pragma_lex (tree
*value
, location_t
*loc
)
41139 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
41140 enum cpp_ttype ret
= tok
->type
;
41142 *value
= tok
->u
.value
;
41144 *loc
= tok
->location
;
41146 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
41148 else if (ret
== CPP_STRING
)
41149 *value
= cp_parser_string_literal (the_parser
, false, false);
41152 if (ret
== CPP_KEYWORD
)
41154 cp_lexer_consume_token (the_parser
->lexer
);
41161 /* External interface. */
41163 /* Parse one entire translation unit. */
41166 c_parse_file (void)
41168 static bool already_called
= false;
41170 if (already_called
)
41171 fatal_error (input_location
,
41172 "inter-module optimizations not implemented for C++");
41173 already_called
= true;
41175 the_parser
= cp_parser_new ();
41176 push_deferring_access_checks (flag_access_control
41177 ? dk_no_deferred
: dk_no_check
);
41178 cp_parser_translation_unit (the_parser
);
41181 finish_translation_unit ();
41184 /* Create an identifier for a generic parameter type (a synthesized
41185 template parameter implied by `auto' or a concept identifier). */
41187 static GTY(()) int generic_parm_count
;
41189 make_generic_type_name ()
41192 sprintf (buf
, "auto:%d", ++generic_parm_count
);
41193 return get_identifier (buf
);
41196 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41197 (creating a new template parameter list if necessary). Returns the newly
41198 created template type parm. */
41201 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
41203 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
41205 /* Before committing to modifying any scope, if we're in an
41206 implicit template scope, and we're trying to synthesize a
41207 constrained parameter, try to find a previous parameter with
41208 the same name. This is the same-type rule for abbreviated
41209 function templates.
41211 NOTE: We can generate implicit parameters when tentatively
41212 parsing a nested name specifier, only to reject that parse
41213 later. However, matching the same template-id as part of a
41214 direct-declarator should generate an identical template
41215 parameter, so this rule will merge them. */
41216 if (parser
->implicit_template_scope
&& constr
)
41218 tree t
= parser
->implicit_template_parms
;
41221 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
41223 tree d
= TREE_VALUE (t
);
41224 if (TREE_CODE (d
) == PARM_DECL
)
41225 /* Return the TEMPLATE_PARM_INDEX. */
41226 d
= DECL_INITIAL (d
);
41229 t
= TREE_CHAIN (t
);
41233 /* We are either continuing a function template that already contains implicit
41234 template parameters, creating a new fully-implicit function template, or
41235 extending an existing explicit function template with implicit template
41238 cp_binding_level
*const entry_scope
= current_binding_level
;
41240 bool become_template
= false;
41241 cp_binding_level
*parent_scope
= 0;
41243 if (parser
->implicit_template_scope
)
41245 gcc_assert (parser
->implicit_template_parms
);
41247 current_binding_level
= parser
->implicit_template_scope
;
41251 /* Roll back to the existing template parameter scope (in the case of
41252 extending an explicit function template) or introduce a new template
41253 parameter scope ahead of the function parameter scope (or class scope
41254 in the case of out-of-line member definitions). The function scope is
41255 added back after template parameter synthesis below. */
41257 cp_binding_level
*scope
= entry_scope
;
41259 while (scope
->kind
== sk_function_parms
)
41261 parent_scope
= scope
;
41262 scope
= scope
->level_chain
;
41264 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
41266 /* If not defining a class, then any class scope is a scope level in
41267 an out-of-line member definition. In this case simply wind back
41268 beyond the first such scope to inject the template parameter list.
41269 Otherwise wind back to the class being defined. The latter can
41270 occur in class member friend declarations such as:
41276 friend void A::foo (auto);
41279 The template parameter list synthesized for the friend declaration
41280 must be injected in the scope of 'B'. This can also occur in
41281 erroneous cases such as:
41287 void B::foo (auto) {}
41290 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41291 but, nevertheless, the template parameter list synthesized for the
41292 declarator should be injected into the scope of 'A' as if the
41293 ill-formed template was specified explicitly. */
41295 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
41297 parent_scope
= scope
;
41298 scope
= scope
->level_chain
;
41302 current_binding_level
= scope
;
41304 if (scope
->kind
!= sk_template_parms
41305 || !function_being_declared_is_template_p (parser
))
41307 /* Introduce a new template parameter list for implicit template
41310 become_template
= true;
41312 parser
->implicit_template_scope
41313 = begin_scope (sk_template_parms
, NULL
);
41315 ++processing_template_decl
;
41317 parser
->fully_implicit_function_template_p
= true;
41318 ++parser
->num_template_parameter_lists
;
41322 /* Synthesize implicit template parameters at the end of the explicit
41323 template parameter list. */
41325 gcc_assert (current_template_parms
);
41327 parser
->implicit_template_scope
= scope
;
41329 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41330 parser
->implicit_template_parms
41331 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
41335 /* Synthesize a new template parameter and track the current template
41336 parameter chain with implicit_template_parms. */
41338 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
41339 tree synth_id
= make_generic_type_name ();
41340 tree synth_tmpl_parm
;
41341 bool non_type
= false;
41343 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
41345 = finish_template_type_parm (class_type_node
, synth_id
);
41346 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
41348 = finish_constrained_template_template_parm (proto
, synth_id
);
41351 synth_tmpl_parm
= copy_decl (proto
);
41352 DECL_NAME (synth_tmpl_parm
) = synth_id
;
41356 // Attach the constraint to the parm before processing.
41357 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
41358 TREE_TYPE (node
) = constr
;
41360 = process_template_parm (parser
->implicit_template_parms
,
41363 /*non_type=*/non_type
,
41364 /*param_pack=*/false);
41366 // Chain the new parameter to the list of implicit parameters.
41367 if (parser
->implicit_template_parms
)
41368 parser
->implicit_template_parms
41369 = TREE_CHAIN (parser
->implicit_template_parms
);
41371 parser
->implicit_template_parms
= new_parm
;
41373 tree new_decl
= get_local_decls ();
41375 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41376 new_decl
= DECL_INITIAL (new_decl
);
41378 /* If creating a fully implicit function template, start the new implicit
41379 template parameter list with this synthesized type, otherwise grow the
41380 current template parameter list. */
41382 if (become_template
)
41384 parent_scope
->level_chain
= current_binding_level
;
41386 tree new_parms
= make_tree_vec (1);
41387 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
41388 current_template_parms
= tree_cons (size_int (processing_template_decl
),
41389 new_parms
, current_template_parms
);
41393 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
41394 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
41395 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
41396 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
41399 // If the new parameter was constrained, we need to add that to the
41400 // constraints in the template parameter list.
41401 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
41403 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
41404 reqs
= conjoin_constraints (reqs
, req
);
41405 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
41408 current_binding_level
= entry_scope
;
41413 /* Finish the declaration of a fully implicit function template. Such a
41414 template has no explicit template parameter list so has not been through the
41415 normal template head and tail processing. synthesize_implicit_template_parm
41416 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41417 provided if the declaration is a class member such that its template
41418 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41419 form is returned. Otherwise NULL_TREE is returned. */
41422 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
41424 gcc_assert (parser
->fully_implicit_function_template_p
);
41426 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
41427 && DECL_VIRTUAL_P (member_decl_opt
))
41429 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
41430 "implicit templates may not be %<virtual%>");
41431 DECL_VIRTUAL_P (member_decl_opt
) = false;
41434 if (member_decl_opt
)
41435 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
41436 end_template_decl ();
41438 parser
->fully_implicit_function_template_p
= false;
41439 parser
->implicit_template_parms
= 0;
41440 parser
->implicit_template_scope
= 0;
41441 --parser
->num_template_parameter_lists
;
41443 return member_decl_opt
;
41446 /* Like finish_fully_implicit_template, but to be used in error
41447 recovery, rearranging scopes so that we restore the state we had
41448 before synthesize_implicit_template_parm inserted the implement
41449 template parms scope. */
41452 abort_fully_implicit_template (cp_parser
*parser
)
41454 cp_binding_level
*return_to_scope
= current_binding_level
;
41456 if (parser
->implicit_template_scope
41457 && return_to_scope
!= parser
->implicit_template_scope
)
41459 cp_binding_level
*child
= return_to_scope
;
41460 for (cp_binding_level
*scope
= child
->level_chain
;
41461 scope
!= parser
->implicit_template_scope
;
41462 scope
= child
->level_chain
)
41464 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
41465 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
41466 current_binding_level
= parser
->implicit_template_scope
;
41469 return_to_scope
= return_to_scope
->level_chain
;
41471 finish_fully_implicit_template (parser
, NULL
);
41473 gcc_assert (current_binding_level
== return_to_scope
);
41476 /* Helper function for diagnostics that have complained about things
41477 being used with 'extern "C"' linkage.
41479 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41482 maybe_show_extern_c_location (void)
41484 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
41485 inform (the_parser
->innermost_linkage_specification_location
,
41486 "%<extern \"C\"%> linkage started here");
41489 #include "gt-cp-parser.h"